next up previous contents index
Next: Functions for allocating/freeing memory Up: Memory Functions Previous: Basic Memory Functions

Fast Functions for Large Numbers of Small Blocks

  Many applications need lots of equally sized small memory blocks which have to be allocated and freed very fast.

Normally one would write some sort of pointer arithmetic or free list management, one for each structure, which splits superblocks into many small blocks. For simplicity these functions will not free any memory if not everything at a time, sometimes the capacity is limited.

The easy way is to use the GRAPE utility functions designed for this purpose. The following functions are a low level application of mem_alloc/mem_free implementing a configurable free list manager which will free unused superblocks if possible, at the only penalty of losing at most sizeof(void *) bytes per block.

void *g_bmm_new (size_t blocksize, size_t alloc_number)
Create a new block memory manager which is returned, which manages blocks of size blocksize, allocating alloc_number at a time.
void g_bmm_destroy (void *bmm)
Totally destroy a block memory manager and all its managed blocks.
int g_bmm_is_empty (void *bmm)
TRUE, if there are no managed blocks.
void *g_bmm_alloc (void *bmm)
Return a new block.
void g_bmm_free (void *bmm, void *ptr)
Free a block.
void g_bmm_new_num (void *bmm, size_t new_alloc_number)
Change the alloc_number of bmm, which will be used for all superblock allocations. This does not influence already allocated ones.
void g_bmm_shrink (void *bmm)
If you free some blocks one empty superblock is kept for further allocations, as a sort of cache. Free this one.

You should call this function after having done a lot of memory operations.

void *g_bmm_get_std (size_t blocksize)
Return the system wide standarb block memory manager for this blocksize. Of course it cannot be destroyed using g_bmm_destroy, you have to call g_bmm_free for any block. Though using the standard manager saves memory, because there are not so many half used superblocks.

An example using a private block memory manager, all blocks are freed with one function call at the end:

{
  void *bmm;
  struct my_struct *p[1000];
  int i;
  
  bmm = g_bmm_new (sizeof (my_struct), 100);

  /* use manager */
  for (i = 0; i < 1000; i++)
    p[i] = g_bmm_alloc (bmm);
  for (i = 0; i < 100; i++)
    g_bmm_free (bmm, p[i*10]);
  for (i = 0; i < 10; i++)
    p[i*100] = g_bmm_alloc (bmm);
  ...
  g_bmm_destroy (bmm);
}
An example using the system wide standard block memory manager for this blocksize:
{
  void *bmm;
  struct my_struct *p[1000];
  int i;
  
  bmm = g_bmm_get_std (sizeof (my_struct));

  /* use manager */
  for (i = 0; i < 1000; i++)
    p[i] = g_bmm_alloc (bmm);
  ...
  for (i = 0; i < 1000; i++)
    g_bmm_free (bmm, p[i]);
}

next up previous contents index
Next: Functions for allocating/freeing memory Up: Memory Functions Previous: Basic Memory Functions

SFB 256 Universität Bonn and IAM Universität Freiburg

Copyright © by the Sonderforschungsbereich 256 at the Institut für Angewandte Mathematik, Universität Bonn.