stringfields.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2006, Digium, Inc.
  5. *
  6. * Kevin P. Fleming <kpfleming@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief String fields
  21. *
  22. * \author Kevin P. Fleming <kpfleming@digium.com>
  23. */
  24. #include "asterisk.h"
  25. ASTERISK_REGISTER_FILE()
  26. #include "asterisk/stringfields.h"
  27. #include "asterisk/utils.h"
  28. /* this is a little complex... string fields are stored with their
  29. allocated size in the bytes preceding the string; even the
  30. constant 'empty' string has to be this way, so the code that
  31. checks to see if there is enough room for a new string doesn't
  32. have to have any special case checks
  33. */
  34. static const struct {
  35. ast_string_field_allocation allocation;
  36. char string[1];
  37. } __ast_string_field_empty_buffer;
  38. ast_string_field __ast_string_field_empty = __ast_string_field_empty_buffer.string;
  39. #define ALLOCATOR_OVERHEAD 48
  40. static size_t optimal_alloc_size(size_t size)
  41. {
  42. unsigned int count;
  43. size += ALLOCATOR_OVERHEAD;
  44. for (count = 1; size; size >>= 1, count++);
  45. return (1 << count) - ALLOCATOR_OVERHEAD;
  46. }
  47. static void *calloc_wrapper(unsigned int num_structs, size_t struct_size,
  48. const char *file, int lineno, const char *func)
  49. {
  50. #if defined(__AST_DEBUG_MALLOC)
  51. return __ast_calloc(num_structs, struct_size, file, lineno, func);
  52. #else
  53. return ast_calloc(num_structs, struct_size);
  54. #endif
  55. }
  56. /*! \brief add a new block to the pool.
  57. * We can only allocate from the topmost pool, so the
  58. * fields in *mgr reflect the size of that only.
  59. */
  60. static int add_string_pool(struct ast_string_field_mgr *mgr, struct ast_string_field_pool **pool_head,
  61. size_t size, const char *file, int lineno, const char *func)
  62. {
  63. struct ast_string_field_pool *pool;
  64. size_t alloc_size = optimal_alloc_size(sizeof(*pool) + size);
  65. if (!(pool = calloc_wrapper(1, alloc_size, file, lineno, func))) {
  66. return -1;
  67. }
  68. pool->prev = *pool_head;
  69. pool->size = alloc_size - sizeof(*pool);
  70. *pool_head = pool;
  71. mgr->last_alloc = NULL;
  72. return 0;
  73. }
  74. static void reset_field(const char **p)
  75. {
  76. *p = __ast_string_field_empty;
  77. }
  78. /*!
  79. * \brief Internal cleanup function
  80. * \internal
  81. * \param mgr
  82. * \param pool_head
  83. * \param cleanup_type
  84. * 0: Reset all string fields and free all pools except the last or embedded pool.
  85. * Keep the internal management structures so the structure can be reused.
  86. * -1: Reset all string fields and free all pools except the embedded pool.
  87. * Free the internal management structures.
  88. * \param file
  89. * \param lineno
  90. * \param func
  91. *
  92. * \retval 0 Success
  93. * \retval -1 Failure
  94. */
  95. int __ast_string_field_free_memory(struct ast_string_field_mgr *mgr,
  96. struct ast_string_field_pool **pool_head, enum ast_stringfield_cleanup_type cleanup_type,
  97. const char *file, int lineno, const char *func)
  98. {
  99. struct ast_string_field_pool *cur = NULL;
  100. struct ast_string_field_pool *preserve = NULL;
  101. if (!mgr->header) {
  102. return -1;
  103. }
  104. /* reset all the fields regardless of cleanup type */
  105. AST_VECTOR_CALLBACK_VOID(&mgr->header->string_fields, reset_field);
  106. switch (cleanup_type) {
  107. case AST_STRINGFIELD_DESTROY:
  108. AST_VECTOR_FREE(&mgr->header->string_fields);
  109. if (mgr->header->embedded_pool) { /* ALWAYS preserve the embedded pool if there is one */
  110. preserve = mgr->header->embedded_pool;
  111. preserve->used = preserve->active = 0;
  112. }
  113. ast_free(mgr->header);
  114. mgr->header = NULL;
  115. break;
  116. case AST_STRINGFIELD_RESET:
  117. /* Preserve the embedded pool if there is one, otherwise the last pool */
  118. if (mgr->header->embedded_pool) {
  119. preserve = mgr->header->embedded_pool;
  120. } else {
  121. if (*pool_head == NULL) {
  122. ast_log(LOG_WARNING, "trying to reset empty pool\n");
  123. return -1;
  124. }
  125. preserve = *pool_head;
  126. }
  127. preserve->used = preserve->active = 0;
  128. break;
  129. default:
  130. return -1;
  131. }
  132. cur = *pool_head;
  133. while (cur) {
  134. struct ast_string_field_pool *prev = cur->prev;
  135. if (cur != preserve) {
  136. ast_free(cur);
  137. }
  138. cur = prev;
  139. }
  140. *pool_head = preserve;
  141. if (preserve) {
  142. preserve->prev = NULL;
  143. }
  144. return 0;
  145. }
  146. /*!
  147. * \brief Internal initialization function
  148. * \internal
  149. * \param mgr
  150. * \param pool_head
  151. * \param needed
  152. * \param file
  153. * \param lineno
  154. * \param func
  155. *
  156. * \retval 0 Success
  157. * \retval -1 Failure
  158. */
  159. int __ast_string_field_init(struct ast_string_field_mgr *mgr, struct ast_string_field_pool **pool_head,
  160. int needed, const char *file, int lineno, const char *func)
  161. {
  162. const char **p = (const char **) pool_head + 1;
  163. size_t initial_vector_size = ((size_t) (((char *)mgr) - ((char *)p))) / sizeof(*p);
  164. if (needed <= 0) {
  165. return __ast_string_field_free_memory(mgr, pool_head, needed, file, lineno, func);
  166. }
  167. mgr->last_alloc = NULL;
  168. #if defined(__AST_DEBUG_MALLOC)
  169. mgr->owner_file = file;
  170. mgr->owner_func = func;
  171. mgr->owner_line = lineno;
  172. #endif
  173. if (!(mgr->header = calloc_wrapper(1, sizeof(*mgr->header), file, lineno, func))) {
  174. return -1;
  175. }
  176. if (AST_VECTOR_INIT(&mgr->header->string_fields, initial_vector_size)) {
  177. ast_free(mgr->header);
  178. mgr->header = NULL;
  179. return -1;
  180. }
  181. while ((struct ast_string_field_mgr *) p != mgr) {
  182. AST_VECTOR_APPEND(&mgr->header->string_fields, p);
  183. *p++ = __ast_string_field_empty;
  184. }
  185. *pool_head = NULL;
  186. mgr->header->embedded_pool = NULL;
  187. if (add_string_pool(mgr, pool_head, needed, file, lineno, func)) {
  188. AST_VECTOR_FREE(&mgr->header->string_fields);
  189. ast_free(mgr->header);
  190. mgr->header = NULL;
  191. return -1;
  192. }
  193. return 0;
  194. }
  195. ast_string_field __ast_string_field_alloc_space(struct ast_string_field_mgr *mgr,
  196. struct ast_string_field_pool **pool_head, size_t needed)
  197. {
  198. char *result = NULL;
  199. size_t space = (*pool_head)->size - (*pool_head)->used;
  200. size_t to_alloc;
  201. /* Make room for ast_string_field_allocation and make it a multiple of that. */
  202. to_alloc = ast_make_room_for(needed, ast_string_field_allocation);
  203. ast_assert(to_alloc % ast_alignof(ast_string_field_allocation) == 0);
  204. if (__builtin_expect(to_alloc > space, 0)) {
  205. size_t new_size = (*pool_head)->size;
  206. while (new_size < to_alloc) {
  207. new_size *= 2;
  208. }
  209. #if defined(__AST_DEBUG_MALLOC)
  210. if (add_string_pool(mgr, pool_head, new_size, mgr->owner_file, mgr->owner_line, mgr->owner_func))
  211. return NULL;
  212. #else
  213. if (add_string_pool(mgr, pool_head, new_size, __FILE__, __LINE__, __FUNCTION__))
  214. return NULL;
  215. #endif
  216. }
  217. /* pool->base is always aligned (gcc aligned attribute). We ensure that
  218. * to_alloc is also a multiple of ast_alignof(ast_string_field_allocation)
  219. * causing result to always be aligned as well; which in turn fixes that
  220. * AST_STRING_FIELD_ALLOCATION(result) is aligned. */
  221. result = (*pool_head)->base + (*pool_head)->used;
  222. (*pool_head)->used += to_alloc;
  223. (*pool_head)->active += needed;
  224. result += ast_alignof(ast_string_field_allocation);
  225. AST_STRING_FIELD_ALLOCATION(result) = needed;
  226. mgr->last_alloc = result;
  227. return result;
  228. }
  229. int __ast_string_field_ptr_grow(struct ast_string_field_mgr *mgr,
  230. struct ast_string_field_pool **pool_head, size_t needed, const ast_string_field *ptr)
  231. {
  232. ssize_t grow = needed - AST_STRING_FIELD_ALLOCATION(*ptr);
  233. size_t space = (*pool_head)->size - (*pool_head)->used;
  234. if (*ptr != mgr->last_alloc) {
  235. return 1;
  236. }
  237. if (space < grow) {
  238. return 1;
  239. }
  240. (*pool_head)->used += grow;
  241. (*pool_head)->active += grow;
  242. AST_STRING_FIELD_ALLOCATION(*ptr) += grow;
  243. return 0;
  244. }
  245. void __ast_string_field_release_active(struct ast_string_field_pool *pool_head,
  246. const ast_string_field ptr)
  247. {
  248. struct ast_string_field_pool *pool, *prev;
  249. if (ptr == __ast_string_field_empty) {
  250. return;
  251. }
  252. for (pool = pool_head, prev = NULL; pool; prev = pool, pool = pool->prev) {
  253. if ((ptr >= pool->base) && (ptr <= (pool->base + pool->size))) {
  254. pool->active -= AST_STRING_FIELD_ALLOCATION(ptr);
  255. if (pool->active == 0) {
  256. if (prev) {
  257. prev->prev = pool->prev;
  258. ast_free(pool);
  259. } else {
  260. pool->used = 0;
  261. }
  262. }
  263. break;
  264. }
  265. }
  266. }
  267. void __ast_string_field_ptr_build_va(struct ast_string_field_mgr *mgr,
  268. struct ast_string_field_pool **pool_head, ast_string_field *ptr,
  269. const char *format, va_list ap)
  270. {
  271. size_t needed;
  272. size_t available;
  273. size_t space = (*pool_head)->size - (*pool_head)->used;
  274. int res;
  275. ssize_t grow;
  276. char *target;
  277. va_list ap2;
  278. /* if the field already has space allocated, try to reuse it;
  279. otherwise, try to use the empty space at the end of the current
  280. pool
  281. */
  282. if (*ptr != __ast_string_field_empty) {
  283. target = (char *) *ptr;
  284. available = AST_STRING_FIELD_ALLOCATION(*ptr);
  285. if (*ptr == mgr->last_alloc) {
  286. available += space;
  287. }
  288. } else {
  289. /* pool->used is always a multiple of ast_alignof(ast_string_field_allocation)
  290. * so we don't need to re-align anything here.
  291. */
  292. target = (*pool_head)->base + (*pool_head)->used + ast_alignof(ast_string_field_allocation);
  293. if (space > ast_alignof(ast_string_field_allocation)) {
  294. available = space - ast_alignof(ast_string_field_allocation);
  295. } else {
  296. available = 0;
  297. }
  298. }
  299. va_copy(ap2, ap);
  300. res = vsnprintf(target, available, format, ap2);
  301. va_end(ap2);
  302. if (res < 0) {
  303. /* Are we out of memory? */
  304. return;
  305. }
  306. if (res == 0) {
  307. __ast_string_field_release_active(*pool_head, *ptr);
  308. *ptr = __ast_string_field_empty;
  309. return;
  310. }
  311. needed = (size_t)res + 1; /* NUL byte */
  312. if (needed > available) {
  313. /* the allocation could not be satisfied using the field's current allocation
  314. (if it has one), or the space available in the pool (if it does not). allocate
  315. space for it, adding a new string pool if necessary.
  316. */
  317. if (!(target = (char *) __ast_string_field_alloc_space(mgr, pool_head, needed))) {
  318. return;
  319. }
  320. vsprintf(target, format, ap);
  321. va_end(ap); /* XXX va_end without va_start? */
  322. __ast_string_field_release_active(*pool_head, *ptr);
  323. *ptr = target;
  324. } else if (*ptr != target) {
  325. /* the allocation was satisfied using available space in the pool, but not
  326. using the space already allocated to the field
  327. */
  328. __ast_string_field_release_active(*pool_head, *ptr);
  329. mgr->last_alloc = *ptr = target;
  330. ast_assert(needed < (ast_string_field_allocation)-1);
  331. AST_STRING_FIELD_ALLOCATION(target) = (ast_string_field_allocation)needed;
  332. (*pool_head)->used += ast_make_room_for(needed, ast_string_field_allocation);
  333. (*pool_head)->active += needed;
  334. } else if ((grow = (needed - AST_STRING_FIELD_ALLOCATION(*ptr))) > 0) {
  335. /* the allocation was satisfied by using available space in the pool *and*
  336. the field was the last allocated field from the pool, so it grew
  337. */
  338. AST_STRING_FIELD_ALLOCATION(*ptr) += grow;
  339. (*pool_head)->used += ast_align_for(grow, ast_string_field_allocation);
  340. (*pool_head)->active += grow;
  341. }
  342. }
  343. void __ast_string_field_ptr_build(struct ast_string_field_mgr *mgr,
  344. struct ast_string_field_pool **pool_head, ast_string_field *ptr, const char *format, ...)
  345. {
  346. va_list ap;
  347. va_start(ap, format);
  348. __ast_string_field_ptr_build_va(mgr, pool_head, ptr, format, ap);
  349. va_end(ap);
  350. }
  351. void *__ast_calloc_with_stringfields(unsigned int num_structs, size_t struct_size,
  352. size_t field_mgr_offset, size_t field_mgr_pool_offset, size_t pool_size, const char *file,
  353. int lineno, const char *func)
  354. {
  355. struct ast_string_field_mgr *mgr;
  356. struct ast_string_field_pool *pool;
  357. struct ast_string_field_pool **pool_head;
  358. size_t pool_size_needed = sizeof(*pool) + pool_size;
  359. size_t size_to_alloc = optimal_alloc_size(struct_size + pool_size_needed);
  360. void *allocation;
  361. const char **p;
  362. size_t initial_vector_size;
  363. ast_assert(num_structs == 1);
  364. if (!(allocation = calloc_wrapper(num_structs, size_to_alloc, file, lineno, func))) {
  365. return NULL;
  366. }
  367. mgr = allocation + field_mgr_offset;
  368. /*
  369. * The header is calloced in __ast_string_field_init so it also gets calloced here
  370. * so __ast_string_fields_free_memory can always just free mgr->header.
  371. */
  372. mgr->header = calloc_wrapper(1, sizeof(*mgr->header), file, lineno, func);
  373. if (!mgr->header) {
  374. ast_free(allocation);
  375. return NULL;
  376. }
  377. pool = allocation + struct_size;
  378. pool_head = allocation + field_mgr_pool_offset;
  379. p = (const char **) pool_head + 1;
  380. initial_vector_size = ((size_t) (((char *)mgr) - ((char *)p))) / sizeof(*p);
  381. if (AST_VECTOR_INIT(&mgr->header->string_fields, initial_vector_size)) {
  382. ast_free(mgr->header);
  383. ast_free(allocation);
  384. return NULL;
  385. }
  386. while ((struct ast_string_field_mgr *) p != mgr) {
  387. AST_VECTOR_APPEND(&mgr->header->string_fields, p);
  388. *p++ = __ast_string_field_empty;
  389. }
  390. mgr->header->embedded_pool = pool;
  391. *pool_head = pool;
  392. pool->size = size_to_alloc - struct_size - sizeof(*pool);
  393. #if defined(__AST_DEBUG_MALLOC)
  394. mgr->owner_file = file;
  395. mgr->owner_func = func;
  396. mgr->owner_line = lineno;
  397. #endif
  398. return allocation;
  399. }
  400. int __ast_string_fields_cmp(struct ast_string_field_vector *left,
  401. struct ast_string_field_vector *right)
  402. {
  403. int i;
  404. int res = 0;
  405. ast_assert(AST_VECTOR_SIZE(left) == AST_VECTOR_SIZE(right));
  406. for (i = 0; i < AST_VECTOR_SIZE(left); i++) {
  407. if ((res = strcmp(*AST_VECTOR_GET(left, i), *AST_VECTOR_GET(right, i)))) {
  408. return res;
  409. }
  410. }
  411. return res;
  412. }
  413. int __ast_string_fields_copy(struct ast_string_field_pool *copy_pool,
  414. struct ast_string_field_mgr *copy_mgr, struct ast_string_field_mgr *orig_mgr)
  415. {
  416. int i;
  417. struct ast_string_field_vector *dest = &(copy_mgr->header->string_fields);
  418. struct ast_string_field_vector *src = &(orig_mgr->header->string_fields);
  419. ast_assert(AST_VECTOR_SIZE(dest) == AST_VECTOR_SIZE(src));
  420. for (i = 0; i < AST_VECTOR_SIZE(dest); i++) {
  421. __ast_string_field_release_active(copy_pool, *AST_VECTOR_GET(dest, i));
  422. *AST_VECTOR_GET(dest, i) = __ast_string_field_empty;
  423. }
  424. for (i = 0; i < AST_VECTOR_SIZE(dest); i++) {
  425. if (ast_string_field_ptr_set_by_fields(copy_pool, *copy_mgr, AST_VECTOR_GET(dest, i),
  426. *AST_VECTOR_GET(src, i))) {
  427. return -1;
  428. }
  429. }
  430. return 0;
  431. }