|
PatchworkOS
69292a3
A non-POSIX operating system.
|
Object Cache. More...
Object Cache.
A object cache using CPU local SLAB allocation to improve performance of frequently allocated and deallocated objects.
The object cache uses slab allocation to allocate memory, each slab consists of a buffer in the below format:
| Size | Description |
|---|---|
| sizeof(cache_slab_t) | Slab metadata |
| (N - 1) * sizeof(cache_bufctl_t) | Buffer control list |
| ... | Padding |
| N * step | Objects |
Where N is the number of objects that can fit in the slab given the object size and alignment and the step is the aligned size of the object.
The buffer control list is an alternative to a intrusive free list, which we cant use as we want free objects to remain in an initalized state.
Each index in the buffer control list corresponds to an object in the slab, and contains the index of the next free object, with the last index in the buffer control list being CACHE_BUFCTL_END.
When an object is allocated we say that the next free object is the object att the current first free objects index, and when we free an object we set its index in the buffer control list to the current first free object and then update the first free object to be the freed object.
In short, the buffer control list acts as a LIFO free list for the objects in the slab.
To minimize CPU contention, each CPU has its own active slab from which it allocates and deallocates objects. It will continue using its own slab until its empty, at which point it will try to acquire a new slab from the shared cache.
Initializing objects, as in setting up their inital state, takes time. To avoid paying this cost on every allocation, the cache supports optional constructor and destructor functions. Such that when an object is freed, it remains in its initalized state allowins us to reuse it without reinitialization.
Data Structures | |
| struct | cache_slab_layout_t |
| Cache slab layout structure. More... | |
| struct | cache_t |
| Cache structure. More... | |
| struct | cache_slab_t |
| Cache slab structure. More... | |
| struct | cache_cpu_t |
| Per-CPU cache context. More... | |
Macros | |
| #define | CACHE_LIMIT 16 |
| Maximum number of free slabs in a cache. | |
| #define | CACHE_BUFCTL_END (0) |
| End of buffer control list marker. | |
| #define | CACHE_LINE 64 |
| Cache line size in bytes. | |
| #define | CACHE_SLAB_PAGES 64 |
| Number of pages in a slab. | |
| #define | CACHE_CREATE(_cache, _name, _size, _alignment, _ctor, _dtor) |
| Macro to create a cache initializer. | |
Typedefs | |
| typedef uint16_t | cache_bufctl_t |
| Buffer control type. | |
Functions | |
| struct | ALIGNED (CACHE_LINE) |
| void * | cache_alloc (cache_t *cache) |
| Allocate an object from the cache. | |
| void | cache_free (void *obj) |
| Free an object back to its cache. | |
Variables | |
| cache_slab_t | |
| cache_cpu_t | |
| #define CACHE_LIMIT 16 |
| #define CACHE_BUFCTL_END (0) |
| #define CACHE_CREATE | ( | _cache, | |
| _name, | |||
| _size, | |||
| _alignment, | |||
| _ctor, | |||
| _dtor | |||
| ) |
Macro to create a cache initializer.
| _cache | The name of the cache variable. |
| _name | The name of the cache, for debugging purposes. |
| _size | The size of each object in the cache. |
| _alignment | The alignment of each object in the cache. |
| _ctor | The constructor function for objects in the cache. |
| _dtor | The destructor function for objects in the cache. |
| typedef uint16_t cache_bufctl_t |
| struct ALIGNED | ( | CACHE_LINE | ) |
| void * cache_alloc | ( | cache_t * | cache | ) |
Allocate an object from the cache.
The object will be constructed using the cache's constructor if one is provided.
| cache | The cache to allocate from. |
NULL on failures. Definition at line 109 of file cache.c.
| void cache_free | ( | void * | obj | ) |