开发者

C Memory Management in Embedded Systems

I have to use c/asm to create a memory management system since malloc/free don't yet exist. I need to have malloc/free!

I was thinking of using the memory stack as the space for the memory, but this would fail because when the stack pointer shrinks, ugly things happen with the allocated space.

1) Where would memory be allocated? If I place it randomly in the middle of the Heap/Stack and the Heap/Stack expands, there will be confl开发者_C百科icts with allocated space!

12 What Is the simplest/cleanest solution for memory management? These are the only options I've researched:

  • A memory stack where malloc grows the stack and free(p) shrinks the stack by shifting [p..stack_pointer] (this would invalidate the shifted memory addresses though...).
  • A linked list (Memory Pool) with a variable-size chunk of memory. However I don't know where to place this in memory... should the linked list be a "global" variable, or "static"?

Thanks!


This article provides a good review of memory management techniques. The resources section at the bottom has links to several open source malloc implementations.


For embedded systems the memory is partitioned at link time into several sections or pools, i.e.: ro (code + constants) rw (heap) zi (zero initialised memory for static variables)

You could add a 4th section in the linker configuration files that would effectively allocate a space in the memory map for dynamic allocations.

However once you have created the raw storage for dynamic memory then you need to understand how many, how large and how frequent the dynamic allocations will occur. From this you can build a picture of how the memory will fragment over time.

Typically an application that is running OS free will not use dynamic memory as you don't want to have to deal with the consequences of malloc failing. If at all possible the better solution is design to avoid it. If this is not at all possible try and simplify the dynamic behaviour using a few large structures that have the data pre-allocated before anything needs to use it.

For example say that you have an application that processes 10bytes of data whilst receiving the next 10 bytes of data to process, you could implement a simple buffering solution. The driver will always be requesting buffers of the same size and there would be a need for 3 buffers. Adding a little meta data to a structure:

{
    int inUse;
    char data[10];
}

You could take an array of three of theses structures (remembering to initialise inUse to 0 and flick between [0] and [1], with [2] reserved for the situations when a few too many interrupts occur and the next buffer is required buffer one is freed (the need for the 3rd buffer). The alloc algorithm would on need to check for the first buffer !inUse and return a pointer to data. The free would merely need to change inUse back to 0.

Depending on the amount of available RAM and machine (physical / virtual addressing) that you're using there are lots of possible algorithms, but the more complex the algorithm the longer the allocations could take.


Declare a huge static char buffer and use this memory to write your own malloc & free functions.

Algorithms for writing malloc and free could be as complex (and optimized) or as simple as you want.

One simple way could be following...

based on the type of memory allocation needs in your application try to find the most common buffer sizes

  • declare structures for each size with a char buffer of that length and a boolean to represent whether buffer is occupied or not.
  • Then declare static arrays of above structures( decide array sizes based on the total memory available in the system)
  • now malloc would simply go the most suitable array based on the required size and search for a free buffer (use some search algo here or simply use linear search) and return. Also mark the boolean in the associated structure to TRUE.
  • free would simply search for buffer and mark the boolean to FALSE.

hope this helps.


Use the GNU C library. You can use just malloc() and free(), or any other subset of the library. Borrowing the design and/or implementation and not reinventing the wheel is a good way to be productive.

Unless, of course, this is homework where the point of the exercise is to implement malloc and free....

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜