]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
heap: Cleanup get/set code
authorArran Cudbard-Bell <a.cudbardb@freeradius.org>
Thu, 21 May 2020 00:45:33 +0000 (19:45 -0500)
committerArran Cudbard-Bell <a.cudbardb@freeradius.org>
Thu, 21 May 2020 02:42:19 +0000 (21:42 -0500)
src/lib/util/heap.c
src/lib/util/heap_tests.c

index 07b0fffca2db52c5677585606883810e7e81819e..9e2231181c85026aad43146d8cebb0a640a74ac6 100644 (file)
@@ -83,27 +83,31 @@ fr_heap_t *_fr_heap_alloc(TALLOC_CTX *ctx, fr_heap_cmp_t cmp, char const *type,
        return fh;
 }
 
-/*
- *     Insert element in heap. Normally, p != NULL, we insert p in a
- *     new position and bubble up. If p == NULL, then the element is
- *     already in place, and key is the position where to start the
- *     bubble-up.
- *
- *     Returns 1 on failure (cannot allocate new heap entry)
- *
- *     If offset > 0 the position (index, int) of the element in the
- *     heap is also stored in the element itself at the given offset
- *     in bytes.
- */
-#define SET_OFFSET(_heap, _node) *((int32_t *)(((uint8_t *)_heap->p[_node]) + _heap->offset)) = _node
+static CC_HINT(always_inline) int32_t index_get(fr_heap_t *hp, void *data)
+{
+       return *((int32_t const *)(((uint8_t const *)data) + hp->offset));
+}
 
-/*
- *     RESET_OFFSET is used for sanity checks. It sets offset to an
- *     invalid value.
- */
-#define RESET_OFFSET(_heap, _node) *((int32_t *)(((uint8_t *)_heap->p[_node]) + _heap->offset)) = -1
+static CC_HINT(always_inline) void index_set(fr_heap_t *hp, void *data, int32_t idx)
+{
+       *((int32_t *)(((uint8_t *)data) + hp->offset)) = idx;
+}
+
+#define OFFSET_SET(_heap, _idx) index_set(_heap, _heap->p[_idx], _idx);
+#define OFFSET_RESET(_heap, _idx) index_set(_heap, _heap->p[_idx], -1);
 
 /** Insert a new element into the heap
+ *
+ * Insert element in heap. Normally, p != NULL, we insert p in a
+ * new position and bubble up. If p == NULL, then the element is
+ * already in place, and key is the position where to start the
+ * bubble-up.
+ *
+ * Returns -1 on failure (cannot allocate new heap entry)
+ *
+ * If offset > 0 the position (index, int) of the element in the
+ * heap is also stored in the element itself at the given offset
+ * in bytes.
  *
  * @param[in] hp       The heap to insert an element into.
  * @param[in] data     Data to insert into the heap.
@@ -123,7 +127,7 @@ int fr_heap_insert(fr_heap_t *hp, void *data)
         *      0  = the node was just allocated via an "alloc_zero"
         *           function
         */
-       child = *((int32_t *)(((uint8_t *)data) + hp->offset));
+       child = index_get(hp, data);
        if ((child > 0) || ((child == 0) && (data == hp->p[0]))) {
                fr_strerror_printf("Node is already in the heap");
                return -1;
@@ -192,10 +196,10 @@ static void fr_heap_bubble(fr_heap_t *hp, int32_t child)
                 *      Child is smaller than the parent, repeat.
                 */
                HEAP_SWAP(hp->p[child], hp->p[parent]);
-               SET_OFFSET(hp, child);
+               OFFSET_SET(hp, child);
                child = parent;
        }
-       SET_OFFSET(hp, child);
+       OFFSET_SET(hp, child);
 }
 
 
@@ -225,7 +229,7 @@ int fr_heap_extract(fr_heap_t *hp, void *data)
                parent = 0;
 
        } else {                /* extract from the middle */
-               parent = *((int32_t *)(((uint8_t *)data) + hp->offset));
+               parent = index_get(hp, data);
 
                /*
                 *      Out of bounds.
@@ -240,7 +244,7 @@ int fr_heap_extract(fr_heap_t *hp, void *data)
        if (!fr_cond_assert(hp->p != NULL)) return -1;
        if (!fr_cond_assert(hp->p[parent] != NULL)) return -1;
 
-       RESET_OFFSET(hp, parent);
+       OFFSET_RESET(hp, parent);
        child = HEAP_LEFT(parent);
        while (child <= max) {
                /*
@@ -251,7 +255,7 @@ int fr_heap_extract(fr_heap_t *hp, void *data)
                        child = child + 1;
                }
                hp->p[parent] = hp->p[child];
-               SET_OFFSET(hp, parent);
+               OFFSET_SET(hp, parent);
                parent = child;
                child = HEAP_LEFT(child);
        }
index f778df30739118bd41dead9f18a0aa8f52ed6693..27e05f9c10ef1415d5e4be797ad7d477de8446cc 100644 (file)
@@ -132,7 +132,7 @@ static void heap_test_skip_10(void)
        heap_test(10);
 }
 
-#define HEAP_CYCLE_SIZE (16000000)
+#define HEAP_CYCLE_SIZE (1600000)
 
 static void heap_cycle(void)
 {