utils.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*-
  2. * Copyright (c) 2010, 2013 The NetBSD Foundation, Inc.
  3. * All rights reserved.
  4. *
  5. * This code is derived from software contributed to The NetBSD Foundation
  6. * by David A. Holland.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
  18. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  19. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  20. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
  21. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. * POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <assert.h>
  32. #include "utils.h"
  33. #define MALLOCDEBUG
  34. const char ws[] =
  35. " \t\f\v"
  36. ;
  37. const char alnum[] =
  38. "0123456789"
  39. "abcdefghijklmnopqrstuvwxyz"
  40. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  41. "_"
  42. ;
  43. ////////////////////////////////////////////////////////////
  44. // malloc
  45. #define ROUNDUP(len, size) ((size) * (((len) + (size) - 1) / (size)))
  46. #ifdef MALLOCDEBUG
  47. struct mallocheader {
  48. struct mallocheader *self;
  49. size_t len;
  50. };
  51. static
  52. size_t
  53. adjustsize(size_t len)
  54. {
  55. const size_t sz = sizeof(struct mallocheader);
  56. return ROUNDUP(len, sz) + 2*sz;
  57. }
  58. static
  59. void *
  60. placeheaders(void *block, size_t len)
  61. {
  62. struct mallocheader *bothdr, *tophdr;
  63. size_t roundedlen;
  64. void *ret;
  65. roundedlen = ROUNDUP(len, sizeof(struct mallocheader));
  66. bothdr = block;
  67. bothdr->len = len;
  68. bothdr->self = block;
  69. ret = bothdr + 1;
  70. tophdr = (void *)(((unsigned char *)ret) + roundedlen);
  71. tophdr->len = len;
  72. tophdr->self = bothdr;
  73. return ret;
  74. }
  75. static
  76. void *
  77. checkheaders(void *block, size_t len)
  78. {
  79. struct mallocheader *bothdr, *tophdr;
  80. size_t roundedlen;
  81. if (block == NULL) {
  82. assert(len == 0);
  83. return block;
  84. }
  85. roundedlen = ROUNDUP(len, sizeof(struct mallocheader));
  86. bothdr = block;
  87. bothdr--;
  88. assert(bothdr->self == bothdr);
  89. assert(bothdr->len == len);
  90. tophdr = (void *)(((unsigned char *)(bothdr + 1)) + roundedlen);
  91. assert(tophdr->self == bothdr);
  92. assert(tophdr->len == len);
  93. return bothdr;
  94. }
  95. #else
  96. #define adjustsize(len) (len)
  97. #define placeheaders(block, len) ((void)(len), (block))
  98. #define checkheaders(ptr, len) ((void)(len), (ptr))
  99. #endif /* MALLOCDEBUG */
  100. void *
  101. domalloc(size_t len)
  102. {
  103. void *ret;
  104. size_t blocklen;
  105. blocklen = adjustsize(len);
  106. ret = malloc(blocklen);
  107. if (ret == NULL) {
  108. complain(NULL, "Out of memory");
  109. die();
  110. }
  111. return placeheaders(ret, len);
  112. }
  113. void *
  114. dorealloc(void *ptr, size_t oldlen, size_t newlen)
  115. {
  116. void *ret;
  117. void *blockptr;
  118. size_t newblocklen;
  119. blockptr = checkheaders(ptr, oldlen);
  120. newblocklen = adjustsize(newlen);
  121. ret = realloc(blockptr, newblocklen);
  122. if (ret == NULL) {
  123. complain(NULL, "Out of memory");
  124. die();
  125. }
  126. return placeheaders(ret, newlen);
  127. }
  128. void
  129. dofree(void *ptr, size_t len)
  130. {
  131. void *blockptr;
  132. blockptr = checkheaders(ptr, len);
  133. free(blockptr);
  134. }
  135. ////////////////////////////////////////////////////////////
  136. // string allocators
  137. char *
  138. dostrdup(const char *s)
  139. {
  140. char *ret;
  141. size_t len;
  142. len = strlen(s);
  143. ret = domalloc(len+1);
  144. strcpy(ret, s);
  145. return ret;
  146. }
  147. char *
  148. dostrdup2(const char *s, const char *t)
  149. {
  150. char *ret;
  151. size_t len;
  152. len = strlen(s) + strlen(t);
  153. ret = domalloc(len+1);
  154. strcpy(ret, s);
  155. strcat(ret, t);
  156. return ret;
  157. }
  158. char *
  159. dostrdup3(const char *s, const char *t, const char *u)
  160. {
  161. char *ret;
  162. size_t len;
  163. len = strlen(s) + strlen(t) + strlen(u);
  164. ret = domalloc(len+1);
  165. strcpy(ret, s);
  166. strcat(ret, t);
  167. strcat(ret, u);
  168. return ret;
  169. }
  170. char *
  171. dostrndup(const char *s, size_t len)
  172. {
  173. char *ret;
  174. ret = domalloc(len+1);
  175. memcpy(ret, s, len);
  176. ret[len] = '\0';
  177. return ret;
  178. }
  179. void
  180. dostrfree(char *s)
  181. {
  182. dofree(s, strlen(s)+1);
  183. }
  184. ////////////////////////////////////////////////////////////
  185. // other stuff
  186. size_t
  187. notrailingws(char *buf, size_t len)
  188. {
  189. while (len > 0 && strchr(ws, buf[len-1])) {
  190. buf[--len] = '\0';
  191. }
  192. return len;
  193. }
  194. bool
  195. is_identifier(const char *str)
  196. {
  197. size_t len;
  198. len = strlen(str);
  199. if (len != strspn(str, alnum)) {
  200. return false;
  201. }
  202. if (str[0] >= '0' && str[0] <= '9') {
  203. return false;
  204. }
  205. return true;
  206. }