client: Fix a memory leak in client_post_select().
[paraslash.git] / buffer_tree.c
index 7180149ee49ade68b6310bb2f596562cb8ff08a8..962359fbfa253790880f1380551335eaf9861c6d 100644 (file)
@@ -8,6 +8,7 @@
 #include "error.h"
 #include "sched.h"
 
+/* whead = NULL means area full */
 struct btr_pool {
        char *name;
        char *area_start;
@@ -56,6 +57,17 @@ struct btr_node {
        void *context;
 };
 
+/**
+ * Create a new buffer pool.
+ *
+ * \param name The name of the new buffer pool.
+ *
+ * \param area The size in bytes of the pool area.
+ *
+ * \return An opaque pointer to the newly created buffer pool. It must be
+ * passed to btr_pool_free() after it is no longer used to deallocate all
+ * resources.
+ */
 struct btr_pool *btr_pool_new(const char *name, size_t area_size)
 {
        struct btr_pool *btrp;
@@ -70,8 +82,11 @@ struct btr_pool *btr_pool_new(const char *name, size_t area_size)
        return btrp;
 }
 
-/* whead = NULL means area full */
-
+/**
+ * Dellocate resources used by a buffer pool.
+ *
+ * \param btrp A pointer obtained via btr_pool_new().
+ */
 void btr_pool_free(struct btr_pool *btrp)
 {
        if (!btrp)
@@ -81,6 +96,14 @@ void btr_pool_free(struct btr_pool *btrp)
        free(btrp);
 }
 
+/**
+ * Return the size of the buffer pool area.
+ *
+ * \param btrp The buffer pool.
+ *
+ * \return The same value which was passed during creation time to
+ * btr_pool_new().
+ */
 size_t btr_pool_size(struct btr_pool *btrp)
 {
        return btrp->area_end - btrp->area_start;
@@ -168,14 +191,6 @@ static void btr_pool_deallocate(struct btr_pool *btrp, size_t size)
 #define FOR_EACH_BUFFER_REF_SAFE(_br, _tmp, _btrn) \
        list_for_each_entry_safe((_br), (_tmp), &(_btrn)->input_queue, node)
 
-/*
-       (parent, child):
-       (NULL, NULL): new, isolated node.
-       (NULL, c): new node becomes root, c must be old root
-       (p, NULL): new leaf node
-       (p, c): new internal node, ch must be child of p
-
-*/
 struct btr_node *btr_new_node(struct btr_node_description *bnd)
 {
        struct btr_node *btrn = para_malloc(sizeof(*btrn));
@@ -186,14 +201,31 @@ struct btr_node *btr_new_node(struct btr_node_description *bnd)
        btrn->context = bnd->context;
        btrn->start.tv_sec = 0;
        btrn->start.tv_usec = 0;
-       if (bnd->parent)
-               list_add_tail(&btrn->node, &bnd->parent->children);
        INIT_LIST_HEAD(&btrn->children);
        INIT_LIST_HEAD(&btrn->input_queue);
-       if (bnd->parent)
-               PARA_INFO_LOG("added %s as child of %s\n", bnd->name, bnd->parent->name);
-       else
-               PARA_INFO_LOG("added %s as btr root\n", bnd->name);
+       if (!bnd->child) {
+               if (bnd->parent) {
+                       list_add_tail(&btrn->node, &bnd->parent->children);
+                       PARA_INFO_LOG("new leaf node: %s (child of %s)\n",
+                               bnd->name, bnd->parent->name);
+               } else
+                       PARA_INFO_LOG("added %s as btr root\n", bnd->name);
+               goto out;
+       }
+       if (!bnd->parent) {
+               assert(!bnd->child->parent);
+               PARA_INFO_LOG("new root: %s (was %s)\n",
+                       bnd->name, bnd->child->name);
+               btrn->parent = NULL;
+               list_add_tail(&bnd->child->node, &btrn->children);
+               /* link it in */
+               bnd->child->parent = btrn;
+               goto out;
+       }
+       PARA_EMERG_LOG("inserting internal nodes not yet supported.\n");
+       exit(EXIT_FAILURE);
+       assert(bnd->child->parent == bnd->parent);
+out:
        return btrn;
 }