几个涉及到的辅助函数
- /*
- * Transfer objects in one arraycache to another.
- * Locking must be handled by the caller.
- *
- * Return the number of entries transferred.
- */
- static int transfer_objects(struct array_cache *to,
- struct array_cache *from, unsigned int max)
- {
- /* Figure out how many entries to transfer */
- int nr = min(min(from->avail, max), to->limit - to->avail);
- if (!nr)
- return 0;
- /*拷贝*/
- memcpy(to->entry + to->avail, from->entry + from->avail -nr,
- sizeof(void *) *nr);
- /*两边数据更新*/
- from->avail -= nr;
- to->avail += nr;
- to->touched = 1;
- return nr;
- }
- /*从slab中提取一个空闲对象*/
- static void *slab_get_obj(struct kmem_cache *cachep, struct slab *slabp,
- int nodeid)
- {
- /* 获得一个空闲的对象,free是本slab中第一个空闲对象的索引 */
- void *objp = index_to_obj(cachep, slabp, slabp->free);
- kmem_bufctl_t next;
- /* 更新在用对象计数 */
- slabp->inuse++;
- /* 获得下一个空闲对象的索引 */
- next = slab_bufctl(slabp)[slabp->free];
- #if DEBUG
- slab_bufctl(slabp)[slabp->free] = BUFCTL_FREE;
- WARN_ON(slabp->nodeid != nodeid);
- #endif
- /* free指向下一个空闲的对象 */
- slabp->free = next;
- return objp;
- }
- static inline void *index_to_obj(struct kmem_cache *cache, struct slab *slab,
- unsigned int idx)
- { /* s_mem是slab中第一个对象的起始地址,buffer_size是每个对象的大小
- ,这里根据对象索引计算对象的地址 */
- return slab->s_mem + cache->buffer_size * idx;
- }
- static inline kmem_bufctl_t *slab_bufctl(struct slab *slabp)
- {
- return (kmem_bufctl_t *) (slabp + 1);
- }
- static inline struct array_cache *cpu_cache_get(struct kmem_cache *cachep)
- {
- return cachep->array[smp_processor_id()];
- }
