28 December 2023, 23:59 | #1 |
Registered User
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?
|
29 December 2023, 00:14 | #2 |
Total Chaos forever!
Join Date: Aug 2007
Location: Waterville, MN, USA
Age: 49
Posts: 2,200
|
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.
|
29 December 2023, 02:28 | #3 | |
Registered User
Join Date: May 2022
Location: Adelaide, South Australia, Australia
Posts: 208
|
Quote:
|
|
29 December 2023, 02:50 | #4 |
Total Chaos forever!
Join Date: Aug 2007
Location: Waterville, MN, USA
Age: 49
Posts: 2,200
|
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.
|
29 December 2023, 10:49 | #5 | |
Registered User
Join Date: Aug 2010
Location: Germany
Posts: 537
|
Quote:
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) |
|
29 December 2023, 13:31 | #6 |
Registered User
Join Date: Sep 2022
Location: Switzerland
Posts: 120
|
|
29 December 2023, 14:56 | #7 |
Registered User
Join Date: Jan 2019
Location: Germany
Posts: 3,322
|
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. 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.
|
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 |
|
|