56 return _aligned_malloc(size, alignment);
58 return std::aligned_alloc(alignment, size);
101 void *ptr = VirtualAlloc(
nullptr, size, MEM_RESERVE | MEM_COMMIT | MEM_LARGE_PAGES, PAGE_READWRITE);
106 void *ptr = mmap(
nullptr, size, PROT_READ | PROT_WRITE,
107 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
108 if (ptr == MAP_FAILED)
114 madvise(ptr, size, MADV_HUGEPAGE);
129 VirtualFree(ptr, 0, MEM_RELEASE);
147 size_t capacity_bytes;
157 bool used_huge_pages;
175 explicit MemoryArena(
size_t total_floats,
bool use_huge_pages =
false)
176 : used_huge_pages(false)
179 capacity_bytes = (total_floats *
sizeof(float) + 63) & ~63;
183 bool succeeded =
false;
185 used_huge_pages = succeeded;
188 if (!used_huge_pages)
200 throw std::bad_alloc();
242 size_t allocation_size = (num_floats *
sizeof(float) + 63) & ~63;
244 if (offset_bytes + allocation_size > capacity_bytes)
246 throw std::runtime_error(
"Fatal: MemoryArena capacity exceeded during allocation.");
249 float *chunk =
reinterpret_cast<float *
>(
250 reinterpret_cast<char *
>(base_ptr) + offset_bytes);
252 offset_bytes += allocation_size;
size_t GetUsedBytes() const
Returns how many bytes have been allocated from the arena so far.
Definition MemoryArena.h:259
MemoryArena(const MemoryArena &)=delete
Deleted: MemoryArena owns a single aligned allocation, so copying would risk a double-free.
~MemoryArena()
Frees the underlying allocation, via whichever deallocator matches the allocator actually used (see u...
Definition MemoryArena.h:208
MemoryArena(size_t total_floats, bool use_huge_pages=false)
Allocates a single 64-byte-aligned buffer large enough to hold total_floats floats,...
Definition MemoryArena.h:175
bool UsedHugePages() const
Returns whether this arena's memory is actually huge-page-backed. Since huge pages are requested on a...
Definition MemoryArena.h:270
MemoryArena & operator=(const MemoryArena &)=delete
Deleted: MemoryArena owns a single aligned allocation, so copy-assignment would risk a double-free.
size_t GetCapacityBytes() const
Returns the arena's total capacity.
Definition MemoryArena.h:262
float * AllocateFloats(size_t num_floats)
Allocates a 64-byte aligned chunk of floats from the arena.
Definition MemoryArena.h:239
Definition MemoryArena.h:33
void * HugePageAllocPortable(size_t size, bool &succeeded)
Attempts to allocate size bytes backed by huge pages. This is genuinely opt-in and best-effort: on Li...
Definition MemoryArena.h:97
void HugePageFreePortable(void *ptr, size_t size)
Frees a block previously returned by HugePageAllocPortable() with succeeded=true.
Definition MemoryArena.h:126
void AlignedFreePortable(void *ptr)
Frees a block previously returned by AlignedAllocPortable().
Definition MemoryArena.h:66
void * AlignedAllocPortable(size_t alignment, size_t size)
Allocates a block of memory aligned to alignment bytes, using whichever aligned-allocation API is act...
Definition MemoryArena.h:53
This file exists to provide both Tensor.h and IComputeBackend.h with device types without introducing...
Definition Backend.cpp:10