[util] Add util allocator.h interface

The allocator is a interface for types that expose allocator functions
for use in container templates like node_map (usage coming soon).

Also added an implementation for the kernel heap allocator.
This commit is contained in:
Justin C. Miller
2022-10-10 20:54:30 -07:00
parent 48e3f9f9d2
commit ba0ce13fe3
4 changed files with 69 additions and 0 deletions

View File

@@ -8,6 +8,7 @@ module("util",
"spinlock.cpp",
],
public_headers = [
"util/allocator.h",
"util/basic_types.h",
"util/bip_buffer.h",
"util/bitset.h",

View File

@@ -0,0 +1,28 @@
#pragma once
/// \file allocator.h
/// Definition of the allocator interface.
namespace util {
// Allocators are types with three static methods with these signatures
struct default_allocator
{
inline static void * allocate(size_t size) { return new char[size]; }
inline static void free(void *p) { delete [] reinterpret_cast<char*>(p); }
inline static void * realloc(void *p, size_t oldsize, size_t newsize) {
void *newp = memcpy(allocate(newsize), p, oldsize);
free(p);
return newp;
}
};
struct null_allocator
{
inline static void * allocate(size_t size) { return nullptr; }
inline static void free(void *p) {}
inline static void * realloc(void *p, size_t, size_t) { return p; }
};
} // namespace util