NCDModuleIndex.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /**
  2. * @file NCDModuleIndex.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/offset.h>
  32. #include <misc/balloc.h>
  33. #include <misc/hashfun.h>
  34. #include <misc/compare.h>
  35. #include <base/BLog.h>
  36. #include "NCDModuleIndex.h"
  37. #include <generated/blog_channel_NCDModuleIndex.h>
  38. #include "NCDModuleIndex_mhash.h"
  39. #include <structure/CHash_impl.h>
  40. static int string_pointer_comparator (void *user, const char **s1, const char **s2)
  41. {
  42. int cmp = strcmp(*s1, *s2);
  43. return B_COMPARE(cmp, 0);
  44. }
  45. static struct NCDModuleIndex_module * find_module (NCDModuleIndex *o, const char *type)
  46. {
  47. NCDModuleIndex__MHashRef ref = NCDModuleIndex__MHash_Lookup(&o->modules_hash, o->modules, type);
  48. if (ref.link == NCDModuleIndex__MHashNullLink()) {
  49. return NULL;
  50. }
  51. ASSERT(!strcmp(ref.ptr->type, type))
  52. return ref.ptr;
  53. }
  54. static struct NCDModuleIndex_base_type * find_base_type (NCDModuleIndex *o, const char *base_type)
  55. {
  56. BAVLNode *node = BAVL_LookupExact(&o->base_types_tree, &base_type);
  57. if (!node) {
  58. return NULL;
  59. }
  60. struct NCDModuleIndex_base_type *bt = UPPER_OBJECT(node, struct NCDModuleIndex_base_type, base_types_tree_node);
  61. ASSERT(!strcmp(bt->base_type, base_type))
  62. return bt;
  63. }
  64. int NCDModuleIndex_Init (NCDModuleIndex *o)
  65. {
  66. // allocate modules array
  67. if (!(o->modules = BAllocArray(NCDMODULEINDEX_MAX_MODULES, sizeof(o->modules[0])))) {
  68. BLog(BLOG_ERROR, "BAllocArray failed");
  69. goto fail0;
  70. }
  71. // set zero modules
  72. o->num_modules = 0;
  73. // init modules hash
  74. if (!NCDModuleIndex__MHash_Init(&o->modules_hash, NCDMODULEINDEX_MODULES_HASH_SIZE)) {
  75. BLog(BLOG_ERROR, "NCDModuleIndex__MHash_Init failed");
  76. goto fail1;
  77. }
  78. // init base types tree
  79. BAVL_Init(&o->base_types_tree, OFFSET_DIFF(struct NCDModuleIndex_base_type, base_type, base_types_tree_node), (BAVL_comparator)string_pointer_comparator, NULL);
  80. DebugObject_Init(&o->d_obj);
  81. return 1;
  82. fail1:
  83. BFree(o->modules);
  84. fail0:
  85. return 0;
  86. }
  87. void NCDModuleIndex_Free (NCDModuleIndex *o)
  88. {
  89. DebugObject_Free(&o->d_obj);
  90. // free base types
  91. while (!BAVL_IsEmpty(&o->base_types_tree)) {
  92. struct NCDModuleIndex_base_type *bt = UPPER_OBJECT(BAVL_GetFirst(&o->base_types_tree), struct NCDModuleIndex_base_type, base_types_tree_node);
  93. BAVL_Remove(&o->base_types_tree, &bt->base_types_tree_node);
  94. free(bt);
  95. }
  96. // free modules hash
  97. NCDModuleIndex__MHash_Free(&o->modules_hash);
  98. // free modules array
  99. BFree(o->modules);
  100. }
  101. int NCDModuleIndex_AddGroup (NCDModuleIndex *o, const struct NCDModuleGroup *group)
  102. {
  103. DebugObject_Access(&o->d_obj);
  104. for (const struct NCDModule *nm = group->modules; nm->type; nm++) {
  105. if (find_module(o, nm->type)) {
  106. BLog(BLOG_ERROR, "module type '%s' already exists", nm->type);
  107. goto loop_fail0;
  108. }
  109. if (strlen(nm->type) > NCDMODULEINDEX_MAX_TYPE_LEN) {
  110. BLog(BLOG_ERROR, "module type '%s' is too long (dump NCDMODULEINDEX_MAX_TYPE_LEN)", nm->type);
  111. goto loop_fail0;
  112. }
  113. if (o->num_modules == NCDMODULEINDEX_MAX_MODULES) {
  114. BLog(BLOG_ERROR, "too many modules (bump NCDMODULEINDEX_MAX_MODULES)");
  115. goto loop_fail0;
  116. }
  117. struct NCDModuleIndex_module *m = &o->modules[o->num_modules];
  118. NCDModuleIndex__MHashRef ref = NCDModuleIndex__MHash_Deref(o->modules, o->num_modules);
  119. strcpy(m->type, nm->type);
  120. m->module = nm;
  121. int res = NCDModuleIndex__MHash_Insert(&o->modules_hash, o->modules, ref, NULL);
  122. ASSERT(res)
  123. const char *base_type = (nm->base_type ? nm->base_type : nm->type);
  124. struct NCDModuleIndex_base_type *bt = find_base_type(o, base_type);
  125. if (bt) {
  126. if (bt->group != group) {
  127. BLog(BLOG_ERROR, "module base type '%s' already exists in another module group", base_type);
  128. goto loop_fail1;
  129. }
  130. } else {
  131. if (!(bt = malloc(sizeof(*bt)))) {
  132. BLog(BLOG_ERROR, "malloc failed");
  133. goto loop_fail1;
  134. }
  135. bt->base_type = base_type;
  136. bt->group = group;
  137. ASSERT_EXECUTE(BAVL_Insert(&o->base_types_tree, &bt->base_types_tree_node, NULL))
  138. }
  139. o->num_modules++;
  140. continue;
  141. loop_fail1:
  142. NCDModuleIndex__MHash_Remove(&o->modules_hash, o->modules, ref);
  143. loop_fail0:
  144. return 0;
  145. }
  146. return 1;
  147. }
  148. const struct NCDModule * NCDModuleIndex_FindModule (NCDModuleIndex *o, const char *type)
  149. {
  150. DebugObject_Access(&o->d_obj);
  151. ASSERT(type)
  152. struct NCDModuleIndex_module *m = find_module(o, type);
  153. if (!m) {
  154. return NULL;
  155. }
  156. return m->module;
  157. }