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

zalloc.c File Reference


Detailed Description

Zone allocator.

Author:
Raphael Manfredi
Date:
2002-2003

#include "common.h"
#include "zalloc.h"
#include "override.h"

Data Structures

struct  subzone
 Extra allocated zones. More...


Defines

#define DEFAULT_HINT   128 /**< Default amount of blocks in a zone */
 Default amount of blocks in a zone.

#define MAX_ZONE_SIZE   16384 /**< Maximum zone size */
 Maximum zone size.


Functions

 RCSID ("$Id:zalloc.c, v 1.8 2005/09/10 10:10:36 daichik Exp $")
void zn_cram (zone_t *, gchar *, gint)
 Cram a new zone in chunk.

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

gchar ** zn_extend (zone_t *)
 Extend zone by allocating a new zone chunk.

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

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

zone_tzcreate (gint size, gint hint)
 Create a new zone able to hold items 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.

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


Define Documentation

#define DEFAULT_HINT   128 /**< Default amount of blocks in a zone */
 

Default amount of blocks in a zone.

#define MAX_ZONE_SIZE   16384 /**< Maximum zone size */
 

Maximum zone size.


Function Documentation

RCSID "$Id:zalloc.  c,
v 1.8 2005/09/10 10:10:36 daichik Exp $" 
 

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.

void zn_cram zone_t zone,
gchar *  arena,
gint  size
[static]
 

Cram a new zone in chunk.

A zone consists of linked blocks, where the address of the next free block is written in the first bytes of each free block.

zone_t * zn_create zone_t zone,
gint  size,
gint  hint
[static]
 

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

gchar ** zn_extend zone_t zone  )  [static]
 

Extend zone by allocating a new zone chunk.

Returns:
the address of the first new free block within the extended chunk arena.


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