NCDValCons.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /**
  2. * @file NCDValCons.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_NCDVALCONS_H
  30. #define BADVPN_NCDVALCONS_H
  31. #include <limits.h>
  32. #include <stdint.h>
  33. #include <misc/debug.h>
  34. #include <ncd/NCDVal.h>
  35. struct NCDValCons__temp_elem {
  36. NCDValSafeRef ref;
  37. int next;
  38. };
  39. /**
  40. * Value constructor; implements a mechanism for efficiently constructing
  41. * NCD values into {@link NCDVal} compact representation, but without
  42. * having to know the number of list or map elements in advance.
  43. * For the purpose of value construction, values are representing using
  44. * {@link NCDValConsVal} objects.
  45. */
  46. typedef struct {
  47. NCDValMem *mem;
  48. struct NCDValCons__temp_elem *elems;
  49. int elems_size;
  50. int elems_capacity;
  51. } NCDValCons;
  52. #define NCDVALCONS_TYPE_COMPLETE 1
  53. #define NCDVALCONS_TYPE_INCOMPLETE_LIST 2
  54. #define NCDVALCONS_TYPE_INCOMPLETE_MAP 3
  55. /**
  56. * Abstract handle which represents a value during constuction via
  57. * {@link NCDValCons}.
  58. */
  59. typedef struct {
  60. int cons_type;
  61. union {
  62. NCDValSafeRef complete_ref;
  63. struct {
  64. int elems_idx;
  65. int count;
  66. } incomplete;
  67. } u;
  68. } NCDValConsVal;
  69. #define NCDVALCONS_ERROR_MEMORY 1
  70. #define NCDVALCONS_ERROR_DUPLICATE_KEY 2
  71. /**
  72. * Initializes a value constructor.
  73. *
  74. * @param o value constructor to initialize
  75. * @param mem memory object where values will be stored into
  76. * @return 1 on success, 0 on failure
  77. */
  78. int NCDValCons_Init (NCDValCons *o, NCDValMem *mem) WARN_UNUSED;
  79. /**
  80. * Frees the value constructor. This only means the constuctor does
  81. * not exist any more; any values constructed and completed using
  82. * {@link NCDValCons_Complete} remain in the memory object.
  83. *
  84. * @param o value constructor to free
  85. */
  86. void NCDValCons_Free (NCDValCons *o);
  87. /**
  88. * Creates a new string value with the given data.
  89. *
  90. * @param o value constructor
  91. * @param data pointer to string data. This must not point into the
  92. * memory object the value constructor is using. The data
  93. * is copied.
  94. * @param len length of the string
  95. * @param out on success, *out will be set to a handle representing
  96. * the new string
  97. * @param out_error on failure, *out_error will be set to an error code
  98. * @return 1 on success, 0 on failure
  99. */
  100. int NCDValCons_NewString (NCDValCons *o, const uint8_t *data, size_t len, NCDValConsVal *out, int *out_error) WARN_UNUSED;
  101. /**
  102. * Creates an empty list value.
  103. *
  104. * @param o value constructor
  105. * @param out *out will be set to a handle representing the new list
  106. */
  107. void NCDValCons_NewList (NCDValCons *o, NCDValConsVal *out);
  108. /**
  109. * Creates an empty map value.
  110. *
  111. * @param o value constructor
  112. * @param out *out will be set to a handle representing the new map
  113. */
  114. void NCDValCons_NewMap (NCDValCons *o, NCDValConsVal *out);
  115. /**
  116. * Prepends an element to a list value.
  117. *
  118. * @param o value constructor
  119. * @param list pointer to the handle representing the list. On success,
  120. * the handle will be modified, and the old handle must not
  121. * be used any more.
  122. * @param elem handle representing the value to be prepended. This handle
  123. * must not be used any more after being prepended to the list.
  124. * @param out_error on failure, *out_error will be set to an error code
  125. * @return 1 on success, 0 on failure
  126. */
  127. int NCDValCons_ListPrepend (NCDValCons *o, NCDValConsVal *list, NCDValConsVal elem, int *out_error) WARN_UNUSED;
  128. /**
  129. * Inserts an entry into a map value.
  130. *
  131. * @param o value constructor
  132. * @param map pointer to the handle representing the map. On success,
  133. * the handle will be modified, and the old handle must not
  134. * be used any more.
  135. * @param key handle representing the key of the entry. This handle
  136. * must not be used any more after being inserted into the map.
  137. * @param value handle representing the value of the entry. This handle
  138. * must not be used any more after being inserted into the
  139. * map.
  140. * @param out_error on failure, *out_error will be set to an error code
  141. * @return 1 on success, 0 on failure
  142. */
  143. int NCDValCons_MapInsert (NCDValCons *o, NCDValConsVal *map, NCDValConsVal key, NCDValConsVal value, int *out_error) WARN_UNUSED;
  144. /**
  145. * Completes a value represented by a {@link NCDValConsVal} handle,
  146. * producing a {@link NCDValRef} object which refers to this value within
  147. * the memory object.
  148. *
  149. * @param o value constructor
  150. * @param val handle representing the value to be completed. After a value
  151. * is completed, the handle must not be used any more.
  152. * @param out on success, *out will be set to a {@link NCDValRef} object
  153. * referencing the completed value
  154. * @param out_error on failure, *out_error will be set to an error code
  155. * @return 1 on success, 0 on failure
  156. */
  157. int NCDValCons_Complete (NCDValCons *o, NCDValConsVal val, NCDValRef *out, int *out_error) WARN_UNUSED;
  158. #endif