English Amiga Board


Go Back   English Amiga Board > Coders > Coders. Language > Coders. C/C++

 
 
Thread Tools
Old 22 September 2019, 22:15   #1
sparhawk
Registered User

sparhawk's Avatar
 
Join Date: Sep 2019
Location: Essen/Germany
Age: 50
Posts: 64
Handling dynamic memory allocations

Programming for a classic Amiga is quite different than on more modern machines. Obviously memory size plays a much stronger role, because of the more limited memory and of course thetr is also the issue of differentiating between chip and fast memory.

So when using a toolchain like gcc, how do you tackle the problem? Do you avoid using std containers? Vector is rather useful but the standard implementation is probably a bit problematic when writing more complex programs. I did some test and when I was using the std::string class the size of my executable was jump from 20kb to whooping 170kb, which is quite a lot.
So I assume that I have to do my own memory handling and avoid using the std containers? Or how do you deal with that?
sparhawk is offline  
Old 22 September 2019, 22:36   #2
Samurai_Crow
Total Chaos forever!

Samurai_Crow's Avatar
 
Join Date: Aug 2007
Location: Ft. Collins, CO USA
Age: 45
Posts: 1,307
Send a message via Yahoo to Samurai_Crow
Bebbo is trying to get GCC to use NewLib as its C runtime. Aros already has an ArosC.library in its kernel. Using template specialization to wrap LibStdC++ runtimes might help in the future.
Samurai_Crow is offline  
Old 23 September 2019, 01:33   #3
Bruce Abbott
Registered User

Bruce Abbott's Avatar
 
Join Date: Mar 2018
Location: Hastings, New Zealand
Posts: 281
Quote:
Originally Posted by sparhawk View Post
when I was using the std::string class the size of my executable was jump from 20kb to whooping 170kb
And the problem is?
Bruce Abbott is offline  
Old 23 September 2019, 01:50   #4
Samurai_Crow
Total Chaos forever!

Samurai_Crow's Avatar
 
Join Date: Aug 2007
Location: Ft. Collins, CO USA
Age: 45
Posts: 1,307
Send a message via Yahoo to Samurai_Crow
The buffered iostream routines are pulling in a lot more dependencies at link time.
Samurai_Crow is offline  
Old 23 September 2019, 13:07   #5
bebbo
botcher

 
Join Date: Jun 2016
Location: Hamburg/Germany
Posts: 426
* don't use iostreams
* and if you don't use exceptions add -fno-exceptions
bebbo is offline  
Old 23 September 2019, 14:08   #6
sparhawk
Registered User

sparhawk's Avatar
 
Join Date: Sep 2019
Location: Essen/Germany
Age: 50
Posts: 64
Quote:
Originally Posted by Bruce Abbott View Post
And the problem is?

It depends. An Amiga 1000 has (usually) 512kb, which is not much. Fresh from the factory it even has only 256kb but I guess that not many people actually use it as such.


So developing a game I would think that I rather use the memory for gfx and sound instead of io routines.


Anyway, I was just experimenting, to see how much overhead certain things will add. For example, using "new" instead of "malloc" also adds 70kb of additional size, which I also didn't expect.
When I created a barebone binary it had about 350bytes, using "printf" it increases to 20kb, which is IMO acceptable, because it's needed for debugging purposes. string functions are convenient, but considering the overhead I wouldn't use them for game code.


Quote:
Originally Posted by bebbo View Post
* don't use iostreams
* and if you don't use exceptions add -fno-exceptions

Does "no-exception" disable any exception or only prevents the standard functions not throwing?
So when I throw and catch within my own code, this would still work, or is it also disabled?
sparhawk is offline  
Old 23 September 2019, 14:39   #7
hooverphonique
ex. demoscener "Bigmama"

 
Join Date: Jun 2012
Location: Fyn / Denmark
Posts: 988
If you're using gcc, you can use stuff like "-ffunction-sections" and "--gc-sections" to have the linker through out unused code - this will likely reduce your executable size.
Link time optimization ("-lto") may help further.
hooverphonique is offline  
Old 23 September 2019, 16:35   #8
bebbo
botcher

 
Join Date: Jun 2016
Location: Hamburg/Germany
Posts: 426
Quote:
Originally Posted by sparhawk View Post
It depends. An Amiga 1000 has (usually) 512kb, which is not much. Fresh from the factory it even has only 256kb but I guess that not many people actually use it as such.


So developing a game I would think that I rather use the memory for gfx and sound instead of io routines.


Anyway, I was just experimenting, to see how much overhead certain things will add. For example, using "new" instead of "malloc" also adds 70kb of additional size, which I also didn't expect.

new throws an exception by default if the memory is exhausted.



Quote:
Originally Posted by sparhawk View Post
When I created a barebone binary it had about 350bytes, using "printf" it increases to 20kb, which is IMO acceptable, because it's needed for debugging purposes. string functions are convenient, but considering the overhead I wouldn't use them for game code.

You aren't using the -noixemul option.



Quote:
Originally Posted by sparhawk View Post
Does "no-exception" disable any exception or only prevents the standard functions not throwing?
So when I throw and catch within my own code, this would still work, or is it also disabled?

Guess the the exception stuff get's linked plus it won't work, since the startup does not initialize it properly.
bebbo is offline  
Old 23 September 2019, 17:02   #9
phx
Natteravn

phx's Avatar
 
Join Date: Nov 2009
Location: Herford / Germany
Posts: 1,479
Quote:
Originally Posted by sparhawk View Post
It depends. An Amiga 1000 has (usually) 512kb, which is not much.
(...)
Anyway, I was just experimenting, to see how much overhead certain things will add. For example, using "new" instead of "malloc" also adds 70kb of additional size, which I also didn't expect.
You are asking for trouble when using C++ in such a low memory configuration. What's wrong with C?
phx is offline  
Old 23 September 2019, 17:09   #10
sparhawk
Registered User

sparhawk's Avatar
 
Join Date: Sep 2019
Location: Essen/Germany
Age: 50
Posts: 64
Quote:
Originally Posted by bebbo View Post
new throws an exception by default if the memory is exhausted.

Yes, but this is a situation that is hard to handle anyway. And if memory gets exhausted, there is not much I can do about it, so it doesn't matter if the game crashes.


Quote:
You aren't using the -noixemul option.

What does this do? Is there some overview about all these options and when to use them?


Quote:
Guess the the exception stuff get's linked plus it won't work, since the startup does not initialize it properly.

Well, if it is linked anyway, then this option doesn't make much sense, right?
sparhawk is offline  
Old 23 September 2019, 17:13   #11
sparhawk
Registered User

sparhawk's Avatar
 
Join Date: Sep 2019
Location: Essen/Germany
Age: 50
Posts: 64
Quote:
Originally Posted by phx View Post
You are asking for trouble when using C++ in such a low memory configuration. What's wrong with C?

Currently I'm only experimenting with the memory, so these are just findings, that I consider interesting.


I don't really want to work with plain C, because C++ adds a lot of other stuff, which is useful and convenient. So knowing the limitations, I can take advantage of the C++ features that I want and can still fall back in some cases if it is necessary.


I prefer C++ because of it's inherent typesafety objects and overloading, which IMO makes the code much clearer. Also templates can be very useful, so all in all, even if one can not use the full feature set of an advanced C++ compiler, IMO there is enough benefit to still use it.
sparhawk is offline  
Old 23 September 2019, 20:25   #12
nogginthenog
Amigan

 
Join Date: Feb 2012
Location: London
Posts: 863
Quote:
Originally Posted by sparhawk View Post
What does this do? Is there some overview about all these options and when to use them?
-noixemul tells GCC to not use the ixemul.library.

ixemul is a POSIX compatibility layer. It lets you easily port Unix style software.
The downside is that it's quite memory hungry and sometimes drops Amiga conventions for compatibility.

It was originally part of the GeekGadgets project (as was GCC) which includes all the userland tools that you would normally find on Linux/BSD etc.
This includes things like X Windows. I've had GIMP running on my Amiga.
nogginthenog is offline  
Old 23 September 2019, 22:22   #13
bebbo
botcher

 
Join Date: Jun 2016
Location: Hamburg/Germany
Posts: 426
Quote:
Originally Posted by nogginthenog View Post
-noixemul tells GCC to not use the ixemul.library.

ixemul is a POSIX compatibility layer. It lets you easily port Unix style software.
The downside is that it's quite memory hungry and sometimes drops Amiga conventions for compatibility.

It was originally part of the GeekGadgets project (as was GCC) which includes all the userland tools that you would normally find on Linux/BSD etc.
This includes things like X Windows. I've had GIMP running on my Amiga.

my gcc toolchain is using a newlib implementation as default, since the ixemul.library did not build that easily... (if someone provides a working build for my toolchain, I'll add it back - maybe even as default)


and that newlib yields larger executables than libnix (aka -noixemul).
bebbo is offline  
Old 23 September 2019, 22:41   #14
sparhawk
Registered User

sparhawk's Avatar
 
Join Date: Sep 2019
Location: Essen/Germany
Age: 50
Posts: 64
When I add " -noixemul" then I get an error:

Code:
/opt/amiga/bin/m68k-amigaos-ld: E:/Programme/msys64/opt/amiga/m68k-amigaos/libnix/lib/ncrt0.o:E:/Programme/msys64/opt/amiga/m68k-amigaos/libnix/lib/ncrt0.o:(.text+0x64): multiple definition of `_exit'; E:/Programme/msys64/opt/amiga/m68k-amigaos/libnix/lib/ncrt0.o:E:/Programme/msys64/opt/amiga/m68k-amigaos/libnix/lib/ncrt0.o:(.text+0x64): first defined here
c
sparhawk is offline  
Old 24 September 2019, 20:36   #15
bebbo
botcher

 
Join Date: Jun 2016
Location: Hamburg/Germany
Posts: 426
Quote:
Originally Posted by sparhawk View Post
When I add " -noixemul" then I get an error:

Code:
/opt/amiga/bin/m68k-amigaos-ld: E:/Programme/msys64/opt/amiga/m68k-amigaos/libnix/lib/ncrt0.o:E:/Programme/msys64/opt/amiga/m68k-amigaos/libnix/lib/ncrt0.o:(.text+0x64): multiple definition of `_exit'; E:/Programme/msys64/opt/amiga/m68k-amigaos/libnix/lib/ncrt0.o:E:/Programme/msys64/opt/amiga/m68k-amigaos/libnix/lib/ncrt0.o:(.text+0x64): first defined here
c

please unhide your command line / makefile
bebbo is offline  
Old 25 September 2019, 19:46   #16
sparhawk
Registered User

sparhawk's Avatar
 
Join Date: Sep 2019
Location: Essen/Germany
Age: 50
Posts: 64
Code:
 m68k-amigaos-g++.exe --save-temps -Wall -pedantic -mcrt=nix13 -O3 -DNDEBUG -noixemul CMakeFiles/testing.dir/main.cpp.o -o ../bin/testing.exe -Wl,--out-implib,../bin/libtesting.dll.a -L/d/src/amiga/projects/testing/../lib/Release -lutils
/opt/amiga/bin/m68k-amigaos-ld: E:/Programme/msys64/opt/amiga/m68k-amigaos/libnix/lib/ncrt0.o:E:/Programme/msys64/opt/amiga/m68k-amigaos/libnix/lib/ncrt0.o:(.text+0x64): multiple definition of `_exit'; E:/Programme/msys64/opt/amiga/m68k-amigaos/libnix/lib/ncrt0.o:E:/Programme/msys64/opt/amiga/m68k-amigaos/libnix/lib/ncrt0.o:(.text+0x64): first defined here
collect2: error: ld returned 1 exit status
sparhawk is offline  
Old 26 September 2019, 21:30   #17
bebbo
botcher

 
Join Date: Jun 2016
Location: Hamburg/Germany
Posts: 426
Quote:
Originally Posted by sparhawk View Post
Code:
 m68k-amigaos-g++.exe --save-temps -Wall -pedantic -mcrt=nix13 -O3 -DNDEBUG -noixemul CMakeFiles/testing.dir/main.cpp.o -o ../bin/testing.exe -Wl,--out-implib,../bin/libtesting.dll.a -L/d/src/amiga/projects/testing/../lib/Release -lutils
/opt/amiga/bin/m68k-amigaos-ld: E:/Programme/msys64/opt/amiga/m68k-amigaos/libnix/lib/ncrt0.o:E:/Programme/msys64/opt/amiga/m68k-amigaos/libnix/lib/ncrt0.o:(.text+0x64): multiple definition of `_exit'; E:/Programme/msys64/opt/amiga/m68k-amigaos/libnix/lib/ncrt0.o:E:/Programme/msys64/opt/amiga/m68k-amigaos/libnix/lib/ncrt0.o:(.text+0x64): first defined here
collect2: error: ld returned 1 exit status

please use only one of
-mcrt=nix13
-noixemul
bebbo is offline  
Old 27 September 2019, 08:09   #18
sparhawk
Registered User

sparhawk's Avatar
 
Join Date: Sep 2019
Location: Essen/Germany
Age: 50
Posts: 64
OK. Thanks! Then I stick to mcrt.
sparhawk is offline  
Old 28 September 2019, 07:10   #19
Bruce Abbott
Registered User

Bruce Abbott's Avatar
 
Join Date: Mar 2018
Location: Hastings, New Zealand
Posts: 281
Quote:
Originally Posted by sparhawk View Post
It depends. An Amiga 1000 has (usually) 512kb, which is not much. Fresh from the factory it even has only 256kb but I guess that not many people actually use it as such.
My first Amiga was an A1000 with 256k. Not for long though. Even 512k was barely enough to do anything useful. But with 2MB of FastRAM...

Back then we were acutely aware of code bloat and went to great lengths to reduce it. I even wrote my own startup code and printf replacement. Today I don't worry about it so much. Anyone who has less than 1MB should get an upgrade.
Bruce Abbott is offline  
Old 28 September 2019, 13:21   #20
Hewitson
Registered User
Hewitson's Avatar
 
Join Date: Feb 2007
Location: Melbourne, Australia
Age: 37
Posts: 3,381
Having more memory isn't an excuse to write shit code. Are you sure you don't work for Microsoft?
Hewitson is offline  
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Similar Threads
Thread Thread Starter Forum Replies Last Post
File Handling in BLitzmode timeslip1974 Coders. Blitz Basic 4 10 September 2019 18:29
DOpus5 lister handling daxb Coders. Scripting 0 05 December 2015 16:55
Program like NoFastMem but which forces all allocations to be MEMF_24BITDMA? mark_k request.Apps 2 03 February 2013 21:54
Cookie handling alexh project.EAB 1 22 October 2007 22:52

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 00:43.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2019, vBulletin Solutions Inc.
Page generated in 0.09766 seconds with 15 queries