From 96e1cf269c09a34129404b430f1a5d158adf3a57 Mon Sep 17 00:00:00 2001 From: Mathieu Desnoyers Date: Tue, 16 Aug 2011 15:00:02 -0400 Subject: [PATCH] Annotate prio heap likely branch Signed-off-by: Mathieu Desnoyers --- include/babeltrace/prio_heap.h | 3 ++- lib/prio_heap.c | 19 ++++++++++--------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/include/babeltrace/prio_heap.h b/include/babeltrace/prio_heap.h index 1a523313..b3e2491e 100644 --- a/include/babeltrace/prio_heap.h +++ b/include/babeltrace/prio_heap.h @@ -21,6 +21,7 @@ */ #include +#include struct ptr_heap { size_t len, alloc_len; @@ -47,7 +48,7 @@ void check_heap(const struct ptr_heap *heap) static inline void *heap_maximum(const struct ptr_heap *heap) { check_heap(heap); - return heap->len ? heap->ptrs[0] : NULL; + return likely(heap->len) ? heap->ptrs[0] : NULL; } /** diff --git a/lib/prio_heap.c b/lib/prio_heap.c index 008c4cc3..a37e64c8 100644 --- a/lib/prio_heap.c +++ b/lib/prio_heap.c @@ -18,6 +18,7 @@ */ #include +#include #include #include #include @@ -67,14 +68,14 @@ int heap_grow(struct ptr_heap *heap, size_t new_len) { void **new_ptrs; - if (heap->alloc_len >= new_len) + if (likely(heap->alloc_len >= new_len)) return 0; heap->alloc_len = max_t(size_t, new_len, heap->alloc_len << 1); new_ptrs = calloc(heap->alloc_len, sizeof(void *)); - if (!new_ptrs) + if (unlikely(!new_ptrs)) return -ENOMEM; - if (heap->ptrs) + if (likely(heap->ptrs)) memcpy(new_ptrs, heap->ptrs, heap->len * sizeof(void *)); free(heap->ptrs); heap->ptrs = new_ptrs; @@ -87,7 +88,7 @@ int heap_set_len(struct ptr_heap *heap, size_t new_len) int ret; ret = heap_grow(heap, new_len); - if (ret) + if (unlikely(ret)) return ret; heap->len = new_len; return 0; @@ -128,7 +129,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 (largest == i) + if (unlikely(largest == i)) break; tmp = ptrs[i]; ptrs[i] = ptrs[largest]; @@ -142,7 +143,7 @@ void *heap_replace_max(struct ptr_heap *heap, void *p) { void *res; - if (!heap->len) { + if (unlikely(!heap->len)) { (void) heap_set_len(heap, 1); heap->ptrs[0] = p; check_heap(heap); @@ -163,7 +164,7 @@ int heap_insert(struct ptr_heap *heap, void *p) int ret; ret = heap_set_len(heap, heap->len + 1); - if (ret) + if (unlikely(ret)) return ret; ptrs = heap->ptrs; pos = heap->len - 1; @@ -197,11 +198,11 @@ void *heap_cherrypick(struct ptr_heap *heap, void *p) size_t pos, len = heap->len; for (pos = 0; pos < len; pos++) - if (heap->ptrs[pos] == p) + if (unlikely(heap->ptrs[pos] == p)) goto found; return NULL; found: - if (heap->len == 1) { + if (unlikely(heap->len == 1)) { (void) heap_set_len(heap, 0); check_heap(heap); return heap->ptrs[0]; -- 2.34.1