How to reserve unknown amount of memory in .bss section

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
j4cobgarby
Member
Member
Posts: 64
Joined: Fri Jan 26, 2018 11:43 am

How to reserve unknown amount of memory in .bss section

Post by j4cobgarby »

For the memory manager in my operating system I'm using a bitmap to represent which blocks of physical memory are available or not. Of course, at compile time there's no way to know how much memory the user will have, and therefore how long this bitmap must be. It's a 32 bit operating system, so in theory I could allocate enough memory in the bitmap to represent a full 4GB of memory, but this seems wasteful. Is there maybe some way to dynamically allocate enough space for this bitmap?

By the way, the main reason this is an issue came to light after a few hours of debugging, when setting the value of an uninitialised global variable (so, it's stored in .bss too) changed data in the bitmap, which I realised was because the compiler was putting such global variables right after the bitmap.

Thanks :)
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: How to reserve unknown amount of memory in .bss section

Post by iansjack »

I'm not sure that I understand your problem. Just allocate the memory dynamically once your OS has started and you know how much memory you have. Not all the memory used in your OS has to be allocated in the .data or .bss sections. Or have I misunderstood your problem?
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: How to reserve unknown amount of memory in .bss section

Post by iansjack »

I'm not sure that I understand your problem. Just allocate the memory dynamically once your OS has started and you know how much memory you have. Not all the memory used in your OS has to be allocated in the .data or .bss sections. Or have I misunderstood your problem?
j4cobgarby
Member
Member
Posts: 64
Joined: Fri Jan 26, 2018 11:43 am

Re: How to reserve unknown amount of memory in .bss section

Post by j4cobgarby »

iansjack wrote:I'm not sure that I understand your problem. Just allocate the memory dynamically once your OS has started and you know how much memory you have. Not all the memory used in your OS has to be allocated in the .data or .bss sections. Or have I misunderstood your problem?
You're right that that works for most cases, but it seems that any global variables which aren't defined at their declaration line are stored in .bss, and I don't think I'm able to change that.
Anyway the more I think about it, it's okay to just allocate enough memory for the bitmap to store all the blocks for a whole 4GB of memory, since that only comes to 1MB.
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: How to reserve unknown amount of memory in .bss section

Post by iansjack »

You obviously can't allocate the array at compile time, but you can allocate a variable to hold a pointer to the bitmap. After you have determined how much RAM you have you can then set aside a block of memory for the bitmap and then fill in the pointer value to point to that array. Obviously you then mark that memory as used when you initialize the bitmap.
j4cobgarby
Member
Member
Posts: 64
Joined: Fri Jan 26, 2018 11:43 am

Re: How to reserve unknown amount of memory in .bss section

Post by j4cobgarby »

iansjack wrote:You obviously can't allocate the array at compile time, but you can allocate a variable to hold a pointer to the bitmap. After you have determined how much RAM you have you can then set aside a block of memory for the bitmap and then fill in the pointer value to point to that array. Obviously you then mark that memory as used when you initialize the bitmap.
Well, that's actually a great idea haha, not sure how I didn't think of that, thanks :)
Post Reply