rcuja range: add private data to allocated ranges
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Sun, 9 Jun 2013 20:12:45 +0000 (16:12 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Sun, 9 Jun 2013 20:12:45 +0000 (16:12 -0400)
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
rcuja/rcuja-range.c
urcu/rcuja-range.h

index 761affb249e343a39e9911621a82456e0f35b4dc..cb5b78b8b8e63365c79a72fae76b2e8f6177f237 100644 (file)
 
 #define CDS_JA_RANGE_KEY_BITS  64
 
+enum cds_ja_range_type {
+       CDS_JA_RANGE_ALLOCATED,
+       CDS_JA_RANGE_FREE,
+       CDS_JA_RANGE_REMOVED,
+};
+
+/*
+ * Range goes from start (inclusive) to end (inclusive).
+ * Range start is used as node key in the Judy array.
+ */
+struct cds_ja_range {
+       uint64_t end;
+       struct cds_ja_node ja_node;
+       pthread_mutex_t lock;
+       void *priv;
+       enum cds_ja_range_type type;
+
+       /* not required on lookup fast-path */
+       uint64_t start;
+       struct rcu_head head;
+};
+
 struct cds_ja_range *cds_ja_range_lookup(struct cds_ja *ja, uint64_t key)
 {
        struct cds_ja_node *node, *last_node;
@@ -150,6 +172,7 @@ static
 struct cds_ja_range *range_create(
                uint64_t start,         /* inclusive */
                uint64_t end,           /* inclusive */
+               void *priv,
                enum cds_ja_range_type type)
 {
        struct cds_ja_range *range;
@@ -159,6 +182,7 @@ struct cds_ja_range *range_create(
                return NULL;
        range->start = start;
        range->end = end;
+       range->priv = priv;
        range->type = type;
        pthread_mutex_init(&range->lock, NULL);
        return range;
@@ -187,7 +211,8 @@ void rcu_free_range(struct cds_ja *ja, struct cds_ja_range *range)
 
 struct cds_ja_range *cds_ja_range_add(struct cds_ja *ja,
                uint64_t start,         /* inclusive */
-               uint64_t end)           /* inclusive */
+               uint64_t end,           /* inclusive */
+               void *priv)
 {
        struct cds_ja_node *old_node, *old_node_end;
        struct cds_ja_range *old_range, *old_range_end, *new_range, *ranges[3];
@@ -238,32 +263,32 @@ retry:
                if (end == old_range->end) {
                        /* 1 range */
                        ranges[0] = new_range = range_create(start, end,
-                               CDS_JA_RANGE_ALLOCATED);
+                               priv, CDS_JA_RANGE_ALLOCATED);
                        nr_ranges = 1;
                } else {
                        /* 2 ranges */
                        ranges[0] = new_range = range_create(start, end,
-                               CDS_JA_RANGE_ALLOCATED);
+                               priv, CDS_JA_RANGE_ALLOCATED);
                        ranges[1] = range_create(end + 1, old_range->end,
-                               CDS_JA_RANGE_FREE);
+                               NULL, CDS_JA_RANGE_FREE);
                        nr_ranges = 2;
                }
        } else {
                if (end == old_range->end) {
                        /* 2 ranges */
                        ranges[0] = range_create(old_range->start, start - 1,
-                               CDS_JA_RANGE_FREE);
+                               NULL, CDS_JA_RANGE_FREE);
                        ranges[1] = new_range = range_create(start, end,
-                               CDS_JA_RANGE_ALLOCATED);
+                               priv, CDS_JA_RANGE_ALLOCATED);
                        nr_ranges = 2;
                } else {
                        /* 3 ranges */
                        ranges[0] = range_create(old_range->start, start - 1,
-                               CDS_JA_RANGE_FREE);
+                               NULL, CDS_JA_RANGE_FREE);
                        ranges[1] = new_range = range_create(start, end,
-                               CDS_JA_RANGE_ALLOCATED);
+                               priv, CDS_JA_RANGE_ALLOCATED);
                        ranges[2] = range_create(end + 1, old_range->end,
-                               CDS_JA_RANGE_FREE);
+                               NULL, CDS_JA_RANGE_FREE);
                        nr_ranges = 3;
                }
        }
@@ -342,7 +367,7 @@ retry:
        /* Create new free range */
        start = merge_ranges[0]->start;
        end = merge_ranges[nr_merge - 1]->end;
-       new_range = range_create(start, end, CDS_JA_RANGE_FREE);
+       new_range = range_create(start, end, NULL, CDS_JA_RANGE_FREE);
        ret = cds_ja_add(ja, start, &new_range->ja_node);
        assert(!ret);
 
@@ -377,7 +402,7 @@ struct cds_ja *_cds_ja_range_new(const struct rcu_flavor_struct *flavor)
        ja = _cds_ja_new(CDS_JA_RANGE_KEY_BITS, flavor);
        if (!ja)
                return NULL;
-       range = range_create(0, UINT64_MAX, CDS_JA_RANGE_FREE);
+       range = range_create(0, UINT64_MAX, NULL, CDS_JA_RANGE_FREE);
        if (!range)
                goto free_ja;
        ret = cds_ja_add(ja, 0, &range->ja_node);
@@ -393,7 +418,8 @@ free_ja:
        return NULL;
 }
 
-int cds_ja_range_destroy(struct cds_ja *ja)
+int cds_ja_range_destroy(struct cds_ja *ja,
+               void (*free_priv)(void *ptr))
 {
        uint64_t key;
        struct cds_ja_node *ja_node;
@@ -410,6 +436,8 @@ int cds_ja_range_destroy(struct cds_ja *ja)
                        ret = cds_ja_del(ja, key, &range->ja_node);
                        if (ret)
                                goto error;
+                       if (free_priv)
+                               free_priv(range->priv);
                        /* Alone using Judy array, OK to free now */
                        free_range(range);
                }
index 52b7c8ac6c2e58e54c410e892cd6070f1bdbb6e1..a1ef3af0a3fbcf2b5c6354a7eb755aca44e550dd 100644 (file)
  */
 
 #include <urcu/rcuja.h>
-#include <pthread.h>
 #include <stdint.h>
 
 #ifdef __cplusplus
 extern "C" {
 #endif
 
-enum cds_ja_range_type {
-       CDS_JA_RANGE_ALLOCATED,
-       CDS_JA_RANGE_FREE,
-       CDS_JA_RANGE_REMOVED,
-};
-
-/*
- * Range goes from start (inclusive) to end (inclusive).
- * Range start is used as node key in the Judy array.
- */
-struct cds_ja_range {
-       uint64_t start, end;
-       struct cds_ja_node ja_node;
-       pthread_mutex_t lock;
-       enum cds_ja_range_type type;
-       struct rcu_head head;
-};
-
 struct cds_ja_range *cds_ja_range_lookup(struct cds_ja *ja, uint64_t key);
 
 struct cds_ja_range *cds_ja_range_lock(struct cds_ja_range *range);
@@ -59,7 +40,8 @@ void cds_ja_range_unlock(struct cds_ja_range *range);
 
 struct cds_ja_range *cds_ja_range_add(struct cds_ja *ja,
                uint64_t start,         /* inclusive */
-               uint64_t end);          /* inclusive */
+               uint64_t end,           /* inclusive */
+               void *priv);
 
 int cds_ja_range_del(struct cds_ja *ja, struct cds_ja_range *range);
 
@@ -71,7 +53,8 @@ struct cds_ja *cds_ja_range_new(void)
        return _cds_ja_range_new(&rcu_flavor);
 }
 
-int cds_ja_range_destroy(struct cds_ja *ja);
+int cds_ja_range_destroy(struct cds_ja *ja,
+               void (*free_priv)(void *ptr));
 
 #ifdef __cplusplus
 }
This page took 0.03353 seconds and 4 git commands to generate.