mike
mike

Reputation: 110

Cannot convert a pointer to a struct to a int pointer

I'm trying to build a memory manager for my operating system. However, when I add a pointer to the next node property of the first heap item, it doesn't work. I suspect it cannot convert a pointer to a heap item to a unsigned int pointer.

Constants

#define MEMORY_BLOCK_FREE 0
#define MEMORY_BLOCK_USED 1
#define MEMORY_BLOCK_TAIL 2

Memory Block Struct

typedef struct
{
    u32* addr;
    u32 size;
    u8 state;
    u32* next_block;
} memory_block;

Add function

u32* add_block(u32 size)
{
    memory_block* cblock = &head;
    while (1)
    {
        if (cblock->state == MEMORY_BLOCK_TAIL)
        {
            break;
        }
        cblock = (memory_block*)cblock->next_block;
    }
    u32* addr = cblock->addr;
    cblock->size = size;
    cblock->state = MEMORY_BLOCK_USED;
    memory_block next;
    next.state = MEMORY_BLOCK_TAIL;
    next.addr = (u32*)(addr + size);
    cblock->next_block = (u32*)&next;
}

Upvotes: 0

Views: 137

Answers (1)

Kungfu Frog
Kungfu Frog

Reputation: 81

memory_block next;

It is a wrong code. Try this

memory_block *next;
next = malloc(sizeof(memory_block));

Upvotes: 0

Related Questions