xref: /unit/src/nxt_list.h (revision 2084:7d479274f334)
1 
2 /*
3  * Copyright (C) Igor Sysoev
4  * Copyright (C) NGINX, Inc.
5  */
6 
7 #ifndef _NXT_LIST_H_INCLUDED_
8 #define _NXT_LIST_H_INCLUDED_
9 
10 
11 typedef struct nxt_list_part_s  nxt_list_part_t;
12 
13 struct nxt_list_part_s {
14     nxt_list_part_t             *next;
15     uintptr_t                   nelts;
16     char                        data[];
17 };
18 
19 
20 typedef struct {
21     nxt_list_part_t             *last;
22 #if (NXT_64BIT)
23     uint32_t                    size;
24     uint32_t                    nalloc;
25 #else
26     uint16_t                    size;
27     uint16_t                    nalloc;
28 #endif
29     nxt_mp_t                    *mem_pool;
30     nxt_list_part_t             part;
31 } nxt_list_t;
32 
33 
34 typedef struct {
35     nxt_list_part_t             *part;
36     uintptr_t                   elt;
37 } nxt_list_next_t;
38 
39 
40 #define nxt_list_part(list)                                                   \
41     (&(list)->part)
42 
43 
44 #define nxt_list_data(part)                                                   \
45     ((void *) part->data)
46 
47 
48 #define nxt_list_first(list)                                                  \
49     nxt_list_data(nxt_list_part(list))
50 
51 
52 nxt_inline void *
nxt_list_elt(nxt_list_t * list,nxt_uint_t n)53 nxt_list_elt(nxt_list_t *list, nxt_uint_t n)
54 {
55     nxt_list_part_t  *part;
56 
57     if (nxt_fast_path((list) != NULL)) {
58         part = nxt_list_part(list);
59 
60         while (part != NULL) {
61             if (n < (nxt_uint_t) part->nelts) {
62                 return nxt_pointer_to(nxt_list_data(part), n * (list)->size);
63             }
64 
65             n -= (nxt_uint_t) part->nelts;
66             part = part->next;
67         }
68     }
69 
70     return NULL;
71 }
72 
73 
74 #define nxt_list_each(elt, list)                                              \
75     do {                                                                      \
76         if (nxt_fast_path((list) != NULL)) {                                  \
77             void             *_end;                                           \
78             nxt_list_part_t  *_part = nxt_list_part(list);                    \
79                                                                               \
80             do {                                                              \
81                 elt = nxt_list_data(_part);                                   \
82                                                                               \
83                 for (_end = (elt + _part->nelts); elt != _end; elt++) {       \
84 
85 #define nxt_list_loop                                                         \
86                 }                                                             \
87                                                                               \
88                 _part = _part->next;                                          \
89                                                                               \
90             } while (_part != NULL);                                          \
91         }                                                                     \
92     } while (0)
93 
94 
95 NXT_EXPORT nxt_list_t *nxt_list_create(nxt_mp_t *mp, nxt_uint_t n, size_t size);
96 NXT_EXPORT void *nxt_list_add(nxt_list_t *list);
97 NXT_EXPORT void *nxt_list_zero_add(nxt_list_t *list);
98 
99 NXT_EXPORT void *nxt_list_next(nxt_list_t *list, nxt_list_next_t *next);
100 
101 
102 #define nxt_list_next_value(list, next)                                       \
103     (nxt_pointer_to(nxt_list_data((next)->part), (next)->elt * (list)->size))
104 
105 
106 nxt_inline nxt_uint_t
nxt_list_nelts(nxt_list_t * list)107 nxt_list_nelts(nxt_list_t *list)
108 {
109     nxt_uint_t       n;
110     nxt_list_part_t  *part;
111 
112     n = 0;
113 
114     if (nxt_fast_path((list) != NULL)) {
115         part = nxt_list_part(list);
116 
117         do {
118             n += (nxt_uint_t) part->nelts;
119             part = part->next;
120         } while (part != NULL);
121     }
122 
123     return n;
124 }
125 
126 
127 #endif /* _NXT_LIST_H_INCLUDED_ */
128