#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/fs.h>

#define CUTBAG_DIR "CU_T-bagwell"
#define MAX_STRING_TEST 20

static struct kmem_cache *T_bagwell_slab_test;
struct test{
        int count;
        unsigned char test_string[MAX_STRING_TEST];
};

struct test *test_for_cu;
static int __init test_init(void)
{
        T_bagwell_slab_test = kmem_cache_create(CUTBAG_DIR,
                        sizeof(struct test),
                        0, SLAB_RECLAIM_ACCOUNT, NULL
                        );
        if(!T_bagwell_slab_test){
                printk(KERN_WARNING "kmem_cache_create faild\n");
                return -1;
        }

        test_for_cu = kmem_cache_alloc(T_bagwell_slab_test, GFP_KERNEL);
        if(!test_for_cu){
                printk(KERN_WARNING "kmem_cache_alloc faild\n");
                kmem_cache_destroy(T_bagwell_slab_test);
                return -1;
        }

        memset(test_for_cu, 0 ,sizeof(struct test));
        test_for_cu->count = 100;
        memcpy(test_for_cu->test_string, "test_for_chinaunix", MAX_STRING_TEST);
        return 0;
}

static void __exit test_cleanup(void)
{
        printk(KERN_WARNING "%s %d\n", test_for_cu->test_string, test_for_cu->count);
        kmem_cache_free(T_bagwell_slab_test, test_for_cu);
        kmem_cache_destroy(T_bagwell_slab_test);
}


module_init(test_init);
module_exit(test_cleanup);

MODULE_LICENSE("GPL");



KERNELDIR = /lib/modules/`uname -r`/build
PWD := $(shell pwd)
obj-m := slab.o

modules:
        $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
clean:
        rm -rf *.o *.ko Module.* module* *.mod.c


12-26 18:27