利用页描述结构的lru域建立页框到slab描述符和cache描述符的映射,实际就是使lru.next指向cache描述符,lru.prev指向slab描述符
- static void slab_map_pages(struct kmem_cache *cache, struct slab *slab,
- void *addr)
- {
- int nr_pages;
- struct page *page;
- page = virt_to_page(addr);
- nr_pages = 1;
- if (likely(!PageCompound(page)))
- nr_pages <<= cache->gfporder;/*分配给slab的页框数*/
- do {
- page_set_cache(page, cache);/*建立到cache的映射*/
- page_set_slab(page, slab); /*建立到slab的映射*/
- page++;
- } while (--nr_pages);
- }
- static inline void page_set_cache(struct page *page, struct kmem_cache *cache)
- {
- page->lru.next = (struct list_head *)cache;
- }
- static inline void page_set_slab(struct page *page, struct slab *slab)
- {
- page->lru.prev = (struct list_head *)slab;
- }
初始化对象
- static void cache_init_objs(struct kmem_cache *cachep,
- struct slab *slabp)
- {
- int i;
- for (i = 0; i < cachep->num; i++) {
- /*得到第i个对象*/
- void *objp = index_to_obj(cachep, slabp, i);
- #if DEBUG /*Debug相关操作*/
- /* need to poison the objs? */
- if (cachep->flags & SLAB_POISON)
- poison_obj(cachep, objp, POISON_FREE);
- if (cachep->flags & SLAB_STORE_USER)
- *dbg_userword(cachep, objp) = NULL;
- if (cachep->flags & SLAB_RED_ZONE) {
- *dbg_redzone1(cachep, objp) = RED_INACTIVE;
- *dbg_redzone2(cachep, objp) = RED_INACTIVE;
- }
- /*
- * Constructors are not allowed to allocate memory from the same
- * cache which they are a constructor for. Otherwise, deadlock.
- * They must also be threaded.
- */
- if (cachep->ctor && !(cachep->flags & SLAB_POISON))
- cachep->ctor(objp + obj_offset(cachep));
- if (cachep->flags & SLAB_RED_ZONE) {
- if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
- slab_error(cachep, "constructor overwrote the"
- " end of an object");
- if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
- slab_error(cachep, "constructor overwrote the"
- " start of an object");
- }
- if ((cachep->buffer_size % PAGE_SIZE) == 0 &&
- OFF_SLAB(cachep) && cachep->flags & SLAB_POISON)
- kernel_map_pages(virt_to_page(objp),
- cachep->buffer_size / PAGE_SIZE, 0);
- #else
- if (cachep->ctor)/*根据构造函数初始化对象*/
- cachep->ctor(objp);
- #endif
- slab_bufctl(slabp)[i] = i + 1;/*确定下一个空闲对象为后面相邻的对象*/
- }
- slab_bufctl(slabp)[i - 1] = BUFCTL_END;
- }
