1 /* 2 * Memory allocation routines with error checking. Idea from GNU libiberty. 3 * 4 * Copyright (C) 2001-2007 Peter Johnson 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS'' 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 * POSSIBILITY OF SUCH DAMAGE. 26 */ 27 #include "util.h" 28 29 #include "coretype.h" 30 #include "errwarn.h" 31 32 33 #ifdef WITH_DMALLOC 34 #undef yasm_xmalloc 35 #undef yasm_xcalloc 36 #undef yasm_xrealloc 37 #undef yasm_xfree 38 #endif 39 40 static /*@only@*/ /*@out@*/ void *def_xmalloc(size_t size); 41 static /*@only@*/ void *def_xcalloc(size_t nelem, size_t elsize); 42 static /*@only@*/ void *def_xrealloc 43 (/*@only@*/ /*@out@*/ /*@returned@*/ /*@null@*/ void *oldmem, size_t size) 44 /*@modifies oldmem@*/; 45 static void def_xfree(/*@only@*/ /*@out@*/ /*@null@*/ void *p) 46 /*@modifies p@*/; 47 48 /* storage for global function pointers */ 49 YASM_LIB_DECL 50 /*@only@*/ /*@out@*/ void * (*yasm_xmalloc) (size_t size) = def_xmalloc; 51 YASM_LIB_DECL 52 /*@only@*/ void * (*yasm_xcalloc) (size_t nelem, size_t elsize) = def_xcalloc; 53 YASM_LIB_DECL 54 /*@only@*/ void * (*yasm_xrealloc) 55 (/*@only@*/ /*@out@*/ /*@returned@*/ /*@null@*/ void *oldmem, size_t size) 56 /*@modifies oldmem@*/ = def_xrealloc; 57 YASM_LIB_DECL 58 void (*yasm_xfree) (/*@only@*/ /*@out@*/ /*@null@*/ void *p) 59 /*@modifies p@*/ = def_xfree; 60 61 62 static void * 63 def_xmalloc(size_t size) 64 { 65 void *newmem; 66 67 if (size == 0) 68 size = 1; 69 newmem = malloc(size); 70 if (!newmem) 71 yasm__fatal(N_("out of memory")); 72 73 return newmem; 74 } 75 76 static void * 77 def_xcalloc(size_t nelem, size_t elsize) 78 { 79 void *newmem; 80 81 if (nelem == 0 || elsize == 0) 82 nelem = elsize = 1; 83 84 newmem = calloc(nelem, elsize); 85 if (!newmem) 86 yasm__fatal(N_("out of memory")); 87 88 return newmem; 89 } 90 91 static void * 92 def_xrealloc(void *oldmem, size_t size) 93 { 94 void *newmem; 95 96 if (size == 0) 97 size = 1; 98 if (!oldmem) 99 newmem = malloc(size); 100 else 101 newmem = realloc(oldmem, size); 102 if (!newmem) 103 yasm__fatal(N_("out of memory")); 104 105 return newmem; 106 } 107 108 static void 109 def_xfree(void *p) 110 { 111 if (!p) 112 return; 113 free(p); 114 } 115