English Amiga Board


Go Back   English Amiga Board > Coders > Coders. General

 
 
Thread Tools
Old 28 December 2023, 23:59   #1
Steam Ranger
Registered User
 
Steam Ranger's Avatar
 
Join Date: May 2022
Location: Adelaide, South Australia, Australia
Posts: 208
Amiga malloc() function

How does the malloc() function work on Amiga? Can I make my program not use the inbuilt one and instead write a custom one?
Steam Ranger is offline  
Old 29 December 2023, 00:14   #2
Samurai_Crow
Total Chaos forever!
 
Samurai_Crow's Avatar
 
Join Date: Aug 2007
Location: Waterville, MN, USA
Age: 49
Posts: 2,187
AllocMem() is the memory allocation routine on the Amiga. C uses malloc() to wrap it or one of several other wrappers that use it. One of the more common wrappers is the memory pool functions added in AmigaOS 3.x but if you use an older version of the OS, writing a wrapper is recommended.
Samurai_Crow is offline  
Old 29 December 2023, 02:28   #3
Steam Ranger
Registered User
 
Steam Ranger's Avatar
 
Join Date: May 2022
Location: Adelaide, South Australia, Australia
Posts: 208
Quote:
Originally Posted by Samurai_Crow View Post
AllocMem() is the memory allocation routine on the Amiga. C uses malloc() to wrap it or one of several other wrappers that use it. One of the more common wrappers is the memory pool functions added in AmigaOS 3.x but if you use an older version of the OS, writing a wrapper is recommended.
I am using Workbench 3.1, and my program is linking in a wrapper automatically. I want to know how I can not link it so I can write my own wrapper to work on WB 1.3 and 3.1.
Steam Ranger is offline  
Old 29 December 2023, 02:50   #4
Samurai_Crow
Total Chaos forever!
 
Samurai_Crow's Avatar
 
Join Date: Aug 2007
Location: Waterville, MN, USA
Age: 49
Posts: 2,187
If you link your version of malloc() before the c runtime, it will redirect to your allocator. You will have to wrap AllocMem() from Exec.library though.
Samurai_Crow is offline  
Old 29 December 2023, 10:49   #5
Olaf Barthel
Registered User
 
Join Date: Aug 2010
Location: Germany
Posts: 532
Quote:
Originally Posted by Steam Ranger View Post
How does the malloc() function work on Amiga? Can I make my program not use the inbuilt one and instead write a custom one?
Why do you need to replace the malloc() function? Which problem are you trying to solve?

As Samurai_Crow already mentioned, replacing the malloc() function by one of your own design will replace it for the entire 'C' runtime library, not just for the use of your own code.

The startup code for your program is likely to use malloc() and also free() before your own program's main() function is called. This means that your replacements of malloc() and free() have to work before your main() function is called and after exit() has been called (either by your code calling it, or the startup code).

And it gets even more complicated very quickly. You not only have to replace malloc() and free(), you will likely have to replace realloc() and calloc(), too, and find a way to release any memory allocated through realloc(), calloc() and malloc() which has not been freed yet, before your program exits since the startup code no longer takes care of it. This is a huge challenge all by itself.

Reimplementing the 'C' runtime library memory management functions and the infrastructure they need is really tough because you have to make it work exactly like it says in the specifications which describe them. I went through that kind of work almost 20 years ago when I built clib2, because I wanted to port Samba 2.0.4 to the Amiga, without relying upon ixemul.library, etc. Let's say it was educational, and I'm still finding and fixing my own bugs after all these decades

Please keep in mind that malloc() is not easily replaced by AllocMem(). Unlike AllocMem(), malloc() will remember the size of the allocation made and it will remember if you forgot to release any of the memory allocated through it, releasing it automatically when your program exits.


There's an alternative option if your own code makes use of malloc() and free() only, and you do not use calloc() or realloc(). You can write your own my_malloc() and my_free() functions and then use macros to redirect the calls to your own functions, like so:

Code:
#include <stddef.h>

extern void * my_malloc(size_t size);
extern void my_free(void * memory);

#define malloc(size) my_malloc(size)
#define free(memory) my_free(memory)
Use atexit() to install a cleanup function for the my_malloc() allocations which will release any memory not freed yet.
Olaf Barthel is offline  
Old 29 December 2023, 13:31   #6
No.3
Registered User
 
Join Date: Sep 2022
Location: Switzerland
Posts: 115
Quote:
Originally Posted by Olaf Barthel View Post
Unlike AllocMem(), malloc() will remember the size of the allocation made...
at least for this case AllocVec() is your friend.
No.3 is offline  
Old 29 December 2023, 14:56   #7
Thomas Richter
Registered User
 
Join Date: Jan 2019
Location: Germany
Posts: 3,233
Quote:
Originally Posted by Steam Ranger View Post
How does the malloc() function work on Amiga?
That is compiler dependent. It is typically a function in the C standard library that is linked to your code, and it typically uses some kind of local memory pool provided by the compiler library that also includes other memory management functions from ANSI C, such as realloc or calloc. Or rather, the memory pool provided by the C library is made such that it allows realloc.
Quote:
Originally Posted by Steam Ranger View Post
Can I make my program not use the inbuilt one and instead write a custom one?
Well, if you are using other functions from the C standard library, that is at least not advisable. If you are creating a program solely for the Amiga that does not use any ANSI-C functions, and that does not use the C startup code, then you should probably use AllocMem() and friends instead of replacing malloc.
Thomas Richter 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
Different Results for malloc(0) Without/With Optimisations with VBCC tygre Coders. C/C++ 20 15 February 2023 23:33
AllocSysObject vs malloc? Sim085 Coders. C/C++ 12 17 December 2022 17:47
fd2pragma inline entry for function that returns pointer to another function Sim085 Coders. C/C++ 11 02 March 2022 10:38
Is there some compiler providing resource tracking for malloc/free/FILE* handles? jotd Coders. C/C++ 3 29 December 2018 11:58
Function 2009 amiga demo releases mihcael Amiga scene 2 08 October 2009 09:01

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 05:31.

Top

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