next up previous contents index
Next: 15.4 Restrictions and guarantees Up: 15 List-based Memory Manager: Previous: 15.2.2 Allocation priority

15.3 Example use

To make an LMM pool ready for use, a client generally proceeds in three stages:

  1. Initialize the LMM pool, using lmm_init.
  2. Add one or more memory regions to the LMM, using lmm_add_region.
  3. Add some free memory to the pool, using lmm_add_free. (The free memory added should overlap at least one of the regions added in step 2; otherwise it will simply be thrown away.)

Here is an example initialization sequence that sets up an LMM pool for use in a Unix-like environment, using an (initially) 1MB memory pool to service allocations. It uses only one region, which covers all possible memory addresses; this allows additional free memory areas to be added to the pool later regardless of where they happen to be located.

#include <oskit/lmm.h>

lmm_t lmm;
lmm_region_t region;

int setup_lmm()
{
  unsigned mem_size = 1024*1024;
  char *mem = sbrk(mem_size);
  if (mem == (char*)-1)
    return -1;

  lmm_init(&lmm);
  lmm_add_region(&lmm, &region, (void*)0, (oskit_size_t)-1, 0, 0);
  lmm_add_free(&lmm, mem, mem_size);

  return 0;
}

After the LMM pool is set up properly, memory blocks can be allocated from it using any of the lmm_alloc functions described in the reference section below, and returned to the memory pool using the lmm_free function.



University of Utah Flux Research Group