NCDVal.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /**
  2. * @file NCDVal.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #ifndef BADVPN_NCDVAL_H
  30. #define BADVPN_NCDVAL_H
  31. #include <stddef.h>
  32. #include <stdint.h>
  33. #include <misc/debug.h>
  34. #include <structure/CAvl.h>
  35. // these are implementation details. The interface is defined below.
  36. #define NCDVAL_FASTBUF_SIZE 64
  37. #define NCDVAL_FIRST_SIZE 256
  38. #define NCDVAL_MAXIDX INT_MAX
  39. typedef int NCDVal__idx;
  40. typedef struct {
  41. char *buf;
  42. NCDVal__idx size;
  43. NCDVal__idx used;
  44. char fastbuf[NCDVAL_FASTBUF_SIZE];
  45. } NCDValMem;
  46. typedef struct {
  47. NCDValMem *mem;
  48. NCDVal__idx idx;
  49. } NCDValRef;
  50. typedef struct {
  51. NCDVal__idx idx;
  52. } NCDValSafeRef;
  53. struct NCDVal__string {
  54. int type;
  55. NCDVal__idx length;
  56. char data[];
  57. };
  58. struct NCDVal__list {
  59. int type;
  60. NCDVal__idx maxcount;
  61. NCDVal__idx count;
  62. NCDVal__idx elem_indices[];
  63. };
  64. struct NCDVal__mapelem {
  65. NCDVal__idx key_idx;
  66. NCDVal__idx val_idx;
  67. NCDVal__idx tree_child[2];
  68. NCDVal__idx tree_parent;
  69. int8_t tree_balance;
  70. };
  71. typedef struct NCDVal__mapelem NCDVal__maptree_entry;
  72. typedef NCDValMem *NCDVal__maptree_arg;
  73. #include "NCDVal_maptree.h"
  74. #include <structure/CAvl_decl.h>
  75. struct NCDVal__map {
  76. int type;
  77. NCDVal__idx maxcount;
  78. NCDVal__idx count;
  79. NCDVal__MapTree tree;
  80. struct NCDVal__mapelem elems[];
  81. };
  82. typedef struct {
  83. NCDVal__idx elemidx;
  84. } NCDValMapElem;
  85. //
  86. #define NCDVAL_STRING 1
  87. #define NCDVAL_LIST 2
  88. #define NCDVAL_MAP 3
  89. /**
  90. * Initializes a value memory object.
  91. * A value memory object holds memory for value structures. Values within
  92. * the memory are referenced using {@link NCDValRef} objects, which point
  93. * to values within memory objects.
  94. *
  95. * Values may be added to a memory object using functions such as
  96. * {@link NCDVal_NewString}, {@link NCDVal_NewList} and {@link NCDVal_NewMap},
  97. * and {@link NCDVal_NewCopy}, which return references to the new values within
  98. * the memory object.
  99. *
  100. * It is not possible to remove values from the memory object, or modify existing
  101. * values other than adding elements to pre-allocated slots in lists and maps.
  102. * Once a value is added, it will consume memory as long as its memory object
  103. * exists. This is by design - this code is intended and optimized for constructing
  104. * and passing around values, not for operating on them in place. In fact, al
  105. * values within a memory object are stored in a single memory buffer, as an
  106. * embedded data structure with relativepointers. For example, map values use an
  107. * embedded AVL tree.
  108. */
  109. void NCDValMem_Init (NCDValMem *o);
  110. /**
  111. * Frees a value memory object.
  112. * All values within the memory object cease to exist, and any {@link NCDValRef}
  113. * object pointing to them must no longer be used.
  114. */
  115. void NCDValMem_Free (NCDValMem *o);
  116. /**
  117. * Does nothing.
  118. * The value reference object must either point to a valid value within a valid
  119. * memory object, or must be an invalid reference (all functions operating on
  120. * {@link NCDValRef} implicitly require that).
  121. */
  122. void NCDVal_Assert (NCDValRef val);
  123. /**
  124. * Determines if a value reference is invalid.
  125. */
  126. int NCDVal_IsInvalid (NCDValRef val);
  127. /**
  128. * Returns the type of the value reference, which must not be an invalid reference.
  129. * Possible values are NCDVAL_STRING, NCDVAL_LIST and NCDVAL_MAP.
  130. */
  131. int NCDVal_Type (NCDValRef val);
  132. /**
  133. * Returns an invalid reference.
  134. */
  135. NCDValRef NCDVal_NewInvalid (void);
  136. /**
  137. * Copies a value into the specified memory object. The source
  138. * must not be an invalid reference, however it may reside in any memory
  139. * object (including 'mem').
  140. * Returns a reference to the copied value. On out of memory, returns
  141. * an invalid reference.
  142. */
  143. NCDValRef NCDVal_NewCopy (NCDValMem *mem, NCDValRef val);
  144. /**
  145. * Compares two values, both of which must not be invalid references.
  146. * Returns -1, 0 or 1.
  147. */
  148. int NCDVal_Compare (NCDValRef val1, NCDValRef val2);
  149. /**
  150. * Converts a value reference to a safe referece format, which remains valid
  151. * if the memory object is moved (safe references do not contain a pointer
  152. * to the memory object, unlike {@link NCDValRef} references).
  153. */
  154. NCDValSafeRef NCDVal_ToSafe (NCDValRef val);
  155. /**
  156. * Converts a safe value reference to a normal value reference.
  157. * This should be used to recover references from safe references
  158. * after the memory object is moved.
  159. */
  160. NCDValRef NCDVal_FromSafe (NCDValMem *mem, NCDValSafeRef sval);
  161. /**
  162. * Fixes a value reference after its memory object was moved.
  163. */
  164. NCDValRef NCDVal_Moved (NCDValMem *mem, NCDValRef val);
  165. /**
  166. * Determines if a value is a string value.
  167. * The value reference must not be an invalid reference.
  168. */
  169. int NCDVal_IsString (NCDValRef val);
  170. /**
  171. * Determines if a value is a string value which has no null bytes.
  172. * The value reference must not be an invalid reference.
  173. */
  174. int NCDVal_IsStringNoNulls (NCDValRef val);
  175. /**
  176. * Builds a new string value from a null-terminated array of bytes.
  177. * Equivalent to NCDVal_NewStringBin(mem, data, strlen(data)).
  178. * Returns a reference to the new value, or an invalid reference
  179. * on out of memory.
  180. * WARNING: The buffer passed must NOT be part of any value in the
  181. * memory object specified. In particular, you may NOT use this
  182. * function to copy a string that resides in the same memory object.
  183. */
  184. NCDValRef NCDVal_NewString (NCDValMem *mem, const char *data);
  185. /**
  186. * Builds a new string value.
  187. * Returns a reference to the new value, or an invalid reference
  188. * on out of memory.
  189. * WARNING: The buffer passed must NOT be part of any value in the
  190. * memory object specified. In particular, you may NOT use this
  191. * function to copy a string that resides in the same memory object.
  192. */
  193. NCDValRef NCDVal_NewStringBin (NCDValMem *mem, const uint8_t *data, size_t len);
  194. /**
  195. * Builds a new string value of the given length with undefined contents.
  196. * You can define the contents of the string later by copying to the address
  197. * returned by {@link NCDVal_StringValue}. The terminating null byte is
  198. * however automatically written.
  199. */
  200. NCDValRef NCDVal_NewStringUninitialized (NCDValMem *mem, size_t len);
  201. /**
  202. * Returns a pointer to the data of a string value. An extra null byte
  203. * is always appended to the actual contents of the string.
  204. * The value reference must point to a string value.
  205. */
  206. const char * NCDVal_StringValue (NCDValRef string);
  207. /**
  208. * Returns the length of the string value, excluding the automatically
  209. * appended null byte.
  210. * The value reference must point to a string value.
  211. */
  212. size_t NCDVal_StringLength (NCDValRef string);
  213. /**
  214. * Determines if the string value has any null bytes in its contents,
  215. * i.e. that length > strlen().
  216. * The value reference must point to a string value.
  217. */
  218. int NCDVal_StringHasNulls (NCDValRef string);
  219. /**
  220. * Determines if the string value is equal to the given null-terminated
  221. * string.
  222. * The value reference must point to a string value.
  223. */
  224. int NCDVal_StringEquals (NCDValRef string, const char *data);
  225. /**
  226. * Determines if a value is a list value.
  227. * The value reference must not be an invalid reference.
  228. */
  229. int NCDVal_IsList (NCDValRef val);
  230. /**
  231. * Builds a new list value. The 'maxcount' argument specifies how
  232. * many element slots to preallocate. Not more than that many
  233. * elements may be appended to the list using {@link NCDVal_ListAppend}.
  234. * Returns a reference to the new value, or an invalid reference
  235. * on out of memory.
  236. */
  237. NCDValRef NCDVal_NewList (NCDValMem *mem, size_t maxcount);
  238. /**
  239. * Appends a value to to the list value.
  240. * The 'list' reference must point to a list value, and the
  241. * 'elem' reference must be non-invalid and point to a value within
  242. * the same memory object as the list.
  243. * Inserting a value into a list does not in any way change it;
  244. * internally, the list only points to it.
  245. */
  246. void NCDVal_ListAppend (NCDValRef list, NCDValRef elem);
  247. /**
  248. * Returns the number of elements in a list value, i.e. the number
  249. * of times {@link NCDVal_ListAppend} was called.
  250. * The 'list' reference must point to a list value.
  251. */
  252. size_t NCDVal_ListCount (NCDValRef list);
  253. /**
  254. * Returns the maximum number of elements a list value may contain,
  255. * i.e. the 'maxcount' argument to {@link NCDVal_NewList}.
  256. * The 'list' reference must point to a list value.
  257. */
  258. size_t NCDVal_ListMaxCount (NCDValRef list);
  259. /**
  260. * Returns a reference to the value at the given position 'pos' in a list,
  261. * starting with zero.
  262. * The 'list' reference must point to a list value.
  263. * The position 'pos' must refer to an existing element, i.e.
  264. * pos < NCDVal_ListCount().
  265. */
  266. NCDValRef NCDVal_ListGet (NCDValRef list, size_t pos);
  267. /**
  268. * Returns references to elements within a list by writing them
  269. * via (NCDValRef *) variable arguments.
  270. * If 'num' == NCDVal_ListCount(), succeeds, returing 1 and writing 'num'
  271. * references, as mentioned.
  272. * If 'num' != NCDVal_ListCount(), fails, returning 0, without writing any
  273. * references
  274. */
  275. int NCDVal_ListRead (NCDValRef list, int num, ...);
  276. /**
  277. * Like {@link NCDVal_ListRead}, but the list can contain more than 'num'
  278. * elements.
  279. */
  280. int NCDVal_ListReadHead (NCDValRef list, int num, ...);
  281. /**
  282. * Determines if a value is a map value.
  283. * The value reference must not be an invalid reference.
  284. */
  285. int NCDVal_IsMap (NCDValRef val);
  286. /**
  287. * Builds a new map value. The 'maxcount' argument specifies how
  288. * many entry slots to preallocate. Not more than that many
  289. * entries may be inserted to the map using {@link NCDVal_MapInsert}.
  290. * Returns a reference to the new value, or an invalid reference
  291. * on out of memory.
  292. */
  293. NCDValRef NCDVal_NewMap (NCDValMem *mem, size_t maxcount);
  294. /**
  295. * Inserts an entry to the map value.
  296. * The 'map' reference must point to a map value, and the
  297. * 'key' and 'val' references must be non-invalid and point to values within
  298. * the same memory object as the map.
  299. * Inserting an entry does not in any way change the 'key'and 'val';
  300. * internally, the map only points to it.
  301. * You must not modify the key after inserting it into a map. This is because
  302. * the map builds an embedded AVL tree of entries indexed by keys.
  303. * If 'key' does not exist in the map, succeeds, returning 1.
  304. * If 'key' already exists in the map, fails, returning 0.
  305. */
  306. int NCDVal_MapInsert (NCDValRef map, NCDValRef key, NCDValRef val);
  307. /**
  308. * Returns the number of entries in a map value, i.e. the number
  309. * of times {@link NCDVal_MapInsert} was called successfully.
  310. * The 'map' reference must point to a map value.
  311. */
  312. size_t NCDVal_MapCount (NCDValRef map);
  313. /**
  314. * Returns the maximum number of entries a map value may contain,
  315. * i.e. the 'maxcount' argument to {@link NCDVal_NewMap}.
  316. * The 'map' reference must point to a map value.
  317. */
  318. size_t NCDVal_MapMaxCount (NCDValRef map);
  319. /**
  320. * Determines if a map entry reference is invalid. This is used in combination
  321. * with the map iteration functions to detect the end of iteration.
  322. */
  323. int NCDVal_MapElemInvalid (NCDValMapElem me);
  324. /**
  325. * Returns a reference to the first entry in a map, with respect to some
  326. * arbitrary order.
  327. * If the map is empty, returns an invalid map entry reference.
  328. */
  329. NCDValMapElem NCDVal_MapFirst (NCDValRef map);
  330. /**
  331. * Returns a reference to the entry in a map that follows the entry referenced
  332. * by 'me', with respect to some arbitrary order.
  333. * The 'me' argument must be a non-invalid reference to an entry in the map.
  334. * If 'me' is the last entry, returns an invalid map entry reference.
  335. */
  336. NCDValMapElem NCDVal_MapNext (NCDValRef map, NCDValMapElem me);
  337. /**
  338. * Like {@link NCDVal_MapFirst}, but with respect to the order defined by
  339. * {@link NCDVal_Compare}.
  340. * Ordered iteration is slower and should only be used when needed.
  341. */
  342. NCDValMapElem NCDVal_MapOrderedFirst (NCDValRef map);
  343. /**
  344. * Like {@link NCDVal_MapNext}, but with respect to the order defined by
  345. * {@link NCDVal_Compare}.
  346. * Ordered iteration is slower and should only be used when needed.
  347. */
  348. NCDValMapElem NCDVal_MapOrderedNext (NCDValRef map, NCDValMapElem me);
  349. /**
  350. * Returns a reference to the key of the map entry referenced by 'me'.
  351. * The 'me' argument must be a non-invalid reference to an entry in the map.
  352. */
  353. NCDValRef NCDVal_MapElemKey (NCDValRef map, NCDValMapElem me);
  354. /**
  355. * Returns a reference to the value of the map entry referenced by 'me'.
  356. * The 'me' argument must be a non-invalid reference to an entry in the map.
  357. */
  358. NCDValRef NCDVal_MapElemVal (NCDValRef map, NCDValMapElem me);
  359. /**
  360. * Looks for a key in the map. The 'key' reference must be a non-invalid
  361. * value reference, and may point to a value in a different memory object
  362. * than the map.
  363. * If the key exists in the map, returns a reference to the corresponding
  364. * map entry.
  365. * If the key does not exist, returns an invalid map entry reference.
  366. */
  367. NCDValMapElem NCDVal_MapFindKey (NCDValRef map, NCDValRef key);
  368. #endif