1a74702c6SGeorge Wang/* Copyright (c) 2017 - 2022 LiteSpeed Technologies Inc.  See LICENSE. */
250aadb33SDmitri Tikhonov/*
350aadb33SDmitri Tikhonov * lsquic_arr.h -- Array
450aadb33SDmitri Tikhonov */
550aadb33SDmitri Tikhonov
650aadb33SDmitri Tikhonov#ifndef LSQUIC_ARR_H
750aadb33SDmitri Tikhonov#define LSQUIC_ARR_H 1
850aadb33SDmitri Tikhonov
950aadb33SDmitri Tikhonov#include <assert.h>
1050aadb33SDmitri Tikhonov#include <string.h>
1150aadb33SDmitri Tikhonov#include <stdint.h>
1250aadb33SDmitri Tikhonov#include <stdlib.h>
1350aadb33SDmitri Tikhonov
1450aadb33SDmitri Tikhonovstruct lsquic_arr
1550aadb33SDmitri Tikhonov{
1650aadb33SDmitri Tikhonov    unsigned        nalloc,
1750aadb33SDmitri Tikhonov                    nelem,
1850aadb33SDmitri Tikhonov                    off;
1950aadb33SDmitri Tikhonov    uintptr_t      *els;
2050aadb33SDmitri Tikhonov};
2150aadb33SDmitri Tikhonov
2250aadb33SDmitri Tikhonov
2350aadb33SDmitri Tikhonov#define lsquic_arr_init(a) do {                                         \
2450aadb33SDmitri Tikhonov    memset((a), 0, sizeof(*(a)));                                       \
2550aadb33SDmitri Tikhonov} while (0)
2650aadb33SDmitri Tikhonov
2750aadb33SDmitri Tikhonov#define lsquic_arr_cleanup(a) do {                                      \
2850aadb33SDmitri Tikhonov    free((a)->els);                                                     \
2950aadb33SDmitri Tikhonov    memset((a), 0, sizeof(*(a)));                                       \
3050aadb33SDmitri Tikhonov} while (0)
3150aadb33SDmitri Tikhonov
3250aadb33SDmitri Tikhonov#define lsquic_arr_clear(a) do {                                        \
3350aadb33SDmitri Tikhonov    (a)->off = 0;                                                       \
3450aadb33SDmitri Tikhonov    (a)->nelem = 0;                                                     \
3550aadb33SDmitri Tikhonov} while (0)
3650aadb33SDmitri Tikhonov
3750aadb33SDmitri Tikhonov#define lsquic_arr_get(a, i) (                                          \
3850aadb33SDmitri Tikhonov    assert((i) < (a)->nelem),                                           \
3950aadb33SDmitri Tikhonov    (a)->els[(a)->off + (i)]                                            \
4050aadb33SDmitri Tikhonov)
4150aadb33SDmitri Tikhonov
4250aadb33SDmitri Tikhonov#define lsquic_arr_shift(a) (                                           \
4350aadb33SDmitri Tikhonov    assert((a)->nelem > 0),                                             \
4450aadb33SDmitri Tikhonov    (a)->nelem -= 1,                                                    \
4550aadb33SDmitri Tikhonov    (a)->els[(a)->off++]                                                \
4650aadb33SDmitri Tikhonov)
4750aadb33SDmitri Tikhonov
4850aadb33SDmitri Tikhonov#define lsquic_arr_peek(a) (                                            \
4950aadb33SDmitri Tikhonov    assert((a)->nelem > 0),                                             \
5050aadb33SDmitri Tikhonov    (a)->els[(a)->off]                                                  \
5150aadb33SDmitri Tikhonov)
5250aadb33SDmitri Tikhonov
5350aadb33SDmitri Tikhonov#define lsquic_arr_pop(a) (                                             \
5450aadb33SDmitri Tikhonov    assert((a)->nelem > 0),                                             \
5550aadb33SDmitri Tikhonov    (a)->nelem -= 1,                                                    \
5650aadb33SDmitri Tikhonov    (a)->els[(a)->off + (a)->nelem]                                     \
5750aadb33SDmitri Tikhonov)
5850aadb33SDmitri Tikhonov
5950aadb33SDmitri Tikhonov#define lsquic_arr_count(a) (+(a)->nelem)
6050aadb33SDmitri Tikhonov
6150aadb33SDmitri Tikhonovint
6250aadb33SDmitri Tikhonovlsquic_arr_push (struct lsquic_arr *, uintptr_t);
6350aadb33SDmitri Tikhonov
64c51ce338SDmitri Tikhonovsize_t
65c51ce338SDmitri Tikhonovlsquic_arr_mem_used (const struct lsquic_arr *);
66c51ce338SDmitri Tikhonov
6750aadb33SDmitri Tikhonov#endif
68