NCDInterpBlock.c 5.7 KB

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