yaffs Added more tests to direct/timothy_tests/mirror_tests
[yaffs2.git] / yaffs_allocator.c
index 66ba5a6..f9cd5be 100644 (file)
  * published by the Free Software Foundation.
  */
 
-
-
 #include "yaffs_allocator.h"
 #include "yaffs_guts.h"
 #include "yaffs_trace.h"
 #include "yportenv.h"
 
-#ifdef CONFIG_YAFFS_YMALLOC_ALLOCATOR
+#ifdef CONFIG_YAFFS_KMALLOC_ALLOCATOR
 
-void yaffs_deinit_raw_tnodes_and_objs(yaffs_dev_t *dev)
+void yaffs_deinit_raw_tnodes_and_objs(struct yaffs_dev *dev)
 {
        dev = dev;
 }
 
-void yaffs_init_raw_tnodes_and_objs(yaffs_dev_t *dev)
+void yaffs_init_raw_tnodes_and_objs(struct yaffs_dev *dev)
 {
        dev = dev;
 }
 
-yaffs_tnode_t *yaffs_alloc_raw_tnode(yaffs_dev_t *dev)
+struct yaffs_tnode *yaffs_alloc_raw_tnode(struct yaffs_dev *dev)
 {
-       return (yaffs_tnode_t *)YMALLOC(dev->tnode_size);
+       return (struct yaffs_tnode *)kmalloc(dev->tnode_size, GFP_NOFS);
 }
 
-void yaffs_free_raw_tnode(yaffs_dev_t *dev, yaffs_tnode_t *tn)
+void yaffs_free_raw_tnode(struct yaffs_dev *dev, struct yaffs_tnode *tn)
 {
        dev = dev;
-       YFREE(tn);
+       kfree(tn);
 }
 
-void yaffs_init_raw_objs(yaffs_dev_t *dev)
+void yaffs_init_raw_objs(struct yaffs_dev *dev)
 {
        dev = dev;
 }
 
-void yaffs_deinit_raw_objs(yaffs_dev_t *dev)
+void yaffs_deinit_raw_objs(struct yaffs_dev *dev)
 {
        dev = dev;
 }
 
-yaffs_obj_t *yaffs_alloc_raw_obj(yaffs_dev_t *dev)
+struct yaffs_obj *yaffs_alloc_raw_obj(struct yaffs_dev *dev)
 {
        dev = dev;
-       return (yaffs_obj_t *) YMALLOC(sizeof(yaffs_obj_t));
+       return (struct yaffs_obj *)kmalloc(sizeof(struct yaffs_obj));
 }
 
-
-void yaffs_free_raw_obj(yaffs_dev_t *dev, yaffs_obj_t *obj)
+void yaffs_free_raw_obj(struct yaffs_dev *dev, struct yaffs_obj *obj)
 {
 
        dev = dev;
-       YFREE(obj);
+       kfree(obj);
 }
 
 #else
 
 struct yaffs_tnode_list {
        struct yaffs_tnode_list *next;
-       yaffs_tnode_t *tnodes;
+       struct yaffs_tnode *tnodes;
 };
 
-typedef struct yaffs_tnode_list yaffs_tnodelist_t;
-
-struct yaffs_obj_list_struct {
-       yaffs_obj_t *objects;
-       struct yaffs_obj_list_struct *next;
+struct yaffs_obj_list {
+       struct yaffs_obj_list *next;
+       struct yaffs_obj *objects;
 };
 
-typedef struct yaffs_obj_list_struct yaffs_obj_list;
-
-
 struct yaffs_allocator {
        int n_tnodes_created;
-       yaffs_tnode_t *free_tnodes;
+       struct yaffs_tnode *free_tnodes;
        int n_free_tnodes;
-       yaffs_tnodelist_t *alloc_tnode_list;
+       struct yaffs_tnode_list *alloc_tnode_list;
 
        int n_obj_created;
-       yaffs_obj_t *free_objs;
+       struct yaffs_obj *free_objs;
        int n_free_objects;
 
-       yaffs_obj_list *allocated_obj_list;
+       struct yaffs_obj_list *allocated_obj_list;
 };
 
-
-static void yaffs_deinit_raw_tnodes(yaffs_dev_t *dev)
+static void yaffs_deinit_raw_tnodes(struct yaffs_dev *dev)
 {
 
-       struct yaffs_allocator *allocator = (struct yaffs_allocator *)dev->allocator;
+       struct yaffs_allocator *allocator =
+           (struct yaffs_allocator *)dev->allocator;
 
-       yaffs_tnodelist_t *tmp;
+       struct yaffs_tnode_list *tmp;
 
-       if(!allocator){
+       if (!allocator) {
                YBUG();
                return;
        }
@@ -111,8 +103,8 @@ static void yaffs_deinit_raw_tnodes(yaffs_dev_t *dev)
        while (allocator->alloc_tnode_list) {
                tmp = allocator->alloc_tnode_list->next;
 
-               YFREE(allocator->alloc_tnode_list->tnodes);
-               YFREE(allocator->alloc_tnode_list);
+               kfree(allocator->alloc_tnode_list->tnodes);
+               kfree(allocator->alloc_tnode_list);
                allocator->alloc_tnode_list = tmp;
 
        }
@@ -122,30 +114,32 @@ static void yaffs_deinit_raw_tnodes(yaffs_dev_t *dev)
        allocator->n_tnodes_created = 0;
 }
 
-static void yaffs_init_raw_tnodes(yaffs_dev_t *dev)
+static void yaffs_init_raw_tnodes(struct yaffs_dev *dev)
 {
        struct yaffs_allocator *allocator = dev->allocator;
 
-       if(allocator){
+       if (allocator) {
                allocator->alloc_tnode_list = NULL;
                allocator->free_tnodes = NULL;
                allocator->n_free_tnodes = 0;
                allocator->n_tnodes_created = 0;
-       } else
+       } else {
                YBUG();
+       }
 }
 
-static int yaffs_create_tnodes(yaffs_dev_t *dev, int n_tnodes)
+static int yaffs_create_tnodes(struct yaffs_dev *dev, int n_tnodes)
 {
-       struct yaffs_allocator *allocator = (struct yaffs_allocator *)dev->allocator;
+       struct yaffs_allocator *allocator =
+           (struct yaffs_allocator *)dev->allocator;
        int i;
-       yaffs_tnode_t *new_tnodes;
-       __u8 *mem;
-       yaffs_tnode_t *curr;
-       yaffs_tnode_t *next;
-       yaffs_tnodelist_t *tnl;
+       struct yaffs_tnode *new_tnodes;
+       u8 *mem;
+       struct yaffs_tnode *curr;
+       struct yaffs_tnode *next;
+       struct yaffs_tnode_list *tnl;
 
-       if(!allocator){
+       if (!allocator) {
                YBUG();
                return YAFFS_FAIL;
        }
@@ -153,28 +147,27 @@ static int yaffs_create_tnodes(yaffs_dev_t *dev, int n_tnodes)
        if (n_tnodes < 1)
                return YAFFS_OK;
 
-
        /* make these things */
 
-       new_tnodes = YMALLOC(n_tnodes * dev->tnode_size);
-       mem = (__u8 *)new_tnodes;
+       new_tnodes = kmalloc(n_tnodes * dev->tnode_size, GFP_NOFS);
+       mem = (u8 *) new_tnodes;
 
        if (!new_tnodes) {
-               T(YAFFS_TRACE_ERROR,
-                       (TSTR("yaffs: Could not allocate Tnodes" TENDSTR)));
+               yaffs_trace(YAFFS_TRACE_ERROR,
+                       "yaffs: Could not allocate Tnodes");
                return YAFFS_FAIL;
        }
 
        /* New hookup for wide tnodes */
        for (i = 0; i < n_tnodes - 1; i++) {
-               curr = (yaffs_tnode_t *) &mem[i * dev->tnode_size];
-               next = (yaffs_tnode_t *) &mem[(i+1) * dev->tnode_size];
+               curr = (struct yaffs_tnode *)&mem[i * dev->tnode_size];
+               next = (struct yaffs_tnode *)&mem[(i + 1) * dev->tnode_size];
                curr->internal[0] = next;
        }
 
-       curr = (yaffs_tnode_t *) &mem[(n_tnodes - 1) * dev->tnode_size];
+       curr = (struct yaffs_tnode *)&mem[(n_tnodes - 1) * dev->tnode_size];
        curr->internal[0] = allocator->free_tnodes;
-       allocator->free_tnodes = (yaffs_tnode_t *)mem;
+       allocator->free_tnodes = (struct yaffs_tnode *)mem;
 
        allocator->n_free_tnodes += n_tnodes;
        allocator->n_tnodes_created += n_tnodes;
@@ -184,30 +177,29 @@ static int yaffs_create_tnodes(yaffs_dev_t *dev, int n_tnodes)
         * but it just means we can't free this bunch of tnodes later.
         */
 
-       tnl = YMALLOC(sizeof(yaffs_tnodelist_t));
+       tnl = kmalloc(sizeof(struct yaffs_tnode_list), GFP_NOFS);
        if (!tnl) {
-               T(YAFFS_TRACE_ERROR,
-                 (TSTR
-                  ("yaffs: Could not add tnodes to management list" TENDSTR)));
-                  return YAFFS_FAIL;
+               yaffs_trace(YAFFS_TRACE_ERROR,
+                       "Could not add tnodes to management list");
+               return YAFFS_FAIL;
        } else {
                tnl->tnodes = new_tnodes;
                tnl->next = allocator->alloc_tnode_list;
                allocator->alloc_tnode_list = tnl;
        }
 
-       T(YAFFS_TRACE_ALLOCATE, (TSTR("yaffs: Tnodes added" TENDSTR)));
+       yaffs_trace(YAFFS_TRACE_ALLOCATE,"Tnodes added");
 
        return YAFFS_OK;
 }
 
-
-yaffs_tnode_t *yaffs_alloc_raw_tnode(yaffs_dev_t *dev)
+struct yaffs_tnode *yaffs_alloc_raw_tnode(struct yaffs_dev *dev)
 {
-       struct yaffs_allocator *allocator = (struct yaffs_allocator *)dev->allocator;
-       yaffs_tnode_t *tn = NULL;
+       struct yaffs_allocator *allocator =
+           (struct yaffs_allocator *)dev->allocator;
+       struct yaffs_tnode *tn = NULL;
 
-       if(!allocator){
+       if (!allocator) {
                YBUG();
                return NULL;
        }
@@ -226,11 +218,11 @@ yaffs_tnode_t *yaffs_alloc_raw_tnode(yaffs_dev_t *dev)
 }
 
 /* FreeTnode frees up a tnode and puts it back on the free list */
-void yaffs_free_raw_tnode(yaffs_dev_t *dev, yaffs_tnode_t *tn)
+void yaffs_free_raw_tnode(struct yaffs_dev *dev, struct yaffs_tnode *tn)
 {
        struct yaffs_allocator *allocator = dev->allocator;
 
-       if(!allocator){
+       if (!allocator) {
                YBUG();
                return;
        }
@@ -240,37 +232,36 @@ void yaffs_free_raw_tnode(yaffs_dev_t *dev, yaffs_tnode_t *tn)
                allocator->free_tnodes = tn;
                allocator->n_free_tnodes++;
        }
-       dev->checkpoint_blocks_required = 0; /* force recalculation*/
+       dev->checkpoint_blocks_required = 0;    /* force recalculation */
 }
 
-
-
-static void yaffs_init_raw_objs(yaffs_dev_t *dev)
+static void yaffs_init_raw_objs(struct yaffs_dev *dev)
 {
        struct yaffs_allocator *allocator = dev->allocator;
 
-       if(allocator) {
+       if (allocator) {
                allocator->allocated_obj_list = NULL;
                allocator->free_objs = NULL;
                allocator->n_free_objects = 0;
-       } else
+       } else {
                YBUG();
+       }
 }
 
-static void yaffs_deinit_raw_objs(yaffs_dev_t *dev)
+static void yaffs_deinit_raw_objs(struct yaffs_dev *dev)
 {
        struct yaffs_allocator *allocator = dev->allocator;
-       yaffs_obj_list *tmp;
+       struct yaffs_obj_list *tmp;
 
-       if(!allocator){
+       if (!allocator) {
                YBUG();
                return;
        }
 
        while (allocator->allocated_obj_list) {
                tmp = allocator->allocated_obj_list->next;
-               YFREE(allocator->allocated_obj_list->objects);
-               YFREE(allocator->allocated_obj_list);
+               kfree(allocator->allocated_obj_list->objects);
+               kfree(allocator->allocated_obj_list);
 
                allocator->allocated_obj_list = tmp;
        }
@@ -280,16 +271,15 @@ static void yaffs_deinit_raw_objs(yaffs_dev_t *dev)
        allocator->n_obj_created = 0;
 }
 
-
-static int yaffs_create_free_objs(yaffs_dev_t *dev, int n_obj)
+static int yaffs_create_free_objs(struct yaffs_dev *dev, int n_obj)
 {
        struct yaffs_allocator *allocator = dev->allocator;
 
        int i;
-       yaffs_obj_t *new_objs;
-       yaffs_obj_list *list;
+       struct yaffs_obj *new_objs;
+       struct yaffs_obj_list *list;
 
-       if(!allocator){
+       if (!allocator) {
                YBUG();
                return YAFFS_FAIL;
        }
@@ -298,27 +288,27 @@ static int yaffs_create_free_objs(yaffs_dev_t *dev, int n_obj)
                return YAFFS_OK;
 
        /* make these things */
-       new_objs = YMALLOC(n_obj * sizeof(yaffs_obj_t));
-       list = YMALLOC(sizeof(yaffs_obj_list));
+       new_objs = kmalloc(n_obj * sizeof(struct yaffs_obj), GFP_NOFS);
+       list = kmalloc(sizeof(struct yaffs_obj_list), GFP_NOFS);
 
        if (!new_objs || !list) {
-               if (new_objs){
-                       YFREE(new_objs);
+               if (new_objs) {
+                       kfree(new_objs);
                        new_objs = NULL;
                }
-               if (list){
-                       YFREE(list);
+               if (list) {
+                       kfree(list);
                        list = NULL;
                }
-               T(YAFFS_TRACE_ALLOCATE,
-                 (TSTR("yaffs: Could not allocate more objects" TENDSTR)));
+               yaffs_trace(YAFFS_TRACE_ALLOCATE,
+                       "Could not allocate more objects");
                return YAFFS_FAIL;
        }
 
        /* Hook them into the free list */
        for (i = 0; i < n_obj - 1; i++) {
                new_objs[i].siblings.next =
-                               (struct ylist_head *)(&new_objs[i + 1]);
+                   (struct list_head *)(&new_objs[i + 1]);
        }
 
        new_objs[n_obj - 1].siblings.next = (void *)allocator->free_objs;
@@ -335,12 +325,12 @@ static int yaffs_create_free_objs(yaffs_dev_t *dev, int n_obj)
        return YAFFS_OK;
 }
 
-yaffs_obj_t *yaffs_alloc_raw_obj(yaffs_dev_t *dev)
+struct yaffs_obj *yaffs_alloc_raw_obj(struct yaffs_dev *dev)
 {
-       yaffs_obj_t *obj = NULL;
+       struct yaffs_obj *obj = NULL;
        struct yaffs_allocator *allocator = dev->allocator;
 
-       if(!allocator) {
+       if (!allocator) {
                YBUG();
                return obj;
        }
@@ -352,55 +342,55 @@ yaffs_obj_t *yaffs_alloc_raw_obj(yaffs_dev_t *dev)
        if (allocator->free_objs) {
                obj = allocator->free_objs;
                allocator->free_objs =
-                       (yaffs_obj_t *) (allocator->free_objs->siblings.next);
+                   (struct yaffs_obj *)(allocator->free_objs->siblings.next);
                allocator->n_free_objects--;
        }
 
        return obj;
 }
 
-
-void yaffs_free_raw_obj(yaffs_dev_t *dev, yaffs_obj_t *obj)
+void yaffs_free_raw_obj(struct yaffs_dev *dev, struct yaffs_obj *obj)
 {
 
        struct yaffs_allocator *allocator = dev->allocator;
 
-       if(!allocator)
+       if (!allocator)
                YBUG();
        else {
                /* Link into the free list. */
-               obj->siblings.next = (struct ylist_head *)(allocator->free_objs);
+               obj->siblings.next = (struct list_head *)(allocator->free_objs);
                allocator->free_objs = obj;
                allocator->n_free_objects++;
        }
 }
 
-void yaffs_deinit_raw_tnodes_and_objs(yaffs_dev_t *dev)
+void yaffs_deinit_raw_tnodes_and_objs(struct yaffs_dev *dev)
 {
-       if(dev->allocator){
+       if (dev->allocator) {
                yaffs_deinit_raw_tnodes(dev);
                yaffs_deinit_raw_objs(dev);
 
-               YFREE(dev->allocator);
-               dev->allocator=NULL;
-       } else
+               kfree(dev->allocator);
+               dev->allocator = NULL;
+       } else {
                YBUG();
+       }
 }
 
-void yaffs_init_raw_tnodes_and_objs(yaffs_dev_t *dev)
+void yaffs_init_raw_tnodes_and_objs(struct yaffs_dev *dev)
 {
        struct yaffs_allocator *allocator;
 
-       if(!dev->allocator){
-               allocator = YMALLOC(sizeof(struct yaffs_allocator));
-               if(allocator){
+       if (!dev->allocator) {
+               allocator = kmalloc(sizeof(struct yaffs_allocator), GFP_NOFS);
+               if (allocator) {
                        dev->allocator = allocator;
                        yaffs_init_raw_tnodes(dev);
                        yaffs_init_raw_objs(dev);
                }
-       } else
+       } else {
                YBUG();
+       }
 }
 
-
 #endif