1 2 #ifndef __KMEM_H__ 3 #define __KMEM_H__ 4 5 #include <sys/queue.h> 6 #include <sys/spinlock.h> 7 8 typedef struct Page { 9 uint32_t refcount; // Number of references 10 uint32_t pincount; // Pin count (HW, Software) 11 uint32_t flags; // Flags 12 uint32_t _unused; 13 } Page; 14 15 /* 16 * Page Allocator 17 */ 18 void PAlloc_Init(); 19 void PAlloc_AddRegion(uintptr_t start, uintptr_t len); 20 void *PAlloc_AllocPage(); 21 void PAlloc_Retain(void *pg); 22 void PAlloc_Release(void *pg); 23 24 /* 25 * XMem Memory Mapping Region 26 */ 27 typedef struct XMem XMem; 28 29 void XMem_Init(); 30 XMem *XMem_New(); 31 void XMem_Destroy(XMem *xmem); 32 uintptr_t XMem_GetBase(XMem *xmem); 33 uintptr_t XMem_GetLength(XMem *xmem); 34 bool XMem_Allocate(XMem *xmem, uintptr_t length); 35 36 /* 37 * Slab Allocator 38 */ 39 40 #define SLAB_NAMELEN 32 41 42 typedef struct SlabElement { 43 LIST_ENTRY(SlabElement) free; 44 } SlabElement; 45 46 typedef struct Slab { 47 uintptr_t objsz; 48 uintptr_t align; 49 XMem *xmem; 50 Spinlock lock; 51 uint64_t objs; 52 uint64_t freeObjs; 53 LIST_HEAD(SlabElementHead, SlabElement) freeList; 54 // Debugging 55 uint64_t allocs; 56 uint64_t frees; 57 char name[SLAB_NAMELEN]; 58 LIST_ENTRY(Slab) slabList; 59 } Slab; 60 61 void Slab_Init(Slab *slab, const char *name, uintptr_t objsz, uintptr_t align); 62 void *Slab_Alloc(Slab *slab) __attribute__((malloc)); 63 void Slab_Free(Slab *slab, void *obj); 64 65 #define DECLARE_SLAB(_type) \ 66 _type *_type##_Alloc(); \ 67 void _type##_Free(_type *obj); 68 69 #define DEFINE_SLAB(_type, _pool) \ 70 _type *_type##_Alloc() { \ 71 return (_type *)Slab_Alloc(_pool); \ 72 } \ 73 void _type##_Free(_type *obj) { \ 74 Slab_Free(_pool, obj); \ 75 } 76 77 #endif /* __KMEM_H__ */ 78 79