Move to kernel style SPDX license identifiers
[babeltrace.git] / src / lib / prio-heap / prio-heap.c
index 29e9068891c76bbfed7d189369ec39dedb670706..d137aed8f03c2343985aa3a978d29b5ab3b8bfa0 100644 (file)
@@ -1,29 +1,13 @@
 /*
- * Static-sized priority heap containing pointers. Based on CLRS,
- * chapter 6.
- *
- * Copyright 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+ * SPDX-License-Identifier: MIT
  *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
+ * Copyright 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
  *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
+ * Static-sized priority heap containing pointers. Based on CLRS,
+ * chapter 6.
  */
 
-#include "common/babeltrace.h"
+#include "common/macros.h"
 #include "common/assert.h"
 #include <errno.h>
 #include <stdlib.h>
@@ -40,7 +24,7 @@ void check_heap(const struct ptr_heap *heap)
                return;
 
        for (i = 1; i < heap->len; i++)
-               BT_ASSERT(!heap->gt(heap->ptrs[i], heap->ptrs[0]));
+               BT_ASSERT_DBG(!heap->gt(heap->ptrs[i], heap->ptrs[0]));
 }
 #endif
 
@@ -70,14 +54,14 @@ int heap_grow(struct ptr_heap *heap, size_t new_len)
 {
        void **new_ptrs;
 
-       if (likely(heap->alloc_len >= new_len))
+       if (G_LIKELY(heap->alloc_len >= new_len))
                return 0;
 
-       heap->alloc_len = max_t(size_t, new_len, heap->alloc_len << 1);
+       heap->alloc_len = bt_max_t(size_t, new_len, heap->alloc_len << 1);
        new_ptrs = calloc(heap->alloc_len, sizeof(void *));
-       if (unlikely(!new_ptrs))
+       if (G_UNLIKELY(!new_ptrs))
                return -ENOMEM;
-       if (likely(heap->ptrs))
+       if (G_LIKELY(heap->ptrs))
                memcpy(new_ptrs, heap->ptrs, heap->len * sizeof(void *));
        free(heap->ptrs);
        heap->ptrs = new_ptrs;
@@ -90,7 +74,7 @@ int heap_set_len(struct ptr_heap *heap, size_t new_len)
        int ret;
 
        ret = heap_grow(heap, new_len);
-       if (unlikely(ret))
+       if (G_UNLIKELY(ret))
                return ret;
        heap->len = new_len;
        return 0;
@@ -107,7 +91,7 @@ int bt_heap_init(struct ptr_heap *heap, size_t alloc_len,
         * Minimum size allocated is 1 entry to ensure memory allocation
         * never fails within bt_heap_replace_max.
         */
-       return heap_grow(heap, max_t(size_t, 1, alloc_len));
+       return heap_grow(heap, bt_max_t(size_t, 1, alloc_len));
 }
 
 void bt_heap_free(struct ptr_heap *heap)
@@ -131,7 +115,7 @@ static void heapify(struct ptr_heap *heap, size_t i)
                        largest = i;
                if (r < heap->len && heap->gt(ptrs[r], ptrs[largest]))
                        largest = r;
-               if (unlikely(largest == i))
+               if (G_UNLIKELY(largest == i))
                        break;
                tmp = ptrs[i];
                ptrs[i] = ptrs[largest];
@@ -145,7 +129,7 @@ void *bt_heap_replace_max(struct ptr_heap *heap, void *p)
 {
        void *res;
 
-       if (unlikely(!heap->len)) {
+       if (G_UNLIKELY(!heap->len)) {
                (void) heap_set_len(heap, 1);
                heap->ptrs[0] = p;
                check_heap(heap);
@@ -166,7 +150,7 @@ int bt_heap_insert(struct ptr_heap *heap, void *p)
        int ret;
 
        ret = heap_set_len(heap, heap->len + 1);
-       if (unlikely(ret))
+       if (G_UNLIKELY(ret))
                return ret;
        ptrs = heap->ptrs;
        pos = heap->len - 1;
@@ -200,11 +184,11 @@ void *bt_heap_cherrypick(struct ptr_heap *heap, void *p)
        size_t pos, len = heap->len;
 
        for (pos = 0; pos < len; pos++)
-               if (unlikely(heap->ptrs[pos] == p))
+               if (G_UNLIKELY(heap->ptrs[pos] == p))
                        goto found;
        return NULL;
 found:
-       if (unlikely(heap->len == 1)) {
+       if (G_UNLIKELY(heap->len == 1)) {
                (void) heap_set_len(heap, 0);
                check_heap(heap);
                return heap->ptrs[0];
This page took 0.025009 seconds and 4 git commands to generate.