Page 1 of 1
Heap Manager Structure
Posted: Tue Dec 19, 2006 10:21 am
by Tolga
Hi. You see attachment. When i created a space for an object, i will start to reserve area from 0x00000000. Is this true? Example, in attachment, Object1 pointer address value is 0. And also, Object2 pointer address value is Object1 size.
I ask this question, because i used malloc in C++. And looked to pointer value, it isnt 0. And i have suspected.
Posted: Wed Dec 20, 2006 12:04 am
by Tolga
I will start to allocate space from 0x00000000 in Stack Segment. This is true or false?
Posted: Wed Dec 20, 2006 2:51 am
by Combuster
depending on your implementation, malloc will store some metadata in the heap itself, with the result that return values of 0 generally dont occur. Besides, that value also means a NULL pointer which can horribly confuse your code should you get it.
Also, the stack grows downward. That means that ESP starts at some high address and decrements to lower addresses when the stack expands.
Posted: Wed Dec 20, 2006 9:07 am
by Tolga
Im using different system to show status of memory allocation. Stack (esp) starts at high of address in stack segment and decrements. Also heap starts at 0 in stack segment. So is this process true?
Thanks.
Posted: Wed Dec 20, 2006 4:02 pm
by Combuster
The Heap and Stack are located in two different segments, DS (Heap) and SS (Stack). In case DS equals SS, then the heap can indeed start at SS:0 (since it would equal DS:0)
Maybe you're not a native speaker, but still, may i suggest you to formulate your questions such that we can see what you are up to, instead of answering something that sounds like "is this the correct approach" which comes close to asking someones opinion.
Posted: Wed Dec 20, 2006 5:19 pm
by Tolga
:S In C++, when we use a pointer, it uses which segment? DS or SS?
if SS, i will create 3 segment for an application or kernel.
Code
Data
Stack
But if DS, i dont understand that how can heap work.
Posted: Wed Dec 20, 2006 5:33 pm
by urxae
Tolga wrote::S In C++, when we use a pointer, it uses which segment? DS or SS?
Most 32-bit C++ compilers will assume DS and SS are equivalent. But the machine code they generate will typically use DS, since it's the default for most registers.
16-bit compilers compilers will usually store the segment as part of the pointer value, so they should reload one of the segment registers with the segment value in the pointer and use that segment register. This will probably not be SS, though.
This is all x86-specific, of course.
Posted: Thu Dec 21, 2006 11:19 am
by Tolga
But the machine code they generate will typically use DS, since it's the default for most registers.
If i create Code, Data and Stack Segments in GDT before application starting , does compiler use SS for pointer?
I will adjust SS and DS with different values. If compiler assumes DS and SS are equivalent, maybe i allocate some space on application code. Because DS = CS.[/quote]
Posted: Sat Dec 23, 2006 2:03 am
by rexlunae
Tolga wrote:If i create Code, Data and Stack Segments in GDT before application starting , does compiler use SS for pointer?
No. It's hard to generalize when you haven't even told us what compiler you're using, but gcc for instance, is completely unaware of the possibility of non-overlapping segments. Or, put another way, it pretends the segmentation mechanism doesn't even exist. So, it doesn't even look at the GDT, and I think it will be almost impossible to make it generate code with pointers relative to SS.
If you are using a segmentation-aware compiler, which generates 32-bit segmented code, and manages your GDT for you, that may be different, I don't know. I don't even know if such a compiler exists.
Posted: Sun Dec 24, 2006 1:32 am
by hendric
Usually Address 0x00000000 is reserved. No mapping [0x00000000,4k) that access the area will cause a page fault and then OS will catch it.
Posted: Sun Dec 24, 2006 1:56 am
by Brendan
Hi,
rexlunae wrote:Tolga wrote:If i create Code, Data and Stack Segments in GDT before application starting , does compiler use SS for pointer?
No. It's hard to generalize when you haven't even told us what compiler you're using, but gcc for instance, is completely unaware of the possibility of non-overlapping segments. Or, put another way, it pretends the segmentation mechanism doesn't even exist. So, it doesn't even look at the GDT, and I think it will be almost impossible to make it generate code with pointers relative to SS.
If you are using a segmentation-aware compiler, which generates 32-bit segmented code, and manages your GDT for you, that may be different, I don't know. I don't even know if such a compiler exists.
For compilers (and code in general), supporting segmentation would be a nightmare. For an example:
Code: Select all
char foo = 'a';
void main(void) {
char bar = 'b';
showCharacter(&foo);
showCharacter(&bar);
}
void showCharacter(char *c) {
printf("%c\n", *c);
}
Now consider the code you'd need in "showCharacter()", and don't forget that "foo" would be in a data segment while "bar" is on the stack.
Basically, every pointer would need to consist of a segment and an offset, and you'd be constantly doing slow segment register loads.
Cheers,
Brendan
Posted: Sun Dec 24, 2006 5:09 am
by rexlunae
Brendan wrote:Basically, every pointer would need to consist of a segment and an offset, and you'd be constantly doing slow segment register loads.
I'm not saying there is a modern 32-bit compiler that is aware of segmentation, but I'm acknowledging that I don't know about every compiler, and it isn't completely impossible. Keeping track of what segment a variable is in would become feasible if you made the pointers all 48-bit far pointers, but most compiler writers lack the necessary level of insanity.