Main Page | Data Structures | File List | Globals

libgu/malloc.c File Reference

memory allocator for PPR programs More...

#include "before_system.h"
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "gu.h"

Defines

#define DODEBUG(a)

Functions

void * gu_alloc (size_t number, size_t size)
 Allocate a memory block to hold an array.

char * gu_strdup (const char *string)
 Duplicate a string.

char * gu_strndup (const char *string, size_t len)
 Duplicate the initial segment of a string.

char * gu_restrdup (char *ptr, size_t *number, const char *string)
 Copy a string into a preexisting block, resizing if necessary.

void * gu_realloc (void *ptr, size_t number, size_t size)
 Change the size of an already allocated array.

void gu_free (void *ptr)
 Free memory.


Variables

int gu_alloc_blocks = 0
 Number of blocks currently allocated, used for debugging.


Detailed Description

memory allocator for PPR programs

This file contains the routines which PPR uses to allocate and free memory.

The normal Unix memory allocation routines return a NULL pointer if they fail to allocate the requested memory. Since it is very rare that a Unix system will refuse to allocate any reasonable amount of memory, programers are often emboldened to ignore the possiblity of malloc() and friends returning NULL. We don't ignore such a possibility, but since a memory allocation failure is highly unlikely, PPR programs treats it as a fatal error, but, rather than test the return value of malloc() after each call, the calls are encapsulated in special functions which test the return value and abort the program if the allocation fails.


Function Documentation

void* gu_alloc size_t  number,
size_t  size
 

Allocate a memory block to hold an array.

The function gu_alloc() takes two arguments. The first is the number of items to allocate, the second is the size of each in bytes. This function will return a void pointer to the allocated memory. The memory is not initialized.

void gu_free void *  ptr  ) 
 

Free memory.

The function gu_free() is used to free any memory allocated by the other functions.

void* gu_realloc void *  ptr,
size_t  number,
size_t  size
 

Change the size of an already allocated array.

The function gu_realloc() changes the size of a memory block. The first argument is a pointer to the old block, the second is the desired new number of members, the third argument is the size of each member in bytes. This function returns a pointer to a resized block, possibly at a different location.

char* gu_strdup const char *  string  ) 
 

Duplicate a string.

The function gu_strdup() takes a string pointer as its sole argument and returns a pointer to a new copy of the string.

char* gu_strndup const char *  string,
size_t  len
 

Duplicate the initial segment of a string.

The function gu_strndup() takes a string pointer and a maximum length as its arguments. It returns a pointer to a new string containing a copy of the string truncated to the maximum length.


Generated on Fri Feb 20 15:17:52 2004 for PPR Libraries by doxygen 1.3.5