MentOS  0.8.0
The Mentoring Operating System
slab.h
Go to the documentation of this file.
1 
6 #pragma once
7 
8 #include "sys/list_head.h"
9 #include "stddef.h"
10 #include "mem/gfp.h"
11 
13 typedef unsigned int slab_flags_t;
14 
16 typedef void (*kmem_fun_t)(void *);
17 
19 #define KMEM_CREATE(objtype) \
20  kmem_cache_create(#objtype, sizeof(objtype), alignof(objtype), GFP_KERNEL, NULL, NULL)
21 
23 #define KMEM_CREATE_CTOR(objtype, ctor) \
24  kmem_cache_create(#objtype, sizeof(objtype), alignof(objtype), GFP_KERNEL, (kmem_fun_t)(ctor), NULL)
25 
27 typedef struct kmem_cache_t {
29  list_head cache_list;
31  const char *name;
33  unsigned int size;
35  unsigned int object_size;
37  unsigned int align;
39  unsigned int total_num;
41  unsigned int free_num;
45  unsigned int gfp_order;
51  list_head slabs_full;
53  list_head slabs_partial;
55  list_head slabs_free;
57 
64 int kmem_cache_init(void);
65 
79  const char *name,
80  unsigned int size,
81  unsigned int align,
82  slab_flags_t flags,
83  kmem_fun_t ctor,
84  kmem_fun_t dtor);
85 
92 int kmem_cache_destroy(kmem_cache_t *cachep);
93 
94 #ifdef ENABLE_CACHE_TRACE
95 
103 void *pr_kmem_cache_alloc(const char *file, const char *fun, int line, kmem_cache_t *cachep, gfp_t flags);
104 
110 void pr_kmem_cache_free(const char *file, const char *fun, int line, void *addr);
111 
113 #define kmem_cache_alloc(...) pr_kmem_cache_alloc(__FILE__, __func__, __LINE__, __VA_ARGS__)
114 
116 #define kmem_cache_free(...) pr_kmem_cache_free(__FILE__, __func__, __LINE__, __VA_ARGS__)
117 
118 #else
119 
124 void *kmem_cache_alloc(kmem_cache_t *cachep, gfp_t flags);
125 
128 void kmem_cache_free(void *addr);
129 
130 #endif
131 
132 #ifdef ENABLE_ALLOC_TRACE
133 
140 void *pr_kmalloc(const char *file, const char *fun, int line, unsigned int size);
141 
147 void pr_kfree(const char *file, const char *fun, int line, void *addr);
148 
150 #define kmalloc(...) pr_kmalloc(__FILE__, __func__, __LINE__, __VA_ARGS__)
151 
153 #define kfree(...) pr_kfree(__FILE__, __func__, __LINE__, __VA_ARGS__)
154 
155 #else
156 
160 void *kmalloc(unsigned int size);
161 
164 void kfree(void *ptr);
165 
166 #endif
List of Get Free Pages (GFP) Flags.
unsigned int gfp_t
Type used for GFP_FLAGS.
Definition: gfp.h:9
void kmem_cache_free(void *addr)
Frees an object previously allocated from a kmem_cache_t.
Definition: slab.c:539
void kfree(void *ptr)
Frees memory allocated by kmalloc or kmem_cache_alloc.
Definition: slab.c:616
void * kmem_cache_alloc(kmem_cache_t *cachep, gfp_t flags)
Allocates an object from the specified kmem_cache_t.
Definition: slab.c:474
void * kmalloc(unsigned int size)
Allocates memory of the specified size using kmalloc.
Definition: slab.c:588
struct kmem_cache_t kmem_cache_t
Stores the information of a cache.
int kmem_cache_init(void)
Initializes the kernel memory cache system.
Definition: slab.c:370
void(* kmem_fun_t)(void *)
Type of function used as constructor/destructor for cache creation and destruction.
Definition: slab.h:16
unsigned int slab_flags_t
Type for slab flags.
Definition: slab.h:13
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.
Definition: slab.c:409
int kmem_cache_destroy(kmem_cache_t *cachep)
Destroys a specified kmem_cache structure.
Definition: slab.c:437
Define basic data types.
Stores the information of a cache.
Definition: slab.h:27
list_head slabs_partial
Handler for the partial slabs list.
Definition: slab.h:53
unsigned int object_size
Size of the objects contained in the cache.
Definition: slab.h:35
unsigned int free_num
The number of free slabs.
Definition: slab.h:41
unsigned int gfp_order
The order for getting free pages.
Definition: slab.h:45
kmem_fun_t dtor
Destructor for the elements.
Definition: slab.h:49
unsigned int size
Size of the cache.
Definition: slab.h:33
list_head slabs_full
Handler for the full slabs list.
Definition: slab.h:51
unsigned int align
Alignment requirement of the type of objects.
Definition: slab.h:37
slab_flags_t flags
The Get Free Pages (GFP) flags.
Definition: slab.h:43
list_head cache_list
Handler for placing it inside a lists of caches.
Definition: slab.h:29
unsigned int total_num
The total number of slabs.
Definition: slab.h:39
list_head slabs_free
Handler for the free slabs list.
Definition: slab.h:55
const char * name
Name of the cache.
Definition: slab.h:31
kmem_fun_t ctor
Constructor for the elements.
Definition: slab.h:47