From 23a151f0f9301b6fee28c9b0e8301f1f50e4ef9b Mon Sep 17 00:00:00 2001 From: Julien Desfossez Date: Tue, 24 Jul 2012 11:54:02 -0400 Subject: [PATCH] Fix: add missing heap_copy This function allows the user to copy an existing priority heap. It is required to fix the seek by position. Signed-off-by: Julien Desfossez Signed-off-by: Mathieu Desnoyers --- include/babeltrace/prio_heap.h | 9 +++++++++ lib/prio_heap/prio_heap.c | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/include/babeltrace/prio_heap.h b/include/babeltrace/prio_heap.h index b3e2491e..347a2ced 100644 --- a/include/babeltrace/prio_heap.h +++ b/include/babeltrace/prio_heap.h @@ -114,4 +114,13 @@ extern void *heap_cherrypick(struct ptr_heap *heap, void *p); */ extern void *heap_replace_max(struct ptr_heap *heap, void *p); +/** + * heap_copy - copy a heap + * @dst: the destination heap (must be allocated) + * @src: the source heap + * + * Returns -ENOMEM if out of memory. + */ +extern int heap_copy(struct ptr_heap *dst, struct ptr_heap *src); + #endif /* _BABELTRACE_PRIO_HEAP_H */ diff --git a/lib/prio_heap/prio_heap.c b/lib/prio_heap/prio_heap.c index a37e64c8..092f0095 100644 --- a/lib/prio_heap/prio_heap.c +++ b/lib/prio_heap/prio_heap.c @@ -214,3 +214,21 @@ found: heapify(heap, pos); return p; } + +int heap_copy(struct ptr_heap *dst, struct ptr_heap *src) +{ + int ret; + + ret = heap_init(dst, src->alloc_len, src->gt); + if (ret < 0) + goto end; + + ret = heap_set_len(dst, src->len); + if (ret < 0) + goto end; + + memcpy(dst->ptrs, src->ptrs, src->len * sizeof(void *)); + +end: + return ret; +} -- 2.34.1