无shared local cache,释放对象到slab三链中
- /*
- * Caller needs to acquire correct kmem_list's list_lock
- */
- /*释放一定数目的对象*/
- static void free_block(struct kmem_cache *cachep, void **objpp, int nr_objects,
- int node)
- {
- int i;
- struct kmem_list3 *l3;
- /* 逐一释放对象到slab三链中 */
- for (i = 0; i < nr_objects; i++) {
- void *objp = objpp[i];
- struct slab *slabp;
- /* 通过虚拟地址得到page,再通过page得到slab */
- slabp = virt_to_slab(objp);
- /* 获得slab三链 */
- l3 = cachep->nodelists[node];
- /* 先将对象所在的slab从链表中摘除 */
- list_del(&slabp->list);
- check_spinlock_acquired_node(cachep, node);
- check_slabp(cachep, slabp);
- /* 将对象释放到其slab中 */
- slab_put_obj(cachep, slabp, objp, node);
- STATS_DEC_ACTIVE(cachep);
- /* 空闲对象数加一 */
- l3->free_objects++;
- check_slabp(cachep, slabp);
- /* fixup slab chains */
- if (slabp->inuse == 0) {
- /* 如果slab中均为空闲对象 */
- if (l3->free_objects > l3->free_limit) {
- /* 如果slab三链中空闲对象数超过上限
- ,直接回收整个slab到内存
- ,空闲对象数减去每个slab中对象数 */
- l3->free_objects -= cachep->num;
- /* No need to drop any previously held
- * lock here, even if we have a off-slab slab
- * descriptor it is guaranteed to come from
- * a different cache, refer to comments before
- * alloc_slabmgmt.
- *//* 销毁struct slab对象 */
- slab_destroy(cachep, slabp);
- } else {
- /* 将此slab添加到空slab链表中 */
- list_add(&slabp->list, &l3->slabs_free);
- }
- } else {
- /* Unconditionally move a slab to the end of the
- * partial list on free - maximum time for the
- * other objects to be freed, too.
- *//*将此slab添加到部分满slab链表中*/
- list_add_tail(&slabp->list, &l3->slabs_partial);
- }
- }
- }
将对象释放到其slab中
- static void slab_put_obj(struct kmem_cache *cachep, struct slab *slabp,
- void *objp, int nodeid)
- { /* 获得对象在kmem_bufctl_t数组中的索引 */
- unsigned int objnr = obj_to_index(cachep, slabp, objp);
- #if DEBUG
- /* Verify that the slab belongs to the intended node */
- WARN_ON(slabp->nodeid != nodeid);
- if (slab_bufctl(slabp)[objnr] + 1 <= SLAB_LIMIT + 1) {
- printk(KERN_ERR "slab: double free detected in cache "
- "'%s', objp %p\n", cachep->name, objp);
- BUG();
- }
- #endif
- /*这两步相当于静态链表的插入操作*/
- /* 指向slab中原来的第一个空闲对象 */
- slab_bufctl(slabp)[objnr] = slabp->free;
- /* 释放的对象作为第一个空闲对象 */
- slabp->free = objnr;
- /* 已分配对象数减一 */
- slabp->inuse--;
- }
辅助函数
- /* 通过虚拟地址得到page,再通过page得到slab */
- static inline struct slab *virt_to_slab(const void *obj)
- {
- struct page *page = virt_to_head_page(obj);
- return page_get_slab(page);
- }
- static inline struct slab *page_get_slab(struct page *page)
- {
- BUG_ON(!PageSlab(page));
- return (struct slab *)page->lru.prev;
- }
可见,用page->lru.prev得到slab,和创建slab时相呼应。
