whalloc

Not logged in

Welcome to libwhalloc

whalloc (the WanderingHorse.net Allocator) is a small C library containing "alternate" memory allocators for C.

This site is a Fossil source repository, containing the source code, a wiki, bug tracker, etc. To be able to download the source code or use most of the hyperlinks, you must click the /login link and log in as "anonymous" with the password shown on the screen. This is to avoid that bots mindlessly waste gigabytes of bandwidth fishing through Fossil's network of hyperlinks, some of which generate a ZIP file of any given version of the whole repository.

Author: Stephan Beal (http://wanderinghorse.net) with input from Amir "Ashran" Mohammadkhani (http://modme.info)

License: Public Domain

Very brief overview:

This C library provides a several independent, but conceptually related, APIs which provide malloc()/free()-like features. The core allocator classes allocate memory from a user-specified memory range. The intention is to give it stack-allocated space, or a pre-malloc()ed block, and it will divide that space and use it for allocation.

This project started out as (and was intended to remain) a single allocator class, but it now has several different allocators, each with their own particular uses and trade-offs.

All of this code is part of my on-going fascination with reducing calls to malloc() in my libraries. More specifically, for libraries/applications where the memory requirements are highly predictable. The include APIs are intended for use cases where avoiding malloc() and friends is, for whatever reason, an important consideration, and for which the memory usage patterns can be predicted with a fair degree of accuracy. That said, memory usage for most applications cannot be well predicted, but sometimes specific parts of applications can get by with well-known amounts of memory. For such cases, this code might just be interesting.

The primary general-purpose allocator class, called whalloc_bt, provides the same features as the C-standard malloc(), free() and realloc(), and is used like this:

// Set up a stack-allocated memory area:
enum { BufLen = 1024 * 8 };
unsigned char buffer[BufLen];
// Set up our allocator/memory pool:
const whalloc_size_t blockSize = sizeof(my_type);
whalloc_bt pool = whalloc_bt_empty;
int rc = whalloc_bt_init(&pool, buffer, BufLen, blockSize );
if( rc ) { ... error ... }
my_type * m = (my_type *) whalloc_bt_alloc(&pool, blockSize );
// ^^^ Semantically equivalent to malloc(blockSize).
// ^^^ m now lives somewhere inside of buffer

// Cleaning up:
whalloc_bt_free( &pool, m );
// ^^^ semantically equivalent to free(m)

Since the memory buffer in the above example is statically allocated, we often don't need to clean up unless we also use our objects need it or we use the so-called fallback allocator option. The fallback option tells the pool to start using malloc() if its own memory runs out. In that case, the corresponding calls to free() will be handled by the pool if the memory passed to it is in the pool's managed range, otherwise it will be passed to free().

Features and Misfeatures

Features:

Misfeatures:

More information...

There are more details on the following pages:

News!

1 March 2010:

26 Feb 2010: