1 /* @include ajmem *************************************************************
2 **
3 ** AJAX memory functions
4 **
5 ** @author Copyright (C) 1999 Peter Rice
6 ** @version $Revision: 1.15 $
7 ** @modified Peter Rice pmr@ebi.ac.uk
8 ** @modified $Date: 2011/10/18 14:23:40 $ by $Author: rice $
9 **
10 ** This library is free software; you can redistribute it and/or
11 ** modify it under the terms of the GNU Lesser General Public
12 ** License as published by the Free Software Foundation; either
13 ** version 2.1 of the License, or (at your option) any later version.
14 **
15 ** This library is distributed in the hope that it will be useful,
16 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 ** Lesser General Public License for more details.
19 **
20 ** You should have received a copy of the GNU Lesser General Public
21 ** License along with this library; if not, write to the Free Software
22 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
23 ** MA  02110-1301,  USA.
24 **
25 ******************************************************************************/
26 
27 #ifndef AJMEM_H
28 #define AJMEM_H
29 
30 /* ========================================================================= */
31 /* ============================= include files ============================= */
32 /* ========================================================================= */
33 
34 #include "ajdefine.h"
35 
36 AJ_BEGIN_DECLS
37 
38 
39 
40 
41 /* ========================================================================= */
42 /* =============================== constants =============================== */
43 /* ========================================================================= */
44 
45 
46 
47 
48 /* ========================================================================= */
49 /* ============================== public data ============================== */
50 /* ========================================================================= */
51 
52 
53 
54 
55 /* ========================================================================= */
56 /* =========================== public functions ============================ */
57 /* ========================================================================= */
58 
59 #ifdef HAVE_MEMMOVE
60 #define ajMemMove(d,s,l) memmove(d,s,l)
61 #else /* !HAVE_MEMMOVE */
62 #define ajMemMove(d,s,l) bcopy(s,d,l)
63 #endif /* !HAVE_MEMMOVE */
64 
65 
66 
67 
68 /*
69 ** Prototype definitions
70 */
71 
72 void *ajMemAlloc(size_t nbytes,
73                  const char *file, ajint line, AjBool nofail);
74 void *ajMemCalloc(size_t count, size_t nbytes,
75                   const char *file, ajint line, AjBool nofail);
76 void *ajMemCallocZero(size_t count, size_t nbytes,
77                       const char *file, ajint line, AjBool nofail);
78 void ajMemFree(void **ptr);
79 void *ajMemResize(void *ptr, size_t nbytes,
80                   const char *file, ajint line, AjBool nofail);
81 void *ajMemResizeZero(void *ptr, size_t oldbytes, size_t nbytes,
82                       const char *file, ajint line, AjBool nofail);
83 void ajMemSetZero(void* ptr, size_t count, size_t nbytes);
84 
85 ajint *ajMemArrB(size_t size);
86 ajint *ajMemArrI(size_t size);
87 float *ajMemArrF(size_t size);
88 void ajMemStat(const char* title);
89 void ajMemExit(void);
90 void ajMemCheck(int istat);
91 void ajMemCheckSetLimit(ajint maxfail);
92 void ajMemProbe(void* ptr, const char* file, ajint line);
93 
94 /*
95 ** End of prototype definitions
96 */
97 
98 
99 
100 
101 #define AJALLOC(nbytes)                                 \
102     ajMemAlloc((nbytes), __FILE__, __LINE__, AJFALSE)
103 #define AJALLOC0(nbytes)                                        \
104     ajMemCallocZero(1, (nbytes), __FILE__, __LINE__, AJFALSE)
105 #define AJCALLOC(count, nbytes)                                 \
106     ajMemCalloc((count), (nbytes), __FILE__, __LINE__, AJFALSE)
107 #define AJCALLOC0(count, nbytes)                                        \
108     ajMemCallocZero((count), (nbytes), __FILE__, __LINE__, AJFALSE)
109 
110 #define AJNEW(p) ((p) = AJALLOC(sizeof *(p)))
111 #define AJCNEW(p,c) ((p) = AJCALLOC((size_t)c, sizeof *(p)))
112 #define AJNEW0(p) ((p) = AJCALLOC0((size_t)1, sizeof *(p)))
113 #define AJCNEW0(p,c) ((p) = AJCALLOC0((size_t)c, sizeof *(p)))
114 #define AJSET0(p) (ajMemSetZero((p), (size_t)1, sizeof *(p)))
115 #define AJCSET0(p,c) (ajMemSetZero((p), (size_t)c, sizeof *(p)))
116 
117 #define AJFREE(ptr) ((void)(ajMemFree((void**)&ptr)))
118 #define AJRESIZE(ptr, nbytes)                                           \
119     ((ptr) = ajMemResize((ptr), (nbytes), __FILE__, __LINE__, AJFALSE))
120 #define AJRESIZE0(ptr, oldbytes, nbytes)                        \
121     ((ptr) = ajMemResizeZero((ptr), (oldbytes), (nbytes),       \
122                              __FILE__, __LINE__, AJFALSE))
123 #define AJCRESIZE(ptr, nbytes)                          \
124     ((ptr) = ajMemResize((ptr), (nbytes)*sizeof *(ptr), \
125                          __FILE__, __LINE__, AJFALSE))
126 #define AJCRESIZE0(ptr, oldbytes, nbytes)                       \
127     ((ptr) = ajMemResizeZero((ptr), (oldbytes)*sizeof *(ptr),   \
128                              (nbytes)*sizeof *(ptr),            \
129                              __FILE__, __LINE__, AJFALSE))
130 #define AJCRESIZETRY(ptr, nbytes)                       \
131     ((ptr) = ajMemResize((ptr), (nbytes)*sizeof *(ptr), \
132                          __FILE__, __LINE__, AJTRUE))
133 #define AJCRESIZETRY0(ptr, oldbytes, nbytes)                    \
134     ((ptr) = ajMemResizeZero((ptr), (oldbytes)*sizeof *(ptr),   \
135                              (nbytes)*sizeof *(ptr),            \
136                              __FILE__, __LINE__, AJTRUE))
137 #define AJMPROBE(ptr)                           \
138     ajMemProbe(ptr, __FILE__, __LINE__)
139 
140 
141 
142 
143 #ifdef AJ_COMPILE_DEPRECATED_BOOK
144 #endif /* AJ_COMPILE_DEPRECATED_BOOK */
145 
146 #ifdef AJ_COMPILE_DEPRECATED
147 
148 __deprecated void *ajMemCalloc0(size_t count, size_t nbytes,
149                                 const char *file, ajint line, AjBool nofail);
150 
151 #endif /* AJ_COMPILE_DEPRECATED */
152 
153 
154 
155 
156 AJ_END_DECLS
157 
158 #endif /* !AJMEM_H */
159