NCDVal.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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. #define NCDVAL_MINIDX INT_MIN
  40. typedef int NCDVal__idx;
  41. typedef struct {
  42. char *buf;
  43. NCDVal__idx size;
  44. NCDVal__idx used;
  45. char fastbuf[NCDVAL_FASTBUF_SIZE];
  46. } NCDValMem;
  47. typedef struct {
  48. NCDValMem *mem;
  49. NCDVal__idx idx;
  50. } NCDValRef;
  51. typedef struct {
  52. NCDVal__idx idx;
  53. } NCDValSafeRef;
  54. struct NCDVal__string {
  55. int type;
  56. NCDVal__idx length;
  57. char data[];
  58. };
  59. struct NCDVal__list {
  60. int type;
  61. NCDVal__idx maxcount;
  62. NCDVal__idx count;
  63. NCDVal__idx elem_indices[];
  64. };
  65. struct NCDVal__mapelem {
  66. NCDVal__idx key_idx;
  67. NCDVal__idx val_idx;
  68. NCDVal__idx tree_child[2];
  69. NCDVal__idx tree_parent;
  70. int8_t tree_balance;
  71. };
  72. typedef struct NCDVal__mapelem NCDVal__maptree_entry;
  73. typedef NCDValMem *NCDVal__maptree_arg;
  74. #include "NCDVal_maptree.h"
  75. #include <structure/CAvl_decl.h>
  76. struct NCDVal__map {
  77. int type;
  78. NCDVal__idx maxcount;
  79. NCDVal__idx count;
  80. NCDVal__MapTree tree;
  81. struct NCDVal__mapelem elems[];
  82. };
  83. typedef struct {
  84. NCDVal__idx elemidx;
  85. } NCDValMapElem;
  86. #define NCDVAL_INSTR_PLACEHOLDER 0
  87. #define NCDVAL_INSTR_REINSERT 1
  88. struct NCDVal__instr {
  89. int type;
  90. union {
  91. struct {
  92. NCDVal__idx plid;
  93. NCDVal__idx plidx;
  94. } placeholder;
  95. struct {
  96. NCDVal__idx mapidx;
  97. NCDVal__idx elempos;
  98. } reinsert;
  99. };
  100. };
  101. typedef struct {
  102. struct NCDVal__instr *instrs;
  103. size_t num_instrs;
  104. } NCDValReplaceProg;
  105. //
  106. #define NCDVAL_STRING 1
  107. #define NCDVAL_LIST 2
  108. #define NCDVAL_MAP 3
  109. #define NCDVAL_PLACEHOLDER 4
  110. /**
  111. * Initializes a value memory object.
  112. * A value memory object holds memory for value structures. Values within
  113. * the memory are referenced using {@link NCDValRef} objects, which point
  114. * to values within memory objects.
  115. *
  116. * Values may be added to a memory object using functions such as
  117. * {@link NCDVal_NewString}, {@link NCDVal_NewList} and {@link NCDVal_NewMap},
  118. * and {@link NCDVal_NewCopy}, which return references to the new values within
  119. * the memory object.
  120. *
  121. * It is not possible to remove values from the memory object, or modify existing
  122. * values other than adding elements to pre-allocated slots in lists and maps.
  123. * Once a value is added, it will consume memory as long as its memory object
  124. * exists. This is by design - this code is intended and optimized for constructing
  125. * and passing around values, not for operating on them in place. In fact, al
  126. * values within a memory object are stored in a single memory buffer, as an
  127. * embedded data structure with relativepointers. For example, map values use an
  128. * embedded AVL tree.
  129. */
  130. void NCDValMem_Init (NCDValMem *o);
  131. /**
  132. * Frees a value memory object.
  133. * All values within the memory object cease to exist, and any {@link NCDValRef}
  134. * object pointing to them must no longer be used.
  135. */
  136. void NCDValMem_Free (NCDValMem *o);
  137. /**
  138. * Attempts to free the value memory object, exporting its data to an external
  139. * memory block.
  140. * On success, 1 is returned, and *out_data and *out_len are set; *out_data
  141. * receives a memory block which should be freed using {@link BFree}.
  142. * After the memory object is exported, copies can be created using
  143. * {@link NCDValMem_InitImport}. Any value references needed from the original
  144. * should be turned to safe references using {@link NCDVal_ToSafe} before
  145. * exporting the block, and imported to copies using {@link NCDVal_FromSafe}.
  146. * On failure, 0 is returned, and the memory object is unchanged (it should
  147. * still be freed using {@link NCDValMem_Free}.
  148. */
  149. int NCDValMem_FreeExport (NCDValMem *o, char **out_data, size_t *out_len) WARN_UNUSED;
  150. /**
  151. * Initializes the value memory object as a copy of an external memory block,
  152. * which was obtained using {@link NCDValMem_FreeExport}.
  153. * The memory block provided is only read, and is copied into this memory object.
  154. * Returns 1 on success, 0 on failure.
  155. */
  156. int NCDValMem_InitImport (NCDValMem *o, const char *data, size_t len) WARN_UNUSED;
  157. /**
  158. * Does nothing.
  159. * The value reference object must either point to a valid value within a valid
  160. * memory object, or must be an invalid reference (most functions operating on
  161. * {@link NCDValRef} implicitly require that).
  162. */
  163. void NCDVal_Assert (NCDValRef val);
  164. /**
  165. * Determines if a value reference is invalid.
  166. */
  167. int NCDVal_IsInvalid (NCDValRef val);
  168. /**
  169. * Determines if a value is a placeholder value.
  170. * The value reference must not be an invalid reference.
  171. */
  172. int NCDVal_IsPlaceholder (NCDValRef val);
  173. /**
  174. * Returns the type of the value reference, which must not be an invalid reference.
  175. * Possible values are NCDVAL_STRING, NCDVAL_LIST, NCDVAL_MAP and NCDVAL_PLACEHOLDER.
  176. * The placeholder type is only used internally in the interpreter for argument
  177. * resolution, and is never seen by modules; see {@link NCDVal_NewPlaceholder}.
  178. */
  179. int NCDVal_Type (NCDValRef val);
  180. /**
  181. * Returns an invalid reference.
  182. * An invalid reference must not be passed to any function here, except:
  183. * {@link NCDVal_Assert}, {@link NCDVal_IsInvalid}, {@link NCDVal_ToSafe},
  184. * {@link NCDVal_FromSafe}, {@link NCDVal_Moved}.
  185. */
  186. NCDValRef NCDVal_NewInvalid (void);
  187. /**
  188. * Returns a new placeholder value reference. A placeholder value is a valid value
  189. * containing an integer placeholder identifier.
  190. * This always succeeds; however, the caller must ensure the identifier is
  191. * non-negative and satisfies (NCDVAL_MINIDX + plid < -1).
  192. *
  193. * The placeholder type is only used internally in the interpreter for argument
  194. * resolution, and is never seen by modules. Also see {@link NCDPlaceholderDb}.
  195. */
  196. NCDValRef NCDVal_NewPlaceholder (NCDValMem *mem, int plid);
  197. /**
  198. * Returns the indentifier of a placeholder value.
  199. * The value reference must point to a placeholder value.
  200. */
  201. int NCDVal_PlaceholderId (NCDValRef val);
  202. /**
  203. * Copies a value into the specified memory object. The source
  204. * must not be an invalid reference, however it may reside in any memory
  205. * object (including 'mem').
  206. * Returns a reference to the copied value. On out of memory, returns
  207. * an invalid reference.
  208. */
  209. NCDValRef NCDVal_NewCopy (NCDValMem *mem, NCDValRef val);
  210. /**
  211. * Compares two values, both of which must not be invalid references.
  212. * Returns -1, 0 or 1.
  213. */
  214. int NCDVal_Compare (NCDValRef val1, NCDValRef val2);
  215. /**
  216. * Converts a value reference to a safe referece format, which remains valid
  217. * if the memory object is moved (safe references do not contain a pointer
  218. * to the memory object, unlike {@link NCDValRef} references).
  219. */
  220. NCDValSafeRef NCDVal_ToSafe (NCDValRef val);
  221. /**
  222. * Converts a safe value reference to a normal value reference.
  223. * This should be used to recover references from safe references
  224. * after the memory object is moved.
  225. */
  226. NCDValRef NCDVal_FromSafe (NCDValMem *mem, NCDValSafeRef sval);
  227. /**
  228. * Fixes a value reference after its memory object was moved.
  229. */
  230. NCDValRef NCDVal_Moved (NCDValMem *mem, NCDValRef val);
  231. /**
  232. * Determines if a value is a string value.
  233. * The value reference must not be an invalid reference.
  234. */
  235. int NCDVal_IsString (NCDValRef val);
  236. /**
  237. * Determines if a value is a string value which has no null bytes.
  238. * The value reference must not be an invalid reference.
  239. */
  240. int NCDVal_IsStringNoNulls (NCDValRef val);
  241. /**
  242. * Builds a new string value from a null-terminated array of bytes.
  243. * Equivalent to NCDVal_NewStringBin(mem, data, strlen(data)).
  244. * Returns a reference to the new value, or an invalid reference
  245. * on out of memory.
  246. * WARNING: The buffer passed must NOT be part of any value in the
  247. * memory object specified. In particular, you may NOT use this
  248. * function to copy a string that resides in the same memory object.
  249. */
  250. NCDValRef NCDVal_NewString (NCDValMem *mem, const char *data);
  251. /**
  252. * Builds a new string value.
  253. * Returns a reference to the new value, or an invalid reference
  254. * on out of memory.
  255. * WARNING: The buffer passed must NOT be part of any value in the
  256. * memory object specified. In particular, you may NOT use this
  257. * function to copy a string that resides in the same memory object.
  258. */
  259. NCDValRef NCDVal_NewStringBin (NCDValMem *mem, const uint8_t *data, size_t len);
  260. /**
  261. * Builds a new string value of the given length with undefined contents.
  262. * You can define the contents of the string later by copying to the address
  263. * returned by {@link NCDVal_StringValue}. The terminating null byte is
  264. * however automatically written.
  265. */
  266. NCDValRef NCDVal_NewStringUninitialized (NCDValMem *mem, size_t len);
  267. /**
  268. * Returns a pointer to the data of a string value. An extra null byte
  269. * is always appended to the actual contents of the string.
  270. * The value reference must point to a string value.
  271. */
  272. const char * NCDVal_StringValue (NCDValRef string);
  273. /**
  274. * Returns the length of the string value, excluding the automatically
  275. * appended null byte.
  276. * The value reference must point to a string value.
  277. */
  278. size_t NCDVal_StringLength (NCDValRef string);
  279. /**
  280. * Determines if the string value has any null bytes in its contents,
  281. * i.e. that length > strlen().
  282. * The value reference must point to a string value.
  283. */
  284. int NCDVal_StringHasNulls (NCDValRef string);
  285. /**
  286. * Determines if the string value is equal to the given null-terminated
  287. * string.
  288. * The value reference must point to a string value.
  289. */
  290. int NCDVal_StringEquals (NCDValRef string, const char *data);
  291. /**
  292. * Determines if a value is a list value.
  293. * The value reference must not be an invalid reference.
  294. */
  295. int NCDVal_IsList (NCDValRef val);
  296. /**
  297. * Builds a new list value. The 'maxcount' argument specifies how
  298. * many element slots to preallocate. Not more than that many
  299. * elements may be appended to the list using {@link NCDVal_ListAppend}.
  300. * Returns a reference to the new value, or an invalid reference
  301. * on out of memory.
  302. */
  303. NCDValRef NCDVal_NewList (NCDValMem *mem, size_t maxcount);
  304. /**
  305. * Appends a value to to the list value.
  306. * The 'list' reference must point to a list value, and the
  307. * 'elem' reference must be non-invalid and point to a value within
  308. * the same memory object as the list.
  309. * Inserting a value into a list does not in any way change it;
  310. * internally, the list only points to it.
  311. */
  312. void NCDVal_ListAppend (NCDValRef list, NCDValRef elem);
  313. /**
  314. * Returns the number of elements in a list value, i.e. the number
  315. * of times {@link NCDVal_ListAppend} was called.
  316. * The 'list' reference must point to a list value.
  317. */
  318. size_t NCDVal_ListCount (NCDValRef list);
  319. /**
  320. * Returns the maximum number of elements a list value may contain,
  321. * i.e. the 'maxcount' argument to {@link NCDVal_NewList}.
  322. * The 'list' reference must point to a list value.
  323. */
  324. size_t NCDVal_ListMaxCount (NCDValRef list);
  325. /**
  326. * Returns a reference to the value at the given position 'pos' in a list,
  327. * starting with zero.
  328. * The 'list' reference must point to a list value.
  329. * The position 'pos' must refer to an existing element, i.e.
  330. * pos < NCDVal_ListCount().
  331. */
  332. NCDValRef NCDVal_ListGet (NCDValRef list, size_t pos);
  333. /**
  334. * Returns references to elements within a list by writing them
  335. * via (NCDValRef *) variable arguments.
  336. * If 'num' == NCDVal_ListCount(), succeeds, returing 1 and writing 'num'
  337. * references, as mentioned.
  338. * If 'num' != NCDVal_ListCount(), fails, returning 0, without writing any
  339. * references
  340. */
  341. int NCDVal_ListRead (NCDValRef list, int num, ...);
  342. /**
  343. * Like {@link NCDVal_ListRead}, but the list can contain more than 'num'
  344. * elements.
  345. */
  346. int NCDVal_ListReadHead (NCDValRef list, int num, ...);
  347. /**
  348. * Determines if a value is a map value.
  349. * The value reference must not be an invalid reference.
  350. */
  351. int NCDVal_IsMap (NCDValRef val);
  352. /**
  353. * Builds a new map value. The 'maxcount' argument specifies how
  354. * many entry slots to preallocate. Not more than that many
  355. * entries may be inserted to the map using {@link NCDVal_MapInsert}.
  356. * Returns a reference to the new value, or an invalid reference
  357. * on out of memory.
  358. */
  359. NCDValRef NCDVal_NewMap (NCDValMem *mem, size_t maxcount);
  360. /**
  361. * Inserts an entry to the map value.
  362. * The 'map' reference must point to a map value, and the
  363. * 'key' and 'val' references must be non-invalid and point to values within
  364. * the same memory object as the map.
  365. * Inserting an entry does not in any way change the 'key'and 'val';
  366. * internally, the map only points to it.
  367. * You must not modify the key after inserting it into a map. This is because
  368. * the map builds an embedded AVL tree of entries indexed by keys.
  369. * If 'key' does not exist in the map, succeeds, returning 1.
  370. * If 'key' already exists in the map, fails, returning 0.
  371. */
  372. int NCDVal_MapInsert (NCDValRef map, NCDValRef key, NCDValRef val);
  373. /**
  374. * Returns the number of entries in a map value, i.e. the number
  375. * of times {@link NCDVal_MapInsert} was called successfully.
  376. * The 'map' reference must point to a map value.
  377. */
  378. size_t NCDVal_MapCount (NCDValRef map);
  379. /**
  380. * Returns the maximum number of entries a map value may contain,
  381. * i.e. the 'maxcount' argument to {@link NCDVal_NewMap}.
  382. * The 'map' reference must point to a map value.
  383. */
  384. size_t NCDVal_MapMaxCount (NCDValRef map);
  385. /**
  386. * Determines if a map entry reference is invalid. This is used in combination
  387. * with the map iteration functions to detect the end of iteration.
  388. */
  389. int NCDVal_MapElemInvalid (NCDValMapElem me);
  390. /**
  391. * Returns a reference to the first entry in a map, with respect to some
  392. * arbitrary order.
  393. * If the map is empty, returns an invalid map entry reference.
  394. */
  395. NCDValMapElem NCDVal_MapFirst (NCDValRef map);
  396. /**
  397. * Returns a reference to the entry in a map that follows the entry referenced
  398. * by 'me', with respect to some arbitrary order.
  399. * The 'me' argument must be a non-invalid reference to an entry in the map.
  400. * If 'me' is the last entry, returns an invalid map entry reference.
  401. */
  402. NCDValMapElem NCDVal_MapNext (NCDValRef map, NCDValMapElem me);
  403. /**
  404. * Like {@link NCDVal_MapFirst}, but with respect to the order defined by
  405. * {@link NCDVal_Compare}.
  406. * Ordered iteration is slower and should only be used when needed.
  407. */
  408. NCDValMapElem NCDVal_MapOrderedFirst (NCDValRef map);
  409. /**
  410. * Like {@link NCDVal_MapNext}, but with respect to the order defined by
  411. * {@link NCDVal_Compare}.
  412. * Ordered iteration is slower and should only be used when needed.
  413. */
  414. NCDValMapElem NCDVal_MapOrderedNext (NCDValRef map, NCDValMapElem me);
  415. /**
  416. * Returns a reference to the key of the map entry referenced by 'me'.
  417. * The 'me' argument must be a non-invalid reference to an entry in the map.
  418. */
  419. NCDValRef NCDVal_MapElemKey (NCDValRef map, NCDValMapElem me);
  420. /**
  421. * Returns a reference to the value of the map entry referenced by 'me'.
  422. * The 'me' argument must be a non-invalid reference to an entry in the map.
  423. */
  424. NCDValRef NCDVal_MapElemVal (NCDValRef map, NCDValMapElem me);
  425. /**
  426. * Looks for a key in the map. The 'key' reference must be a non-invalid
  427. * value reference, and may point to a value in a different memory object
  428. * than the map.
  429. * If the key exists in the map, returns a reference to the corresponding
  430. * map entry.
  431. * If the key does not exist, returns an invalid map entry reference.
  432. */
  433. NCDValMapElem NCDVal_MapFindKey (NCDValRef map, NCDValRef key);
  434. /**
  435. * Builds a placeholder replacement program, which is a list of instructions for
  436. * efficiently replacing placeholders in identical values in identical memory
  437. * objects.
  438. * To actually perform replacements, make copies of the memory object of this value
  439. * using {@link NCDValMem_FreeExport} and {@link NCDValMem_InitImport}, then call
  440. * {@link NCDValReplaceProg_Execute} on the copies.
  441. * The value passed must be a valid value, and not a placeholder.
  442. * Returns 1 on success, 0 on failure.
  443. */
  444. int NCDValReplaceProg_Init (NCDValReplaceProg *o, NCDValRef val);
  445. /**
  446. * Frees the placeholder replacement program.
  447. */
  448. void NCDValReplaceProg_Free (NCDValReplaceProg *o);
  449. /**
  450. * Callback used by {@link NCDValReplaceProg_Execute} to allow the caller to produce
  451. * values of placeholders.
  452. * This function should build a new value within the memory object 'mem' (which is
  453. * the same as of the memory object where placeholders are being replaced).
  454. * On success, it should return 1, writing a valid value reference to *out.
  455. * On failure, it can either return 0, or return 1 but write an invalid value reference.
  456. * This callback must not access the memory object in any other way than building
  457. * new values in it; it must not modify any values that were already present at the
  458. * point it was called.
  459. */
  460. typedef int (*NCDVal_replace_func) (void *arg, int plid, NCDValMem *mem, NCDValRef *out);
  461. /**
  462. * Executes the replacement program, replacing placeholders in a value.
  463. * The memory object must given be identical to the memory object which was used in
  464. * {@link NCDValReplaceProg_Init}; see {@link NCDValMem_FreeExport} and
  465. * {@link NCDValMem_InitImport}.
  466. * This will call the callback 'replace', which should build the values to replace
  467. * the placeholders.
  468. * Returns 1 on success and 0 on failure. On failure, the entire memory object enters
  469. * and inconsistent state and must be freed using {@link NCDValMem_Free} before
  470. * performing any other operation on it.
  471. * The program is passed by value instead of pointer because this appears to be faster.
  472. * Is is not modified in any way.
  473. */
  474. int NCDValReplaceProg_Execute (NCDValReplaceProg prog, NCDValMem *mem, NCDVal_replace_func replace, void *arg);
  475. #endif