NCDMethodIndex.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /**
  2. * @file NCDMethodIndex.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 <limits.h>
  32. #include <misc/hashfun.h>
  33. #include <misc/balloc.h>
  34. #include <misc/strdup.h>
  35. #include "NCDMethodIndex.h"
  36. #include "NCDMethodIndex_hash.h"
  37. #include <structure/CHash_impl.h>
  38. #define GROWARRAY_NAME NamesArray
  39. #define GROWARRAY_OBJECT_TYPE NCDMethodIndex
  40. #define GROWARRAY_ARRAY_MEMBER names
  41. #define GROWARRAY_CAPACITY_MEMBER names_capacity
  42. #define GROWARRAY_MAX_CAPACITY INT_MAX
  43. #include <misc/grow_array.h>
  44. #define GROWARRAY_NAME EntriesArray
  45. #define GROWARRAY_OBJECT_TYPE NCDMethodIndex
  46. #define GROWARRAY_ARRAY_MEMBER entries
  47. #define GROWARRAY_CAPACITY_MEMBER entries_capacity
  48. #define GROWARRAY_MAX_CAPACITY INT_MAX
  49. #include <misc/grow_array.h>
  50. #include <generated/blog_channel_ncd.h>
  51. static int find_method_name (NCDMethodIndex *o, const char *method_name, int *out_entry_idx)
  52. {
  53. ASSERT(method_name)
  54. NCDMethodIndex__HashRef ref = NCDMethodIndex__Hash_Lookup(&o->hash, o->names, method_name);
  55. if (ref.link == -1) {
  56. return 0;
  57. }
  58. ASSERT(ref.link >= 0)
  59. ASSERT(ref.link < o->num_names)
  60. struct NCDMethodIndex__method_name *name_entry = ref.ptr;
  61. ASSERT(!strcmp(name_entry->method_name, method_name))
  62. ASSERT(name_entry->first_entry >= 0)
  63. ASSERT(name_entry->first_entry < o->num_entries)
  64. if (out_entry_idx) {
  65. *out_entry_idx = name_entry->first_entry;
  66. }
  67. return 1;
  68. }
  69. static int add_method_name (NCDMethodIndex *o, const char *method_name, int *out_entry_idx)
  70. {
  71. ASSERT(method_name)
  72. ASSERT(!find_method_name(o, method_name, NULL))
  73. if (o->num_entries == o->entries_capacity && !EntriesArray_DoubleUp(o)) {
  74. BLog(BLOG_ERROR, "EntriesArray_DoubleUp failed");
  75. goto fail0;
  76. }
  77. if (o->num_names == o->names_capacity && !NamesArray_DoubleUp(o)) {
  78. BLog(BLOG_ERROR, "NamesArray_DoubleUp failed");
  79. goto fail0;
  80. }
  81. struct NCDMethodIndex__entry *entry = &o->entries[o->num_entries];
  82. entry->obj_type = -1;
  83. entry->next = -1;
  84. struct NCDMethodIndex__method_name *name_entry = &o->names[o->num_names];
  85. if (!(name_entry->method_name = b_strdup(method_name))) {
  86. BLog(BLOG_ERROR, "b_strdup failed");
  87. goto fail0;
  88. }
  89. name_entry->first_entry = o->num_entries;
  90. NCDMethodIndex__HashRef ref = {name_entry, o->num_names};
  91. int res = NCDMethodIndex__Hash_Insert(&o->hash, o->names, ref, NULL);
  92. ASSERT_EXECUTE(res)
  93. o->num_entries++;
  94. o->num_names++;
  95. if (out_entry_idx) {
  96. *out_entry_idx = name_entry->first_entry;
  97. }
  98. return 1;
  99. fail0:
  100. return 0;
  101. }
  102. int NCDMethodIndex_Init (NCDMethodIndex *o, NCDStringIndex *string_index)
  103. {
  104. ASSERT(string_index)
  105. o->string_index = string_index;
  106. if (!NamesArray_Init(o, NCDMETHODINDEX_NUM_EXPECTED_METHOD_NAMES)) {
  107. BLog(BLOG_ERROR, "NamesArray_Init failed");
  108. goto fail0;
  109. }
  110. if (!EntriesArray_Init(o, NCDMETHODINDEX_NUM_EXPECTED_ENTRIES)) {
  111. BLog(BLOG_ERROR, "EntriesArray_Init failed");
  112. goto fail1;
  113. }
  114. o->num_names = 0;
  115. o->num_entries = 0;
  116. if (!NCDMethodIndex__Hash_Init(&o->hash, NCDMETHODINDEX_NUM_EXPECTED_METHOD_NAMES)) {
  117. BLog(BLOG_ERROR, "NCDMethodIndex__Hash_Init failed");
  118. goto fail2;
  119. }
  120. return 1;
  121. fail2:
  122. EntriesArray_Free(o);
  123. fail1:
  124. NamesArray_Free(o);
  125. fail0:
  126. return 0;
  127. }
  128. void NCDMethodIndex_Free (NCDMethodIndex *o)
  129. {
  130. for (int i = 0; i < o->num_names; i++) {
  131. free(o->names[i].method_name);
  132. }
  133. NCDMethodIndex__Hash_Free(&o->hash);
  134. EntriesArray_Free(o);
  135. NamesArray_Free(o);
  136. }
  137. int NCDMethodIndex_AddMethod (NCDMethodIndex *o, const char *obj_type, size_t obj_type_len, const char *method_name, const struct NCDInterpModule *module)
  138. {
  139. ASSERT(obj_type)
  140. ASSERT(method_name)
  141. ASSERT(module)
  142. NCD_string_id_t obj_type_id = NCDStringIndex_GetBin(o->string_index, obj_type, obj_type_len);
  143. if (obj_type_id < 0) {
  144. BLog(BLOG_ERROR, "NCDStringIndex_Get failed");
  145. goto fail0;
  146. }
  147. int entry_idx;
  148. int first_entry_idx;
  149. if (!find_method_name(o, method_name, &first_entry_idx)) {
  150. if (!add_method_name(o, method_name, &entry_idx)) {
  151. goto fail0;
  152. }
  153. ASSERT(entry_idx >= 0)
  154. ASSERT(entry_idx < o->num_entries)
  155. struct NCDMethodIndex__entry *entry = &o->entries[entry_idx];
  156. entry->obj_type = obj_type_id;
  157. entry->module = module;
  158. } else {
  159. ASSERT(first_entry_idx >= 0)
  160. ASSERT(first_entry_idx < o->num_entries)
  161. if (o->num_entries == o->entries_capacity && !EntriesArray_DoubleUp(o)) {
  162. BLog(BLOG_ERROR, "EntriesArray_DoubleUp failed");
  163. goto fail0;
  164. }
  165. entry_idx = o->num_entries;
  166. struct NCDMethodIndex__entry *entry = &o->entries[o->num_entries];
  167. entry->obj_type = obj_type_id;
  168. entry->module = module;
  169. entry->next = o->entries[first_entry_idx].next;
  170. o->entries[first_entry_idx].next = o->num_entries;
  171. o->num_entries++;
  172. }
  173. return entry_idx;
  174. fail0:
  175. return -1;
  176. }
  177. void NCDMethodIndex_RemoveMethod (NCDMethodIndex *o, int method_name_id)
  178. {
  179. ASSERT(method_name_id >= 0)
  180. ASSERT(method_name_id < o->num_entries)
  181. ASSERT(o->entries[method_name_id].obj_type >= 0)
  182. o->entries[method_name_id].obj_type = -1;
  183. }
  184. int NCDMethodIndex_GetMethodNameId (NCDMethodIndex *o, const char *method_name)
  185. {
  186. ASSERT(method_name)
  187. int first_entry_idx;
  188. if (!find_method_name(o, method_name, &first_entry_idx)) {
  189. if (!add_method_name(o, method_name, &first_entry_idx)) {
  190. return -1;
  191. }
  192. }
  193. ASSERT(first_entry_idx >= 0)
  194. ASSERT(first_entry_idx < o->num_entries)
  195. return first_entry_idx;
  196. }
  197. const struct NCDInterpModule * NCDMethodIndex_GetMethodModule (NCDMethodIndex *o, NCD_string_id_t obj_type, int method_name_id)
  198. {
  199. ASSERT(obj_type >= 0)
  200. ASSERT(method_name_id >= 0)
  201. ASSERT(method_name_id < o->num_entries)
  202. do {
  203. struct NCDMethodIndex__entry *entry = &o->entries[method_name_id];
  204. if (entry->obj_type == obj_type) {
  205. ASSERT(entry->module)
  206. return entry->module;
  207. }
  208. method_name_id = entry->next;
  209. ASSERT(method_name_id >= -1)
  210. ASSERT(method_name_id < o->num_entries)
  211. } while (method_name_id >= 0);
  212. return NULL;
  213. }