// Override new operator on chunk to use mmap to allocate kChunkSize void* Chunk::operator new(std::size_t count __attribute__((unused))) noexcept { assert(count == sizeof(Chunk)); void* mem = MapAligned(kChunkSize, kChunkSize); if (!mem) { abort(); //throw std::bad_alloc; } return mem; }
// Override new operator on HeapImpl to use mmap to allocate a page void* HeapImpl::operator new(std::size_t count __attribute__((unused))) noexcept { assert(count == sizeof(HeapImpl)); void* mem = MapAligned(kPageSize, kPageSize); if (!mem) { abort(); // throw std::bad_alloc; } heap_count++; return mem; }
void* HeapImpl::MapAlloc(size_t size) { size = (size + kPageSize - 1) & ~(kPageSize - 1); MapAllocation* allocation = reinterpret_cast<MapAllocation*>(AllocLocked(sizeof(MapAllocation))); void* ptr = MapAligned(size, kChunkSize); if (!ptr) { FreeLocked(allocation); abort(); // throw std::bad_alloc; } allocation->ptr = ptr; allocation->size = size; allocation->next = map_allocation_list_; map_allocation_list_ = allocation; return ptr; }