zoukankan      html  css  js  c++  java
  • 一个基于 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 量子的相反, 它带来一个不可预测的内存碎片.

  • 相关阅读:
    【经验】AngularJS
    jquery复选框选择 DoTop
    SQL查询数据库名、表名、列名 DoTop
    C#读取配置文件中的信息 DoTop
    ASP.NET获取工程根目录的方法集合 DoTop
    ASP.NET前台Html.DropDownList的使用 DoTop
    JS的同步和异步加载
    tornado nginx 同源(AccessControlAllowOrigin)错误处理记录
    sql join 的一次小使用
    关于CSS3 animation 属性在ie edge浏览器中不能工作
  • 原文地址:https://www.cnblogs.com/fanweisheng/p/11142095.html
Copyright © 2011-2022 走看看