EmbeddedRelated.com
Forums

Memory allocation with malloc

Started by "argy.tzak" August 20, 2007
Dear all,

could someone suggest a method in Dynamic C that produces similar
results to the following code (specifically, the part with malloc)?

if (!(fuzzy_system->emem = (IN_MEM *) malloc(sizeof(IN_MEM)))) {
printf("Error allocating memory.\n");
exit(1);

I am reading the manual and I can see that Dynamic C has different
functions for dynamic memory allocation, but giving an example for the
code above would really help.
I also found an application note which described how to use malloc
with Dynamic C (although not recommended). So what is the optimal way?

Thx
Argy
There may be better dynamic allocation techniques that I don't know
of, but I use xalloc. Make "emem" an unsigned long, and it looks like
this:
if( xavail(NULL) < sizeof(IN_MEM) ) // checks amount of xmem left
{
printf("Error allocating memory.\n");
exit(1);
}
fuzzy_system.emem = xalloc(sizeof(IN_MEM));
// saves the address of the allocated memory into fuzzy_system.emem

Then, to get data into this allocated memory, use root2xmem(), and
use xmem2root() to get it back out again.

However, this is fairly annoying in that xmem allocation/deallocation
must occur as a stack, that is, the most recent allocation must be the
first to be deallocated. If you are not deallocating, don't worry
about it, but if you are then you will need _xalloc() instead
(http://www.rabbitsemiconductor.com/documentation/docs/manuals/DynCFunctionReference/12fun492.htm).
See the documentation for xrelease() for more info.

Hope this helps, and someone please let me know if I said anything
incorrect. I'm still pretty new to Dynamic C.

--David
--- In r..., "argy.tzak" wrote:
>
> Dear all,
>
> could someone suggest a method in Dynamic C that produces similar
> results to the following code (specifically, the part with malloc)?
>
> if (!(fuzzy_system->emem = (IN_MEM *) malloc(sizeof(IN_MEM)))) {
> printf("Error allocating memory.\n");
> exit(1);
>
> I am reading the manual and I can see that Dynamic C has different
> functions for dynamic memory allocation, but giving an example for the
> code above would really help.
> I also found an application note which described how to use malloc
> with Dynamic C (although not recommended). So what is the optimal way?
>
> Thx
> Argy
>
Rabbit have included a 'pool' system that I haven't really used, it
seems to use discreet sized chunks, check out
DCRABBIT_9_xx\lib\pool.lib & DCRABBIT_9_xx\samples\pool.c
I didn't find it useful for the type of data allocation I wanted so I
have been using mostly static data (arrays, structs) until recently. I
only use xmem for battery backed storage. ( Oh yea, I did write a
very nasty xmemalloc library but again it had a static freelist struct
=>limits!!). And its just not possible to do useful pointer stuff like
struct object *last;
for(last = head; last->next; last = last->next);
last->next = this;
(AFAIK) with xmem since "last = last->next;" requires xmem2root() or
xgetlong()

Recently I downloaded the malloc code from this group (see Files
section) and have been using it....and I'm very happy with it.
It allows me to write malloc() and free() (strdup(), etc) as you would
with a 'normal' C compiler. (Anyone else using it??)
It may be what you need !!
Good luck,
/Wayne

--- In r..., "argy.tzak" wrote:
>
> Dear all,
>
> could someone suggest a method in Dynamic C that produces similar
> results to the following code (specifically, the part with malloc)?
>
> if (!(fuzzy_system->emem = (IN_MEM *) malloc(sizeof(IN_MEM)))) {
> printf("Error allocating memory.\n");
> exit(1);
>
> I am reading the manual and I can see that Dynamic C has different
> functions for dynamic memory allocation, but giving an example for the
> code above would really help.
> I also found an application note which described how to use malloc
> with Dynamic C (although not recommended). So what is the optimal way?
>
> Thx
> Argy
>