NCDModuleIndex.c 6.3 KB

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