My personal C pointer casting mess
Posted: Fri Apr 03, 2009 1:54 am
Hi all,
I can say for myself that I'm quite good at programming, but when writing an operating system you sometimes have to program in a different way than programming somewhere in the userspace where all the dirty work is done for you with libs and the operating system itself.
I've been coding my kmalloc() last night and I did some very ugly pointer casting mess, I think.
Only one example:
I've got the following global, static variable in my file which hold the current end of my heap, to see wether a the heap needs a new frame mapped into the address space or not:
I also have a struct, which defines the begining of a memory block in the heap (mem_info).
If I want to edit this on a specific location, I need to do such a cast:
This would give me the beginning of the linked list for managing my memory blocks in the heap.
Now I allocated some bytes, splitted up the linked list, created a new item for it and I want to know if I need to allocate a new frame and map it to the end of the heap to store the new linked list item.
I need to do such an ugly casting mess to check this:
This just doesn't look right. But I can't add anything I want to my pointers or even compare them. So I have to cast them to a normal integer.
Is this really neccessary?
I can say for myself that I'm quite good at programming, but when writing an operating system you sometimes have to program in a different way than programming somewhere in the userspace where all the dirty work is done for you with libs and the operating system itself.
I've been coding my kmalloc() last night and I did some very ugly pointer casting mess, I think.
Only one example:
I've got the following global, static variable in my file which hold the current end of my heap, to see wether a the heap needs a new frame mapped into the address space or not:
Code: Select all
static uint8_t* kheap_current_end = (void*)KHEAP_START;
If I want to edit this on a specific location, I need to do such a cast:
Code: Select all
struct mem_info *newBlock = (struct mem_info *)(kheap_start);
Now I allocated some bytes, splitted up the linked list, created a new item for it and I want to know if I need to allocate a new frame and map it to the end of the heap to store the new linked list item.
I need to do such an ugly casting mess to check this:
Code: Select all
if((uint32_t)newBlock + sizeof(struct mem_info) > (uint32_t)kheap_current_end) expandKheap();
Is this really neccessary?