NCDInterpBlock.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /**
  2. * @file NCDInterpBlock.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 <stdint.h>
  30. #include <limits.h>
  31. #include <string.h>
  32. #include <stdlib.h>
  33. #include <misc/balloc.h>
  34. #include <misc/split_string.h>
  35. #include <base/BLog.h>
  36. #include "NCDInterpBlock.h"
  37. #include <generated/blog_channel_ncd.h>
  38. static size_t djb2_hash (const unsigned char *str)
  39. {
  40. size_t hash = 5381;
  41. int c;
  42. while (c = *str++) {
  43. hash = ((hash << 5) + hash) + c;
  44. }
  45. return hash;
  46. }
  47. #include "NCDInterpBlock_hash.h"
  48. #include <structure/CHash_impl.h>
  49. int NCDInterpBlock_Init (NCDInterpBlock *o, NCDBlock *block)
  50. {
  51. if (NCDBlock_NumStatements(block) > INT_MAX) {
  52. BLog(BLOG_ERROR, "too many statements");
  53. goto fail0;
  54. }
  55. int num_stmts = NCDBlock_NumStatements(block);
  56. if (!(o->stmts = BAllocArray(num_stmts, sizeof(o->stmts[0])))) {
  57. BLog(BLOG_ERROR, "BAllocArray failed");
  58. goto fail0;
  59. }
  60. if (!NCDInterpBlock__Hash_Init(&o->hash, num_stmts)) {
  61. BLog(BLOG_ERROR, "NCDInterpBlock__Hash_Init failed");
  62. goto fail1;
  63. }
  64. o->num_stmts = 0;
  65. for (NCDStatement *s = NCDBlock_FirstStatement(block); s; s = NCDBlock_NextStatement(block, s)) {
  66. ASSERT(NCDStatement_Type(s) == NCDSTATEMENT_REG)
  67. struct NCDInterpBlock__stmt *e = &o->stmts[o->num_stmts];
  68. e->name = NCDStatement_Name(s);
  69. e->cmdname = NCDStatement_RegCmdName(s);
  70. e->objnames = NULL;
  71. if (!NCDInterpValue_Init(&e->ivalue, NCDStatement_RegArgs(s))) {
  72. BLog(BLOG_ERROR, "NCDInterpValue_Init failed");
  73. goto loop_fail0;
  74. }
  75. if (NCDStatement_RegObjName(s) && !(e->objnames = split_string(NCDStatement_RegObjName(s), '.'))) {
  76. BLog(BLOG_ERROR, "split_string failed");
  77. goto loop_fail1;
  78. }
  79. if (e->name) {
  80. NCDInterpBlock__HashRef ref = NCDInterpBlock__Hash_Deref(o->stmts, o->num_stmts);
  81. NCDInterpBlock__Hash_InsertMulti(&o->hash, o->stmts, ref);
  82. }
  83. o->num_stmts++;
  84. continue;
  85. loop_fail1:
  86. NCDInterpValue_Free(&e->ivalue);
  87. loop_fail0:
  88. goto fail2;
  89. }
  90. ASSERT(o->num_stmts == num_stmts)
  91. DebugObject_Init(&o->d_obj);
  92. return 1;
  93. fail2:
  94. while (o->num_stmts-- > 0) {
  95. struct NCDInterpBlock__stmt *e = &o->stmts[o->num_stmts];
  96. if (e->objnames) {
  97. free_strings(e->objnames);
  98. }
  99. NCDInterpValue_Free(&e->ivalue);
  100. }
  101. fail1:
  102. BFree(o->stmts);
  103. fail0:
  104. return 0;
  105. }
  106. void NCDInterpBlock_Free (NCDInterpBlock *o)
  107. {
  108. DebugObject_Free(&o->d_obj);
  109. while (o->num_stmts-- > 0) {
  110. struct NCDInterpBlock__stmt *e = &o->stmts[o->num_stmts];
  111. if (e->objnames) {
  112. free_strings(e->objnames);
  113. }
  114. NCDInterpValue_Free(&e->ivalue);
  115. }
  116. NCDInterpBlock__Hash_Free(&o->hash);
  117. BFree(o->stmts);
  118. }
  119. int NCDInterpBlock_FindStatement (NCDInterpBlock *o, int from_index, const char *name)
  120. {
  121. DebugObject_Access(&o->d_obj);
  122. ASSERT(from_index >= 0)
  123. ASSERT(from_index <= o->num_stmts)
  124. ASSERT(name)
  125. // We rely on that we get matching statements here in reverse order of insertion,
  126. // to properly return the greatest matching statement lesser than from_index.
  127. NCDInterpBlock__HashRef ref = NCDInterpBlock__Hash_Lookup(&o->hash, o->stmts, name);
  128. while (ref.link != NCDInterpBlock__HashNullLink()) {
  129. ASSERT(ref.link >= 0)
  130. ASSERT(ref.link < o->num_stmts)
  131. ASSERT(ref.ptr == &o->stmts[ref.link])
  132. ASSERT(!strcmp(ref.ptr->name, name))
  133. if (ref.link < from_index) {
  134. return ref.link;
  135. }
  136. ref = NCDInterpBlock__Hash_GetNextEqual(&o->hash, o->stmts, ref);
  137. }
  138. return -1;
  139. }
  140. const char * NCDInterpBlock_StatementCmdName (NCDInterpBlock *o, int i)
  141. {
  142. DebugObject_Access(&o->d_obj);
  143. ASSERT(i >= 0)
  144. ASSERT(i < o->num_stmts)
  145. return o->stmts[i].cmdname;
  146. }
  147. char ** NCDInterpBlock_StatementObjNames (NCDInterpBlock *o, int i)
  148. {
  149. DebugObject_Access(&o->d_obj);
  150. ASSERT(i >= 0)
  151. ASSERT(i < o->num_stmts)
  152. return o->stmts[i].objnames;
  153. }
  154. NCDInterpValue * NCDInterpBlock_StatementInterpValue (NCDInterpBlock *o, int i)
  155. {
  156. DebugObject_Access(&o->d_obj);
  157. ASSERT(i >= 0)
  158. ASSERT(i < o->num_stmts)
  159. return &o->stmts[i].ivalue;
  160. }