Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

malloc implementation from fixed size buffer

Tags:

c

malloc

I need a generic malloc implementation that uses one big fixed-size buffer. Something similar to the "Zero-malloc memory allocator" SQLite has. Do you know of any such implementations? It should be light-weight and portable that can be used for embedded applications.

Thanks in advance.

like image 739
Manish Avatar asked Jan 11 '11 12:01

Manish


People also ask

How malloc is implemented internally?

When one calls malloc , memory is taken from the large heap cell, which is returned by malloc . The rest is formed into a new heap cell that consists of all the rest of the memory. When one frees memory, the heap cell is added to the end of the heap's free list.

How can I get malloc size?

Use a pointer-to-array type rather than a pointer-to-element type: int (*parray)[10] = malloc(sizeof *parray); Then sizeof *parray gives you the desired answer, but you need to access the array using the * operator, as in (*parray)[i] (or equivalently albeit confusingly, parray[0][i] ).

How do you determine the size of a buffer?

To check the buffer window, multiply the bit rate (bits per second) by the buffer window (in seconds) and divide by 1000 to get the size, in bits, of the buffer for the stream.


2 Answers

Two suggestions:

  1. IF you need something production quality and well tested, just borrow SQLite's allocator. SQLite's source code is very well-written, documented, extremely well-tested and has a very permissive open-source license.
  2. IF you need something small and simple, either to learn or to use in an embedded environment, consider this implementation [shameless plug!] - just 350 LOC of commented C code.
like image 105
Eli Bendersky Avatar answered Sep 20 '22 08:09

Eli Bendersky


The SQLite source code is freely available. If you like that a particular implementation, why not use it?

like image 33
bta Avatar answered Sep 20 '22 08:09

bta