A year or two ago, I posted the following code on my website:
Code: Select all
void pmm_free_page(unsigned int p)
{
// Sanity check
p &= PAGE_MASK;
// Throw away irrecoverable pages
// (will also catch null pointers)
if(p < pmm_pos)
return;
if(pmm_page_stack_top >= pmm_page_stack_roof)
{
// If there's no more space in the stack
// use the freed page to expand it
vmm_page_set(pmm_page_stack_roof, VMM_PAGE_VAL(p, PAGE_PRESENT | PAGE_WRITE));
pmm_page_stack_roof += PAGE_SIZE;
} else {
// Add the freed page to the top of the stack
// and increase the stack pointer
*((unsigned int*)pmm_page_stack_top) = (unsigned int )p;
pmm_page_stack_top += sizeof(unsigned int *);
}
// When there's at least one page in the stack, the PMM is good to go
pmm_running = TRUE;
}
Since then I have been trying to figure out in what way my use of casting is wrong and how to do it right.[...]
I haven't read this one but the first I found after pressing CTRL+F and searching for "*)" was exactly what I expected: improper use of casting.
[...]
Would it be better to use
Code: Select all
uint32_t *stack = (uint32_t*)pmm_stack_loc;
*stack = p;
pmm_stack_loc += sizeof (uint32_t);
If so, what's the difference and why?
Best regards
/ Thomas