1 #ifndef ARRAY_H
2 #define ARRAY_H
3 #include "first.h"
4 
5 #include "buffer.h"
6 
7 struct data_unset; /* declaration */
8 
9 struct data_methods {
10 	struct data_unset *(*copy)(const struct data_unset *src); \
11 	void (*free)(struct data_unset *p); \
12 	void (*insert_dup)(struct data_unset *dst, struct data_unset *src);
13 };
14 
15 typedef enum { TYPE_STRING, TYPE_ARRAY, TYPE_INTEGER, TYPE_CONFIG, TYPE_OTHER } data_type_t;
16 #define DATA_UNSET \
17 	buffer key; \
18 	const struct data_methods *fn; /* function table */ \
19 	data_type_t type
20 
21 typedef struct data_unset {
22 	DATA_UNSET;
23 } data_unset;
24 
25 typedef struct {
26 	data_unset **data;
27 	data_unset **sorted;
28 
29 	uint32_t used; /* <= INT32_MAX */
30 	uint32_t size;
31 } array;
32 
33 typedef struct {
34 	DATA_UNSET;
35 	int ext; /*(fits in space due to alignment in 64-bit; extends 32-bit)*/
36 	buffer value;
37 } data_string;
38 
39 __attribute_returns_nonnull__
40 data_string *array_data_string_init(void);
41 
42 typedef struct {
43 	DATA_UNSET;
44 
45 	array value;
46 } data_array;
47 
48 __attribute_returns_nonnull__
49 data_array *array_data_array_init(void);
50 
51 typedef struct {
52 	DATA_UNSET;
53 
54 	int value;
55 } data_integer;
56 
57 __attribute_returns_nonnull__
58 data_integer *array_data_integer_init(void);
59 
60 __attribute_returns_nonnull__
61 array *array_init(uint32_t n);
62 
63 __attribute_cold__
64 void array_copy_array(array *dst, const array *src);
65 
66 __attribute_cold__
67 void array_free_data(array *a);
68 
69 void array_free(array *a);
70 
71 __attribute_hot__
72 void array_reset_data_strings(array *a);
73 
74 __attribute_cold__
75 void array_insert_unique(array *a, data_unset *entry);
76 
77 __attribute_cold__
78 data_unset *array_pop(array *a); /* only works on "simple" lists with autogenerated keys */
79 
80 __attribute_cold__
81 __attribute_pure__
82 int array_is_vlist(const array *a);
83 
84 __attribute_cold__
85 __attribute_pure__
86 int array_is_kvany(const array *a);
87 
88 __attribute_cold__
89 __attribute_pure__
90 int array_is_kvarray(const array *a);
91 
92 __attribute_cold__
93 __attribute_pure__
94 int array_is_kvstring(const array *a);
95 
96 __attribute_pure__
97 data_unset *array_get_element_klen_ext(const array *a, int ext, const char *key, uint32_t klen);
98 
99 __attribute_pure__
100 const data_unset *array_get_element_klen(const array *a, const char *key, uint32_t klen);
101 
102 __attribute_cold__
103 __attribute_pure__
104 data_unset *array_get_data_unset(const array *a, const char *key, uint32_t klen);
105 
106 __attribute_cold__
107 data_unset *array_extract_element_klen(array *a, const char *key, uint32_t klen); /* removes found entry from array */
108 
109 __attribute_returns_nonnull__
110 int * array_get_int_ptr(array *a, const char *k, uint32_t klen);
111 
112 __attribute_returns_nonnull__
113 buffer * array_get_buf_ptr_ext(array *a, int ext, const char *k, uint32_t klen);
114 
115 __attribute_returns_nonnull__
116 buffer * array_get_buf_ptr(array *a, const char *k, uint32_t klen);
117 
118 void array_insert_value(array *a, const char *v, uint32_t vlen);
119 
120 static inline void array_set_key_value(array * const a, const char * const k, const uint32_t klen, const char * const v, const uint32_t vlen);
121 
array_set_key_value(array * const a,const char * const k,const uint32_t klen,const char * const v,const uint32_t vlen)122 static inline void array_set_key_value(array * const a, const char * const k, const uint32_t klen, const char * const v, const uint32_t vlen) {
123     buffer_copy_string_len(array_get_buf_ptr(a, k, klen), v, vlen);
124 }
125 
126 __attribute_cold__
127 void array_replace(array *a, data_unset *entry);
128 
129 __attribute_pure__
130 data_unset * array_match_key_prefix_klen (const array * const a, const char * const s, const uint32_t slen);
131 
132 __attribute_pure__
133 data_unset * array_match_key_prefix_nc_klen (const array * const a, const char * const s, const uint32_t slen);
134 
135 __attribute_pure__
136 data_unset * array_match_key_prefix (const array * const a, const buffer * const b);
137 
138 __attribute_pure__
139 data_unset * array_match_key_prefix_nc (const array * const a, const buffer * const b);
140 
141 __attribute_pure__
142 const buffer * array_match_value_prefix (const array * const a, const buffer * const b);
143 
144 __attribute_pure__
145 const buffer * array_match_value_prefix_nc (const array * const a, const buffer * const b);
146 
147 __attribute_pure__
148 data_unset * array_match_key_suffix (const array * const a, const buffer * const b);
149 
150 __attribute_pure__
151 data_unset * array_match_key_suffix_nc (const array * const a, const buffer * const b);
152 
153 __attribute_pure__
154 const buffer * array_match_value_suffix (const array * const a, const buffer * const b);
155 
156 __attribute_pure__
157 const buffer * array_match_value_suffix_nc (const array * const a, const buffer * const b);
158 
159 __attribute_pure__
160 data_unset * array_match_path_or_ext (const array * const a, const buffer * const b);
161 
162 #endif
163