MentOS
0.8.0
The Mentoring Operating System
|
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_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. 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_t * | malloc_blocks [MAX_KMALLOC_CACHE_ORDER] |
Array of slab caches for different orders of kmalloc. | |
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.
#define ADDR_FROM_KMEM_OBJ | ( | object | ) | ((void *)(object)) |
#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.
#define KMEM_OBJ_FROM_ADDR | ( | addr | ) | ((kmem_obj_t *)(addr)) |
#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.
#define KMEM_START_OBJ_COUNT 8 |
Initial object count for each slab.
The starting number of objects in a newly allocated slab cache.
#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 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.
|
static |
Allocates and initializes a new slab page for a memory cache.
cachep | Pointer to the memory cache (kmem_cache_t ) for which a new slab page is being allocated. |
flags | Allocation flags (e.g., GFP_KERNEL) passed to control memory allocation behavior. |
|
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.
cachep | Pointer to the memory cache (kmem_cache_t ) whose size and order are being computed. |
|
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.
cachep | Pointer to the cache from which the object is being allocated. |
slab_page | Pointer to the slab page from which to allocate the object. |
|
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.
cachep | Pointer to the memory cache structure to initialize. |
name | Name of the cache. |
size | Size of the objects to be allocated from the cache. |
align | Alignment requirement for the objects. |
flags | Slab allocation flags. |
ctor | Constructor function to initialize objects (optional, can be NULL). |
dtor | Destructor function to clean up objects (optional, can be NULL). |
start_count | Initial number of objects to populate in the cache. |
|
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.
cachep | Pointer to the cache from which the slab page is being freed. |
slab_page | Pointer to the slab page to be freed. |
|
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.
cachep | Pointer to the memory cache (kmem_cache_t ) that needs to be refilled. |
free_num | The desired number of free objects in the cache. |
flags | Allocation flags used for controlling memory allocation behavior (e.g., GFP_KERNEL). |
void kfree | ( | void * | ptr | ) |
Frees memory allocated by kmalloc or kmem_cache_alloc.
ptr | Pointer to the memory to free. |
void* kmalloc | ( | unsigned int | size | ) |
Allocates memory of the specified size using kmalloc.
size | Size of the memory to allocate. |
void* kmem_cache_alloc | ( | kmem_cache_t * | cachep, |
gfp_t | flags | ||
) |
Allocates an object from the specified kmem_cache_t.
cachep | Pointer to the cache from which to allocate the object. |
flags | Flags for the allocation (e.g., GFP_KERNEL). |
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.
name | Name of the cache. |
size | Size of each object in the cache. |
align | Alignment requirement for objects in the cache. |
flags | Flags for slab allocation. |
ctor | Constructor function for initializing objects (can be NULL). |
dtor | Destructor function for cleaning up objects (can be NULL). |
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.
cachep | Pointer to the kmem_cache_t structure to destroy. |
void kmem_cache_free | ( | void * | addr | ) |
Frees an object previously allocated from a kmem_cache_t.
addr | Pointer to the object to free. |
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.