constexpr std::size_t small_size{ 4036 }; constexpr std::size_t large_size{ 1'000'000 }; struct SmallBucket { const static std::size_t data_size{ small_size }; std::byte data[data_size]; }; struct LargeBucket { using byte = std::byte; const static std::size_t data_size{ large_size }; byte data[data_size]; }; struct Heap { void* allocate(std::size_t bytes) { if (bytes >= 1 && bytes <= SmallBucket::data_size) { for (int i{}; i < n_heap_buckets; ++i) { if (!buckets_used[i]) { buckets_used[i] = true; return large_buckets[i].data; } } } else if (bytes > SmallBucket::data_size && bytes <= LargeBucket::data_size) { for (int i{}; i < n_heap_buckets; ++i) { if (!buckets_used[i]) { buckets_used[i] = true; return small_buckets[i].data; } } } throw std::bad_alloc{}; } void free(void* p) { for (std::size_t i{}; i < n_heap_buckets; ++i) { if (large_buckets[i].data == p) { buckets_used[i] = false; return; } } } const static std::size_t n_heap_buckets{ 10 }; SmallBucket small_buckets[n_heap_buckets]; LargeBucket large_buckets[n_heap_buckets]; bool buckets_used[n_heap_buckets]; }; Heap heap; void* operator new(std::size_t n_bytes) { return heap.allocate(n_bytes); } void operator delete(void* p) { heap.free(p); }