PageRenderTime 32ms CodeModel.GetById 35ms RepoModel.GetById 1ms app.codeStats 0ms

/src/glsl/ralloc.c

https://github.com/G4G/glsl-optimizer
C | 458 lines | 326 code | 91 blank | 41 comment | 66 complexity | 1fbfa2661f5873563749b819a17afe1c MD5 | raw file
  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. #include "ralloc.h"
  30. #ifdef __GNUC__
  31. #define likely(x) __builtin_expect(!!(x),1)
  32. #define unlikely(x) __builtin_expect(!!(x),0)
  33. #else
  34. #define likely(x) !!(x)
  35. #define unlikely(x) !!(x)
  36. #endif
  37. #ifndef va_copy
  38. #ifdef __va_copy
  39. #define va_copy(dest, src) __va_copy((dest), (src))
  40. #else
  41. #define va_copy(dest, src) (dest) = (src)
  42. #endif
  43. #endif
  44. #define CANARY 0x5A1106
  45. struct ralloc_header
  46. {
  47. /* A canary value used to determine whether a pointer is ralloc'd. */
  48. unsigned canary;
  49. struct ralloc_header *parent;
  50. /* The first child (head of a linked list) */
  51. struct ralloc_header *child;
  52. /* Linked list of siblings */
  53. struct ralloc_header *prev;
  54. struct ralloc_header *next;
  55. void (*destructor)(void *);
  56. };
  57. typedef struct ralloc_header ralloc_header;
  58. static void unlink_block(ralloc_header *info);
  59. static void unsafe_free(ralloc_header *info);
  60. static ralloc_header *
  61. get_header(const void *ptr)
  62. {
  63. ralloc_header *info = (ralloc_header *) (((char *) ptr) -
  64. sizeof(ralloc_header));
  65. assert(info->canary == CANARY);
  66. return info;
  67. }
  68. #define PTR_FROM_HEADER(info) (((char *) info) + sizeof(ralloc_header))
  69. static void
  70. add_child(ralloc_header *parent, ralloc_header *info)
  71. {
  72. if (parent != NULL) {
  73. info->parent = parent;
  74. info->next = parent->child;
  75. parent->child = info;
  76. if (info->next != NULL)
  77. info->next->prev = info;
  78. }
  79. }
  80. void *
  81. ralloc_context(const void *ctx)
  82. {
  83. return ralloc_size(ctx, 0);
  84. }
  85. void *
  86. ralloc_size(const void *ctx, size_t size)
  87. {
  88. void *block = calloc(1, size + sizeof(ralloc_header));
  89. ralloc_header *info = (ralloc_header *) block;
  90. ralloc_header *parent = ctx != NULL ? get_header(ctx) : NULL;
  91. add_child(parent, info);
  92. info->canary = CANARY;
  93. return PTR_FROM_HEADER(info);
  94. }
  95. void *
  96. rzalloc_size(const void *ctx, size_t size)
  97. {
  98. void *ptr = ralloc_size(ctx, size);
  99. if (likely(ptr != NULL))
  100. memset(ptr, 0, size);
  101. return ptr;
  102. }
  103. /* helper function - assumes ptr != NULL */
  104. static void *
  105. resize(void *ptr, size_t size)
  106. {
  107. ralloc_header *child, *old, *info;
  108. old = get_header(ptr);
  109. info = realloc(old, size + sizeof(ralloc_header));
  110. if (info == NULL)
  111. return NULL;
  112. /* Update parent and sibling's links to the reallocated node. */
  113. if (info != old && info->parent != NULL) {
  114. if (info->parent->child == old)
  115. info->parent->child = info;
  116. if (info->prev != NULL)
  117. info->prev->next = info;
  118. if (info->next != NULL)
  119. info->next->prev = info;
  120. }
  121. /* Update child->parent links for all children */
  122. for (child = info->child; child != NULL; child = child->next)
  123. child->parent = info;
  124. return PTR_FROM_HEADER(info);
  125. }
  126. void *
  127. reralloc_size(const void *ctx, void *ptr, size_t size)
  128. {
  129. if (unlikely(ptr == NULL))
  130. return ralloc_size(ctx, size);
  131. assert(ralloc_parent(ptr) == ctx);
  132. return resize(ptr, size);
  133. }
  134. void *
  135. ralloc_array_size(const void *ctx, size_t size, unsigned count)
  136. {
  137. if (count > SIZE_MAX/size)
  138. return NULL;
  139. return ralloc_size(ctx, size * count);
  140. }
  141. void *
  142. rzalloc_array_size(const void *ctx, size_t size, unsigned count)
  143. {
  144. if (count > SIZE_MAX/size)
  145. return NULL;
  146. return rzalloc_size(ctx, size * count);
  147. }
  148. void *
  149. reralloc_array_size(const void *ctx, void *ptr, size_t size, unsigned count)
  150. {
  151. if (count > SIZE_MAX/size)
  152. return NULL;
  153. return reralloc_size(ctx, ptr, size * count);
  154. }
  155. void
  156. ralloc_free(void *ptr)
  157. {
  158. ralloc_header *info;
  159. if (ptr == NULL)
  160. return;
  161. info = get_header(ptr);
  162. unlink_block(info);
  163. unsafe_free(info);
  164. }
  165. static void
  166. unlink_block(ralloc_header *info)
  167. {
  168. /* Unlink from parent & siblings */
  169. if (info->parent != NULL) {
  170. if (info->parent->child == info)
  171. info->parent->child = info->next;
  172. if (info->prev != NULL)
  173. info->prev->next = info->next;
  174. if (info->next != NULL)
  175. info->next->prev = info->prev;
  176. }
  177. info->parent = NULL;
  178. info->prev = NULL;
  179. info->next = NULL;
  180. }
  181. static void
  182. unsafe_free(ralloc_header *info)
  183. {
  184. /* Recursively free any children...don't waste time unlinking them. */
  185. ralloc_header *temp;
  186. while (info->child != NULL) {
  187. temp = info->child;
  188. info->child = temp->next;
  189. unsafe_free(temp);
  190. }
  191. /* Free the block itself. Call the destructor first, if any. */
  192. if (info->destructor != NULL)
  193. info->destructor(PTR_FROM_HEADER(info));
  194. free(info);
  195. }
  196. void
  197. ralloc_steal(const void *new_ctx, void *ptr)
  198. {
  199. ralloc_header *info, *parent;
  200. if (unlikely(ptr == NULL))
  201. return;
  202. info = get_header(ptr);
  203. parent = get_header(new_ctx);
  204. unlink_block(info);
  205. add_child(parent, info);
  206. }
  207. void *
  208. ralloc_parent(const void *ptr)
  209. {
  210. ralloc_header *info;
  211. if (unlikely(ptr == NULL))
  212. return NULL;
  213. info = get_header(ptr);
  214. return PTR_FROM_HEADER(info->parent);
  215. }
  216. static void *autofree_context = NULL;
  217. static void
  218. autofree(void)
  219. {
  220. ralloc_free(autofree_context);
  221. }
  222. void *
  223. ralloc_autofree_context(void)
  224. {
  225. if (unlikely(autofree_context == NULL)) {
  226. autofree_context = ralloc_context(NULL);
  227. atexit(autofree);
  228. }
  229. return autofree_context;
  230. }
  231. void
  232. ralloc_set_destructor(const void *ptr, void(*destructor)(void *))
  233. {
  234. ralloc_header *info = get_header(ptr);
  235. info->destructor = destructor;
  236. }
  237. char *
  238. ralloc_strdup(const void *ctx, const char *str)
  239. {
  240. size_t n;
  241. char *ptr;
  242. if (unlikely(str == NULL))
  243. return NULL;
  244. n = strlen(str);
  245. ptr = ralloc_array(ctx, char, n + 1);
  246. memcpy(ptr, str, n);
  247. ptr[n] = '\0';
  248. return ptr;
  249. }
  250. char *
  251. ralloc_strndup(const void *ctx, const char *str, size_t max)
  252. {
  253. size_t n;
  254. char *ptr;
  255. if (unlikely(str == NULL))
  256. return NULL;
  257. n = strlen(str);
  258. if (n > max)
  259. n = max;
  260. ptr = ralloc_array(ctx, char, n + 1);
  261. memcpy(ptr, str, n);
  262. ptr[n] = '\0';
  263. return ptr;
  264. }
  265. /* helper routine for strcat/strncat - n is the exact amount to copy */
  266. static bool
  267. cat(char **dest, const char *str, size_t n)
  268. {
  269. char *both;
  270. size_t existing_length;
  271. assert(dest != NULL && *dest != NULL);
  272. existing_length = strlen(*dest);
  273. both = resize(*dest, existing_length + n + 1);
  274. if (unlikely(both == NULL))
  275. return false;
  276. memcpy(both + existing_length, str, n);
  277. both[existing_length + n] = '\0';
  278. *dest = both;
  279. return true;
  280. }
  281. bool
  282. ralloc_strcat(char **dest, const char *str)
  283. {
  284. return cat(dest, str, strlen(str));
  285. }
  286. bool
  287. ralloc_strncat(char **dest, const char *str, size_t n)
  288. {
  289. /* Clamp n to the string length */
  290. size_t str_length = strlen(str);
  291. if (str_length < n)
  292. n = str_length;
  293. return cat(dest, str, n);
  294. }
  295. char *
  296. ralloc_asprintf(const void *ctx, const char *fmt, ...)
  297. {
  298. char *ptr;
  299. va_list args;
  300. va_start(args, fmt);
  301. ptr = ralloc_vasprintf(ctx, fmt, args);
  302. va_end(args);
  303. return ptr;
  304. }
  305. /* Return the length of the string that would be generated by a printf-style
  306. * format and argument list, not including the \0 byte.
  307. */
  308. static size_t
  309. printf_length(const char *fmt, va_list untouched_args)
  310. {
  311. int size;
  312. char junk;
  313. /* Make a copy of the va_list so the original caller can still use it */
  314. va_list args;
  315. va_copy(args, untouched_args);
  316. #ifdef _MSC_VER
  317. /* We need to use _vcsprintf to calculate the size as vsnprintf returns -1
  318. * if the number of characters to write is greater than count.
  319. */
  320. size = _vscprintf(fmt, args);
  321. (void)junk;
  322. #else
  323. size = vsnprintf(&junk, 1, fmt, args);
  324. #endif
  325. assert(size >= 0);
  326. va_end(args);
  327. return size;
  328. }
  329. char *
  330. ralloc_vasprintf(const void *ctx, const char *fmt, va_list args)
  331. {
  332. size_t size = printf_length(fmt, args) + 1;
  333. char *ptr = ralloc_size(ctx, size);
  334. if (ptr != NULL)
  335. vsnprintf(ptr, size, fmt, args);
  336. return ptr;
  337. }
  338. bool
  339. ralloc_asprintf_append(char **str, const char *fmt, ...)
  340. {
  341. bool success;
  342. va_list args;
  343. va_start(args, fmt);
  344. success = ralloc_vasprintf_append(str, fmt, args);
  345. va_end(args);
  346. return success;
  347. }
  348. bool
  349. ralloc_vasprintf_append(char **str, const char *fmt, va_list args)
  350. {
  351. size_t existing_length, new_length;
  352. char *ptr;
  353. assert(str != NULL);
  354. if (unlikely(*str == NULL)) {
  355. // Assuming a NULL context is probably bad, but it's expected behavior.
  356. *str = ralloc_vasprintf(NULL, fmt, args);
  357. return true;
  358. }
  359. existing_length = strlen(*str);
  360. new_length = printf_length(fmt, args);
  361. ptr = resize(*str, existing_length + new_length + 1);
  362. if (unlikely(ptr == NULL))
  363. return false;
  364. vsnprintf(ptr + existing_length, new_length + 1, fmt, args);
  365. *str = ptr;
  366. return true;
  367. }