xmalloc.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. #include "coretype.h"
  29. #include "errwarn.h"
  30. #ifdef WITH_DMALLOC
  31. #undef yasm_xmalloc
  32. #undef yasm_xcalloc
  33. #undef yasm_xrealloc
  34. #undef yasm_xfree
  35. #endif
  36. static /*@only@*/ /*@out@*/ void *def_xmalloc(size_t size);
  37. static /*@only@*/ void *def_xcalloc(size_t nelem, size_t elsize);
  38. static /*@only@*/ void *def_xrealloc
  39. (/*@only@*/ /*@out@*/ /*@returned@*/ /*@null@*/ void *oldmem, size_t size)
  40. /*@modifies oldmem@*/;
  41. static void def_xfree(/*@only@*/ /*@out@*/ /*@null@*/ void *p)
  42. /*@modifies p@*/;
  43. /* storage for global function pointers */
  44. YASM_LIB_DECL
  45. /*@only@*/ /*@out@*/ void * (*yasm_xmalloc) (size_t size) = def_xmalloc;
  46. YASM_LIB_DECL
  47. /*@only@*/ void * (*yasm_xcalloc) (size_t nelem, size_t elsize) = def_xcalloc;
  48. YASM_LIB_DECL
  49. /*@only@*/ void * (*yasm_xrealloc)
  50. (/*@only@*/ /*@out@*/ /*@returned@*/ /*@null@*/ void *oldmem, size_t size)
  51. /*@modifies oldmem@*/ = def_xrealloc;
  52. YASM_LIB_DECL
  53. void (*yasm_xfree) (/*@only@*/ /*@out@*/ /*@null@*/ void *p)
  54. /*@modifies p@*/ = def_xfree;
  55. static void *
  56. def_xmalloc(size_t size)
  57. {
  58. void *newmem;
  59. if (size == 0)
  60. size = 1;
  61. newmem = malloc(size);
  62. if (!newmem)
  63. yasm__fatal(N_("out of memory"));
  64. return newmem;
  65. }
  66. static void *
  67. def_xcalloc(size_t nelem, size_t elsize)
  68. {
  69. void *newmem;
  70. if (nelem == 0 || elsize == 0)
  71. nelem = elsize = 1;
  72. newmem = calloc(nelem, elsize);
  73. if (!newmem)
  74. yasm__fatal(N_("out of memory"));
  75. return newmem;
  76. }
  77. static void *
  78. def_xrealloc(void *oldmem, size_t size)
  79. {
  80. void *newmem;
  81. if (size == 0)
  82. size = 1;
  83. if (!oldmem)
  84. newmem = malloc(size);
  85. else
  86. newmem = realloc(oldmem, size);
  87. if (!newmem)
  88. yasm__fatal(N_("out of memory"));
  89. return newmem;
  90. }
  91. static void
  92. def_xfree(void *p)
  93. {
  94. if (!p)
  95. return;
  96. free(p);
  97. }