iomgr/timer_heap.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
20 
22 
23 #include <string.h>
24 
25 #include <grpc/support/alloc.h>
26 
29 
30 /* Adjusts a heap so as to move a hole at position i closer to the root,
31  until a suitable position is found for element t. Then, copies t into that
32  position. This functor is called each time immediately after modifying a
33  value in the underlying container, with the offset of the modified element as
34  its argument. */
36  while (i > 0) {
37  uint32_t parent = static_cast<uint32_t>((static_cast<int>(i) - 1) / 2);
38  if (first[parent]->deadline <= t->deadline) break;
39  first[i] = first[parent];
40  first[i]->heap_index = i;
41  i = parent;
42  }
43  first[i] = t;
44  t->heap_index = i;
45 }
46 
47 /* Adjusts a heap so as to move a hole at position i farther away from the root,
48  until a suitable position is found for element t. Then, copies t into that
49  position. */
51  grpc_timer* t) {
52  for (;;) {
53  uint32_t left_child = 1u + 2u * i;
54  if (left_child >= length) break;
55  uint32_t right_child = left_child + 1;
56  uint32_t next_i = right_child < length && first[left_child]->deadline >
57  first[right_child]->deadline
58  ? right_child
59  : left_child;
60  if (t->deadline <= first[next_i]->deadline) break;
61  first[i] = first[next_i];
62  first[i]->heap_index = i;
63  i = next_i;
64  }
65  first[i] = t;
66  t->heap_index = i;
67 }
68 
69 #define SHRINK_MIN_ELEMS 8
70 #define SHRINK_FULLNESS_FACTOR 2
71 
73  if (heap->timer_count >= 8 &&
74  heap->timer_count <= heap->timer_capacity / SHRINK_FULLNESS_FACTOR / 2) {
75  heap->timer_capacity = heap->timer_count * SHRINK_FULLNESS_FACTOR;
76  heap->timers = static_cast<grpc_timer**>(
77  gpr_realloc(heap->timers, heap->timer_capacity * sizeof(grpc_timer*)));
78  }
79 }
80 
82  uint32_t i = timer->heap_index;
83  uint32_t parent = static_cast<uint32_t>((static_cast<int>(i) - 1) / 2);
84  if (heap->timers[parent]->deadline > timer->deadline) {
85  adjust_upwards(heap->timers, i, timer);
86  } else {
87  adjust_downwards(heap->timers, i, heap->timer_count, timer);
88  }
89 }
90 
92  memset(heap, 0, sizeof(*heap));
93 }
94 
96 
98  if (heap->timer_count == heap->timer_capacity) {
99  heap->timer_capacity =
100  std::max(heap->timer_capacity + 1, heap->timer_capacity * 3 / 2);
101  heap->timers = static_cast<grpc_timer**>(
102  gpr_realloc(heap->timers, heap->timer_capacity * sizeof(grpc_timer*)));
103  }
104  timer->heap_index = heap->timer_count;
105  adjust_upwards(heap->timers, heap->timer_count, timer);
106  heap->timer_count++;
107  return timer->heap_index == 0;
108 }
109 
111  uint32_t i = timer->heap_index;
112  if (i == heap->timer_count - 1) {
113  heap->timer_count--;
115  return;
116  }
117  heap->timers[i] = heap->timers[heap->timer_count - 1];
118  heap->timers[i]->heap_index = i;
119  heap->timer_count--;
121  note_changed_priority(heap, heap->timers[i]);
122 }
123 
125  return heap->timer_count == 0;
126 }
127 
129  return heap->timers[0];
130 }
131 
134 }
adjust_upwards
static void adjust_upwards(grpc_timer **first, uint32_t i, grpc_timer *t)
Definition: iomgr/timer_heap.cc:35
memset
return memset(p, 0, total)
grpc_timer_heap_remove
void grpc_timer_heap_remove(grpc_timer_heap *heap, grpc_timer *timer)
Definition: iomgr/timer_heap.cc:110
timer_heap.h
string.h
gpr_free
GPRAPI void gpr_free(void *ptr)
Definition: alloc.cc:51
useful.h
u
OPENSSL_EXPORT pem_password_cb void * u
Definition: pem.h:351
grpc_timer
Definition: iomgr/timer.h:33
maybe_shrink
static void maybe_shrink(grpc_timer_heap *heap)
Definition: iomgr/timer_heap.cc:72
grpc_timer_heap_destroy
void grpc_timer_heap_destroy(grpc_timer_heap *heap)
Definition: iomgr/timer_heap.cc:95
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
grpc_timer_heap_add
bool grpc_timer_heap_add(grpc_timer_heap *heap, grpc_timer *timer)
Definition: iomgr/timer_heap.cc:97
gpr_realloc
GPRAPI void * gpr_realloc(void *p, size_t size)
Definition: alloc.cc:56
max
int max
Definition: bloaty/third_party/zlib/examples/enough.c:170
note_changed_priority
static void note_changed_priority(grpc_timer_heap *heap, grpc_timer *timer)
Definition: iomgr/timer_heap.cc:81
SHRINK_FULLNESS_FACTOR
#define SHRINK_FULLNESS_FACTOR
Definition: iomgr/timer_heap.cc:70
grpc_timer_heap_init
void grpc_timer_heap_init(grpc_timer_heap *heap)
Definition: iomgr/timer_heap.cc:91
grpc_timer_heap_is_empty
bool grpc_timer_heap_is_empty(grpc_timer_heap *heap)
Definition: iomgr/timer_heap.cc:124
grpc_timer_heap_pop
void grpc_timer_heap_pop(grpc_timer_heap *heap)
Definition: iomgr/timer_heap.cc:132
grpc_timer_heap_top
grpc_timer * grpc_timer_heap_top(grpc_timer_heap *heap)
Definition: iomgr/timer_heap.cc:128
adjust_downwards
static void adjust_downwards(grpc_timer **first, uint32_t i, uint32_t length, grpc_timer *t)
Definition: iomgr/timer_heap.cc:50
port.h
alloc.h
grpc_timer_heap
Definition: iomgr/timer_heap.h:26
first
StrT first
Definition: cxa_demangle.cpp:4884
length
std::size_t length
Definition: abseil-cpp/absl/time/internal/test_util.cc:57
heap
Definition: heap-inl.h:40
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
timer
static uv_timer_t timer
Definition: test-callback-stack.c:34
port_platform.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:39