]> www.infradead.org Git - users/jedix/linux-maple.git/commitdiff
maple_tree: Don't assume 16 byte alignment
authorLiam R. Howlett <Liam.Howlett@Oracle.com>
Mon, 10 Dec 2018 17:15:27 +0000 (12:15 -0500)
committerLiam R. Howlett <Liam.Howlett@Oracle.com>
Mon, 10 Dec 2018 17:15:27 +0000 (12:15 -0500)
Assume the allocations are 8 byte aligned, so only use the last two bits
to indicate the requested allocation.  Test and return the allocation
count.

Also, fix a small issue in decrementing the requested count before we
actually have succeeded in allocating.

Signed-off-by: Liam R. Howlett <Liam.Howlett@Oracle.com>
lib/maple_tree.c

index 8a9c1c20fc8b65319eaf4039e72a33f35b4de4e6..57c0b554f6d1b0c7fd3892d36e23622cc762c5ad 100644 (file)
@@ -63,28 +63,30 @@ static inline void *ma_mk_node(const struct maple_node *node)
        return (void *)((unsigned long)node | 2);
 }
 
-static inline void ma_set_alloc_cnt(struct maple_state *ms, int count)
+static inline struct maple_node *ma_get_alloc(const struct maple_state *ms)
 {
-       ms->alloc = (struct maple_node *)((unsigned long)ms->alloc & ~0x03);
-       ms->alloc = (struct maple_node *)((unsigned long)ms->alloc | count);
+       return (struct maple_node *)((unsigned long)ms->alloc & ~0x0F);
 }
 static inline int ma_get_alloc_cnt(const struct maple_state *ms)
 {
-       return (int)((unsigned long)ms->alloc & 3);
+       struct maple_node *node = ma_get_alloc(ms);
+
+       if (!node)
+               return 0;
+       if (!node->slot[0])
+               return 1;
+       if (!node->slot[1])
+               return 2;
+       return 3;
 }
 static inline void ma_set_alloc_req(struct maple_state *ms, int count)
 {
-       ms->alloc = (struct maple_node *)((unsigned long)ms->alloc & ~0x0C);
-       ms->alloc = (struct maple_node *)((unsigned long)ms->alloc |
-                       (count << 2));
+       ms->alloc = (struct maple_node *)((unsigned long)ms->alloc & ~0x03);
+       ms->alloc = (struct maple_node *)((unsigned long)ms->alloc | count);
 }
 static inline int ma_get_alloc_req(const struct maple_state *ms)
 {
-       return (int)(((unsigned long)ms->alloc & 0x0C) >> 2);
-}
-static inline struct maple_node *ma_get_alloc(const struct maple_state *ms)
-{
-       return (struct maple_node *)((unsigned long)ms->alloc & ~0x0F);
+       return (int)(((unsigned long)ms->alloc & 0x03));
 }
 static inline struct maple_node *ma_next_alloc(struct maple_state *ms)
 {
@@ -102,7 +104,6 @@ static inline struct maple_node *ma_next_alloc(struct maple_state *ms)
        } else {
                smn = (struct maple_node*)mn->slot[cnt - 1];
                mn->slot[cnt - 1] = NULL;
-               ma_set_alloc_cnt(ms, cnt);
                mn = smn;
        }
 
@@ -150,18 +151,17 @@ static void maple_new_node(struct maple_state *ms, gfp_t gfp)
 
        slot = allocated - 1;
        while (req > 0) {
-               req--;
                smn = kzalloc(sizeof(*mn), gfp);
                if (!smn)
                        goto slot_failed;
                smn->parent = NULL;
                mn->slot[slot++] = smn;
+               req--;
                allocated++;
        }
 
 slot_failed:
        ms->alloc = mn;
-       ma_set_alloc_cnt(ms, allocated);
        ma_set_alloc_req(ms, req);
 
 list_failed: