Time to get our hands dirty with some code! Let's look at a simple demonstration of using the preceding APIs to create our very own custom slab cache. As usual, we show only relevant code here. I urge you to clone the book's GitHub repository and try it out yourself! You can find the code for this file at ch9/slab_custom/slab_custom.c.
In our init code path, we first call the following function to create our custom slab cache:
// ch9/slab_custom/slab_custom.c
#define OURCACHENAME "our_ctx"
/* Our 'demo' structure, that (we imagine) is often allocated and freed;
* hence, we create a custom slab cache to hold pre-allocated 'instances'
* of it... Its size: 328 bytes.
*/
struct myctx {
u32 iarr[10];
u64 uarr[10];
char uname[128], passwd[16], config[64];
};
static struct kmem_cache *gctx_cachep;
In the preceding code, we declare a (global) pointer (gctx_cachep) to the to-be-created custom slab cache...