Module Storage (1)
Posted: Sun Dec 03, 2023 4:19 am
I need to write my own heap memory allocator, and I am studying module Storage, which of course is a good starting point.
I have trouble understanding this part in the module initialisation:
As far as I understand, 'Heap Limit' (LinkOptions.HeapLimit) in the configuration defines how the memory is shared between the stack and the heap. The stack grows downwards from the its top below the module variables (LinkOptions.StackStart), and the heap grows upwards from the value of 'Heap Start' (LinkOptions.HeapStart).
If the value of 'Heap Limit is set to zero, there's dynamic sharing, else the heap is limited to the value set. With dynamic sharing, the heap can grow up to the current stack pointer at the time of the allocation. The stack can grow down without limitations, even into the heap space. (Without hardware support, it would be performance consuming to keep the stack pointer "in check" upon each procedure call.)
I would have expected that 'stackLimit' be set to 'heapLimit' if 'Heap Limit' is not zero. The constant '010000200H' is not an address within the 32 kB RAM range of my MO STM32F091 (the data range of the STM32F091 is 020000000H to 020008000H).
I am aware that the allocator does not use 'stackLimit', but 'Storage.StackAvailable' will return a wrong value with a non-zero 'Heap Limit' value, given the above assigment of the constant.
I have trouble understanding this part in the module initialisation:
Code: Select all
IF heapLimit = 0 THEN
stackLimit := 0
ELSE
(* Separate heap / stack *)
stackLimit := 010000200H
END;
If the value of 'Heap Limit is set to zero, there's dynamic sharing, else the heap is limited to the value set. With dynamic sharing, the heap can grow up to the current stack pointer at the time of the allocation. The stack can grow down without limitations, even into the heap space. (Without hardware support, it would be performance consuming to keep the stack pointer "in check" upon each procedure call.)
I would have expected that 'stackLimit' be set to 'heapLimit' if 'Heap Limit' is not zero. The constant '010000200H' is not an address within the 32 kB RAM range of my MO STM32F091 (the data range of the STM32F091 is 020000000H to 020008000H).
I am aware that the allocator does not use 'stackLimit', but 'Storage.StackAvailable' will return a wrong value with a non-zero 'Heap Limit' value, given the above assigment of the constant.