内存管理-SLAB(SLAB的基本数据结构)

本文介绍了slab分配器的基本原理,它是为了解决内碎片问题而引入到Linux操作系统中的。slab分配器采用面向对象思想,将小块内存区域作为对象进行管理,通过cache和slab结构实现内存的有效复用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

slab分配器基本原理:


  slab最初是在Solaris 2.4中引入Linux操作系统的,用于解决内碎片问题。程序经常需要创建一些数据结构,比如进程描述符task_struct,内存描述符mm_struct等。slab分配器把这些需要分配的小块内存区作为对象,类似面向对象的思想。每一类对象分配一个cache,cache有一个或多个slab组成,slab由一个或多个物理页面组成。需要分配对象的时候从slab中空闲的对象取,用完了再放回slab中,而不是释放给物理页分配器,这样下次用的时候就不用重新初始化了,实现了对象的复用。

struct kmem_cache_s {  
/* 1) per-cpu data, touched during every alloc/free */  
    struct array_cache  *array[NR_CPUS];  
    unsigned int        batchcount;  
    unsigned int        limit;  
/* 2) touched by every alloc & free from the backend */  
    struct kmem_list3   lists;  
    /* NUMA: kmem_3list_t   *nodelists[MAX_NUMNODES] */  
    unsigned int        objsize;  
    unsigned int        flags;  /* constant flags */  
    unsigned int        num;    /* # of objs per slab */  
    unsigned int        free_limit; /* upper limit of objects in the lists */  
    spinlock_t      spinlock;  
  
/* 3) cache_grow/shrink */  
    /* order of pgs per slab (2^n) */  
    unsigned int        gfporder;  
  
    /* force GFP flags, e.g. GFP_DMA */  
    unsigned int        gfpflags;  
  
    size_t          colour;     /* cache colouring range */  
    unsigned int        colour_off; /* colour offset */  
    unsigned int        colour_next;    /* cache colouring */  
    kmem_cache_t        *slabp_cache;  
    unsigned int        slab_size;  
    unsigned int        dflags;     /* dynamic flags */  
  
    /* constructor func */  
    void (*ctor)(void *, kmem_cache_t *, unsigned long);  
  
    /* de-constructor func */  
    void (*dtor)(void *, kmem_cache_t *, unsigned long);  
  
/* 4) cache creation/removal */  
    const char      *name;  
    struct list_head    next;  
  
/* 5) statistics */  
#if STATS  
    unsigned long       num_active;  
    unsigned long       num_allocations;  
    unsigned long       high_mark;  
    unsigned long       grown;  
    unsigned long       reaped;  
    unsigned long       errors;  
    unsigned long       max_freeable;  
    unsigned long       node_allocs;  
    atomic_t        allochit;  
    atomic_t        allocmiss;  
    atomic_t        freehit;  
    atomic_t        freemiss;  
#endif  
#if DEBUG  
    int         dbghead;  
    int         reallen;  
#endif  
};  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值