NCDVal.h 21 KB

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