Page 1 of 1

Variable's when my kernel starts

Posted: Thu Nov 14, 2002 12:00 am
by Brill
ok, my kernel is loaded by grub, but i want to dynamically allocate my kernels variables, not all but some, to save on memory and the like. But when i declare a variable, grub init's it. How can i init a variable with my own allocator after my kernel is loaded. I'm writing my kernel is asm too so no C/C++ examples. I don't quite know what those languages do to init variables.

Thanx in advance. Thanx to CarbonBased too for all his help :)
Cheers, Brill

RE:Variable's when my kernel starts

Posted: Thu Nov 14, 2002 12:00 am
by carbonBased
Easiest way, in my opinion, is to define it in the BSS section, and GRUB'll take care of allocating the space (but it wont take up any space in the binary).

Otherwise, you'll need your own memory management routines, and you'll have to alloc variables yourself:

myStruct = myMemAlloc(size);

(I know it's C/C++, but I'm sure you know what I mean).

For single longs, etc, this is overkill.  I'd just define 'em in your binary, and not worry about 'em.  I'd only allocate large structures, if anything.

Jeff

RE:Variable's when my kernel starts

Posted: Fri Nov 15, 2002 12:00 am
by Brill
Ok, thanks. But how do i store the variable without actually initialising it.
I need to point the variable to the location of memory. I need to store somewhere in my elf file the name of the variable. i.e. i need it to be in my elf relocation string table i believe it is, that stores my variable names and offsets, but without actually having it point anywhere.
I hope you understand, i don't know how to write it any other way.
Thanks.
Brill.

RE:Variable's when my kernel starts

Posted: Fri Nov 15, 2002 12:00 am
by carbonBased
All you need to do is create pointer variables

myPtr dd ?

And after allocating space, assign myPtr the address of the allocated space.  This is pointless, however, if you're trying to do this for bytes, words, or dwords... you aren't saving any space, and for the former two, you're actually using more space (a pointer is larger than a byte or word in x86 arch).  In this case, I'd just put them in the BSS section (in fact, everything you're trying to do can be done by putting them in the BSS section, but it seems like you don't want to do this).

Hope that helps,
Jeff

RE:Variable's when my kernel starts

Posted: Sat Nov 16, 2002 12:00 am
by Brill
Thanks, i understand that i wouldn't be saving space. I'm not going to be doing it for larger sizes of variables. I wanted to know it because as well as the kernel i need to tell programs that get loaded where their allocated memory it. I was also going to use it for testing my allocator, regardless of how much space i save/waste it's just a test.

Instead of making 10 functions and 'hoping' they work i tend to hack my way through them to test them out, then i know they work to the extent i've tested and personally it makes finding problems easier.

Cheers, Brill.