Skip to content
Snippets Groups Projects
Commit 647d4722 authored by Marek Vavrusa's avatar Marek Vavrusa
Browse files

Fixed heap bubbling and memory sizes.

parent ad0cd532
No related branches found
No related tags found
No related merge requests found
......@@ -26,10 +26,10 @@
#define OPENBSD_SLAB_BROKEN
#endif
/* According to libucw heap, x<y should return non-zero else zero value. */
/* Heap only cares about x<y. */
static int compare_event_heap_nodes(event_t **e1, event_t **e2)
{
if (timercmp(&(*e1)->tv, &(*e2)->tv, <)) return 1;
if (timercmp(&(*e1)->tv, &(*e2)->tv, <)) return -1;
return 0;
}
......@@ -97,8 +97,9 @@ void evsched_delete(evsched_t **s)
while (! EMPTY_HEAP(&(*s)->heap)) /* FIXME: Would be faster to simply walk through the array */
{
evsched_event_free((*s), *((event_t**)(HHEAD(&(*s)->heap))));
event_t *e = *((event_t**)(HHEAD(&(*s)->heap)));
heap_delmin(&(*s)->heap);
evsched_event_free((*s), e);
}
free((*s)->heap.data);
......@@ -144,11 +145,6 @@ void evsched_event_free(evsched_t *s, event_t *ev)
if (!s || !ev) {
return;
}
int found = 0;
if ((found = heap_find(&s->heap, &ev))) {
heap_delete(&s->heap, found);
}
#ifndef OPENBSD_SLAB_BROKEN
pthread_mutex_lock(&s->cache.lock);
......
......@@ -75,9 +75,10 @@ static inline void _heap_bubble_down(struct heap *h, int e)
{
e1 = 2*e;
if(e1 > h->num) break;
if(((h->cmp(HELEMENT(h, e),HELEMENT(h,e1)) < 0) && (e1 == h->num)) || (h->cmp(HELEMENT(h, e),HELEMENT(h,e1+1)) < 0)) break;
if((h->cmp(HELEMENT(h, e),HELEMENT(h,e1)) < 0) && (e1 == h->num || (h->cmp(HELEMENT(h, e),HELEMENT(h,e1+1)) < 0))) break;
if((e1 != h->num) && (h->cmp(HELEMENT(h, e1+1), HELEMENT(h,e1)) < 0)) e1++;
h->swap(h,HELEMENT(h,e),HELEMENT(h,e1));
e = e1;
}
}
......@@ -87,7 +88,7 @@ static inline void _heap_bubble_up(struct heap *h, int e)
while (e > 1)
{
e1 = e/2;
if(h->cmp(HELEMENT(h, e),HELEMENT(h,e1)) < 0) break;
if(h->cmp(HELEMENT(h, e1),HELEMENT(h,e)) < 0) break;
h->swap(h,HELEMENT(h,e),HELEMENT(h,e1));
e = e1;
}
......@@ -110,7 +111,7 @@ int heap_insert(struct heap *h, void *e)
if(h->num == h->max_size)
{
h->max_size = h->max_size * HEAP_INCREASE_STEP;
h->data = realloc(h->data, sizeof(struct heap) + (h->max_size + 1) * h->elm_size);
h->data = realloc(h->data, (h->max_size + 1) * h->elm_size);
}
if(h)
......@@ -119,6 +120,7 @@ int heap_insert(struct heap *h, void *e)
memcpy(HELEMENT(h,h->num),e,h->elm_size);
_heap_bubble_up(h,h->num);
}
return h->data ? 1 :0 ;
}
......@@ -128,7 +130,7 @@ int heap_find(struct heap *h, void *elm) /* FIXME - very slow */
while(i > 0)
{
if(! h->cmp(HELEMENT(h, i),elm) ) break;
if(h->cmp(HELEMENT(h, i),elm) == 0) break;
--i;
}
return i;
......@@ -144,7 +146,7 @@ void heap_delete(struct heap *h, int e)
if ((h->num > INITIAL_HEAP_SIZE) && (h->num < h->max_size / HEAP_DECREASE_THRESHOLD))
{
h->max_size = h->max_size / HEAP_INCREASE_STEP;
h->data = realloc(h->data, h->max_size);
h->data = realloc(h->data, (h->max_size + 1) * h->elm_size);
}
}
......@@ -37,11 +37,11 @@ struct heap {
void *data;
}; /* Array follows */
#define INITIAL_HEAP_SIZE 1000
#define HEAP_INCREASE_STEP 10
#define HEAP_DECREASE_THRESHOLD 50 /* h->num be divided by this number */
#define INITIAL_HEAP_SIZE 512 /* initial heap size */
#define HEAP_INCREASE_STEP 2 /* multiplier for each inflation, keep conservative */
#define HEAP_DECREASE_THRESHOLD 2 /* threshold for deflation, keep conservative */
#define HTEMPELEMENT(h) ((h)->data) /* Pointer to tmp element (for swap) */
#define HELEMENT(h,num) ((char*)(h)->data + num * (h)->elm_size)
#define HELEMENT(h,num) ((char*)(h)->data + (num) * (h)->elm_size)
#define HHEAD(h) HELEMENT((h),1)
#define EMPTY_HEAP(h) ((h)->num == 0) /* h->num == 0 */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment