Main Page | Modules | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

zalloc.h File Reference


Detailed Description

Zone allocator.

Author:
Raphael Manfredi
Date:
2002-2003

#include <glib.h>

Go to the source code of this file.

Data Structures

struct  zone
 Zone structure. More...


Defines

#define ZALLOC_ALIGNBYTES   MEM_ALIGNBYTES
#define ZALLOC_MASK   (ZALLOC_ALIGNBYTES - 1)
#define zalloc_round(s)   ((gulong) (((gulong) (s) + ZALLOC_MASK) & ~ZALLOC_MASK))
#define zcount(z)   ((z)->zn_cnt) /**< Amount of allocated blocks */
 Amount of allocated blocks.


Typedefs

typedef zone zone_t

Functions

zone_tzcreate (gint, gint)
 Create a new zone able to hold items of 'size' bytes.

zone_tzget (gint, gint)
 Get a zone suitable for allocating blocks of 'size' bytes.

void zdestroy (zone_t *zone)
 Destroy a zone chunk by releasing its memory to the system if possible, converting it into a malloc chunk otherwise.

gpointer zalloc (zone_t *)
 Allcate memory with fixed size blocks (zone allocation).

void zfree (zone_t *, gpointer)
 Return block to its zone, hence freeing it.


Define Documentation

#define ZALLOC_ALIGNBYTES   MEM_ALIGNBYTES
 

#define ZALLOC_MASK   (ZALLOC_ALIGNBYTES - 1)
 

#define zalloc_round s   )     ((gulong) (((gulong) (s) + ZALLOC_MASK) & ~ZALLOC_MASK))
 

#define zcount  )     ((z)->zn_cnt) /**< Amount of allocated blocks */
 

Amount of allocated blocks.


Typedef Documentation

typedef struct zone zone_t
 


Function Documentation

gpointer zalloc zone_t zone  ) 
 

Allcate memory with fixed size blocks (zone allocation).

Returns a pointer to a block containing at least 'size' bytes of memory. It is a fatal error if memory cannot be allocated.

A zone is, in its simplest expression, a memory chunk where fix-sized blocks are sliced, all free blocks being chained together via a link written in the first bytes of the block. To allocate a block, the first free block is removed from the list. Freeing is just as easy, since we insert the block at the head of the free list.

Zone chunks are linked together to make a bigger pool, where only the first zone descriptor is accurate (i.e. only it has meaningful zn_cnt and zn_free fields).

The advantages for allocating from a zone are:

  • very fast allocation time.
  • absence of block header overhead.
  • no risk of memory fragmentation.

The disadvantages are:

  • need to allocate the zone before allocating items.
  • need to pass-in the zone descriptor each time.
  • programmer must be careful to return each block to its native zone.

Moreover, periodic calls to the zone gc are needed to collect unused chunks when peak allocations are infrequent or occur at random.

< Allocated block

zone_t* zcreate gint  size,
gint  hint
 

Create a new zone able to hold items of 'size' bytes.

Returns NULL if no new zone can be created.

The hint argument is to be construed as the average amount of objects that are to be created per zone chunks. That is not the total amount of expected objects of a given type. Leaving it a 0 selects the default hint value.

void zdestroy zone_t zone  ) 
 

Destroy a zone chunk by releasing its memory to the system if possible, converting it into a malloc chunk otherwise.

void zfree zone_t zone,
gpointer  ptr
 

Return block to its zone, hence freeing it.

Previous content of the block is lost.

Since a zone consists of blocks with a fixed size, memory fragmentation is not an issue. Therefore, the block is returned to the zone by being inserted at the head of the free list.

Warning: returning a block to the wrong zone may lead to disasters.

zone_t* zget gint  size,
gint  hint
 

Get a zone suitable for allocating blocks of 'size' bytes.

`hint' represents the desired amount of blocks per subzone.

This is mainly intended for external clients who want distinct zones for distinct sizes, yet may share zones for distinct albeit same-sized blocks. For instance, walloc() requests zones for power-of-two sizes and uses zget() to get the zone, instead of zcreate() to maximize sharing.


Generated on Sun Feb 12 10:50:10 2006 for Gtk-Gnutella by doxygen 1.3.6