开发者

initializing a data structure in C to manage a pool of memory

i am writing a simple function for a library, that will take in as a parameter the size of memory to be managed by my other functions.

i have a data structure that holds the 开发者_如何学运维information of this large memory pool initialized by the user.

typedef struct memBlock{
    struct memBlock* next;
    unsigned int size;  // Size of this block
    unsigned int is_used;  // bool 0 = not used 1 = used
}  memBlock;

I also have this function that i am trying to figure out how to initialize this data structure as well as allocate enough space to be managed initially?

int initialize_memory(unsigned long size){

    memBlock *ptr; // the beginning of our whole memory to be handled

    ptr = malloc(size); // this is the ptr to the original memory first allocated.
    ptr->next = NULL;
    ptr->size = NULL;
    ptr->is_used = 0;

    has_initialized = 1; // the memory has been initialized
}

please help


Change ptr->size = NULL; to ptr->size = size;. You also need to return ptr, or store it somewhere. Your function returns int, but you don't return anything. has_initialized seems unnecessary -- you know you've initialized because your memory pool (the ptr value you will return) isn't NULL. If you need more help than that, you're going to have to explain more.

Addendum: You need to decide whether memBlock.size is the size of the allocated space or the size of the memory block represented by the memBlock ... if the latter, then you need to account for the space occupied by the memblock itself by subtracting that off the amount of space you allocated: ptr->size = size - sizeof(struct memBlock); You also need a way to address your memory pool ... since that immediately follows the memBlock, its address is (ptr + 1) or &ptr[1] (if you don't understand that, look up "pointer arithmetic in C").

P.S. You wrote in a comment "Essentially i also have another function that will act like 'malloc' to reserve a number of bytes but will first check this data structure to see if any memory is available from my pool"

Why do you want to do that? malloc already manages memory far better than your function will, considering the skill level and time invested, and there's no point in layering another memory allocator on top of it. Unless this is a school project to write a memory allocator, in which case you should say that up front.


typedef struct memBlock {
   unsigned int size;
   unsigned int initialized;
   void* block;
} memBlock;

memBlock* new_memBlock(unsigned int size)
{
    memBlock* memblock;

    memblock = malloc(sizeof(memBlock));

    if (memblock)
    {
        memblock->size = size;
        memblock->block = malloc(size);

        if (memblock->block)
            memblock->initialized = 1;
    }

    return memblock;
}

void free_memBlock(memBlock** memblock)
{
    if (*memblock)
    {
        free(*memblock->block)
        *memblock->block = 0;
    }

    free(*memblock);
    *memblock = 0;
}

void main()
{
    memBlock* memblock = new_memBlock(1024);

    if (memblock && memblock->initialized)
        printf("Initialized\n");
    else
        printf("Not initialized\n");

    free_memBlock(&memblock);
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜