I have several heaps defined to differentiate blocks of internal RAM and external RAM for various uses. It appears I cannot use all of the heap area. I know there is overhead in establishing the heap and for each heap allocate. I am not concerned with that. What appears to be happening is I cannot allocate a block that is within 108 bytes of the end of the heap. I have the following code for my final allocation in my internal heap:

{

internal_heap = heap_lookup(2);

size = heap_space_remaining(internal heap); // returns with size = 284

buffer = new(internal_heap) s32[42]; // Works if size is 42

size = heap_space_remaining(internal heap); // returns with size = 108

}

The above code fails with kNewFailure if the allocation is increased to 43 instead of 42. Is there a reason for this failure?

My final allocation wants to be 48 32-bit values which should fit with some room to spare including the overhead of 8 bytes.

Any help is appreciated.

Rusty

There are 28 bytes of memory required at the start of the block for the heapdata information. The first 64-byte aligned address after that is the start of the memory that will be allocated which is why the total memory that can be allocated is 480 bytes rather than the full 512 in the example.

The allocation algorithm works by using the free list and a first-fit method, so on the first allocation it puts the biggest power of 2 less than 512 bytes block of memory on the free list for use, or the size requested if that's bigger (which is why the workaround method I suggested works). In the example the largest power of 2 used is 256 bytes. As that block is bigger than required it's split once it's on the free list. So when we asked for 42 ints, that requires 168 bytes for the allocation and 8 bytes for the heap block overhead, making 176 bytes, leaving 80 bytes available on the free list.

We will give some consideration to making all the memory available on the free list in heap_init by default rather than requesting it as needed by allocations as it does appear to be simpler and offer the opportunity of making bigger blocks available more successfully and should simply the heap support a little.

Regards,

Stuart.