Bioplib
Protein Structure C Library
 All Data Structures Files Functions Variables Typedefs Macros Pages
Data Structures | Macros | Typedefs | Functions
safemem.c File Reference

Safe malloc()/free() routines which check for array overflow on free. More...

#include <stdio.h>
#include <stdlib.h>
#include "macros.h"
#include "SysDefs.h"

Go to the source code of this file.

Data Structures

struct  _memlist
 

Macros

#define CHECKSIZE   256 /* Default protection buffer size */
 
#define BLANK   (char)(0xFF) /* Default blanking character */
 
#define MAXBYTE   8 /* Max bytes occupied by an address */
 

Typedefs

typedef struct _memlist MEMLIST
 

Functions

void * blSafemalloc (int nbytes)
 
BOOL blSafefree (void *ptr)
 
void blSafeleaks (void)
 

Detailed Description

Safe malloc()/free() routines which check for array overflow on free.

Version
V1.3
Date
07.07.14
Author
Dr. Andrew C. R. Martin
Institute of Structural & Molecular Biology, University College London, Gower Street, London. WC1E 6BT.
andre.nosp@m.w@bi.nosp@m.oinf..nosp@m.org..nosp@m.uk andre.nosp@m.w.ma.nosp@m.rtin@.nosp@m.ucl..nosp@m.ac.uk

This code is NOT IN THE PUBLIC DOMAIN, but it may be copied according to the conditions laid out in the accompanying file COPYING.DOC.

The code may be modified as required, but any modifications must be documented so that the person responsible can be identified.

The code may not be sold commercially or included as part of a commercial product except as described in the file COPYING.DOC.

Description:

blSafemalloc() and blSafefree() are provided as temporary debugging replacements for malloc() and free(). They maintain their own linked list of malloc()'d memory and allocate a `protection buffer' each side of the requested amount of memory. This is filled with a given pattern and, when safefree() is called, this buffer is checked to ensure the pattern is still present.

Unlike free(), blSafefree() is of type BOOL, returning TRUE if an error occured.

SM_SIZE Environment Variable

The size of the buffer (default 256bytes each side) may be controlled by the environment variable SM_SIZE. If code continues to core dump, try increasing the value of SM_SIZE.

SM_FILL Environment Variable

The byte used for filling the protection buffers (default 255) is defined by the environment variable SM_FILL. If code which core dumps with normal malloc()/free(), but not with blSafemalloc() / blSafefree(), yet no error message is generated, the character which is corrupting memory is probably the same as the fill character. Try modifying SM_FILL.

Usage:

NOTE: safemem.h must be included after macros.h

Revision History:

Definition in file safemem.c.

Macro Definition Documentation

#define BLANK   (char)(0xFF) /* Default blanking character */

Definition at line 109 of file safemem.c.

#define CHECKSIZE   256 /* Default protection buffer size */

Definition at line 108 of file safemem.c.

#define MAXBYTE   8 /* Max bytes occupied by an address */

Definition at line 110 of file safemem.c.

Typedef Documentation

typedef struct _memlist MEMLIST

Function Documentation

BOOL blSafefree ( void *  ptr)

Debugging version of free() which checks protection buffers each side of the requested memory block.

  • 23.06.95 Original By: ACRM
  • 27.02.98 Added cast to ptr
  • 07.07.14 Use bl prefix for functions By: CTP

Definition at line 246 of file safemem.c.

void blSafeleaks ( void  )

Prints a list of any safemalloc()'d memory which was not freed

  • 03.07.06 Original By: ACRM
  • 07.07.14 Use bl prefix for functions By: CTP

Definition at line 322 of file safemem.c.

void* blSafemalloc ( int  nbytes)
Parameters
[in]nbytesNumber of bytes to allocate
Returns
Pointer to block of memory

Debugging version of malloc() which creates protection buffers each side of the requested memory block.

  • 23.06.95 Original By: ACRM
  • 07.07.14 Use bl prefix for functions By: CTP

Definition at line 145 of file safemem.c.