NCDValCons.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. #define NCDVALCONS_ERROR_DEPTH 3
  72. /**
  73. * Initializes a value constructor.
  74. *
  75. * @param o value constructor to initialize
  76. * @param mem memory object where values will be stored into
  77. * @return 1 on success, 0 on failure
  78. */
  79. int NCDValCons_Init (NCDValCons *o, NCDValMem *mem) WARN_UNUSED;
  80. /**
  81. * Frees the value constructor. This only means the constuctor does
  82. * not exist any more; any values constructed and completed using
  83. * {@link NCDValCons_Complete} remain in the memory object.
  84. *
  85. * @param o value constructor to free
  86. */
  87. void NCDValCons_Free (NCDValCons *o);
  88. /**
  89. * Creates a new string value with the given data.
  90. *
  91. * @param o value constructor
  92. * @param data pointer to string data. This must not point into the
  93. * memory object the value constructor is using. The data
  94. * is copied.
  95. * @param len length of the string
  96. * @param out on success, *out will be set to a handle representing
  97. * the new string
  98. * @param out_error on failure, *out_error will be set to an error code
  99. * @return 1 on success, 0 on failure
  100. */
  101. int NCDValCons_NewString (NCDValCons *o, const uint8_t *data, size_t len, NCDValConsVal *out, int *out_error) WARN_UNUSED;
  102. /**
  103. * Creates an empty list value.
  104. *
  105. * @param o value constructor
  106. * @param out *out will be set to a handle representing the new list
  107. */
  108. void NCDValCons_NewList (NCDValCons *o, NCDValConsVal *out);
  109. /**
  110. * Creates an empty map value.
  111. *
  112. * @param o value constructor
  113. * @param out *out will be set to a handle representing the new map
  114. */
  115. void NCDValCons_NewMap (NCDValCons *o, NCDValConsVal *out);
  116. /**
  117. * Prepends an element to a list value.
  118. *
  119. * @param o value constructor
  120. * @param list pointer to the handle representing the list. On success,
  121. * the handle will be modified, and the old handle must not
  122. * be used any more.
  123. * @param elem handle representing the value to be prepended. This handle
  124. * must not be used any more after being prepended to the list.
  125. * @param out_error on failure, *out_error will be set to an error code
  126. * @return 1 on success, 0 on failure
  127. */
  128. int NCDValCons_ListPrepend (NCDValCons *o, NCDValConsVal *list, NCDValConsVal elem, int *out_error) WARN_UNUSED;
  129. /**
  130. * Inserts an entry into a map value.
  131. *
  132. * @param o value constructor
  133. * @param map pointer to the handle representing the map. On success,
  134. * the handle will be modified, and the old handle must not
  135. * be used any more.
  136. * @param key handle representing the key of the entry. This handle
  137. * must not be used any more after being inserted into the map.
  138. * @param value handle representing the value of the entry. This handle
  139. * must not be used any more after being inserted into the
  140. * map.
  141. * @param out_error on failure, *out_error will be set to an error code
  142. * @return 1 on success, 0 on failure
  143. */
  144. int NCDValCons_MapInsert (NCDValCons *o, NCDValConsVal *map, NCDValConsVal key, NCDValConsVal value, int *out_error) WARN_UNUSED;
  145. /**
  146. * Completes a value represented by a {@link NCDValConsVal} handle,
  147. * producing a {@link NCDValRef} object which refers to this value within
  148. * the memory object.
  149. *
  150. * @param o value constructor
  151. * @param val handle representing the value to be completed. After a value
  152. * is completed, the handle must not be used any more.
  153. * @param out on success, *out will be set to a {@link NCDValRef} object
  154. * referencing the completed value
  155. * @param out_error on failure, *out_error will be set to an error code
  156. * @return 1 on success, 0 on failure
  157. */
  158. int NCDValCons_Complete (NCDValCons *o, NCDValConsVal val, NCDValRef *out, int *out_error) WARN_UNUSED;
  159. #endif