NCDVal.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  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. #include <ncd/NCDStringIndex.h>
  36. #include <ncd/NCDRefTarget.h>
  37. // these are implementation details. The interface is defined below.
  38. #define NCDVAL_FASTBUF_SIZE 64
  39. #define NCDVAL_FIRST_SIZE 256
  40. #define NCDVAL_MAX_DEPTH 32
  41. #define NCDVAL_MAXIDX INT_MAX
  42. #define NCDVAL_MINIDX INT_MIN
  43. typedef int NCDVal__idx;
  44. struct NCDVal__ref {
  45. NCDVal__idx next;
  46. NCDRefTarget *target;
  47. };
  48. struct NCDVal__string {
  49. int type;
  50. NCDVal__idx length;
  51. char data[];
  52. };
  53. struct NCDVal__list {
  54. int type;
  55. NCDVal__idx maxcount;
  56. NCDVal__idx count;
  57. NCDVal__idx elem_indices[];
  58. };
  59. struct NCDVal__mapelem {
  60. NCDVal__idx key_idx;
  61. NCDVal__idx val_idx;
  62. NCDVal__idx tree_child[2];
  63. NCDVal__idx tree_parent;
  64. int8_t tree_balance;
  65. };
  66. struct NCDVal__idstring {
  67. int type;
  68. NCD_string_id_t string_id;
  69. NCDStringIndex *string_index;
  70. };
  71. struct NCDVal__externalstring {
  72. int type;
  73. const char *data;
  74. size_t length;
  75. struct NCDVal__ref ref;
  76. };
  77. typedef struct {
  78. char *buf;
  79. NCDVal__idx size;
  80. NCDVal__idx used;
  81. NCDVal__idx first_ref;
  82. union {
  83. char fastbuf[NCDVAL_FASTBUF_SIZE];
  84. struct NCDVal__ref align_ref;
  85. struct NCDVal__string align_string;
  86. struct NCDVal__list align_list;
  87. struct NCDVal__mapelem align_mapelem;
  88. struct NCDVal__idstring align_idstring;
  89. struct NCDVal__externalstring align_externalstring;
  90. };
  91. } NCDValMem;
  92. typedef struct {
  93. NCDValMem *mem;
  94. NCDVal__idx idx;
  95. } NCDValRef;
  96. typedef struct {
  97. NCDVal__idx idx;
  98. } NCDValSafeRef;
  99. typedef struct NCDVal__mapelem NCDVal__maptree_entry;
  100. typedef NCDValMem *NCDVal__maptree_arg;
  101. #include "NCDVal_maptree.h"
  102. #include <structure/CAvl_decl.h>
  103. struct NCDVal__map {
  104. int type;
  105. NCDVal__idx maxcount;
  106. NCDVal__idx count;
  107. NCDVal__MapTree tree;
  108. struct NCDVal__mapelem elems[];
  109. };
  110. typedef struct {
  111. NCDVal__idx elemidx;
  112. } NCDValMapElem;
  113. #define NCDVAL_INSTR_PLACEHOLDER 0
  114. #define NCDVAL_INSTR_REINSERT 1
  115. #define NCDVAL_INSTR_BUMPDEPTH 2
  116. struct NCDVal__instr {
  117. int type;
  118. union {
  119. struct {
  120. NCDVal__idx plid;
  121. NCDVal__idx plidx;
  122. } placeholder;
  123. struct {
  124. NCDVal__idx mapidx;
  125. NCDVal__idx elempos;
  126. } reinsert;
  127. struct {
  128. NCDVal__idx parent_idx;
  129. NCDVal__idx child_idx_idx;
  130. } bumpdepth;
  131. };
  132. };
  133. typedef struct {
  134. struct NCDVal__instr *instrs;
  135. size_t num_instrs;
  136. } NCDValReplaceProg;
  137. typedef struct {
  138. char *data;
  139. int is_allocated;
  140. } NCDValNullTermString;
  141. //
  142. #define NCDVAL_STRING 1
  143. #define NCDVAL_LIST 2
  144. #define NCDVAL_MAP 3
  145. #define NCDVAL_PLACEHOLDER 4
  146. /**
  147. * Initializes a value memory object.
  148. * A value memory object holds memory for value structures. Values within
  149. * the memory are referenced using {@link NCDValRef} objects, which point
  150. * to values within memory objects.
  151. *
  152. * Values may be added to a memory object using functions such as
  153. * {@link NCDVal_NewString}, {@link NCDVal_NewList} and {@link NCDVal_NewMap},
  154. * and {@link NCDVal_NewCopy}, which return references to the new values within
  155. * the memory object.
  156. *
  157. * It is not possible to remove values from the memory object, or modify existing
  158. * values other than adding elements to pre-allocated slots in lists and maps.
  159. * Once a value is added, it will consume memory as long as its memory object
  160. * exists. This is by design - this code is intended and optimized for constructing
  161. * and passing around values, not for operating on them in place. In fact, al
  162. * values within a memory object are stored in a single memory buffer, as an
  163. * embedded data structure with relativepointers. For example, map values use an
  164. * embedded AVL tree.
  165. */
  166. void NCDValMem_Init (NCDValMem *o);
  167. /**
  168. * Frees a value memory object.
  169. * All values within the memory object cease to exist, and any {@link NCDValRef}
  170. * object pointing to them must no longer be used.
  171. */
  172. void NCDValMem_Free (NCDValMem *o);
  173. /**
  174. * Initializes the memory object to be a copy of an existing memory object.
  175. * Value references from the original may be used if they are first turned
  176. * to {@link NCDValSafeRef} using {@link NCDVal_ToSafe} and back to
  177. * {@link NCDValRef} using {@link NCDVal_FromSafe} with the new memory object
  178. * specified. Alternatively, {@link NCDVal_Moved} can be used.
  179. * Returns 1 on success and 0 on failure.
  180. */
  181. int NCDValMem_InitCopy (NCDValMem *o, NCDValMem *other) WARN_UNUSED;
  182. /**
  183. * Does nothing.
  184. * The value reference object must either point to a valid value within a valid
  185. * memory object, or must be an invalid reference (most functions operating on
  186. * {@link NCDValRef} implicitly require that).
  187. */
  188. void NCDVal_Assert (NCDValRef val);
  189. /**
  190. * Determines if a value reference is invalid.
  191. */
  192. int NCDVal_IsInvalid (NCDValRef val);
  193. /**
  194. * Determines if a value is a placeholder value.
  195. * The value reference must not be an invalid reference.
  196. */
  197. int NCDVal_IsPlaceholder (NCDValRef val);
  198. /**
  199. * Returns the type of the value reference, which must not be an invalid reference.
  200. * Possible values are NCDVAL_STRING, NCDVAL_LIST, NCDVAL_MAP and NCDVAL_PLACEHOLDER.
  201. * The placeholder type is only used internally in the interpreter for argument
  202. * resolution, and is never seen by modules; see {@link NCDVal_NewPlaceholder}.
  203. */
  204. int NCDVal_Type (NCDValRef val);
  205. /**
  206. * Returns an invalid reference.
  207. * An invalid reference must not be passed to any function here, except:
  208. * {@link NCDVal_Assert}, {@link NCDVal_IsInvalid}, {@link NCDVal_ToSafe},
  209. * {@link NCDVal_FromSafe}, {@link NCDVal_Moved}.
  210. */
  211. NCDValRef NCDVal_NewInvalid (void);
  212. /**
  213. * Returns a new placeholder value reference. A placeholder value is a valid value
  214. * containing an integer placeholder identifier.
  215. * This always succeeds; however, the caller must ensure the identifier is
  216. * non-negative and satisfies (NCDVAL_MINIDX + plid < -1).
  217. *
  218. * The placeholder type is only used internally in the interpreter for argument
  219. * resolution, and is never seen by modules. Also see {@link NCDPlaceholderDb}.
  220. */
  221. NCDValRef NCDVal_NewPlaceholder (NCDValMem *mem, int plid);
  222. /**
  223. * Returns the indentifier of a placeholder value.
  224. * The value reference must point to a placeholder value.
  225. */
  226. int NCDVal_PlaceholderId (NCDValRef val);
  227. /**
  228. * Copies a value into the specified memory object. The source
  229. * must not be an invalid reference, however it may reside in any memory
  230. * object (including 'mem').
  231. * Returns a reference to the copied value. On out of memory, returns
  232. * an invalid reference.
  233. */
  234. NCDValRef NCDVal_NewCopy (NCDValMem *mem, NCDValRef val);
  235. /**
  236. * Compares two values, both of which must not be invalid references.
  237. * Returns -1, 0 or 1.
  238. */
  239. int NCDVal_Compare (NCDValRef val1, NCDValRef val2);
  240. /**
  241. * Converts a value reference to a safe referece format, which remains valid
  242. * if the memory object is moved (safe references do not contain a pointer
  243. * to the memory object, unlike {@link NCDValRef} references).
  244. */
  245. NCDValSafeRef NCDVal_ToSafe (NCDValRef val);
  246. /**
  247. * Converts a safe value reference to a normal value reference.
  248. * This should be used to recover references from safe references
  249. * after the memory object is moved.
  250. */
  251. NCDValRef NCDVal_FromSafe (NCDValMem *mem, NCDValSafeRef sval);
  252. /**
  253. * Fixes a value reference after its memory object was moved.
  254. */
  255. NCDValRef NCDVal_Moved (NCDValMem *mem, NCDValRef val);
  256. /**
  257. * Determines if a value is a string value.
  258. * The value reference must not be an invalid reference.
  259. */
  260. int NCDVal_IsString (NCDValRef val);
  261. /**
  262. * Determines if a value is an ID-string value. See {@link NCDVal_NewIdString}
  263. * for an explanation of ID-string values.
  264. * The value reference must not be an invalid reference.
  265. */
  266. int NCDVal_IsIdString (NCDValRef val);
  267. /**
  268. * Determines if a value is an external string value.
  269. * See {@link NCDVal_NewExternalString} for an explanation of external
  270. * string values.
  271. * The value reference must not be an invalid reference.
  272. */
  273. int NCDVal_IsExternalString (NCDValRef val);
  274. /**
  275. * Determines if a value is a string value which has no null bytes.
  276. * The value reference must not be an invalid reference.
  277. */
  278. int NCDVal_IsStringNoNulls (NCDValRef val);
  279. /**
  280. * Builds a new string value from a null-terminated array of bytes.
  281. * Equivalent to NCDVal_NewStringBin(mem, data, strlen(data)).
  282. * Returns a reference to the new value, or an invalid reference
  283. * on out of memory.
  284. * WARNING: The buffer passed must NOT be part of any value in the
  285. * memory object specified. In particular, you may NOT use this
  286. * function to copy a string that resides in the same memory object.
  287. */
  288. NCDValRef NCDVal_NewString (NCDValMem *mem, const char *data);
  289. /**
  290. * Builds a new string value.
  291. * Returns a reference to the new value, or an invalid reference
  292. * on out of memory.
  293. * WARNING: The buffer passed must NOT be part of any value in the
  294. * memory object specified. In particular, you may NOT use this
  295. * function to copy a string that resides in the same memory object.
  296. */
  297. NCDValRef NCDVal_NewStringBin (NCDValMem *mem, const uint8_t *data, size_t len);
  298. /**
  299. * Builds a new string value of the given length with undefined contents.
  300. * You can define the contents of the string later by copying to the address
  301. * returned by {@link NCDVal_StringData}.
  302. */
  303. NCDValRef NCDVal_NewStringUninitialized (NCDValMem *mem, size_t len);
  304. /**
  305. * Builds a new ID-string value.
  306. * Returns a reference to the new value, or an invalid reference
  307. * on out of memory.
  308. *
  309. * An ID-string value is a special kind of string value which is represented
  310. * efficiently as a string identifier via {@link NCDStringIndex}. An ID-string
  311. * is also a string and is transparent for use. For example, for an ID-string,
  312. * {@link NCDVal_Type} still returns NCDVAL_STRING, {@link NCDVal_IsString}
  313. * returns 1, and {@link NCDVal_StringData} and {@link NCDVal_StringLength}
  314. * both work. The only way to distinguish an ID-string from a non-ID string is
  315. * by calling {@link NCDVal_IsIdString}.
  316. */
  317. NCDValRef NCDVal_NewIdString (NCDValMem *mem, NCD_string_id_t string_id,
  318. NCDStringIndex *string_index);
  319. /**
  320. * Builds a new string value pointing to the given external data. A reference to
  321. * the external data is taken using {@link NCDRefTarget}, unless 'ref_target' is
  322. * NULL. The data must not change while this value exists. Like ID-strings,
  323. * external strings are transparent for use. An external string can be recognized
  324. * using {@link NCDVal_IsExternalString}.
  325. *
  326. * Returns a reference to the new value, or an invalid reference
  327. * on out of memory.
  328. */
  329. NCDValRef NCDVal_NewExternalString (NCDValMem *mem, const char *data, size_t len,
  330. NCDRefTarget *ref_target);
  331. /**
  332. * Returns a pointer to the data of a string value.
  333. * WARNING: the string data may not be null-terminated. To get a null-terminated
  334. * version, use {@link NCDVal_StringNullTerminate}.
  335. * The value reference must point to a string value.
  336. */
  337. const char * NCDVal_StringData (NCDValRef string);
  338. /**
  339. * Returns the length of the string value.
  340. * The value reference must point to a string value.
  341. */
  342. size_t NCDVal_StringLength (NCDValRef string);
  343. /**
  344. * Produces a null-terminated version of a string value. On success, the result is
  345. * stored into a {@link NCDValNullTermString} structure, and the null-terminated
  346. * string is available via its 'data' member. This function may either simply pass
  347. * through the data pointer as returned by {@link NCDVal_StringData} (if the string
  348. * is known to be null-terminated) or produce a null-terminated dynamically allocated
  349. * copy.
  350. * On success, {@link NCDValNullTermString_Free} should be called to release any allocated
  351. * memory when the null-terminated string is no longer needed. This must be called before
  352. * the memory object is freed, because it may point to data inside the memory object.
  353. * It is guaranteed that *out is not modified on failure.
  354. * Returns 1 on success and 0 on failure.
  355. */
  356. int NCDVal_StringNullTerminate (NCDValRef string, NCDValNullTermString *out) WARN_UNUSED;
  357. /**
  358. * Returns a dummy {@link NCDValNullTermString} which can be freed using
  359. * {@link NCDValNullTermString_Free}, but need not be.
  360. */
  361. NCDValNullTermString NCDValNullTermString_NewDummy (void);
  362. /**
  363. * Releases any memory which was dynamically allocated by {@link NCDVal_StringNullTerminate}
  364. * to null-terminate a string.
  365. */
  366. void NCDValNullTermString_Free (NCDValNullTermString *o);
  367. /**
  368. * Returns the string ID and the string index of an ID-string.
  369. * The value given must be an ID-string value (which can be determined via
  370. * {@link NCDVal_IsIdString}). Both the \a out_string_id and \a out_string_index
  371. * pointers must be non-NULL.
  372. */
  373. void NCDVal_IdStringGet (NCDValRef idstring, NCD_string_id_t *out_string_id,
  374. NCDStringIndex **out_string_index);
  375. /**
  376. * Returns the string ID of an ID-string.
  377. * The value given must be an ID-string value (which can be determined via
  378. * {@link NCDVal_IsIdString}).
  379. */
  380. NCD_string_id_t NCDVal_IdStringId (NCDValRef idstring);
  381. /**
  382. * Returns the string index of an ID-string.
  383. * The value given must be an ID-string value (which can be determined via
  384. * {@link NCDVal_IsIdString}).
  385. */
  386. NCDStringIndex * NCDVal_IdStringStringIndex (NCDValRef idstring);
  387. /**
  388. * Returns the reference target of an external string. This may be NULL
  389. * if the external string is not associated with a reference target.
  390. * The value given must be an external string value (which can be determined
  391. * via {@link NCDVal_IsExternalString}).
  392. */
  393. NCDRefTarget * NCDVal_ExternalStringTarget (NCDValRef externalstring);
  394. /**
  395. * Determines if the string value has any null bytes in its contents.
  396. * The value reference must point to a string value.
  397. */
  398. int NCDVal_StringHasNulls (NCDValRef string);
  399. /**
  400. * Determines if the string value is equal to the given null-terminated
  401. * string.
  402. * The value reference must point to a string value.
  403. */
  404. int NCDVal_StringEquals (NCDValRef string, const char *data);
  405. /**
  406. * Determines if the string value is equal to the given string represented
  407. * by an {@link NCDStringIndex} identifier.
  408. * The value reference must point to a string value.
  409. * NOTE: \a string_index must be equal to the string_index of every ID-string
  410. * that exist within this memory object.
  411. */
  412. int NCDVal_StringEqualsId (NCDValRef string, NCD_string_id_t string_id,
  413. NCDStringIndex *string_index);
  414. /**
  415. * Determines if a value is a list value.
  416. * The value reference must not be an invalid reference.
  417. */
  418. int NCDVal_IsList (NCDValRef val);
  419. /**
  420. * Builds a new list value. The 'maxcount' argument specifies how
  421. * many element slots to preallocate. Not more than that many
  422. * elements may be appended to the list using {@link NCDVal_ListAppend}.
  423. * Returns a reference to the new value, or an invalid reference
  424. * on out of memory.
  425. */
  426. NCDValRef NCDVal_NewList (NCDValMem *mem, size_t maxcount);
  427. /**
  428. * Appends a value to to the list value.
  429. * The 'list' reference must point to a list value, and the
  430. * 'elem' reference must be non-invalid and point to a value within
  431. * the same memory object as the list.
  432. * Inserting a value into a list does not in any way change it;
  433. * internally, the list only points to it.
  434. * You must not modify the element after it has been inserted into the
  435. * list.
  436. * Returns 1 on success and 0 on failure (depth limit exceeded).
  437. */
  438. int NCDVal_ListAppend (NCDValRef list, NCDValRef elem) WARN_UNUSED;
  439. /**
  440. * Returns the number of elements in a list value, i.e. the number
  441. * of times {@link NCDVal_ListAppend} was called.
  442. * The 'list' reference must point to a list value.
  443. */
  444. size_t NCDVal_ListCount (NCDValRef list);
  445. /**
  446. * Returns the maximum number of elements a list value may contain,
  447. * i.e. the 'maxcount' argument to {@link NCDVal_NewList}.
  448. * The 'list' reference must point to a list value.
  449. */
  450. size_t NCDVal_ListMaxCount (NCDValRef list);
  451. /**
  452. * Returns a reference to the value at the given position 'pos' in a list,
  453. * starting with zero.
  454. * The 'list' reference must point to a list value.
  455. * The position 'pos' must refer to an existing element, i.e.
  456. * pos < NCDVal_ListCount().
  457. */
  458. NCDValRef NCDVal_ListGet (NCDValRef list, size_t pos);
  459. /**
  460. * Returns references to elements within a list by writing them
  461. * via (NCDValRef *) variable arguments.
  462. * If 'num' == NCDVal_ListCount(), succeeds, returing 1 and writing 'num'
  463. * references, as mentioned.
  464. * If 'num' != NCDVal_ListCount(), fails, returning 0, without writing any
  465. * references
  466. */
  467. int NCDVal_ListRead (NCDValRef list, int num, ...);
  468. /**
  469. * Like {@link NCDVal_ListRead}, but the list can contain more than 'num'
  470. * elements.
  471. */
  472. int NCDVal_ListReadHead (NCDValRef list, int num, ...);
  473. /**
  474. * Determines if a value is a map value.
  475. * The value reference must not be an invalid reference.
  476. */
  477. int NCDVal_IsMap (NCDValRef val);
  478. /**
  479. * Builds a new map value. The 'maxcount' argument specifies how
  480. * many entry slots to preallocate. Not more than that many
  481. * entries may be inserted to the map using {@link NCDVal_MapInsert}.
  482. * Returns a reference to the new value, or an invalid reference
  483. * on out of memory.
  484. */
  485. NCDValRef NCDVal_NewMap (NCDValMem *mem, size_t maxcount);
  486. /**
  487. * Inserts an entry to the map value.
  488. * The 'map' reference must point to a map value, and the
  489. * 'key' and 'val' references must be non-invalid and point to values within
  490. * the same memory object as the map.
  491. * Inserting an entry does not in any way change the 'key'and 'val';
  492. * internally, the map only points to it.
  493. * You must not modify the key after inserting it into a map. This is because
  494. * the map builds an embedded AVL tree of entries indexed by keys.
  495. * If insertion fails due to a maximum depth limit, returns 0.
  496. * Otherwise returns 1, and *out_inserted is set to 1 if the key did not
  497. * yet exist and the entry was inserted, and to 0 if it did exist and the
  498. * entry was not inserted. The 'out_inserted' pointer may be NULL, in which
  499. * case *out_inserted is never set.
  500. */
  501. int NCDVal_MapInsert (NCDValRef map, NCDValRef key, NCDValRef val, int *out_inserted) WARN_UNUSED;
  502. /**
  503. * Returns the number of entries in a map value, i.e. the number
  504. * of times {@link NCDVal_MapInsert} was called successfully.
  505. * The 'map' reference must point to a map value.
  506. */
  507. size_t NCDVal_MapCount (NCDValRef map);
  508. /**
  509. * Returns the maximum number of entries a map value may contain,
  510. * i.e. the 'maxcount' argument to {@link NCDVal_NewMap}.
  511. * The 'map' reference must point to a map value.
  512. */
  513. size_t NCDVal_MapMaxCount (NCDValRef map);
  514. /**
  515. * Determines if a map entry reference is invalid. This is used in combination
  516. * with the map iteration functions to detect the end of iteration.
  517. */
  518. int NCDVal_MapElemInvalid (NCDValMapElem me);
  519. /**
  520. * Returns a reference to the first entry in a map, with respect to some
  521. * arbitrary order.
  522. * If the map is empty, returns an invalid map entry reference.
  523. */
  524. NCDValMapElem NCDVal_MapFirst (NCDValRef map);
  525. /**
  526. * Returns a reference to the entry in a map that follows the entry referenced
  527. * by 'me', with respect to some arbitrary order.
  528. * The 'me' argument must be a non-invalid reference to an entry in the map.
  529. * If 'me' is the last entry, returns an invalid map entry reference.
  530. */
  531. NCDValMapElem NCDVal_MapNext (NCDValRef map, NCDValMapElem me);
  532. /**
  533. * Like {@link NCDVal_MapFirst}, but with respect to the order defined by
  534. * {@link NCDVal_Compare}.
  535. * Ordered iteration is slower and should only be used when needed.
  536. */
  537. NCDValMapElem NCDVal_MapOrderedFirst (NCDValRef map);
  538. /**
  539. * Like {@link NCDVal_MapNext}, but with respect to the order defined by
  540. * {@link NCDVal_Compare}.
  541. * Ordered iteration is slower and should only be used when needed.
  542. */
  543. NCDValMapElem NCDVal_MapOrderedNext (NCDValRef map, NCDValMapElem me);
  544. /**
  545. * Returns a reference to the key of the map entry referenced by 'me'.
  546. * The 'me' argument must be a non-invalid reference to an entry in the map.
  547. */
  548. NCDValRef NCDVal_MapElemKey (NCDValRef map, NCDValMapElem me);
  549. /**
  550. * Returns a reference to the value of the map entry referenced by 'me'.
  551. * The 'me' argument must be a non-invalid reference to an entry in the map.
  552. */
  553. NCDValRef NCDVal_MapElemVal (NCDValRef map, NCDValMapElem me);
  554. /**
  555. * Looks for a key in the map. The 'key' reference must be a non-invalid
  556. * value reference, and may point to a value in a different memory object
  557. * than the map.
  558. * If the key exists in the map, returns a reference to the corresponding
  559. * map entry.
  560. * If the key does not exist, returns an invalid map entry reference.
  561. */
  562. NCDValMapElem NCDVal_MapFindKey (NCDValRef map, NCDValRef key);
  563. /**
  564. * Retrieves the value reference to the value of the map entry whose key is a
  565. * string value equal to the given null-terminated string. If there is no such
  566. * entry, returns an invalid value reference.
  567. */
  568. NCDValRef NCDVal_MapGetValue (NCDValRef map, const char *key_str);
  569. /**
  570. * Builds a placeholder replacement program, which is a list of instructions for
  571. * efficiently replacing placeholders in identical values in identical memory
  572. * objects.
  573. * To actually perform replacements, make copies of the memory object of this value
  574. * using {@link NCDValMem_InitCopy}, then call {@link NCDValReplaceProg_Execute}
  575. * on the copies.
  576. * The value passed must be a valid value, and not a placeholder.
  577. * Returns 1 on success, 0 on failure.
  578. */
  579. int NCDValReplaceProg_Init (NCDValReplaceProg *o, NCDValRef val);
  580. /**
  581. * Frees the placeholder replacement program.
  582. */
  583. void NCDValReplaceProg_Free (NCDValReplaceProg *o);
  584. /**
  585. * Callback used by {@link NCDValReplaceProg_Execute} to allow the caller to produce
  586. * values of placeholders.
  587. * This function should build a new value within the memory object 'mem' (which is
  588. * the same as of the memory object where placeholders are being replaced).
  589. * On success, it should return 1, writing a valid value reference to *out.
  590. * On failure, it can either return 0, or return 1 but write an invalid value reference.
  591. * This callback must not access the memory object in any other way than building
  592. * new values in it; it must not modify any values that were already present at the
  593. * point it was called.
  594. */
  595. typedef int (*NCDVal_replace_func) (void *arg, int plid, NCDValMem *mem, NCDValRef *out);
  596. /**
  597. * Executes the replacement program, replacing placeholders in a value.
  598. * The memory object must given be identical to the memory object which was used in
  599. * {@link NCDValReplaceProg_Init}; see {@link NCDValMem_InitCopy}.
  600. * This will call the callback 'replace', which should build the values to replace
  601. * the placeholders.
  602. * Returns 1 on success and 0 on failure. On failure, the entire memory object enters
  603. * and inconsistent state and must be freed using {@link NCDValMem_Free} before
  604. * performing any other operation on it.
  605. * The program is passed by value instead of pointer because this appears to be faster.
  606. * Is is not modified in any way.
  607. */
  608. int NCDValReplaceProg_Execute (NCDValReplaceProg prog, NCDValMem *mem, NCDVal_replace_func replace, void *arg);
  609. #endif