NCDStringIndex.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /**
  2. * @file NCDStringIndex.c
  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. #include <string.h>
  30. #include <stdlib.h>
  31. #include <misc/hashfun.h>
  32. #include <misc/strdup.h>
  33. #include <misc/array_length.h>
  34. #include <base/BLog.h>
  35. #include "NCDStringIndex.h"
  36. #include "NCDStringIndex_hash.h"
  37. #include <structure/CHash_impl.h>
  38. #define GROWARRAY_NAME Array
  39. #define GROWARRAY_OBJECT_TYPE NCDStringIndex
  40. #define GROWARRAY_ARRAY_MEMBER entries
  41. #define GROWARRAY_CAPACITY_MEMBER entries_capacity
  42. #define GROWARRAY_MAX_CAPACITY NCD_STRING_ID_MAX
  43. #include <misc/grow_array.h>
  44. #include <generated/blog_channel_ncd.h>
  45. // NOTE: keep synchronized with static_strings.h
  46. static const char *static_strings[] = {
  47. "",
  48. "_args",
  49. "_arg0",
  50. "_arg1",
  51. "_arg2",
  52. "_arg3",
  53. "_arg4",
  54. "_arg5",
  55. "_arg6",
  56. "_arg7",
  57. "_arg8",
  58. "_arg9",
  59. "_arg10",
  60. "_arg11",
  61. "_arg12",
  62. "_arg13",
  63. "_arg14",
  64. "_arg15",
  65. "_arg16",
  66. "_arg17",
  67. "_arg18",
  68. "_arg19",
  69. "true",
  70. "false",
  71. "<none>",
  72. "_caller",
  73. "succeeded",
  74. "is_error",
  75. "not_eof",
  76. "length",
  77. "type",
  78. "exit_status",
  79. "size",
  80. "eof",
  81. };
  82. static NCD_string_id_t do_get (NCDStringIndex *o, const char *str, size_t str_len)
  83. {
  84. ASSERT(str)
  85. NCDStringIndex_hash_key key = {str, str_len};
  86. NCDStringIndex__HashRef ref = NCDStringIndex__Hash_Lookup(&o->hash, o->entries, key);
  87. ASSERT(ref.link == -1 || ref.link >= 0)
  88. ASSERT(ref.link == -1 || ref.link < o->entries_size)
  89. ASSERT(ref.link == -1 || (ref.ptr->str_len == str_len && !memcmp(ref.ptr->str, str, str_len)))
  90. if (ref.link != -1) {
  91. return ref.link;
  92. }
  93. if (o->entries_size == o->entries_capacity) {
  94. if (!Array_DoubleUp(o)) {
  95. BLog(BLOG_ERROR, "Array_DoubleUp failed");
  96. return -1;
  97. }
  98. if (!NCDStringIndex__Hash_MultiplyBuckets(&o->hash, o->entries, 1)) {
  99. BLog(BLOG_ERROR, "NCDStringIndex__Hash_MultiplyBuckets failed");
  100. return -1;
  101. }
  102. }
  103. ASSERT(o->entries_size < o->entries_capacity)
  104. struct NCDStringIndex__entry *entry = &o->entries[o->entries_size];
  105. if (!(entry->str = b_strdup_bin(str, str_len))) {
  106. BLog(BLOG_ERROR, "b_strdup_bin failed");
  107. return -1;
  108. }
  109. entry->str_len = str_len;
  110. entry->has_nulls = !!memchr(str, '\0', str_len);
  111. NCDStringIndex__HashRef newref = {entry, o->entries_size};
  112. int res = NCDStringIndex__Hash_Insert(&o->hash, o->entries, newref, NULL);
  113. ASSERT_EXECUTE(res)
  114. return o->entries_size++;
  115. }
  116. int NCDStringIndex_Init (NCDStringIndex *o)
  117. {
  118. o->entries_size = 0;
  119. if (!Array_Init(o, NCDSTRINGINDEX_INITIAL_CAPACITY)) {
  120. BLog(BLOG_ERROR, "Array_Init failed");
  121. goto fail0;
  122. }
  123. if (!NCDStringIndex__Hash_Init(&o->hash, NCDSTRINGINDEX_INITIAL_HASH_BUCKETS)) {
  124. BLog(BLOG_ERROR, "NCDStringIndex__Hash_Init failed");
  125. goto fail1;
  126. }
  127. for (size_t i = 0; i < B_ARRAY_LENGTH(static_strings); i++) {
  128. if (do_get(o, static_strings[i], strlen(static_strings[i])) < 0) {
  129. goto fail2;
  130. }
  131. }
  132. DebugObject_Init(&o->d_obj);
  133. return 1;
  134. fail2:
  135. for (NCD_string_id_t i = 0; i < o->entries_size; i++) {
  136. free(o->entries[i].str);
  137. }
  138. NCDStringIndex__Hash_Free(&o->hash);
  139. fail1:
  140. Array_Free(o);
  141. fail0:
  142. return 0;
  143. }
  144. void NCDStringIndex_Free (NCDStringIndex *o)
  145. {
  146. DebugObject_Free(&o->d_obj);
  147. for (NCD_string_id_t i = 0; i < o->entries_size; i++) {
  148. free(o->entries[i].str);
  149. }
  150. NCDStringIndex__Hash_Free(&o->hash);
  151. Array_Free(o);
  152. }
  153. NCD_string_id_t NCDStringIndex_Lookup (NCDStringIndex *o, const char *str)
  154. {
  155. DebugObject_Access(&o->d_obj);
  156. ASSERT(str)
  157. return NCDStringIndex_LookupBin(o, str, strlen(str));
  158. }
  159. NCD_string_id_t NCDStringIndex_LookupBin (NCDStringIndex *o, const char *str, size_t str_len)
  160. {
  161. DebugObject_Access(&o->d_obj);
  162. ASSERT(str)
  163. NCDStringIndex_hash_key key = {str, str_len};
  164. NCDStringIndex__HashRef ref = NCDStringIndex__Hash_Lookup(&o->hash, o->entries, key);
  165. ASSERT(ref.link == -1 || ref.link >= 0)
  166. ASSERT(ref.link == -1 || ref.link < o->entries_size)
  167. ASSERT(ref.link == -1 || (ref.ptr->str_len == str_len && !memcmp(ref.ptr->str, str, str_len)))
  168. return ref.link;
  169. }
  170. NCD_string_id_t NCDStringIndex_Get (NCDStringIndex *o, const char *str)
  171. {
  172. DebugObject_Access(&o->d_obj);
  173. ASSERT(str)
  174. return NCDStringIndex_GetBin(o, str, strlen(str));
  175. }
  176. NCD_string_id_t NCDStringIndex_GetBin (NCDStringIndex *o, const char *str, size_t str_len)
  177. {
  178. DebugObject_Access(&o->d_obj);
  179. ASSERT(str)
  180. return do_get(o, str, str_len);
  181. }
  182. const char * NCDStringIndex_Value (NCDStringIndex *o, NCD_string_id_t id)
  183. {
  184. DebugObject_Access(&o->d_obj);
  185. ASSERT(id >= 0)
  186. ASSERT(id < o->entries_size)
  187. ASSERT(o->entries[id].str)
  188. return o->entries[id].str;
  189. }
  190. size_t NCDStringIndex_Length (NCDStringIndex *o, NCD_string_id_t id)
  191. {
  192. DebugObject_Access(&o->d_obj);
  193. ASSERT(id >= 0)
  194. ASSERT(id < o->entries_size)
  195. ASSERT(o->entries[id].str)
  196. return o->entries[id].str_len;
  197. }
  198. int NCDStringIndex_HasNulls (NCDStringIndex *o, NCD_string_id_t id)
  199. {
  200. DebugObject_Access(&o->d_obj);
  201. ASSERT(id >= 0)
  202. ASSERT(id < o->entries_size)
  203. ASSERT(o->entries[id].str)
  204. return o->entries[id].has_nulls;
  205. }
  206. int NCDStringIndex_GetRequests (NCDStringIndex *o, struct NCD_string_request *requests)
  207. {
  208. DebugObject_Access(&o->d_obj);
  209. ASSERT(requests)
  210. while (requests->str) {
  211. NCD_string_id_t id = NCDStringIndex_Get(o, requests->str);
  212. if (id < 0) {
  213. return 0;
  214. }
  215. requests->id = id;
  216. requests++;
  217. }
  218. return 1;
  219. }