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.
I would think the heap can grow any way you code it to.
Like if I start the heap start address at 0x1000000 then I should beable to make it grow down by
having my malloc implementation use the lower address's under 0x1000000. --
As for the stack this is more controlled by the hardware i.e push , pop , call ,...etc and what way they work on memory.
Curious though if you can do it.
Is their any machines that have stack growing up....
Sam111 wrote:Must the heap grow up and the stack grow down?
I would think the heap can grow any way you code it to.
I would think the same, since the heap is usually entirely coded by the developer of the kernel/OS, you can choose yourself what you want it to be like. I think people opt for a heap that grows up because it seems more logical (low to high) and usually memory is laid out that way (e.g. 0 - 4 GB for 32-bit systems). In 64-bit and with the PML4 however, it may be feasible to have a heap that grows down for the part of the virtual address space that is negative.
When the chance of succeeding is 99%, there is still a 50% chance of that success happening.
I can place it anywhere but call , push , pop , ret ,...etc force the stack to grow down I believe.
This is more ingranded in the hardware/opcodes how it will grow.
I am wondering is their any hardware/os out their that have the stack growing up?
Is it possible to effect the opcodes in some way as to have the stack grow up.
I can place it anywhere but call , push , pop , ret ,...etc force the stack to grow down I believe.
This is more ingranded in the hardware/opcodes how it will grow.
I am wondering is their any hardware/os out their that have the stack growing up?
Is it possible to effect the opcodes in some way as to have the stack grow up.
The ARM architecture generally supports stacks that grow down as well as up, whereas the x86(-64)-architecture only support stacks that grow downwards. You can have stacks that grow upwards, but the x86(-64) doesn't have native instructions that support this, hence why you should simulate such stack in that case.
STIA/STDA/STIB/STDB (Store increment after/decrement after/increment before/decrement before)
LDIA/STIA/STIB/STDB
BL/BLX (Branch and link, branch and link exchange)
B/BX (Branch/branch and exchange)
LDM/STM versions of each of the above LD/ST opcodes (Load/Store Multiple)
STMDB sp!, {r4-r7, lr} # Or as desired
... code ...
LDMIA sp!, {r4-r7, pc} # Or as desired
A longer form of the last one is
LDMIA sp!, {r4-r7, lr}
BX lr
* Thumb has PUSH/POP; they're a compressed form of STDB sp!/LDIA sp!. Thumb2 adds STDB/LDIA/STMDB/LDMIA opcodes to Thumb mode. You [i
can't[/i] have (efficient) upwards-moving stacks if you support Thumb code (as you probably want to do)
curious in C does the function malloc , free ...etc call the OS memory allocator functions.
It must at some level... so when I use malloc on windows and use malloc on linux they must be coded to call the kernels memory allocator for the different OS you are using.
Also Curious what the windows xp or linux memory allocator functions are called and if I can bypass the malloc / free function calls and call the kernel equivalent?
Or is malloc/ free the only way you can call the memory allocators of the os (indirectly)
The buddy allocator is really designed for physical memory, where allocations are in large chunks, although you could somehow "layer" it with another allocator and use it decently for a heap in virtual memory. Not that it won't work, but it's probably less apt and more prone to internal fragmentation than an algorithm like dlmalloc (glibc's allocator).
The heap in the kernel and the heap used by processes are completely separate by the way. The kernel heap is useful for storing kernel objects, but user programs (at least when using protection and/or multitasking) couldn't possibly use the same heap, because it returns kernelspace memory that is mapped across all address spaces. Fortunately, if you are porting an existing libc (like newlib), that libc will already contain a heap implementation for userspace, so you don't have to worry about that.