Skip to content

Commit 8fea4e9

Browse files
Pekka EnbergLinus Torvalds
authored andcommitted
[PATCH] slab: object to index mapping cleanup
Clean up the object to index mapping that has been spread around mm/slab.c. Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
1 parent b50ec7d commit 8fea4e9

1 file changed

Lines changed: 23 additions & 11 deletions

File tree

‎mm/slab.c‎

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,18 @@ static inline struct slab *virt_to_slab(const void *obj)
609609
return page_get_slab(page);
610610
}
611611

612+
static inline void *index_to_obj(struct kmem_cache *cache, struct slab *slab,
613+
unsigned int idx)
614+
{
615+
return slab->s_mem + cache->buffer_size * idx;
616+
}
617+
618+
static inline unsigned int obj_to_index(struct kmem_cache *cache,
619+
struct slab *slab, void *obj)
620+
{
621+
return (unsigned)(obj - slab->s_mem) / cache->buffer_size;
622+
}
623+
612624
/* These are the default caches for kmalloc. Custom caches can have other sizes. */
613625
struct cache_sizes malloc_sizes[] = {
614626
#define CACHE(x) { .cs_size = (x) },
@@ -1568,18 +1580,18 @@ static void check_poison_obj(struct kmem_cache *cachep, void *objp)
15681580
* exist:
15691581
*/
15701582
struct slab *slabp = virt_to_slab(objp);
1571-
int objnr;
1583+
unsigned int objnr;
15721584

1573-
objnr = (unsigned)(objp - slabp->s_mem) / cachep->buffer_size;
1585+
objnr = obj_to_index(cachep, slabp, objp);
15741586
if (objnr) {
1575-
objp = slabp->s_mem + (objnr - 1) * cachep->buffer_size;
1587+
objp = index_to_obj(cachep, slabp, objnr - 1);
15761588
realobj = (char *)objp + obj_offset(cachep);
15771589
printk(KERN_ERR "Prev obj: start=%p, len=%d\n",
15781590
realobj, size);
15791591
print_objinfo(cachep, objp, 2);
15801592
}
15811593
if (objnr + 1 < cachep->num) {
1582-
objp = slabp->s_mem + (objnr + 1) * cachep->buffer_size;
1594+
objp = index_to_obj(cachep, slabp, objnr + 1);
15831595
realobj = (char *)objp + obj_offset(cachep);
15841596
printk(KERN_ERR "Next obj: start=%p, len=%d\n",
15851597
realobj, size);
@@ -1598,7 +1610,7 @@ static void slab_destroy_objs(struct kmem_cache *cachep, struct slab *slabp)
15981610
{
15991611
int i;
16001612
for (i = 0; i < cachep->num; i++) {
1601-
void *objp = slabp->s_mem + cachep->buffer_size * i;
1613+
void *objp = index_to_obj(cachep, slabp, i);
16021614

16031615
if (cachep->flags & SLAB_POISON) {
16041616
#ifdef CONFIG_DEBUG_PAGEALLOC
@@ -1631,7 +1643,7 @@ static void slab_destroy_objs(struct kmem_cache *cachep, struct slab *slabp)
16311643
if (cachep->dtor) {
16321644
int i;
16331645
for (i = 0; i < cachep->num; i++) {
1634-
void *objp = slabp->s_mem + cachep->buffer_size * i;
1646+
void *objp = index_to_obj(cachep, slabp, i);
16351647
(cachep->dtor) (objp, cachep, 0);
16361648
}
16371649
}
@@ -2307,7 +2319,7 @@ static void cache_init_objs(struct kmem_cache *cachep,
23072319
int i;
23082320

23092321
for (i = 0; i < cachep->num; i++) {
2310-
void *objp = slabp->s_mem + cachep->buffer_size * i;
2322+
void *objp = index_to_obj(cachep, slabp, i);
23112323
#if DEBUG
23122324
/* need to poison the objs? */
23132325
if (cachep->flags & SLAB_POISON)
@@ -2363,7 +2375,7 @@ static void kmem_flagcheck(struct kmem_cache *cachep, gfp_t flags)
23632375

23642376
static void *slab_get_obj(struct kmem_cache *cachep, struct slab *slabp, int nodeid)
23652377
{
2366-
void *objp = slabp->s_mem + (slabp->free * cachep->buffer_size);
2378+
void *objp = index_to_obj(cachep, slabp, slabp->free);
23672379
kmem_bufctl_t next;
23682380

23692381
slabp->inuse++;
@@ -2380,7 +2392,7 @@ static void *slab_get_obj(struct kmem_cache *cachep, struct slab *slabp, int nod
23802392
static void slab_put_obj(struct kmem_cache *cachep, struct slab *slabp, void *objp,
23812393
int nodeid)
23822394
{
2383-
unsigned int objnr = (unsigned)(objp-slabp->s_mem) / cachep->buffer_size;
2395+
unsigned int objnr = obj_to_index(cachep, slabp, objp);
23842396

23852397
#if DEBUG
23862398
/* Verify that the slab belongs to the intended node */
@@ -2565,10 +2577,10 @@ static void *cache_free_debugcheck(struct kmem_cache *cachep, void *objp,
25652577
if (cachep->flags & SLAB_STORE_USER)
25662578
*dbg_userword(cachep, objp) = caller;
25672579

2568-
objnr = (unsigned)(objp - slabp->s_mem) / cachep->buffer_size;
2580+
objnr = obj_to_index(cachep, slabp, objp);
25692581

25702582
BUG_ON(objnr >= cachep->num);
2571-
BUG_ON(objp != slabp->s_mem + objnr * cachep->buffer_size);
2583+
BUG_ON(objp != index_to_obj(cachep, slabp, objnr));
25722584

25732585
if (cachep->flags & SLAB_DEBUG_INITIAL) {
25742586
/* Need to call the slab's constructor so the

0 commit comments

Comments
 (0)