Page 1 of 1

Static Pointers / kmalloc

Posted: Wed Mar 24, 2004 12:40 am
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).

Re:Static Pointers / kmalloc

Posted: Wed Mar 24, 2004 1:36 am
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?

Re:Static Pointers / kmalloc

Posted: Wed Mar 24, 2004 2:15 am
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.