What is that?

This is the SLAB-like memory allocator for use in the user space applications. USlab stands for user space slab.

It is very portable and have no direct dependencies on the standard C library. It has been tested on Linux and Windows.

The technique itself is described in Bonwick94 and Bonwick01. The first one is absolutely must read if you want to write memory manager and it is recommended for better understanding of the ideas under USlab too.

This technique and its derivatives is widely used inside a OS kernels and as parts of the segregate user space memory allocators. It was first introduced in the SunOS 2.4 kernel.

The basic idea under it is that initialization of an object may be more costly than allocating memory for it, so it is worthwhile to keep some unused initialized objects for future use. So you ask a big chunk of the memory, split it into the smaller pieces and recycle them.

Basically, chunk can be called as slab, set of the slabs can be called a cache. Each slab is a few of the initialized objects.


USlab have a very simple API. It is described in the uslab.h. You need two files from the distribution: uslab.h and uslab.c. For usage examples see test.c file.

If you have special requirements (for example, you can't use standard C library), you need to specify few data types in the beginning of the uslab.h.

It should be stressed that it is not allocate memory directly from the OS. It means that you need to specify your functions for memory allocation and deallocation. It does not meaningful what upper lying allocator you will use - it can be simple wrappers on top of the C malloc()/free() or simple page supplier on top of the virtual memory API used in your OS. The following functions should be OK for you in most cases:

/* simple wrappers on top of the C library */
void *stdc_alloc_mem(size_t size, void *udata)
    /* NOTE: it should return NULL on error,
    /* just like malloc() does */
    return malloc(size);

void stdc_free_mem(void *p, void *udata)

Objects tagging

This allocator support objects tagging. You can assign to any slab cache integer value - a tag. All objects inside the slab cache share the same tag value.

Tag is a simple integer value. You can easily create them using enum in C/C++. It may be useful if you want to distinguish different object types (like in Lisp interpreter ;).

Memory usage and performance

Following the terminology given in the above noted papers USlab is the implementation of the large slabs, so for a small objects (up to 512 bytes) it can be not so effective as for large objects. But not go away!

It is generally as memory effective as any modern memory manager. Typically, any malloc() also allocates additionally 8-32 bytes per any allocation. USlab allocates additionally (roughly) 4-8 bytes (depending on the machine word size). So you will not waste too much.

If you need to allocate/deallocate objects often USlab can give you up to 30-40% (or even more) additional performance compared to raw allocation using malloc()/free(). When you want to allocate many objects and deallocate them all when they are became unneeded, you can just destroy the cache - it will twice faster then using standard malloc()/free() for each object (tested using glibc malloc()/free()).

I hope you will find it useful. Enjoy!