6 Evolved over several years, starting with the initial
7 SMARTALLOC code for AutoSketch in 1986, guided by the Blind
8 Watchbreaker, John Walker. Isolated in this general-purpose
9 form in September of 1989. Updated with be more POSIX
10 compliant and to include Web-friendly HTML documentation in
11 October of 1998 by the same culprit. For additional
12 information and the current version visit the Web page:
14 http://www.fourmilab.ch/smartall/
22 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
24 This program is free software; you can redistribute it and/or
25 modify it under the terms of the GNU General Public License as
26 published by the Free Software Foundation; either version 2 of
27 the License, or (at your option) any later version.
29 This program is distributed in the hope that it will be useful,
30 but WITHOUT ANY WARRANTY; without even the implied warranty of
31 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
32 General Public License for more details.
34 You should have received a copy of the GNU General Public
35 License along with this program; if not, write to the Free
36 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
42 /* Use the real routines here */
53 extern char my_name[]; /* daemon name */
55 typedef unsigned short sm_ushort;
57 #define EOS '\0' /* End of string sentinel */
58 #define sm_min(a, b) ((a) < (b) ? (a) : (b))
60 /* Queue data structures */
62 /* Memory allocation control structures and storage. */
65 struct b_queue abq; /* Links on allocated queue */
66 unsigned ablen; /* Buffer length in bytes */
67 char *abfname; /* File name pointer */
68 sm_ushort ablineno; /* Line number of allocation */
71 static struct b_queue abqueue = { /* Allocated buffer queue */
75 static Boolean bufimode = False; /* Buffers not tracked when True */
77 #define HEAD_SIZE BALIGN(sizeof(struct abufhead))
80 /* SMALLOC -- Allocate buffer, enqueing on the orphaned buffer
83 static void *smalloc(char *fname, int lineno, unsigned int nbytes)
87 /* Note: Unix MALLOC actually permits a zero length to be
88 passed and allocates a valid block with zero user bytes.
89 Such a block can later be expanded with realloc(). We
90 disallow this based on the belief that it's better to make
91 a special case and allocate one byte in the rare case this
92 is desired than to miss all the erroneous occurrences where
93 buffer length calculation code results in a zero. */
97 nbytes += HEAD_SIZE + 1;
98 if ((buf = (char *) malloc(nbytes)) != NULL) {
99 /* Enqueue buffer on allocated list */
100 qinsert(&abqueue, (struct b_queue *) buf);
101 ((struct abufhead *) buf)->ablen = nbytes;
102 ((struct abufhead *) buf)->abfname = bufimode ? NULL : fname;
103 ((struct abufhead *) buf)->ablineno = (sm_ushort) lineno;
104 /* Emplace end-clobber detector at end of buffer */
105 buf[nbytes - 1] = (((long) buf) & 0xFF) ^ 0xC5;
106 buf += HEAD_SIZE; /* Increment to user data start */
108 sm_check(fname, lineno, True);
109 Dmsg4(1150, "smalloc %d at %x from %s:%d\n", nbytes, buf, fname, lineno);
113 /* SM_NEW_OWNER -- Update the File and line number for a buffer
114 This is to accomodate mem_pool. */
116 void sm_new_owner(char *fname, int lineno, char *buf)
118 buf -= HEAD_SIZE; /* Decrement to header */
119 ((struct abufhead *) buf)->abfname = bufimode ? NULL : fname;
120 ((struct abufhead *) buf)->ablineno = (sm_ushort) lineno;
124 /* SM_FREE -- Update free pool availability. FREE is never called
125 except through this interface or by actuallyfree().
126 free(x) is defined to generate a call to this
129 void sm_free(char *file, int line, void *fp)
131 char *cp = (char *) fp;
134 sm_check(__FILE__, __LINE__, True);
136 Emsg2(M_ABORT, 0, "Attempt to free NULL called from %s:%d\n", file, line);
140 qp = (struct b_queue *) cp;
142 Dmsg4(1150, "sm_free %d at %x from %s:%d\n",
143 ((struct abufhead *)cp)->ablen, fp,
144 ((struct abufhead *)cp)->abfname, ((struct abufhead *)cp)->ablineno);
146 /* The following assertions will catch virtually every release
147 of an address which isn't an allocated buffer. */
148 if (qp->qnext->qprev != qp) {
149 Emsg2(M_ABORT, 0, "qp->qnext->qprev != qp called from %s:%d\n", file, line);
151 if (qp->qprev->qnext != qp) {
152 Emsg2(M_ABORT, 0, "qp->qprev->qnext != qp called from %s:%d\n", file, line);
155 /* The following assertion detects storing off the end of the
156 allocated space in the buffer by comparing the end of buffer
157 checksum with the address of the buffer. */
159 if (((unsigned char *) cp)[((struct abufhead *) cp)->ablen - 1] !=
160 ((((long) cp) & 0xFF) ^ 0xC5)) {
161 Emsg2(M_ABORT, 0, "Buffer overrun called from %s:%d\n", file, line);
167 /* Now we wipe the contents of the just-released buffer with
168 "designer garbage" (Duff Kurland's phrase) of alternating
169 bits. This is intended to ruin the day for any miscreant who
170 attempts to access data through a pointer into storage that's
171 been previously released. */
173 memset(cp, 0xAA, (int) ((struct abufhead *) cp)->ablen);
178 /* SM_MALLOC -- Allocate buffer. NULL is returned if no memory
181 void *sm_malloc(char *fname, int lineno, unsigned int nbytes)
185 if ((buf = smalloc(fname, lineno, nbytes)) != NULL) {
187 /* To catch sloppy code that assumes buffers obtained from
188 malloc() are zeroed, we preset the buffer contents to
189 "designer garbage" consisting of alternating bits. */
191 memset(buf, 0x55, (int) nbytes);
196 /* SM_CALLOC -- Allocate an array and clear it to zero. */
198 void *sm_calloc(char *fname, int lineno,
199 unsigned int nelem, unsigned int elsize)
203 if ((buf = smalloc(fname, lineno, nelem * elsize)) != NULL) {
204 memset(buf, 0, (int) (nelem * elsize));
209 /* SM_REALLOC -- Adjust the size of a previously allocated buffer.
210 Note that the trick of "resurrecting" a previously
211 freed buffer with realloc() is NOT supported by this
212 function. Further, because of the need to maintain
213 our control storage, SM_REALLOC must always allocate
214 a new block and copy the data in the old block.
215 This may result in programs which make heavy use of
216 realloc() running much slower than normally. */
218 void *sm_realloc(char *fname, int lineno, void *ptr, unsigned int size)
222 char *cp = (char *) ptr;
224 Dmsg4(400, "sm_realloc %s:%d 0x%x %d\n", fname, lineno, ptr, size);
225 sm_check(fname, lineno, True);
227 e_msg(fname, lineno, M_ABORT, 0, "sm_realloc size: %d\n", size);
230 /* If the old block pointer is NULL, treat realloc() as a
231 malloc(). SVID is silent on this, but many C libraries
235 return sm_malloc(fname, lineno, size);
237 /* If the old and new sizes are the same, be a nice guy and just
238 return the buffer passed in. */
241 osize = ((struct abufhead *) cp)->ablen -
247 /* Sizes differ. Allocate a new buffer of the requested size.
248 If we can't obtain such a buffer, act as defined in SVID:
249 return NULL from realloc() and leave the buffer in PTR
252 if ((buf = smalloc(fname, lineno, size)) != NULL) {
253 memcpy(buf, ptr, (int) sm_min(size, osize));
254 /* If the new buffer is larger than the old, fill the balance
255 of it with "designer garbage". */
257 memset(((char *) buf) + osize, 0x55, (int) (size - osize));
260 /* All done. Free and dechain the original buffer. */
262 sm_free(__FILE__, __LINE__, ptr);
264 Dmsg4(150, "sm_realloc %d at %x from %s:%d\n", size, buf, fname, lineno);
265 sm_check(fname, lineno, True);
269 /* ACTUALLYMALLOC -- Call the system malloc() function to obtain
270 storage which will eventually be released
271 by system or library routines not compiled
274 void *actuallymalloc(unsigned int size)
279 /* ACTUALLYCALLOC -- Call the system calloc() function to obtain
280 storage which will eventually be released
281 by system or library routines not compiled
284 void *actuallycalloc(unsigned int nelem, unsigned int elsize)
286 return calloc(nelem, elsize);
289 /* ACTUALLYREALLOC -- Call the system realloc() function to obtain
290 storage which will eventually be released
291 by system or library routines not compiled
294 void *actuallyrealloc(void *ptr, unsigned int size)
296 Dmsg2(400, "Actuallyrealloc 0x%x %d\n", ptr, size);
297 return realloc(ptr, size);
300 /* ACTUALLYFREE -- Interface to system free() function to release
301 buffers allocated by low-level routines. */
303 void actuallyfree(void *cp)
308 /* SM_DUMP -- Print orphaned buffers (and dump them if BUFDUMP is
310 * N.B. DO NOT USE any Bacula print routines (Dmsg, Jmsg, Emsg, ...)
311 * as they have all been shut down at this point.
313 void sm_dump(Boolean bufdump)
315 struct abufhead *ap = (struct abufhead *) abqueue.qnext;
317 while (ap != (struct abufhead *) &abqueue) {
320 (ap->abq.qnext->qprev != (struct b_queue *) ap) ||
321 (ap->abq.qprev->qnext != (struct b_queue *) ap)) {
323 "\nOrphaned buffers exist. Dump terminated following\n");
325 " discovery of bad links in chain of orphaned buffers.\n");
327 " Buffer address with bad links: %lx\n", (long) ap);
331 if (ap->abfname != NULL) {
332 unsigned memsize = ap->ablen - (HEAD_SIZE + 1);
336 "Orphaned buffer: %6u bytes allocated at line %d of %s %s\n",
337 memsize, ap->ablineno, my_name, ap->abfname
339 fprintf(stderr, "%s", errmsg);
342 char *cp = ((char *) ap) + HEAD_SIZE;
347 strcat(errmsg, "\n");
349 fprintf(stderr, "%s", errmsg);
352 sprintf(errmsg + strlen(errmsg), " %02X",
357 fprintf(stderr, "%s\n", errmsg);
360 ap = (struct abufhead *) ap->abq.qnext;
365 /* SM_CHECK -- Check the buffers and dump if any damage exists. */
366 void sm_check(char *fname, int lineno, Boolean bufdump)
368 if (!sm_check_rtn(fname, lineno, bufdump)) {
369 Emsg2(M_ABORT, 0, "Damaged buffer found. Called from %s:%d\n",
375 /* SM_CHECK_RTN -- Check the buffers and return 1 if OK otherwise 0 */
376 int sm_check_rtn(char *fname, int lineno, Boolean bufdump)
378 struct abufhead *ap = (struct abufhead *) abqueue.qnext;
381 while (ap != (struct abufhead *) &abqueue) {
384 (ap->abq.qnext->qprev != (struct b_queue *) ap)) {
387 if (ap->abq.qprev->qnext != (struct b_queue *) ap) {
390 if (((unsigned char *) ap)[((struct abufhead *) ap)->ablen - 1] !=
391 ((((long) ap) & 0xFF) ^ 0xC5)) {
397 "\nDamaged buffers found at %s:%d\n", fname, lineno);
399 Emsg0(M_FATAL, 0, " discovery of bad prev link.\n");
402 Emsg0(M_FATAL, 0, " discovery of bad next link.\n");
405 Emsg0(M_FATAL, 0, " discovery of data overrun.\n");
408 Emsg1(M_FATAL, 0, " Buffer address: %lx\n", (long) ap);
410 if (ap->abfname != NULL) {
411 unsigned memsize = ap->ablen - (HEAD_SIZE + 1);
415 "Damaged buffer: %6u bytes allocated at line %d of %s %s\n",
416 memsize, ap->ablineno, my_name, ap->abfname
418 Emsg1(M_FATAL, 0, "%s", errmsg);
421 char *cp = ((char *) ap) + HEAD_SIZE;
426 strcat(errmsg, "\n");
428 fprintf(stderr, "%s", errmsg);
432 sprintf(errmsg + strlen(errmsg), " %02X",
435 sprintf(errmsg + strlen(errmsg), " %c ",
441 Emsg1(M_FATAL, 0, "%s\n", errmsg);
445 ap = (struct abufhead *) ap->abq.qnext;
447 return badbuf ? 0 : 1;
451 /* SM_STATIC -- Orphaned buffer detection can be disabled (for such
452 items as buffers allocated during initialisation) by
453 calling sm_static(1). Normal orphaned buffer
454 detection can be re-enabled with sm_static(0). Note
455 that all the other safeguards still apply to buffers
456 allocated when sm_static(1) mode is in effect. */
458 void sm_static(int mode)
460 bufimode = (Boolean) (mode != 0);