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

Functions and structures for managing memory slabs. More...

Go to the source code of this file.

Classes

struct  kmem_cache_t
 Stores the information of a cache. More...
 

Macros

#define KMEM_CREATE(objtype)    kmem_cache_create(#objtype, sizeof(objtype), alignof(objtype), GFP_KERNEL, NULL, NULL)
 Create a new cache.
 
#define KMEM_CREATE_CTOR(objtype, ctor)    kmem_cache_create(#objtype, sizeof(objtype), alignof(objtype), GFP_KERNEL, (kmem_fun_t)(ctor), NULL)
 Creates a new cache and allows to specify the constructor.
 

Typedefs

typedef unsigned int slab_flags_t
 Type for slab flags.
 
typedef void(* kmem_fun_t) (void *)
 Type of function used as constructor/destructor for cache creation and destruction.
 
typedef struct kmem_cache_t kmem_cache_t
 Stores the information of a cache.
 

Functions

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 *addr)
 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...
 

Detailed Description

Functions and structures for managing memory slabs.

Function Documentation

◆ 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.