1*663afb9bSAndroid Build Coastguard Worker /*
2*663afb9bSAndroid Build Coastguard Worker * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
3*663afb9bSAndroid Build Coastguard Worker *
4*663afb9bSAndroid Build Coastguard Worker * Copyright (c) 2006 Maxim Yegorushkin <[email protected]>
5*663afb9bSAndroid Build Coastguard Worker *
6*663afb9bSAndroid Build Coastguard Worker * Redistribution and use in source and binary forms, with or without
7*663afb9bSAndroid Build Coastguard Worker * modification, are permitted provided that the following conditions
8*663afb9bSAndroid Build Coastguard Worker * are met:
9*663afb9bSAndroid Build Coastguard Worker * 1. Redistributions of source code must retain the above copyright
10*663afb9bSAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer.
11*663afb9bSAndroid Build Coastguard Worker * 2. Redistributions in binary form must reproduce the above copyright
12*663afb9bSAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer in the
13*663afb9bSAndroid Build Coastguard Worker * documentation and/or other materials provided with the distribution.
14*663afb9bSAndroid Build Coastguard Worker * 3. The name of the author may not be used to endorse or promote products
15*663afb9bSAndroid Build Coastguard Worker * derived from this software without specific prior written permission.
16*663afb9bSAndroid Build Coastguard Worker *
17*663afb9bSAndroid Build Coastguard Worker * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18*663afb9bSAndroid Build Coastguard Worker * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19*663afb9bSAndroid Build Coastguard Worker * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20*663afb9bSAndroid Build Coastguard Worker * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21*663afb9bSAndroid Build Coastguard Worker * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22*663afb9bSAndroid Build Coastguard Worker * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23*663afb9bSAndroid Build Coastguard Worker * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24*663afb9bSAndroid Build Coastguard Worker * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25*663afb9bSAndroid Build Coastguard Worker * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26*663afb9bSAndroid Build Coastguard Worker * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*663afb9bSAndroid Build Coastguard Worker */
28*663afb9bSAndroid Build Coastguard Worker #ifndef MINHEAP_INTERNAL_H_INCLUDED_
29*663afb9bSAndroid Build Coastguard Worker #define MINHEAP_INTERNAL_H_INCLUDED_
30*663afb9bSAndroid Build Coastguard Worker
31*663afb9bSAndroid Build Coastguard Worker #include "event2/event-config.h"
32*663afb9bSAndroid Build Coastguard Worker #include "evconfig-private.h"
33*663afb9bSAndroid Build Coastguard Worker #include "event2/event.h"
34*663afb9bSAndroid Build Coastguard Worker #include "event2/event_struct.h"
35*663afb9bSAndroid Build Coastguard Worker #include "event2/util.h"
36*663afb9bSAndroid Build Coastguard Worker #include "util-internal.h"
37*663afb9bSAndroid Build Coastguard Worker #include "mm-internal.h"
38*663afb9bSAndroid Build Coastguard Worker
39*663afb9bSAndroid Build Coastguard Worker typedef struct min_heap
40*663afb9bSAndroid Build Coastguard Worker {
41*663afb9bSAndroid Build Coastguard Worker struct event** p;
42*663afb9bSAndroid Build Coastguard Worker unsigned n, a;
43*663afb9bSAndroid Build Coastguard Worker } min_heap_t;
44*663afb9bSAndroid Build Coastguard Worker
45*663afb9bSAndroid Build Coastguard Worker static inline void min_heap_ctor_(min_heap_t* s);
46*663afb9bSAndroid Build Coastguard Worker static inline void min_heap_dtor_(min_heap_t* s);
47*663afb9bSAndroid Build Coastguard Worker static inline void min_heap_elem_init_(struct event* e);
48*663afb9bSAndroid Build Coastguard Worker static inline int min_heap_elt_is_top_(const struct event *e);
49*663afb9bSAndroid Build Coastguard Worker static inline int min_heap_empty_(min_heap_t* s);
50*663afb9bSAndroid Build Coastguard Worker static inline unsigned min_heap_size_(min_heap_t* s);
51*663afb9bSAndroid Build Coastguard Worker static inline struct event* min_heap_top_(min_heap_t* s);
52*663afb9bSAndroid Build Coastguard Worker static inline int min_heap_reserve_(min_heap_t* s, unsigned n);
53*663afb9bSAndroid Build Coastguard Worker static inline int min_heap_push_(min_heap_t* s, struct event* e);
54*663afb9bSAndroid Build Coastguard Worker static inline struct event* min_heap_pop_(min_heap_t* s);
55*663afb9bSAndroid Build Coastguard Worker static inline int min_heap_adjust_(min_heap_t *s, struct event* e);
56*663afb9bSAndroid Build Coastguard Worker static inline int min_heap_erase_(min_heap_t* s, struct event* e);
57*663afb9bSAndroid Build Coastguard Worker static inline void min_heap_shift_up_(min_heap_t* s, unsigned hole_index, struct event* e);
58*663afb9bSAndroid Build Coastguard Worker static inline void min_heap_shift_up_unconditional_(min_heap_t* s, unsigned hole_index, struct event* e);
59*663afb9bSAndroid Build Coastguard Worker static inline void min_heap_shift_down_(min_heap_t* s, unsigned hole_index, struct event* e);
60*663afb9bSAndroid Build Coastguard Worker
61*663afb9bSAndroid Build Coastguard Worker #define min_heap_elem_greater(a, b) \
62*663afb9bSAndroid Build Coastguard Worker (evutil_timercmp(&(a)->ev_timeout, &(b)->ev_timeout, >))
63*663afb9bSAndroid Build Coastguard Worker
min_heap_ctor_(min_heap_t * s)64*663afb9bSAndroid Build Coastguard Worker void min_heap_ctor_(min_heap_t* s) { s->p = 0; s->n = 0; s->a = 0; }
min_heap_dtor_(min_heap_t * s)65*663afb9bSAndroid Build Coastguard Worker void min_heap_dtor_(min_heap_t* s) { if (s->p) mm_free(s->p); }
min_heap_elem_init_(struct event * e)66*663afb9bSAndroid Build Coastguard Worker void min_heap_elem_init_(struct event* e) { e->ev_timeout_pos.min_heap_idx = -1; }
min_heap_empty_(min_heap_t * s)67*663afb9bSAndroid Build Coastguard Worker int min_heap_empty_(min_heap_t* s) { return 0u == s->n; }
min_heap_size_(min_heap_t * s)68*663afb9bSAndroid Build Coastguard Worker unsigned min_heap_size_(min_heap_t* s) { return s->n; }
min_heap_top_(min_heap_t * s)69*663afb9bSAndroid Build Coastguard Worker struct event* min_heap_top_(min_heap_t* s) { return s->n ? *s->p : 0; }
70*663afb9bSAndroid Build Coastguard Worker
min_heap_push_(min_heap_t * s,struct event * e)71*663afb9bSAndroid Build Coastguard Worker int min_heap_push_(min_heap_t* s, struct event* e)
72*663afb9bSAndroid Build Coastguard Worker {
73*663afb9bSAndroid Build Coastguard Worker if (s->n == UINT32_MAX || min_heap_reserve_(s, s->n + 1))
74*663afb9bSAndroid Build Coastguard Worker return -1;
75*663afb9bSAndroid Build Coastguard Worker min_heap_shift_up_(s, s->n++, e);
76*663afb9bSAndroid Build Coastguard Worker return 0;
77*663afb9bSAndroid Build Coastguard Worker }
78*663afb9bSAndroid Build Coastguard Worker
min_heap_pop_(min_heap_t * s)79*663afb9bSAndroid Build Coastguard Worker struct event* min_heap_pop_(min_heap_t* s)
80*663afb9bSAndroid Build Coastguard Worker {
81*663afb9bSAndroid Build Coastguard Worker if (s->n)
82*663afb9bSAndroid Build Coastguard Worker {
83*663afb9bSAndroid Build Coastguard Worker struct event* e = *s->p;
84*663afb9bSAndroid Build Coastguard Worker min_heap_shift_down_(s, 0u, s->p[--s->n]);
85*663afb9bSAndroid Build Coastguard Worker e->ev_timeout_pos.min_heap_idx = -1;
86*663afb9bSAndroid Build Coastguard Worker return e;
87*663afb9bSAndroid Build Coastguard Worker }
88*663afb9bSAndroid Build Coastguard Worker return 0;
89*663afb9bSAndroid Build Coastguard Worker }
90*663afb9bSAndroid Build Coastguard Worker
min_heap_elt_is_top_(const struct event * e)91*663afb9bSAndroid Build Coastguard Worker int min_heap_elt_is_top_(const struct event *e)
92*663afb9bSAndroid Build Coastguard Worker {
93*663afb9bSAndroid Build Coastguard Worker return e->ev_timeout_pos.min_heap_idx == 0;
94*663afb9bSAndroid Build Coastguard Worker }
95*663afb9bSAndroid Build Coastguard Worker
min_heap_erase_(min_heap_t * s,struct event * e)96*663afb9bSAndroid Build Coastguard Worker int min_heap_erase_(min_heap_t* s, struct event* e)
97*663afb9bSAndroid Build Coastguard Worker {
98*663afb9bSAndroid Build Coastguard Worker if (-1 != e->ev_timeout_pos.min_heap_idx)
99*663afb9bSAndroid Build Coastguard Worker {
100*663afb9bSAndroid Build Coastguard Worker struct event *last = s->p[--s->n];
101*663afb9bSAndroid Build Coastguard Worker unsigned parent = (e->ev_timeout_pos.min_heap_idx - 1) / 2;
102*663afb9bSAndroid Build Coastguard Worker /* we replace e with the last element in the heap. We might need to
103*663afb9bSAndroid Build Coastguard Worker shift it upward if it is less than its parent, or downward if it is
104*663afb9bSAndroid Build Coastguard Worker greater than one or both its children. Since the children are known
105*663afb9bSAndroid Build Coastguard Worker to be less than the parent, it can't need to shift both up and
106*663afb9bSAndroid Build Coastguard Worker down. */
107*663afb9bSAndroid Build Coastguard Worker if (e->ev_timeout_pos.min_heap_idx > 0 && min_heap_elem_greater(s->p[parent], last))
108*663afb9bSAndroid Build Coastguard Worker min_heap_shift_up_unconditional_(s, e->ev_timeout_pos.min_heap_idx, last);
109*663afb9bSAndroid Build Coastguard Worker else
110*663afb9bSAndroid Build Coastguard Worker min_heap_shift_down_(s, e->ev_timeout_pos.min_heap_idx, last);
111*663afb9bSAndroid Build Coastguard Worker e->ev_timeout_pos.min_heap_idx = -1;
112*663afb9bSAndroid Build Coastguard Worker return 0;
113*663afb9bSAndroid Build Coastguard Worker }
114*663afb9bSAndroid Build Coastguard Worker return -1;
115*663afb9bSAndroid Build Coastguard Worker }
116*663afb9bSAndroid Build Coastguard Worker
min_heap_adjust_(min_heap_t * s,struct event * e)117*663afb9bSAndroid Build Coastguard Worker int min_heap_adjust_(min_heap_t *s, struct event *e)
118*663afb9bSAndroid Build Coastguard Worker {
119*663afb9bSAndroid Build Coastguard Worker if (-1 == e->ev_timeout_pos.min_heap_idx) {
120*663afb9bSAndroid Build Coastguard Worker return min_heap_push_(s, e);
121*663afb9bSAndroid Build Coastguard Worker } else {
122*663afb9bSAndroid Build Coastguard Worker unsigned parent = (e->ev_timeout_pos.min_heap_idx - 1) / 2;
123*663afb9bSAndroid Build Coastguard Worker /* The position of e has changed; we shift it up or down
124*663afb9bSAndroid Build Coastguard Worker * as needed. We can't need to do both. */
125*663afb9bSAndroid Build Coastguard Worker if (e->ev_timeout_pos.min_heap_idx > 0 && min_heap_elem_greater(s->p[parent], e))
126*663afb9bSAndroid Build Coastguard Worker min_heap_shift_up_unconditional_(s, e->ev_timeout_pos.min_heap_idx, e);
127*663afb9bSAndroid Build Coastguard Worker else
128*663afb9bSAndroid Build Coastguard Worker min_heap_shift_down_(s, e->ev_timeout_pos.min_heap_idx, e);
129*663afb9bSAndroid Build Coastguard Worker return 0;
130*663afb9bSAndroid Build Coastguard Worker }
131*663afb9bSAndroid Build Coastguard Worker }
132*663afb9bSAndroid Build Coastguard Worker
min_heap_reserve_(min_heap_t * s,unsigned n)133*663afb9bSAndroid Build Coastguard Worker int min_heap_reserve_(min_heap_t* s, unsigned n)
134*663afb9bSAndroid Build Coastguard Worker {
135*663afb9bSAndroid Build Coastguard Worker if (s->a < n)
136*663afb9bSAndroid Build Coastguard Worker {
137*663afb9bSAndroid Build Coastguard Worker struct event** p;
138*663afb9bSAndroid Build Coastguard Worker unsigned a = s->a ? s->a * 2 : 8;
139*663afb9bSAndroid Build Coastguard Worker if (a < n)
140*663afb9bSAndroid Build Coastguard Worker a = n;
141*663afb9bSAndroid Build Coastguard Worker #if (SIZE_MAX == UINT32_MAX)
142*663afb9bSAndroid Build Coastguard Worker if (a > SIZE_MAX / sizeof *p)
143*663afb9bSAndroid Build Coastguard Worker return -1;
144*663afb9bSAndroid Build Coastguard Worker #endif
145*663afb9bSAndroid Build Coastguard Worker if (!(p = (struct event**)mm_realloc(s->p, a * sizeof *p)))
146*663afb9bSAndroid Build Coastguard Worker return -1;
147*663afb9bSAndroid Build Coastguard Worker s->p = p;
148*663afb9bSAndroid Build Coastguard Worker s->a = a;
149*663afb9bSAndroid Build Coastguard Worker }
150*663afb9bSAndroid Build Coastguard Worker return 0;
151*663afb9bSAndroid Build Coastguard Worker }
152*663afb9bSAndroid Build Coastguard Worker
min_heap_shift_up_unconditional_(min_heap_t * s,unsigned hole_index,struct event * e)153*663afb9bSAndroid Build Coastguard Worker void min_heap_shift_up_unconditional_(min_heap_t* s, unsigned hole_index, struct event* e)
154*663afb9bSAndroid Build Coastguard Worker {
155*663afb9bSAndroid Build Coastguard Worker unsigned parent = (hole_index - 1) / 2;
156*663afb9bSAndroid Build Coastguard Worker do
157*663afb9bSAndroid Build Coastguard Worker {
158*663afb9bSAndroid Build Coastguard Worker (s->p[hole_index] = s->p[parent])->ev_timeout_pos.min_heap_idx = hole_index;
159*663afb9bSAndroid Build Coastguard Worker hole_index = parent;
160*663afb9bSAndroid Build Coastguard Worker parent = (hole_index - 1) / 2;
161*663afb9bSAndroid Build Coastguard Worker } while (hole_index && min_heap_elem_greater(s->p[parent], e));
162*663afb9bSAndroid Build Coastguard Worker (s->p[hole_index] = e)->ev_timeout_pos.min_heap_idx = hole_index;
163*663afb9bSAndroid Build Coastguard Worker }
164*663afb9bSAndroid Build Coastguard Worker
min_heap_shift_up_(min_heap_t * s,unsigned hole_index,struct event * e)165*663afb9bSAndroid Build Coastguard Worker void min_heap_shift_up_(min_heap_t* s, unsigned hole_index, struct event* e)
166*663afb9bSAndroid Build Coastguard Worker {
167*663afb9bSAndroid Build Coastguard Worker unsigned parent = (hole_index - 1) / 2;
168*663afb9bSAndroid Build Coastguard Worker while (hole_index && min_heap_elem_greater(s->p[parent], e))
169*663afb9bSAndroid Build Coastguard Worker {
170*663afb9bSAndroid Build Coastguard Worker (s->p[hole_index] = s->p[parent])->ev_timeout_pos.min_heap_idx = hole_index;
171*663afb9bSAndroid Build Coastguard Worker hole_index = parent;
172*663afb9bSAndroid Build Coastguard Worker parent = (hole_index - 1) / 2;
173*663afb9bSAndroid Build Coastguard Worker }
174*663afb9bSAndroid Build Coastguard Worker (s->p[hole_index] = e)->ev_timeout_pos.min_heap_idx = hole_index;
175*663afb9bSAndroid Build Coastguard Worker }
176*663afb9bSAndroid Build Coastguard Worker
min_heap_shift_down_(min_heap_t * s,unsigned hole_index,struct event * e)177*663afb9bSAndroid Build Coastguard Worker void min_heap_shift_down_(min_heap_t* s, unsigned hole_index, struct event* e)
178*663afb9bSAndroid Build Coastguard Worker {
179*663afb9bSAndroid Build Coastguard Worker unsigned min_child = 2 * (hole_index + 1);
180*663afb9bSAndroid Build Coastguard Worker while (min_child <= s->n)
181*663afb9bSAndroid Build Coastguard Worker {
182*663afb9bSAndroid Build Coastguard Worker min_child -= min_child == s->n || min_heap_elem_greater(s->p[min_child], s->p[min_child - 1]);
183*663afb9bSAndroid Build Coastguard Worker if (!(min_heap_elem_greater(e, s->p[min_child])))
184*663afb9bSAndroid Build Coastguard Worker break;
185*663afb9bSAndroid Build Coastguard Worker (s->p[hole_index] = s->p[min_child])->ev_timeout_pos.min_heap_idx = hole_index;
186*663afb9bSAndroid Build Coastguard Worker hole_index = min_child;
187*663afb9bSAndroid Build Coastguard Worker min_child = 2 * (hole_index + 1);
188*663afb9bSAndroid Build Coastguard Worker }
189*663afb9bSAndroid Build Coastguard Worker (s->p[hole_index] = e)->ev_timeout_pos.min_heap_idx = hole_index;
190*663afb9bSAndroid Build Coastguard Worker }
191*663afb9bSAndroid Build Coastguard Worker
192*663afb9bSAndroid Build Coastguard Worker #endif /* MINHEAP_INTERNAL_H_INCLUDED_ */
193