1a74702c6SGeorge Wang/* Copyright (c) 2017 - 2022 LiteSpeed Technologies Inc. See LICENSE. */ 250aadb33SDmitri Tikhonov/* 350aadb33SDmitri Tikhonov * lsquic_arr.c 450aadb33SDmitri Tikhonov */ 550aadb33SDmitri Tikhonov 650aadb33SDmitri Tikhonov#include "lsquic_arr.h" 750aadb33SDmitri Tikhonov 850aadb33SDmitri Tikhonovint 950aadb33SDmitri Tikhonovlsquic_arr_push (struct lsquic_arr *arr, uintptr_t val) 1050aadb33SDmitri Tikhonov{ 1150aadb33SDmitri Tikhonov uintptr_t *new_els; 1250aadb33SDmitri Tikhonov unsigned n; 1350aadb33SDmitri Tikhonov 1450aadb33SDmitri Tikhonov if (arr->off + arr->nelem < arr->nalloc) 1550aadb33SDmitri Tikhonov { 1650aadb33SDmitri Tikhonov arr->els[arr->off + arr->nelem] = val; 1750aadb33SDmitri Tikhonov ++arr->nelem; 1850aadb33SDmitri Tikhonov return 0; 1950aadb33SDmitri Tikhonov } 2050aadb33SDmitri Tikhonov 2150aadb33SDmitri Tikhonov if (arr->off > arr->nalloc / 2) 2250aadb33SDmitri Tikhonov { 2350aadb33SDmitri Tikhonov memmove(arr->els, arr->els + arr->off, 2450aadb33SDmitri Tikhonov sizeof(arr->els[0]) * arr->nelem); 2550aadb33SDmitri Tikhonov arr->off = 0; 2650aadb33SDmitri Tikhonov arr->els[arr->nelem] = val; 2750aadb33SDmitri Tikhonov ++arr->nelem; 2850aadb33SDmitri Tikhonov return 0; 2950aadb33SDmitri Tikhonov } 3050aadb33SDmitri Tikhonov 3150aadb33SDmitri Tikhonov if (arr->nalloc) 3250aadb33SDmitri Tikhonov n = arr->nalloc * 2; 3350aadb33SDmitri Tikhonov else 3450aadb33SDmitri Tikhonov n = 64; 3550aadb33SDmitri Tikhonov new_els = malloc(n * sizeof(arr->els[0])); 3650aadb33SDmitri Tikhonov if (!new_els) 3750aadb33SDmitri Tikhonov return -1; 3850aadb33SDmitri Tikhonov memcpy(new_els, arr->els + arr->off, sizeof(arr->els[0]) * arr->nelem); 3950aadb33SDmitri Tikhonov free(arr->els); 4050aadb33SDmitri Tikhonov arr->off = 0; 4150aadb33SDmitri Tikhonov arr->els = new_els; 4250aadb33SDmitri Tikhonov arr->nalloc = n; 4350aadb33SDmitri Tikhonov arr->els[arr->off + arr->nelem] = val; 4450aadb33SDmitri Tikhonov ++arr->nelem; 4550aadb33SDmitri Tikhonov return 0; 4650aadb33SDmitri Tikhonov} 47c51ce338SDmitri Tikhonov 48c51ce338SDmitri Tikhonov 49c51ce338SDmitri Tikhonovsize_t 50c51ce338SDmitri Tikhonovlsquic_arr_mem_used (const struct lsquic_arr *arr) 51c51ce338SDmitri Tikhonov{ 52c51ce338SDmitri Tikhonov return sizeof(*arr) + arr->nalloc * sizeof(arr->els[0]); 53c51ce338SDmitri Tikhonov} 54