acadia/zion/memory/slab_allocator.h
Drew Galbraith 659f122c9e [Zion] Free memory that was allocated with the slab allocator.
This reduces the number of active allocs by ~ 200.
Size 8: 142 -> 2
Size 16: 319 > 313
Size 32: 364-> 312
2023-11-15 12:53:14 -08:00

52 lines
1 KiB
C++

#pragma once
#include <glacier/container/intrusive_list.h>
#include <glacier/memory/ref_counted.h>
#include <glacier/status/error.h>
#include <glacier/status/error_or.h>
#include <stdint.h>
#include "memory/paging_util.h"
class Slab : public glcr::RefCounted<Slab>,
public glcr::IntrusiveListNode<Slab> {
public:
explicit Slab(uint64_t elem_size);
Slab(Slab&) = delete;
Slab(Slab&&) = delete;
void* Allocate();
void Free(void* addr);
bool IsFull();
uint64_t Allocations() { return num_allocated_; }
private:
struct FreeListEntry {
FreeListEntry* next;
};
uint64_t elem_size_;
uint64_t num_allocated_;
FreeListEntry* first_free_;
bool IsContained(void* addr);
};
class SlabAllocator {
public:
SlabAllocator(uint64_t elem_size) : elem_size_(elem_size) {}
SlabAllocator(SlabAllocator&) = delete;
glcr::ErrorOr<void*> Allocate();
// Stats:
uint64_t SlabCount();
uint64_t Allocations();
private:
uint64_t elem_size_;
glcr::IntrusiveList<Slab> slabs_;
};
void SlabFree(void* addr);