PICmicro & dsPIC >> PICmicro & dsPIC

Pages: 1
Nah



Reged: Nov 17 2005
Posts: 2
malloc() ? ( dynamic memory allocation )
      #19905 - Fri Nov 18 2005 03:05 AM

Hi all,

I'm totally new to Picc18( was using mcc18 before ).

I'm wondering if it's possible to make dynamic memory allocations, as malloc() do on a computer for example.

I searched this forum and on google, and found nothing..

Thanks in advance for any reply.

Have a nice day.

Nico


Post Extras: Print Post   Remind Me!   Notify Moderator  
mikerjModerator
Guru
****

Reged: Oct 19 2003
Posts: 1482
Loc: Devon, UK
Re: malloc() ? ( dynamic memory allocation ) [Re: Nah]
      #19907 - Fri Nov 18 2005 05:53 AM

It's certainly possible, but are you sure you really want or need to? Dynamicaly allocating memory on tiny resource limited micro's is normaly avoided. Can you not work with fixed memory blocks?

If you really feel you need to dynamicly allocate memory, the source for an implementation of malloc is in the compilers "source" directory.


Post Extras: Print Post   Remind Me!   Notify Moderator  
Nah



Reged: Nov 17 2005
Posts: 2
Re: malloc() ? ( dynamic memory allocation ) [Re: mikerj]
      #19909 - Fri Nov 18 2005 07:46 AM

thanks a lot for your reply.

have a nice day

Nico.


Post Extras: Print Post   Remind Me!   Notify Moderator  
John Payson

****

Reged: Oct 16 2003
Posts: 1251
Re: malloc() ? ( dynamic memory allocation ) [Re: Nah]
      #19983 - Wed Nov 23 2005 01:38 PM

The memory-allocation functions in C allow blocks to be allocated and freed in any order, with the caveat that fragmentation may occur.

In many embedded systems, this is not necessary and it is sufficient to unallocate memory in the reverse order allocated (or in some cases just allocate memory and never deallocate it at all until system reinitialization).

A good approach for handling this is to simply keep a pointer to the last address allocated. When you receive a request for more allocation, copy and bump the pointer.

Depending upon your application, it may be better to allocate memory bottom-up, top-down, or both.

Code:

unsigned char *botptr;
unsigned char *topptr;
#define MEM_SIZE 64
unsigned char allocmem[MEM_SIZE];

#define mem_init() \
(botptr = allocmem, topptr = allocmem+MEM_SIZE)
#define bot_alloc(size) \
(void*)(botptr += (size), botptr-(size))
#define top_alloc(size) \
(void*)(topptr -= (size))
#define bot_free(what) \
(botptr=(char*)(what))
#define top_free(what,size) \
(topptr = (char*)(what)+(size))
#define top_keep(what) \
(topptr = (char*)(what))



Note that the code as given does not do any error-checking (to ensure that topptr remains above botptr). Bot_free() will release the indicated object and everything allocated after it by bot_alloc(). Top_free() will do likewise for stuff allocated by top_alloc(), but you must supply the size of the item. Top_keep will not free the indicated item, but will free everything allocated after it by top_alloc().

Note that regardless of the number of items allocated, there is no overhead other than the top and bottom pointers.


Post Extras: Print Post   Remind Me!   Notify Moderator  
Pages: 1



Extra information
1 registered and 30 anonymous users are browsing this forum.

Moderator:  mikerj, jtemples, jeff, Dan Henry, Andrew L 

Print Topic

Forum Permissions
      You cannot start new topics
      You cannot reply to topics
      HTML is enabled
      UBBCode is enabled

Rating:
Topic views: 2386

Rate this topic

Jump to

Contact Us | Privacy statement HI-TECH Software

Powered by UBB.threads™ 6.5.5