xref: /unit/src/nxt_vector.h (revision 0:a63ceefd6ab0)
1 
2 /*
3  * Copyright (C) Igor Sysoev
4  * Copyright (C) NGINX, Inc.
5  */
6 
7 #ifndef _NXT_VECTOR_H_INCLUDED_
8 #define _NXT_VECTOR_H_INCLUDED_
9 
10 
11 typedef enum {
12     NXT_VECTOR_INITED = 0,
13     NXT_VECTOR_DESCRETE,
14     NXT_VECTOR_EMBEDDED,
15 } nxt_vector_type_t;
16 
17 
18 typedef struct {
19     void               *start;
20     /*
21      * A vector can hold no more than 65536 items.
22      * The item size is no more than 64K.
23      */
24     uint16_t           items;
25     uint16_t           avalaible;
26     uint16_t           item_size;
27     nxt_vector_type_t  type:8;
28 } nxt_vector_t;
29 
30 
31 NXT_EXPORT nxt_vector_t *nxt_vector_create(nxt_uint_t items, size_t item_size,
32     const nxt_mem_proto_t *proto, void *pool);
33 NXT_EXPORT void *nxt_vector_init(nxt_vector_t *vector, nxt_uint_t items,
34     size_t item_size, const nxt_mem_proto_t *proto, void *pool);
35 NXT_EXPORT void nxt_vector_destroy(nxt_vector_t *vector,
36     const nxt_mem_proto_t *proto, void *pool);
37 NXT_EXPORT void *nxt_vector_add(nxt_vector_t *vector,
38     const nxt_mem_proto_t *proto, void *pool);
39 NXT_EXPORT void *nxt_vector_zero_add(nxt_vector_t *vector,
40     const nxt_mem_proto_t *proto, void *pool);
41 NXT_EXPORT void nxt_vector_remove(nxt_vector_t *vector, void *item);
42 
43 
44 #define                                                                       \
45 nxt_vector_last(vector)                                                       \
46     ((void *)                                                                 \
47         ((char *) (vector)->start                                             \
48                       + (vector)->item_size * ((vector)->items - 1)))
49 
50 
51 #define                                                                       \
52 nxt_vector_reset(vector)                                                      \
53     (vector)->items = 0;
54 
55 
56 #define                                                                       \
57 nxt_vector_is_empty(vector)                                                   \
58     ((vector)->items == 0)
59 
60 
61 nxt_inline void *
62 nxt_vector_remove_last(nxt_vector_t *vector)
63 {
64     vector->items--;
65     return (char *) vector->start + vector->item_size * vector->items;
66 }
67 
68 
69 #endif /* _NXT_VECTOR_H_INCLUDED_ */
70