145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org/* 245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * Memory allocation routines with error checking. Idea from GNU libiberty. 345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * 445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * Copyright (C) 2001-2007 Peter Johnson 545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * 645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * Redistribution and use in source and binary forms, with or without 745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * modification, are permitted provided that the following conditions 845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * are met: 945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * 1. Redistributions of source code must retain the above copyright 1045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * notice, this list of conditions and the following disclaimer. 1145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * 2. Redistributions in binary form must reproduce the above copyright 1245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * notice, this list of conditions and the following disclaimer in the 1345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * documentation and/or other materials provided with the distribution. 1445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * 1545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS'' 1645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE 1945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 2045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 2145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 2345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 2445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 2545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org * POSSIBILITY OF SUCH DAMAGE. 2645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org */ 2745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#include "util.h" 2845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 2945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#include "coretype.h" 3045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#include "errwarn.h" 3145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 3245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 3345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#ifdef WITH_DMALLOC 3445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#undef yasm_xmalloc 3545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#undef yasm_xcalloc 3645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#undef yasm_xrealloc 3745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#undef yasm_xfree 3845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#endif 3945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 4045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstatic /*@only@*/ /*@out@*/ void *def_xmalloc(size_t size); 4145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstatic /*@only@*/ void *def_xcalloc(size_t nelem, size_t elsize); 4245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstatic /*@only@*/ void *def_xrealloc 4345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org (/*@only@*/ /*@out@*/ /*@returned@*/ /*@null@*/ void *oldmem, size_t size) 4445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org /*@modifies oldmem@*/; 4545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstatic void def_xfree(/*@only@*/ /*@out@*/ /*@null@*/ void *p) 4645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org /*@modifies p@*/; 4745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 4845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org/* storage for global function pointers */ 4945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgYASM_LIB_DECL 5045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org/*@only@*/ /*@out@*/ void * (*yasm_xmalloc) (size_t size) = def_xmalloc; 5145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgYASM_LIB_DECL 5245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org/*@only@*/ void * (*yasm_xcalloc) (size_t nelem, size_t elsize) = def_xcalloc; 5345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgYASM_LIB_DECL 5445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org/*@only@*/ void * (*yasm_xrealloc) 5545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org (/*@only@*/ /*@out@*/ /*@returned@*/ /*@null@*/ void *oldmem, size_t size) 5645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org /*@modifies oldmem@*/ = def_xrealloc; 5745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgYASM_LIB_DECL 5845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgvoid (*yasm_xfree) (/*@only@*/ /*@out@*/ /*@null@*/ void *p) 5945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org /*@modifies p@*/ = def_xfree; 6045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 6145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 6245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstatic void * 6345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgdef_xmalloc(size_t size) 6445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{ 6545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org void *newmem; 6645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 6745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if (size == 0) 6845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org size = 1; 6945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org newmem = malloc(size); 7045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if (!newmem) 7145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org yasm__fatal(N_("out of memory")); 7245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 7345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org return newmem; 7445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org} 7545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 7645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstatic void * 7745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgdef_xcalloc(size_t nelem, size_t elsize) 7845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{ 7945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org void *newmem; 8045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 8145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if (nelem == 0 || elsize == 0) 8245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org nelem = elsize = 1; 8345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 8445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org newmem = calloc(nelem, elsize); 8545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if (!newmem) 8645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org yasm__fatal(N_("out of memory")); 8745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 8845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org return newmem; 8945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org} 9045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 9145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstatic void * 9245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgdef_xrealloc(void *oldmem, size_t size) 9345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{ 9445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org void *newmem; 9545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 9645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if (size == 0) 9745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org size = 1; 9845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if (!oldmem) 9945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org newmem = malloc(size); 10045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org else 10145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org newmem = realloc(oldmem, size); 10245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if (!newmem) 10345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org yasm__fatal(N_("out of memory")); 10445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 10545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org return newmem; 10645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org} 10745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org 10845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstatic void 10945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgdef_xfree(void *p) 11045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{ 11145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org if (!p) 11245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org return; 11345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org free(p); 11445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org} 115