malloc.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* ----------------------------------------------------------------------- *
  2. *
  3. * Copyright 1996-2018 The NASM Authors - All Rights Reserved
  4. * See the file AUTHORS included with the NASM distribution for
  5. * the specific copyright holders.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following
  9. * conditions are met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * * Redistributions in binary form must reproduce the above
  14. * copyright notice, this list of conditions and the following
  15. * disclaimer in the documentation and/or other materials provided
  16. * with the distribution.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  19. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  20. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  25. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  26. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  29. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  30. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. * ----------------------------------------------------------------------- */
  33. /*
  34. * nasmlib.c library routines for the Netwide Assembler
  35. */
  36. #include "compiler.h"
  37. #include <stdlib.h>
  38. #include "nasmlib.h"
  39. #include "error.h"
  40. static no_return nasm_alloc_failed(void)
  41. {
  42. /* If nasm_fatal() gets us back here, then croak hard */
  43. static bool already_here = false;
  44. FILE *errfile;
  45. if (likely(!already_here)) {
  46. already_here = true;
  47. nasm_fatal(0, "out of memory!");
  48. }
  49. errfile = error_file;
  50. if (!errfile)
  51. error_file = stderr;
  52. fprintf(error_file, "nasm: out of memory!\n");
  53. fflush(error_file);
  54. fflush(NULL);
  55. abort();
  56. }
  57. static inline void *validate_ptr(void *p)
  58. {
  59. if (unlikely(!p))
  60. nasm_alloc_failed();
  61. return p;
  62. }
  63. void *nasm_malloc(size_t size)
  64. {
  65. return validate_ptr(malloc(size));
  66. }
  67. void *nasm_calloc(size_t size, size_t nelem)
  68. {
  69. return validate_ptr(calloc(size, nelem));
  70. }
  71. void *nasm_zalloc(size_t size)
  72. {
  73. return validate_ptr(calloc(1, size));
  74. }
  75. void *nasm_realloc(void *q, size_t size)
  76. {
  77. return validate_ptr(q ? realloc(q, size) : malloc(size));
  78. }
  79. void nasm_free(void *q)
  80. {
  81. if (q)
  82. free(q);
  83. }
  84. char *nasm_strdup(const char *s)
  85. {
  86. char *p;
  87. size_t size = strlen(s) + 1;
  88. p = nasm_malloc(size);
  89. return memcpy(p, s, size);
  90. }
  91. char *nasm_strndup(const char *s, size_t len)
  92. {
  93. char *p;
  94. len = strnlen(s, len);
  95. p = nasm_malloc(len+1);
  96. p[len] = '\0';
  97. return memcpy(p, s, len);
  98. }
  99. char *nasm_strcat(const char *one, const char *two)
  100. {
  101. char *rslt;
  102. size_t l1 = strlen(one);
  103. size_t l2 = strlen(two);
  104. rslt = nasm_malloc(l1 + l2 + 1);
  105. memcpy(rslt, one, l1);
  106. memcpy(rslt + l1, two, l2+1);
  107. return rslt;
  108. }
  109. char *nasm_strcatn(const char *str1, ...)
  110. {
  111. va_list ap;
  112. char *rslt; /* Output buffer */
  113. size_t s; /* Total buffer size */
  114. size_t n; /* Number of arguments */
  115. size_t *ltbl; /* Table of lengths */
  116. size_t l, *lp; /* Length for current argument */
  117. const char *p; /* Currently examined argument */
  118. char *q; /* Output pointer */
  119. n = 0; /* No strings encountered yet */
  120. p = str1;
  121. va_start(ap, str1);
  122. while (p) {
  123. n++;
  124. p = va_arg(ap, const char *);
  125. }
  126. va_end(ap);
  127. ltbl = nasm_malloc(n * sizeof(size_t));
  128. s = 1; /* Space for final NULL */
  129. p = str1;
  130. lp = ltbl;
  131. va_start(ap, str1);
  132. while (p) {
  133. *lp++ = l = strlen(p);
  134. s += l;
  135. p = va_arg(ap, const char *);
  136. }
  137. va_end(ap);
  138. q = rslt = nasm_malloc(s);
  139. p = str1;
  140. lp = ltbl;
  141. va_start(ap, str1);
  142. while (p) {
  143. l = *lp++;
  144. memcpy(q, p, l);
  145. q += l;
  146. p = va_arg(ap, const char *);
  147. }
  148. va_end(ap);
  149. *q = '\0';
  150. nasm_free(ltbl);
  151. return rslt;
  152. }