summaryrefslogtreecommitdiffhomepage
path: root/src/nxt_array.h
blob: f06ff14c86b46d40fd4564a5cc3a8fa1d9f9e3e8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

/*
 * Copyright (C) Igor Sysoev
 * Copyright (C) NGINX, Inc.
 */

#ifndef _NXT_ARRAY_H_INCLUDED_
#define _NXT_ARRAY_H_INCLUDED_


typedef struct {
    void      *elts;
    /* nelts has uint32_t type because it is used most often. */
    uint32_t  nelts;
    uint16_t  size;
    uint16_t  nalloc;
    nxt_mp_t  *mem_pool;
} nxt_array_t;


nxt_inline void
nxt_array_init(nxt_array_t *array, nxt_mp_t *mp, size_t size)
{
    array->elts = nxt_pointer_to(array, sizeof(nxt_array_t));
    array->size = size;
    array->mem_pool = mp;
}

NXT_EXPORT nxt_array_t *nxt_array_create(nxt_mp_t *mp, nxt_uint_t n,
    size_t size);
NXT_EXPORT void nxt_array_destroy(nxt_array_t *array);
NXT_EXPORT void *nxt_array_add(nxt_array_t *array);
NXT_EXPORT void *nxt_array_zero_add(nxt_array_t *array);
NXT_EXPORT void nxt_array_remove(nxt_array_t *array, void *elt);
NXT_EXPORT nxt_array_t *nxt_array_copy(nxt_mp_t *mp, nxt_array_t *dst,
    nxt_array_t *src);

#define nxt_array_last(array)                                                 \
    nxt_pointer_to((array)->elts, (array)->size * ((array)->nelts - 1))


#define nxt_array_reset(array)                                                \
    (array)->nelts = 0;


#define nxt_array_is_empty(array)                                             \
    ((array)->nelts == 0)


nxt_inline void *
nxt_array_remove_last(nxt_array_t *array)
{
    array->nelts--;
    return nxt_pointer_to(array->elts, array->size * array->nelts);
}


#endif /* _NXT_ARRAY_H_INCLUDED_ */