Skip to content

Commit e5f81bd

Browse files
spl: Use size_t to store spl_heap’s count (#19482)
1 parent b73d8b3 commit e5f81bd

File tree

1 file changed

+18
-23
lines changed

1 file changed

+18
-23
lines changed

ext/spl/spl_heap.c

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ typedef struct _spl_ptr_heap {
5050
spl_ptr_heap_ctor_func ctor;
5151
spl_ptr_heap_dtor_func dtor;
5252
spl_ptr_heap_cmp_func cmp;
53-
int count;
53+
size_t count;
5454
int flags;
5555
size_t max_size;
5656
size_t elem_size;
@@ -267,8 +267,6 @@ static spl_ptr_heap *spl_ptr_heap_init(spl_ptr_heap_cmp_func cmp, spl_ptr_heap_c
267267
/* }}} */
268268

269269
static void spl_ptr_heap_insert(spl_ptr_heap *heap, void *elem, void *cmp_userdata) { /* {{{ */
270-
int i;
271-
272270
if (heap->count+1 > heap->max_size) {
273271
size_t alloc_size = heap->max_size * heap->elem_size;
274272
/* we need to allocate more memory */
@@ -280,8 +278,9 @@ static void spl_ptr_heap_insert(spl_ptr_heap *heap, void *elem, void *cmp_userda
280278
heap->flags |= SPL_HEAP_WRITE_LOCKED;
281279

282280
/* sifting up */
283-
for (i = heap->count; i > 0 && heap->cmp(spl_heap_elem(heap, (i-1)/2), elem, cmp_userdata) < 0; i = (i-1)/2) {
284-
spl_heap_elem_copy(heap, spl_heap_elem(heap, i), spl_heap_elem(heap, (i-1)/2));
281+
size_t pos;
282+
for (pos = heap->count; pos > 0 && heap->cmp(spl_heap_elem(heap, (pos-1)/2), elem, cmp_userdata) < 0; pos = (pos-1)/2) {
283+
spl_heap_elem_copy(heap, spl_heap_elem(heap, pos), spl_heap_elem(heap, (pos-1)/2));
285284
}
286285
heap->count++;
287286

@@ -292,7 +291,7 @@ static void spl_ptr_heap_insert(spl_ptr_heap *heap, void *elem, void *cmp_userda
292291
heap->flags |= SPL_HEAP_CORRUPTED;
293292
}
294293

295-
spl_heap_elem_copy(heap, spl_heap_elem(heap, i), elem);
294+
spl_heap_elem_copy(heap, spl_heap_elem(heap, pos), elem);
296295
}
297296
/* }}} */
298297

@@ -306,8 +305,7 @@ static void *spl_ptr_heap_top(spl_ptr_heap *heap) { /* {{{ */
306305
/* }}} */
307306

308307
static zend_result spl_ptr_heap_delete_top(spl_ptr_heap *heap, void *elem, void *cmp_userdata) { /* {{{ */
309-
int i, j;
310-
const int limit = (heap->count-1)/2;
308+
const size_t limit = (heap->count-1)/2;
311309
void *bottom;
312310

313311
if (heap->count == 0) {
@@ -324,16 +322,17 @@ static zend_result spl_ptr_heap_delete_top(spl_ptr_heap *heap, void *elem, void
324322

325323
bottom = spl_heap_elem(heap, --heap->count);
326324

327-
for (i = 0; i < limit; i = j) {
325+
size_t parent_idx, child_idx;
326+
for (parent_idx = 0; parent_idx < limit; parent_idx = child_idx) {
328327
/* Find smaller child */
329-
j = i * 2 + 1;
330-
if (j != heap->count && heap->cmp(spl_heap_elem(heap, j+1), spl_heap_elem(heap, j), cmp_userdata) > 0) {
331-
j++; /* next child is bigger */
328+
child_idx = parent_idx * 2 + 1;
329+
if (child_idx != heap->count && heap->cmp(spl_heap_elem(heap, child_idx+1), spl_heap_elem(heap, child_idx), cmp_userdata) > 0) {
330+
child_idx++; /* next child is bigger */
332331
}
333332

334333
/* swap elements between two levels */
335-
if(heap->cmp(bottom, spl_heap_elem(heap, j), cmp_userdata) < 0) {
336-
spl_heap_elem_copy(heap, spl_heap_elem(heap, i), spl_heap_elem(heap, j));
334+
if(heap->cmp(bottom, spl_heap_elem(heap, child_idx), cmp_userdata) < 0) {
335+
spl_heap_elem_copy(heap, spl_heap_elem(heap, parent_idx), spl_heap_elem(heap, child_idx));
337336
} else {
338337
break;
339338
}
@@ -346,7 +345,7 @@ static zend_result spl_ptr_heap_delete_top(spl_ptr_heap *heap, void *elem, void
346345
heap->flags |= SPL_HEAP_CORRUPTED;
347346
}
348347

349-
void *to = spl_heap_elem(heap, i);
348+
void *to = spl_heap_elem(heap, parent_idx);
350349
if (to != bottom) {
351350
spl_heap_elem_copy(heap, to, bottom);
352351
}
@@ -355,8 +354,6 @@ static zend_result spl_ptr_heap_delete_top(spl_ptr_heap *heap, void *elem, void
355354
/* }}} */
356355

357356
static spl_ptr_heap *spl_ptr_heap_clone(spl_ptr_heap *from) { /* {{{ */
358-
int i;
359-
360357
spl_ptr_heap *heap = emalloc(sizeof(spl_ptr_heap));
361358

362359
heap->dtor = from->dtor;
@@ -370,7 +367,7 @@ static spl_ptr_heap *spl_ptr_heap_clone(spl_ptr_heap *from) { /* {{{ */
370367
heap->elements = safe_emalloc(from->elem_size, from->max_size, 0);
371368
memcpy(heap->elements, from->elements, from->elem_size * from->max_size);
372369

373-
for (i = 0; i < heap->count; ++i) {
370+
for (size_t i = 0; i < heap->count; ++i) {
374371
heap->ctor(spl_heap_elem(heap, i));
375372
}
376373

@@ -384,11 +381,9 @@ static void spl_ptr_heap_destroy(spl_ptr_heap *heap) { /* {{{ */
384381
return;
385382
}
386383

387-
int i;
388-
389384
heap->flags |= SPL_HEAP_WRITE_LOCKED;
390385

391-
for (i = 0; i < heap->count; ++i) {
386+
for (size_t i = 0; i < heap->count; ++i) {
392387
heap->dtor(spl_heap_elem(heap, i));
393388
}
394389

@@ -399,7 +394,7 @@ static void spl_ptr_heap_destroy(spl_ptr_heap *heap) { /* {{{ */
399394
}
400395
/* }}} */
401396

402-
static int spl_ptr_heap_count(spl_ptr_heap *heap) { /* {{{ */
397+
static size_t spl_ptr_heap_count(spl_ptr_heap *heap) { /* {{{ */
403398
return heap->count;
404399
}
405400
/* }}} */
@@ -534,7 +529,7 @@ static HashTable* spl_heap_object_get_debug_info(const zend_class_entry *ce, zen
534529

535530
array_init(&heap_array);
536531

537-
for (zend_ulong i = 0; i < intern->heap->count; ++i) {
532+
for (size_t i = 0; i < intern->heap->count; ++i) {
538533
if (ce == spl_ce_SplPriorityQueue) {
539534
spl_pqueue_elem *pq_elem = spl_heap_elem(intern->heap, i);
540535
zval elem;

0 commit comments

Comments
 (0)