NCDMethodIndex.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 = NULL;
  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(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)
  103. {
  104. if (!NamesArray_Init(o, NCDMETHODINDEX_NUM_EXPECTED_METHOD_NAMES)) {
  105. BLog(BLOG_ERROR, "NamesArray_Init failed");
  106. goto fail0;
  107. }
  108. if (!EntriesArray_Init(o, NCDMETHODINDEX_NUM_EXPECTED_ENTRIES)) {
  109. BLog(BLOG_ERROR, "EntriesArray_Init failed");
  110. goto fail1;
  111. }
  112. o->num_names = 0;
  113. o->num_entries = 0;
  114. if (!NCDMethodIndex__Hash_Init(&o->hash, NCDMETHODINDEX_NUM_EXPECTED_METHOD_NAMES)) {
  115. BLog(BLOG_ERROR, "NCDMethodIndex__Hash_Init failed");
  116. goto fail2;
  117. }
  118. return 1;
  119. fail2:
  120. EntriesArray_Free(o);
  121. fail1:
  122. NamesArray_Free(o);
  123. fail0:
  124. return 0;
  125. }
  126. void NCDMethodIndex_Free (NCDMethodIndex *o)
  127. {
  128. for (int i = 0; i < o->num_names; i++) {
  129. free(o->names[i].method_name);
  130. }
  131. for (int i = 0; i < o->num_entries; i++) {
  132. free(o->entries[i].obj_type);
  133. }
  134. NCDMethodIndex__Hash_Free(&o->hash);
  135. EntriesArray_Free(o);
  136. NamesArray_Free(o);
  137. }
  138. int NCDMethodIndex_AddMethod (NCDMethodIndex *o, const char *obj_type, const char *method_name, const struct NCDModule *module)
  139. {
  140. ASSERT(obj_type)
  141. ASSERT(method_name)
  142. ASSERT(module)
  143. int first_entry_idx;
  144. if (!find_method_name(o, method_name, &first_entry_idx)) {
  145. int entry_idx;
  146. if (!add_method_name(o, method_name, &entry_idx)) {
  147. goto fail0;
  148. }
  149. ASSERT(entry_idx >= 0)
  150. ASSERT(entry_idx < o->num_entries)
  151. struct NCDMethodIndex__entry *entry = &o->entries[entry_idx];
  152. if (!(entry->obj_type = b_strdup(obj_type))) {
  153. BLog(BLOG_ERROR, "b_strdup failed");
  154. goto fail0;
  155. }
  156. entry->module = module;
  157. } else {
  158. ASSERT(first_entry_idx >= 0)
  159. ASSERT(first_entry_idx < o->num_entries)
  160. if (o->num_entries == o->entries_capacity && !EntriesArray_DoubleUp(o)) {
  161. BLog(BLOG_ERROR, "EntriesArray_DoubleUp failed");
  162. goto fail0;
  163. }
  164. struct NCDMethodIndex__entry *entry = &o->entries[o->num_entries];
  165. if (!(entry->obj_type = b_strdup(obj_type))) {
  166. BLog(BLOG_ERROR, "b_strdup failed");
  167. goto fail0;
  168. }
  169. entry->module = module;
  170. entry->next = o->entries[first_entry_idx].next;
  171. o->entries[first_entry_idx].next = o->num_entries;
  172. o->num_entries++;
  173. }
  174. return 1;
  175. fail0:
  176. return 0;
  177. }
  178. int NCDMethodIndex_GetMethodNameId (NCDMethodIndex *o, const char *method_name)
  179. {
  180. ASSERT(method_name)
  181. int first_entry_idx;
  182. if (!find_method_name(o, method_name, &first_entry_idx)) {
  183. if (!add_method_name(o, method_name, &first_entry_idx)) {
  184. return -1;
  185. }
  186. }
  187. ASSERT(first_entry_idx >= 0)
  188. ASSERT(first_entry_idx < o->num_entries)
  189. return first_entry_idx;
  190. }
  191. const struct NCDModule * NCDMethodIndex_GetMethodModule (NCDMethodIndex *o, const char *obj_type, int method_name_id)
  192. {
  193. ASSERT(obj_type)
  194. ASSERT(method_name_id >= 0)
  195. ASSERT(method_name_id < o->num_entries)
  196. do {
  197. struct NCDMethodIndex__entry *entry = &o->entries[method_name_id];
  198. if (entry->obj_type && !strcmp(entry->obj_type, obj_type)) {
  199. ASSERT(entry->module)
  200. return entry->module;
  201. }
  202. method_name_id = entry->next;
  203. ASSERT(method_name_id >= -1)
  204. ASSERT(method_name_id < o->num_entries)
  205. } while (method_name_id >= 0);
  206. return NULL;
  207. }