kmp_alloc.cpp 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271
  1. /*
  2. * kmp_alloc.cpp -- private/shared dynamic memory allocation and management
  3. */
  4. //===----------------------------------------------------------------------===//
  5. //
  6. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  7. // See https://llvm.org/LICENSE.txt for license information.
  8. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  9. //
  10. //===----------------------------------------------------------------------===//
  11. #include "kmp.h"
  12. #include "kmp_io.h"
  13. #include "kmp_wrapper_malloc.h"
  14. #ifdef __clang__
  15. #if __has_feature(address_sanitizer)
  16. extern "C" { // sanitizers API
  17. void __lsan_ignore_object(const void* p);
  18. }
  19. #else
  20. #define __lsan_ignore_object(p)
  21. #endif
  22. #else
  23. #define __lsan_ignore_object(p)
  24. #endif
  25. // Disable bget when it is not used
  26. #if KMP_USE_BGET
  27. /* Thread private buffer management code */
  28. typedef int (*bget_compact_t)(size_t, int);
  29. typedef void *(*bget_acquire_t)(size_t);
  30. typedef void (*bget_release_t)(void *);
  31. /* NOTE: bufsize must be a signed datatype */
  32. #if KMP_OS_WINDOWS
  33. #if KMP_ARCH_X86 || KMP_ARCH_ARM
  34. typedef kmp_int32 bufsize;
  35. #else
  36. typedef kmp_int64 bufsize;
  37. #endif
  38. #else
  39. typedef ssize_t bufsize;
  40. #endif // KMP_OS_WINDOWS
  41. /* The three modes of operation are, fifo search, lifo search, and best-fit */
  42. typedef enum bget_mode {
  43. bget_mode_fifo = 0,
  44. bget_mode_lifo = 1,
  45. bget_mode_best = 2
  46. } bget_mode_t;
  47. static void bpool(kmp_info_t *th, void *buffer, bufsize len);
  48. static void *bget(kmp_info_t *th, bufsize size);
  49. static void *bgetz(kmp_info_t *th, bufsize size);
  50. static void *bgetr(kmp_info_t *th, void *buffer, bufsize newsize);
  51. static void brel(kmp_info_t *th, void *buf);
  52. static void bectl(kmp_info_t *th, bget_compact_t compact,
  53. bget_acquire_t acquire, bget_release_t release,
  54. bufsize pool_incr);
  55. /* BGET CONFIGURATION */
  56. /* Buffer allocation size quantum: all buffers allocated are a
  57. multiple of this size. This MUST be a power of two. */
  58. /* On IA-32 architecture with Linux* OS, malloc() does not
  59. ensure 16 byte alignment */
  60. #if KMP_ARCH_X86 || !KMP_HAVE_QUAD
  61. #define SizeQuant 8
  62. #define AlignType double
  63. #else
  64. #define SizeQuant 16
  65. #define AlignType _Quad
  66. #endif
  67. // Define this symbol to enable the bstats() function which calculates the
  68. // total free space in the buffer pool, the largest available buffer, and the
  69. // total space currently allocated.
  70. #define BufStats 1
  71. #ifdef KMP_DEBUG
  72. // Define this symbol to enable the bpoold() function which dumps the buffers
  73. // in a buffer pool.
  74. #define BufDump 1
  75. // Define this symbol to enable the bpoolv() function for validating a buffer
  76. // pool.
  77. #define BufValid 1
  78. // Define this symbol to enable the bufdump() function which allows dumping the
  79. // contents of an allocated or free buffer.
  80. #define DumpData 1
  81. #ifdef NOT_USED_NOW
  82. // Wipe free buffers to a guaranteed pattern of garbage to trip up miscreants
  83. // who attempt to use pointers into released buffers.
  84. #define FreeWipe 1
  85. // Use a best fit algorithm when searching for space for an allocation request.
  86. // This uses memory more efficiently, but allocation will be much slower.
  87. #define BestFit 1
  88. #endif /* NOT_USED_NOW */
  89. #endif /* KMP_DEBUG */
  90. static bufsize bget_bin_size[] = {
  91. 0,
  92. // 1 << 6, /* .5 Cache line */
  93. 1 << 7, /* 1 Cache line, new */
  94. 1 << 8, /* 2 Cache lines */
  95. 1 << 9, /* 4 Cache lines, new */
  96. 1 << 10, /* 8 Cache lines */
  97. 1 << 11, /* 16 Cache lines, new */
  98. 1 << 12, 1 << 13, /* new */
  99. 1 << 14, 1 << 15, /* new */
  100. 1 << 16, 1 << 17, 1 << 18, 1 << 19, 1 << 20, /* 1MB */
  101. 1 << 21, /* 2MB */
  102. 1 << 22, /* 4MB */
  103. 1 << 23, /* 8MB */
  104. 1 << 24, /* 16MB */
  105. 1 << 25, /* 32MB */
  106. };
  107. #define MAX_BGET_BINS (int)(sizeof(bget_bin_size) / sizeof(bufsize))
  108. struct bfhead;
  109. // Declare the interface, including the requested buffer size type, bufsize.
  110. /* Queue links */
  111. typedef struct qlinks {
  112. struct bfhead *flink; /* Forward link */
  113. struct bfhead *blink; /* Backward link */
  114. } qlinks_t;
  115. /* Header in allocated and free buffers */
  116. typedef struct bhead2 {
  117. kmp_info_t *bthr; /* The thread which owns the buffer pool */
  118. bufsize prevfree; /* Relative link back to previous free buffer in memory or
  119. 0 if previous buffer is allocated. */
  120. bufsize bsize; /* Buffer size: positive if free, negative if allocated. */
  121. } bhead2_t;
  122. /* Make sure the bhead structure is a multiple of SizeQuant in size. */
  123. typedef union bhead {
  124. KMP_ALIGN(SizeQuant)
  125. AlignType b_align;
  126. char b_pad[sizeof(bhead2_t) + (SizeQuant - (sizeof(bhead2_t) % SizeQuant))];
  127. bhead2_t bb;
  128. } bhead_t;
  129. #define BH(p) ((bhead_t *)(p))
  130. /* Header in directly allocated buffers (by acqfcn) */
  131. typedef struct bdhead {
  132. bufsize tsize; /* Total size, including overhead */
  133. bhead_t bh; /* Common header */
  134. } bdhead_t;
  135. #define BDH(p) ((bdhead_t *)(p))
  136. /* Header in free buffers */
  137. typedef struct bfhead {
  138. bhead_t bh; /* Common allocated/free header */
  139. qlinks_t ql; /* Links on free list */
  140. } bfhead_t;
  141. #define BFH(p) ((bfhead_t *)(p))
  142. typedef struct thr_data {
  143. bfhead_t freelist[MAX_BGET_BINS];
  144. #if BufStats
  145. size_t totalloc; /* Total space currently allocated */
  146. long numget, numrel; /* Number of bget() and brel() calls */
  147. long numpblk; /* Number of pool blocks */
  148. long numpget, numprel; /* Number of block gets and rels */
  149. long numdget, numdrel; /* Number of direct gets and rels */
  150. #endif /* BufStats */
  151. /* Automatic expansion block management functions */
  152. bget_compact_t compfcn;
  153. bget_acquire_t acqfcn;
  154. bget_release_t relfcn;
  155. bget_mode_t mode; /* what allocation mode to use? */
  156. bufsize exp_incr; /* Expansion block size */
  157. bufsize pool_len; /* 0: no bpool calls have been made
  158. -1: not all pool blocks are the same size
  159. >0: (common) block size for all bpool calls made so far
  160. */
  161. bfhead_t *last_pool; /* Last pool owned by this thread (delay deallocation) */
  162. } thr_data_t;
  163. /* Minimum allocation quantum: */
  164. #define QLSize (sizeof(qlinks_t))
  165. #define SizeQ ((SizeQuant > QLSize) ? SizeQuant : QLSize)
  166. #define MaxSize \
  167. (bufsize)( \
  168. ~(((bufsize)(1) << (sizeof(bufsize) * CHAR_BIT - 1)) | (SizeQuant - 1)))
  169. // Maximum for the requested size.
  170. /* End sentinel: value placed in bsize field of dummy block delimiting
  171. end of pool block. The most negative number which will fit in a
  172. bufsize, defined in a way that the compiler will accept. */
  173. #define ESent \
  174. ((bufsize)(-(((((bufsize)1) << ((int)sizeof(bufsize) * 8 - 2)) - 1) * 2) - 2))
  175. /* Thread Data management routines */
  176. static int bget_get_bin(bufsize size) {
  177. // binary chop bins
  178. int lo = 0, hi = MAX_BGET_BINS - 1;
  179. KMP_DEBUG_ASSERT(size > 0);
  180. while ((hi - lo) > 1) {
  181. int mid = (lo + hi) >> 1;
  182. if (size < bget_bin_size[mid])
  183. hi = mid - 1;
  184. else
  185. lo = mid;
  186. }
  187. KMP_DEBUG_ASSERT((lo >= 0) && (lo < MAX_BGET_BINS));
  188. return lo;
  189. }
  190. static void set_thr_data(kmp_info_t *th) {
  191. int i;
  192. thr_data_t *data;
  193. data = (thr_data_t *)((!th->th.th_local.bget_data)
  194. ? __kmp_allocate(sizeof(*data))
  195. : th->th.th_local.bget_data);
  196. memset(data, '\0', sizeof(*data));
  197. for (i = 0; i < MAX_BGET_BINS; ++i) {
  198. data->freelist[i].ql.flink = &data->freelist[i];
  199. data->freelist[i].ql.blink = &data->freelist[i];
  200. }
  201. th->th.th_local.bget_data = data;
  202. th->th.th_local.bget_list = 0;
  203. #if !USE_CMP_XCHG_FOR_BGET
  204. #ifdef USE_QUEUING_LOCK_FOR_BGET
  205. __kmp_init_lock(&th->th.th_local.bget_lock);
  206. #else
  207. __kmp_init_bootstrap_lock(&th->th.th_local.bget_lock);
  208. #endif /* USE_LOCK_FOR_BGET */
  209. #endif /* ! USE_CMP_XCHG_FOR_BGET */
  210. }
  211. static thr_data_t *get_thr_data(kmp_info_t *th) {
  212. thr_data_t *data;
  213. data = (thr_data_t *)th->th.th_local.bget_data;
  214. KMP_DEBUG_ASSERT(data != 0);
  215. return data;
  216. }
  217. /* Walk the free list and release the enqueued buffers */
  218. static void __kmp_bget_dequeue(kmp_info_t *th) {
  219. void *p = TCR_SYNC_PTR(th->th.th_local.bget_list);
  220. if (p != 0) {
  221. #if USE_CMP_XCHG_FOR_BGET
  222. {
  223. volatile void *old_value = TCR_SYNC_PTR(th->th.th_local.bget_list);
  224. while (!KMP_COMPARE_AND_STORE_PTR(&th->th.th_local.bget_list,
  225. CCAST(void *, old_value), nullptr)) {
  226. KMP_CPU_PAUSE();
  227. old_value = TCR_SYNC_PTR(th->th.th_local.bget_list);
  228. }
  229. p = CCAST(void *, old_value);
  230. }
  231. #else /* ! USE_CMP_XCHG_FOR_BGET */
  232. #ifdef USE_QUEUING_LOCK_FOR_BGET
  233. __kmp_acquire_lock(&th->th.th_local.bget_lock, __kmp_gtid_from_thread(th));
  234. #else
  235. __kmp_acquire_bootstrap_lock(&th->th.th_local.bget_lock);
  236. #endif /* USE_QUEUING_LOCK_FOR_BGET */
  237. p = (void *)th->th.th_local.bget_list;
  238. th->th.th_local.bget_list = 0;
  239. #ifdef USE_QUEUING_LOCK_FOR_BGET
  240. __kmp_release_lock(&th->th.th_local.bget_lock, __kmp_gtid_from_thread(th));
  241. #else
  242. __kmp_release_bootstrap_lock(&th->th.th_local.bget_lock);
  243. #endif
  244. #endif /* USE_CMP_XCHG_FOR_BGET */
  245. /* Check again to make sure the list is not empty */
  246. while (p != 0) {
  247. void *buf = p;
  248. bfhead_t *b = BFH(((char *)p) - sizeof(bhead_t));
  249. KMP_DEBUG_ASSERT(b->bh.bb.bsize != 0);
  250. KMP_DEBUG_ASSERT(((kmp_uintptr_t)TCR_PTR(b->bh.bb.bthr) & ~1) ==
  251. (kmp_uintptr_t)th); // clear possible mark
  252. KMP_DEBUG_ASSERT(b->ql.blink == 0);
  253. p = (void *)b->ql.flink;
  254. brel(th, buf);
  255. }
  256. }
  257. }
  258. /* Chain together the free buffers by using the thread owner field */
  259. static void __kmp_bget_enqueue(kmp_info_t *th, void *buf
  260. #ifdef USE_QUEUING_LOCK_FOR_BGET
  261. ,
  262. kmp_int32 rel_gtid
  263. #endif
  264. ) {
  265. bfhead_t *b = BFH(((char *)buf) - sizeof(bhead_t));
  266. KMP_DEBUG_ASSERT(b->bh.bb.bsize != 0);
  267. KMP_DEBUG_ASSERT(((kmp_uintptr_t)TCR_PTR(b->bh.bb.bthr) & ~1) ==
  268. (kmp_uintptr_t)th); // clear possible mark
  269. b->ql.blink = 0;
  270. KC_TRACE(10, ("__kmp_bget_enqueue: moving buffer to T#%d list\n",
  271. __kmp_gtid_from_thread(th)));
  272. #if USE_CMP_XCHG_FOR_BGET
  273. {
  274. volatile void *old_value = TCR_PTR(th->th.th_local.bget_list);
  275. /* the next pointer must be set before setting bget_list to buf to avoid
  276. exposing a broken list to other threads, even for an instant. */
  277. b->ql.flink = BFH(CCAST(void *, old_value));
  278. while (!KMP_COMPARE_AND_STORE_PTR(&th->th.th_local.bget_list,
  279. CCAST(void *, old_value), buf)) {
  280. KMP_CPU_PAUSE();
  281. old_value = TCR_PTR(th->th.th_local.bget_list);
  282. /* the next pointer must be set before setting bget_list to buf to avoid
  283. exposing a broken list to other threads, even for an instant. */
  284. b->ql.flink = BFH(CCAST(void *, old_value));
  285. }
  286. }
  287. #else /* ! USE_CMP_XCHG_FOR_BGET */
  288. #ifdef USE_QUEUING_LOCK_FOR_BGET
  289. __kmp_acquire_lock(&th->th.th_local.bget_lock, rel_gtid);
  290. #else
  291. __kmp_acquire_bootstrap_lock(&th->th.th_local.bget_lock);
  292. #endif
  293. b->ql.flink = BFH(th->th.th_local.bget_list);
  294. th->th.th_local.bget_list = (void *)buf;
  295. #ifdef USE_QUEUING_LOCK_FOR_BGET
  296. __kmp_release_lock(&th->th.th_local.bget_lock, rel_gtid);
  297. #else
  298. __kmp_release_bootstrap_lock(&th->th.th_local.bget_lock);
  299. #endif
  300. #endif /* USE_CMP_XCHG_FOR_BGET */
  301. }
  302. /* insert buffer back onto a new freelist */
  303. static void __kmp_bget_insert_into_freelist(thr_data_t *thr, bfhead_t *b) {
  304. int bin;
  305. KMP_DEBUG_ASSERT(((size_t)b) % SizeQuant == 0);
  306. KMP_DEBUG_ASSERT(b->bh.bb.bsize % SizeQuant == 0);
  307. bin = bget_get_bin(b->bh.bb.bsize);
  308. KMP_DEBUG_ASSERT(thr->freelist[bin].ql.blink->ql.flink ==
  309. &thr->freelist[bin]);
  310. KMP_DEBUG_ASSERT(thr->freelist[bin].ql.flink->ql.blink ==
  311. &thr->freelist[bin]);
  312. b->ql.flink = &thr->freelist[bin];
  313. b->ql.blink = thr->freelist[bin].ql.blink;
  314. thr->freelist[bin].ql.blink = b;
  315. b->ql.blink->ql.flink = b;
  316. }
  317. /* unlink the buffer from the old freelist */
  318. static void __kmp_bget_remove_from_freelist(bfhead_t *b) {
  319. KMP_DEBUG_ASSERT(b->ql.blink->ql.flink == b);
  320. KMP_DEBUG_ASSERT(b->ql.flink->ql.blink == b);
  321. b->ql.blink->ql.flink = b->ql.flink;
  322. b->ql.flink->ql.blink = b->ql.blink;
  323. }
  324. /* GET STATS -- check info on free list */
  325. static void bcheck(kmp_info_t *th, bufsize *max_free, bufsize *total_free) {
  326. thr_data_t *thr = get_thr_data(th);
  327. int bin;
  328. *total_free = *max_free = 0;
  329. for (bin = 0; bin < MAX_BGET_BINS; ++bin) {
  330. bfhead_t *b, *best;
  331. best = &thr->freelist[bin];
  332. b = best->ql.flink;
  333. while (b != &thr->freelist[bin]) {
  334. *total_free += (b->bh.bb.bsize - sizeof(bhead_t));
  335. if ((best == &thr->freelist[bin]) || (b->bh.bb.bsize < best->bh.bb.bsize))
  336. best = b;
  337. /* Link to next buffer */
  338. b = b->ql.flink;
  339. }
  340. if (*max_free < best->bh.bb.bsize)
  341. *max_free = best->bh.bb.bsize;
  342. }
  343. if (*max_free > (bufsize)sizeof(bhead_t))
  344. *max_free -= sizeof(bhead_t);
  345. }
  346. /* BGET -- Allocate a buffer. */
  347. static void *bget(kmp_info_t *th, bufsize requested_size) {
  348. thr_data_t *thr = get_thr_data(th);
  349. bufsize size = requested_size;
  350. bfhead_t *b;
  351. void *buf;
  352. int compactseq = 0;
  353. int use_blink = 0;
  354. /* For BestFit */
  355. bfhead_t *best;
  356. if (size < 0 || size + sizeof(bhead_t) > MaxSize) {
  357. return NULL;
  358. }
  359. __kmp_bget_dequeue(th); /* Release any queued buffers */
  360. if (size < (bufsize)SizeQ) { // Need at least room for the queue links.
  361. size = SizeQ;
  362. }
  363. #if defined(SizeQuant) && (SizeQuant > 1)
  364. size = (size + (SizeQuant - 1)) & (~(SizeQuant - 1));
  365. #endif
  366. size += sizeof(bhead_t); // Add overhead in allocated buffer to size required.
  367. KMP_DEBUG_ASSERT(size >= 0);
  368. KMP_DEBUG_ASSERT(size % SizeQuant == 0);
  369. use_blink = (thr->mode == bget_mode_lifo);
  370. /* If a compact function was provided in the call to bectl(), wrap
  371. a loop around the allocation process to allow compaction to
  372. intervene in case we don't find a suitable buffer in the chain. */
  373. for (;;) {
  374. int bin;
  375. for (bin = bget_get_bin(size); bin < MAX_BGET_BINS; ++bin) {
  376. /* Link to next buffer */
  377. b = (use_blink ? thr->freelist[bin].ql.blink
  378. : thr->freelist[bin].ql.flink);
  379. if (thr->mode == bget_mode_best) {
  380. best = &thr->freelist[bin];
  381. /* Scan the free list searching for the first buffer big enough
  382. to hold the requested size buffer. */
  383. while (b != &thr->freelist[bin]) {
  384. if (b->bh.bb.bsize >= (bufsize)size) {
  385. if ((best == &thr->freelist[bin]) ||
  386. (b->bh.bb.bsize < best->bh.bb.bsize)) {
  387. best = b;
  388. }
  389. }
  390. /* Link to next buffer */
  391. b = (use_blink ? b->ql.blink : b->ql.flink);
  392. }
  393. b = best;
  394. }
  395. while (b != &thr->freelist[bin]) {
  396. if ((bufsize)b->bh.bb.bsize >= (bufsize)size) {
  397. // Buffer is big enough to satisfy the request. Allocate it to the
  398. // caller. We must decide whether the buffer is large enough to split
  399. // into the part given to the caller and a free buffer that remains
  400. // on the free list, or whether the entire buffer should be removed
  401. // from the free list and given to the caller in its entirety. We
  402. // only split the buffer if enough room remains for a header plus the
  403. // minimum quantum of allocation.
  404. if ((b->bh.bb.bsize - (bufsize)size) >
  405. (bufsize)(SizeQ + (sizeof(bhead_t)))) {
  406. bhead_t *ba, *bn;
  407. ba = BH(((char *)b) + (b->bh.bb.bsize - (bufsize)size));
  408. bn = BH(((char *)ba) + size);
  409. KMP_DEBUG_ASSERT(bn->bb.prevfree == b->bh.bb.bsize);
  410. /* Subtract size from length of free block. */
  411. b->bh.bb.bsize -= (bufsize)size;
  412. /* Link allocated buffer to the previous free buffer. */
  413. ba->bb.prevfree = b->bh.bb.bsize;
  414. /* Plug negative size into user buffer. */
  415. ba->bb.bsize = -size;
  416. /* Mark this buffer as owned by this thread. */
  417. TCW_PTR(ba->bb.bthr,
  418. th); // not an allocated address (do not mark it)
  419. /* Mark buffer after this one not preceded by free block. */
  420. bn->bb.prevfree = 0;
  421. // unlink buffer from old freelist, and reinsert into new freelist
  422. __kmp_bget_remove_from_freelist(b);
  423. __kmp_bget_insert_into_freelist(thr, b);
  424. #if BufStats
  425. thr->totalloc += (size_t)size;
  426. thr->numget++; /* Increment number of bget() calls */
  427. #endif
  428. buf = (void *)((((char *)ba) + sizeof(bhead_t)));
  429. KMP_DEBUG_ASSERT(((size_t)buf) % SizeQuant == 0);
  430. return buf;
  431. } else {
  432. bhead_t *ba;
  433. ba = BH(((char *)b) + b->bh.bb.bsize);
  434. KMP_DEBUG_ASSERT(ba->bb.prevfree == b->bh.bb.bsize);
  435. /* The buffer isn't big enough to split. Give the whole
  436. shebang to the caller and remove it from the free list. */
  437. __kmp_bget_remove_from_freelist(b);
  438. #if BufStats
  439. thr->totalloc += (size_t)b->bh.bb.bsize;
  440. thr->numget++; /* Increment number of bget() calls */
  441. #endif
  442. /* Negate size to mark buffer allocated. */
  443. b->bh.bb.bsize = -(b->bh.bb.bsize);
  444. /* Mark this buffer as owned by this thread. */
  445. TCW_PTR(ba->bb.bthr, th); // not an allocated address (do not mark)
  446. /* Zero the back pointer in the next buffer in memory
  447. to indicate that this buffer is allocated. */
  448. ba->bb.prevfree = 0;
  449. /* Give user buffer starting at queue links. */
  450. buf = (void *)&(b->ql);
  451. KMP_DEBUG_ASSERT(((size_t)buf) % SizeQuant == 0);
  452. return buf;
  453. }
  454. }
  455. /* Link to next buffer */
  456. b = (use_blink ? b->ql.blink : b->ql.flink);
  457. }
  458. }
  459. /* We failed to find a buffer. If there's a compact function defined,
  460. notify it of the size requested. If it returns TRUE, try the allocation
  461. again. */
  462. if ((thr->compfcn == 0) || (!(*thr->compfcn)(size, ++compactseq))) {
  463. break;
  464. }
  465. }
  466. /* No buffer available with requested size free. */
  467. /* Don't give up yet -- look in the reserve supply. */
  468. if (thr->acqfcn != 0) {
  469. if (size > (bufsize)(thr->exp_incr - sizeof(bhead_t))) {
  470. /* Request is too large to fit in a single expansion block.
  471. Try to satisfy it by a direct buffer acquisition. */
  472. bdhead_t *bdh;
  473. size += sizeof(bdhead_t) - sizeof(bhead_t);
  474. KE_TRACE(10, ("%%%%%% MALLOC( %d )\n", (int)size));
  475. /* richryan */
  476. bdh = BDH((*thr->acqfcn)((bufsize)size));
  477. if (bdh != NULL) {
  478. // Mark the buffer special by setting size field of its header to zero.
  479. bdh->bh.bb.bsize = 0;
  480. /* Mark this buffer as owned by this thread. */
  481. TCW_PTR(bdh->bh.bb.bthr, th); // don't mark buffer as allocated,
  482. // because direct buffer never goes to free list
  483. bdh->bh.bb.prevfree = 0;
  484. bdh->tsize = size;
  485. #if BufStats
  486. thr->totalloc += (size_t)size;
  487. thr->numget++; /* Increment number of bget() calls */
  488. thr->numdget++; /* Direct bget() call count */
  489. #endif
  490. buf = (void *)(bdh + 1);
  491. KMP_DEBUG_ASSERT(((size_t)buf) % SizeQuant == 0);
  492. return buf;
  493. }
  494. } else {
  495. /* Try to obtain a new expansion block */
  496. void *newpool;
  497. KE_TRACE(10, ("%%%%%% MALLOCB( %d )\n", (int)thr->exp_incr));
  498. /* richryan */
  499. newpool = (*thr->acqfcn)((bufsize)thr->exp_incr);
  500. KMP_DEBUG_ASSERT(((size_t)newpool) % SizeQuant == 0);
  501. if (newpool != NULL) {
  502. bpool(th, newpool, thr->exp_incr);
  503. buf = bget(
  504. th, requested_size); /* This can't, I say, can't get into a loop. */
  505. return buf;
  506. }
  507. }
  508. }
  509. /* Still no buffer available */
  510. return NULL;
  511. }
  512. /* BGETZ -- Allocate a buffer and clear its contents to zero. We clear
  513. the entire contents of the buffer to zero, not just the
  514. region requested by the caller. */
  515. static void *bgetz(kmp_info_t *th, bufsize size) {
  516. char *buf = (char *)bget(th, size);
  517. if (buf != NULL) {
  518. bhead_t *b;
  519. bufsize rsize;
  520. b = BH(buf - sizeof(bhead_t));
  521. rsize = -(b->bb.bsize);
  522. if (rsize == 0) {
  523. bdhead_t *bd;
  524. bd = BDH(buf - sizeof(bdhead_t));
  525. rsize = bd->tsize - (bufsize)sizeof(bdhead_t);
  526. } else {
  527. rsize -= sizeof(bhead_t);
  528. }
  529. KMP_DEBUG_ASSERT(rsize >= size);
  530. (void)memset(buf, 0, (bufsize)rsize);
  531. }
  532. return ((void *)buf);
  533. }
  534. /* BGETR -- Reallocate a buffer. This is a minimal implementation,
  535. simply in terms of brel() and bget(). It could be
  536. enhanced to allow the buffer to grow into adjacent free
  537. blocks and to avoid moving data unnecessarily. */
  538. static void *bgetr(kmp_info_t *th, void *buf, bufsize size) {
  539. void *nbuf;
  540. bufsize osize; /* Old size of buffer */
  541. bhead_t *b;
  542. nbuf = bget(th, size);
  543. if (nbuf == NULL) { /* Acquire new buffer */
  544. return NULL;
  545. }
  546. if (buf == NULL) {
  547. return nbuf;
  548. }
  549. b = BH(((char *)buf) - sizeof(bhead_t));
  550. osize = -b->bb.bsize;
  551. if (osize == 0) {
  552. /* Buffer acquired directly through acqfcn. */
  553. bdhead_t *bd;
  554. bd = BDH(((char *)buf) - sizeof(bdhead_t));
  555. osize = bd->tsize - (bufsize)sizeof(bdhead_t);
  556. } else {
  557. osize -= sizeof(bhead_t);
  558. }
  559. KMP_DEBUG_ASSERT(osize > 0);
  560. (void)KMP_MEMCPY((char *)nbuf, (char *)buf, /* Copy the data */
  561. (size_t)((size < osize) ? size : osize));
  562. brel(th, buf);
  563. return nbuf;
  564. }
  565. /* BREL -- Release a buffer. */
  566. static void brel(kmp_info_t *th, void *buf) {
  567. thr_data_t *thr = get_thr_data(th);
  568. bfhead_t *b, *bn;
  569. kmp_info_t *bth;
  570. KMP_DEBUG_ASSERT(buf != NULL);
  571. KMP_DEBUG_ASSERT(((size_t)buf) % SizeQuant == 0);
  572. b = BFH(((char *)buf) - sizeof(bhead_t));
  573. if (b->bh.bb.bsize == 0) { /* Directly-acquired buffer? */
  574. bdhead_t *bdh;
  575. bdh = BDH(((char *)buf) - sizeof(bdhead_t));
  576. KMP_DEBUG_ASSERT(b->bh.bb.prevfree == 0);
  577. #if BufStats
  578. thr->totalloc -= (size_t)bdh->tsize;
  579. thr->numdrel++; /* Number of direct releases */
  580. thr->numrel++; /* Increment number of brel() calls */
  581. #endif /* BufStats */
  582. #ifdef FreeWipe
  583. (void)memset((char *)buf, 0x55, (size_t)(bdh->tsize - sizeof(bdhead_t)));
  584. #endif /* FreeWipe */
  585. KE_TRACE(10, ("%%%%%% FREE( %p )\n", (void *)bdh));
  586. KMP_DEBUG_ASSERT(thr->relfcn != 0);
  587. (*thr->relfcn)((void *)bdh); /* Release it directly. */
  588. return;
  589. }
  590. bth = (kmp_info_t *)((kmp_uintptr_t)TCR_PTR(b->bh.bb.bthr) &
  591. ~1); // clear possible mark before comparison
  592. if (bth != th) {
  593. /* Add this buffer to be released by the owning thread later */
  594. __kmp_bget_enqueue(bth, buf
  595. #ifdef USE_QUEUING_LOCK_FOR_BGET
  596. ,
  597. __kmp_gtid_from_thread(th)
  598. #endif
  599. );
  600. return;
  601. }
  602. /* Buffer size must be negative, indicating that the buffer is allocated. */
  603. if (b->bh.bb.bsize >= 0) {
  604. bn = NULL;
  605. }
  606. KMP_DEBUG_ASSERT(b->bh.bb.bsize < 0);
  607. /* Back pointer in next buffer must be zero, indicating the same thing: */
  608. KMP_DEBUG_ASSERT(BH((char *)b - b->bh.bb.bsize)->bb.prevfree == 0);
  609. #if BufStats
  610. thr->numrel++; /* Increment number of brel() calls */
  611. thr->totalloc += (size_t)b->bh.bb.bsize;
  612. #endif
  613. /* If the back link is nonzero, the previous buffer is free. */
  614. if (b->bh.bb.prevfree != 0) {
  615. /* The previous buffer is free. Consolidate this buffer with it by adding
  616. the length of this buffer to the previous free buffer. Note that we
  617. subtract the size in the buffer being released, since it's negative to
  618. indicate that the buffer is allocated. */
  619. bufsize size = b->bh.bb.bsize;
  620. /* Make the previous buffer the one we're working on. */
  621. KMP_DEBUG_ASSERT(BH((char *)b - b->bh.bb.prevfree)->bb.bsize ==
  622. b->bh.bb.prevfree);
  623. b = BFH(((char *)b) - b->bh.bb.prevfree);
  624. b->bh.bb.bsize -= size;
  625. /* unlink the buffer from the old freelist */
  626. __kmp_bget_remove_from_freelist(b);
  627. } else {
  628. /* The previous buffer isn't allocated. Mark this buffer size as positive
  629. (i.e. free) and fall through to place the buffer on the free list as an
  630. isolated free block. */
  631. b->bh.bb.bsize = -b->bh.bb.bsize;
  632. }
  633. /* insert buffer back onto a new freelist */
  634. __kmp_bget_insert_into_freelist(thr, b);
  635. /* Now we look at the next buffer in memory, located by advancing from
  636. the start of this buffer by its size, to see if that buffer is
  637. free. If it is, we combine this buffer with the next one in
  638. memory, dechaining the second buffer from the free list. */
  639. bn = BFH(((char *)b) + b->bh.bb.bsize);
  640. if (bn->bh.bb.bsize > 0) {
  641. /* The buffer is free. Remove it from the free list and add
  642. its size to that of our buffer. */
  643. KMP_DEBUG_ASSERT(BH((char *)bn + bn->bh.bb.bsize)->bb.prevfree ==
  644. bn->bh.bb.bsize);
  645. __kmp_bget_remove_from_freelist(bn);
  646. b->bh.bb.bsize += bn->bh.bb.bsize;
  647. /* unlink the buffer from the old freelist, and reinsert it into the new
  648. * freelist */
  649. __kmp_bget_remove_from_freelist(b);
  650. __kmp_bget_insert_into_freelist(thr, b);
  651. /* Finally, advance to the buffer that follows the newly
  652. consolidated free block. We must set its backpointer to the
  653. head of the consolidated free block. We know the next block
  654. must be an allocated block because the process of recombination
  655. guarantees that two free blocks will never be contiguous in
  656. memory. */
  657. bn = BFH(((char *)b) + b->bh.bb.bsize);
  658. }
  659. #ifdef FreeWipe
  660. (void)memset(((char *)b) + sizeof(bfhead_t), 0x55,
  661. (size_t)(b->bh.bb.bsize - sizeof(bfhead_t)));
  662. #endif
  663. KMP_DEBUG_ASSERT(bn->bh.bb.bsize < 0);
  664. /* The next buffer is allocated. Set the backpointer in it to point
  665. to this buffer; the previous free buffer in memory. */
  666. bn->bh.bb.prevfree = b->bh.bb.bsize;
  667. /* If a block-release function is defined, and this free buffer
  668. constitutes the entire block, release it. Note that pool_len
  669. is defined in such a way that the test will fail unless all
  670. pool blocks are the same size. */
  671. if (thr->relfcn != 0 &&
  672. b->bh.bb.bsize == (bufsize)(thr->pool_len - sizeof(bhead_t))) {
  673. #if BufStats
  674. if (thr->numpblk !=
  675. 1) { /* Do not release the last buffer until finalization time */
  676. #endif
  677. KMP_DEBUG_ASSERT(b->bh.bb.prevfree == 0);
  678. KMP_DEBUG_ASSERT(BH((char *)b + b->bh.bb.bsize)->bb.bsize == ESent);
  679. KMP_DEBUG_ASSERT(BH((char *)b + b->bh.bb.bsize)->bb.prevfree ==
  680. b->bh.bb.bsize);
  681. /* Unlink the buffer from the free list */
  682. __kmp_bget_remove_from_freelist(b);
  683. KE_TRACE(10, ("%%%%%% FREE( %p )\n", (void *)b));
  684. (*thr->relfcn)(b);
  685. #if BufStats
  686. thr->numprel++; /* Nr of expansion block releases */
  687. thr->numpblk--; /* Total number of blocks */
  688. KMP_DEBUG_ASSERT(thr->numpblk == thr->numpget - thr->numprel);
  689. // avoid leaving stale last_pool pointer around if it is being dealloced
  690. if (thr->last_pool == b)
  691. thr->last_pool = 0;
  692. } else {
  693. thr->last_pool = b;
  694. }
  695. #endif /* BufStats */
  696. }
  697. }
  698. /* BECTL -- Establish automatic pool expansion control */
  699. static void bectl(kmp_info_t *th, bget_compact_t compact,
  700. bget_acquire_t acquire, bget_release_t release,
  701. bufsize pool_incr) {
  702. thr_data_t *thr = get_thr_data(th);
  703. thr->compfcn = compact;
  704. thr->acqfcn = acquire;
  705. thr->relfcn = release;
  706. thr->exp_incr = pool_incr;
  707. }
  708. /* BPOOL -- Add a region of memory to the buffer pool. */
  709. static void bpool(kmp_info_t *th, void *buf, bufsize len) {
  710. /* int bin = 0; */
  711. thr_data_t *thr = get_thr_data(th);
  712. bfhead_t *b = BFH(buf);
  713. bhead_t *bn;
  714. __kmp_bget_dequeue(th); /* Release any queued buffers */
  715. #ifdef SizeQuant
  716. len &= ~((bufsize)(SizeQuant - 1));
  717. #endif
  718. if (thr->pool_len == 0) {
  719. thr->pool_len = len;
  720. } else if (len != thr->pool_len) {
  721. thr->pool_len = -1;
  722. }
  723. #if BufStats
  724. thr->numpget++; /* Number of block acquisitions */
  725. thr->numpblk++; /* Number of blocks total */
  726. KMP_DEBUG_ASSERT(thr->numpblk == thr->numpget - thr->numprel);
  727. #endif /* BufStats */
  728. /* Since the block is initially occupied by a single free buffer,
  729. it had better not be (much) larger than the largest buffer
  730. whose size we can store in bhead.bb.bsize. */
  731. KMP_DEBUG_ASSERT(len - sizeof(bhead_t) <= -((bufsize)ESent + 1));
  732. /* Clear the backpointer at the start of the block to indicate that
  733. there is no free block prior to this one. That blocks
  734. recombination when the first block in memory is released. */
  735. b->bh.bb.prevfree = 0;
  736. /* Create a dummy allocated buffer at the end of the pool. This dummy
  737. buffer is seen when a buffer at the end of the pool is released and
  738. blocks recombination of the last buffer with the dummy buffer at
  739. the end. The length in the dummy buffer is set to the largest
  740. negative number to denote the end of the pool for diagnostic
  741. routines (this specific value is not counted on by the actual
  742. allocation and release functions). */
  743. len -= sizeof(bhead_t);
  744. b->bh.bb.bsize = (bufsize)len;
  745. /* Set the owner of this buffer */
  746. TCW_PTR(b->bh.bb.bthr,
  747. (kmp_info_t *)((kmp_uintptr_t)th |
  748. 1)); // mark the buffer as allocated address
  749. /* Chain the new block to the free list. */
  750. __kmp_bget_insert_into_freelist(thr, b);
  751. #ifdef FreeWipe
  752. (void)memset(((char *)b) + sizeof(bfhead_t), 0x55,
  753. (size_t)(len - sizeof(bfhead_t)));
  754. #endif
  755. bn = BH(((char *)b) + len);
  756. bn->bb.prevfree = (bufsize)len;
  757. /* Definition of ESent assumes two's complement! */
  758. KMP_DEBUG_ASSERT((~0) == -1 && (bn != 0));
  759. bn->bb.bsize = ESent;
  760. }
  761. /* BFREED -- Dump the free lists for this thread. */
  762. static void bfreed(kmp_info_t *th) {
  763. int bin = 0, count = 0;
  764. int gtid = __kmp_gtid_from_thread(th);
  765. thr_data_t *thr = get_thr_data(th);
  766. #if BufStats
  767. __kmp_printf_no_lock("__kmp_printpool: T#%d total=%" KMP_UINT64_SPEC
  768. " get=%" KMP_INT64_SPEC " rel=%" KMP_INT64_SPEC
  769. " pblk=%" KMP_INT64_SPEC " pget=%" KMP_INT64_SPEC
  770. " prel=%" KMP_INT64_SPEC " dget=%" KMP_INT64_SPEC
  771. " drel=%" KMP_INT64_SPEC "\n",
  772. gtid, (kmp_uint64)thr->totalloc, (kmp_int64)thr->numget,
  773. (kmp_int64)thr->numrel, (kmp_int64)thr->numpblk,
  774. (kmp_int64)thr->numpget, (kmp_int64)thr->numprel,
  775. (kmp_int64)thr->numdget, (kmp_int64)thr->numdrel);
  776. #endif
  777. for (bin = 0; bin < MAX_BGET_BINS; ++bin) {
  778. bfhead_t *b;
  779. for (b = thr->freelist[bin].ql.flink; b != &thr->freelist[bin];
  780. b = b->ql.flink) {
  781. bufsize bs = b->bh.bb.bsize;
  782. KMP_DEBUG_ASSERT(b->ql.blink->ql.flink == b);
  783. KMP_DEBUG_ASSERT(b->ql.flink->ql.blink == b);
  784. KMP_DEBUG_ASSERT(bs > 0);
  785. count += 1;
  786. __kmp_printf_no_lock(
  787. "__kmp_printpool: T#%d Free block: 0x%p size %6ld bytes.\n", gtid, b,
  788. (long)bs);
  789. #ifdef FreeWipe
  790. {
  791. char *lerr = ((char *)b) + sizeof(bfhead_t);
  792. if ((bs > sizeof(bfhead_t)) &&
  793. ((*lerr != 0x55) ||
  794. (memcmp(lerr, lerr + 1, (size_t)(bs - (sizeof(bfhead_t) + 1))) !=
  795. 0))) {
  796. __kmp_printf_no_lock("__kmp_printpool: T#%d (Contents of above "
  797. "free block have been overstored.)\n",
  798. gtid);
  799. }
  800. }
  801. #endif
  802. }
  803. }
  804. if (count == 0)
  805. __kmp_printf_no_lock("__kmp_printpool: T#%d No free blocks\n", gtid);
  806. }
  807. void __kmp_initialize_bget(kmp_info_t *th) {
  808. KMP_DEBUG_ASSERT(SizeQuant >= sizeof(void *) && (th != 0));
  809. set_thr_data(th);
  810. bectl(th, (bget_compact_t)0, (bget_acquire_t)malloc, (bget_release_t)free,
  811. (bufsize)__kmp_malloc_pool_incr);
  812. }
  813. void __kmp_finalize_bget(kmp_info_t *th) {
  814. thr_data_t *thr;
  815. bfhead_t *b;
  816. KMP_DEBUG_ASSERT(th != 0);
  817. #if BufStats
  818. thr = (thr_data_t *)th->th.th_local.bget_data;
  819. KMP_DEBUG_ASSERT(thr != NULL);
  820. b = thr->last_pool;
  821. /* If a block-release function is defined, and this free buffer constitutes
  822. the entire block, release it. Note that pool_len is defined in such a way
  823. that the test will fail unless all pool blocks are the same size. */
  824. // Deallocate the last pool if one exists because we no longer do it in brel()
  825. if (thr->relfcn != 0 && b != 0 && thr->numpblk != 0 &&
  826. b->bh.bb.bsize == (bufsize)(thr->pool_len - sizeof(bhead_t))) {
  827. KMP_DEBUG_ASSERT(b->bh.bb.prevfree == 0);
  828. KMP_DEBUG_ASSERT(BH((char *)b + b->bh.bb.bsize)->bb.bsize == ESent);
  829. KMP_DEBUG_ASSERT(BH((char *)b + b->bh.bb.bsize)->bb.prevfree ==
  830. b->bh.bb.bsize);
  831. /* Unlink the buffer from the free list */
  832. __kmp_bget_remove_from_freelist(b);
  833. KE_TRACE(10, ("%%%%%% FREE( %p )\n", (void *)b));
  834. (*thr->relfcn)(b);
  835. thr->numprel++; /* Nr of expansion block releases */
  836. thr->numpblk--; /* Total number of blocks */
  837. KMP_DEBUG_ASSERT(thr->numpblk == thr->numpget - thr->numprel);
  838. }
  839. #endif /* BufStats */
  840. /* Deallocate bget_data */
  841. if (th->th.th_local.bget_data != NULL) {
  842. __kmp_free(th->th.th_local.bget_data);
  843. th->th.th_local.bget_data = NULL;
  844. }
  845. }
  846. void kmpc_set_poolsize(size_t size) {
  847. bectl(__kmp_get_thread(), (bget_compact_t)0, (bget_acquire_t)malloc,
  848. (bget_release_t)free, (bufsize)size);
  849. }
  850. size_t kmpc_get_poolsize(void) {
  851. thr_data_t *p;
  852. p = get_thr_data(__kmp_get_thread());
  853. return p->exp_incr;
  854. }
  855. void kmpc_set_poolmode(int mode) {
  856. thr_data_t *p;
  857. if (mode == bget_mode_fifo || mode == bget_mode_lifo ||
  858. mode == bget_mode_best) {
  859. p = get_thr_data(__kmp_get_thread());
  860. p->mode = (bget_mode_t)mode;
  861. }
  862. }
  863. int kmpc_get_poolmode(void) {
  864. thr_data_t *p;
  865. p = get_thr_data(__kmp_get_thread());
  866. return p->mode;
  867. }
  868. void kmpc_get_poolstat(size_t *maxmem, size_t *allmem) {
  869. kmp_info_t *th = __kmp_get_thread();
  870. bufsize a, b;
  871. __kmp_bget_dequeue(th); /* Release any queued buffers */
  872. bcheck(th, &a, &b);
  873. *maxmem = a;
  874. *allmem = b;
  875. }
  876. void kmpc_poolprint(void) {
  877. kmp_info_t *th = __kmp_get_thread();
  878. __kmp_bget_dequeue(th); /* Release any queued buffers */
  879. bfreed(th);
  880. }
  881. #endif // #if KMP_USE_BGET
  882. void *kmpc_malloc(size_t size) {
  883. void *ptr;
  884. ptr = bget(__kmp_entry_thread(), (bufsize)(size + sizeof(ptr)));
  885. if (ptr != NULL) {
  886. // save allocated pointer just before one returned to user
  887. *(void **)ptr = ptr;
  888. ptr = (void **)ptr + 1;
  889. }
  890. return ptr;
  891. }
  892. #define IS_POWER_OF_TWO(n) (((n) & ((n)-1)) == 0)
  893. void *kmpc_aligned_malloc(size_t size, size_t alignment) {
  894. void *ptr;
  895. void *ptr_allocated;
  896. KMP_DEBUG_ASSERT(alignment < 32 * 1024); // Alignment should not be too big
  897. if (!IS_POWER_OF_TWO(alignment)) {
  898. // AC: do we need to issue a warning here?
  899. errno = EINVAL;
  900. return NULL;
  901. }
  902. size = size + sizeof(void *) + alignment;
  903. ptr_allocated = bget(__kmp_entry_thread(), (bufsize)size);
  904. if (ptr_allocated != NULL) {
  905. // save allocated pointer just before one returned to user
  906. ptr = (void *)(((kmp_uintptr_t)ptr_allocated + sizeof(void *) + alignment) &
  907. ~(alignment - 1));
  908. *((void **)ptr - 1) = ptr_allocated;
  909. } else {
  910. ptr = NULL;
  911. }
  912. return ptr;
  913. }
  914. void *kmpc_calloc(size_t nelem, size_t elsize) {
  915. void *ptr;
  916. ptr = bgetz(__kmp_entry_thread(), (bufsize)(nelem * elsize + sizeof(ptr)));
  917. if (ptr != NULL) {
  918. // save allocated pointer just before one returned to user
  919. *(void **)ptr = ptr;
  920. ptr = (void **)ptr + 1;
  921. }
  922. return ptr;
  923. }
  924. void *kmpc_realloc(void *ptr, size_t size) {
  925. void *result = NULL;
  926. if (ptr == NULL) {
  927. // If pointer is NULL, realloc behaves like malloc.
  928. result = bget(__kmp_entry_thread(), (bufsize)(size + sizeof(ptr)));
  929. // save allocated pointer just before one returned to user
  930. if (result != NULL) {
  931. *(void **)result = result;
  932. result = (void **)result + 1;
  933. }
  934. } else if (size == 0) {
  935. // If size is 0, realloc behaves like free.
  936. // The thread must be registered by the call to kmpc_malloc() or
  937. // kmpc_calloc() before.
  938. // So it should be safe to call __kmp_get_thread(), not
  939. // __kmp_entry_thread().
  940. KMP_ASSERT(*((void **)ptr - 1));
  941. brel(__kmp_get_thread(), *((void **)ptr - 1));
  942. } else {
  943. result = bgetr(__kmp_entry_thread(), *((void **)ptr - 1),
  944. (bufsize)(size + sizeof(ptr)));
  945. if (result != NULL) {
  946. *(void **)result = result;
  947. result = (void **)result + 1;
  948. }
  949. }
  950. return result;
  951. }
  952. // NOTE: the library must have already been initialized by a previous allocate
  953. void kmpc_free(void *ptr) {
  954. if (!__kmp_init_serial) {
  955. return;
  956. }
  957. if (ptr != NULL) {
  958. kmp_info_t *th = __kmp_get_thread();
  959. __kmp_bget_dequeue(th); /* Release any queued buffers */
  960. // extract allocated pointer and free it
  961. KMP_ASSERT(*((void **)ptr - 1));
  962. brel(th, *((void **)ptr - 1));
  963. }
  964. }
  965. void *___kmp_thread_malloc(kmp_info_t *th, size_t size KMP_SRC_LOC_DECL) {
  966. void *ptr;
  967. KE_TRACE(30, ("-> __kmp_thread_malloc( %p, %d ) called from %s:%d\n", th,
  968. (int)size KMP_SRC_LOC_PARM));
  969. ptr = bget(th, (bufsize)size);
  970. KE_TRACE(30, ("<- __kmp_thread_malloc() returns %p\n", ptr));
  971. return ptr;
  972. }
  973. void *___kmp_thread_calloc(kmp_info_t *th, size_t nelem,
  974. size_t elsize KMP_SRC_LOC_DECL) {
  975. void *ptr;
  976. KE_TRACE(30, ("-> __kmp_thread_calloc( %p, %d, %d ) called from %s:%d\n", th,
  977. (int)nelem, (int)elsize KMP_SRC_LOC_PARM));
  978. ptr = bgetz(th, (bufsize)(nelem * elsize));
  979. KE_TRACE(30, ("<- __kmp_thread_calloc() returns %p\n", ptr));
  980. return ptr;
  981. }
  982. void *___kmp_thread_realloc(kmp_info_t *th, void *ptr,
  983. size_t size KMP_SRC_LOC_DECL) {
  984. KE_TRACE(30, ("-> __kmp_thread_realloc( %p, %p, %d ) called from %s:%d\n", th,
  985. ptr, (int)size KMP_SRC_LOC_PARM));
  986. ptr = bgetr(th, ptr, (bufsize)size);
  987. KE_TRACE(30, ("<- __kmp_thread_realloc() returns %p\n", ptr));
  988. return ptr;
  989. }
  990. void ___kmp_thread_free(kmp_info_t *th, void *ptr KMP_SRC_LOC_DECL) {
  991. KE_TRACE(30, ("-> __kmp_thread_free( %p, %p ) called from %s:%d\n", th,
  992. ptr KMP_SRC_LOC_PARM));
  993. if (ptr != NULL) {
  994. __kmp_bget_dequeue(th); /* Release any queued buffers */
  995. brel(th, ptr);
  996. }
  997. KE_TRACE(30, ("<- __kmp_thread_free()\n"));
  998. }
  999. /* OMP 5.0 Memory Management support */
  1000. static const char *kmp_mk_lib_name;
  1001. static void *h_memkind;
  1002. /* memkind experimental API: */
  1003. // memkind_alloc
  1004. static void *(*kmp_mk_alloc)(void *k, size_t sz);
  1005. // memkind_free
  1006. static void (*kmp_mk_free)(void *kind, void *ptr);
  1007. // memkind_check_available
  1008. static int (*kmp_mk_check)(void *kind);
  1009. // kinds we are going to use
  1010. static void **mk_default;
  1011. static void **mk_interleave;
  1012. static void **mk_hbw;
  1013. static void **mk_hbw_interleave;
  1014. static void **mk_hbw_preferred;
  1015. static void **mk_hugetlb;
  1016. static void **mk_hbw_hugetlb;
  1017. static void **mk_hbw_preferred_hugetlb;
  1018. static void **mk_dax_kmem;
  1019. static void **mk_dax_kmem_all;
  1020. static void **mk_dax_kmem_preferred;
  1021. static void *(*kmp_target_alloc_host)(size_t size, int device);
  1022. static void *(*kmp_target_alloc_shared)(size_t size, int device);
  1023. static void *(*kmp_target_alloc_device)(size_t size, int device);
  1024. static void *(*kmp_target_free)(void *ptr, int device);
  1025. static bool __kmp_target_mem_available;
  1026. #define KMP_IS_TARGET_MEM_SPACE(MS) \
  1027. (MS == llvm_omp_target_host_mem_space || \
  1028. MS == llvm_omp_target_shared_mem_space || \
  1029. MS == llvm_omp_target_device_mem_space)
  1030. #define KMP_IS_TARGET_MEM_ALLOC(MA) \
  1031. (MA == llvm_omp_target_host_mem_alloc || \
  1032. MA == llvm_omp_target_shared_mem_alloc || \
  1033. MA == llvm_omp_target_device_mem_alloc)
  1034. #if KMP_OS_UNIX && KMP_DYNAMIC_LIB && !KMP_OS_DARWIN
  1035. static inline void chk_kind(void ***pkind) {
  1036. KMP_DEBUG_ASSERT(pkind);
  1037. if (*pkind) // symbol found
  1038. if (kmp_mk_check(**pkind)) // kind not available or error
  1039. *pkind = NULL;
  1040. }
  1041. #endif
  1042. void __kmp_init_memkind() {
  1043. // as of 2018-07-31 memkind does not support Windows*, exclude it for now
  1044. #if KMP_OS_UNIX && KMP_DYNAMIC_LIB && !KMP_OS_DARWIN
  1045. // use of statically linked memkind is problematic, as it depends on libnuma
  1046. kmp_mk_lib_name = "libmemkind.so";
  1047. h_memkind = dlopen(kmp_mk_lib_name, RTLD_LAZY);
  1048. if (h_memkind) {
  1049. kmp_mk_check = (int (*)(void *))dlsym(h_memkind, "memkind_check_available");
  1050. kmp_mk_alloc =
  1051. (void *(*)(void *, size_t))dlsym(h_memkind, "memkind_malloc");
  1052. kmp_mk_free = (void (*)(void *, void *))dlsym(h_memkind, "memkind_free");
  1053. mk_default = (void **)dlsym(h_memkind, "MEMKIND_DEFAULT");
  1054. if (kmp_mk_check && kmp_mk_alloc && kmp_mk_free && mk_default &&
  1055. !kmp_mk_check(*mk_default)) {
  1056. __kmp_memkind_available = 1;
  1057. mk_interleave = (void **)dlsym(h_memkind, "MEMKIND_INTERLEAVE");
  1058. chk_kind(&mk_interleave);
  1059. mk_hbw = (void **)dlsym(h_memkind, "MEMKIND_HBW");
  1060. chk_kind(&mk_hbw);
  1061. mk_hbw_interleave = (void **)dlsym(h_memkind, "MEMKIND_HBW_INTERLEAVE");
  1062. chk_kind(&mk_hbw_interleave);
  1063. mk_hbw_preferred = (void **)dlsym(h_memkind, "MEMKIND_HBW_PREFERRED");
  1064. chk_kind(&mk_hbw_preferred);
  1065. mk_hugetlb = (void **)dlsym(h_memkind, "MEMKIND_HUGETLB");
  1066. chk_kind(&mk_hugetlb);
  1067. mk_hbw_hugetlb = (void **)dlsym(h_memkind, "MEMKIND_HBW_HUGETLB");
  1068. chk_kind(&mk_hbw_hugetlb);
  1069. mk_hbw_preferred_hugetlb =
  1070. (void **)dlsym(h_memkind, "MEMKIND_HBW_PREFERRED_HUGETLB");
  1071. chk_kind(&mk_hbw_preferred_hugetlb);
  1072. mk_dax_kmem = (void **)dlsym(h_memkind, "MEMKIND_DAX_KMEM");
  1073. chk_kind(&mk_dax_kmem);
  1074. mk_dax_kmem_all = (void **)dlsym(h_memkind, "MEMKIND_DAX_KMEM_ALL");
  1075. chk_kind(&mk_dax_kmem_all);
  1076. mk_dax_kmem_preferred =
  1077. (void **)dlsym(h_memkind, "MEMKIND_DAX_KMEM_PREFERRED");
  1078. chk_kind(&mk_dax_kmem_preferred);
  1079. KE_TRACE(25, ("__kmp_init_memkind: memkind library initialized\n"));
  1080. return; // success
  1081. }
  1082. dlclose(h_memkind); // failure
  1083. }
  1084. #else // !(KMP_OS_UNIX && KMP_DYNAMIC_LIB)
  1085. kmp_mk_lib_name = "";
  1086. #endif // !(KMP_OS_UNIX && KMP_DYNAMIC_LIB)
  1087. h_memkind = NULL;
  1088. kmp_mk_check = NULL;
  1089. kmp_mk_alloc = NULL;
  1090. kmp_mk_free = NULL;
  1091. mk_default = NULL;
  1092. mk_interleave = NULL;
  1093. mk_hbw = NULL;
  1094. mk_hbw_interleave = NULL;
  1095. mk_hbw_preferred = NULL;
  1096. mk_hugetlb = NULL;
  1097. mk_hbw_hugetlb = NULL;
  1098. mk_hbw_preferred_hugetlb = NULL;
  1099. mk_dax_kmem = NULL;
  1100. mk_dax_kmem_all = NULL;
  1101. mk_dax_kmem_preferred = NULL;
  1102. }
  1103. void __kmp_fini_memkind() {
  1104. #if KMP_OS_UNIX && KMP_DYNAMIC_LIB
  1105. if (__kmp_memkind_available)
  1106. KE_TRACE(25, ("__kmp_fini_memkind: finalize memkind library\n"));
  1107. if (h_memkind) {
  1108. dlclose(h_memkind);
  1109. h_memkind = NULL;
  1110. }
  1111. kmp_mk_check = NULL;
  1112. kmp_mk_alloc = NULL;
  1113. kmp_mk_free = NULL;
  1114. mk_default = NULL;
  1115. mk_interleave = NULL;
  1116. mk_hbw = NULL;
  1117. mk_hbw_interleave = NULL;
  1118. mk_hbw_preferred = NULL;
  1119. mk_hugetlb = NULL;
  1120. mk_hbw_hugetlb = NULL;
  1121. mk_hbw_preferred_hugetlb = NULL;
  1122. mk_dax_kmem = NULL;
  1123. mk_dax_kmem_all = NULL;
  1124. mk_dax_kmem_preferred = NULL;
  1125. #endif
  1126. }
  1127. void __kmp_init_target_mem() {
  1128. *(void **)(&kmp_target_alloc_host) = KMP_DLSYM("llvm_omp_target_alloc_host");
  1129. *(void **)(&kmp_target_alloc_shared) =
  1130. KMP_DLSYM("llvm_omp_target_alloc_shared");
  1131. *(void **)(&kmp_target_alloc_device) =
  1132. KMP_DLSYM("llvm_omp_target_alloc_device");
  1133. *(void **)(&kmp_target_free) = KMP_DLSYM("omp_target_free");
  1134. __kmp_target_mem_available = kmp_target_alloc_host &&
  1135. kmp_target_alloc_shared &&
  1136. kmp_target_alloc_device && kmp_target_free;
  1137. }
  1138. omp_allocator_handle_t __kmpc_init_allocator(int gtid, omp_memspace_handle_t ms,
  1139. int ntraits,
  1140. omp_alloctrait_t traits[]) {
  1141. // OpenMP 5.0 only allows predefined memspaces
  1142. KMP_DEBUG_ASSERT(ms == omp_default_mem_space || ms == omp_low_lat_mem_space ||
  1143. ms == omp_large_cap_mem_space || ms == omp_const_mem_space ||
  1144. ms == omp_high_bw_mem_space || KMP_IS_TARGET_MEM_SPACE(ms));
  1145. kmp_allocator_t *al;
  1146. int i;
  1147. al = (kmp_allocator_t *)__kmp_allocate(sizeof(kmp_allocator_t)); // zeroed
  1148. al->memspace = ms; // not used currently
  1149. for (i = 0; i < ntraits; ++i) {
  1150. switch (traits[i].key) {
  1151. case omp_atk_sync_hint:
  1152. case omp_atk_access:
  1153. case omp_atk_pinned:
  1154. break;
  1155. case omp_atk_alignment:
  1156. __kmp_type_convert(traits[i].value, &(al->alignment));
  1157. KMP_ASSERT(IS_POWER_OF_TWO(al->alignment));
  1158. break;
  1159. case omp_atk_pool_size:
  1160. al->pool_size = traits[i].value;
  1161. break;
  1162. case omp_atk_fallback:
  1163. al->fb = (omp_alloctrait_value_t)traits[i].value;
  1164. KMP_DEBUG_ASSERT(
  1165. al->fb == omp_atv_default_mem_fb || al->fb == omp_atv_null_fb ||
  1166. al->fb == omp_atv_abort_fb || al->fb == omp_atv_allocator_fb);
  1167. break;
  1168. case omp_atk_fb_data:
  1169. al->fb_data = RCAST(kmp_allocator_t *, traits[i].value);
  1170. break;
  1171. case omp_atk_partition:
  1172. al->memkind = RCAST(void **, traits[i].value);
  1173. break;
  1174. default:
  1175. KMP_ASSERT2(0, "Unexpected allocator trait");
  1176. }
  1177. }
  1178. if (al->fb == 0) {
  1179. // set default allocator
  1180. al->fb = omp_atv_default_mem_fb;
  1181. al->fb_data = (kmp_allocator_t *)omp_default_mem_alloc;
  1182. } else if (al->fb == omp_atv_allocator_fb) {
  1183. KMP_ASSERT(al->fb_data != NULL);
  1184. } else if (al->fb == omp_atv_default_mem_fb) {
  1185. al->fb_data = (kmp_allocator_t *)omp_default_mem_alloc;
  1186. }
  1187. if (__kmp_memkind_available) {
  1188. // Let's use memkind library if available
  1189. if (ms == omp_high_bw_mem_space) {
  1190. if (al->memkind == (void *)omp_atv_interleaved && mk_hbw_interleave) {
  1191. al->memkind = mk_hbw_interleave;
  1192. } else if (mk_hbw_preferred) {
  1193. // AC: do not try to use MEMKIND_HBW for now, because memkind library
  1194. // cannot reliably detect exhaustion of HBW memory.
  1195. // It could be possible using hbw_verify_memory_region() but memkind
  1196. // manual says: "Using this function in production code may result in
  1197. // serious performance penalty".
  1198. al->memkind = mk_hbw_preferred;
  1199. } else {
  1200. // HBW is requested but not available --> return NULL allocator
  1201. __kmp_free(al);
  1202. return omp_null_allocator;
  1203. }
  1204. } else if (ms == omp_large_cap_mem_space) {
  1205. if (mk_dax_kmem_all) {
  1206. // All pmem nodes are visited
  1207. al->memkind = mk_dax_kmem_all;
  1208. } else if (mk_dax_kmem) {
  1209. // Only closest pmem node is visited
  1210. al->memkind = mk_dax_kmem;
  1211. } else {
  1212. __kmp_free(al);
  1213. return omp_null_allocator;
  1214. }
  1215. } else {
  1216. if (al->memkind == (void *)omp_atv_interleaved && mk_interleave) {
  1217. al->memkind = mk_interleave;
  1218. } else {
  1219. al->memkind = mk_default;
  1220. }
  1221. }
  1222. } else if (KMP_IS_TARGET_MEM_SPACE(ms) && !__kmp_target_mem_available) {
  1223. __kmp_free(al);
  1224. return omp_null_allocator;
  1225. } else {
  1226. if (ms == omp_high_bw_mem_space) {
  1227. // cannot detect HBW memory presence without memkind library
  1228. __kmp_free(al);
  1229. return omp_null_allocator;
  1230. }
  1231. }
  1232. return (omp_allocator_handle_t)al;
  1233. }
  1234. void __kmpc_destroy_allocator(int gtid, omp_allocator_handle_t allocator) {
  1235. if (allocator > kmp_max_mem_alloc)
  1236. __kmp_free(allocator);
  1237. }
  1238. void __kmpc_set_default_allocator(int gtid, omp_allocator_handle_t allocator) {
  1239. if (allocator == omp_null_allocator)
  1240. allocator = omp_default_mem_alloc;
  1241. __kmp_threads[gtid]->th.th_def_allocator = allocator;
  1242. }
  1243. omp_allocator_handle_t __kmpc_get_default_allocator(int gtid) {
  1244. return __kmp_threads[gtid]->th.th_def_allocator;
  1245. }
  1246. typedef struct kmp_mem_desc { // Memory block descriptor
  1247. void *ptr_alloc; // Pointer returned by allocator
  1248. size_t size_a; // Size of allocated memory block (initial+descriptor+align)
  1249. size_t size_orig; // Original size requested
  1250. void *ptr_align; // Pointer to aligned memory, returned
  1251. kmp_allocator_t *allocator; // allocator
  1252. } kmp_mem_desc_t;
  1253. static int alignment = sizeof(void *); // align to pointer size by default
  1254. // external interfaces are wrappers over internal implementation
  1255. void *__kmpc_alloc(int gtid, size_t size, omp_allocator_handle_t allocator) {
  1256. KE_TRACE(25, ("__kmpc_alloc: T#%d (%d, %p)\n", gtid, (int)size, allocator));
  1257. void *ptr = __kmp_alloc(gtid, 0, size, allocator);
  1258. KE_TRACE(25, ("__kmpc_alloc returns %p, T#%d\n", ptr, gtid));
  1259. return ptr;
  1260. }
  1261. void *__kmpc_aligned_alloc(int gtid, size_t algn, size_t size,
  1262. omp_allocator_handle_t allocator) {
  1263. KE_TRACE(25, ("__kmpc_aligned_alloc: T#%d (%d, %d, %p)\n", gtid, (int)algn,
  1264. (int)size, allocator));
  1265. void *ptr = __kmp_alloc(gtid, algn, size, allocator);
  1266. KE_TRACE(25, ("__kmpc_aligned_alloc returns %p, T#%d\n", ptr, gtid));
  1267. return ptr;
  1268. }
  1269. void *__kmpc_calloc(int gtid, size_t nmemb, size_t size,
  1270. omp_allocator_handle_t allocator) {
  1271. KE_TRACE(25, ("__kmpc_calloc: T#%d (%d, %d, %p)\n", gtid, (int)nmemb,
  1272. (int)size, allocator));
  1273. void *ptr = __kmp_calloc(gtid, 0, nmemb, size, allocator);
  1274. KE_TRACE(25, ("__kmpc_calloc returns %p, T#%d\n", ptr, gtid));
  1275. return ptr;
  1276. }
  1277. void *__kmpc_realloc(int gtid, void *ptr, size_t size,
  1278. omp_allocator_handle_t allocator,
  1279. omp_allocator_handle_t free_allocator) {
  1280. KE_TRACE(25, ("__kmpc_realloc: T#%d (%p, %d, %p, %p)\n", gtid, ptr, (int)size,
  1281. allocator, free_allocator));
  1282. void *nptr = __kmp_realloc(gtid, ptr, size, allocator, free_allocator);
  1283. KE_TRACE(25, ("__kmpc_realloc returns %p, T#%d\n", nptr, gtid));
  1284. return nptr;
  1285. }
  1286. void __kmpc_free(int gtid, void *ptr, omp_allocator_handle_t allocator) {
  1287. KE_TRACE(25, ("__kmpc_free: T#%d free(%p,%p)\n", gtid, ptr, allocator));
  1288. ___kmpc_free(gtid, ptr, allocator);
  1289. KE_TRACE(10, ("__kmpc_free: T#%d freed %p (%p)\n", gtid, ptr, allocator));
  1290. return;
  1291. }
  1292. // internal implementation, called from inside the library
  1293. void *__kmp_alloc(int gtid, size_t algn, size_t size,
  1294. omp_allocator_handle_t allocator) {
  1295. void *ptr = NULL;
  1296. kmp_allocator_t *al;
  1297. KMP_DEBUG_ASSERT(__kmp_init_serial);
  1298. if (size == 0)
  1299. return NULL;
  1300. if (allocator == omp_null_allocator)
  1301. allocator = __kmp_threads[gtid]->th.th_def_allocator;
  1302. al = RCAST(kmp_allocator_t *, allocator);
  1303. int sz_desc = sizeof(kmp_mem_desc_t);
  1304. kmp_mem_desc_t desc;
  1305. kmp_uintptr_t addr; // address returned by allocator
  1306. kmp_uintptr_t addr_align; // address to return to caller
  1307. kmp_uintptr_t addr_descr; // address of memory block descriptor
  1308. size_t align = alignment; // default alignment
  1309. if (allocator > kmp_max_mem_alloc && al->alignment > align)
  1310. align = al->alignment; // alignment required by allocator trait
  1311. if (align < algn)
  1312. align = algn; // max of allocator trait, parameter and sizeof(void*)
  1313. desc.size_orig = size;
  1314. desc.size_a = size + sz_desc + align;
  1315. if (__kmp_memkind_available) {
  1316. if (allocator < kmp_max_mem_alloc) {
  1317. // pre-defined allocator
  1318. if (allocator == omp_high_bw_mem_alloc && mk_hbw_preferred) {
  1319. ptr = kmp_mk_alloc(*mk_hbw_preferred, desc.size_a);
  1320. } else if (allocator == omp_large_cap_mem_alloc && mk_dax_kmem_all) {
  1321. ptr = kmp_mk_alloc(*mk_dax_kmem_all, desc.size_a);
  1322. } else {
  1323. ptr = kmp_mk_alloc(*mk_default, desc.size_a);
  1324. }
  1325. } else if (al->pool_size > 0) {
  1326. // custom allocator with pool size requested
  1327. kmp_uint64 used =
  1328. KMP_TEST_THEN_ADD64((kmp_int64 *)&al->pool_used, desc.size_a);
  1329. if (used + desc.size_a > al->pool_size) {
  1330. // not enough space, need to go fallback path
  1331. KMP_TEST_THEN_ADD64((kmp_int64 *)&al->pool_used, -desc.size_a);
  1332. if (al->fb == omp_atv_default_mem_fb) {
  1333. al = (kmp_allocator_t *)omp_default_mem_alloc;
  1334. ptr = kmp_mk_alloc(*mk_default, desc.size_a);
  1335. } else if (al->fb == omp_atv_abort_fb) {
  1336. KMP_ASSERT(0); // abort fallback requested
  1337. } else if (al->fb == omp_atv_allocator_fb) {
  1338. KMP_ASSERT(al != al->fb_data);
  1339. al = al->fb_data;
  1340. return __kmp_alloc(gtid, algn, size, (omp_allocator_handle_t)al);
  1341. } // else ptr == NULL;
  1342. } else {
  1343. // pool has enough space
  1344. ptr = kmp_mk_alloc(*al->memkind, desc.size_a);
  1345. if (ptr == NULL) {
  1346. if (al->fb == omp_atv_default_mem_fb) {
  1347. al = (kmp_allocator_t *)omp_default_mem_alloc;
  1348. ptr = kmp_mk_alloc(*mk_default, desc.size_a);
  1349. } else if (al->fb == omp_atv_abort_fb) {
  1350. KMP_ASSERT(0); // abort fallback requested
  1351. } else if (al->fb == omp_atv_allocator_fb) {
  1352. KMP_ASSERT(al != al->fb_data);
  1353. al = al->fb_data;
  1354. return __kmp_alloc(gtid, algn, size, (omp_allocator_handle_t)al);
  1355. }
  1356. }
  1357. }
  1358. } else {
  1359. // custom allocator, pool size not requested
  1360. ptr = kmp_mk_alloc(*al->memkind, desc.size_a);
  1361. if (ptr == NULL) {
  1362. if (al->fb == omp_atv_default_mem_fb) {
  1363. al = (kmp_allocator_t *)omp_default_mem_alloc;
  1364. ptr = kmp_mk_alloc(*mk_default, desc.size_a);
  1365. } else if (al->fb == omp_atv_abort_fb) {
  1366. KMP_ASSERT(0); // abort fallback requested
  1367. } else if (al->fb == omp_atv_allocator_fb) {
  1368. KMP_ASSERT(al != al->fb_data);
  1369. al = al->fb_data;
  1370. return __kmp_alloc(gtid, algn, size, (omp_allocator_handle_t)al);
  1371. }
  1372. }
  1373. }
  1374. } else if (allocator < kmp_max_mem_alloc) {
  1375. if (KMP_IS_TARGET_MEM_ALLOC(allocator)) {
  1376. // Use size input directly as the memory may not be accessible on host.
  1377. // Use default device for now.
  1378. if (__kmp_target_mem_available) {
  1379. kmp_int32 device =
  1380. __kmp_threads[gtid]->th.th_current_task->td_icvs.default_device;
  1381. if (allocator == llvm_omp_target_host_mem_alloc)
  1382. ptr = kmp_target_alloc_host(size, device);
  1383. else if (allocator == llvm_omp_target_shared_mem_alloc)
  1384. ptr = kmp_target_alloc_shared(size, device);
  1385. else // allocator == llvm_omp_target_device_mem_alloc
  1386. ptr = kmp_target_alloc_device(size, device);
  1387. }
  1388. return ptr;
  1389. }
  1390. // pre-defined allocator
  1391. if (allocator == omp_high_bw_mem_alloc) {
  1392. // ptr = NULL;
  1393. } else if (allocator == omp_large_cap_mem_alloc) {
  1394. // warnings?
  1395. } else {
  1396. ptr = __kmp_thread_malloc(__kmp_thread_from_gtid(gtid), desc.size_a);
  1397. }
  1398. } else if (KMP_IS_TARGET_MEM_SPACE(al->memspace)) {
  1399. if (__kmp_target_mem_available) {
  1400. kmp_int32 device =
  1401. __kmp_threads[gtid]->th.th_current_task->td_icvs.default_device;
  1402. if (al->memspace == llvm_omp_target_host_mem_space)
  1403. ptr = kmp_target_alloc_host(size, device);
  1404. else if (al->memspace == llvm_omp_target_shared_mem_space)
  1405. ptr = kmp_target_alloc_shared(size, device);
  1406. else // al->memspace == llvm_omp_target_device_mem_space
  1407. ptr = kmp_target_alloc_device(size, device);
  1408. }
  1409. return ptr;
  1410. } else if (al->pool_size > 0) {
  1411. // custom allocator with pool size requested
  1412. kmp_uint64 used =
  1413. KMP_TEST_THEN_ADD64((kmp_int64 *)&al->pool_used, desc.size_a);
  1414. if (used + desc.size_a > al->pool_size) {
  1415. // not enough space, need to go fallback path
  1416. KMP_TEST_THEN_ADD64((kmp_int64 *)&al->pool_used, -desc.size_a);
  1417. if (al->fb == omp_atv_default_mem_fb) {
  1418. al = (kmp_allocator_t *)omp_default_mem_alloc;
  1419. ptr = __kmp_thread_malloc(__kmp_thread_from_gtid(gtid), desc.size_a);
  1420. } else if (al->fb == omp_atv_abort_fb) {
  1421. KMP_ASSERT(0); // abort fallback requested
  1422. } else if (al->fb == omp_atv_allocator_fb) {
  1423. KMP_ASSERT(al != al->fb_data);
  1424. al = al->fb_data;
  1425. return __kmp_alloc(gtid, algn, size, (omp_allocator_handle_t)al);
  1426. } // else ptr == NULL;
  1427. } else {
  1428. // pool has enough space
  1429. ptr = __kmp_thread_malloc(__kmp_thread_from_gtid(gtid), desc.size_a);
  1430. if (ptr == NULL && al->fb == omp_atv_abort_fb) {
  1431. KMP_ASSERT(0); // abort fallback requested
  1432. } // no sense to look for another fallback because of same internal alloc
  1433. }
  1434. } else {
  1435. // custom allocator, pool size not requested
  1436. ptr = __kmp_thread_malloc(__kmp_thread_from_gtid(gtid), desc.size_a);
  1437. if (ptr == NULL && al->fb == omp_atv_abort_fb) {
  1438. KMP_ASSERT(0); // abort fallback requested
  1439. } // no sense to look for another fallback because of same internal alloc
  1440. }
  1441. KE_TRACE(10, ("__kmp_alloc: T#%d %p=alloc(%d)\n", gtid, ptr, desc.size_a));
  1442. if (ptr == NULL)
  1443. return NULL;
  1444. addr = (kmp_uintptr_t)ptr;
  1445. addr_align = (addr + sz_desc + align - 1) & ~(align - 1);
  1446. addr_descr = addr_align - sz_desc;
  1447. desc.ptr_alloc = ptr;
  1448. desc.ptr_align = (void *)addr_align;
  1449. desc.allocator = al;
  1450. *((kmp_mem_desc_t *)addr_descr) = desc; // save descriptor contents
  1451. KMP_MB();
  1452. return desc.ptr_align;
  1453. }
  1454. void *__kmp_calloc(int gtid, size_t algn, size_t nmemb, size_t size,
  1455. omp_allocator_handle_t allocator) {
  1456. void *ptr = NULL;
  1457. kmp_allocator_t *al;
  1458. KMP_DEBUG_ASSERT(__kmp_init_serial);
  1459. if (allocator == omp_null_allocator)
  1460. allocator = __kmp_threads[gtid]->th.th_def_allocator;
  1461. al = RCAST(kmp_allocator_t *, allocator);
  1462. if (nmemb == 0 || size == 0)
  1463. return ptr;
  1464. if ((SIZE_MAX - sizeof(kmp_mem_desc_t)) / size < nmemb) {
  1465. if (al->fb == omp_atv_abort_fb) {
  1466. KMP_ASSERT(0);
  1467. }
  1468. return ptr;
  1469. }
  1470. ptr = __kmp_alloc(gtid, algn, nmemb * size, allocator);
  1471. if (ptr) {
  1472. memset(ptr, 0x00, nmemb * size);
  1473. }
  1474. return ptr;
  1475. }
  1476. void *__kmp_realloc(int gtid, void *ptr, size_t size,
  1477. omp_allocator_handle_t allocator,
  1478. omp_allocator_handle_t free_allocator) {
  1479. void *nptr = NULL;
  1480. KMP_DEBUG_ASSERT(__kmp_init_serial);
  1481. if (size == 0) {
  1482. if (ptr != NULL)
  1483. ___kmpc_free(gtid, ptr, free_allocator);
  1484. return nptr;
  1485. }
  1486. nptr = __kmp_alloc(gtid, 0, size, allocator);
  1487. if (nptr != NULL && ptr != NULL) {
  1488. kmp_mem_desc_t desc;
  1489. kmp_uintptr_t addr_align; // address to return to caller
  1490. kmp_uintptr_t addr_descr; // address of memory block descriptor
  1491. addr_align = (kmp_uintptr_t)ptr;
  1492. addr_descr = addr_align - sizeof(kmp_mem_desc_t);
  1493. desc = *((kmp_mem_desc_t *)addr_descr); // read descriptor
  1494. KMP_DEBUG_ASSERT(desc.ptr_align == ptr);
  1495. KMP_DEBUG_ASSERT(desc.size_orig > 0);
  1496. KMP_DEBUG_ASSERT(desc.size_orig < desc.size_a);
  1497. KMP_MEMCPY((char *)nptr, (char *)ptr,
  1498. (size_t)((size < desc.size_orig) ? size : desc.size_orig));
  1499. }
  1500. if (nptr != NULL) {
  1501. ___kmpc_free(gtid, ptr, free_allocator);
  1502. }
  1503. return nptr;
  1504. }
  1505. void ___kmpc_free(int gtid, void *ptr, omp_allocator_handle_t allocator) {
  1506. if (ptr == NULL)
  1507. return;
  1508. kmp_allocator_t *al;
  1509. omp_allocator_handle_t oal;
  1510. al = RCAST(kmp_allocator_t *, CCAST(omp_allocator_handle_t, allocator));
  1511. kmp_mem_desc_t desc;
  1512. kmp_uintptr_t addr_align; // address to return to caller
  1513. kmp_uintptr_t addr_descr; // address of memory block descriptor
  1514. if (KMP_IS_TARGET_MEM_ALLOC(allocator) ||
  1515. (allocator > kmp_max_mem_alloc &&
  1516. KMP_IS_TARGET_MEM_SPACE(al->memspace))) {
  1517. KMP_DEBUG_ASSERT(kmp_target_free);
  1518. kmp_int32 device =
  1519. __kmp_threads[gtid]->th.th_current_task->td_icvs.default_device;
  1520. kmp_target_free(ptr, device);
  1521. return;
  1522. }
  1523. addr_align = (kmp_uintptr_t)ptr;
  1524. addr_descr = addr_align - sizeof(kmp_mem_desc_t);
  1525. desc = *((kmp_mem_desc_t *)addr_descr); // read descriptor
  1526. KMP_DEBUG_ASSERT(desc.ptr_align == ptr);
  1527. if (allocator) {
  1528. KMP_DEBUG_ASSERT(desc.allocator == al || desc.allocator == al->fb_data);
  1529. }
  1530. al = desc.allocator;
  1531. oal = (omp_allocator_handle_t)al; // cast to void* for comparisons
  1532. KMP_DEBUG_ASSERT(al);
  1533. if (__kmp_memkind_available) {
  1534. if (oal < kmp_max_mem_alloc) {
  1535. // pre-defined allocator
  1536. if (oal == omp_high_bw_mem_alloc && mk_hbw_preferred) {
  1537. kmp_mk_free(*mk_hbw_preferred, desc.ptr_alloc);
  1538. } else if (oal == omp_large_cap_mem_alloc && mk_dax_kmem_all) {
  1539. kmp_mk_free(*mk_dax_kmem_all, desc.ptr_alloc);
  1540. } else {
  1541. kmp_mk_free(*mk_default, desc.ptr_alloc);
  1542. }
  1543. } else {
  1544. if (al->pool_size > 0) { // custom allocator with pool size requested
  1545. kmp_uint64 used =
  1546. KMP_TEST_THEN_ADD64((kmp_int64 *)&al->pool_used, -desc.size_a);
  1547. (void)used; // to suppress compiler warning
  1548. KMP_DEBUG_ASSERT(used >= desc.size_a);
  1549. }
  1550. kmp_mk_free(*al->memkind, desc.ptr_alloc);
  1551. }
  1552. } else {
  1553. if (oal > kmp_max_mem_alloc && al->pool_size > 0) {
  1554. kmp_uint64 used =
  1555. KMP_TEST_THEN_ADD64((kmp_int64 *)&al->pool_used, -desc.size_a);
  1556. (void)used; // to suppress compiler warning
  1557. KMP_DEBUG_ASSERT(used >= desc.size_a);
  1558. }
  1559. __kmp_thread_free(__kmp_thread_from_gtid(gtid), desc.ptr_alloc);
  1560. }
  1561. }
  1562. /* If LEAK_MEMORY is defined, __kmp_free() will *not* free memory. It causes
  1563. memory leaks, but it may be useful for debugging memory corruptions, used
  1564. freed pointers, etc. */
  1565. /* #define LEAK_MEMORY */
  1566. struct kmp_mem_descr { // Memory block descriptor.
  1567. void *ptr_allocated; // Pointer returned by malloc(), subject for free().
  1568. size_t size_allocated; // Size of allocated memory block.
  1569. void *ptr_aligned; // Pointer to aligned memory, to be used by client code.
  1570. size_t size_aligned; // Size of aligned memory block.
  1571. };
  1572. typedef struct kmp_mem_descr kmp_mem_descr_t;
  1573. /* Allocate memory on requested boundary, fill allocated memory with 0x00.
  1574. NULL is NEVER returned, __kmp_abort() is called in case of memory allocation
  1575. error. Must use __kmp_free when freeing memory allocated by this routine! */
  1576. static void *___kmp_allocate_align(size_t size,
  1577. size_t alignment KMP_SRC_LOC_DECL) {
  1578. /* __kmp_allocate() allocates (by call to malloc()) bigger memory block than
  1579. requested to return properly aligned pointer. Original pointer returned
  1580. by malloc() and size of allocated block is saved in descriptor just
  1581. before the aligned pointer. This information used by __kmp_free() -- it
  1582. has to pass to free() original pointer, not aligned one.
  1583. +---------+------------+-----------------------------------+---------+
  1584. | padding | descriptor | aligned block | padding |
  1585. +---------+------------+-----------------------------------+---------+
  1586. ^ ^
  1587. | |
  1588. | +- Aligned pointer returned to caller
  1589. +- Pointer returned by malloc()
  1590. Aligned block is filled with zeros, paddings are filled with 0xEF. */
  1591. kmp_mem_descr_t descr;
  1592. kmp_uintptr_t addr_allocated; // Address returned by malloc().
  1593. kmp_uintptr_t addr_aligned; // Aligned address to return to caller.
  1594. kmp_uintptr_t addr_descr; // Address of memory block descriptor.
  1595. KE_TRACE(25, ("-> ___kmp_allocate_align( %d, %d ) called from %s:%d\n",
  1596. (int)size, (int)alignment KMP_SRC_LOC_PARM));
  1597. KMP_DEBUG_ASSERT(alignment < 32 * 1024); // Alignment should not be too
  1598. KMP_DEBUG_ASSERT(sizeof(void *) <= sizeof(kmp_uintptr_t));
  1599. // Make sure kmp_uintptr_t is enough to store addresses.
  1600. descr.size_aligned = size;
  1601. descr.size_allocated =
  1602. descr.size_aligned + sizeof(kmp_mem_descr_t) + alignment;
  1603. #if KMP_DEBUG
  1604. descr.ptr_allocated = _malloc_src_loc(descr.size_allocated, _file_, _line_);
  1605. #else
  1606. descr.ptr_allocated = malloc_src_loc(descr.size_allocated KMP_SRC_LOC_PARM);
  1607. #endif
  1608. __lsan_ignore_object(descr.ptr_allocated); // espetrov@yandex-team.ru: asan considers descr.ptr_allocated leaked because of address alignment arithmetics
  1609. KE_TRACE(10, (" malloc( %d ) returned %p\n", (int)descr.size_allocated,
  1610. descr.ptr_allocated));
  1611. if (descr.ptr_allocated == NULL) {
  1612. KMP_FATAL(OutOfHeapMemory);
  1613. }
  1614. addr_allocated = (kmp_uintptr_t)descr.ptr_allocated;
  1615. addr_aligned =
  1616. (addr_allocated + sizeof(kmp_mem_descr_t) + alignment) & ~(alignment - 1);
  1617. addr_descr = addr_aligned - sizeof(kmp_mem_descr_t);
  1618. descr.ptr_aligned = (void *)addr_aligned;
  1619. KE_TRACE(26, (" ___kmp_allocate_align: "
  1620. "ptr_allocated=%p, size_allocated=%d, "
  1621. "ptr_aligned=%p, size_aligned=%d\n",
  1622. descr.ptr_allocated, (int)descr.size_allocated,
  1623. descr.ptr_aligned, (int)descr.size_aligned));
  1624. KMP_DEBUG_ASSERT(addr_allocated <= addr_descr);
  1625. KMP_DEBUG_ASSERT(addr_descr + sizeof(kmp_mem_descr_t) == addr_aligned);
  1626. KMP_DEBUG_ASSERT(addr_aligned + descr.size_aligned <=
  1627. addr_allocated + descr.size_allocated);
  1628. KMP_DEBUG_ASSERT(addr_aligned % alignment == 0);
  1629. #ifdef KMP_DEBUG
  1630. memset(descr.ptr_allocated, 0xEF, descr.size_allocated);
  1631. // Fill allocated memory block with 0xEF.
  1632. #endif
  1633. memset(descr.ptr_aligned, 0x00, descr.size_aligned);
  1634. // Fill the aligned memory block (which is intended for using by caller) with
  1635. // 0x00. Do not
  1636. // put this filling under KMP_DEBUG condition! Many callers expect zeroed
  1637. // memory. (Padding
  1638. // bytes remain filled with 0xEF in debugging library.)
  1639. *((kmp_mem_descr_t *)addr_descr) = descr;
  1640. KMP_MB();
  1641. KE_TRACE(25, ("<- ___kmp_allocate_align() returns %p\n", descr.ptr_aligned));
  1642. return descr.ptr_aligned;
  1643. } // func ___kmp_allocate_align
  1644. /* Allocate memory on cache line boundary, fill allocated memory with 0x00.
  1645. Do not call this func directly! Use __kmp_allocate macro instead.
  1646. NULL is NEVER returned, __kmp_abort() is called in case of memory allocation
  1647. error. Must use __kmp_free when freeing memory allocated by this routine! */
  1648. void *___kmp_allocate(size_t size KMP_SRC_LOC_DECL) {
  1649. void *ptr;
  1650. KE_TRACE(25, ("-> __kmp_allocate( %d ) called from %s:%d\n",
  1651. (int)size KMP_SRC_LOC_PARM));
  1652. ptr = ___kmp_allocate_align(size, __kmp_align_alloc KMP_SRC_LOC_PARM);
  1653. KE_TRACE(25, ("<- __kmp_allocate() returns %p\n", ptr));
  1654. return ptr;
  1655. } // func ___kmp_allocate
  1656. /* Allocate memory on page boundary, fill allocated memory with 0x00.
  1657. Does not call this func directly! Use __kmp_page_allocate macro instead.
  1658. NULL is NEVER returned, __kmp_abort() is called in case of memory allocation
  1659. error. Must use __kmp_free when freeing memory allocated by this routine! */
  1660. void *___kmp_page_allocate(size_t size KMP_SRC_LOC_DECL) {
  1661. int page_size = 8 * 1024;
  1662. void *ptr;
  1663. KE_TRACE(25, ("-> __kmp_page_allocate( %d ) called from %s:%d\n",
  1664. (int)size KMP_SRC_LOC_PARM));
  1665. ptr = ___kmp_allocate_align(size, page_size KMP_SRC_LOC_PARM);
  1666. KE_TRACE(25, ("<- __kmp_page_allocate( %d ) returns %p\n", (int)size, ptr));
  1667. return ptr;
  1668. } // ___kmp_page_allocate
  1669. /* Free memory allocated by __kmp_allocate() and __kmp_page_allocate().
  1670. In debug mode, fill the memory block with 0xEF before call to free(). */
  1671. void ___kmp_free(void *ptr KMP_SRC_LOC_DECL) {
  1672. kmp_mem_descr_t descr;
  1673. #if KMP_DEBUG
  1674. kmp_uintptr_t addr_allocated; // Address returned by malloc().
  1675. kmp_uintptr_t addr_aligned; // Aligned address passed by caller.
  1676. #endif
  1677. KE_TRACE(25,
  1678. ("-> __kmp_free( %p ) called from %s:%d\n", ptr KMP_SRC_LOC_PARM));
  1679. KMP_ASSERT(ptr != NULL);
  1680. descr = *(kmp_mem_descr_t *)((kmp_uintptr_t)ptr - sizeof(kmp_mem_descr_t));
  1681. KE_TRACE(26, (" __kmp_free: "
  1682. "ptr_allocated=%p, size_allocated=%d, "
  1683. "ptr_aligned=%p, size_aligned=%d\n",
  1684. descr.ptr_allocated, (int)descr.size_allocated,
  1685. descr.ptr_aligned, (int)descr.size_aligned));
  1686. #if KMP_DEBUG
  1687. addr_allocated = (kmp_uintptr_t)descr.ptr_allocated;
  1688. addr_aligned = (kmp_uintptr_t)descr.ptr_aligned;
  1689. KMP_DEBUG_ASSERT(addr_aligned % CACHE_LINE == 0);
  1690. KMP_DEBUG_ASSERT(descr.ptr_aligned == ptr);
  1691. KMP_DEBUG_ASSERT(addr_allocated + sizeof(kmp_mem_descr_t) <= addr_aligned);
  1692. KMP_DEBUG_ASSERT(descr.size_aligned < descr.size_allocated);
  1693. KMP_DEBUG_ASSERT(addr_aligned + descr.size_aligned <=
  1694. addr_allocated + descr.size_allocated);
  1695. memset(descr.ptr_allocated, 0xEF, descr.size_allocated);
  1696. // Fill memory block with 0xEF, it helps catch using freed memory.
  1697. #endif
  1698. #ifndef LEAK_MEMORY
  1699. KE_TRACE(10, (" free( %p )\n", descr.ptr_allocated));
  1700. #ifdef KMP_DEBUG
  1701. _free_src_loc(descr.ptr_allocated, _file_, _line_);
  1702. #else
  1703. free_src_loc(descr.ptr_allocated KMP_SRC_LOC_PARM);
  1704. #endif
  1705. #endif
  1706. KMP_MB();
  1707. KE_TRACE(25, ("<- __kmp_free() returns\n"));
  1708. } // func ___kmp_free
  1709. #if USE_FAST_MEMORY == 3
  1710. // Allocate fast memory by first scanning the thread's free lists
  1711. // If a chunk the right size exists, grab it off the free list.
  1712. // Otherwise allocate normally using kmp_thread_malloc.
  1713. // AC: How to choose the limit? Just get 16 for now...
  1714. #define KMP_FREE_LIST_LIMIT 16
  1715. // Always use 128 bytes for determining buckets for caching memory blocks
  1716. #define DCACHE_LINE 128
  1717. void *___kmp_fast_allocate(kmp_info_t *this_thr, size_t size KMP_SRC_LOC_DECL) {
  1718. void *ptr;
  1719. size_t num_lines, idx;
  1720. int index;
  1721. void *alloc_ptr;
  1722. size_t alloc_size;
  1723. kmp_mem_descr_t *descr;
  1724. KE_TRACE(25, ("-> __kmp_fast_allocate( T#%d, %d ) called from %s:%d\n",
  1725. __kmp_gtid_from_thread(this_thr), (int)size KMP_SRC_LOC_PARM));
  1726. num_lines = (size + DCACHE_LINE - 1) / DCACHE_LINE;
  1727. idx = num_lines - 1;
  1728. KMP_DEBUG_ASSERT(idx >= 0);
  1729. if (idx < 2) {
  1730. index = 0; // idx is [ 0, 1 ], use first free list
  1731. num_lines = 2; // 1, 2 cache lines or less than cache line
  1732. } else if ((idx >>= 2) == 0) {
  1733. index = 1; // idx is [ 2, 3 ], use second free list
  1734. num_lines = 4; // 3, 4 cache lines
  1735. } else if ((idx >>= 2) == 0) {
  1736. index = 2; // idx is [ 4, 15 ], use third free list
  1737. num_lines = 16; // 5, 6, ..., 16 cache lines
  1738. } else if ((idx >>= 2) == 0) {
  1739. index = 3; // idx is [ 16, 63 ], use fourth free list
  1740. num_lines = 64; // 17, 18, ..., 64 cache lines
  1741. } else {
  1742. goto alloc_call; // 65 or more cache lines ( > 8KB ), don't use free lists
  1743. }
  1744. ptr = this_thr->th.th_free_lists[index].th_free_list_self;
  1745. if (ptr != NULL) {
  1746. // pop the head of no-sync free list
  1747. this_thr->th.th_free_lists[index].th_free_list_self = *((void **)ptr);
  1748. KMP_DEBUG_ASSERT(this_thr == ((kmp_mem_descr_t *)((kmp_uintptr_t)ptr -
  1749. sizeof(kmp_mem_descr_t)))
  1750. ->ptr_aligned);
  1751. goto end;
  1752. }
  1753. ptr = TCR_SYNC_PTR(this_thr->th.th_free_lists[index].th_free_list_sync);
  1754. if (ptr != NULL) {
  1755. // no-sync free list is empty, use sync free list (filled in by other
  1756. // threads only)
  1757. // pop the head of the sync free list, push NULL instead
  1758. while (!KMP_COMPARE_AND_STORE_PTR(
  1759. &this_thr->th.th_free_lists[index].th_free_list_sync, ptr, nullptr)) {
  1760. KMP_CPU_PAUSE();
  1761. ptr = TCR_SYNC_PTR(this_thr->th.th_free_lists[index].th_free_list_sync);
  1762. }
  1763. // push the rest of chain into no-sync free list (can be NULL if there was
  1764. // the only block)
  1765. this_thr->th.th_free_lists[index].th_free_list_self = *((void **)ptr);
  1766. KMP_DEBUG_ASSERT(this_thr == ((kmp_mem_descr_t *)((kmp_uintptr_t)ptr -
  1767. sizeof(kmp_mem_descr_t)))
  1768. ->ptr_aligned);
  1769. goto end;
  1770. }
  1771. alloc_call:
  1772. // haven't found block in the free lists, thus allocate it
  1773. size = num_lines * DCACHE_LINE;
  1774. alloc_size = size + sizeof(kmp_mem_descr_t) + DCACHE_LINE;
  1775. KE_TRACE(25, ("__kmp_fast_allocate: T#%d Calling __kmp_thread_malloc with "
  1776. "alloc_size %d\n",
  1777. __kmp_gtid_from_thread(this_thr), alloc_size));
  1778. alloc_ptr = bget(this_thr, (bufsize)alloc_size);
  1779. // align ptr to DCACHE_LINE
  1780. ptr = (void *)((((kmp_uintptr_t)alloc_ptr) + sizeof(kmp_mem_descr_t) +
  1781. DCACHE_LINE) &
  1782. ~(DCACHE_LINE - 1));
  1783. descr = (kmp_mem_descr_t *)(((kmp_uintptr_t)ptr) - sizeof(kmp_mem_descr_t));
  1784. descr->ptr_allocated = alloc_ptr; // remember allocated pointer
  1785. // we don't need size_allocated
  1786. descr->ptr_aligned = (void *)this_thr; // remember allocating thread
  1787. // (it is already saved in bget buffer,
  1788. // but we may want to use another allocator in future)
  1789. descr->size_aligned = size;
  1790. end:
  1791. KE_TRACE(25, ("<- __kmp_fast_allocate( T#%d ) returns %p\n",
  1792. __kmp_gtid_from_thread(this_thr), ptr));
  1793. return ptr;
  1794. } // func __kmp_fast_allocate
  1795. // Free fast memory and place it on the thread's free list if it is of
  1796. // the correct size.
  1797. void ___kmp_fast_free(kmp_info_t *this_thr, void *ptr KMP_SRC_LOC_DECL) {
  1798. kmp_mem_descr_t *descr;
  1799. kmp_info_t *alloc_thr;
  1800. size_t size;
  1801. size_t idx;
  1802. int index;
  1803. KE_TRACE(25, ("-> __kmp_fast_free( T#%d, %p ) called from %s:%d\n",
  1804. __kmp_gtid_from_thread(this_thr), ptr KMP_SRC_LOC_PARM));
  1805. KMP_ASSERT(ptr != NULL);
  1806. descr = (kmp_mem_descr_t *)(((kmp_uintptr_t)ptr) - sizeof(kmp_mem_descr_t));
  1807. KE_TRACE(26, (" __kmp_fast_free: size_aligned=%d\n",
  1808. (int)descr->size_aligned));
  1809. size = descr->size_aligned; // 2, 4, 16, 64, 65, 66, ... cache lines
  1810. idx = DCACHE_LINE * 2; // 2 cache lines is minimal size of block
  1811. if (idx == size) {
  1812. index = 0; // 2 cache lines
  1813. } else if ((idx <<= 1) == size) {
  1814. index = 1; // 4 cache lines
  1815. } else if ((idx <<= 2) == size) {
  1816. index = 2; // 16 cache lines
  1817. } else if ((idx <<= 2) == size) {
  1818. index = 3; // 64 cache lines
  1819. } else {
  1820. KMP_DEBUG_ASSERT(size > DCACHE_LINE * 64);
  1821. goto free_call; // 65 or more cache lines ( > 8KB )
  1822. }
  1823. alloc_thr = (kmp_info_t *)descr->ptr_aligned; // get thread owning the block
  1824. if (alloc_thr == this_thr) {
  1825. // push block to self no-sync free list, linking previous head (LIFO)
  1826. *((void **)ptr) = this_thr->th.th_free_lists[index].th_free_list_self;
  1827. this_thr->th.th_free_lists[index].th_free_list_self = ptr;
  1828. } else {
  1829. void *head = this_thr->th.th_free_lists[index].th_free_list_other;
  1830. if (head == NULL) {
  1831. // Create new free list
  1832. this_thr->th.th_free_lists[index].th_free_list_other = ptr;
  1833. *((void **)ptr) = NULL; // mark the tail of the list
  1834. descr->size_allocated = (size_t)1; // head of the list keeps its length
  1835. } else {
  1836. // need to check existed "other" list's owner thread and size of queue
  1837. kmp_mem_descr_t *dsc =
  1838. (kmp_mem_descr_t *)((char *)head - sizeof(kmp_mem_descr_t));
  1839. // allocating thread, same for all queue nodes
  1840. kmp_info_t *q_th = (kmp_info_t *)(dsc->ptr_aligned);
  1841. size_t q_sz =
  1842. dsc->size_allocated + 1; // new size in case we add current task
  1843. if (q_th == alloc_thr && q_sz <= KMP_FREE_LIST_LIMIT) {
  1844. // we can add current task to "other" list, no sync needed
  1845. *((void **)ptr) = head;
  1846. descr->size_allocated = q_sz;
  1847. this_thr->th.th_free_lists[index].th_free_list_other = ptr;
  1848. } else {
  1849. // either queue blocks owner is changing or size limit exceeded
  1850. // return old queue to allocating thread (q_th) synchronously,
  1851. // and start new list for alloc_thr's tasks
  1852. void *old_ptr;
  1853. void *tail = head;
  1854. void *next = *((void **)head);
  1855. while (next != NULL) {
  1856. KMP_DEBUG_ASSERT(
  1857. // queue size should decrease by 1 each step through the list
  1858. ((kmp_mem_descr_t *)((char *)next - sizeof(kmp_mem_descr_t)))
  1859. ->size_allocated +
  1860. 1 ==
  1861. ((kmp_mem_descr_t *)((char *)tail - sizeof(kmp_mem_descr_t)))
  1862. ->size_allocated);
  1863. tail = next; // remember tail node
  1864. next = *((void **)next);
  1865. }
  1866. KMP_DEBUG_ASSERT(q_th != NULL);
  1867. // push block to owner's sync free list
  1868. old_ptr = TCR_PTR(q_th->th.th_free_lists[index].th_free_list_sync);
  1869. /* the next pointer must be set before setting free_list to ptr to avoid
  1870. exposing a broken list to other threads, even for an instant. */
  1871. *((void **)tail) = old_ptr;
  1872. while (!KMP_COMPARE_AND_STORE_PTR(
  1873. &q_th->th.th_free_lists[index].th_free_list_sync, old_ptr, head)) {
  1874. KMP_CPU_PAUSE();
  1875. old_ptr = TCR_PTR(q_th->th.th_free_lists[index].th_free_list_sync);
  1876. *((void **)tail) = old_ptr;
  1877. }
  1878. // start new list of not-selt tasks
  1879. this_thr->th.th_free_lists[index].th_free_list_other = ptr;
  1880. *((void **)ptr) = NULL;
  1881. descr->size_allocated = (size_t)1; // head of queue keeps its length
  1882. }
  1883. }
  1884. }
  1885. goto end;
  1886. free_call:
  1887. KE_TRACE(25, ("__kmp_fast_free: T#%d Calling __kmp_thread_free for size %d\n",
  1888. __kmp_gtid_from_thread(this_thr), size));
  1889. __kmp_bget_dequeue(this_thr); /* Release any queued buffers */
  1890. brel(this_thr, descr->ptr_allocated);
  1891. end:
  1892. KE_TRACE(25, ("<- __kmp_fast_free() returns\n"));
  1893. } // func __kmp_fast_free
  1894. // Initialize the thread free lists related to fast memory
  1895. // Only do this when a thread is initially created.
  1896. void __kmp_initialize_fast_memory(kmp_info_t *this_thr) {
  1897. KE_TRACE(10, ("__kmp_initialize_fast_memory: Called from th %p\n", this_thr));
  1898. memset(this_thr->th.th_free_lists, 0, NUM_LISTS * sizeof(kmp_free_list_t));
  1899. }
  1900. // Free the memory in the thread free lists related to fast memory
  1901. // Only do this when a thread is being reaped (destroyed).
  1902. void __kmp_free_fast_memory(kmp_info_t *th) {
  1903. // Suppose we use BGET underlying allocator, walk through its structures...
  1904. int bin;
  1905. thr_data_t *thr = get_thr_data(th);
  1906. void **lst = NULL;
  1907. KE_TRACE(
  1908. 5, ("__kmp_free_fast_memory: Called T#%d\n", __kmp_gtid_from_thread(th)));
  1909. __kmp_bget_dequeue(th); // Release any queued buffers
  1910. // Dig through free lists and extract all allocated blocks
  1911. for (bin = 0; bin < MAX_BGET_BINS; ++bin) {
  1912. bfhead_t *b = thr->freelist[bin].ql.flink;
  1913. while (b != &thr->freelist[bin]) {
  1914. if ((kmp_uintptr_t)b->bh.bb.bthr & 1) { // the buffer is allocated address
  1915. *((void **)b) =
  1916. lst; // link the list (override bthr, but keep flink yet)
  1917. lst = (void **)b; // push b into lst
  1918. }
  1919. b = b->ql.flink; // get next buffer
  1920. }
  1921. }
  1922. while (lst != NULL) {
  1923. void *next = *lst;
  1924. KE_TRACE(10, ("__kmp_free_fast_memory: freeing %p, next=%p th %p (%d)\n",
  1925. lst, next, th, __kmp_gtid_from_thread(th)));
  1926. (*thr->relfcn)(lst);
  1927. #if BufStats
  1928. // count blocks to prevent problems in __kmp_finalize_bget()
  1929. thr->numprel++; /* Nr of expansion block releases */
  1930. thr->numpblk--; /* Total number of blocks */
  1931. #endif
  1932. lst = (void **)next;
  1933. }
  1934. KE_TRACE(
  1935. 5, ("__kmp_free_fast_memory: Freed T#%d\n", __kmp_gtid_from_thread(th)));
  1936. }
  1937. #endif // USE_FAST_MEMORY