1

Is there any malloc/realloc/free like implementation where i can specify a memory region where to manage the memory allocation?

I mean regular malloc (etc.) functions manages only the heap memory region. What if I need to allocate some space in a shared memory segment or in a memory mapped file?

Dankó Dávid
  • 302
  • 3
  • 17
  • What do you mean by "allocate some space in a shared memory segment or in a memory mapped file"? Do you want to do that using `malloc()` or similar functions? – Andrew Henle Jan 29 '16 at 10:34
  • I think you are trying to do something that won't work as you expect it to. Shared-memory regions are typically mapped to different addresses so using pointers into them is, let's say, tricky. – fuz Jan 29 '16 at 10:40
  • 1
    Yes i need same functionality like malloc/realloc/free, but like this: cust_malloc(struct* mem_region, size_t length), and so on. I know malloc implementation is more complex as firs looks like, it's manages "formatting" the heap memory, starts brk calls for the kernel (for more memory or for shrinking) and take care about concurrent memory allocations. – Dankó Dávid Jan 29 '16 at 12:12
  • I would like to use to share (or store) data structures which has no pointer but countinous data like wecbam captured image, PCM sample, long string, which is basically a structure with some data and a (linear) array. – Dankó Dávid Jan 29 '16 at 12:19

4 Answers4

1

Not 100 %, As per your question you want to maintain your own memory region. so you need to go for your own my_malloc, my_realloc and my_free

Implementing your own my_malloc may help you

void* my_malloc(int size)    
{
    char* ptr = malloc(size+sizeof(int));
    memcpy(ptr, &size, sizeof(int));
    return ptr+sizeof(int); 
}

This is just a small idea, full implementation will take you to the answer.

Refer this question

use the same method to achieve my_realloc and my_free

Community
  • 1
  • 1
Embedded C
  • 1,448
  • 3
  • 16
  • 29
  • That's prone to failure. `malloc()` is required to return a value *suitably aligned so that it may be assigned to a pointer to any type of object with a fundamental alignment requirement ...* ([**7.22.3 Memory management functions**](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf)) Offsetting the value returned from `malloc()` by `sizeof(int)` breaks that requirement. – Andrew Henle Jan 29 '16 at 11:47
1

I asked myself this question recently too, because I wanted a malloc implementation for my security programs which could safely wipe out a static memory region just before exit (which contains sensitive data like encryption keys, passwords and other such data).

First, I found this. I thought it could be very good for my purpose, but I really could not understand it's code completely. The license status was also unclear, as it is very important for one of my projects too.

I ended up writing my own. My own implementation supports multiple heaps at same time, operating over them with pool descriptor structure, automatic memory zeroing of freed blocks, undefined behavior and OOM handlers, getting exact usable size of allocated objects and testing that pointer is still allocated, which is very sufficient for me. It's not very fast and it is more educational grade rather than professional one, but I wanted one in a hurry.

Note that it does not (yet) knows about alignment requirements, but at least it returns an address suitable for storing an 32 bit integer.

Mike Pelley
  • 2,939
  • 22
  • 23
ElectroRys
  • 11
  • 3
0

Iam using Tasking and I can store data in a specific space of memory. For example I can use:

testVar _at(0x200000);

I'm not sure if this is what you are looking for, but for example I'am using it to store data to external RAM. But as far as I know, it's only workin for global variables.

xy36
  • 315
  • 1
  • 11
0

It is not very hard to implement your own my_alloc and my_free and use preferred memory range. It is simple chain of: block size, flag free/in use, and block data plus final-block marker (e.g. block size = 0). In the beginning you have one large free block and know its address. Note that my_alloc returns the address of block data and block size/flag are few bytes before.

i486
  • 6,491
  • 4
  • 24
  • 41