Bioplib
Protein Structure C Library
 All Data Structures Files Functions Variables Typedefs Macros Pages
safemem.h
Go to the documentation of this file.
1 /************************************************************************/
2 /**
3 
4  \file safemem.h
5 
6  \version V1.4
7  \date 14.08.14
8  \brief Safe malloc()/free() routines which check for array
9  overflow on free.
10 
11  \copyright (c) UCL / Dr. Andrew C. R. Martin 1995-2014
12  \author Dr. Andrew C. R. Martin
13  \par
14  Institute of Structural & Molecular Biology,
15  University College London,
16  Gower Street,
17  London.
18  WC1E 6BT.
19  \par
20  andrew@bioinf.org.uk
21  andrew.martin@ucl.ac.uk
22 
23 **************************************************************************
24 
25  This code is NOT IN THE PUBLIC DOMAIN, but it may be copied
26  according to the conditions laid out in the accompanying file
27  COPYING.DOC.
28 
29  The code may be modified as required, but any modifications must be
30  documented so that the person responsible can be identified.
31 
32  The code may not be sold commercially or included as part of a
33  commercial product except as described in the file COPYING.DOC.
34 
35 **************************************************************************
36 
37  Description:
38  ============
39 
40 
41 **************************************************************************
42 
43  Usage:
44  ======
45  N.B. This header file should be included *after* macros.h
46 
47 **************************************************************************
48 
49  Revision History:
50  =================
51 - V1.0 23.06.95 Original
52 - V1.1 03.07.06 Added safeleaks() prototype
53 - V1.2 07.07.14 Use bl prefix for functions By: CTP
54 - V1.3 31.07.14 Updated deprecation: Removed deprecated.h and added
55  prototypes for renamed functions. By: CTP
56 - V1.4 14.08.14 Moved deprecated function prototypes to deprecated.h
57  Use blSafefree instead of safefree for macros.
58  By: CTP
59 
60 *************************************************************************/
61 #ifndef _SAFEMEM_H
62 #define _SAFEMEM_H
63 
64 /* Includes */
65 #include "SysDefs.h"
66 
67 /* Prototypes */
68 void *blSafemalloc(int nbytes);
69 BOOL blSafefree(void *ptr);
70 void blSafeleaks(void);
71 
72 
73 /************************************************************************/
74 /* Include deprecated functions */
75 #define _SAFEMEM_H_DEPRECATED
76 # include "deprecated.h"
77 /************************************************************************/
78 
79 
80 /************************************************************************/
81 /* Undefine memory macros defined by macros.h */
82 #ifdef _MACROS_H
83 #undef INIT
84 #undef INITPREV
85 #undef ALLOCNEXT
86 #undef ALLOCNEXTPREV
87 #undef FREELIST
88 #undef DELETE
89 #endif
90 
91 /* Redefine macros to use safe versions of malloc()/free() */
92 #define INIT(x,y) do { x=(y *)blSafemalloc(sizeof(y)); \
93  if(x != NULL) x->next = NULL; } while(0)
94 #define INITPREV(x,y) do { x=(y *)blSafemalloc(sizeof(y)); \
95  if(x != NULL) {x->next = NULL; x->prev = NULL;} } \
96  while(0)
97 #define ALLOCNEXT(x,y) do { (x)->next=(y *)blSafemalloc(sizeof(y)); \
98  if((x)->next != NULL) { (x)->next->next=NULL; } \
99  NEXT(x); } while(0)
100 #define ALLOCNEXTPREV(x,y) do { (x)->next=(y *)blSafemalloc(sizeof(y)); \
101  if((x)->next != NULL) \
102  { (x)->next->prev = (x); \
103  (x)->next->next=NULL; } \
104  NEXT(x);} while(0)
105 /* FREELIST takes 2 parameters:
106  y: name of list
107  z: type of list
108 */
109 #define FREELIST(y,z) while((y)!=NULL) \
110  { z *_freelist_macro_q; \
111  _freelist_macro_q = (y)->next; \
112  blSafefree((char *)(y)); \
113  (y) = _freelist_macro_q; \
114  }
115 #define ORDFREELIST(y,z) while((y)!=NULL) \
116  { z *_freelist_macro_q; \
117  _freelist_macro_q = (y)->next; \
118  free((char *)(y)); \
119  (y) = _freelist_macro_q; \
120  }
121 
122 /*>DELETE(start, item, type)
123  -------------------------
124  Deletes item from a linked list
125  start may be modified if item is the first in the list
126  item is returned as the pointer to the next item in the list (i.e.
127  as item->next). One can therefore simply call the routine N time
128  to delete N items. If start or item is NULL, does nothing
129 
130 - 16.02.95 Original By: ACRM
131 */
132 #define DELETE(x, y, z) \
133 do { \
134  z *_delete_macro_p, \
135  *_delete_macro_prev = NULL, \
136  *_delete_macro_temp, \
137  *_delete_macro_temp2; \
138  if((x)!=NULL && (y)!=NULL) \
139  { \
140  for(_delete_macro_p=(x); \
141  _delete_macro_p!=NULL; \
142  NEXT(_delete_macro_p)) \
143  { \
144  if(_delete_macro_p == (y)) \
145  { \
146  _delete_macro_temp2 = (y)->next; \
147  if(_delete_macro_prev == NULL) \
148  { \
149  _delete_macro_temp = (x)->next; \
150  blSafefree(x); \
151  (x) = _delete_macro_temp; \
152  break; \
153  } \
154  else \
155  { \
156  _delete_macro_prev->next = _delete_macro_p->next; \
157  blSafefree(_delete_macro_p); \
158  } \
159  } \
160  _delete_macro_prev = _delete_macro_p; \
161  } \
162  (y) = _delete_macro_temp2; \
163  } \
164 } while(FALSE)
165 
166 #define SAFEINIT(x,y) INIT(x,y)
167 #define SAFEINITPREV(x,y) INITPREV(x,y)
168 #define SAFEALLOCNEXT(x,y) ALLOCNEXT(x,y)
169 #define SAFEALLOCNEXTPREV(x,y) ALLOCNEXTPREV(x,y)
170 #define SAFEFREELIST(y,z) FREELIST(y,z)
171 #define SAFEDELETE(x, y, z) DELETE(x, y, z)
172 
173 #endif
short BOOL
Definition: SysDefs.h:64
Redirect calls to deprecated functions.
void * blSafemalloc(int nbytes)
Definition: safemem.c:145
void blSafeleaks(void)
Definition: safemem.c:322
System-type variable type definitions.
BOOL blSafefree(void *ptr)
Definition: safemem.c:246