diff options
Diffstat (limited to 'src/core/lib/iomgr/timer_heap.cc')
-rw-r--r-- | src/core/lib/iomgr/timer_heap.cc | 41 |
1 files changed, 20 insertions, 21 deletions
diff --git a/src/core/lib/iomgr/timer_heap.cc b/src/core/lib/iomgr/timer_heap.cc index 2648d5da5d..b350452c63 100644 --- a/src/core/lib/iomgr/timer_heap.cc +++ b/src/core/lib/iomgr/timer_heap.cc @@ -32,7 +32,7 @@ position. This functor is called each time immediately after modifying a value in the underlying container, with the offset of the modified element as its argument. */ -static void adjust_upwards(grpc_timer **first, uint32_t i, grpc_timer *t) { +static void adjust_upwards(grpc_timer** first, uint32_t i, grpc_timer* t) { while (i > 0) { uint32_t parent = (uint32_t)(((int)i - 1) / 2); if (first[parent]->deadline <= t->deadline) break; @@ -47,17 +47,16 @@ static void adjust_upwards(grpc_timer **first, uint32_t i, grpc_timer *t) { /* Adjusts a heap so as to move a hole at position i farther away from the root, until a suitable position is found for element t. Then, copies t into that position. */ -static void adjust_downwards(grpc_timer **first, uint32_t i, uint32_t length, - grpc_timer *t) { +static void adjust_downwards(grpc_timer** first, uint32_t i, uint32_t length, + grpc_timer* t) { for (;;) { uint32_t left_child = 1u + 2u * i; if (left_child >= length) break; uint32_t right_child = left_child + 1; - uint32_t next_i = - right_child < length && - first[left_child]->deadline > first[right_child]->deadline - ? right_child - : left_child; + uint32_t next_i = right_child < length && first[left_child]->deadline > + first[right_child]->deadline + ? right_child + : left_child; if (t->deadline <= first[next_i]->deadline) break; first[i] = first[next_i]; first[i]->heap_index = i; @@ -70,16 +69,16 @@ static void adjust_downwards(grpc_timer **first, uint32_t i, uint32_t length, #define SHRINK_MIN_ELEMS 8 #define SHRINK_FULLNESS_FACTOR 2 -static void maybe_shrink(grpc_timer_heap *heap) { +static void maybe_shrink(grpc_timer_heap* heap) { if (heap->timer_count >= 8 && heap->timer_count <= heap->timer_capacity / SHRINK_FULLNESS_FACTOR / 2) { heap->timer_capacity = heap->timer_count * SHRINK_FULLNESS_FACTOR; - heap->timers = (grpc_timer **)gpr_realloc( - heap->timers, heap->timer_capacity * sizeof(grpc_timer *)); + heap->timers = (grpc_timer**)gpr_realloc( + heap->timers, heap->timer_capacity * sizeof(grpc_timer*)); } } -static void note_changed_priority(grpc_timer_heap *heap, grpc_timer *timer) { +static void note_changed_priority(grpc_timer_heap* heap, grpc_timer* timer) { uint32_t i = timer->heap_index; uint32_t parent = (uint32_t)(((int)i - 1) / 2); if (heap->timers[parent]->deadline > timer->deadline) { @@ -89,18 +88,18 @@ static void note_changed_priority(grpc_timer_heap *heap, grpc_timer *timer) { } } -void grpc_timer_heap_init(grpc_timer_heap *heap) { +void grpc_timer_heap_init(grpc_timer_heap* heap) { memset(heap, 0, sizeof(*heap)); } -void grpc_timer_heap_destroy(grpc_timer_heap *heap) { gpr_free(heap->timers); } +void grpc_timer_heap_destroy(grpc_timer_heap* heap) { gpr_free(heap->timers); } -int grpc_timer_heap_add(grpc_timer_heap *heap, grpc_timer *timer) { +int grpc_timer_heap_add(grpc_timer_heap* heap, grpc_timer* timer) { if (heap->timer_count == heap->timer_capacity) { heap->timer_capacity = GPR_MAX(heap->timer_capacity + 1, heap->timer_capacity * 3 / 2); - heap->timers = (grpc_timer **)gpr_realloc( - heap->timers, heap->timer_capacity * sizeof(grpc_timer *)); + heap->timers = (grpc_timer**)gpr_realloc( + heap->timers, heap->timer_capacity * sizeof(grpc_timer*)); } timer->heap_index = heap->timer_count; adjust_upwards(heap->timers, heap->timer_count, timer); @@ -108,7 +107,7 @@ int grpc_timer_heap_add(grpc_timer_heap *heap, grpc_timer *timer) { return timer->heap_index == 0; } -void grpc_timer_heap_remove(grpc_timer_heap *heap, grpc_timer *timer) { +void grpc_timer_heap_remove(grpc_timer_heap* heap, grpc_timer* timer) { uint32_t i = timer->heap_index; if (i == heap->timer_count - 1) { heap->timer_count--; @@ -122,15 +121,15 @@ void grpc_timer_heap_remove(grpc_timer_heap *heap, grpc_timer *timer) { note_changed_priority(heap, heap->timers[i]); } -int grpc_timer_heap_is_empty(grpc_timer_heap *heap) { +int grpc_timer_heap_is_empty(grpc_timer_heap* heap) { return heap->timer_count == 0; } -grpc_timer *grpc_timer_heap_top(grpc_timer_heap *heap) { +grpc_timer* grpc_timer_heap_top(grpc_timer_heap* heap) { return heap->timers[0]; } -void grpc_timer_heap_pop(grpc_timer_heap *heap) { +void grpc_timer_heap_pop(grpc_timer_heap* heap) { grpc_timer_heap_remove(heap, grpc_timer_heap_top(heap)); } |