-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsample.c
49 lines (37 loc) · 1 KB
/
sample.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
* This is a sample code for using slab memory allocator, briefly demo
* how to use it.
* Author: Ethan Huang (huangyitian@gwu.edu)
*/
#include "slab.h"
#define ECHO(x) printf(x);printf("\n");
int main()
{
/* Address of the memory you allocate */
void *p;
kmem_cache_t *cc;
/* initialize the cache starts from 4, 4*2, 4*2^2, ..., 4*2^20 */
kmem_cache_init(2, 4, 4*1024*1024);
/* create the object with name 'a', size '4' and no align */
cc = kmem_cache_create("a", 4, 0);
/* allocate for memory */
p = kmem_cache_alloc(cc, 0);
/* statistic of cache after allocating memory
* 1 refers to the first cache, it may be different depending
* on which size of cache you used above.
* 0 refers to all caches.
*/
ECHO("********* After allocation *********");
statistic(1);
ECHO("");
/*
* use memory here
*/
/* free memory */
kmem_cache_free(cc,p);
/* statistic of cache after freeing memory */
ECHO("********* After free *********");
statistic(1);
ECHO("");
return 0;
}