English Amiga Board


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

 
 
Thread Tools
Old 22 September 2019, 21:15   #1
sparhawk
Registered User
 
sparhawk's Avatar
 
Join Date: Sep 2019
Location: Essen/Germany
Age: 55
Posts: 463
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, 21:36   #2
Samurai_Crow
Total Chaos forever!
 
Samurai_Crow's Avatar
 
Join Date: Aug 2007
Location: Waterville, MN, USA
Age: 49
Posts: 2,186
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, 00:33   #3
Bruce Abbott
Registered User
 
Bruce Abbott's Avatar
 
Join Date: Mar 2018
Location: Hastings, New Zealand
Posts: 2,546
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, 00:50   #4
Samurai_Crow
Total Chaos forever!
 
Samurai_Crow's Avatar
 
Join Date: Aug 2007
Location: Waterville, MN, USA
Age: 49
Posts: 2,186
The buffered iostream routines are pulling in a lot more dependencies at link time.
Samurai_Crow is offline  
Old 23 September 2019, 12:07   #5
bebbo
bye
 
Join Date: Jun 2016
Location: Some / Where
Posts: 680
* don't use iostreams
* and if you don't use exceptions add -fno-exceptions
bebbo is offline  
Old 23 September 2019, 13:08   #6
sparhawk
Registered User
 
sparhawk's Avatar
 
Join Date: Sep 2019
Location: Essen/Germany
Age: 55
Posts: 463
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, 13:39   #7
hooverphonique
ex. demoscener "Bigmama"
 
Join Date: Jun 2012
Location: Fyn / Denmark
Posts: 1,624
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, 15:35   #8
bebbo
bye
 
Join Date: Jun 2016
Location: Some / Where
Posts: 680
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, 16:02   #9
phx
Natteravn
 
phx's Avatar
 
Join Date: Nov 2009
Location: Herford / Germany
Posts: 2,496
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, 16:09   #10
sparhawk
Registered User
 
sparhawk's Avatar
 
Join Date: Sep 2019
Location: Essen/Germany
Age: 55
Posts: 463
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, 16:13   #11
sparhawk
Registered User
 
sparhawk's Avatar
 
Join Date: Sep 2019
Location: Essen/Germany
Age: 55
Posts: 463
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, 19:25   #12
nogginthenog
Amigan
 
Join Date: Feb 2012
Location: London
Posts: 1,309
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, 21:22   #13
bebbo
bye
 
Join Date: Jun 2016
Location: Some / Where
Posts: 680
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, 21:41   #14
sparhawk
Registered User
 
sparhawk's Avatar
 
Join Date: Sep 2019
Location: Essen/Germany
Age: 55
Posts: 463
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, 19:36   #15
bebbo
bye
 
Join Date: Jun 2016
Location: Some / Where
Posts: 680
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, 18:46   #16
sparhawk
Registered User
 
sparhawk's Avatar
 
Join Date: Sep 2019
Location: Essen/Germany
Age: 55
Posts: 463
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, 20:30   #17
bebbo
bye
 
Join Date: Jun 2016
Location: Some / Where
Posts: 680
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, 07:09   #18
sparhawk
Registered User
 
sparhawk's Avatar
 
Join Date: Sep 2019
Location: Essen/Germany
Age: 55
Posts: 463
OK. Thanks! Then I stick to mcrt.
sparhawk is offline  
Old 28 September 2019, 06:10   #19
Bruce Abbott
Registered User
 
Bruce Abbott's Avatar
 
Join Date: Mar 2018
Location: Hastings, New Zealand
Posts: 2,546
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, 12:21   #20
Hewitson
Registered User
 
Hewitson's Avatar
 
Join Date: Feb 2007
Location: Melbourne, Australia
Age: 41
Posts: 3,772
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 17:29
DOpus5 lister handling daxb Coders. Scripting 0 05 December 2015 15:55
Program like NoFastMem but which forces all allocations to be MEMF_24BITDMA? mark_k request.Apps 2 03 February 2013 20:54
Cookie handling alexh project.EAB 1 22 October 2007 21: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 10:15.

Top

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