su_alloc.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913
  1. /*
  2. * This file is part of the Sofia-SIP package
  3. *
  4. * Copyright (C) 2005 Nokia Corporation.
  5. *
  6. * Contact: Pekka Pessi <pekka.pessi@nokia.com>
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public License
  10. * as published by the Free Software Foundation; either version 2.1 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  21. * 02110-1301 USA
  22. *
  23. */
  24. #include "config.h"
  25. /**@defgroup su_alloc Memory Management Tutorial
  26. *
  27. * This page gives a short overview of home-based memory management used
  28. * with Sofia. Such home-based memory management is useful when a lot of
  29. * memory blocks are allocated for given task. The allocations are done via
  30. * the @e memory @e home, which keeps a reference to each block. When the
  31. * memory home is then freed, it will free all blocks to which it has
  32. * reference.
  33. *
  34. * Typically, there is a @e home @e object which contains a su_home_t
  35. * structure in the beginning of the object (sort of inheritance from
  36. * su_home_t):
  37. * @code
  38. * struct context {
  39. * su_home_t ctx_home[1];
  40. * other_t *ctx_stuff;
  41. * ...
  42. * }
  43. * @endcode
  44. *
  45. * A new home memory pool can be created with su_home_new():
  46. * @code
  47. * struct context *ctx = su_home_new(sizeof (struct context));
  48. * @endcode
  49. *
  50. * It is also possible to create a secondary memory pool that can be
  51. * released separately:
  52. *
  53. * @code
  54. * struct context *ctx = su_home_clone(tophome, sizeof (struct context));
  55. * @endcode
  56. *
  57. * Note that the tophome has a reference to @a ctx structure; whenever
  58. * tophome is freed, the @a ctx is also freed.
  59. *
  60. * You can also create an independent home object by passing NULL as @a
  61. * tophome argument. This is identical to the call to su_home_new().
  62. *
  63. * The memory allocations using @a ctx proceed then as follows:
  64. * @code
  65. * zeroblock = su_zalloc(ctx->ctx_home, sizeof (*zeroblock));
  66. * @endcode
  67. *
  68. * The home memory pool - the home object and all the memory blocks
  69. * allocated using it - are freed when su_home_unref() is called:
  70. *
  71. * @code
  72. * su_home_unref(ctx->ctx_home).
  73. * @endcode
  74. *
  75. * @note For historical reasons, su_home_unref() is also known as
  76. * su_home_zap().
  77. *
  78. * As you might have guessed, it is also possible to use reference counting
  79. * with home objects. The function su_home_ref() increases the reference
  80. * count, su_home_unref() decreases it. A newly allocated or initialized
  81. * home object has reference count of 1.
  82. *
  83. * @note Please note that while it is possible to create new references to
  84. * secondary home objects which have a parent home, the secondary home
  85. * objects will always be destroyed when the parent home is destroyed even
  86. * if there are other references left to them.
  87. *
  88. * The memory blocks in a cloned home object are freed when the object with
  89. * home itself is freed:
  90. * @code
  91. * su_free(tophome, ctx);
  92. * @endcode
  93. *
  94. * @note
  95. *
  96. * The su_home_destroy() function is deprecated as it does not free the home
  97. * object itself. Like su_home_deinit(), it should be called only on home
  98. * objects with reference count of 1.
  99. *
  100. * The function su_home_init() initializes a home object structure. When the
  101. * initialized home object is destroyed or deinitialized or its reference
  102. * count reaches zero, the memory allocate thorugh it reclaimed but the home
  103. * object structure itself is not freed.
  104. *
  105. * @section su_home_destructor_usage Destructors
  106. *
  107. * It is possible to give a destructor function to a home object. The
  108. * destructor releases other resources associated with the home object
  109. * besides memory. The destructor function will be called when the reference
  110. * count of home reaches zero (upon calling su_home_unref()) or the home
  111. * object is otherwise deinitialized (calling su_home_deinit() on
  112. * objects allocated from stack).
  113. *
  114. * @section su_home_move_example Combining Allocations
  115. *
  116. * In some cases, an operation that makes multiple memory allocations may
  117. * fail, making those allocations redundant. If the allocations are made
  118. * through a temporary home, they can be conveniently freed by calling
  119. * su_home_deinit(), for instance. If, however, the operation is successful,
  120. * and one wants to keep the allocations, the allocations can be combined
  121. * into an existing home with su_home_move(). For example,
  122. * @code
  123. * int example(su_home_t *home, ...)
  124. * {
  125. * su_home_t temphome[1] = { SU_HOME_INIT(temphome) };
  126. *
  127. * ... do lot of allocations with temphome ...
  128. *
  129. * if (success)
  130. * su_home_move(home, temphome);
  131. * su_home_deinit(temphome);
  132. *
  133. * return success;
  134. * }
  135. * @endcode
  136. *
  137. * Note that the @a temphome is deinitialized in every case, but when
  138. * operation is successful, the allocations are moved from @a temphome to @a
  139. * home.
  140. *
  141. * @section su_alloc_threadsafe Threadsafe Operation
  142. *
  143. * If multiple threads need to access same home object, it must be marked as
  144. * @e threadsafe by calling su_home_threadsafe() with the home pointer as
  145. * argument. The threadsafeness is not inherited by clones.
  146. *
  147. * The threadsafe home objects can be locked and unlocked with
  148. * su_home_mutex_lock() and su_home_mutex_unlock(). These operations are
  149. * no-op on home object that is not threadsafe.
  150. *
  151. * @section su_alloc_preloading Preloading a Memory Home
  152. *
  153. * In some situations there is quite heavy overhead if the global heap
  154. * allocator is used. The overhead caused by the large number of small
  155. * allocations can be reduced by using su_home_preload(): it allocates or
  156. * preloads some a memory to home to be used as a kind of private heap. The
  157. * preloaded memory area is then used to satisfy small enough allocations.
  158. * For instance, the SIP parser typically preloads some 2K of memory when it
  159. * starts to parse the message.
  160. *
  161. * @section su_alloc_stack Using Stack
  162. *
  163. * In some situation, it is sensible to use memory allocated from stack for
  164. * some operations. The su_home_auto() function can be used for that
  165. * purpose. The memory area from stack is used to satisfy the allocations as
  166. * far as possible; if it is not enough, allocation is made from heap.
  167. *
  168. * The word @e auto refers to the automatic scope; however, the home object
  169. * that was initialized with su_home_auto() must be explicitly deinitialized
  170. * with su_home_deinit() or su_home_unref() when the program exits the scope
  171. * where the stack frame used in su_home_auto() was allocated.
  172. */
  173. /**@ingroup su_alloc
  174. * @CFILE su_alloc.c Home-based memory management.
  175. *
  176. * @author Pekka Pessi <Pekka.Pessi@nokia.com>.
  177. *
  178. * @date Created: Thu Aug 19 01:12:25 1999 ppessi
  179. */
  180. #include <sofia-sip/su_config.h>
  181. #include "sofia-sip/su_alloc.h"
  182. #include "sofia-sip/su_alloc_stat.h"
  183. #include "sofia-sip/su_errno.h"
  184. #include <stdio.h>
  185. #include <stdlib.h>
  186. #include <stddef.h>
  187. #include <memory.h>
  188. #include <limits.h>
  189. #include <assert.h>
  190. int (*_su_home_locker)(void *mutex);
  191. int (*_su_home_unlocker)(void *mutex);
  192. int (*_su_home_mutex_locker)(void *mutex);
  193. int (*_su_home_mutex_trylocker)(void *mutex);
  194. int (*_su_home_mutex_unlocker)(void *mutex);
  195. void (*_su_home_destroy_mutexes)(void *mutex);
  196. #if HAVE_FREE_NULL
  197. #define safefree(x) free((x))
  198. #else
  199. su_inline void safefree(void *b) { b ? free(b) : (void)0; }
  200. #endif
  201. static inline su_block_t* MEMLOCK(const su_home_t *h) {
  202. if (h) {
  203. if (h->suh_lock) _su_home_locker(h->suh_lock);
  204. return h->suh_blocks;
  205. }
  206. return NULL;
  207. }
  208. static inline void* UNLOCK(const su_home_t *h) {
  209. if (h && h->suh_lock) _su_home_unlocker(h->suh_lock);
  210. return NULL;
  211. }
  212. #ifdef NDEBUG
  213. #define MEMCHECK 0
  214. #define MEMCHECK_EXTRA 0
  215. #elif !defined(MEMCHECK)
  216. /* Default settings for valgrinding */
  217. #define MEMCHECK 1
  218. #define MEMCHECK_EXTRA 0
  219. #elif !defined(MEMCHECK_EXTRA)
  220. #define MEMCHECK_EXTRA sizeof (size_t)
  221. #endif
  222. enum {
  223. SUB_N = 31, /**< Initial size */
  224. SUB_N_AUTO = 7, /**< Initial size for autohome */
  225. SUB_P = 29 /**< Secondary probe.
  226. * Secondary probe must be relative prime
  227. * with all sub_n values */
  228. };
  229. #define ALIGNMENT (8)
  230. #define __ALIGN(n) (size_t)(((n) + (ALIGNMENT - 1)) & (size_t)~(ALIGNMENT - 1))
  231. #define SIZEBITS (sizeof (unsigned) * 8 - 1)
  232. typedef struct {
  233. unsigned sua_size:SIZEBITS; /**< Size of the block */
  234. unsigned sua_home:1; /**< Is this another home? */
  235. unsigned :0;
  236. void *sua_data; /**< Data pointer */
  237. } su_alloc_t;
  238. struct su_block_s {
  239. su_home_t *sub_parent; /**< Parent home */
  240. char *sub_preload; /**< Preload area */
  241. su_home_stat_t *sub_stats; /**< Statistics.. */
  242. void (*sub_destructor)(void *); /**< Destructor function */
  243. size_t sub_ref; /**< Reference count */
  244. #define REF_MAX SIZE_MAX
  245. size_t sub_used; /**< Number of blocks allocated */
  246. size_t sub_n; /**< Size of hash table */
  247. unsigned sub_prsize:16; /**< Preload size */
  248. unsigned sub_prused:16; /**< Used from preload */
  249. unsigned sub_hauto:1; /**< "Home" is not from malloc */
  250. unsigned sub_auto:1; /**< struct su_block_s is not from malloc */
  251. unsigned sub_preauto:1; /**< Preload is not from malloc */
  252. unsigned sub_auto_all:1; /**< Everything is from stack! */
  253. unsigned :0;
  254. su_alloc_t sub_nodes[SUB_N]; /**< Pointers to data/lower blocks */
  255. };
  256. static void su_home_check_blocks(su_block_t const *b);
  257. static void su_home_stats_alloc(su_block_t *, void *preload,
  258. size_t size, int zero);
  259. static void su_home_stats_free(su_block_t *sub, void *preload,
  260. unsigned size);
  261. static void _su_home_deinit(su_home_t *home);
  262. #define SU_ALLOC_STATS 1
  263. #if SU_ALLOC_STATS
  264. size_t count_su_block_find, count_su_block_find_loop;
  265. size_t size_su_block_find, used_su_block_find;
  266. size_t max_size_su_block_find, max_used_su_block_find;
  267. size_t su_block_find_collision, su_block_find_collision_used,
  268. su_block_find_collision_size;
  269. #endif
  270. su_inline su_alloc_t *su_block_find(su_block_t const *b, void const *p)
  271. {
  272. size_t h, h0, probe;
  273. #if SU_ALLOC_STATS
  274. size_t collision = 0;
  275. count_su_block_find++;
  276. size_su_block_find += b->sub_n;
  277. used_su_block_find += b->sub_used;
  278. if (b->sub_n > max_size_su_block_find)
  279. max_size_su_block_find = b->sub_n;
  280. if (b->sub_used > max_used_su_block_find)
  281. max_used_su_block_find = b->sub_used;
  282. #endif
  283. assert(p != NULL);
  284. h = h0 = (size_t)((uintptr_t)p % b->sub_n);
  285. probe = (b->sub_n > SUB_P) ? SUB_P : 1;
  286. do {
  287. if (b->sub_nodes[h].sua_data == p) {
  288. su_alloc_t const *retval = &b->sub_nodes[h];
  289. return (su_alloc_t *)retval; /* discard const */
  290. }
  291. h += probe;
  292. if (h >= b->sub_n)
  293. h -= b->sub_n;
  294. #if SU_ALLOC_STATS
  295. if (++collision > su_block_find_collision)
  296. su_block_find_collision = collision,
  297. su_block_find_collision_used = b->sub_used,
  298. su_block_find_collision_size = b->sub_n;
  299. count_su_block_find_loop++;
  300. #endif
  301. } while (h != h0);
  302. return NULL;
  303. }
  304. su_inline su_alloc_t *su_block_add(su_block_t *b, void *p)
  305. {
  306. size_t h, probe;
  307. assert(p != NULL);
  308. h = (size_t)((uintptr_t)p % b->sub_n);
  309. probe = (b->sub_n > SUB_P) ? SUB_P : 1;
  310. while (b->sub_nodes[h].sua_data) {
  311. h += probe;
  312. if (h >= b->sub_n)
  313. h -= b->sub_n;
  314. }
  315. b->sub_used++;
  316. b->sub_nodes[h].sua_data = p;
  317. return &b->sub_nodes[h];
  318. }
  319. su_inline int su_is_preloaded(su_block_t const *sub, char *data)
  320. {
  321. return
  322. sub->sub_preload &&
  323. sub->sub_preload <= data &&
  324. sub->sub_preload + sub->sub_prsize > data;
  325. }
  326. su_inline int su_alloc_check(su_block_t const *sub, su_alloc_t const *sua)
  327. {
  328. #if MEMCHECK_EXTRA
  329. size_t size, term;
  330. assert(sua);
  331. if (sua) {
  332. size = (size_t)sua->sua_size;
  333. memcpy(&term, (char *)sua->sua_data + size, sizeof (term));
  334. assert(size - term == 0);
  335. return size - term == 0;
  336. }
  337. else
  338. return 0;
  339. #endif
  340. return sua != NULL;
  341. }
  342. /** Allocate the block hash table.
  343. *
  344. * @internal
  345. *
  346. * Allocate a block hash table of @a n elements.
  347. *
  348. * @param home pointer to home object
  349. * @param n number of buckets in hash table
  350. *
  351. * @return
  352. * This function returns a pointer to the allocated hash table or
  353. * NULL if an error occurred.
  354. */
  355. su_inline su_block_t *su_hash_alloc(size_t n)
  356. {
  357. su_block_t *b = calloc(1, offsetof(su_block_t, sub_nodes[n]));
  358. if (b) {
  359. /* Implicit su_home_init(); */
  360. b->sub_ref = 1;
  361. b->sub_hauto = 1;
  362. b->sub_n = n;
  363. }
  364. return b;
  365. }
  366. enum sub_zero { do_malloc, do_calloc, do_clone };
  367. /** Allocate a memory block.
  368. *
  369. * @internal
  370. *
  371. * Precondition: locked home
  372. *
  373. * @param home home to allocate
  374. * @param sub block structure used to allocate
  375. * @param size
  376. * @param zero if true, zero allocated block;
  377. * if > 1, allocate a subhome
  378. *
  379. */
  380. static
  381. void *sub_alloc(su_home_t *home,
  382. su_block_t *sub,
  383. size_t size,
  384. enum sub_zero zero)
  385. {
  386. void *data, *preload = NULL;
  387. assert (size < (((size_t)1) << SIZEBITS));
  388. if (size >= ((size_t)1) << SIZEBITS)
  389. return (void)(errno = ENOMEM), NULL;
  390. if (!size) return NULL;
  391. if (sub == NULL || 3 * sub->sub_used > 2 * sub->sub_n) {
  392. /* Resize the hash table */
  393. size_t i, n, n2;
  394. su_block_t *b2;
  395. if (sub)
  396. n = home->suh_blocks->sub_n, n2 = 4 * n + 3; //, used = sub->sub_used;
  397. else
  398. n = 0, n2 = SUB_N; //, used = 0;
  399. #if 0
  400. printf("su_alloc(home = %p): realloc block hash of size %d\n", home, n2);
  401. #endif
  402. if (!(b2 = su_hash_alloc(n2)))
  403. return NULL;
  404. for (i = 0; i < n; i++) {
  405. if (sub->sub_nodes[i].sua_data)
  406. su_block_add(b2, sub->sub_nodes[i].sua_data)[0] = sub->sub_nodes[i];
  407. }
  408. if (sub) {
  409. b2->sub_parent = sub->sub_parent;
  410. b2->sub_ref = sub->sub_ref;
  411. b2->sub_preload = sub->sub_preload;
  412. b2->sub_prsize = sub->sub_prsize;
  413. b2->sub_prused = sub->sub_prused;
  414. b2->sub_hauto = sub->sub_hauto;
  415. b2->sub_preauto = sub->sub_preauto;
  416. b2->sub_destructor = sub->sub_destructor;
  417. /* auto_all is not copied! */
  418. b2->sub_stats = sub->sub_stats;
  419. }
  420. home->suh_blocks = b2;
  421. if (sub && !sub->sub_auto)
  422. free(sub);
  423. sub = b2;
  424. }
  425. if (sub && zero < do_clone &&
  426. sub->sub_preload && size <= sub->sub_prsize) {
  427. /* Use preloaded memory */
  428. size_t prused = sub->sub_prused + size + MEMCHECK_EXTRA;
  429. prused = __ALIGN(prused);
  430. if (prused <= sub->sub_prsize) {
  431. preload = (char *)sub->sub_preload + sub->sub_prused;
  432. sub->sub_prused = (unsigned)prused;
  433. }
  434. }
  435. if (preload && zero)
  436. data = memset(preload, 0, size);
  437. else if (preload)
  438. data = preload;
  439. else if (zero)
  440. data = calloc(1, size + MEMCHECK_EXTRA);
  441. else
  442. data = malloc(size + MEMCHECK_EXTRA);
  443. if (data) {
  444. su_alloc_t *sua;
  445. #if MEMCHECK_EXTRA
  446. size_t term = 0 - size;
  447. memcpy((char *)data + size, &term, sizeof (term));
  448. #endif
  449. if (!preload)
  450. sub->sub_auto_all = 0;
  451. if (zero >= do_clone) {
  452. /* Prepare cloned home */
  453. su_home_t *subhome = data;
  454. assert(preload == 0);
  455. subhome->suh_blocks = su_hash_alloc(SUB_N);
  456. if (!subhome->suh_blocks)
  457. return (void)safefree(data), NULL;
  458. subhome->suh_size = (unsigned)size;
  459. subhome->suh_blocks->sub_parent = home;
  460. subhome->suh_blocks->sub_hauto = 0;
  461. }
  462. /* OK, add the block to the hash table. */
  463. sua = su_block_add(sub, data); assert(sua);
  464. sua->sua_size = (unsigned)size;
  465. sua->sua_home = zero > 1;
  466. if (sub->sub_stats)
  467. su_home_stats_alloc(sub, preload, size, zero);
  468. }
  469. return data;
  470. }
  471. /**Create a new su_home_t object.
  472. *
  473. * Create a home object used to collect multiple memory allocations under
  474. * one handle. The memory allocations made using this home object is freed
  475. * either when this home is destroyed.
  476. *
  477. * The maximum @a size of a home object is INT_MAX (2 gigabytes).
  478. *
  479. * @param size size of home object
  480. *
  481. * The memory home object allocated with su_home_new() can be reclaimed with
  482. * su_home_unref().
  483. *
  484. * @return
  485. * This function returns a pointer to an su_home_t object, or NULL upon
  486. * an error.
  487. */
  488. void *su_home_new(isize_t size)
  489. {
  490. su_home_t *home;
  491. assert(size >= sizeof (*home));
  492. if (size < sizeof (*home))
  493. return (void)(errno = EINVAL), NULL;
  494. else if (size > INT_MAX)
  495. return (void)(errno = ENOMEM), NULL;
  496. home = calloc(1, size);
  497. if (home) {
  498. home->suh_size = (int)size;
  499. home->suh_blocks = su_hash_alloc(SUB_N);
  500. if (home->suh_blocks)
  501. home->suh_blocks->sub_hauto = 0;
  502. else
  503. safefree(home), home = NULL;
  504. }
  505. return home;
  506. }
  507. /** Set destructor function.
  508. *
  509. * The destructor function is called after the reference count of a
  510. * #su_home_t object reaches zero or a home object is deinitialized, but
  511. * before any of the memory areas within the home object are freed.
  512. *
  513. * @since New in @VERSION_1_12_4.
  514. * Earlier versions had su_home_desctructor() (spelling).
  515. */
  516. int su_home_destructor(su_home_t *home, void (*destructor)(void *))
  517. {
  518. int retval = -1;
  519. if (home) {
  520. su_block_t *sub = MEMLOCK(home);
  521. if (sub && sub->sub_destructor == NULL) {
  522. sub->sub_destructor = destructor;
  523. retval = 0;
  524. }
  525. UNLOCK(home);
  526. }
  527. else
  528. su_seterrno(EFAULT);
  529. return retval;
  530. }
  531. #undef su_home_desctructor
  532. /** Set destructor function.
  533. *
  534. * @deprecated The su_home_destructor() was added in @VERSION_1_12_4. The
  535. * su_home_desctructor() is now defined as a macro expanding as
  536. * su_home_destructor(). If you want to compile an application as binary
  537. * compatible with earlier versions, you have to define su_home_desctructor
  538. * as itself, e.g.,
  539. * @code
  540. * #define su_home_desctructor su_home_desctructor
  541. * #include <sofia-sip/su_alloc.h>
  542. * @endcode
  543. */
  544. int su_home_desctructor(su_home_t *home, void (*destructor)(void *))
  545. {
  546. return su_home_destructor(home, destructor);
  547. }
  548. #if (defined(HAVE_MEMLEAK_LOG) && (HAVE_MEMLEAK_LOG != 1))
  549. #include "sofia-sip/su_debug.h"
  550. static void *real_su_home_ref(su_home_t const *home)
  551. {
  552. if (home) {
  553. su_block_t *sub = MEMLOCK(home);
  554. if (sub == NULL || sub->sub_ref == 0) {
  555. assert(sub && sub->sub_ref != 0);
  556. UNLOCK(home);
  557. return NULL;
  558. }
  559. if (sub->sub_ref != REF_MAX)
  560. sub->sub_ref++;
  561. UNLOCK(home);
  562. }
  563. else
  564. su_seterrno(EFAULT);
  565. return (void *)home;
  566. }
  567. static int real_su_home_unref(su_home_t *home)
  568. {
  569. su_block_t *sub;
  570. if (home == NULL)
  571. return 0;
  572. sub = MEMLOCK(home);
  573. if (sub == NULL) {
  574. /* Xyzzy */
  575. return 0;
  576. }
  577. else if (sub->sub_ref == REF_MAX) {
  578. UNLOCK(home);
  579. return 0;
  580. }
  581. else if (--sub->sub_ref > 0) {
  582. UNLOCK(home);
  583. return 0;
  584. }
  585. else if (sub->sub_parent) {
  586. su_home_t *parent = sub->sub_parent;
  587. UNLOCK(home);
  588. su_free(parent, home);
  589. return 1;
  590. }
  591. else {
  592. int hauto = sub->sub_hauto;
  593. _su_home_deinit(home);
  594. if (!hauto)
  595. safefree(home);
  596. /* UNLOCK(home); */
  597. return 1;
  598. }
  599. }
  600. su_home_t *
  601. _su_home_ref_by(su_home_t *home,
  602. char const *file, unsigned line,
  603. char const *function)
  604. {
  605. if (home)
  606. SU_DEBUG_0(("%ld %p - su_home_ref() => "MOD_ZU" by %s:%u: %s()\n", pthread_self(),
  607. home, su_home_refcount(home) + 1, file, line, function));
  608. return (su_home_t *)real_su_home_ref(home);
  609. }
  610. int
  611. _su_home_unref_by(su_home_t *home,
  612. char const *file, unsigned line,
  613. char const *function)
  614. {
  615. if (home) {
  616. size_t refcount = su_home_refcount(home) - 1;
  617. int freed = real_su_home_unref(home);
  618. if (freed) refcount = 0;
  619. SU_DEBUG_0(("%ld %p - su_home_unref() => "MOD_ZU" by %s:%u: %s()\n", pthread_self(),
  620. home, refcount, file, line, function));
  621. return freed;
  622. }
  623. return 0;
  624. }
  625. #else
  626. /** Create a new reference to a home object. */
  627. void *su_home_ref(su_home_t const *home)
  628. {
  629. if (home) {
  630. su_block_t *sub = MEMLOCK(home);
  631. if (sub == NULL || sub->sub_ref == 0) {
  632. assert(sub && sub->sub_ref != 0);
  633. UNLOCK(home);
  634. return NULL;
  635. }
  636. if (sub->sub_ref != REF_MAX)
  637. sub->sub_ref++;
  638. UNLOCK(home);
  639. }
  640. else
  641. su_seterrno(EFAULT);
  642. return (void *)home;
  643. }
  644. /**Unreference a su_home_t object.
  645. *
  646. * Decrements the reference count on home object and destroys and frees it
  647. * and the memory allocations using it if the reference count reaches 0.
  648. *
  649. * @param home memory pool object to be unreferenced
  650. *
  651. * @retval 1 if object was freed
  652. * @retval 0 if object is still alive
  653. */
  654. int su_home_unref(su_home_t *home)
  655. {
  656. su_block_t *sub;
  657. if (home == NULL)
  658. return 0;
  659. sub = MEMLOCK(home);
  660. if (sub == NULL) {
  661. /* Xyzzy */
  662. return 0;
  663. }
  664. else if (sub->sub_ref == REF_MAX) {
  665. UNLOCK(home);
  666. return 0;
  667. }
  668. else if (--sub->sub_ref > 0) {
  669. UNLOCK(home);
  670. return 0;
  671. }
  672. else if (sub->sub_parent) {
  673. su_home_t *parent = sub->sub_parent;
  674. UNLOCK(home);
  675. su_free(parent, home);
  676. return 1;
  677. }
  678. else {
  679. int hauto = sub->sub_hauto;
  680. _su_home_deinit(home);
  681. if (!hauto)
  682. safefree(home);
  683. /* UNLOCK(home); */
  684. return 1;
  685. }
  686. }
  687. #endif
  688. /** Return reference count of home. */
  689. size_t su_home_refcount(su_home_t *home)
  690. {
  691. size_t count = 0;
  692. if (home) {
  693. su_block_t *sub = MEMLOCK(home);
  694. if (sub)
  695. count = sub->sub_ref;
  696. UNLOCK(home);
  697. }
  698. return count;
  699. }
  700. /**Clone a su_home_t object.
  701. *
  702. * Clone a secondary home object used to collect multiple memoryf
  703. * allocations under one handle. The memory is freed either when the cloned
  704. * home is destroyed or when the parent home is destroyed.
  705. *
  706. * An independent
  707. * home object is created if NULL is passed as @a parent argument.
  708. *
  709. * @param parent a parent object (may be NULL)
  710. * @param size size of home object
  711. *
  712. * The memory home object allocated with su_home_clone() can be freed with
  713. * su_home_unref().
  714. *
  715. * @return
  716. * This function returns a pointer to an su_home_t object, or NULL upon
  717. * an error.
  718. */
  719. void *su_home_clone(su_home_t *parent, isize_t size)
  720. {
  721. su_home_t *home;
  722. assert(size >= sizeof (*home));
  723. if (size < sizeof (*home))
  724. return (void)(errno = EINVAL), NULL;
  725. else if (size > INT_MAX)
  726. return (void)(errno = ENOMEM), NULL;
  727. if (parent) {
  728. su_block_t *sub = MEMLOCK(parent);
  729. home = sub_alloc(parent, sub, size, (enum sub_zero)2);
  730. UNLOCK(parent);
  731. }
  732. else {
  733. home = su_home_new(size);
  734. }
  735. return home;
  736. }
  737. /** Return true if home is a clone. */
  738. int su_home_has_parent(su_home_t const *home)
  739. {
  740. return su_home_parent(home) != NULL;
  741. }
  742. /** Return home's parent home. */
  743. su_home_t *su_home_parent(su_home_t const *home)
  744. {
  745. su_home_t *parent = NULL;
  746. if (home && home->suh_blocks) {
  747. su_block_t *sub = MEMLOCK(home);
  748. parent = sub->sub_parent;
  749. UNLOCK(home);
  750. }
  751. return parent;
  752. }
  753. /** Allocate a memory block.
  754. *
  755. * Allocates a memory block of a given @a size.
  756. *
  757. * If @a home is NULL, this function behaves exactly like malloc().
  758. *
  759. * @param home pointer to home object
  760. * @param size size of the memory block to be allocated
  761. *
  762. * @return
  763. * This function returns a pointer to the allocated memory block or
  764. * NULL if an error occurred.
  765. */
  766. void *su_alloc(su_home_t *home, isize_t size)
  767. {
  768. void *data;
  769. if (home) {
  770. data = sub_alloc(home, MEMLOCK(home), size, (enum sub_zero)0);
  771. UNLOCK(home);
  772. }
  773. else
  774. data = malloc(size);
  775. return data;
  776. }
  777. /**Free a memory block.
  778. *
  779. * Frees a single memory block. The @a home must be the owner of the memory
  780. * block (usually the memory home used to allocate the memory block, or NULL
  781. * if no home was used).
  782. *
  783. * @param home pointer to home object
  784. * @param data pointer to the memory block to be freed
  785. */
  786. void su_free(su_home_t *home, void *data)
  787. {
  788. if (!data)
  789. return;
  790. if (home) {
  791. su_alloc_t *allocation;
  792. su_block_t *sub = MEMLOCK(home);
  793. assert(sub);
  794. allocation = su_block_find(sub, data);
  795. assert(allocation);
  796. if (su_alloc_check(sub, allocation)) {
  797. void *preloaded = NULL;
  798. /* Is this preloaded data? */
  799. if (su_is_preloaded(sub, data))
  800. preloaded = data;
  801. if (sub->sub_stats)
  802. su_home_stats_free(sub, preloaded, allocation->sua_size);
  803. if (allocation->sua_home) {
  804. su_home_t *subhome = data;
  805. su_block_t *sub = MEMLOCK(subhome);
  806. assert(sub->sub_ref != REF_MAX);
  807. /* assert(sub->sub_ref > 0); */
  808. sub->sub_ref = 0; /* Zap all references */
  809. _su_home_deinit(subhome);
  810. }
  811. #if MEMCHECK != 0
  812. memset(data, 0xaa, (size_t)allocation->sua_size);
  813. #endif
  814. memset(allocation, 0, sizeof (*allocation));
  815. sub->sub_used--;
  816. if (preloaded)
  817. data = NULL;
  818. }
  819. UNLOCK(home);
  820. }
  821. safefree(data);
  822. }
  823. /** Check if pointer has been allocated through home.
  824. *
  825. * @param home pointer to a memory home
  826. * @param data pointer to a memory area possibly allocated though home
  827. *
  828. * @NEW_1_12_9
  829. */
  830. int su_home_check_alloc(su_home_t const *home, void const *data)
  831. {
  832. int retval = 0;
  833. if (home && data) {
  834. su_block_t const *sub = MEMLOCK(home);
  835. su_alloc_t *allocation = su_block_find(sub, data);
  836. retval = allocation != NULL;
  837. UNLOCK(home);
  838. }
  839. return retval;
  840. }
  841. /** Check home consistency.
  842. *
  843. * Ensures that the home structure and all memory blocks allocated through
  844. * it are consistent. It can be used to catch memory allocation and usage
  845. * errors.
  846. *
  847. * @param home Pointer to a memory home.
  848. */
  849. void su_home_check(su_home_t const *home)
  850. {
  851. #if MEMCHECK != 0
  852. su_block_t const *b = MEMLOCK(home);
  853. su_home_check_blocks(b);
  854. UNLOCK(home);
  855. #endif
  856. }
  857. /** Check home blocks. */
  858. static
  859. void su_home_check_blocks(su_block_t const *b)
  860. {
  861. #if MEMCHECK != 0
  862. if (b) {
  863. size_t i, used;
  864. assert(b->sub_used <= b->sub_n);
  865. for (i = 0, used = 0; i < b->sub_n; i++)
  866. if (b->sub_nodes[i].sua_data) {
  867. su_alloc_check(b, &b->sub_nodes[i]), used++;
  868. if (b->sub_nodes[i].sua_home)
  869. su_home_check((su_home_t *)b->sub_nodes[i].sua_data);
  870. }
  871. assert(used == b->sub_used);
  872. }
  873. #endif
  874. }
  875. /**
  876. * Create an su_home_t object.
  877. *
  878. * Creates a home object. A home object is used to collect multiple memory
  879. * allocations, so that they all can be freed by calling su_home_unref().
  880. *
  881. * @return This function returns a pointer to an #su_home_t object, or
  882. * NULL upon an error.
  883. */
  884. su_home_t *su_home_create(void)
  885. {
  886. return su_home_new(sizeof(su_home_t));
  887. }
  888. /** Destroy a home object
  889. *
  890. * Frees all memory blocks associated with a home object. Note that the home
  891. * object structure is not freed.
  892. *
  893. * @param home pointer to a home object
  894. *
  895. * @deprecated
  896. * su_home_destroy() is included for backwards compatibility only. Use
  897. * su_home_unref() instead of su_home_destroy().
  898. */
  899. void su_home_destroy(su_home_t *home)
  900. {
  901. if (MEMLOCK(home)) {
  902. assert(home->suh_blocks);
  903. assert(home->suh_blocks->sub_ref == 1);
  904. if (!home->suh_blocks->sub_hauto)
  905. /* should warn user */;
  906. home->suh_blocks->sub_hauto = 1;
  907. _su_home_deinit(home);
  908. /* UNLOCK(home); */
  909. }
  910. }
  911. /** Initialize an su_home_t struct.
  912. *
  913. * Initializes an su_home_t structure. It can be used when the home
  914. * structure is allocated from stack or when the home structure is part of
  915. * an another object.
  916. *
  917. * @param home pointer to home object
  918. *
  919. * @retval 0 when successful
  920. * @retval -1 upon an error.
  921. *
  922. * @sa SU_HOME_INIT(), su_home_deinit(), su_home_new(), su_home_clone()
  923. *
  924. * @bug
  925. * Prior to @VERSION_1_12_8 the su_home_t structure should have been
  926. * initialized with SU_HOME_INIT() or otherwise zeroed before calling
  927. * su_home_init().
  928. */
  929. int su_home_init(su_home_t *home)
  930. {
  931. su_block_t *sub;
  932. if (home == NULL)
  933. return -1;
  934. home->suh_blocks = sub = su_hash_alloc(SUB_N);
  935. home->suh_lock = NULL;
  936. if (!sub)
  937. return -1;
  938. return 0;
  939. }
  940. /** Internal deinitialization */
  941. static
  942. void _su_home_deinit(su_home_t *home)
  943. {
  944. if (home->suh_blocks) {
  945. size_t i;
  946. su_block_t *b;
  947. void *suh_lock = home->suh_lock;
  948. home->suh_lock = NULL;
  949. if (home->suh_blocks->sub_destructor) {
  950. void (*destructor)(void *) = home->suh_blocks->sub_destructor;
  951. home->suh_blocks->sub_destructor = NULL;
  952. destructor(home);
  953. }
  954. b = home->suh_blocks;
  955. su_home_check_blocks(b);
  956. for (i = 0; i < b->sub_n; i++) {
  957. if (b->sub_nodes[i].sua_data) {
  958. if (b->sub_nodes[i].sua_home) {
  959. su_home_t *subhome = b->sub_nodes[i].sua_data;
  960. su_block_t *subb = MEMLOCK(subhome);
  961. assert(subb); assert(subb->sub_ref >= 1);
  962. #if 0
  963. if (subb->sub_ref > 0)
  964. SU_DEBUG_7(("su_home_unref: subhome %p with destructor %p has still %u refs\n",
  965. subhome, subb->sub_destructor, subb->sub_ref));
  966. #endif
  967. subb->sub_ref = 0; /* zap them all */
  968. _su_home_deinit(subhome);
  969. }
  970. else if (su_is_preloaded(b, b->sub_nodes[i].sua_data))
  971. continue;
  972. safefree(b->sub_nodes[i].sua_data);
  973. }
  974. }
  975. if (b->sub_preload && !b->sub_preauto)
  976. free(b->sub_preload);
  977. if (b->sub_stats)
  978. free(b->sub_stats);
  979. if (!b->sub_auto)
  980. free(b);
  981. home->suh_blocks = NULL;
  982. if (suh_lock) {
  983. /* Unlock, or risk assert() or leak handles on Windows */
  984. _su_home_unlocker(suh_lock);
  985. _su_home_destroy_mutexes(suh_lock);
  986. }
  987. }
  988. }
  989. /** Free memory blocks allocated through home.
  990. *
  991. * Frees the memory blocks associated with the home object allocated. It
  992. * does not free the home object itself. Use su_home_unref() to free the
  993. * home object.
  994. *
  995. * @param home pointer to home object
  996. *
  997. * @sa su_home_init()
  998. */
  999. void su_home_deinit(su_home_t *home)
  1000. {
  1001. if (MEMLOCK(home)) {
  1002. assert(home->suh_blocks);
  1003. assert(home->suh_blocks->sub_ref == 1);
  1004. assert(home->suh_blocks->sub_hauto);
  1005. _su_home_deinit(home);
  1006. /* UNLOCK(home); */
  1007. }
  1008. }
  1009. /**Move allocations from a su_home_t object to another.
  1010. *
  1011. * Moves allocations made through the @a src home object under the @a dst
  1012. * home object. It is handy, for example, if an operation allocates some
  1013. * number of blocks that should be freed upon an error. It uses a temporary
  1014. * home and moves the blocks from temporary to a proper home when
  1015. * successful, but frees the temporary home upon an error.
  1016. *
  1017. * If @a src has destructor, it is called before starting to move.
  1018. *
  1019. * @param dst destination home
  1020. * @param src source home
  1021. *
  1022. * @retval 0 if succesful
  1023. * @retval -1 upon an error
  1024. */
  1025. int su_home_move(su_home_t *dst, su_home_t *src)
  1026. {
  1027. size_t i, n, n2, used;
  1028. su_block_t *s, *d, *d2;
  1029. if (src == NULL || dst == src)
  1030. return 0;
  1031. if (dst) {
  1032. s = MEMLOCK(src); d = MEMLOCK(dst);
  1033. if (s && s->sub_n) {
  1034. if (s->sub_destructor) {
  1035. void (*destructor)(void *) = s->sub_destructor;
  1036. s->sub_destructor = NULL;
  1037. destructor(src);
  1038. }
  1039. if (d)
  1040. used = s->sub_used + d->sub_used;
  1041. else
  1042. used = s->sub_used;
  1043. if (used && (d == NULL || 3 * used > 2 * d->sub_n)) {
  1044. if (d)
  1045. for (n = n2 = d->sub_n; 3 * used > 2 * n2; n2 = 4 * n2 + 3)
  1046. ;
  1047. else
  1048. n = 0, n2 = s->sub_n;
  1049. if (!(d2 = su_hash_alloc(n2))) {
  1050. UNLOCK(dst); UNLOCK(src);
  1051. return -1;
  1052. }
  1053. dst->suh_blocks = d2;
  1054. for (i = 0; i < n; i++)
  1055. if (d->sub_nodes[i].sua_data)
  1056. su_block_add(d2, d->sub_nodes[i].sua_data)[0] = d->sub_nodes[i];
  1057. if (d) {
  1058. d2->sub_parent = d->sub_parent;
  1059. d2->sub_ref = d->sub_ref;
  1060. d2->sub_preload = d->sub_preload;
  1061. d2->sub_prsize = d->sub_prsize;
  1062. d2->sub_prused = d->sub_prused;
  1063. d2->sub_preauto = d->sub_preauto;
  1064. d2->sub_stats = d->sub_stats;
  1065. }
  1066. if (d && !d->sub_auto)
  1067. free(d);
  1068. d = d2;
  1069. }
  1070. if (s->sub_used) {
  1071. n = s->sub_n;
  1072. for (i = 0; i < n; i++)
  1073. if (s->sub_nodes[i].sua_data) {
  1074. su_block_add(d, s->sub_nodes[i].sua_data)[0] = s->sub_nodes[i];
  1075. if (s->sub_nodes[i].sua_home) {
  1076. su_home_t *subhome = s->sub_nodes[i].sua_data;
  1077. su_block_t *subsub = MEMLOCK(subhome);
  1078. subsub->sub_parent = dst;
  1079. UNLOCK(subhome);
  1080. }
  1081. }
  1082. s->sub_used = 0;
  1083. memset(s->sub_nodes, 0, n * sizeof (s->sub_nodes[0]));
  1084. }
  1085. if (s->sub_stats) {
  1086. /* XXX */
  1087. }
  1088. }
  1089. UNLOCK(dst); UNLOCK(src);
  1090. }
  1091. else {
  1092. s = MEMLOCK(src);
  1093. if (s && s->sub_used) {
  1094. n = s->sub_n;
  1095. for (i = 0; i < n; i++) {
  1096. if (s->sub_nodes[i].sua_data && s->sub_nodes[i].sua_home) {
  1097. su_home_t *subhome = s->sub_nodes[i].sua_data;
  1098. su_block_t *subsub = MEMLOCK(subhome);
  1099. subsub->sub_parent = dst;
  1100. UNLOCK(subhome);
  1101. }
  1102. }
  1103. s->sub_used = 0;
  1104. memset(s->sub_nodes, 0, s->sub_n * sizeof (s->sub_nodes[0]));
  1105. s->sub_used = 0;
  1106. }
  1107. UNLOCK(src);
  1108. }
  1109. return 0;
  1110. }
  1111. /** Preload a memory home.
  1112. *
  1113. * The function su_home_preload() preloads a memory home.
  1114. */
  1115. void su_home_preload(su_home_t *home, isize_t n, isize_t isize)
  1116. {
  1117. su_block_t *sub;
  1118. if (home == NULL)
  1119. return;
  1120. if (home->suh_blocks == NULL)
  1121. su_home_init(home);
  1122. sub = MEMLOCK(home);
  1123. if (!sub->sub_preload) {
  1124. size_t size;
  1125. void *preload;
  1126. size = n * __ALIGN(isize);
  1127. if (size > 65535) /* We have 16 bits... */
  1128. size = 65535 & (ALIGNMENT - 1);
  1129. preload = malloc(size);
  1130. home->suh_blocks->sub_preload = preload;
  1131. home->suh_blocks->sub_prsize = (unsigned)size;
  1132. }
  1133. UNLOCK(home);
  1134. }
  1135. /** Preload a memory home from stack.
  1136. *
  1137. * Initializes a memory home using an area allocated from stack. Poor man's
  1138. * alloca().
  1139. */
  1140. su_home_t *su_home_auto(void *area, isize_t size)
  1141. {
  1142. su_home_t *home;
  1143. su_block_t *sub;
  1144. size_t homesize = __ALIGN(sizeof *home);
  1145. size_t subsize = __ALIGN(offsetof(su_block_t, sub_nodes[SUB_N_AUTO]));
  1146. size_t prepsize;
  1147. char *p = area;
  1148. prepsize = homesize + subsize + (__ALIGN((intptr_t)p) - (intptr_t)p);
  1149. if (area == NULL || size < prepsize)
  1150. return NULL;
  1151. if (size > INT_MAX)
  1152. size = INT_MAX;
  1153. home = memset(p, 0, homesize);
  1154. home->suh_size = (int)size;
  1155. sub = memset(p + homesize, 0, subsize);
  1156. home->suh_blocks = sub;
  1157. if (size > prepsize + 65535)
  1158. size = prepsize + 65535;
  1159. sub->sub_n = SUB_N_AUTO;
  1160. sub->sub_ref = 1;
  1161. sub->sub_preload = p + prepsize;
  1162. sub->sub_prsize = (unsigned)(size - prepsize);
  1163. sub->sub_hauto = 1;
  1164. sub->sub_auto = 1;
  1165. sub->sub_preauto = 1;
  1166. sub->sub_auto_all = 1;
  1167. return home;
  1168. }
  1169. /** Reallocate a memory block.
  1170. *
  1171. * Allocates a memory block of @a size bytes.
  1172. * It copies the old block contents to the new block and frees the old
  1173. * block.
  1174. *
  1175. * If @a home is NULL, this function behaves exactly like realloc().
  1176. *
  1177. * @param home pointer to memory pool object
  1178. * @param data pointer to old memory block
  1179. * @param size size of the memory block to be allocated
  1180. *
  1181. * @return
  1182. * A pointer to the allocated memory block or
  1183. * NULL if an error occurred.
  1184. */
  1185. void *su_realloc(su_home_t *home, void *data, isize_t size)
  1186. {
  1187. void *ndata;
  1188. su_alloc_t *sua;
  1189. su_block_t *sub;
  1190. size_t p;
  1191. size_t term = 0 - size;
  1192. if (!home)
  1193. return realloc(data, size);
  1194. if (size == 0) {
  1195. if (data)
  1196. su_free(home, data);
  1197. return NULL;
  1198. }
  1199. sub = MEMLOCK(home);
  1200. if (!data) {
  1201. data = sub_alloc(home, sub, size, (enum sub_zero)0);
  1202. UNLOCK(home);
  1203. return data;
  1204. }
  1205. sua = su_block_find(sub, data);
  1206. if (!su_alloc_check(sub, sua))
  1207. return UNLOCK(home);
  1208. assert(!sua->sua_home);
  1209. if (sua->sua_home)
  1210. return UNLOCK(home);
  1211. if (!su_is_preloaded(sub, data)) {
  1212. ndata = realloc(data, size + MEMCHECK_EXTRA);
  1213. if (ndata) {
  1214. if (sub->sub_stats) {
  1215. su_home_stats_free(sub, 0, sua->sua_size);
  1216. su_home_stats_alloc(sub, 0, size, 1);
  1217. }
  1218. #if MEMCHECK_EXTRA
  1219. memcpy((char *)ndata + size, &term, sizeof (term));
  1220. #else
  1221. (void)term;
  1222. #endif
  1223. memset(sua, 0, sizeof *sua);
  1224. sub->sub_used--;
  1225. su_block_add(sub, ndata)->sua_size = (unsigned)size;
  1226. }
  1227. UNLOCK(home);
  1228. return ndata;
  1229. }
  1230. p = (char *)data - home->suh_blocks->sub_preload;
  1231. p += sua->sua_size + MEMCHECK_EXTRA;
  1232. p = __ALIGN(p);
  1233. if (p == sub->sub_prused) {
  1234. size_t p2 = (char *)data - sub->sub_preload + size + MEMCHECK_EXTRA;
  1235. p2 = __ALIGN(p2);
  1236. if (p2 <= sub->sub_prsize) {
  1237. /* Extend/reduce existing preload */
  1238. if (sub->sub_stats) {
  1239. su_home_stats_free(sub, data, sua->sua_size);
  1240. su_home_stats_alloc(sub, data, size, 0);
  1241. }
  1242. sub->sub_prused = (unsigned)p2;
  1243. sua->sua_size = (unsigned)size;
  1244. #if MEMCHECK_EXTRA
  1245. memcpy((char *)data + size, &term, sizeof (term));
  1246. #endif
  1247. UNLOCK(home);
  1248. return data;
  1249. }
  1250. }
  1251. else if (size < (size_t)sua->sua_size) {
  1252. /* Reduce existing preload */
  1253. if (sub->sub_stats) {
  1254. su_home_stats_free(sub, data, sua->sua_size);
  1255. su_home_stats_alloc(sub, data, size, 0);
  1256. }
  1257. #if MEMCHECK_EXTRA
  1258. memcpy((char *)data + size, &term, sizeof (term));
  1259. #endif
  1260. sua->sua_size = (unsigned)size;
  1261. UNLOCK(home);
  1262. return data;
  1263. }
  1264. ndata = malloc(size + MEMCHECK_EXTRA);
  1265. if (ndata) {
  1266. if (p == sub->sub_prused) {
  1267. /* Free preload */
  1268. sub->sub_prused = (char *)data - home->suh_blocks->sub_preload;
  1269. if (sub->sub_stats)
  1270. su_home_stats_free(sub, data, sua->sua_size);
  1271. }
  1272. memcpy(ndata, data,
  1273. (size_t)sua->sua_size < size
  1274. ? (size_t)sua->sua_size
  1275. : size);
  1276. #if MEMCHECK_EXTRA
  1277. memcpy((char *)ndata + size, &term, sizeof (term));
  1278. #endif
  1279. if (sub->sub_stats)
  1280. su_home_stats_alloc(sub, 0, size, 1);
  1281. memset(sua, 0, sizeof *sua); sub->sub_used--;
  1282. su_block_add(sub, ndata)->sua_size = (unsigned)size;
  1283. }
  1284. UNLOCK(home);
  1285. return ndata;
  1286. }
  1287. /**Check if a memory block has been allocated from the @a home.
  1288. *
  1289. * Check if the given memory block has been allocated from the home.
  1290. *
  1291. * @param home pointer to memory pool object
  1292. * @param memory ponter to memory block
  1293. *
  1294. * @retval 1 if @a memory has been allocated from @a home.
  1295. * @retval 0 otherwise
  1296. *
  1297. * @since New in @VERSION_1_12_4.
  1298. */
  1299. int su_in_home(su_home_t *home, void const *memory)
  1300. {
  1301. su_alloc_t *sua;
  1302. su_block_t *sub;
  1303. int retval = 0;
  1304. if (!home || !memory)
  1305. return 0;
  1306. sub = MEMLOCK(home);
  1307. if (sub) {
  1308. sua = su_block_find(sub, memory);
  1309. retval = su_alloc_check(sub, sua);
  1310. UNLOCK(home);
  1311. }
  1312. return retval;
  1313. }
  1314. /**Allocate and zero a memory block.
  1315. *
  1316. * Allocates a memory block with a given size from
  1317. * given memory home @a home and zeroes the allocated block.
  1318. *
  1319. * @param home pointer to memory pool object
  1320. * @param size size of the memory block
  1321. *
  1322. * @note The memory home pointer @a home may be @c NULL. In that case, the
  1323. * allocated memory block is not associated with any memory home, and it
  1324. * must be freed by calling su_free() or free().
  1325. *
  1326. * @return
  1327. * The function su_zalloc() returns a pointer to the allocated memory block,
  1328. * or NULL upon an error.
  1329. */
  1330. void *su_zalloc(su_home_t *home, isize_t size)
  1331. {
  1332. void *data;
  1333. assert (size >= 0);
  1334. if (home) {
  1335. data = sub_alloc(home, MEMLOCK(home), size, (enum sub_zero)1);
  1336. UNLOCK(home);
  1337. }
  1338. else
  1339. data = calloc(1, size);
  1340. return data;
  1341. }
  1342. /** Allocate a structure
  1343. *
  1344. * Allocates a structure with a given size, zeros
  1345. * it, and initializes the size field to the given size. The size field
  1346. * is an int at the beginning of the structure. Note that it has type of int.
  1347. *
  1348. * @param home pointer to memory pool object
  1349. * @param size size of the structure
  1350. *
  1351. * @par Example
  1352. * The structure is defined and allocated as follows:
  1353. * @code
  1354. * struct test {
  1355. * int tst_size;
  1356. * char *tst_name;
  1357. * void *tst_ptr[3];
  1358. * };
  1359. *
  1360. * struct test *t;
  1361. * ...
  1362. * t = su_salloc(home, sizeof (*t));
  1363. * assert(t && t->t_size == sizeof (*t));
  1364. *
  1365. * @endcode
  1366. * After calling su_salloc() we get a pointer t to a struct test,
  1367. * initialized to zero except the tst_size field, which is initialized to
  1368. * sizeof (*t).
  1369. *
  1370. * @return A pointer to the allocated structure, or NULL upon an error.
  1371. */
  1372. void *su_salloc(su_home_t *home, isize_t size)
  1373. {
  1374. struct { int size; } *retval;
  1375. if (size < sizeof (*retval))
  1376. size = sizeof (*retval);
  1377. if (size > INT_MAX)
  1378. return (void)(errno = ENOMEM), NULL;
  1379. if (home) {
  1380. retval = sub_alloc(home, MEMLOCK(home), size, (enum sub_zero)1);
  1381. UNLOCK(home);
  1382. }
  1383. else
  1384. retval = calloc(1, size);
  1385. if (retval)
  1386. retval->size = (int)size;
  1387. return retval;
  1388. }
  1389. /** Check if a memory home is threadsafe */
  1390. int su_home_is_threadsafe(su_home_t const *home)
  1391. {
  1392. return home && home->suh_lock;
  1393. }
  1394. /** Increase refcount and obtain exclusive lock on home.
  1395. *
  1396. * @note The #su_home_t structure must be created with su_home_new() or
  1397. * su_home_clone(), or initialized with su_home_init() before using this
  1398. * function.
  1399. *
  1400. * In order to enable actual locking, use su_home_threadsafe(), too.
  1401. * Otherwise the su_home_mutex_lock() will just increase the reference
  1402. * count.
  1403. */
  1404. #if (defined(HAVE_MEMLEAK_LOG) && (HAVE_MEMLEAK_LOG != 1))
  1405. int _su_home_mutex_lock(su_home_t *home, const char *file, unsigned int line, const char *function)
  1406. #else
  1407. int su_home_mutex_lock(su_home_t *home)
  1408. #endif
  1409. {
  1410. int error;
  1411. if (home == NULL)
  1412. return su_seterrno(EFAULT);
  1413. #if (defined(HAVE_MEMLEAK_LOG) && (HAVE_MEMLEAK_LOG != 1))
  1414. if (home->suh_blocks == NULL || !_su_home_ref_by(home, file, line, function))
  1415. #else
  1416. if (home->suh_blocks == NULL || !su_home_ref(home))
  1417. #endif
  1418. return su_seterrno(EINVAL); /* Uninitialized home */
  1419. if (!home->suh_lock)
  1420. return 0; /* No-op */
  1421. error = _su_home_mutex_locker(home->suh_lock);
  1422. if (error)
  1423. return su_seterrno(error);
  1424. return 0;
  1425. }
  1426. /** Release exclusive lock on home and decrease refcount (if home is threadsafe).
  1427. *
  1428. * @sa su_home_unlock().
  1429. */
  1430. #if (defined(HAVE_MEMLEAK_LOG) && (HAVE_MEMLEAK_LOG != 1))
  1431. int _su_home_mutex_unlock(su_home_t *home, const char *file, unsigned int line, const char *function)
  1432. #else
  1433. int su_home_mutex_unlock(su_home_t *home)
  1434. #endif
  1435. {
  1436. if (home == NULL)
  1437. return su_seterrno(EFAULT);
  1438. if (home->suh_lock) {
  1439. int error = _su_home_mutex_unlocker(home->suh_lock);
  1440. if (error)
  1441. return su_seterrno(error);
  1442. }
  1443. if (home->suh_blocks == NULL)
  1444. return su_seterrno(EINVAL), -1; /* Uninitialized home */
  1445. #if (defined(HAVE_MEMLEAK_LOG) && (HAVE_MEMLEAK_LOG != 1))
  1446. _su_home_unref_by(home, file, line, function);
  1447. #else
  1448. su_home_unref(home);
  1449. #endif
  1450. return 0;
  1451. }
  1452. /** Obtain exclusive lock on home without increasing refcount.
  1453. *
  1454. * Unless su_home_threadsafe() has been used to intialize locking on home
  1455. * object the function just returns -1.
  1456. *
  1457. * @return 0 if successful, -1 if not threadsafe, error code otherwise.
  1458. *
  1459. * @sa su_home_mutex_lock(), su_home_unlock(), su_home_trylock().
  1460. *
  1461. * @NEW_1_12_8
  1462. */
  1463. int su_home_lock(su_home_t *home)
  1464. {
  1465. if (home == NULL)
  1466. return EFAULT;
  1467. if (home->suh_lock == NULL)
  1468. return -1; /* No-op */
  1469. return _su_home_mutex_locker(home->suh_lock);
  1470. }
  1471. /** Try to obtain exclusive lock on home without increasing refcount.
  1472. *
  1473. * @return 0 if successful, -1 if not threadsafe,
  1474. * EBUSY if already locked, error code otherwise.
  1475. *
  1476. * @sa su_home_lock(), su_home_unlock().
  1477. *
  1478. * @NEW_1_12_8
  1479. */
  1480. int su_home_trylock(su_home_t *home)
  1481. {
  1482. if (home == NULL)
  1483. return EFAULT;
  1484. if (home->suh_lock == NULL)
  1485. return -1; /* No-op */
  1486. return _su_home_mutex_trylocker(home->suh_lock);
  1487. }
  1488. /** Release exclusive lock on home.
  1489. *
  1490. * Release lock without decreasing refcount.
  1491. *
  1492. * @return 0 if successful, -1 if not threadsafe, error code otherwise.
  1493. *
  1494. * @sa su_home_lock(), su_home_trylock(), su_home_mutex_unlock().
  1495. *
  1496. * @NEW_1_12_8
  1497. */
  1498. int su_home_unlock(su_home_t *home)
  1499. {
  1500. if (home == NULL)
  1501. return EFAULT;
  1502. if (home->suh_lock == NULL)
  1503. return -1; /* No-op */
  1504. return _su_home_mutex_unlocker(home->suh_lock);
  1505. }
  1506. /** Initialize statistics structure */
  1507. void su_home_init_stats(su_home_t *home)
  1508. {
  1509. su_block_t *sub;
  1510. size_t size;
  1511. if (home == NULL)
  1512. return;
  1513. sub = home->suh_blocks;
  1514. if (!sub)
  1515. sub = home->suh_blocks = su_hash_alloc(SUB_N);
  1516. if (!sub)
  1517. return;
  1518. if (!sub->sub_stats) {
  1519. size = sizeof (*sub->sub_stats);
  1520. sub->sub_stats = malloc(size);
  1521. if (!sub->sub_stats)
  1522. return;
  1523. }
  1524. else
  1525. size = sub->sub_stats->hs_size;
  1526. memset(sub->sub_stats, 0, size);
  1527. sub->sub_stats->hs_size = (int)size;
  1528. sub->sub_stats->hs_blocksize = sub->sub_n;
  1529. }
  1530. /** Retrieve statistics from memory home.
  1531. */
  1532. void su_home_get_stats(su_home_t *home, int include_clones,
  1533. su_home_stat_t *hs,
  1534. isize_t size)
  1535. {
  1536. su_block_t *sub;
  1537. if (hs == NULL || size < (sizeof hs->hs_size))
  1538. return;
  1539. memset((void *)hs, 0, size);
  1540. sub = MEMLOCK(home);
  1541. if (sub && sub->sub_stats) {
  1542. int sub_size = sub->sub_stats->hs_size;
  1543. if (sub_size > (int)size)
  1544. sub_size = (int)size;
  1545. sub->sub_stats->hs_preload.hsp_size = sub->sub_prsize;
  1546. sub->sub_stats->hs_preload.hsp_used = sub->sub_prused;
  1547. memcpy(hs, sub->sub_stats, sub_size);
  1548. hs->hs_size = sub_size;
  1549. }
  1550. UNLOCK(home);
  1551. }
  1552. static
  1553. void su_home_stats_alloc(su_block_t *sub, void *preload,
  1554. size_t size, int zero)
  1555. {
  1556. su_home_stat_t *hs = sub->sub_stats;
  1557. size_t rsize = __ALIGN(size);
  1558. hs->hs_rehash += (sub->sub_n != hs->hs_blocksize);
  1559. hs->hs_blocksize = sub->sub_n;
  1560. hs->hs_clones += zero > 1;
  1561. if (preload) {
  1562. hs->hs_allocs.hsa_preload++;
  1563. return;
  1564. }
  1565. hs->hs_allocs.hsa_number++;
  1566. hs->hs_allocs.hsa_bytes += size;
  1567. hs->hs_allocs.hsa_rbytes += rsize;
  1568. if (hs->hs_allocs.hsa_rbytes > hs->hs_allocs.hsa_maxrbytes)
  1569. hs->hs_allocs.hsa_maxrbytes = hs->hs_allocs.hsa_rbytes;
  1570. hs->hs_blocks.hsb_number++;
  1571. hs->hs_blocks.hsb_bytes += size;
  1572. hs->hs_blocks.hsb_rbytes += rsize;
  1573. }
  1574. static
  1575. void su_home_stats_free(su_block_t *sub, void *preload,
  1576. unsigned size)
  1577. {
  1578. su_home_stat_t *hs = sub->sub_stats;
  1579. size_t rsize = __ALIGN(size);
  1580. if (preload) {
  1581. hs->hs_frees.hsf_preload++;
  1582. return;
  1583. }
  1584. hs->hs_frees.hsf_number++;
  1585. hs->hs_frees.hsf_bytes += size;
  1586. hs->hs_frees.hsf_rbytes += rsize;
  1587. hs->hs_blocks.hsb_number--;
  1588. hs->hs_blocks.hsb_bytes -= size;
  1589. hs->hs_blocks.hsb_rbytes -= rsize;
  1590. }
  1591. void su_home_stat_add(su_home_stat_t total[1], su_home_stat_t const hs[1])
  1592. {
  1593. total->hs_clones += hs->hs_clones;
  1594. total->hs_rehash += hs->hs_rehash;
  1595. if (total->hs_blocksize < hs->hs_blocksize)
  1596. total->hs_blocksize = hs->hs_blocksize;
  1597. total->hs_allocs.hsa_number += hs->hs_allocs.hsa_number;
  1598. total->hs_allocs.hsa_bytes += hs->hs_allocs.hsa_bytes;
  1599. total->hs_allocs.hsa_rbytes += hs->hs_allocs.hsa_rbytes;
  1600. total->hs_allocs.hsa_maxrbytes += hs->hs_allocs.hsa_maxrbytes;
  1601. total->hs_frees.hsf_number += hs->hs_frees.hsf_number;
  1602. total->hs_frees.hsf_bytes += hs->hs_frees.hsf_bytes;
  1603. total->hs_frees.hsf_rbytes += hs->hs_frees.hsf_rbytes;
  1604. total->hs_blocks.hsb_number += hs->hs_blocks.hsb_number;
  1605. total->hs_blocks.hsb_bytes += hs->hs_blocks.hsb_bytes;
  1606. total->hs_blocks.hsb_rbytes += hs->hs_blocks.hsb_rbytes;
  1607. }