MentOS  0.8.0
The Mentoring Operating System
Classes | Macros | Typedefs | Functions | Variables
slab.c File Reference

Memory slab allocator implementation in kernel. This file provides functions for managing memory allocation using the slab allocator technique. Slab allocators are efficient in managing frequent small memory allocations with minimal fragmentation. More...

Classes

struct  kmem_obj
 Structure to represent an individual memory object within a slab. More...
 

Macros

#define __DEBUG_HEADER__   "[SLAB ]"
 Change header.
 
#define __DEBUG_LEVEL__   LOGLEVEL_NOTICE
 Set log level.
 
#define MAX_KMALLOC_CACHE_ORDER   12
 Maximum order of kmalloc cache allocations. More...
 
#define KMEM_OBJ_OVERHEAD   sizeof(kmem_obj_t)
 Overhead size for each memory object in the slab cache. More...
 
#define KMEM_START_OBJ_COUNT   8
 Initial object count for each slab. More...
 
#define KMEM_MAX_REFILL_OBJ_COUNT   64
 Maximum number of objects to refill in a slab cache at once. More...
 
#define KMEM_OBJ_FROM_ADDR(addr)   ((kmem_obj_t *)(addr))
 Macro to convert an address into a kmem_obj pointer. More...
 
#define ADDR_FROM_KMEM_OBJ(object)   ((void *)(object))
 Macro to get the address from a kmem_obj structure. More...
 

Typedefs

typedef struct kmem_obj kmem_obj_t
 Structure to represent an individual memory object within a slab. More...
 

Functions

static int __alloc_slab_page (kmem_cache_t *cachep, gfp_t flags)
 Allocates and initializes a new slab page for a memory cache. More...
 
static int __kmem_cache_refill (kmem_cache_t *cachep, unsigned int free_num, gfp_t flags)
 Refills a memory cache with new slab pages to reach a specified number of free objects. More...
 
static int __compute_size_and_order (kmem_cache_t *cachep)
 Computes and sets the size and gfp order for a memory cache. More...
 
static int __kmem_cache_create (kmem_cache_t *cachep, const char *name, unsigned int size, unsigned int align, slab_flags_t flags, kmem_fun_t ctor, kmem_fun_t dtor, unsigned int start_count)
 Initializes and creates a new memory cache. More...
 
static void * __kmem_cache_alloc_slab (kmem_cache_t *cachep, page_t *slab_page)
 Allocates an object from a specified slab page. More...
 
static int __kmem_cache_free_slab (kmem_cache_t *cachep, page_t *slab_page)
 Frees a slab page and updates the associated cache statistics. More...
 
int kmem_cache_init (void)
 Initializes the kernel memory cache system. More...
 
kmem_cache_tkmem_cache_create (const char *name, unsigned int size, unsigned int align, slab_flags_t flags, kmem_fun_t ctor, kmem_fun_t dtor)
 Creates a new kmem_cache structure. More...
 
int kmem_cache_destroy (kmem_cache_t *cachep)
 Destroys a specified kmem_cache structure. More...
 
void * kmem_cache_alloc (kmem_cache_t *cachep, gfp_t flags)
 Allocates an object from the specified kmem_cache_t. More...
 
void kmem_cache_free (void *ptr)
 Frees an object previously allocated from a kmem_cache_t. More...
 
void * kmalloc (unsigned int size)
 Allocates memory of the specified size using kmalloc. More...
 
void kfree (void *ptr)
 Frees memory allocated by kmalloc or kmem_cache_alloc. More...
 

Variables

static list_head kmem_caches_list
 List of all active memory caches in the system.
 
static kmem_cache_t kmem_cache
 Cache used for managing metadata about the memory caches themselves.
 
static kmem_cache_tmalloc_blocks [MAX_KMALLOC_CACHE_ORDER]
 Array of slab caches for different orders of kmalloc.
 

Detailed Description

Memory slab allocator implementation in kernel. This file provides functions for managing memory allocation using the slab allocator technique. Slab allocators are efficient in managing frequent small memory allocations with minimal fragmentation.

Macro Definition Documentation

◆ ADDR_FROM_KMEM_OBJ

#define ADDR_FROM_KMEM_OBJ (   object)    ((void *)(object))

Macro to get the address from a kmem_obj structure.

Parameters
objectPointer to the kmem_obj structure.
Returns
Address of the object as a void *.

◆ KMEM_MAX_REFILL_OBJ_COUNT

#define KMEM_MAX_REFILL_OBJ_COUNT   64

Maximum number of objects to refill in a slab cache at once.

This defines the upper limit on how many objects to replenish in the slab when it runs out of free objects.

◆ KMEM_OBJ_FROM_ADDR

#define KMEM_OBJ_FROM_ADDR (   addr)    ((kmem_obj_t *)(addr))

Macro to convert an address into a kmem_obj pointer.

Parameters
addrAddress of the object.
Returns
Pointer to a kmem_obj structure.

◆ KMEM_OBJ_OVERHEAD

#define KMEM_OBJ_OVERHEAD   sizeof(kmem_obj_t)

Overhead size for each memory object in the slab cache.

This defines the extra space required for managing the object, including the kmem_obj structure itself.

◆ KMEM_START_OBJ_COUNT

#define KMEM_START_OBJ_COUNT   8

Initial object count for each slab.

The starting number of objects in a newly allocated slab cache.

◆ MAX_KMALLOC_CACHE_ORDER

#define MAX_KMALLOC_CACHE_ORDER   12

Maximum order of kmalloc cache allocations.

If a requested memory allocation exceeds this order, a raw page allocation is done instead of using the slab cache.

Typedef Documentation

◆ kmem_obj_t

typedef struct kmem_obj kmem_obj_t

Structure to represent an individual memory object within a slab.

This structure is used to manage individual objects allocated from the slab. It contains a linked list to connect objects in the cache.

Function Documentation

◆ __alloc_slab_page()

static int __alloc_slab_page ( kmem_cache_t cachep,
gfp_t  flags 
)
static

Allocates and initializes a new slab page for a memory cache.

Parameters
cachepPointer to the memory cache (kmem_cache_t) for which a new slab page is being allocated.
flagsAllocation flags (e.g., GFP_KERNEL) passed to control memory allocation behavior.
Returns
0 on success, -1 on failure.

◆ __compute_size_and_order()

static int __compute_size_and_order ( kmem_cache_t cachep)
static

Computes and sets the size and gfp order for a memory cache.

This function adjusts the size of objects in the cache based on padding and alignment requirements, and calculates the gfp_order (number of contiguous pages) needed for slab allocations.

Parameters
cachepPointer to the memory cache (kmem_cache_t) whose size and order are being computed.
Returns
0 on success, -1 on failure.

◆ __kmem_cache_alloc_slab()

static void* __kmem_cache_alloc_slab ( kmem_cache_t cachep,
page_t slab_page 
)
inlinestatic

Allocates an object from a specified slab page.

This function retrieves a free object from the given slab page's free list. It decrements the count of free objects in both the slab page and the cache. If the constructor function is defined, it will be called to initialize the object.

Parameters
cachepPointer to the cache from which the object is being allocated.
slab_pagePointer to the slab page from which to allocate the object.
Returns
Pointer to the allocated object, or NULL if allocation fails.

◆ __kmem_cache_create()

static int __kmem_cache_create ( kmem_cache_t cachep,
const char *  name,
unsigned int  size,
unsigned int  align,
slab_flags_t  flags,
kmem_fun_t  ctor,
kmem_fun_t  dtor,
unsigned int  start_count 
)
static

Initializes and creates a new memory cache.

This function sets up a new memory cache (kmem_cache_t) with the provided parameters such as object size, alignment, constructor/destructor functions, and flags. It also initializes slab lists, computes the appropriate size and order, refills the cache with objects, and adds it to the global cache list.

Parameters
cachepPointer to the memory cache structure to initialize.
nameName of the cache.
sizeSize of the objects to be allocated from the cache.
alignAlignment requirement for the objects.
flagsSlab allocation flags.
ctorConstructor function to initialize objects (optional, can be NULL).
dtorDestructor function to clean up objects (optional, can be NULL).
start_countInitial number of objects to populate in the cache.
Returns
0 on success, -1 on failure.

◆ __kmem_cache_free_slab()

static int __kmem_cache_free_slab ( kmem_cache_t cachep,
page_t slab_page 
)
inlinestatic

Frees a slab page and updates the associated cache statistics.

This function updates the total and free object counts in the cache and resets the slab page's metadata to indicate that it is no longer in use. It also frees the memory associated with the slab page.

Parameters
cachepPointer to the cache from which the slab page is being freed.
slab_pagePointer to the slab page to be freed.
Returns
Returns 0 on success, or -1 if an error occurs.

◆ __kmem_cache_refill()

static int __kmem_cache_refill ( kmem_cache_t cachep,
unsigned int  free_num,
gfp_t  flags 
)
static

Refills a memory cache with new slab pages to reach a specified number of free objects.

This function allocates new slab pages as needed until the cache has at least free_num free objects. If a page allocation fails, the refill process is aborted.

Parameters
cachepPointer to the memory cache (kmem_cache_t) that needs to be refilled.
free_numThe desired number of free objects in the cache.
flagsAllocation flags used for controlling memory allocation behavior (e.g., GFP_KERNEL).
Returns
0 on success, -1 on failure.

◆ kfree()

void kfree ( void *  ptr)

Frees memory allocated by kmalloc or kmem_cache_alloc.

Parameters
ptrPointer to the memory to free.

◆ kmalloc()

void* kmalloc ( unsigned int  size)

Allocates memory of the specified size using kmalloc.

Parameters
sizeSize of the memory to allocate.
Returns
Pointer to the allocated memory, or NULL if allocation fails.

◆ kmem_cache_alloc()

void* kmem_cache_alloc ( kmem_cache_t cachep,
gfp_t  flags 
)

Allocates an object from the specified kmem_cache_t.

Parameters
cachepPointer to the cache from which to allocate the object.
flagsFlags for the allocation (e.g., GFP_KERNEL).
Returns
Pointer to the allocated object, or NULL if allocation fails.

◆ kmem_cache_create()

kmem_cache_t* kmem_cache_create ( const char *  name,
unsigned int  size,
unsigned int  align,
slab_flags_t  flags,
kmem_fun_t  ctor,
kmem_fun_t  dtor 
)

Creates a new kmem_cache structure.

This function allocates memory for a new cache and initializes it with the provided parameters. The cache is ready for use after this function returns.

Parameters
nameName of the cache.
sizeSize of each object in the cache.
alignAlignment requirement for objects in the cache.
flagsFlags for slab allocation.
ctorConstructor function for initializing objects (can be NULL).
dtorDestructor function for cleaning up objects (can be NULL).
Returns
Pointer to the newly created kmem_cache_t, or NULL if allocation fails.

◆ kmem_cache_destroy()

int kmem_cache_destroy ( kmem_cache_t cachep)

Destroys a specified kmem_cache structure.

This function cleans up and frees all memory associated with the specified cache, including all associated slab pages. After calling this function, the cache should no longer be used.

Parameters
cachepPointer to the kmem_cache_t structure to destroy.
Returns
Returns 0 on success, or -1 if an error occurs.

◆ kmem_cache_free()

void kmem_cache_free ( void *  addr)

Frees an object previously allocated from a kmem_cache_t.

Parameters
addrPointer to the object to free.

◆ kmem_cache_init()

int kmem_cache_init ( void  )

Initializes the kernel memory cache system.

This function initializes the global cache list and creates the main cache for managing kmem_cache_t structures. It also creates caches for different order sizes for kmalloc allocations.

Note
This function should be called during system initialization.
Returns
Returns 0 on success, or -1 if an error occurs.