Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ralloc.c 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /*
  2. * Copyright © 2010 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <assert.h>
  24. #include <stdlib.h>
  25. #include <stdarg.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <stdint.h>
  29. /* Android defines SIZE_MAX in limits.h, instead of the standard stdint.h */
  30. #ifdef ANDROID
  31. #include <limits.h>
  32. #endif
  33. /* Some versions of MinGW are missing _vscprintf's declaration, although they
  34. * still provide the symbol in the import library. */
  35. #ifdef __MINGW32__
  36. _CRTIMP int _vscprintf(const char *format, va_list argptr);
  37. #endif
  38. #include "ralloc.h"
  39. #ifdef __GNUC__
  40. #define likely(x) __builtin_expect(!!(x),1)
  41. #define unlikely(x) __builtin_expect(!!(x),0)
  42. #else
  43. #define likely(x) !!(x)
  44. #define unlikely(x) !!(x)
  45. #endif
  46. #ifndef va_copy
  47. #ifdef __va_copy
  48. #define va_copy(dest, src) __va_copy((dest), (src))
  49. #else
  50. #define va_copy(dest, src) (dest) = (src)
  51. #endif
  52. #endif
  53. #define CANARY 0x5A1106
  54. struct ralloc_header
  55. {
  56. /* A canary value used to determine whether a pointer is ralloc'd. */
  57. unsigned canary;
  58. struct ralloc_header *parent;
  59. /* The first child (head of a linked list) */
  60. struct ralloc_header *child;
  61. /* Linked list of siblings */
  62. struct ralloc_header *prev;
  63. struct ralloc_header *next;
  64. void (*destructor)(void *);
  65. };
  66. typedef struct ralloc_header ralloc_header;
  67. static void unlink_block(ralloc_header *info);
  68. static void unsafe_free(ralloc_header *info);
  69. static ralloc_header *
  70. get_header(const void *ptr)
  71. {
  72. ralloc_header *info = (ralloc_header *) (((char *) ptr) -
  73. sizeof(ralloc_header));
  74. assert(info->canary == CANARY);
  75. return info;
  76. }
  77. #define PTR_FROM_HEADER(info) (((char *) info) + sizeof(ralloc_header))
  78. static void
  79. add_child(ralloc_header *parent, ralloc_header *info)
  80. {
  81. if (parent != NULL) {
  82. info->parent = parent;
  83. info->next = parent->child;
  84. parent->child = info;
  85. if (info->next != NULL)
  86. info->next->prev = info;
  87. }
  88. }
  89. void *
  90. ralloc_context(const void *ctx)
  91. {
  92. return ralloc_size(ctx, 0);
  93. }
  94. void *
  95. ralloc_size(const void *ctx, size_t size)
  96. {
  97. void *block = calloc(1, size + sizeof(ralloc_header));
  98. ralloc_header *info = (ralloc_header *) block;
  99. ralloc_header *parent = ctx != NULL ? get_header(ctx) : NULL;
  100. add_child(parent, info);
  101. info->canary = CANARY;
  102. return PTR_FROM_HEADER(info);
  103. }
  104. void *
  105. rzalloc_size(const void *ctx, size_t size)
  106. {
  107. void *ptr = ralloc_size(ctx, size);
  108. if (likely(ptr != NULL))
  109. memset(ptr, 0, size);
  110. return ptr;
  111. }
  112. /* helper function - assumes ptr != NULL */
  113. static void *
  114. resize(void *ptr, size_t size)
  115. {
  116. ralloc_header *child, *old, *info;
  117. old = get_header(ptr);
  118. info = realloc(old, size + sizeof(ralloc_header));
  119. if (info == NULL)
  120. return NULL;
  121. /* Update parent and sibling's links to the reallocated node. */
  122. if (info != old && info->parent != NULL) {
  123. if (info->parent->child == old)
  124. info->parent->child = info;
  125. if (info->prev != NULL)
  126. info->prev->next = info;
  127. if (info->next != NULL)
  128. info->next->prev = info;
  129. }
  130. /* Update child->parent links for all children */
  131. for (child = info->child; child != NULL; child = child->next)
  132. child->parent = info;
  133. return PTR_FROM_HEADER(info);
  134. }
  135. void *
  136. reralloc_size(const void *ctx, void *ptr, size_t size)
  137. {
  138. if (unlikely(ptr == NULL))
  139. return ralloc_size(ctx, size);
  140. assert(ralloc_parent(ptr) == ctx);
  141. return resize(ptr, size);
  142. }
  143. void *
  144. ralloc_array_size(const void *ctx, size_t size, unsigned count)
  145. {
  146. if (count > SIZE_MAX/size)
  147. return NULL;
  148. return ralloc_size(ctx, size * count);
  149. }
  150. void *
  151. rzalloc_array_size(const void *ctx, size_t size, unsigned count)
  152. {
  153. if (count > SIZE_MAX/size)
  154. return NULL;
  155. return rzalloc_size(ctx, size * count);
  156. }
  157. void *
  158. reralloc_array_size(const void *ctx, void *ptr, size_t size, unsigned count)
  159. {
  160. if (count > SIZE_MAX/size)
  161. return NULL;
  162. return reralloc_size(ctx, ptr, size * count);
  163. }
  164. void
  165. ralloc_free(void *ptr)
  166. {
  167. ralloc_header *info;
  168. if (ptr == NULL)
  169. return;
  170. info = get_header(ptr);
  171. unlink_block(info);
  172. unsafe_free(info);
  173. }
  174. static void
  175. unlink_block(ralloc_header *info)
  176. {
  177. /* Unlink from parent & siblings */
  178. if (info->parent != NULL) {
  179. if (info->parent->child == info)
  180. info->parent->child = info->next;
  181. if (info->prev != NULL)
  182. info->prev->next = info->next;
  183. if (info->next != NULL)
  184. info->next->prev = info->prev;
  185. }
  186. info->parent = NULL;
  187. info->prev = NULL;
  188. info->next = NULL;
  189. }
  190. static void
  191. unsafe_free(ralloc_header *info)
  192. {
  193. /* Recursively free any children...don't waste time unlinking them. */
  194. ralloc_header *temp;
  195. while (info->child != NULL) {
  196. temp = info->child;
  197. info->child = temp->next;
  198. unsafe_free(temp);
  199. }
  200. /* Free the block itself. Call the destructor first, if any. */
  201. if (info->destructor != NULL)
  202. info->destructor(PTR_FROM_HEADER(info));
  203. free(info);
  204. }
  205. void
  206. ralloc_steal(const void *new_ctx, void *ptr)
  207. {
  208. ralloc_header *info, *parent;
  209. if (unlikely(ptr == NULL))
  210. return;
  211. info = get_header(ptr);
  212. parent = get_header(new_ctx);
  213. unlink_block(info);
  214. add_child(parent, info);
  215. }
  216. void *
  217. ralloc_parent(const void *ptr)
  218. {
  219. ralloc_header *info;
  220. if (unlikely(ptr == NULL))
  221. return NULL;
  222. info = get_header(ptr);
  223. return PTR_FROM_HEADER(info->parent);
  224. }
  225. static void *autofree_context = NULL;
  226. static void
  227. autofree(void)
  228. {
  229. ralloc_free(autofree_context);
  230. }
  231. void *
  232. ralloc_autofree_context(void)
  233. {
  234. if (unlikely(autofree_context == NULL)) {
  235. autofree_context = ralloc_context(NULL);
  236. atexit(autofree);
  237. }
  238. return autofree_context;
  239. }
  240. void
  241. ralloc_set_destructor(const void *ptr, void(*destructor)(void *))
  242. {
  243. ralloc_header *info = get_header(ptr);
  244. info->destructor = destructor;
  245. }
  246. char *
  247. ralloc_strdup(const void *ctx, const char *str)
  248. {
  249. size_t n;
  250. char *ptr;
  251. if (unlikely(str == NULL))
  252. return NULL;
  253. n = strlen(str);
  254. ptr = ralloc_array(ctx, char, n + 1);
  255. memcpy(ptr, str, n);
  256. ptr[n] = '\0';
  257. return ptr;
  258. }
  259. char *
  260. ralloc_strndup(const void *ctx, const char *str, size_t max)
  261. {
  262. size_t n;
  263. char *ptr;
  264. if (unlikely(str == NULL))
  265. return NULL;
  266. n = strlen(str);
  267. if (n > max)
  268. n = max;
  269. ptr = ralloc_array(ctx, char, n + 1);
  270. memcpy(ptr, str, n);
  271. ptr[n] = '\0';
  272. return ptr;
  273. }
  274. /* helper routine for strcat/strncat - n is the exact amount to copy */
  275. static bool
  276. cat(char **dest, const char *str, size_t n)
  277. {
  278. char *both;
  279. size_t existing_length;
  280. assert(dest != NULL && *dest != NULL);
  281. existing_length = strlen(*dest);
  282. both = resize(*dest, existing_length + n + 1);
  283. if (unlikely(both == NULL))
  284. return false;
  285. memcpy(both + existing_length, str, n);
  286. both[existing_length + n] = '\0';
  287. *dest = both;
  288. return true;
  289. }
  290. bool
  291. ralloc_strcat(char **dest, const char *str)
  292. {
  293. return cat(dest, str, strlen(str));
  294. }
  295. bool
  296. ralloc_strncat(char **dest, const char *str, size_t n)
  297. {
  298. /* Clamp n to the string length */
  299. size_t str_length = strlen(str);
  300. if (str_length < n)
  301. n = str_length;
  302. return cat(dest, str, n);
  303. }
  304. char *
  305. ralloc_asprintf(const void *ctx, const char *fmt, ...)
  306. {
  307. char *ptr;
  308. va_list args;
  309. va_start(args, fmt);
  310. ptr = ralloc_vasprintf(ctx, fmt, args);
  311. va_end(args);
  312. return ptr;
  313. }
  314. /* Return the length of the string that would be generated by a printf-style
  315. * format and argument list, not including the \0 byte.
  316. */
  317. static size_t
  318. printf_length(const char *fmt, va_list untouched_args)
  319. {
  320. int size;
  321. char junk;
  322. /* Make a copy of the va_list so the original caller can still use it */
  323. va_list args;
  324. va_copy(args, untouched_args);
  325. #ifdef _WIN32
  326. /* We need to use _vcsprintf to calculate the size as vsnprintf returns -1
  327. * if the number of characters to write is greater than count.
  328. */
  329. size = _vscprintf(fmt, args);
  330. (void)junk;
  331. #else
  332. size = vsnprintf(&junk, 1, fmt, args);
  333. #endif
  334. assert(size >= 0);
  335. va_end(args);
  336. return size;
  337. }
  338. char *
  339. ralloc_vasprintf(const void *ctx, const char *fmt, va_list args)
  340. {
  341. size_t size = printf_length(fmt, args) + 1;
  342. char *ptr = ralloc_size(ctx, size);
  343. if (ptr != NULL)
  344. vsnprintf(ptr, size, fmt, args);
  345. return ptr;
  346. }
  347. bool
  348. ralloc_asprintf_append(char **str, const char *fmt, ...)
  349. {
  350. bool success;
  351. va_list args;
  352. va_start(args, fmt);
  353. success = ralloc_vasprintf_append(str, fmt, args);
  354. va_end(args);
  355. return success;
  356. }
  357. bool
  358. ralloc_vasprintf_append(char **str, const char *fmt, va_list args)
  359. {
  360. size_t existing_length;
  361. assert(str != NULL);
  362. existing_length = *str ? strlen(*str) : 0;
  363. return ralloc_vasprintf_rewrite_tail(str, &existing_length, fmt, args);
  364. }
  365. bool
  366. ralloc_asprintf_rewrite_tail(char **str, size_t *start, const char *fmt, ...)
  367. {
  368. bool success;
  369. va_list args;
  370. va_start(args, fmt);
  371. success = ralloc_vasprintf_rewrite_tail(str, start, fmt, args);
  372. va_end(args);
  373. return success;
  374. }
  375. bool
  376. ralloc_vasprintf_rewrite_tail(char **str, size_t *start, const char *fmt,
  377. va_list args)
  378. {
  379. size_t new_length;
  380. char *ptr;
  381. assert(str != NULL);
  382. if (unlikely(*str == NULL)) {
  383. // Assuming a NULL context is probably bad, but it's expected behavior.
  384. *str = ralloc_vasprintf(NULL, fmt, args);
  385. return true;
  386. }
  387. new_length = printf_length(fmt, args);
  388. ptr = resize(*str, *start + new_length + 1);
  389. if (unlikely(ptr == NULL))
  390. return false;
  391. vsnprintf(ptr + *start, new_length + 1, fmt, args);
  392. *str = ptr;
  393. *start += new_length;
  394. return true;
  395. }