3535#include " utils_math.h"
3636#include " utils_sanitizers.h"
3737
38+ #include " utils_concurrency.h"
39+
3840// TODO remove
3941#ifdef __cplusplus
4042extern " C" {
@@ -49,11 +51,6 @@ struct slab_t;
4951#endif
5052// end TODO remove
5153
52- typedef struct umf_disjoint_pool_shared_limits_t {
53- size_t MaxSize;
54- std::atomic<size_t > TotalSize;
55- } umf_disjoint_pool_shared_limits_t ;
56-
5754class DisjointPool {
5855 public:
5956 class AllocImpl ;
@@ -78,12 +75,12 @@ class DisjointPool {
7875
7976umf_disjoint_pool_shared_limits_t *
8077umfDisjointPoolSharedLimitsCreate (size_t MaxSize) {
81- return new umf_disjoint_pool_shared_limits_t { MaxSize, 0 } ;
78+ return shared_limits_create ( MaxSize) ;
8279}
8380
8481void umfDisjointPoolSharedLimitsDestroy (
8582 umf_disjoint_pool_shared_limits_t *limits) {
86- delete limits;
83+ shared_limits_destroy ( limits) ;
8784}
8885
8986// Allocations are a minimum of 4KB/64KB/2MB even when a smaller size is
@@ -125,8 +122,7 @@ class DisjointPool::AllocImpl {
125122 // Configuration for this instance
126123 umf_disjoint_pool_params_t params;
127124
128- umf_disjoint_pool_shared_limits_t DefaultSharedLimits = {
129- (std::numeric_limits<size_t >::max)(), 0 };
125+ umf_disjoint_pool_shared_limits_t *DefaultSharedLimits;
130126
131127 // Used in algorithm for finding buckets
132128 std::size_t MinBucketSizeExp;
@@ -150,13 +146,14 @@ class DisjointPool::AllocImpl {
150146 Size1 = std::max (Size1, UMF_DISJOINT_POOL_MIN_BUCKET_DEFAULT_SIZE);
151147 // Calculate the exponent for MinBucketSize used for finding buckets.
152148 MinBucketSizeExp = (size_t )log2Utils (Size1);
149+ DefaultSharedLimits = shared_limits_create (SIZE_MAX);
153150 auto Size2 = Size1 + Size1 / 2 ;
154151 for (; Size2 < CutOff; Size1 *= 2 , Size2 *= 2 ) {
155152 // TODO copy allocimpl
156- Buckets.push_back (create_bucket (Size1, this ));
157- Buckets.push_back (create_bucket (Size2, this ));
153+ Buckets.push_back (create_bucket (Size1, this , this -> getLimits () ));
154+ Buckets.push_back (create_bucket (Size2, this , this -> getLimits () ));
158155 }
159- Buckets.push_back (create_bucket (CutOff, this ));
156+ Buckets.push_back (create_bucket (CutOff, this , this -> getLimits () ));
160157
161158 auto ret = umfMemoryProviderGetMinPageSize (hProvider, nullptr ,
162159 &ProviderMinPageSize);
@@ -166,6 +163,8 @@ class DisjointPool::AllocImpl {
166163 }
167164
168165 ~AllocImpl () {
166+ // TODO
167+ // destroy DefaultSharedLimits
169168
170169 for (auto it = Buckets.begin (); it != Buckets.end (); it++) {
171170 destroy_bucket (*it);
@@ -196,7 +195,7 @@ class DisjointPool::AllocImpl {
196195 if (params.SharedLimits ) {
197196 return params.SharedLimits ;
198197 } else {
199- return & DefaultSharedLimits;
198+ return DefaultSharedLimits;
200199 }
201200 };
202201
@@ -254,14 +253,6 @@ std::ostream &operator<<(std::ostream &Os, slab_t &Slab) {
254253}
255254*/
256255
257- // If a slab was available in the pool then note that the current pooled
258- // size has reduced by the size of a slab in this bucket.
259- void bucket_decrement_pool (bucket_t *bucket, bool *FromPool) {
260- *FromPool = true ;
261- bucket_update_stats (bucket, 1 , -1 );
262- bucket_get_limits (bucket)->TotalSize -= bucket_slab_alloc_size (bucket);
263- }
264-
265256/*
266257void Bucket::printStats(bool &TitlePrinted, const std::string &Label) {
267258 if (allocCount) {
@@ -553,7 +544,7 @@ size_t DisjointPool::malloc_usable_size(void *) {
553544umf_result_t DisjointPool::free (void *ptr) {
554545 bool ToPool;
555546 umf_result_t ret = impl->deallocate (ptr, ToPool);
556-
547+ /*
557548 if (ret == UMF_RESULT_SUCCESS) {
558549
559550 if (impl->getParams().PoolTrace > 2) {
@@ -565,7 +556,7 @@ umf_result_t DisjointPool::free(void *ptr) {
565556 << ", Current pool size for " << MT << " "
566557 << impl->getParams().CurPoolSize << "\n";
567558 }
568- }
559+ }*/
569560 return ret;
570561}
571562
@@ -577,10 +568,11 @@ DisjointPool::DisjointPool() {}
577568
578569// Define destructor for use with unique_ptr
579570DisjointPool::~DisjointPool () {
580- bool TitlePrinted = false ;
581- size_t HighBucketSize;
582- size_t HighPeakSlabsInUse;
571+ /*
583572 if (impl->getParams().PoolTrace > 1) {
573+ bool TitlePrinted = false;
574+ size_t HighBucketSize;
575+ size_t HighPeakSlabsInUse;
584576 auto name = impl->getParams().Name;
585577 //try { // cannot throw in destructor
586578 impl->printStats(TitlePrinted, HighBucketSize, HighPeakSlabsInUse,
@@ -596,6 +588,7 @@ DisjointPool::~DisjointPool() {
596588 //} catch (...) { // ignore exceptions
597589 // }
598590 }
591+ */
599592}
600593
601594static umf_memory_pool_ops_t UMF_DISJOINT_POOL_OPS =
@@ -666,47 +659,6 @@ void slab_unreg_by_addr(void *addr, slab_t *slab) {
666659 assert (false && " Slab is not found" );
667660}
668661
669- bool bucket_can_pool (bucket_t *bucket, bool *ToPool) {
670- size_t NewFreeSlabsInBucket;
671- // Check if this bucket is used in chunked form or as full slabs.
672- bool chunkedBucket =
673- bucket_get_size (bucket) <= bucket_chunk_cut_off (bucket);
674- if (chunkedBucket) {
675- NewFreeSlabsInBucket = bucket->chunkedSlabsInPool + 1 ;
676- } else {
677- // TODO optimize
678- size_t avail_num = 0 ;
679- slab_list_item_t *it = NULL ;
680- DL_FOREACH (bucket->AvailableSlabs , it) { avail_num++; }
681- NewFreeSlabsInBucket = avail_num + 1 ;
682- }
683- if (bucket_capacity (bucket) >= NewFreeSlabsInBucket) {
684- size_t PoolSize = bucket_get_limits (bucket)->TotalSize ;
685- while (true ) {
686- size_t NewPoolSize = PoolSize + bucket_slab_alloc_size (bucket);
687-
688- if (bucket_get_limits (bucket)->MaxSize < NewPoolSize) {
689- break ;
690- }
691-
692- if (bucket_get_limits (bucket)->TotalSize .compare_exchange_strong (
693- PoolSize, NewPoolSize)) {
694- if (chunkedBucket) {
695- ++bucket->chunkedSlabsInPool ;
696- }
697-
698- bucket_update_stats (bucket, -1 , 1 );
699- *ToPool = true ;
700- return true ;
701- }
702- }
703- }
704-
705- bucket_update_stats (bucket, -1 , 0 );
706- *ToPool = false ;
707- return false ;
708- }
709-
710662#ifdef __cplusplus
711663}
712664#endif
0 commit comments