20 #ifndef GALOIS_RUNTIME_MEM_H
21 #define GALOIS_RUNTIME_MEM_H
31 #include <boost/utility.hpp>
33 #include "galois/config.h"
56 void pageIn(
void* buf,
size_t len,
size_t stride);
74 void*
allocate(
size_t size) {
return malloc(size); }
81 template <
class SourceHeap>
91 template <
typename... Args>
92 inline void*
allocate(
size_t size, Args&&... args) {
93 return heaps.
getLocal()->allocate(size, std::forward<Args>(args)...);
99 for (
unsigned int i = 0; i < heaps.
size(); i++)
105 template <
class SourceHeap>
114 void* retval = SourceHeap::allocate(size);
121 SourceHeap::deallocate(ptr);
126 template <
typename SourceHeap>
132 void* retval = SourceHeap::allocate(size);
133 memset(retval, 0, size);
137 inline void deallocate(
void* ptr) { SourceHeap::deallocate(ptr); }
141 template <
typename Header,
typename SourceHeap>
144 offset = (
sizeof(Header) + (
sizeof(
double) - 1)) & ~(
sizeof(double) - 1)
150 void* ptr = SourceHeap::allocate(size + offset);
152 return (
char*)ptr + offset;
158 return (Header*)((
char*)ptr - offset);
163 template <
class SourceHeap>
185 template <
class SourceHeap>
201 SourceHeap::deallocate(N);
212 dbg::print(
this,
" picking from free list, ptr = ", ptr);
215 void* ptr = SourceHeap::allocate(size);
216 dbg::print(
this,
" allocating from SourceHeap, ptr = ", ptr);
224 assert((uintptr_t)ptr > 0x100);
225 FreeNode* NH = (FreeNode*)ptr;
228 dbg::print(
this,
" adding block to list, head = ", head);
233 template <
class SourceHeap>
247 }
while (!__sync_bool_compare_and_swap(&head, h, 0));
251 SourceHeap::deallocate(N);
268 return SourceHeap::allocate(size);
271 }
while (!__sync_bool_compare_and_swap(&head, OH, NH));
286 }
while (!__sync_bool_compare_and_swap(&head, OH, NH));
290 template <
unsigned ElemSize,
typename SourceHeap>
293 double data[((ElemSize +
sizeof(double) - 1) & ~(
sizeof(double) - 1)) /
306 BytesLeft = (SourceHeap::AllocSize -
sizeof(Block_basic)),
307 BytesLeftR = BytesLeft & ~(
sizeof(double) - 1),
308 FitLeft = BytesLeftR /
sizeof(TyEq[1]),
309 TotalFit = FitLeft + 1
324 void* P = SourceHeap::allocate(SourceHeap::AllocSize);
325 Block* BP = (Block*)P;
338 SourceHeap::deallocate(B);
343 static_assert(
sizeof(Block) <= SourceHeap::AllocSize,
"");
348 inline void*
allocate(
size_t GALOIS_USED_ONLY_IN_DEBUG(size)) {
349 assert(size == ElemSize);
350 if (!head || headIndex == TotalFit)
352 return &head->data[headIndex++];
359 template <
typename SourceHeap>
372 void* P = SourceHeap::allocate(SourceHeap::AllocSize);
373 Block* BP = (Block*)P;
376 offset =
sizeof(Block);
390 SourceHeap::deallocate(B);
396 size_t alignedSize = (size +
sizeof(double) - 1) & ~(
sizeof(double) - 1);
398 if (!head || offset + alignedSize > SourceHeap::AllocSize) {
401 if (offset + alignedSize > SourceHeap::AllocSize) {
403 throw std::bad_alloc();
405 char* retval = (
char*)head;
407 offset += alignedSize;
415 inline void*
allocate(
size_t size,
size_t& allocated) {
417 size_t alignedSize = (size +
sizeof(double) - 1) & ~(
sizeof(double) - 1);
418 if (alignedSize > SourceHeap::AllocSize) {
419 alignedSize = SourceHeap::AllocSize;
422 if (!head || offset + alignedSize > SourceHeap::AllocSize) {
423 size_t remaining = SourceHeap::AllocSize - offset;
424 assert((remaining & (
sizeof(
double) - 1)) ==
429 alignedSize = remaining;
432 char* retval = (
char*)head;
434 offset += alignedSize;
435 allocated = (alignedSize > size) ? size : alignedSize;
446 template <
typename SourceHeap>
460 void refill(
void* P, Block*& h,
int* o) {
461 Block* BP = (Block*)P;
479 SourceHeap::deallocate(B);
481 while (fallbackHead) {
482 Block* B = fallbackHead;
483 fallbackHead = B->next;
490 size_t alignedSize = (size +
sizeof(double) - 1) & ~(
sizeof(double) - 1);
491 if (
sizeof(Block) + alignedSize > SourceHeap::AllocSize) {
492 void* p = malloc(alignedSize +
sizeof(Block));
493 refill(p, fallbackHead, NULL);
494 return (
char*)p +
sizeof(Block);
497 if (!head || offset + alignedSize > SourceHeap::AllocSize)
498 refill(SourceHeap::allocate(SourceHeap::AllocSize), head, &offset);
499 char* retval = (
char*)head;
501 offset += alignedSize;
523 template <
typename Derived>
531 Derived* f = ptr.getValue();
546 ptr.unlock_and_set(f);
556 template <
typename Derived>
580 void* ptr = innerHeap.allocate(size);
581 dbg::print(
this,
" PageHeap allocate, ptr = ", ptr);
587 dbg::print(
this,
" PageHeap deallocate ptr = ", ptr);
588 innerHeap.deallocate(ptr);
592 #ifdef GALOIS_FORCE_STANDALONE
593 class SizedHeapFactory :
private boost::noncopyable {
595 typedef MallocHeap SizedHeap;
597 static SizedHeap* getHeapForSize(
const size_t) {
return &
alloc; }
600 static SizedHeap
alloc;
612 static SizedHeap* getHeapForSize(
const size_t);
615 typedef std::map<size_t, SizedHeap*> HeapMap;
616 static thread_local HeapMap* localHeaps;
618 std::list<HeapMap*> allLocalHeaps;
648 if (!heap && size != 0) {
649 fprintf(stderr,
"ERROR: Cannot init a fixed sized heap from "
650 "SizedHeapFactory\n");
651 throw std::bad_alloc();
656 void*
alloc = heap->allocate(size);
657 if (alloc ==
nullptr && size != 0) {
658 fprintf(stderr,
"ERROR: Fixed sized heap allocate called failed\n");
659 throw std::bad_alloc();
667 return heap != rhs.heap;
671 return heap == rhs.heap;
678 ~(
sizeof(double) - 1)
688 return (
char*)(header->get()) + offset;
692 char* realPtr = ((
char*)ptr - offset);
702 template <
typename Ty>
714 template <
typename Other>
720 template <
typename Ty>
722 inline void destruct(
char*)
const {}
723 inline void destruct(
wchar_t*)
const {}
724 template <
typename T>
725 inline void destruct(T* t)
const {
740 template <
class Other>
749 : heap(sizeof(Ty)) {}
755 if (size > max_size())
756 throw std::bad_alloc();
757 return static_cast<pointer>(heap.allocate(
sizeof(Ty)));
762 heap.deallocate(ptr);
765 template <
class U,
class... Args>
767 ::new ((
void*)p) U(std::forward<Args>(args)...);
774 template <
typename T1>
776 return heap != rhs.heap;
779 template <
typename T1>
781 return heap == rhs.heap;
791 static const bool USE_MALLOC_AS_BACKUP =
true;
793 static const size_t LOG2_MIN_SIZE = 3;
794 static const size_t LOG2_MAX_SIZE = 16;
798 std::vector<Heap_ty> heapTable;
800 static inline size_t pow2(
unsigned i) {
return (1U << i); }
802 static unsigned nextLog2(
const size_t allocSize) {
804 unsigned i = LOG2_MIN_SIZE;
806 while (pow2(i) < allocSize) {
818 void populateTable(
void) {
819 assert(heapTable.empty());
822 for (
unsigned i = 0; i <= LOG2_MAX_SIZE; ++i) {
823 heapTable.push_back(Heap_ty(pow2(i)));
827 Pow_2_BlockHeap() noexcept;
830 void* allocateBlock(const
size_t allocSize) {
832 if (allocSize > pow2(LOG2_MAX_SIZE)) {
833 if (USE_MALLOC_AS_BACKUP) {
834 return malloc(allocSize);
836 fprintf(stderr,
"ERROR: block bigger than huge page size requested\n");
837 throw std::bad_alloc();
841 unsigned i = nextLog2(allocSize);
842 assert(i < heapTable.size());
843 return heapTable[i].allocate(pow2(i));
848 if (allocSize > pow2(LOG2_MAX_SIZE)) {
849 if (USE_MALLOC_AS_BACKUP) {
852 fprintf(stderr,
"ERROR: block bigger than huge page size requested\n");
853 throw std::bad_alloc();
856 unsigned i = nextLog2(allocSize);
857 assert(i < heapTable.size());
858 heapTable[i].deallocate(ptr);
863 template <
typename Ty>
866 template <
typename T>
867 static inline void destruct(T* t) {
868 if (!std::is_scalar<T>::value) {
882 template <
class Other>
894 template <
typename U>
903 return static_cast<pointer>(heap->allocateBlock(size *
sizeof(Ty)));
907 heap->deallocateBlock(ptr, len *
sizeof(Ty));
910 template <
class U,
class... Args>
912 ::new ((
void*)p) U(std::forward<Args>(args)...);
919 template <
typename T1>
921 return heap != rhs.
heap;
924 template <
typename T1>
926 return heap == rhs.
heap;
939 template <
typename Other>
946 template <
typename Ty,
typename HeapTy>
949 template <
typename HeapTy>
958 template <
typename Other>
964 template <
typename Ty,
typename HeapTy>
966 inline void destruct(
char*)
const {}
967 inline void destruct(
wchar_t*)
const {}
968 template <
typename T>
969 inline void destruct(T* t)
const {
984 template <
class Other>
1001 if (size > max_size())
1002 throw std::bad_alloc();
1003 return static_cast<pointer>(heap->allocate(size *
sizeof(Ty)));
1012 template <
class U,
class... Args>
1014 ::new ((
void*)p) U(std::forward<Args>(args)...);
1020 return (HeapTy::AllocSize == 0) ? size_t(-1) /
sizeof(Ty)
1021 : HeapTy::AllocSize /
sizeof(Ty);
1024 template <
typename T1,
typename A1>
1026 return heap != rhs.
heap;
1029 template <
typename T1,
typename A1>
1031 return heap == rhs.
heap;
1035 template <
typename T>
1041 template <
class Other>
void * allocate(size_t size, Args &&...args)
Definition: runtime/Mem.h:92
pointer allocate(size_type size)
Definition: runtime/Mem.h:1000
BlockHeap()
Definition: runtime/Mem.h:342
void * allocate(size_t size)
Definition: runtime/Mem.h:168
void printInterleavedStats(int minPages=16 *1024)
Print lines from /proc/pid/numa_maps that contain at least n (non-huge) pages.
void destroy(pointer ptr) const
Definition: runtime/Mem.h:915
Definition: runtime/Mem.h:86
FreeListHeap()
Definition: runtime/Mem.h:205
This is the base source of memory for all allocators.
Definition: runtime/Mem.h:510
void deallocate(pointer ptr, size_type)
Definition: runtime/Mem.h:1006
HeapTy * heap
Definition: runtime/Mem.h:974
ExternalHeapAllocator< Other, HeapTy > other
Definition: runtime/Mem.h:986
static void print(const Args &...)
Definition: gIO.h:123
Definition: runtime/Mem.h:1042
Definition: runtime/Mem.h:524
Maintain a freelist using a lock which doesn't cover SourceHeap.
Definition: runtime/Mem.h:234
void deallocate(void *ptr)
Definition: runtime/Mem.h:96
~SelfLockFreeListHeap()
Definition: runtime/Mem.h:256
const void * const_pointer
Definition: runtime/Mem.h:955
void deallocate(pointer ptr, size_type len)
Definition: runtime/Mem.h:906
Definition: runtime/Mem.h:675
Ty * pointer
Definition: runtime/Mem.h:876
const_pointer address(const_reference val) const
Definition: runtime/Mem.h:752
ptrdiff_t difference_type
Definition: runtime/Mem.h:709
void deallocate(void *)
Definition: runtime/Mem.h:439
SelfLockFreeListHeap()
Definition: runtime/Mem.h:255
void value_type
Definition: runtime/Mem.h:712
Pow_2_BlockAllocator< Other > other
Definition: runtime/Mem.h:884
static SizedHeap * getHeapForSize(const size_t)
[FixedSizeAllocator example]
Definition: Mem.cpp:36
void * pointer
Definition: runtime/Mem.h:935
ptrdiff_t difference_type
Definition: runtime/Mem.h:733
void clear()
Definition: runtime/Mem.h:334
size_type max_size() const noexcept
Definition: runtime/Mem.h:1019
void deallocate(void *ptr)
Definition: runtime/Mem.h:691
Pow_2_BlockAllocator(const Pow_2_BlockAllocator< U > &that) noexcept
Definition: runtime/Mem.h:895
const Ty * const_pointer
Definition: runtime/Mem.h:877
Definition: runtime/Mem.h:291
Definition: runtime/Mem.h:1036
void clear()
Definition: runtime/Mem.h:386
pointer allocate(size_type size)
Definition: runtime/Mem.h:902
void * allocate(size_t size)
Definition: runtime/Mem.h:131
[Example Third Party Allocator]
Definition: runtime/Mem.h:68
T * getLocal()
Definition: PerThreadStorage.h:128
const_pointer address(const_reference val) const
Definition: runtime/Mem.h:998
const Ty & const_reference
Definition: runtime/Mem.h:981
Keep a reference to an external allocator.
Definition: runtime/Mem.h:947
void * allocate(size_t size)
Definition: runtime/Mem.h:684
pointer address(reference val) const
Definition: runtime/Mem.h:898
void * allocate(size_t size)
Definition: runtime/Mem.h:74
const void * const_pointer
Definition: runtime/Mem.h:936
void deallocate(void *ptr)
Definition: runtime/Mem.h:277
bool operator!=(const Pow_2_BlockAllocator< T1 > &rhs) const
Definition: runtime/Mem.h:920
void construct(U *p, Args &&...args) const
Definition: runtime/Mem.h:1013
SerialNumaAllocator()
Definition: runtime/Mem.h:1046
void value_type
Definition: runtime/Mem.h:956
pointer address(reference val) const
Definition: runtime/Mem.h:751
void * allocate(size_t size)
Definition: runtime/Mem.h:258
SerialNumaAllocator< Other > other
Definition: runtime/Mem.h:1043
void pageIn(void *buf, size_t len, size_t stride)
Forces the given block to be paged into physical memory.
BumpHeap()
Definition: runtime/Mem.h:382
Ty & reference
Definition: runtime/Mem.h:980
bool operator==(const ExternalHeapAllocator< T1, A1 > &rhs) const
Definition: runtime/Mem.h:1030
size_type max_size() const noexcept
Definition: runtime/Mem.h:917
Definition: runtime/Mem.h:195
pointer address(reference val) const
Definition: runtime/Mem.h:996
Apply a lock to a heap.
Definition: runtime/Mem.h:106
Ty value_type
Definition: runtime/Mem.h:738
void * allocate(size_t size)
Definition: runtime/Mem.h:112
Definition: runtime/Mem.h:603
static Derived * getInstance(void)
Definition: runtime/Mem.h:530
void * allocate(size_t size)
Definition: runtime/Mem.h:578
void unlock() const
Definition: SimpleLock.h:68
size_t allocSize()
Definition: PageAlloc.cpp:69
Definition: runtime/Mem.h:72
Definition: runtime/Mem.h:883
void deallocate(void *)
Definition: runtime/Mem.h:355
void construct(U *p, Args &&...args) const
Definition: runtime/Mem.h:911
size_t size_type
Definition: runtime/Mem.h:874
size_t size_type
Definition: runtime/Mem.h:952
void construct(U *p, Args &&...args) const
Definition: runtime/Mem.h:766
Ty & reference
Definition: runtime/Mem.h:878
Definition: runtime/Mem.h:785
void deallocateBlock(void *ptr, const size_t allocSize)
Definition: runtime/Mem.h:847
ptrdiff_t difference_type
Definition: runtime/Mem.h:875
bool operator!=(const ExternalHeapAllocator< T1, A1 > &rhs) const
Definition: runtime/Mem.h:1025
ExternalHeapAllocator(HeapTy *a) noexcept
Definition: runtime/Mem.h:989
BumpWithMallocHeap()
Definition: runtime/Mem.h:471
pointer allocate(size_type size)
Definition: runtime/Mem.h:754
ptrdiff_t difference_type
Definition: runtime/Mem.h:977
void pagePoolFree(void *)
Definition: PagePool.cpp:48
void pagePreAlloc(int numpages)
Preallocate numpages large pages for each thread.
void * allocate(size_t size)
Definition: runtime/Mem.h:208
Definition: runtime/Mem.h:985
size_t size_type
Definition: runtime/Mem.h:732
Definition: runtime/Mem.h:864
~FreeListHeap()
Definition: runtime/Mem.h:206
void deallocate(void *ptr)
Definition: runtime/Mem.h:221
unsigned int activeThreads
Definition: Threads.cpp:26
This implements a bump pointer though chunks of memory.
Definition: runtime/Mem.h:360
Definition: runtime/Mem.h:560
void preAlloc_impl(unsigned num)
Memory management functionality.
Definition: PreAlloc.cpp:24
void clear()
Definition: runtime/Mem.h:243
const_pointer address(const_reference val) const
Definition: runtime/Mem.h:900
ptrdiff_t difference_type
Definition: runtime/Mem.h:934
~BlockHeap()
Definition: runtime/Mem.h:346
static OwnerTaggedHeap * owner(void *ptr)
Definition: runtime/Mem.h:179
FixedSizeHeap(size_t size)
Definition: runtime/Mem.h:646
const Ty * const_pointer
Definition: runtime/Mem.h:735
void destroy(pointer ptr) const
Definition: runtime/Mem.h:770
Definition: runtime/Mem.h:332
Definition: runtime/Mem.h:110
ptrdiff_t difference_type
Definition: runtime/Mem.h:953
Main scalable allocator in Galois.
Definition: runtime/Mem.h:642
void deallocate(void *ptr)
Definition: runtime/Mem.h:520
This implements a bump pointer though chunks of memory that falls back to malloc if the source heap c...
Definition: runtime/Mem.h:447
Allow looking up parent heap pointers.
Definition: runtime/Mem.h:164
void * alloc()
Definition: PerThreadStorage.cpp:42
void * allocate(size_t GALOIS_USED_ONLY_IN_DEBUG(size))
Definition: runtime/Mem.h:348
void deallocate(void *ptr)
Definition: runtime/Mem.h:119
void * pointer
Definition: runtime/Mem.h:954
void * pointer
Definition: runtime/Mem.h:710
void destroy(pointer ptr) const
Definition: runtime/Mem.h:1017
Scalable variable-size allocations.
Definition: runtime/Mem.h:637
Maintain a freelist.
Definition: runtime/Mem.h:186
void clear()
Definition: runtime/Mem.h:197
Definition: runtime/Mem.h:127
void * allocate(size_t)
Definition: runtime/Mem.h:518
Ty & reference
Definition: runtime/Mem.h:736
ExternalHeapAllocator(const ExternalHeapAllocator< T1, HeapTy > &rhs) noexcept
Definition: runtime/Mem.h:992
void * allocate(size_t size)
Definition: runtime/Mem.h:394
size_type max_size() const noexcept
Definition: runtime/Mem.h:772
Definition: runtime/Mem.h:129
Ty value_type
Definition: runtime/Mem.h:982
LAptr largeMallocInterleaved(size_t bytes, unsigned numThreads)
Definition: NumaMem.cpp:150
ThreadPrivateHeap< FreeListHeap< BumpHeap< SystemHeap > > > SizedHeap
[FixedSizeAllocator example]
Definition: runtime/Mem.h:609
bool operator!=(const FixedSizeAllocator< T1 > &rhs) const
Definition: runtime/Mem.h:775
ThreadPrivateHeap()
Definition: runtime/Mem.h:88
bool operator==(const Pow_2_BlockAllocator< T1 > &rhs) const
Definition: runtime/Mem.h:925
void lock() const
Definition: SimpleLock.h:55
Pow_2_BlockAllocator< Other > other
Definition: runtime/Mem.h:941
unsigned size() const
Definition: PerThreadStorage.h:159
void deallocate(void *ptr)
Definition: runtime/Mem.h:174
void * pagePoolAlloc()
Low level page pool (individual pages, use largeMalloc for large blocks)
Definition: PagePool.cpp:41
const Ty & const_reference
Definition: runtime/Mem.h:879
void deallocate(void *ptr)
Definition: runtime/Mem.h:76
size_t size_type
Definition: runtime/Mem.h:976
SimpleLock is a spinlock.
Definition: SimpleLock.h:36
size_t size_type
Definition: runtime/Mem.h:933
[Example Third Party Allocator]
Definition: runtime/Mem.h:82
~BumpWithMallocHeap()
Definition: runtime/Mem.h:473
Ty value_type
Definition: runtime/Mem.h:880
void deallocate(pointer ptr, size_type GALOIS_USED_ONLY_IN_DEBUG(len))
Definition: runtime/Mem.h:760
bool operator==(const FixedSizeHeap &rhs) const
Definition: runtime/Mem.h:670
Definition: runtime/Mem.h:741
std::unique_ptr< void, internal::largeFreer > LAptr
Definition: NumaMem.h:39
void clear()
Definition: runtime/Mem.h:98
bool operator!=(const FixedSizeHeap &rhs) const
Definition: runtime/Mem.h:666
void * allocate(size_t size)
Definition: runtime/Mem.h:655
~ThreadPrivateHeap()
Definition: runtime/Mem.h:89
void value_type
Definition: runtime/Mem.h:937
FixedSizeAllocator< Other > other
Definition: runtime/Mem.h:742
static void print(const Args &...args)
Definition: gIO.h:115
const Ty & const_reference
Definition: runtime/Mem.h:737
void * allocate(size_t size, size_t &allocated)
Allocates size bytes but may fail.
Definition: runtime/Mem.h:415
size_t size_type
Definition: runtime/Mem.h:708
FixedSizeAllocator< Other > other
Definition: runtime/Mem.h:716
Ty * pointer
Definition: runtime/Mem.h:734
const Ty * const_pointer
Definition: runtime/Mem.h:979
void pageInReadOnly(void *buf, size_t len, size_t stride)
Forces the given readonly block to be paged into physical memory.
T * getRemote(unsigned int thread)
Definition: PerThreadStorage.h:149
const void * const_pointer
Definition: runtime/Mem.h:711
void deallocate(void *ptr)
Definition: runtime/Mem.h:137
void * allocate(size_t size)
Definition: runtime/Mem.h:488
int numNumaAllocForNode(unsigned nodeid)
Returns total small pages allocated by OS on a NUMA node.
void deallocate(void *)
Definition: runtime/Mem.h:505
Ty * pointer
Definition: runtime/Mem.h:978
void deallocate(void *ptr)
Definition: runtime/Mem.h:585
A fixed size block allocator.
Definition: runtime/Mem.h:703
bool operator==(const FixedSizeAllocator< T1 > &rhs) const
Definition: runtime/Mem.h:780
FixedSizeAllocator() noexcept
Definition: runtime/Mem.h:745
FixedSizeAllocator(const FixedSizeAllocator< U > &) noexcept
Definition: runtime/Mem.h:748
~BumpHeap()
Definition: runtime/Mem.h:384
void construct(pointer ptr, const_reference val) const
Definition: runtime/Mem.h:1008
Pow_2_BlockAllocator() noexcept
Definition: runtime/Mem.h:889
Pow_2_BlockHeap * heap
Definition: runtime/Mem.h:887
ExternalHeapAllocator< Other, HeapTy > other
Definition: runtime/Mem.h:960
Definition: runtime/Mem.h:241
void clear()
Definition: runtime/Mem.h:475
void deallocate(void *ptr)
Definition: runtime/Mem.h:664