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

Resolved heap inserting part of structure, removing from heap and some macros.

parent e10a05a5
No related branches found
No related tags found
No related merge requests found
......@@ -77,7 +77,7 @@ evsched_t *evsched_new()
#ifndef OPENBSD_SLAB_BROKEN
slab_cache_init(&s->cache.alloc, sizeof(event_t));
#endif
heap_init(&s->heap, sizeof(void *), compare_event_heap_nodes, 0, NULL);
heap_init(&s->heap, sizeof(event_t *), compare_event_heap_nodes, 0, NULL);
return s;
}
......@@ -97,7 +97,7 @@ 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), (HHEAD(&(*s)->heap)));
evsched_event_free((*s), *((event_t**)(HHEAD(&(*s)->heap))));
heap_delmin(&(*s)->heap);
}
......@@ -174,12 +174,12 @@ event_t* evsched_next(evsched_t *s)
gettimeofday(&dt, 0);
/* Get next event. */
event_t *next_ev = HHEAD(&s->heap);
event_t *next_ev = *((event_t**)HHEAD(&s->heap));
/* Immediately return. */
if (timercmp(&dt, &next_ev->tv, >=)) {
s->current = next_ev;
rem_node(&next_ev->n);
heap_delmin(&s->heap);
pthread_mutex_unlock(&s->mx);
pthread_mutex_lock(&s->rl);
return next_ev;
......@@ -232,7 +232,7 @@ int evsched_schedule(evsched_t *s, event_t *ev, uint32_t dt)
/* Lock calendar. */
pthread_mutex_lock(&s->mx);
heap_insert(&s->heap, ev);
heap_insert(&s->heap, &ev);
/* Unlock calendar. */
pthread_cond_signal(&s->notify);
......@@ -299,7 +299,7 @@ int evsched_cancel(evsched_t *s, event_t *ev)
/* Lock calendar. */
pthread_mutex_lock(&s->mx);
if ((found = heap_find(&s->heap, ev))) {
if ((found = heap_find(&s->heap, &ev))) {
heap_delete(&s->heap, found);
}
......
......@@ -63,6 +63,7 @@ int heap_init(struct heap *h, int elm_size, int (*cmp)(void *, void *), int init
h->cmp = cmp;
h->swap = swap ? swap : _def_swap;
h->data = malloc((isize + 1) * elm_size);
h->elm_size = elm_size;
return h->data ? 1 : 0;
};
......@@ -119,7 +120,6 @@ int heap_insert(struct heap *h, void *e)
_heap_bubble_up(h,h->num);
}
return h->data ? 1 :0 ;
}
int heap_find(struct heap *h, void *elm) /* FIXME - very slow */
......
......@@ -41,9 +41,9 @@ struct heap {
#define HEAP_INCREASE_STEP 10
#define HEAP_DECREASE_THRESHOLD 50 /* h->num be divided by this number */
#define HTEMPELEMENT(h) ((h)->data) /* Pointer to tmp element (for swap) */
#define HHEAD(h) (void *)((h)->data + (h)->elm_size)
#define HELEMENT(h,num) ((h)->data + num * (h)->elm_size)
#define EMPTY_HEAP(h) ((h)->num) /* h->num > 0 */
#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 */
int heap_init(struct heap *, int, int (*cmp)(), int, void (*swap)());
void heap_delmin(struct heap *);
......
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