English Amiga Board


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

 
 
Thread Tools
Old 14 December 2022, 12:06   #1
Sim085
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))?
Sim085 is offline  
Old 14 December 2022, 12:40   #2
phx
Natteravn
 
phx's Avatar
 
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.
phx is offline  
Old 14 December 2022, 13:44   #3
thomas
Registered User
 
thomas's Avatar
 
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.
thomas is offline  
Old 14 December 2022, 13:52   #4
Sim085
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);
Aren't these two code sets the same? Is AllocSysObject doing anything more than just allocating memory? What risks may I enter in the future if I use malloc instead of AllocSysObject?

I am using example here for reference:
https://wiki.amigaos.net/wiki/Exec_L...c_List_Example

Quote:
Originally Posted by phx View Post
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.
Sim085 is offline  
Old 14 December 2022, 13:58   #5
Thomas Richter
Registered User
 
Join Date: Jan 2019
Location: Germany
Posts: 3,233
Quote:
Originally Posted by Sim085 View Post
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))?
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.
Thomas Richter is offline  
Old 14 December 2022, 14:04   #6
Sim085
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:
Originally Posted by thomas View Post
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.
Is there an example where memory not freed at end of execution is something that is wanted?

Quote:
Originally Posted by thomas View Post
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.
Quote:
Originally Posted by Thomas Richter View Post
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.
btw - just want to highlight that I am not criticizing one approach or the other. All my life I was familiar with malloc. Looking at Amiga C examples I can see these AllocSysObject functions and want to understand why it would make sense using one approach instead of another.

Last edited by Sim085; 14 December 2022 at 14:10.
Sim085 is offline  
Old 14 December 2022, 14:28   #7
Thomas Richter
Registered User
 
Join Date: Jan 2019
Location: Germany
Posts: 3,233
Quote:
Originally Posted by Sim085 View Post
So is malloc more efficient? I would assuming clearing memory would take some extra time?
malloc() does not ensure that memory is cleared. calloc() does that. As said, I would expect that malloc() is in general providing better performance than the system functions as typical malloc() implementations go through a memory pool maintained by the C library of the compiler, whereas the system functions would typically go through AllocMem() and its rather primitive MemChunk allocator. Whether that makes any practical difference in your program is of course debatable. If you have only a small number of allocations to make, and these allocations are infrequent, it may pay off not having to include a rather bulky C library.


So, as always, "it depends".


Quote:
Originally Posted by Sim085 View Post
Is there an example where memory not freed at end of execution is something that is wanted?
Well, if the list is part of a system structure that needs to remain in the system after termination of your program. There are a couple of such structures the Os maintains - they make not necessarily good examples for a "struct List" exactly - but just for the purpose of fairly general "stay resident" structures: public message ports, semaphores, or anything a device driver needs to do. "Load and stay resident" programs are not completely uncommon, but require a bit more care in their design.




Quote:
Originally Posted by Sim085 View Post
btw - just want to highlight that I am not criticizing one approach or the other. All my life I was familiar with malloc. Looking at Amiga C examples I can see these AllocSysObject functions and want to understand why it would make sense using one approach instead of another.

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).
Thomas Richter is offline  
Old 14 December 2022, 14:58   #8
Karlos
Alien Bleed
 
Karlos's Avatar
 
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.
Karlos is offline  
Old 14 December 2022, 16:38   #9
phx
Natteravn
 
phx's Avatar
 
Join Date: Nov 2009
Location: Herford / Germany
Posts: 2,500
Quote:
Originally Posted by Sim085 View Post
Aren't these two code sets the same? Is AllocSysObject doing anything more than just allocating memory? What risks may I enter in the future if I use malloc instead of AllocSysObject?
If your question is only refering to Lists and Nodes, then they will likely stay the same in future and it doesn't really matter. My reply was more generally. Functions like AllocSysObject exist because the size and memory requirements of an object might change in future OS versions. Then the code using AllocSysObject will still work, while malloc fails.

Quote:
Originally Posted by Sim085 View Post
All my life I was familiar with malloc. Looking at Amiga C examples I can see these AllocSysObject functions and want to understand why it would make sense using one approach instead of another.
My personal approach is: when writing an AmigaOS program, then use AmigaOS functions only. Avoid the compiler's clib and mlib. Use AllocMem or AllocVec instead of malloc, Printf instead of printf, Open instead of fopen or open, etc.. This usually results in a much smaller and faster program.

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).
phx is offline  
Old 17 December 2022, 14:48   #10
bebbo
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.
bebbo is offline  
Old 17 December 2022, 14:58   #11
Karlos
Alien Bleed
 
Karlos's Avatar
 
Join Date: Aug 2022
Location: UK
Posts: 4,178
Quote:
Originally Posted by bebbo View Post
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.
How does using the C standard library routines prevent memory fragmentation? The implementation has to interface with the OS eventually. The reason for using the standard library is if you are aiming for portability with other platforms.
Karlos is offline  
Old 17 December 2022, 15:55   #12
Thomas Richter
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.
Thomas Richter is offline  
Old 17 December 2022, 17:47   #13
MortenBr
Registered User
 
Join Date: Jul 2020
Location: Porsgrunn/Norway
Posts: 1
Quote:
Originally Posted by Sim085 View Post
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);
Aren't these two code sets the same? Is AllocSysObject doing anything more than just allocating memory? What risks may I enter in the future if I use malloc instead of AllocSysObject?

I am using example here for reference:
https://wiki.amigaos.net/wiki/Exec_L...c_List_Example
You've gotta NewList on mylist before you can add the node in the second example. malloc does not initialise the list for you.
I'm still on team malloc, though..

-Morten-
MortenBr 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
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

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 17:40.

Top

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