2727#include " ../cpp_helpers.hpp"
2828#include " pool_disjoint.h"
2929#include " umf.h"
30+ #include " utils_common.h"
3031#include " utils_log.h"
3132#include " utils_math.h"
3233#include " utils_sanitizers.h"
@@ -103,31 +104,6 @@ void umfDisjointPoolSharedLimitsDestroy(
103104// go directly to the provider.
104105static constexpr size_t CutOff = (size_t )1 << 31 ; // 2GB
105106
106- // Aligns the pointer down to the specified alignment
107- // (e.g. returns 8 for Size = 13, Alignment = 8)
108- static void *AlignPtrDown (void *Ptr, const size_t Alignment) {
109- return reinterpret_cast <void *>((reinterpret_cast <size_t >(Ptr)) &
110- (~(Alignment - 1 )));
111- }
112-
113- // Aligns the pointer up to the specified alignment
114- // (e.g. returns 16 for Size = 13, Alignment = 8)
115- static void *AlignPtrUp (void *Ptr, const size_t Alignment) {
116- void *AlignedPtr = AlignPtrDown (Ptr, Alignment);
117- // Special case when the pointer is already aligned
118- if (Ptr == AlignedPtr) {
119- return Ptr;
120- }
121- return static_cast <char *>(AlignedPtr) + Alignment;
122- }
123-
124- // Aligns the value up to the specified alignment
125- // (e.g. returns 16 for Size = 13, Alignment = 8)
126- static size_t AlignUp (size_t Val, size_t Alignment) {
127- assert (Alignment > 0 );
128- return (Val + Alignment - 1 ) & (~(Alignment - 1 ));
129- }
130-
131107typedef struct MemoryProviderError {
132108 umf_result_t code;
133109} MemoryProviderError_t;
@@ -691,7 +667,7 @@ void *DisjointPool::AllocImpl::allocate(size_t Size, size_t Alignment,
691667 // This allocation will be served from a Bucket which size is multiple
692668 // of Alignment and Slab address is aligned to ProviderMinPageSize
693669 // so the address will be properly aligned.
694- AlignedSize = (Size > 1 ) ? AlignUp (Size, Alignment) : Alignment;
670+ AlignedSize = (Size > 1 ) ? ALIGN_UP (Size, Alignment) : Alignment;
695671 } else {
696672 // Slabs are only aligned to ProviderMinPageSize, we need to compensate
697673 // for that in case the allocation is within pooling limit.
@@ -720,9 +696,9 @@ void *DisjointPool::AllocImpl::allocate(size_t Size, size_t Alignment,
720696 Bucket.countAlloc (FromPool);
721697 }
722698
723- VALGRIND_DO_MEMPOOL_ALLOC (this , AlignPtrUp ( Ptr, Alignment), Size);
724- annotate_memory_undefined (AlignPtrUp ( Ptr, Alignment), Size);
725- return AlignPtrUp ( Ptr, Alignment);
699+ VALGRIND_DO_MEMPOOL_ALLOC (this , ALIGN_UP (( size_t ) Ptr, Alignment), Size);
700+ annotate_memory_undefined (( void *) ALIGN_UP (( size_t ) Ptr, Alignment), Size);
701+ return ( void *) ALIGN_UP (( size_t ) Ptr, Alignment);
726702} catch (MemoryProviderError &e) {
727703 umf::getPoolLastStatusRef<DisjointPool>() = e.code ;
728704 return nullptr ;
@@ -760,7 +736,11 @@ Bucket &DisjointPool::AllocImpl::findBucket(size_t Size) {
760736}
761737
762738void DisjointPool::AllocImpl::deallocate (void *Ptr, bool &ToPool) {
763- auto *SlabPtr = AlignPtrDown (Ptr, SlabMinSize ());
739+ if (Ptr == nullptr ) {
740+ return ;
741+ }
742+
743+ auto *SlabPtr = (void *)ALIGN_DOWN ((size_t )Ptr, SlabMinSize ());
764744
765745 // Lock the map on read
766746 std::shared_lock<std::shared_timed_mutex> Lk (getKnownSlabsMapLock ());
@@ -990,7 +970,8 @@ void slab_unreg_by_addr(void *addr, slab_t *slab) {
990970
991971void slab_reg (slab_t *slab) {
992972 Bucket *bucket = (Bucket *)slab_get_bucket (slab);
993- void *start_addr = AlignPtrDown (slab_get (slab), bucket->SlabMinSize ());
973+ void *start_addr =
974+ (void *)ALIGN_DOWN ((size_t )slab_get (slab), bucket->SlabMinSize ());
994975 void *end_addr = static_cast <char *>(start_addr) + bucket->SlabMinSize ();
995976
996977 slab_reg_by_addr (start_addr, slab);
@@ -999,7 +980,8 @@ void slab_reg(slab_t *slab) {
999980
1000981void slab_unreg (slab_t *slab) {
1001982 Bucket *bucket = (Bucket *)slab_get_bucket (slab);
1002- void *start_addr = AlignPtrDown (slab_get (slab), bucket->SlabMinSize ());
983+ void *start_addr =
984+ (void *)ALIGN_DOWN ((size_t )slab_get (slab), bucket->SlabMinSize ());
1003985 void *end_addr = static_cast <char *>(start_addr) + bucket->SlabMinSize ();
1004986
1005987 slab_unreg_by_addr (start_addr, slab);
0 commit comments