3

Is there a way to create a heap at a specific memory location (separate from the system heap) that can be used for dynamic memory allocation, but makes use of the dynamic memory allocator built into the C library.

I'm looking for something functionally equivalent to this:

UserHeap heap(startAddress, size);

void* allocatedMemory1 = heap.Allocate(100);  //Allocate 100 bytes.
void* allocatedMemory2 = heap.Allocate(200);  //Allocate 200 bytes.

/* ... Do seomthing useful with the memory ... */

heap.Free(allocatedMemory1);
heap.Free(allocatedMemory2);

This uheap.h seems to have what I'm looking for, but this doesn't seem to exist in newlib. I'm wondering if GCC or newlib might have something. If not I think I may end up porting ptmalloc. However, its my understanding that this code is already in the C library and I'd hate to waste the memory reproducing it.

I'm using Sourcery Codebench Lite (GCC) with newlib (C library).

3
  • There's nothing in the standard library to support this (portably). Commented Nov 3, 2012 at 16:27
  • @JerryCoffin: I'm not worried about portability. This is an embedded system designed for one and only one kind of hardware. If there's some GCC-only/Newlib-only functions that I can make use of, I'd like to know what they are. Commented Nov 3, 2012 at 16:31
  • Way back in V7 Unix, IIRC, there was a nonstandard call donate(char *p, int size) that let you donate some memory to malloc's pool. It was useful (especially in those 64k address space days, that is) if you had a bunch of static data structures that were needed only during initialization, and you wanted to reuse their memory after you were done with them. Commented Aug 20, 2021 at 21:27

2 Answers 2

1

Rolling out your own allocator isn't all that hard and in many ways can be quite instructive. First you need to figure out what sort of allocation/deallocation pattern you want to support in that heap. For general use, allocators similar to dlmalloc (Doug Lea's) are quite good. There are many more kinds for specific usages: stack based (where a free is always the address of the last allocated unfreed memory block), fixed sized based (where the memory is divided in fixed sizes which are returned), queue based, small block, etc.

Most of these can have an internal allocator as well, for example suppose you have a fixed sized allocator, you could tell it to allocates in blocks of X bytes (from a second allocator or from the system) which then get divided in blocks of Y bytes. Pointers to chunks of size Y bytes are returned but when there is no free block, you allocate a new block of size X and divide it up.

For convenience, you can also implement the new/delete operators globally or on certain classes to redirect their allocations to your custom allocator. Placement new on your heap is also handy, see below.

I personally prefer using macros since it allows extra debug information to be added (function/line/etc) quite easily. ie:

#define CUSTOM_NEW(Heap) new(Heap, __FUNCTION__, __LINE__)
int* ptr = CUSTOM_NEW(someHeap) int[32];
Sign up to request clarification or add additional context in comments.

Comments

0

I finally came across a blog by Eli Bendersky that illustrates exactly what I was looking for. memmgr – a fixed-pool memory allocator

3 Comments

It doesn't appear to do what is stated in the OP where it specifically requests a heap at a given address (startAddress).
@MarkWilkins The source code is available. If you look at the source code you will see a static byte pool[POOL_SIZE] variable. This can easily be replaced with static byte pool = (byte*)0x60000000 which will make pool point to a specific address (0x60000000 in this case).
That seems good then. I thought from the OP that it needed to be specified at run time. Sorry for the misunderstanding.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.