lsquic_arr.c revision 229fce07
1/* Copyright (c) 2017 - 2019 LiteSpeed Technologies Inc. See LICENSE. */ 2/* 3 * lsquic_arr.c 4 */ 5 6#include "lsquic_arr.h" 7 8int 9lsquic_arr_push (struct lsquic_arr *arr, uintptr_t val) 10{ 11 uintptr_t *new_els; 12 unsigned n; 13 14 if (arr->off + arr->nelem < arr->nalloc) 15 { 16 arr->els[arr->off + arr->nelem] = val; 17 ++arr->nelem; 18 return 0; 19 } 20 21 if (arr->off > arr->nalloc / 2) 22 { 23 memmove(arr->els, arr->els + arr->off, 24 sizeof(arr->els[0]) * arr->nelem); 25 arr->off = 0; 26 arr->els[arr->nelem] = val; 27 ++arr->nelem; 28 return 0; 29 } 30 31 if (arr->nalloc) 32 n = arr->nalloc * 2; 33 else 34 n = 64; 35 new_els = malloc(n * sizeof(arr->els[0])); 36 if (!new_els) 37 return -1; 38 memcpy(new_els, arr->els + arr->off, sizeof(arr->els[0]) * arr->nelem); 39 free(arr->els); 40 arr->off = 0; 41 arr->els = new_els; 42 arr->nalloc = n; 43 arr->els[arr->off + arr->nelem] = val; 44 ++arr->nelem; 45 return 0; 46} 47 48 49size_t 50lsquic_arr_mem_used (const struct lsquic_arr *arr) 51{ 52 return sizeof(*arr) + arr->nalloc * sizeof(arr->els[0]); 53} 54