lsquic_arr.h revision 06b2a236
1/* Copyright (c) 2017 - 2021 LiteSpeed Technologies Inc.  See LICENSE. */
2/*
3 * lsquic_arr.h -- Array
4 */
5
6#ifndef LSQUIC_ARR_H
7#define LSQUIC_ARR_H 1
8
9#include <assert.h>
10#include <string.h>
11#include <stdint.h>
12#include <stdlib.h>
13
14struct lsquic_arr
15{
16    unsigned        nalloc,
17                    nelem,
18                    off;
19    uintptr_t      *els;
20};
21
22
23#define lsquic_arr_init(a) do {                                         \
24    memset((a), 0, sizeof(*(a)));                                       \
25} while (0)
26
27#define lsquic_arr_cleanup(a) do {                                      \
28    free((a)->els);                                                     \
29    memset((a), 0, sizeof(*(a)));                                       \
30} while (0)
31
32#define lsquic_arr_clear(a) do {                                        \
33    (a)->off = 0;                                                       \
34    (a)->nelem = 0;                                                     \
35} while (0)
36
37#define lsquic_arr_get(a, i) (                                          \
38    assert((i) < (a)->nelem),                                           \
39    (a)->els[(a)->off + (i)]                                            \
40)
41
42#define lsquic_arr_shift(a) (                                           \
43    assert((a)->nelem > 0),                                             \
44    (a)->nelem -= 1,                                                    \
45    (a)->els[(a)->off++]                                                \
46)
47
48#define lsquic_arr_peek(a) (                                            \
49    assert((a)->nelem > 0),                                             \
50    (a)->els[(a)->off]                                                  \
51)
52
53#define lsquic_arr_pop(a) (                                             \
54    assert((a)->nelem > 0),                                             \
55    (a)->nelem -= 1,                                                    \
56    (a)->els[(a)->off + (a)->nelem]                                     \
57)
58
59#define lsquic_arr_count(a) (+(a)->nelem)
60
61int
62lsquic_arr_push (struct lsquic_arr *, uintptr_t);
63
64size_t
65lsquic_arr_mem_used (const struct lsquic_arr *);
66
67#endif
68