一个基于 Slab 缓存的 scull: scullc

是时候给个例子了. scullc 是一个简化的 scull 模块的版本, 它只实现空设备 -- 永久 的内存区. 不象 scull, 它使用 kmalloc, scullc 使用内存缓存. 量子的大小可在编译 时和加载时修改, 但是不是在运行时 -- 这可能需要创建一个新内存区, 并且我们不想处 理这些不必要的细节.

 

scullc 使用一个完整的例子, 可用来试验 slab 分配器. 它区别于 scull 只在几行代码. 首先, 我们必须声明我们自己的 slab 缓存:

 

/* declare one cache pointer: use it for all devices */ kmem_cache_t *scullc_cache;

 

slab 缓存的创建以这样的方式处理( 在模块加载时 ):

 

/* scullc_init: create a cache for our quanta */ scullc_cache = kmem_cache_create("scullc", scullc_quantum,

0, SLAB_HWCACHE_ALIGN, NULL, NULL); /* no

ctor/dtor */

 

if (!scullc_cache)

{

scullc_cleanup(); return -ENOMEM;

 

}

 

这是它如何分配内存量子:

 

/* Allocate a quantum using the memory cache */ if (!dptr->data[s_pos])

{

dptr->data[s_pos] = kmem_cache_alloc(scullc_cache, GFP_KERNEL); if (!dptr->data[s_pos])

 

goto nomem;

memset(dptr->data[s_pos], 0, scullc_quantum);

}

 

还有这些代码行释放内存:

 

for (i = 0; i < qset; i++)

if (dptr->data[i])

kmem_cache_free(scullc_cache, dptr->data[i]); 最后, 在模块卸载时, 我们不得不返回缓存给系统:

/* scullc_cleanup: release the cache of our quanta */ if (scullc_cache)

kmem_cache_destroy(scullc_cache);

 

从 scull 到 scullc 的主要不同是稍稍的速度提升以及更好的内存使用. 因为量子从一 个恰好是合适大小的内存片的池中分配, 它们在内存中的排列是尽可能的密集, 与 scull 量子的相反, 它带来一个不可预测的内存碎片.

posted @ 2019-07-06 11:21  樊伟胜  阅读(249)  评论(0编辑  收藏  举报