15 #include "kmp_wrapper_malloc.h" 22 typedef int (*bget_compact_t)(size_t, int);
23 typedef void *(*bget_acquire_t)(size_t);
24 typedef void (*bget_release_t)(
void *);
29 #if KMP_ARCH_X86 || KMP_ARCH_ARM 30 typedef kmp_int32 bufsize;
32 typedef kmp_int64 bufsize;
35 typedef ssize_t bufsize;
36 #endif // KMP_OS_WINDOWS 40 typedef enum bget_mode {
46 static void bpool(kmp_info_t *th,
void *buffer, bufsize len);
47 static void *bget(kmp_info_t *th, bufsize size);
48 static void *bgetz(kmp_info_t *th, bufsize size);
49 static void *bgetr(kmp_info_t *th,
void *buffer, bufsize newsize);
50 static void brel(kmp_info_t *th,
void *buf);
51 static void bectl(kmp_info_t *th, bget_compact_t compact,
52 bget_acquire_t acquire, bget_release_t release,
62 #if KMP_ARCH_X86 || !KMP_HAVE_QUAD 65 #define AlignType double 70 #define AlignType _Quad 106 static bufsize bget_bin_size[] = {
116 1 << 16, 1 << 17, 1 << 18, 1 << 19, 1 << 20,
124 #define MAX_BGET_BINS (int)(sizeof(bget_bin_size) / sizeof(bufsize)) 131 typedef struct qlinks {
132 struct bfhead *flink;
133 struct bfhead *blink;
137 typedef struct bhead2 {
145 typedef union bhead {
148 char b_pad[
sizeof(bhead2_t) + (SizeQuant - (
sizeof(bhead2_t) % SizeQuant))];
151 #define BH(p) ((bhead_t *)(p)) 154 typedef struct bdhead {
158 #define BDH(p) ((bdhead_t *)(p)) 161 typedef struct bfhead {
165 #define BFH(p) ((bfhead_t *)(p)) 167 typedef struct thr_data {
168 bfhead_t freelist[MAX_BGET_BINS];
173 long numpget, numprel;
174 long numdget, numdrel;
178 bget_compact_t compfcn;
179 bget_acquire_t acqfcn;
180 bget_release_t relfcn;
193 #define QLSize (sizeof(qlinks_t)) 194 #define SizeQ ((SizeQuant > QLSize) ? SizeQuant : QLSize) 197 ~(((bufsize)(1) << (sizeof(bufsize) * CHAR_BIT - 1)) | (SizeQuant - 1))) 205 ((bufsize)(-(((((bufsize)1) << ((int)sizeof(bufsize) * 8 - 2)) - 1) * 2) - 2)) 208 static int bget_get_bin(bufsize size) {
210 int lo = 0, hi = MAX_BGET_BINS - 1;
212 KMP_DEBUG_ASSERT(size > 0);
214 while ((hi - lo) > 1) {
215 int mid = (lo + hi) >> 1;
216 if (size < bget_bin_size[mid])
222 KMP_DEBUG_ASSERT((lo >= 0) && (lo < MAX_BGET_BINS));
227 static void set_thr_data(kmp_info_t *th) {
231 data = (thr_data_t *)((!th->th.th_local.bget_data)
232 ? __kmp_allocate(
sizeof(*data))
233 : th->th.th_local.bget_data);
235 memset(data,
'\0',
sizeof(*data));
237 for (i = 0; i < MAX_BGET_BINS; ++i) {
238 data->freelist[i].ql.flink = &data->freelist[i];
239 data->freelist[i].ql.blink = &data->freelist[i];
242 th->th.th_local.bget_data = data;
243 th->th.th_local.bget_list = 0;
244 #if !USE_CMP_XCHG_FOR_BGET 245 #ifdef USE_QUEUING_LOCK_FOR_BGET 246 __kmp_init_lock(&th->th.th_local.bget_lock);
248 __kmp_init_bootstrap_lock(&th->th.th_local.bget_lock);
253 static thr_data_t *get_thr_data(kmp_info_t *th) {
256 data = (thr_data_t *)th->th.th_local.bget_data;
258 KMP_DEBUG_ASSERT(data != 0);
264 static void __kmp_bget_dequeue(kmp_info_t *th) {
265 void *p = TCR_SYNC_PTR(th->th.th_local.bget_list);
268 #if USE_CMP_XCHG_FOR_BGET 270 volatile void *old_value = TCR_SYNC_PTR(th->th.th_local.bget_list);
271 while (!KMP_COMPARE_AND_STORE_PTR(&th->th.th_local.bget_list,
272 CCAST(
void *, old_value),
nullptr)) {
274 old_value = TCR_SYNC_PTR(th->th.th_local.bget_list);
276 p = CCAST(
void *, old_value);
279 #ifdef USE_QUEUING_LOCK_FOR_BGET 280 __kmp_acquire_lock(&th->th.th_local.bget_lock, __kmp_gtid_from_thread(th));
282 __kmp_acquire_bootstrap_lock(&th->th.th_local.bget_lock);
285 p = (
void *)th->th.th_local.bget_list;
286 th->th.th_local.bget_list = 0;
288 #ifdef USE_QUEUING_LOCK_FOR_BGET
289 __kmp_release_lock(&th->th.th_local.bget_lock, __kmp_gtid_from_thread(th));
291 __kmp_release_bootstrap_lock(&th->th.th_local.bget_lock);
298 bfhead_t *b = BFH(((
char *)p) -
sizeof(bhead_t));
300 KMP_DEBUG_ASSERT(b->bh.bb.bsize != 0);
301 KMP_DEBUG_ASSERT(((kmp_uintptr_t)TCR_PTR(b->bh.bb.bthr) & ~1) ==
303 KMP_DEBUG_ASSERT(b->ql.blink == 0);
305 p = (
void *)b->ql.flink;
313 static void __kmp_bget_enqueue(kmp_info_t *th,
void *buf
314 #ifdef USE_QUEUING_LOCK_FOR_BGET
319 bfhead_t *b = BFH(((
char *)buf) -
sizeof(bhead_t));
321 KMP_DEBUG_ASSERT(b->bh.bb.bsize != 0);
322 KMP_DEBUG_ASSERT(((kmp_uintptr_t)TCR_PTR(b->bh.bb.bthr) & ~1) ==
327 KC_TRACE(10, (
"__kmp_bget_enqueue: moving buffer to T#%d list\n",
328 __kmp_gtid_from_thread(th)));
330 #if USE_CMP_XCHG_FOR_BGET 332 volatile void *old_value = TCR_PTR(th->th.th_local.bget_list);
335 b->ql.flink = BFH(CCAST(
void *, old_value));
337 while (!KMP_COMPARE_AND_STORE_PTR(&th->th.th_local.bget_list,
338 CCAST(
void *, old_value), buf)) {
340 old_value = TCR_PTR(th->th.th_local.bget_list);
343 b->ql.flink = BFH(CCAST(
void *, old_value));
347 #ifdef USE_QUEUING_LOCK_FOR_BGET 348 __kmp_acquire_lock(&th->th.th_local.bget_lock, rel_gtid);
350 __kmp_acquire_bootstrap_lock(&th->th.th_local.bget_lock);
353 b->ql.flink = BFH(th->th.th_local.bget_list);
354 th->th.th_local.bget_list = (
void *)buf;
356 #ifdef USE_QUEUING_LOCK_FOR_BGET 357 __kmp_release_lock(&th->th.th_local.bget_lock, rel_gtid);
359 __kmp_release_bootstrap_lock(&th->th.th_local.bget_lock);
365 static void __kmp_bget_insert_into_freelist(thr_data_t *thr, bfhead_t *b) {
368 KMP_DEBUG_ASSERT(((
size_t)b) % SizeQuant == 0);
369 KMP_DEBUG_ASSERT(b->bh.bb.bsize % SizeQuant == 0);
371 bin = bget_get_bin(b->bh.bb.bsize);
373 KMP_DEBUG_ASSERT(thr->freelist[bin].ql.blink->ql.flink ==
374 &thr->freelist[bin]);
375 KMP_DEBUG_ASSERT(thr->freelist[bin].ql.flink->ql.blink ==
376 &thr->freelist[bin]);
378 b->ql.flink = &thr->freelist[bin];
379 b->ql.blink = thr->freelist[bin].ql.blink;
381 thr->freelist[bin].ql.blink = b;
382 b->ql.blink->ql.flink = b;
386 static void __kmp_bget_remove_from_freelist(bfhead_t *b) {
387 KMP_DEBUG_ASSERT(b->ql.blink->ql.flink == b);
388 KMP_DEBUG_ASSERT(b->ql.flink->ql.blink == b);
390 b->ql.blink->ql.flink = b->ql.flink;
391 b->ql.flink->ql.blink = b->ql.blink;
395 static void bcheck(kmp_info_t *th, bufsize *max_free, bufsize *total_free) {
396 thr_data_t *thr = get_thr_data(th);
399 *total_free = *max_free = 0;
401 for (bin = 0; bin < MAX_BGET_BINS; ++bin) {
404 best = &thr->freelist[bin];
407 while (b != &thr->freelist[bin]) {
408 *total_free += (b->bh.bb.bsize -
sizeof(bhead_t));
409 if ((best == &thr->freelist[bin]) || (b->bh.bb.bsize < best->bh.bb.bsize))
416 if (*max_free < best->bh.bb.bsize)
417 *max_free = best->bh.bb.bsize;
420 if (*max_free > (bufsize)
sizeof(bhead_t))
421 *max_free -=
sizeof(bhead_t);
425 static void *bget(kmp_info_t *th, bufsize requested_size) {
426 thr_data_t *thr = get_thr_data(th);
427 bufsize size = requested_size;
435 if (size < 0 || size +
sizeof(bhead_t) > MaxSize) {
439 __kmp_bget_dequeue(th);
441 if (size < (bufsize)SizeQ) {
444 #if defined(SizeQuant) && (SizeQuant > 1) 445 size = (size + (SizeQuant - 1)) & (~(SizeQuant - 1));
448 size +=
sizeof(bhead_t);
449 KMP_DEBUG_ASSERT(size >= 0);
450 KMP_DEBUG_ASSERT(size % SizeQuant == 0);
452 use_blink = (thr->mode == bget_mode_lifo);
461 for (bin = bget_get_bin(size); bin < MAX_BGET_BINS; ++bin) {
463 b = (use_blink ? thr->freelist[bin].ql.blink
464 : thr->freelist[bin].ql.flink);
466 if (thr->mode == bget_mode_best) {
467 best = &thr->freelist[bin];
471 while (b != &thr->freelist[bin]) {
472 if (b->bh.bb.bsize >= (bufsize)size) {
473 if ((best == &thr->freelist[bin]) ||
474 (b->bh.bb.bsize < best->bh.bb.bsize)) {
480 b = (use_blink ? b->ql.blink : b->ql.flink);
485 while (b != &thr->freelist[bin]) {
486 if ((bufsize)b->bh.bb.bsize >= (bufsize)size) {
495 if ((b->bh.bb.bsize - (bufsize)size) >
496 (bufsize)(SizeQ + (
sizeof(bhead_t)))) {
499 ba = BH(((
char *)b) + (b->bh.bb.bsize - (bufsize)size));
500 bn = BH(((
char *)ba) + size);
502 KMP_DEBUG_ASSERT(bn->bb.prevfree == b->bh.bb.bsize);
505 b->bh.bb.bsize -= (bufsize)size;
508 ba->bb.prevfree = b->bh.bb.bsize;
511 ba->bb.bsize = -size;
520 __kmp_bget_remove_from_freelist(b);
521 __kmp_bget_insert_into_freelist(thr, b);
523 thr->totalloc += (size_t)size;
526 buf = (
void *)((((
char *)ba) +
sizeof(bhead_t)));
527 KMP_DEBUG_ASSERT(((
size_t)buf) % SizeQuant == 0);
532 ba = BH(((
char *)b) + b->bh.bb.bsize);
534 KMP_DEBUG_ASSERT(ba->bb.prevfree == b->bh.bb.bsize);
539 __kmp_bget_remove_from_freelist(b);
541 thr->totalloc += (size_t)b->bh.bb.bsize;
545 b->bh.bb.bsize = -(b->bh.bb.bsize);
548 TCW_PTR(ba->bb.bthr, th);
554 buf = (
void *)&(b->ql);
555 KMP_DEBUG_ASSERT(((
size_t)buf) % SizeQuant == 0);
561 b = (use_blink ? b->ql.blink : b->ql.flink);
569 if ((thr->compfcn == 0) || (!(*thr->compfcn)(size, ++compactseq))) {
577 if (thr->acqfcn != 0) {
578 if (size > (bufsize)(thr->exp_incr -
sizeof(bhead_t))) {
583 size +=
sizeof(bdhead_t) -
sizeof(bhead_t);
585 KE_TRACE(10, (
"%%%%%% MALLOC( %d )\n", (
int)size));
588 bdh = BDH((*thr->acqfcn)((bufsize)size));
592 bdh->bh.bb.bsize = 0;
595 TCW_PTR(bdh->bh.bb.bthr, th);
597 bdh->bh.bb.prevfree = 0;
600 thr->totalloc += (size_t)size;
604 buf = (
void *)(bdh + 1);
605 KMP_DEBUG_ASSERT(((
size_t)buf) % SizeQuant == 0);
614 KE_TRACE(10, (
"%%%%%% MALLOCB( %d )\n", (
int)thr->exp_incr));
617 newpool = (*thr->acqfcn)((bufsize)thr->exp_incr);
618 KMP_DEBUG_ASSERT(((
size_t)newpool) % SizeQuant == 0);
619 if (newpool != NULL) {
620 bpool(th, newpool, thr->exp_incr);
637 static void *bgetz(kmp_info_t *th, bufsize size) {
638 char *buf = (
char *)bget(th, size);
644 b = BH(buf -
sizeof(bhead_t));
645 rsize = -(b->bb.bsize);
649 bd = BDH(buf -
sizeof(bdhead_t));
650 rsize = bd->tsize - (bufsize)
sizeof(bdhead_t);
652 rsize -=
sizeof(bhead_t);
655 KMP_DEBUG_ASSERT(rsize >= size);
657 (void)memset(buf, 0, (bufsize)rsize);
659 return ((
void *)buf);
667 static void *bgetr(kmp_info_t *th,
void *buf, bufsize size) {
672 nbuf = bget(th, size);
679 b = BH(((
char *)buf) -
sizeof(bhead_t));
680 osize = -b->bb.bsize;
685 bd = BDH(((
char *)buf) -
sizeof(bdhead_t));
686 osize = bd->tsize - (bufsize)
sizeof(bdhead_t);
688 osize -=
sizeof(bhead_t);
691 KMP_DEBUG_ASSERT(osize > 0);
693 (void)KMP_MEMCPY((
char *)nbuf, (
char *)buf,
694 (
size_t)((size < osize) ? size : osize));
701 static void brel(kmp_info_t *th,
void *buf) {
702 thr_data_t *thr = get_thr_data(th);
706 KMP_DEBUG_ASSERT(buf != NULL);
707 KMP_DEBUG_ASSERT(((
size_t)buf) % SizeQuant == 0);
709 b = BFH(((
char *)buf) -
sizeof(bhead_t));
711 if (b->bh.bb.bsize == 0) {
714 bdh = BDH(((
char *)buf) -
sizeof(bdhead_t));
715 KMP_DEBUG_ASSERT(b->bh.bb.prevfree == 0);
717 thr->totalloc -= (size_t)bdh->tsize;
722 (
void)memset((
char *)buf, 0x55, (
size_t)(bdh->tsize -
sizeof(bdhead_t)));
725 KE_TRACE(10, (
"%%%%%% FREE( %p )\n", (
void *)bdh));
727 KMP_DEBUG_ASSERT(thr->relfcn != 0);
728 (*thr->relfcn)((
void *)bdh);
732 bth = (kmp_info_t *)((kmp_uintptr_t)TCR_PTR(b->bh.bb.bthr) &
736 __kmp_bget_enqueue(bth, buf
737 #ifdef USE_QUEUING_LOCK_FOR_BGET
739 __kmp_gtid_from_thread(th)
746 if (b->bh.bb.bsize >= 0) {
749 KMP_DEBUG_ASSERT(b->bh.bb.bsize < 0);
753 KMP_DEBUG_ASSERT(BH((
char *)b - b->bh.bb.bsize)->bb.prevfree == 0);
757 thr->totalloc += (size_t)b->bh.bb.bsize;
762 if (b->bh.bb.prevfree != 0) {
767 bufsize size = b->bh.bb.bsize;
770 KMP_DEBUG_ASSERT(BH((
char *)b - b->bh.bb.prevfree)->bb.bsize ==
772 b = BFH(((
char *)b) - b->bh.bb.prevfree);
773 b->bh.bb.bsize -= size;
776 __kmp_bget_remove_from_freelist(b);
781 b->bh.bb.bsize = -b->bh.bb.bsize;
785 __kmp_bget_insert_into_freelist(thr, b);
791 bn = BFH(((
char *)b) + b->bh.bb.bsize);
792 if (bn->bh.bb.bsize > 0) {
796 KMP_DEBUG_ASSERT(BH((
char *)bn + bn->bh.bb.bsize)->bb.prevfree ==
799 __kmp_bget_remove_from_freelist(bn);
801 b->bh.bb.bsize += bn->bh.bb.bsize;
805 __kmp_bget_remove_from_freelist(b);
806 __kmp_bget_insert_into_freelist(thr, b);
814 bn = BFH(((
char *)b) + b->bh.bb.bsize);
817 (void)memset(((
char *)b) +
sizeof(bfhead_t), 0x55,
818 (
size_t)(b->bh.bb.bsize -
sizeof(bfhead_t)));
820 KMP_DEBUG_ASSERT(bn->bh.bb.bsize < 0);
825 bn->bh.bb.prevfree = b->bh.bb.bsize;
831 if (thr->relfcn != 0 &&
832 b->bh.bb.bsize == (bufsize)(thr->pool_len -
sizeof(bhead_t))) {
838 KMP_DEBUG_ASSERT(b->bh.bb.prevfree == 0);
839 KMP_DEBUG_ASSERT(BH((
char *)b + b->bh.bb.bsize)->bb.bsize == ESent);
840 KMP_DEBUG_ASSERT(BH((
char *)b + b->bh.bb.bsize)->bb.prevfree ==
844 __kmp_bget_remove_from_freelist(b);
846 KE_TRACE(10, (
"%%%%%% FREE( %p )\n", (
void *)b));
852 KMP_DEBUG_ASSERT(thr->numpblk == thr->numpget - thr->numprel);
855 if (thr->last_pool == b)
865 static void bectl(kmp_info_t *th, bget_compact_t compact,
866 bget_acquire_t acquire, bget_release_t release,
868 thr_data_t *thr = get_thr_data(th);
870 thr->compfcn = compact;
871 thr->acqfcn = acquire;
872 thr->relfcn = release;
873 thr->exp_incr = pool_incr;
877 static void bpool(kmp_info_t *th,
void *buf, bufsize len) {
879 thr_data_t *thr = get_thr_data(th);
880 bfhead_t *b = BFH(buf);
883 __kmp_bget_dequeue(th);
886 len &= ~(SizeQuant - 1);
888 if (thr->pool_len == 0) {
890 }
else if (len != thr->pool_len) {
896 KMP_DEBUG_ASSERT(thr->numpblk == thr->numpget - thr->numprel);
902 KMP_DEBUG_ASSERT(len -
sizeof(bhead_t) <= -((bufsize)ESent + 1));
907 b->bh.bb.prevfree = 0;
916 len -=
sizeof(bhead_t);
917 b->bh.bb.bsize = (bufsize)len;
919 TCW_PTR(b->bh.bb.bthr,
920 (kmp_info_t *)((kmp_uintptr_t)th |
924 __kmp_bget_insert_into_freelist(thr, b);
927 (void)memset(((
char *)b) +
sizeof(bfhead_t), 0x55,
928 (
size_t)(len -
sizeof(bfhead_t)));
930 bn = BH(((
char *)b) + len);
931 bn->bb.prevfree = (bufsize)len;
933 KMP_DEBUG_ASSERT((~0) == -1 && (bn != 0));
935 bn->bb.bsize = ESent;
939 static void bfreed(kmp_info_t *th) {
940 int bin = 0, count = 0;
941 int gtid = __kmp_gtid_from_thread(th);
942 thr_data_t *thr = get_thr_data(th);
945 __kmp_printf_no_lock(
"__kmp_printpool: T#%d total=%" KMP_UINT64_SPEC
946 " get=%" KMP_INT64_SPEC
" rel=%" KMP_INT64_SPEC
947 " pblk=%" KMP_INT64_SPEC
" pget=%" KMP_INT64_SPEC
948 " prel=%" KMP_INT64_SPEC
" dget=%" KMP_INT64_SPEC
949 " drel=%" KMP_INT64_SPEC
"\n",
950 gtid, (kmp_uint64)thr->totalloc, (kmp_int64)thr->numget,
951 (kmp_int64)thr->numrel, (kmp_int64)thr->numpblk,
952 (kmp_int64)thr->numpget, (kmp_int64)thr->numprel,
953 (kmp_int64)thr->numdget, (kmp_int64)thr->numdrel);
956 for (bin = 0; bin < MAX_BGET_BINS; ++bin) {
959 for (b = thr->freelist[bin].ql.flink; b != &thr->freelist[bin];
961 bufsize bs = b->bh.bb.bsize;
963 KMP_DEBUG_ASSERT(b->ql.blink->ql.flink == b);
964 KMP_DEBUG_ASSERT(b->ql.flink->ql.blink == b);
965 KMP_DEBUG_ASSERT(bs > 0);
969 __kmp_printf_no_lock(
970 "__kmp_printpool: T#%d Free block: 0x%p size %6ld bytes.\n", gtid, b,
974 char *lerr = ((
char *)b) +
sizeof(bfhead_t);
975 if ((bs >
sizeof(bfhead_t)) &&
977 (memcmp(lerr, lerr + 1, (
size_t)(bs - (
sizeof(bfhead_t) + 1))) !=
979 __kmp_printf_no_lock(
"__kmp_printpool: T#%d (Contents of above " 980 "free block have been overstored.)\n",
989 __kmp_printf_no_lock(
"__kmp_printpool: T#%d No free blocks\n", gtid);
992 void __kmp_initialize_bget(kmp_info_t *th) {
993 KMP_DEBUG_ASSERT(SizeQuant >=
sizeof(
void *) && (th != 0));
997 bectl(th, (bget_compact_t)0, (bget_acquire_t)malloc, (bget_release_t)free,
998 (bufsize)__kmp_malloc_pool_incr);
1001 void __kmp_finalize_bget(kmp_info_t *th) {
1005 KMP_DEBUG_ASSERT(th != 0);
1008 thr = (thr_data_t *)th->th.th_local.bget_data;
1009 KMP_DEBUG_ASSERT(thr != NULL);
1017 if (thr->relfcn != 0 && b != 0 && thr->numpblk != 0 &&
1018 b->bh.bb.bsize == (bufsize)(thr->pool_len -
sizeof(bhead_t))) {
1019 KMP_DEBUG_ASSERT(b->bh.bb.prevfree == 0);
1020 KMP_DEBUG_ASSERT(BH((
char *)b + b->bh.bb.bsize)->bb.bsize == ESent);
1021 KMP_DEBUG_ASSERT(BH((
char *)b + b->bh.bb.bsize)->bb.prevfree ==
1025 __kmp_bget_remove_from_freelist(b);
1027 KE_TRACE(10, (
"%%%%%% FREE( %p )\n", (
void *)b));
1032 KMP_DEBUG_ASSERT(thr->numpblk == thr->numpget - thr->numprel);
1037 if (th->th.th_local.bget_data != NULL) {
1038 __kmp_free(th->th.th_local.bget_data);
1039 th->th.th_local.bget_data = NULL;
1043 void kmpc_set_poolsize(
size_t size) {
1044 bectl(__kmp_get_thread(), (bget_compact_t)0, (bget_acquire_t)malloc,
1045 (bget_release_t)free, (bufsize)size);
1048 size_t kmpc_get_poolsize(
void) {
1051 p = get_thr_data(__kmp_get_thread());
1056 void kmpc_set_poolmode(
int mode) {
1059 if (mode == bget_mode_fifo || mode == bget_mode_lifo ||
1060 mode == bget_mode_best) {
1061 p = get_thr_data(__kmp_get_thread());
1062 p->mode = (bget_mode_t)mode;
1066 int kmpc_get_poolmode(
void) {
1069 p = get_thr_data(__kmp_get_thread());
1074 void kmpc_get_poolstat(
size_t *maxmem,
size_t *allmem) {
1075 kmp_info_t *th = __kmp_get_thread();
1078 __kmp_bget_dequeue(th);
1086 void kmpc_poolprint(
void) {
1087 kmp_info_t *th = __kmp_get_thread();
1089 __kmp_bget_dequeue(th);
1094 #endif // #if KMP_USE_BGET 1096 void *kmpc_malloc(
size_t size) {
1098 ptr = bget(__kmp_entry_thread(), (bufsize)(size +
sizeof(ptr)));
1101 *(
void **)ptr = ptr;
1102 ptr = (
void **)ptr + 1;
1107 #define IS_POWER_OF_TWO(n) (((n) & ((n)-1)) == 0) 1109 void *kmpc_aligned_malloc(
size_t size,
size_t alignment) {
1111 void *ptr_allocated;
1112 KMP_DEBUG_ASSERT(alignment < 32 * 1024);
1113 if (!IS_POWER_OF_TWO(alignment)) {
1118 size = size +
sizeof(
void *) + alignment;
1119 ptr_allocated = bget(__kmp_entry_thread(), (bufsize)size);
1120 if (ptr_allocated != NULL) {
1122 ptr = (
void *)(((kmp_uintptr_t)ptr_allocated +
sizeof(
void *) + alignment) &
1124 *((
void **)ptr - 1) = ptr_allocated;
1131 void *kmpc_calloc(
size_t nelem,
size_t elsize) {
1133 ptr = bgetz(__kmp_entry_thread(), (bufsize)(nelem * elsize +
sizeof(ptr)));
1136 *(
void **)ptr = ptr;
1137 ptr = (
void **)ptr + 1;
1142 void *kmpc_realloc(
void *ptr,
size_t size) {
1143 void *result = NULL;
1146 result = bget(__kmp_entry_thread(), (bufsize)(size +
sizeof(ptr)));
1148 if (result != NULL) {
1149 *(
void **)result = result;
1150 result = (
void **)result + 1;
1152 }
else if (size == 0) {
1158 KMP_ASSERT(*((
void **)ptr - 1));
1159 brel(__kmp_get_thread(), *((
void **)ptr - 1));
1161 result = bgetr(__kmp_entry_thread(), *((
void **)ptr - 1),
1162 (bufsize)(size +
sizeof(ptr)));
1163 if (result != NULL) {
1164 *(
void **)result = result;
1165 result = (
void **)result + 1;
1172 void kmpc_free(
void *ptr) {
1173 if (!__kmp_init_serial) {
1177 kmp_info_t *th = __kmp_get_thread();
1178 __kmp_bget_dequeue(th);
1180 KMP_ASSERT(*((
void **)ptr - 1));
1181 brel(th, *((
void **)ptr - 1));
1185 void *___kmp_thread_malloc(kmp_info_t *th,
size_t size KMP_SRC_LOC_DECL) {
1187 KE_TRACE(30, (
"-> __kmp_thread_malloc( %p, %d ) called from %s:%d\n", th,
1188 (
int)size KMP_SRC_LOC_PARM));
1189 ptr = bget(th, (bufsize)size);
1190 KE_TRACE(30, (
"<- __kmp_thread_malloc() returns %p\n", ptr));
1194 void *___kmp_thread_calloc(kmp_info_t *th,
size_t nelem,
1195 size_t elsize KMP_SRC_LOC_DECL) {
1197 KE_TRACE(30, (
"-> __kmp_thread_calloc( %p, %d, %d ) called from %s:%d\n", th,
1198 (
int)nelem, (
int)elsize KMP_SRC_LOC_PARM));
1199 ptr = bgetz(th, (bufsize)(nelem * elsize));
1200 KE_TRACE(30, (
"<- __kmp_thread_calloc() returns %p\n", ptr));
1204 void *___kmp_thread_realloc(kmp_info_t *th,
void *ptr,
1205 size_t size KMP_SRC_LOC_DECL) {
1206 KE_TRACE(30, (
"-> __kmp_thread_realloc( %p, %p, %d ) called from %s:%d\n", th,
1207 ptr, (
int)size KMP_SRC_LOC_PARM));
1208 ptr = bgetr(th, ptr, (bufsize)size);
1209 KE_TRACE(30, (
"<- __kmp_thread_realloc() returns %p\n", ptr));
1213 void ___kmp_thread_free(kmp_info_t *th,
void *ptr KMP_SRC_LOC_DECL) {
1214 KE_TRACE(30, (
"-> __kmp_thread_free( %p, %p ) called from %s:%d\n", th,
1215 ptr KMP_SRC_LOC_PARM));
1217 __kmp_bget_dequeue(th);
1220 KE_TRACE(30, (
"<- __kmp_thread_free()\n"));
1224 static const char *kmp_mk_lib_name;
1225 static void *h_memkind;
1228 static void *(*kmp_mk_alloc)(
void *k,
size_t sz);
1230 static void (*kmp_mk_free)(
void *kind,
void *ptr);
1232 static int (*kmp_mk_check)(
void *kind);
1234 static void **mk_default;
1235 static void **mk_interleave;
1236 static void **mk_hbw;
1237 static void **mk_hbw_interleave;
1238 static void **mk_hbw_preferred;
1239 static void **mk_hugetlb;
1240 static void **mk_hbw_hugetlb;
1241 static void **mk_hbw_preferred_hugetlb;
1243 #if KMP_OS_UNIX && KMP_DYNAMIC_LIB 1244 static inline void chk_kind(
void ***pkind) {
1245 KMP_DEBUG_ASSERT(pkind);
1247 if (kmp_mk_check(**pkind))
1252 void __kmp_init_memkind() {
1254 #if KMP_OS_UNIX && KMP_DYNAMIC_LIB 1256 kmp_mk_lib_name =
"libmemkind.so";
1257 h_memkind = dlopen(kmp_mk_lib_name, RTLD_LAZY);
1259 kmp_mk_check = (int (*)(
void *))dlsym(h_memkind,
"memkind_check_available");
1261 (
void *(*)(
void *, size_t))dlsym(h_memkind,
"memkind_malloc");
1262 kmp_mk_free = (void (*)(
void *,
void *))dlsym(h_memkind,
"memkind_free");
1263 mk_default = (
void **)dlsym(h_memkind,
"MEMKIND_DEFAULT");
1264 if (kmp_mk_check && kmp_mk_alloc && kmp_mk_free && mk_default &&
1265 !kmp_mk_check(*mk_default)) {
1266 __kmp_memkind_available = 1;
1267 mk_interleave = (
void **)dlsym(h_memkind,
"MEMKIND_INTERLEAVE");
1268 chk_kind(&mk_interleave);
1269 mk_hbw = (
void **)dlsym(h_memkind,
"MEMKIND_HBW");
1271 mk_hbw_interleave = (
void **)dlsym(h_memkind,
"MEMKIND_HBW_INTERLEAVE");
1272 chk_kind(&mk_hbw_interleave);
1273 mk_hbw_preferred = (
void **)dlsym(h_memkind,
"MEMKIND_HBW_PREFERRED");
1274 chk_kind(&mk_hbw_preferred);
1275 mk_hugetlb = (
void **)dlsym(h_memkind,
"MEMKIND_HUGETLB");
1276 chk_kind(&mk_hugetlb);
1277 mk_hbw_hugetlb = (
void **)dlsym(h_memkind,
"MEMKIND_HBW_HUGETLB");
1278 chk_kind(&mk_hbw_hugetlb);
1279 mk_hbw_preferred_hugetlb =
1280 (
void **)dlsym(h_memkind,
"MEMKIND_HBW_PREFERRED_HUGETLB");
1281 chk_kind(&mk_hbw_preferred_hugetlb);
1282 KE_TRACE(25, (
"__kmp_init_memkind: memkind library initialized\n"));
1288 kmp_mk_check = NULL;
1289 kmp_mk_alloc = NULL;
1292 mk_interleave = NULL;
1294 mk_hbw_interleave = NULL;
1295 mk_hbw_preferred = NULL;
1297 mk_hbw_hugetlb = NULL;
1298 mk_hbw_preferred_hugetlb = NULL;
1300 kmp_mk_lib_name =
"";
1302 kmp_mk_check = NULL;
1303 kmp_mk_alloc = NULL;
1306 mk_interleave = NULL;
1308 mk_hbw_interleave = NULL;
1309 mk_hbw_preferred = NULL;
1311 mk_hbw_hugetlb = NULL;
1312 mk_hbw_preferred_hugetlb = NULL;
1316 void __kmp_fini_memkind() {
1317 #if KMP_OS_UNIX && KMP_DYNAMIC_LIB 1318 if (__kmp_memkind_available)
1319 KE_TRACE(25, (
"__kmp_fini_memkind: finalize memkind library\n"));
1324 kmp_mk_check = NULL;
1325 kmp_mk_alloc = NULL;
1328 mk_interleave = NULL;
1330 mk_hbw_interleave = NULL;
1331 mk_hbw_preferred = NULL;
1333 mk_hbw_hugetlb = NULL;
1334 mk_hbw_preferred_hugetlb = NULL;
1338 omp_allocator_handle_t __kmpc_init_allocator(
int gtid, omp_memspace_handle_t ms,
1340 omp_alloctrait_t traits[]) {
1342 KMP_DEBUG_ASSERT(ms == omp_default_mem_space || ms == omp_low_lat_mem_space ||
1343 ms == omp_large_cap_mem_space || ms == omp_const_mem_space ||
1344 ms == omp_high_bw_mem_space);
1345 kmp_allocator_t *al;
1347 al = (kmp_allocator_t *)__kmp_allocate(
sizeof(kmp_allocator_t));
1349 for (i = 0; i < ntraits; ++i) {
1350 switch (traits[i].key) {
1351 case OMP_ATK_THREADMODEL:
1352 case OMP_ATK_ACCESS:
1353 case OMP_ATK_PINNED:
1355 case OMP_ATK_ALIGNMENT:
1356 al->alignment = traits[i].value;
1357 KMP_ASSERT(IS_POWER_OF_TWO(al->alignment));
1359 case OMP_ATK_POOL_SIZE:
1360 al->pool_size = traits[i].value;
1362 case OMP_ATK_FALLBACK:
1363 al->fb = (omp_alloctrait_value_t)traits[i].value;
1365 al->fb == OMP_ATV_DEFAULT_MEM_FB || al->fb == OMP_ATV_NULL_FB ||
1366 al->fb == OMP_ATV_ABORT_FB || al->fb == OMP_ATV_ALLOCATOR_FB);
1368 case OMP_ATK_FB_DATA:
1369 al->fb_data = RCAST(kmp_allocator_t *, traits[i].value);
1371 case OMP_ATK_PARTITION:
1372 al->memkind = RCAST(
void **, traits[i].value);
1375 KMP_ASSERT2(0,
"Unexpected allocator trait");
1380 al->fb = OMP_ATV_DEFAULT_MEM_FB;
1381 al->fb_data = (kmp_allocator_t *)omp_default_mem_alloc;
1382 }
else if (al->fb == OMP_ATV_ALLOCATOR_FB) {
1383 KMP_ASSERT(al->fb_data != NULL);
1384 }
else if (al->fb == OMP_ATV_DEFAULT_MEM_FB) {
1385 al->fb_data = (kmp_allocator_t *)omp_default_mem_alloc;
1387 if (__kmp_memkind_available) {
1389 if (ms == omp_high_bw_mem_space) {
1390 if (al->memkind == (
void *)OMP_ATV_INTERLEAVED && mk_hbw_interleave) {
1391 al->memkind = mk_hbw_interleave;
1392 }
else if (mk_hbw_preferred) {
1398 al->memkind = mk_hbw_preferred;
1402 return omp_null_allocator;
1405 if (al->memkind == (
void *)OMP_ATV_INTERLEAVED && mk_interleave) {
1406 al->memkind = mk_interleave;
1408 al->memkind = mk_default;
1412 if (ms == omp_high_bw_mem_space) {
1415 return omp_null_allocator;
1418 return (omp_allocator_handle_t)al;
1421 void __kmpc_destroy_allocator(
int gtid, omp_allocator_handle_t allocator) {
1422 if (allocator > kmp_max_mem_alloc)
1423 __kmp_free(allocator);
1426 void __kmpc_set_default_allocator(
int gtid, omp_allocator_handle_t allocator) {
1427 if (allocator == omp_null_allocator)
1428 allocator = omp_default_mem_alloc;
1429 __kmp_threads[gtid]->th.th_def_allocator = allocator;
1432 omp_allocator_handle_t __kmpc_get_default_allocator(
int gtid) {
1433 return __kmp_threads[gtid]->th.th_def_allocator;
1436 typedef struct kmp_mem_desc {
1440 kmp_allocator_t *allocator;
1442 static int alignment =
sizeof(
void *);
1444 void *__kmpc_alloc(
int gtid,
size_t size, omp_allocator_handle_t allocator) {
1446 kmp_allocator_t *al;
1447 KMP_DEBUG_ASSERT(__kmp_init_serial);
1448 if (allocator == omp_null_allocator)
1449 allocator = __kmp_threads[gtid]->th.th_def_allocator;
1451 KE_TRACE(25, (
"__kmpc_alloc: T#%d (%d, %p)\n", gtid, (
int)size, allocator));
1452 al = RCAST(kmp_allocator_t *, CCAST(omp_allocator_handle_t, allocator));
1454 int sz_desc =
sizeof(kmp_mem_desc_t);
1455 kmp_mem_desc_t desc;
1457 kmp_uintptr_t addr_align;
1458 kmp_uintptr_t addr_descr;
1459 int align = alignment;
1460 if (allocator > kmp_max_mem_alloc && al->alignment > 0) {
1461 align = al->alignment;
1463 desc.size_a = size + sz_desc + align;
1465 if (__kmp_memkind_available) {
1466 if (allocator < kmp_max_mem_alloc) {
1468 if (allocator == omp_high_bw_mem_alloc && mk_hbw_preferred) {
1469 ptr = kmp_mk_alloc(*mk_hbw_preferred, desc.size_a);
1471 ptr = kmp_mk_alloc(*mk_default, desc.size_a);
1473 }
else if (al->pool_size > 0) {
1476 KMP_TEST_THEN_ADD64((kmp_int64 *)&al->pool_used, desc.size_a);
1477 if (used + desc.size_a > al->pool_size) {
1479 KMP_TEST_THEN_ADD64((kmp_int64 *)&al->pool_used, -desc.size_a);
1480 if (al->fb == OMP_ATV_DEFAULT_MEM_FB) {
1481 al = (kmp_allocator_t *)omp_default_mem_alloc;
1482 ptr = kmp_mk_alloc(*mk_default, desc.size_a);
1483 }
else if (al->fb == OMP_ATV_ABORT_FB) {
1485 }
else if (al->fb == OMP_ATV_ALLOCATOR_FB) {
1486 KMP_ASSERT(al != al->fb_data);
1488 return __kmpc_alloc(gtid, size, (omp_allocator_handle_t)al);
1492 ptr = kmp_mk_alloc(*al->memkind, desc.size_a);
1494 if (al->fb == OMP_ATV_DEFAULT_MEM_FB) {
1495 al = (kmp_allocator_t *)omp_default_mem_alloc;
1496 ptr = kmp_mk_alloc(*mk_default, desc.size_a);
1497 }
else if (al->fb == OMP_ATV_ABORT_FB) {
1499 }
else if (al->fb == OMP_ATV_ALLOCATOR_FB) {
1500 KMP_ASSERT(al != al->fb_data);
1502 return __kmpc_alloc(gtid, size, (omp_allocator_handle_t)al);
1508 ptr = kmp_mk_alloc(*al->memkind, desc.size_a);
1510 if (al->fb == OMP_ATV_DEFAULT_MEM_FB) {
1511 al = (kmp_allocator_t *)omp_default_mem_alloc;
1512 ptr = kmp_mk_alloc(*mk_default, desc.size_a);
1513 }
else if (al->fb == OMP_ATV_ABORT_FB) {
1515 }
else if (al->fb == OMP_ATV_ALLOCATOR_FB) {
1516 KMP_ASSERT(al != al->fb_data);
1518 return __kmpc_alloc(gtid, size, (omp_allocator_handle_t)al);
1522 }
else if (allocator < kmp_max_mem_alloc) {
1524 if (allocator == omp_high_bw_mem_alloc) {
1527 ptr = __kmp_thread_malloc(__kmp_thread_from_gtid(gtid), desc.size_a);
1529 }
else if (al->pool_size > 0) {
1532 KMP_TEST_THEN_ADD64((kmp_int64 *)&al->pool_used, desc.size_a);
1533 if (used + desc.size_a > al->pool_size) {
1535 KMP_TEST_THEN_ADD64((kmp_int64 *)&al->pool_used, -desc.size_a);
1536 if (al->fb == OMP_ATV_DEFAULT_MEM_FB) {
1537 al = (kmp_allocator_t *)omp_default_mem_alloc;
1538 ptr = __kmp_thread_malloc(__kmp_thread_from_gtid(gtid), desc.size_a);
1539 }
else if (al->fb == OMP_ATV_ABORT_FB) {
1541 }
else if (al->fb == OMP_ATV_ALLOCATOR_FB) {
1542 KMP_ASSERT(al != al->fb_data);
1544 return __kmpc_alloc(gtid, size, (omp_allocator_handle_t)al);
1548 ptr = __kmp_thread_malloc(__kmp_thread_from_gtid(gtid), desc.size_a);
1549 if (ptr == NULL && al->fb == OMP_ATV_ABORT_FB) {
1555 ptr = __kmp_thread_malloc(__kmp_thread_from_gtid(gtid), desc.size_a);
1556 if (ptr == NULL && al->fb == OMP_ATV_ABORT_FB) {
1560 KE_TRACE(10, (
"__kmpc_alloc: T#%d %p=alloc(%d)\n", gtid, ptr, desc.size_a));
1564 addr = (kmp_uintptr_t)ptr;
1565 addr_align = (addr + sz_desc + align - 1) & ~(align - 1);
1566 addr_descr = addr_align - sz_desc;
1568 desc.ptr_alloc = ptr;
1569 desc.ptr_align = (
void *)addr_align;
1570 desc.allocator = al;
1571 *((kmp_mem_desc_t *)addr_descr) = desc;
1574 KE_TRACE(25, (
"__kmpc_alloc returns %p, T#%d\n", desc.ptr_align, gtid));
1575 return desc.ptr_align;
1578 void __kmpc_free(
int gtid,
void *ptr,
const omp_allocator_handle_t allocator) {
1579 KE_TRACE(25, (
"__kmpc_free: T#%d free(%p,%p)\n", gtid, ptr, allocator));
1583 kmp_allocator_t *al;
1584 omp_allocator_handle_t oal;
1585 al = RCAST(kmp_allocator_t *, CCAST(omp_allocator_handle_t, allocator));
1586 kmp_mem_desc_t desc;
1587 kmp_uintptr_t addr_align;
1588 kmp_uintptr_t addr_descr;
1590 addr_align = (kmp_uintptr_t)ptr;
1591 addr_descr = addr_align -
sizeof(kmp_mem_desc_t);
1592 desc = *((kmp_mem_desc_t *)addr_descr);
1594 KMP_DEBUG_ASSERT(desc.ptr_align == ptr);
1596 KMP_DEBUG_ASSERT(desc.allocator == al || desc.allocator == al->fb_data);
1598 al = desc.allocator;
1599 oal = (omp_allocator_handle_t)al;
1600 KMP_DEBUG_ASSERT(al);
1602 if (__kmp_memkind_available) {
1603 if (oal < kmp_max_mem_alloc) {
1605 if (oal == omp_high_bw_mem_alloc && mk_hbw_preferred) {
1606 kmp_mk_free(*mk_hbw_preferred, desc.ptr_alloc);
1608 kmp_mk_free(*mk_default, desc.ptr_alloc);
1611 if (al->pool_size > 0) {
1613 KMP_TEST_THEN_ADD64((kmp_int64 *)&al->pool_used, -desc.size_a);
1615 KMP_DEBUG_ASSERT(used >= desc.size_a);
1617 kmp_mk_free(*al->memkind, desc.ptr_alloc);
1620 if (oal > kmp_max_mem_alloc && al->pool_size > 0) {
1622 KMP_TEST_THEN_ADD64((kmp_int64 *)&al->pool_used, -desc.size_a);
1624 KMP_DEBUG_ASSERT(used >= desc.size_a);
1626 __kmp_thread_free(__kmp_thread_from_gtid(gtid), desc.ptr_alloc);
1628 KE_TRACE(10, (
"__kmpc_free: T#%d freed %p (%p)\n", gtid, desc.ptr_alloc,
1636 struct kmp_mem_descr {
1637 void *ptr_allocated;
1638 size_t size_allocated;
1640 size_t size_aligned;
1642 typedef struct kmp_mem_descr kmp_mem_descr_t;
1647 static void *___kmp_allocate_align(
size_t size,
1648 size_t alignment KMP_SRC_LOC_DECL) {
1665 kmp_mem_descr_t descr;
1666 kmp_uintptr_t addr_allocated;
1667 kmp_uintptr_t addr_aligned;
1668 kmp_uintptr_t addr_descr;
1670 KE_TRACE(25, (
"-> ___kmp_allocate_align( %d, %d ) called from %s:%d\n",
1671 (
int)size, (
int)alignment KMP_SRC_LOC_PARM));
1673 KMP_DEBUG_ASSERT(alignment < 32 * 1024);
1674 KMP_DEBUG_ASSERT(
sizeof(
void *) <=
sizeof(kmp_uintptr_t));
1677 descr.size_aligned = size;
1678 descr.size_allocated =
1679 descr.size_aligned +
sizeof(kmp_mem_descr_t) + alignment;
1682 descr.ptr_allocated = _malloc_src_loc(descr.size_allocated, _file_, _line_);
1684 descr.ptr_allocated = malloc_src_loc(descr.size_allocated KMP_SRC_LOC_PARM);
1686 KE_TRACE(10, (
" malloc( %d ) returned %p\n", (
int)descr.size_allocated,
1687 descr.ptr_allocated));
1688 if (descr.ptr_allocated == NULL) {
1689 KMP_FATAL(OutOfHeapMemory);
1692 addr_allocated = (kmp_uintptr_t)descr.ptr_allocated;
1694 (addr_allocated +
sizeof(kmp_mem_descr_t) + alignment) & ~(alignment - 1);
1695 addr_descr = addr_aligned -
sizeof(kmp_mem_descr_t);
1697 descr.ptr_aligned = (
void *)addr_aligned;
1699 KE_TRACE(26, (
" ___kmp_allocate_align: " 1700 "ptr_allocated=%p, size_allocated=%d, " 1701 "ptr_aligned=%p, size_aligned=%d\n",
1702 descr.ptr_allocated, (
int)descr.size_allocated,
1703 descr.ptr_aligned, (
int)descr.size_aligned));
1705 KMP_DEBUG_ASSERT(addr_allocated <= addr_descr);
1706 KMP_DEBUG_ASSERT(addr_descr +
sizeof(kmp_mem_descr_t) == addr_aligned);
1707 KMP_DEBUG_ASSERT(addr_aligned + descr.size_aligned <=
1708 addr_allocated + descr.size_allocated);
1709 KMP_DEBUG_ASSERT(addr_aligned % alignment == 0);
1711 memset(descr.ptr_allocated, 0xEF, descr.size_allocated);
1714 memset(descr.ptr_aligned, 0x00, descr.size_aligned);
1720 *((kmp_mem_descr_t *)addr_descr) = descr;
1724 KE_TRACE(25, (
"<- ___kmp_allocate_align() returns %p\n", descr.ptr_aligned));
1725 return descr.ptr_aligned;
1732 void *___kmp_allocate(
size_t size KMP_SRC_LOC_DECL) {
1734 KE_TRACE(25, (
"-> __kmp_allocate( %d ) called from %s:%d\n",
1735 (
int)size KMP_SRC_LOC_PARM));
1736 ptr = ___kmp_allocate_align(size, __kmp_align_alloc KMP_SRC_LOC_PARM);
1737 KE_TRACE(25, (
"<- __kmp_allocate() returns %p\n", ptr));
1745 void *___kmp_page_allocate(
size_t size KMP_SRC_LOC_DECL) {
1746 int page_size = 8 * 1024;
1749 KE_TRACE(25, (
"-> __kmp_page_allocate( %d ) called from %s:%d\n",
1750 (
int)size KMP_SRC_LOC_PARM));
1751 ptr = ___kmp_allocate_align(size, page_size KMP_SRC_LOC_PARM);
1752 KE_TRACE(25, (
"<- __kmp_page_allocate( %d ) returns %p\n", (
int)size, ptr));
1758 void ___kmp_free(
void *ptr KMP_SRC_LOC_DECL) {
1759 kmp_mem_descr_t descr;
1760 kmp_uintptr_t addr_allocated;
1761 kmp_uintptr_t addr_aligned;
1764 (
"-> __kmp_free( %p ) called from %s:%d\n", ptr KMP_SRC_LOC_PARM));
1765 KMP_ASSERT(ptr != NULL);
1767 descr = *(kmp_mem_descr_t *)((kmp_uintptr_t)ptr -
sizeof(kmp_mem_descr_t));
1769 KE_TRACE(26, (
" __kmp_free: " 1770 "ptr_allocated=%p, size_allocated=%d, " 1771 "ptr_aligned=%p, size_aligned=%d\n",
1772 descr.ptr_allocated, (
int)descr.size_allocated,
1773 descr.ptr_aligned, (
int)descr.size_aligned));
1775 addr_allocated = (kmp_uintptr_t)descr.ptr_allocated;
1776 addr_aligned = (kmp_uintptr_t)descr.ptr_aligned;
1778 KMP_DEBUG_ASSERT(addr_aligned % CACHE_LINE == 0);
1779 KMP_DEBUG_ASSERT(descr.ptr_aligned == ptr);
1780 KMP_DEBUG_ASSERT(addr_allocated +
sizeof(kmp_mem_descr_t) <= addr_aligned);
1781 KMP_DEBUG_ASSERT(descr.size_aligned < descr.size_allocated);
1782 KMP_DEBUG_ASSERT(addr_aligned + descr.size_aligned <=
1783 addr_allocated + descr.size_allocated);
1786 memset(descr.ptr_allocated, 0xEF, descr.size_allocated);
1791 KE_TRACE(10, (
" free( %p )\n", descr.ptr_allocated));
1793 _free_src_loc(descr.ptr_allocated, _file_, _line_);
1795 free_src_loc(descr.ptr_allocated KMP_SRC_LOC_PARM);
1799 KE_TRACE(25, (
"<- __kmp_free() returns\n"));
1802 #if USE_FAST_MEMORY == 3 1808 #define KMP_FREE_LIST_LIMIT 16 1811 #define DCACHE_LINE 128 1813 void *___kmp_fast_allocate(kmp_info_t *this_thr,
size_t size KMP_SRC_LOC_DECL) {
1820 kmp_mem_descr_t *descr;
1822 KE_TRACE(25, (
"-> __kmp_fast_allocate( T#%d, %d ) called from %s:%d\n",
1823 __kmp_gtid_from_thread(this_thr), (
int)size KMP_SRC_LOC_PARM));
1825 num_lines = (size + DCACHE_LINE - 1) / DCACHE_LINE;
1826 idx = num_lines - 1;
1827 KMP_DEBUG_ASSERT(idx >= 0);
1831 }
else if ((idx >>= 2) == 0) {
1834 }
else if ((idx >>= 2) == 0) {
1837 }
else if ((idx >>= 2) == 0) {
1844 ptr = this_thr->th.th_free_lists[index].th_free_list_self;
1847 this_thr->th.th_free_lists[index].th_free_list_self = *((
void **)ptr);
1850 ((kmp_mem_descr_t *)((kmp_uintptr_t)ptr -
sizeof(kmp_mem_descr_t)))
1854 ptr = TCR_SYNC_PTR(this_thr->th.th_free_lists[index].th_free_list_sync);
1859 while (!KMP_COMPARE_AND_STORE_PTR(
1860 &this_thr->th.th_free_lists[index].th_free_list_sync, ptr,
nullptr)) {
1862 ptr = TCR_SYNC_PTR(this_thr->th.th_free_lists[index].th_free_list_sync);
1866 this_thr->th.th_free_lists[index].th_free_list_self = *((
void **)ptr);
1869 ((kmp_mem_descr_t *)((kmp_uintptr_t)ptr -
sizeof(kmp_mem_descr_t)))
1876 size = num_lines * DCACHE_LINE;
1878 alloc_size = size +
sizeof(kmp_mem_descr_t) + DCACHE_LINE;
1879 KE_TRACE(25, (
"__kmp_fast_allocate: T#%d Calling __kmp_thread_malloc with " 1881 __kmp_gtid_from_thread(this_thr), alloc_size));
1882 alloc_ptr = bget(this_thr, (bufsize)alloc_size);
1885 ptr = (
void *)((((kmp_uintptr_t)alloc_ptr) +
sizeof(kmp_mem_descr_t) +
1887 ~(DCACHE_LINE - 1));
1888 descr = (kmp_mem_descr_t *)(((kmp_uintptr_t)ptr) -
sizeof(kmp_mem_descr_t));
1890 descr->ptr_allocated = alloc_ptr;
1892 descr->ptr_aligned = (
void *)this_thr;
1895 descr->size_aligned = size;
1898 KE_TRACE(25, (
"<- __kmp_fast_allocate( T#%d ) returns %p\n",
1899 __kmp_gtid_from_thread(this_thr), ptr));
1905 void ___kmp_fast_free(kmp_info_t *this_thr,
void *ptr KMP_SRC_LOC_DECL) {
1906 kmp_mem_descr_t *descr;
1907 kmp_info_t *alloc_thr;
1912 KE_TRACE(25, (
"-> __kmp_fast_free( T#%d, %p ) called from %s:%d\n",
1913 __kmp_gtid_from_thread(this_thr), ptr KMP_SRC_LOC_PARM));
1914 KMP_ASSERT(ptr != NULL);
1916 descr = (kmp_mem_descr_t *)(((kmp_uintptr_t)ptr) -
sizeof(kmp_mem_descr_t));
1918 KE_TRACE(26, (
" __kmp_fast_free: size_aligned=%d\n",
1919 (
int)descr->size_aligned));
1921 size = descr->size_aligned;
1923 idx = DCACHE_LINE * 2;
1926 }
else if ((idx <<= 1) == size) {
1928 }
else if ((idx <<= 2) == size) {
1930 }
else if ((idx <<= 2) == size) {
1933 KMP_DEBUG_ASSERT(size > DCACHE_LINE * 64);
1937 alloc_thr = (kmp_info_t *)descr->ptr_aligned;
1938 if (alloc_thr == this_thr) {
1940 *((
void **)ptr) = this_thr->th.th_free_lists[index].th_free_list_self;
1941 this_thr->th.th_free_lists[index].th_free_list_self = ptr;
1943 void *head = this_thr->th.th_free_lists[index].th_free_list_other;
1946 this_thr->th.th_free_lists[index].th_free_list_other = ptr;
1947 *((
void **)ptr) = NULL;
1948 descr->size_allocated = (size_t)1;
1951 kmp_mem_descr_t *dsc =
1952 (kmp_mem_descr_t *)((
char *)head -
sizeof(kmp_mem_descr_t));
1954 kmp_info_t *q_th = (kmp_info_t *)(dsc->ptr_aligned);
1956 dsc->size_allocated + 1;
1957 if (q_th == alloc_thr && q_sz <= KMP_FREE_LIST_LIMIT) {
1959 *((
void **)ptr) = head;
1960 descr->size_allocated = q_sz;
1961 this_thr->th.th_free_lists[index].th_free_list_other = ptr;
1968 void *next = *((
void **)head);
1969 while (next != NULL) {
1972 ((kmp_mem_descr_t *)((
char *)next -
sizeof(kmp_mem_descr_t)))
1975 ((kmp_mem_descr_t *)((
char *)tail -
sizeof(kmp_mem_descr_t)))
1978 next = *((
void **)next);
1980 KMP_DEBUG_ASSERT(q_th != NULL);
1982 old_ptr = TCR_PTR(q_th->th.th_free_lists[index].th_free_list_sync);
1985 *((
void **)tail) = old_ptr;
1987 while (!KMP_COMPARE_AND_STORE_PTR(
1988 &q_th->th.th_free_lists[index].th_free_list_sync, old_ptr, head)) {
1990 old_ptr = TCR_PTR(q_th->th.th_free_lists[index].th_free_list_sync);
1991 *((
void **)tail) = old_ptr;
1995 this_thr->th.th_free_lists[index].th_free_list_other = ptr;
1996 *((
void **)ptr) = NULL;
1997 descr->size_allocated = (size_t)1;
2004 KE_TRACE(25, (
"__kmp_fast_free: T#%d Calling __kmp_thread_free for size %d\n",
2005 __kmp_gtid_from_thread(this_thr), size));
2006 __kmp_bget_dequeue(this_thr);
2007 brel(this_thr, descr->ptr_allocated);
2010 KE_TRACE(25, (
"<- __kmp_fast_free() returns\n"));
2016 void __kmp_initialize_fast_memory(kmp_info_t *this_thr) {
2017 KE_TRACE(10, (
"__kmp_initialize_fast_memory: Called from th %p\n", this_thr));
2019 memset(this_thr->th.th_free_lists, 0, NUM_LISTS *
sizeof(kmp_free_list_t));
2024 void __kmp_free_fast_memory(kmp_info_t *th) {
2027 thr_data_t *thr = get_thr_data(th);
2031 5, (
"__kmp_free_fast_memory: Called T#%d\n", __kmp_gtid_from_thread(th)));
2033 __kmp_bget_dequeue(th);
2036 for (bin = 0; bin < MAX_BGET_BINS; ++bin) {
2037 bfhead_t *b = thr->freelist[bin].ql.flink;
2038 while (b != &thr->freelist[bin]) {
2039 if ((kmp_uintptr_t)b->bh.bb.bthr & 1) {
2047 while (lst != NULL) {
2049 KE_TRACE(10, (
"__kmp_free_fast_memory: freeing %p, next=%p th %p (%d)\n",
2050 lst, next, th, __kmp_gtid_from_thread(th)));
2051 (*thr->relfcn)(lst);
2057 lst = (
void **)next;
2061 5, (
"__kmp_free_fast_memory: Freed T#%d\n", __kmp_gtid_from_thread(th)));
2064 #endif // USE_FAST_MEMORY