Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom memory allocator/manager in C ? which approach?

I looking for some (custom) memory managers/allocator written in c and went through some articles, -

Some Links :

  • IBM - Inside memory management
  • Valgrind - How to Shadow Every Byte of Memory Used by a Program
  • Stack Overflow Question - Write your own memory manager
  • ned Productions - nedmalloc Homepage
  • Two-Level Segregate Fit (TLSF) - Website
  • Wikipedia - Dynamic memory allocation
  • Fourmilab - The BGET Memory Allocator

I have to sandbox a small web-server using any available one and I have no problem in writing wrappers for thread handling / allocation scheme. Apache WS uses memory pools for handling memory and the pools are not persistent, it is per-request basis. Can you guys suggest something? Some good / best approach to this problem ? My requirements are as below ;-

  1. (Bounded Response Time) allocation and de-allocation has to be known in advance, i.e some constant cost O(c), where c is constant.

  2. Fragmentation from heterogeneous allocation/de-allocation sizes or sequences should be handled, I can write the schema / wrapper to provide the same.

Truly appreciate you help and ideas!

like image 741
yadab Avatar asked Oct 13 '10 02:10

yadab


People also ask

What is custom memory allocator?

The memory allocator I wrote allocates a block of memory to the front of the buffer and it also writes its size. The free function removes the last block of allocated memory in the buffer. So the order of the malloc/free calls matter or it will not work.

What is memory allocation algorithm?

The buddy memory allocation technique is a memory allocation algorithm that divides memory into partitions to try to satisfy a memory request as suitably as possible. This system makes use of splitting memory into halves to try to give a best fit.

What are the types of memory allocation?

There are two types of memory allocations: Compile-time or Static Memory Allocation. Run-time or Dynamic Memory Allocation.


2 Answers

Fragmentation from heterogeneous allocation/de-allocation sizes or sequences should be handled, I can write the schema / wrapper to provide the same.

To avoid fragmentation, you will have to use a hybrid block allocation strategy. Hybrid here means different sized element blocks than having single sized element blocks i.e. The allocator (or a wrapper around it) should maintain blocks of different-sized elements(small, medium and large etc.). All allocation requests should be rounded up to the nearest block boundary. This strategy shall ensure you will not suffer from external fragmentation but can cause internal fragmentation. You can find more info at the following links:

http://www.cotsjournalonline.com/magazine/articles/view/101217/pg:2 http://people.freebsd.org/~jasone/jemalloc/bsdcan2006/jemalloc.pdf

like image 129
Suresh Kumar Avatar answered Oct 08 '22 06:10

Suresh Kumar


Just to add one more to your list

Google Performance Tools

It improves significantly memory allocation performance and it has CPU and memory profilers. Their Thread-Caching Malloc implementation is meant to be quite efficient for multithreaded applications.

like image 29
Manuel Salvadores Avatar answered Oct 08 '22 04:10

Manuel Salvadores