[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:
@@ -8,6 +8,7 @@ module("util",
|
||||
"spinlock.cpp",
|
||||
],
|
||||
public_headers = [
|
||||
"util/allocator.h",
|
||||
"util/basic_types.h",
|
||||
"util/bip_buffer.h",
|
||||
"util/bitset.h",
|
||||
|
||||
28
src/libraries/util/util/allocator.h
Normal file
28
src/libraries/util/util/allocator.h
Normal 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
|
||||
Reference in New Issue
Block a user