Home

Awesome

About

This is a drop-in replacement for malloc/free that is designed from the ground up to be used in scalable server software.

Why another memory allocator?

Here are the advantages of our allocator:

The disadvantages:

Note: there is only limited support for the functions memalign, posix_memalign and valloc.

Usage

Build requirements:

For building the library, you will need:

Usage requirements:

Compiling

Edit Makefile to set your build options; type make to build a static and a shared version of the library.

Note: we recommend integrating the allocator into your own project(s). The enclosed Makefile is intended only as a demonstration, not a complete build solution.

Using the library

There are five options:

  1. Compile into a statically-linked application. For this, you must:

    • Compile your application's object files as normal.
    • Compile lite-malloc.cpp, or use the liblite-malloc-static.a generated with the enclosed Makefile.
    • Link your app with the -static flag, and these linker flags for wrapping:
    -Wl,--wrap,malloc -Wl,--wrap,free -Wl,--wrap,calloc -Wl,--wrap,realloc
    -Wl,--wrap,memalign -Wl,--wrap,valloc -Wl,--wrap,posix_memalign
    
  2. Link with a shared library. Linking with liblite-malloc-shared.so should work.

  3. Via standard glibc malloc hooks. (Deprecated.) To enable, just include lite-hooks.h as the first line of your program.

  4. Via the magic LD_PRELOAD environment variable:

    $ LD_PRELOAD=<full pathname>/liblite-malloc-shared.so <your application>
    
  5. Compile a malloc hook static library into your dynamically-linked application. Follow the instructions in option 1, except omit the -static flag, and add these linker flags:

    -Wl,--defsym=malloc=__wrap_malloc -Wl,--defsym=free=__wrap_free -Wl,--defsym=calloc=__wrap_calloc
    -Wl,--defsym=realloc=__wrap_realloc -Wl,--defsym=memalign=__wrap_memalign
    -Wl,--defsym=valloc=__wrap_valloc -Wl,--defsym=posix_memalign=__wrap_posix_memalign
    

Important notes:

Linux has an OS-imposed limit on the number of virtual memory mappings. If you want to allocate large amounts of memory, you may need to adjust this limit via /proc/sys/vm/max_map_count.

Configuration

The allocator can be tuned with compile-time defines. Here are the supported options:

LITE_MALLOC_SUPERBLOCK_CACHE_SIZE

(default is 32)

Mmap blocks of size LITE_MALLOC_SUPERBLOCK_CACHE_SIZE * 4096 bytes and less will be cached. (With a separate cache per size / 4096 bytes.)

LITE_MALLOC_MINIBLOCK_CACHE_SIZE

(default is 1024)

Allocations of size LITE_MALLOC_MINIBLOCK_CACHE_SIZE * 8 bytes and less will be cached. (With a separate cache per size / 8 bytes.)

LITE_MALLOC_ENGINES_COUNT

(default is 32)

This many independent allocator engines will be used.

LITE_MALLOC_USE_PERTHREAD_ENGINES

(default is 1)

If 1, pin a specific allocator engine to each thread. If 0, choose an allocator engine round-robin for each allocation.

(Use 1 unless you have threads with highly unbalanced allocation workloads and are seeing dramatic overhead.)

Credits

https://github.com/Begun/lockfree-malloc

This library is licensed under the GNU LGPL. Please see the file LICENSE.