su_alloc.c File Reference


Detailed Description

Home-based memory management.

Author:
Pekka Pessi <Pekka.Pessi@nokia-email.address.hidden>.
Date:
Created: Thu Aug 19 01:12:25 1999 ppessi

#include "config.h"
#include <sofia-sip/su_config.h>
#include "sofia-sip/su_alloc.h"
#include "sofia-sip/su_alloc_stat.h"
#include "sofia-sip/su_errno.h"
#include <stdlib.h>
#include <stddef.h>
#include <memory.h>
#include <limits.h>
#include <assert.h>

Include dependency graph for su_alloc.c:


Enumerations

enum  {
  SUB_N,
  SUB_N_AUTO,
  SUB_P
}

Functions

void * su_home_new (int size)
 Create a new su_home_t object.
void * su_home_ref (su_home_t const *home)
 Create a new reference to a home object.
int su_home_desctructor (su_home_t *home, void(*destructor)(void *))
 Set destructor function.
int su_home_unref (su_home_t *home)
 Unreference a su_home_t object.
void * su_home_clone (su_home_t *parent, int size)
 Clone a su_home_t object.
int su_home_has_parent (su_home_t const *home)
 Return true if home is a clone.
void * su_alloc (su_home_t *home, int size)
 Allocate a memory block.
void su_free (su_home_t *home, void *data)
 Free a memory block.
void su_home_check (su_home_t const *home)
 Check home consistency.
su_home_tsu_home_create (void)
 Create an su_home_t object.
void su_home_destroy (su_home_t *home)
 Deinitialize a home object.
int su_home_init (su_home_t *home)
 Initialize an su_home_t object.
void su_home_deinit (su_home_t *home)
 Free memory blocks allocated through home.
int su_home_move (su_home_t *dst, su_home_t *src)
 Move allocations from a su_home_t object to another.
void su_home_preload (su_home_t *home, int n, int isize)
 Preload a memory home.
su_home_tsu_home_auto (void *area, int size)
 Preload a memory home from stack.
void * su_realloc (su_home_t *home, void *data, int size)
 Reallocate a memory block.
void * su_zalloc (su_home_t *home, int size)
 Allocate and zero a memory block.
void * su_salloc (su_home_t *home, int size)
 Allocate a structure.
int su_home_is_threadsafe (su_home_t const *home)
 Check if a memory home is threadsafe.
int su_home_mutex_lock (su_home_t *home)
 Obtain exclusive lock on home (if home is threadsafe).
int su_home_mutex_unlock (su_home_t *home)
 Release exclusive lock on home (if home is threadsafe).
void su_home_init_stats (su_home_t *home)
 Initialize statistics structure.
void su_home_get_stats (su_home_t *home, int include_clones, su_home_stat_t hs[1], int size)
 Retrieve statistics from memory home.

Enumeration Type Documentation

anonymous enum

Enumerator:
SUB_N  Initial size.
SUB_N_AUTO  Initial size for autohome.
SUB_P  Secondary probe.

Secondary probe must be relative prime with all sub_n values


Function Documentation

void* su_alloc ( su_home_t home,
int  size 
)

Allocate a memory block.

The function su_alloc() allocates a memory block of a given size.

If home is NULL, this function behaves exactly like malloc().

Parameters:
home pointer to home object
size size of the memory block to be allocated
Returns:
This function returns a pointer to the allocated memory block or NULL if an error occurred.

void su_free ( su_home_t home,
void *  data 
)

Free a memory block.

The function su_free() frees a single memory block. The home must be the owner of the memory block (usually the memory home used to allocate the memory block, or NULL if no home was used).

Parameters:
home pointer to home object
data pointer to the memory block to be freed

su_home_t* su_home_auto ( void *  area,
int  size 
)

Preload a memory home from stack.

The function su_home_auto() initalizes a memory home using an area allocated from stack. Poor mans alloca().

void su_home_check ( su_home_t const *  home  ) 

Check home consistency.

The function su_home_check() ensures that the home structure and all memory blocks allocated through it are consistent. It can be used to catch memory allocation and usage errors.

Parameters:
home Pointer to a memory home.

void* su_home_clone ( su_home_t parent,
int  size 
)

Clone a su_home_t object.

Clone a secondary home object used to collect multiple memoryf allocations under one handle. The memory is freed either when the cloned home is destroyed or when the parent home is destroyed.

An independent home object is created if NULL is passed as parent argument.

Parameters:
parent a parent object (may be NULL)
size size of home object
The memory home object allocated with su_home_clone() can be freed with su_home_unref().

Returns:
This function returns a pointer to an su_home_t object, or NULL upon an error.

su_home_t* su_home_create ( void   ) 

Create an su_home_t object.

The function su_home_create() creates a home object. A home object is used to collect multiple memory allocations, so that they all can be freed by calling su_home_destroy().

Returns:
This function returns a pointer to an su_home_t object, or NULL upon an error.
Deprecated:
Use su_home_clone() instead of su_home_create().

void su_home_deinit ( su_home_t home  ) 

Free memory blocks allocated through home.

The function su_home_deinit() frees the memory blocks associated with the home object.

Parameters:
home pointer to home object

void su_home_destroy ( su_home_t home  ) 

Deinitialize a home object.

The function su_home_destroy() frees a home object, and all memory blocks associated with it.

Parameters:
home pointer to a home object
Deprecated:
Use su_home_deinit() instead of su_home_destroy().

int su_home_init ( su_home_t home  ) 

Initialize an su_home_t object.

The function su_home_init() initializes an object derived from su_home_t.

Parameters:
home pointer to home object
Returns:
The function su_home_init() returns 0 when successful, or -1 upon an error.

int su_home_move ( su_home_t dst,
su_home_t src 
)

Move allocations from a su_home_t object to another.

The function su_home_move() moves allocations made through the src home object under the dst home object. It is handy, for example, if an operation allocates some number of blocks that should be freed upon an error. It uses a temporary home and moves the blocks from temporary to a proper home when successful, but frees the temporary home upon an error.

If src has destructor, it is called before starting to move.

Parameters:
dst destination home
src source home
Return values:
0 if succesful
-1 upon an error

void* su_home_new ( int  size  ) 

Create a new su_home_t object.

Create a home object used to collect multiple memory allocations under one handle. The memory allocations made using this home object is freed either when this home is destroyed.

Parameters:
size size of home object
The memory home object allocated with su_home_new() can be reclaimed with su_home_unref().

Returns:
This function returns a pointer to an su_home_t object, or NULL upon an error.

void su_home_preload ( su_home_t home,
int  n,
int  isize 
)

Preload a memory home.

The function su_home_preload() preloads a memory home.

int su_home_unref ( su_home_t home  ) 

Unreference a su_home_t object.

The function su_home_unref() decrements the reference count on a home object and destroys and frees it and the memory allocations using it.

Parameters:
home memory pool object to be unreferences
The function return values is

Return values:
1 if object was freed
0 if object is still alive

void* su_realloc ( su_home_t home,
void *  data,
int  size 
)

Reallocate a memory block.

The function su_realloc() allocates a memory block of size bytes. It copies the old block contents to the new block and frees the old block.

If home is NULL, this function behaves exactly like realloc().

Parameters:
home pointer to memory pool object
data pointer to old memory block
size size of the memory block to be allocated
Returns:
This function returns a pointer to the allocated memory block or NULL if an error occurred.

void* su_salloc ( su_home_t home,
int  size 
)

Allocate a structure.

The function su_salloc() allocates a structure with a given size, zeros it, and initializes the size field to the given size. The size field is the first in the structure.

Parameters:
home pointer to memory pool object
size size of the structure
Example
The structure is defined and allocated as follows:
   struct test {
     int   tst_size;
     char *tst_name;
     void *tst_ptr[3];
   };
 
   struct test *t;
   ...
   t = su_salloc(home, sizeof (*t)); 
   assert(t && t->t_size == sizeof (*t));
   t->name
After calling su_salloc() we get a pointer t to a struct test, initialized to zero except the
Returns:
This function returns a pointer to the allocated structure, or NULL upon an error.

void* su_zalloc ( su_home_t home,
int  size 
)

Allocate and zero a memory block.

The function su_zalloc() allocates a memory block with a given size from given memory home home and zeroes the allocated block.

Parameters:
home pointer to memory pool object
size size of the memory block
Note:
The memory home pointer home may be NULL. In that case, the allocated memory block is not associated with any memory home, and it must be freed by calling su_free() or free().
Returns:
The function su_zalloc() returns a pointer to the allocated memory block, or NULL upon an error.


Sofia-SIP 1.12.1 - Copyright (C) 2006 Nokia Corporation. All rights reserved. Licensed under the terms of the GNU Lesser General Public License.