Skip to content

Commit

Permalink
Add SlabAllocator to runtime library
Browse files Browse the repository at this point in the history
  • Loading branch information
mempler committed Dec 7, 2023
1 parent 82451a1 commit 912b324
Show file tree
Hide file tree
Showing 6 changed files with 924 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/runtime/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ cc_library(
srcs = [
"source/Memory/BumpAllocator.c",
"source/Memory/Common.c",
"source/Memory/SlabAllocator.c",
"source/Memory/SlabAllocatorUtilities.c",
"source/Memory/SlabAllocatorUtilities.h",
],
hdrs = [
"include/RuntimeLib.h",
"include/RuntimeLib/Error.h",
"include/RuntimeLib/GUID.h",
"include/RuntimeLib/Memory.h",
"include/RuntimeLib/Memory/BumpAllocator.h",
"include/RuntimeLib/Memory/SlabAllocator.h",
"include/RuntimeLib/TypeDefs.h",
],
strip_include_prefix = "include",
Expand Down
1 change: 1 addition & 0 deletions lib/runtime/include/RuntimeLib/Memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,5 +198,6 @@ RtlZeroMemory (
// --------------------------------------------------------------- Sub-includes

#include "Memory/BumpAllocator.h"
#include "Memory/SlabAllocator.h"

EXTERN_C_END
149 changes: 149 additions & 0 deletions lib/runtime/include/RuntimeLib/Memory/SlabAllocator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
#pragma once

#ifndef __RUNTIME_LIB_H__
#error "This file should be included from RuntimeLib.h"
#endif

#define SLAB_CACHE_FLAGS_NONE BIT(0)
#define SLAB_CACHE_FLAGS_ZERO BIT(1)

typedef struct _SLAB_CACHE SLAB_CACHE;

typedef
STATUS
(* SLAB_CTOR)(
IN VOID* Object,
IN SLAB_CACHE* Cache,
IN OPTIONAL UINTN Flags
);

typedef
STATUS
(* SLAB_DTOR)(
IN VOID* Object,
IN SLAB_CACHE* Cache,
IN OPTIONAL UINTN Flags
);

typedef struct _SLAB {
struct _SLAB* Next;
struct _SLAB* Prev;
struct _SLAB_CACHE* Cache;

CHAR8* FreeMap;
CHAR8* FreeMapEnd;
UINTN FreeCount;

VOID* Data;
VOID* DataEnd;
} SLAB;

typedef struct _SLAB_CACHE {
struct _SLAB* Full;
struct _SLAB* Partial;
struct _SLAB* Empty;

SLAB_CTOR Ctor;
SLAB_DTOR Dtor;

UINTN Flags;

UINTN ObjectSize; // Size of the objects to be allocated
UINTN ObjectCount; // Number of objects per slab

PAGE_ALLOCATOR* BaseAllocator;
} SLAB_CACHE;

STATUS
SYSAPI
RtlDefaultSlabCtor (
IN VOID* Object,
IN SLAB_CACHE* Cache,
IN OPTIONAL UINTN Flags
);

STATUS
SYSAPI
RtlDefaultSlabDtor (
IN VOID* Object,
IN SLAB_CACHE* Cache,
IN OPTIONAL UINTN Flags
);

/**
* @brief Create a slab cache
*
* @param[out] Cache The cache to initialize
* @param[in] BaseAllocator The base allocator to use
* @param[in] ObjectSize The size of the objects to be allocated
* @param[in,optional] Flags The flags for the cache
* @param[in,optional] Ctor The constructor for the objects
* @param[in,optional] Dtor The destructor for the objects
*
* @return STATUS_SUCCESS The initialization was successful
* @return STATUS_INVALID_PARAMETER Cache is NULL or ObjectSize is 0
* @return STATUS_NOT_INITIALIZED The Page Allocator is not initialized
**/
STATUS
SYSAPI
RtlCreateSlabCache (
OUT SLAB_CACHE** Cache,
IN PAGE_ALLOCATOR* BaseAllocator,
IN UINTN ObjectSize,
IN OPTIONAL UINTN Flags,
IN OPTIONAL SLAB_CTOR Ctor,
IN OPTIONAL SLAB_DTOR Dtor
);

/**
* @brief Destroy a slab cache
*
* @param[in,out] Cache The cache to destroy
*
* @return STATUS_SUCCESS The destruction was successful
* @return STATUS_INVALID_PARAMETER Cache is NULL
* @return STATUS_NOT_INITIALIZED The Page Allocator is not initialized
**/
STATUS
SYSAPI
RtlDestroySlabCache (
IN OUT SLAB_CACHE** Cache
);

/**
* @brief Allocate an object from a slab cache
*
* @param[out] Object The object to allocate
* @param[in] Cache The cache to allocate from
* @param[in] Flags The flags for the allocation
*
* @return STATUS_SUCCESS The allocation was successful
* @return STATUS_INVALID_PARAMETER Object or Cache is NULL
*
**/
STATUS
SYSAPI
RtlSlabAllocate (
IN SLAB_CACHE* Cache,
IN UINTN Flags,
OUT VOID** Object
);

/**
* @brief Free an object from a slab cache
*
* @param[in] Cache The cache to free from
* @param[in] Flags The flags for the free
* @param[in,out] Object The object to free
*
* @return STATUS_SUCCESS The free was successful
* @return STATUS_INVALID_PARAMETER Object or Cache is NULL
* @return STATUS_NOT_FOUND The object was not found in the cache
**/
STATUS
SYSAPI
RtlSlabFree (
IN SLAB_CACHE* Cache,
IN UINTN Flags,
IN OUT VOID** Object
);
Loading

0 comments on commit 912b324

Please sign in to comment.