mardi 26 juillet 2016

Is there a way to reuse existing malloc/free algorithm for allocating from existing memory pools?

I am interested in using existing buffers of fixed size in my programs for allocating new memory from, rather than using functions such as new and malloc.

For example:

#include <stdint.h>

#define POOL_SIZE 1024

typedef struct {
    uint32_t nbytes;
    void *addr; 
} pool_t;

uint8_t mem[POOL_SIZE];

int main(int argc, char **argv)
{
    pool_t my_pool = { POOL_SIZE, (void *)&mem };

    int *my_int = custom_malloc(100 * sizeof(int));        
    printf("memory block address: %x.n", (uintptr_t)&mem);
    printf("my_int address: %x.n", (uintptr_t)my_int);
    custom_free(my_int);

    return 0;
}

Is it possible to reuse the algorithm that malloc/free use for creating my own implementation of such memory allocations, or do I have to create these algorithms myself?

I changed this question to better reflect what I want to accomplish

Aucun commentaire:

Enregistrer un commentaire