14 December 2022, 12:06 | #1 |
Registered User
Join Date: Apr 2009
Location: N/A
Posts: 962
|
AllocSysObject vs malloc?
What is the difference between AllocSysObject and malloc?
I want to create a List to store a custom list of items. The examples I found use AllocSysObjectTags(ASOT_LIST, TAG_END). However how is this different from using malloc(sizeof(struct List))? |
14 December 2022, 12:40 | #2 |
Natteravn
Join Date: Nov 2009
Location: Herford / Germany
Posts: 2,500
|
AllocSysObject() knows the size and memory type requirements of your object. So it is guaranteed to be compatible with future AmigaOS versions and should always be prefered.
|
14 December 2022, 13:44 | #3 |
Registered User
Join Date: Jan 2002
Location: Germany
Posts: 7,002
|
AllocSysObject initialises the object so that you can immediately use it. malloc just gives you a piece of memory, it does not even fill it with zeroes.
Allocations done by malloc are guaranteed to be freed at the end of your program, even if you forget it. Allocations using any of the OS Alloc... functions are not, unless you do it. |
14 December 2022, 13:52 | #4 |
Registered User
Join Date: Apr 2009
Location: N/A
Posts: 962
|
But how is the end result different between the following two:
Code:
struct List *mylist = AllocSysObjectTags(ASOT_LIST, TAG_END); struct Node *mynode = AllocSysObjectTags( ASOT_NODE, ASONODE_Size, sizeof(struct Node), ASONODE_Type, NAMENODE_ID, TAG_END); AddHead(mylist, mynode); Code:
struct List *mylist = malloc(sizeof(struct List)); struct Node *mynode = malloc(sizeof(struct Node)); mynode->ln_Type = NAMENODE_ID; AddHead(mylist, mynode); I am using example here for reference: https://wiki.amigaos.net/wiki/Exec_L...c_List_Example |
14 December 2022, 13:58 | #5 |
Registered User
Join Date: Jan 2019
Location: Germany
Posts: 3,233
|
This must be some form of Os4-ism as the classic AmigaOs does not have this function (though some related functions of similar purpose in the dos.library and the graphics.library). Frankly, a "struct List" I would never really allocate, unless it has have a life time that should extend beyond the end of the task executing the program or I need some other form of dynamic life time. It is a tiny, well documented structure that, if it would ever change its size, break many many implementations. mallcoc() is a function the C library of your compiler offers. How exactly it is implemented is up to the compiler vendor (or the vendor of the C library of the compiler). I would guess that it is potentially performing better than AllocMem(), and probably performing as good as the memory-pool based allocations of exec, and - hopefully - even performing better than this one. However, as others say, malloc() and the memory it allocates is released (as a service) by the shutdown code of the C library. That is in moist cases what you want, but not always.
|
14 December 2022, 14:04 | #6 | ||
Registered User
Join Date: Apr 2009
Location: N/A
Posts: 962
|
So is malloc more efficient? I would assuming clearing memory would take some extra time?
Quote:
Quote:
Last edited by Sim085; 14 December 2022 at 14:10. |
||
14 December 2022, 14:28 | #7 | |||
Registered User
Join Date: Jan 2019
Location: Germany
Posts: 3,233
|
Quote:
So, as always, "it depends". Quote:
Quote:
That's understood. As others say, there is no "right or wrong" here, it depends on what you want to do, really. malloc() allocates "conceptually" memory "on the heap", which is a term defined by the C language, and "the heap" ceases to exist after program termination. The Os memory allocation functions allocate "system memory" (for whatever that means) and this memory will stay allocated independent of the task that allocated it. Technically, "the heap" is a junk of memory that is also allocated "as system memory", though the C library ensures that upon program termination, this piece of system memory establishing "the heap" is returned to the system. (Or at least, this is a typical implementation of malloc(), though not necessarily the only possible one). |
|||
14 December 2022, 14:58 | #8 |
Alien Bleed
Join Date: Aug 2022
Location: UK
Posts: 4,178
|
Think of AllocSysObject as a factory. It knows the size requirements and sensible initialisation requirements. I've no doubt it's probably slower than malloc but if you are doing this sort of resources management inside any loop time critical enough to matter you should probably reconsider your design.
|
14 December 2022, 16:38 | #9 | ||
Natteravn
Join Date: Nov 2009
Location: Herford / Germany
Posts: 2,500
|
Quote:
Quote:
On the other hand, for really portable C programs, use clib and mlib functions only. Also avoid extended clib functions (like POSIX), which are not required to be in the clib according to ISO-C (for example prefer fopen over open). |
||
17 December 2022, 14:48 | #10 |
bye
Join Date: Jun 2016
Location: Some / Where
Posts: 680
|
IMHO using Alloc/Free-Vec/Mem is then a bad choice, if your program needs more than initial allocations, since it often leads to a fragmented system memory list and gets slower and slower.
Using Allocate/Deallocate (V39+) is a better choice, but also means you have to hazzle with that. Using malloc/free is hazzlefree. And with a proper library, all memory is freed on program exit. Sticking to standards also eases the way to reuse code written for/on other platforms. There is still room for using AllocMem & Co, e.g. if you need to allocate some memory and pass it to a different task which takes over the ownership of the transfered data. Then it's a MUST to use the system functions. |
17 December 2022, 14:58 | #11 | |
Alien Bleed
Join Date: Aug 2022
Location: UK
Posts: 4,178
|
Quote:
|
|
17 December 2022, 15:55 | #12 |
Registered User
Join Date: Jan 2019
Location: Germany
Posts: 3,233
|
This is of course a matter of their implementation. Typically, malloc() goes to a memory pool which is only enlarged from the Os if it runs dry. Thus, small allocations are pooled and thus do not impact the fragmentation of Os memory.
|
17 December 2022, 17:47 | #13 | |
Registered User
Join Date: Jul 2020
Location: Porsgrunn/Norway
Posts: 1
|
Quote:
I'm still on team malloc, though.. -Morten- |
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Thread Tools | |
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Is there some compiler providing resource tracking for malloc/free/FILE* handles? | jotd | Coders. C/C++ | 3 | 29 December 2018 11:58 |
[SAS/C] Making malloc() allocate in fast RAM instead of chip RAM? | 8bitbubsy | Coders. General | 3 | 07 August 2011 07:06 |
|
|