Static Pointers / kmalloc

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
stonedzealot

Static Pointers / kmalloc

Post by stonedzealot »

if kmalloc is void *kmalloc(int bytes) then how is it possible to get a function to reuse memore allocated to it. On instinct (and I think logic) I would say something like this:

static int *thing = kmalloc(sizeof(int));

It seems pretty simple. But GCC doesn't like it because kmalloc() isn't a constant and it's going to try to handle the pointer at linktime before kmalloc() is ready. So I understand why that detonates, but how else can I get a pointer that will point to the same memory through multiple calls?

The only method I can think of that would avoid this is to save the addresses in non-pointer statics and assign the pointers to those statics but that's really inefficient considering you have a full integer (size of address) overhead for every single variable you allocate memory for (bleh).
BI lazy

Re:Static Pointers / kmalloc

Post by BI lazy »

Hmmm ... that's no wonder, because the compiler/linker tries to determine the content of the variable at declaration time and finds a function there - this will not work out. Kmalloc is called in runtime. *gg* as you've said.

Declare the pointer variable in the data area of the module you are about to compile instead on its stack (local var) and have kmalloc hand out the required adress at runtime.

so you have to assign the adress only one time and every function can access it.

... you do know about global and local variables, don't you?
User avatar
Solar
Member
Member
Posts: 7632
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:Static Pointers / kmalloc

Post by Solar »

Code: Select all

static type_t * thing = 0;

int kernel_main()
{
    thing = kmalloc(sizeof(type_t));
    ...
}
Replace type_t with whatever type you want.
Every good solution is obvious once you've found it.
Post Reply