NCDInterpBlock.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 <misc/maxalign.h>
  37. #include <base/BLog.h>
  38. #include "NCDInterpBlock.h"
  39. #include <generated/blog_channel_ncd.h>
  40. #include "NCDInterpBlock_hash.h"
  41. #include <structure/CHash_impl.h>
  42. static int compute_prealloc (NCDInterpBlock *o)
  43. {
  44. int size = 0;
  45. for (int i = 0; i < o->num_stmts; i++) {
  46. int mod = size % BMAX_ALIGN;
  47. int align_size = (mod == 0 ? 0 : BMAX_ALIGN - mod);
  48. if (align_size + o->stmts[i].alloc_size > INT_MAX - size) {
  49. return 0;
  50. }
  51. o->stmts[i].prealloc_offset = size + align_size;
  52. size += align_size + o->stmts[i].alloc_size;
  53. }
  54. ASSERT(size >= 0)
  55. o->prealloc_size = size;
  56. return 1;
  57. }
  58. int NCDInterpBlock_Init (NCDInterpBlock *o, NCDBlock *block)
  59. {
  60. if (NCDBlock_NumStatements(block) > INT_MAX) {
  61. BLog(BLOG_ERROR, "too many statements");
  62. goto fail0;
  63. }
  64. int num_stmts = NCDBlock_NumStatements(block);
  65. if (!(o->stmts = BAllocArray(num_stmts, sizeof(o->stmts[0])))) {
  66. BLog(BLOG_ERROR, "BAllocArray failed");
  67. goto fail0;
  68. }
  69. if (!NCDInterpBlock__Hash_Init(&o->hash, num_stmts)) {
  70. BLog(BLOG_ERROR, "NCDInterpBlock__Hash_Init failed");
  71. goto fail1;
  72. }
  73. o->num_stmts = 0;
  74. o->prealloc_size = -1;
  75. for (NCDStatement *s = NCDBlock_FirstStatement(block); s; s = NCDBlock_NextStatement(block, s)) {
  76. ASSERT(NCDStatement_Type(s) == NCDSTATEMENT_REG)
  77. struct NCDInterpBlock__stmt *e = &o->stmts[o->num_stmts];
  78. e->name = NCDStatement_Name(s);
  79. e->cmdname = NCDStatement_RegCmdName(s);
  80. e->objnames = NULL;
  81. e->alloc_size = 0;
  82. if (!NCDInterpValue_Init(&e->ivalue, NCDStatement_RegArgs(s))) {
  83. BLog(BLOG_ERROR, "NCDInterpValue_Init failed");
  84. goto loop_fail0;
  85. }
  86. if (NCDStatement_RegObjName(s) && !(e->objnames = split_string(NCDStatement_RegObjName(s), '.'))) {
  87. BLog(BLOG_ERROR, "split_string failed");
  88. goto loop_fail1;
  89. }
  90. if (e->name) {
  91. NCDInterpBlock__HashRef ref = NCDInterpBlock__Hash_Deref(o->stmts, o->num_stmts);
  92. NCDInterpBlock__Hash_InsertMulti(&o->hash, o->stmts, ref);
  93. }
  94. o->num_stmts++;
  95. continue;
  96. loop_fail1:
  97. NCDInterpValue_Free(&e->ivalue);
  98. loop_fail0:
  99. goto fail2;
  100. }
  101. ASSERT(o->num_stmts == num_stmts)
  102. DebugObject_Init(&o->d_obj);
  103. return 1;
  104. fail2:
  105. while (o->num_stmts-- > 0) {
  106. struct NCDInterpBlock__stmt *e = &o->stmts[o->num_stmts];
  107. if (e->objnames) {
  108. free_strings(e->objnames);
  109. }
  110. NCDInterpValue_Free(&e->ivalue);
  111. }
  112. fail1:
  113. BFree(o->stmts);
  114. fail0:
  115. return 0;
  116. }
  117. void NCDInterpBlock_Free (NCDInterpBlock *o)
  118. {
  119. DebugObject_Free(&o->d_obj);
  120. while (o->num_stmts-- > 0) {
  121. struct NCDInterpBlock__stmt *e = &o->stmts[o->num_stmts];
  122. if (e->objnames) {
  123. free_strings(e->objnames);
  124. }
  125. NCDInterpValue_Free(&e->ivalue);
  126. }
  127. NCDInterpBlock__Hash_Free(&o->hash);
  128. BFree(o->stmts);
  129. }
  130. int NCDInterpBlock_FindStatement (NCDInterpBlock *o, int from_index, const char *name)
  131. {
  132. DebugObject_Access(&o->d_obj);
  133. ASSERT(from_index >= 0)
  134. ASSERT(from_index <= o->num_stmts)
  135. ASSERT(name)
  136. // We rely on that we get matching statements here in reverse order of insertion,
  137. // to properly return the greatest matching statement lesser than from_index.
  138. NCDInterpBlock__HashRef ref = NCDInterpBlock__Hash_Lookup(&o->hash, o->stmts, name);
  139. while (ref.link != NCDInterpBlock__HashNullLink()) {
  140. ASSERT(ref.link >= 0)
  141. ASSERT(ref.link < o->num_stmts)
  142. ASSERT(ref.ptr == &o->stmts[ref.link])
  143. ASSERT(!strcmp(ref.ptr->name, name))
  144. if (ref.link < from_index) {
  145. return ref.link;
  146. }
  147. ref = NCDInterpBlock__Hash_GetNextEqual(&o->hash, o->stmts, ref);
  148. }
  149. return -1;
  150. }
  151. const char * NCDInterpBlock_StatementCmdName (NCDInterpBlock *o, int i)
  152. {
  153. DebugObject_Access(&o->d_obj);
  154. ASSERT(i >= 0)
  155. ASSERT(i < o->num_stmts)
  156. return o->stmts[i].cmdname;
  157. }
  158. char ** NCDInterpBlock_StatementObjNames (NCDInterpBlock *o, int i)
  159. {
  160. DebugObject_Access(&o->d_obj);
  161. ASSERT(i >= 0)
  162. ASSERT(i < o->num_stmts)
  163. return o->stmts[i].objnames;
  164. }
  165. NCDInterpValue * NCDInterpBlock_StatementInterpValue (NCDInterpBlock *o, int i)
  166. {
  167. DebugObject_Access(&o->d_obj);
  168. ASSERT(i >= 0)
  169. ASSERT(i < o->num_stmts)
  170. return &o->stmts[i].ivalue;
  171. }
  172. void NCDInterpBlock_StatementBumpAllocSize (NCDInterpBlock *o, int i, int alloc_size)
  173. {
  174. DebugObject_Access(&o->d_obj);
  175. ASSERT(i >= 0)
  176. ASSERT(i < o->num_stmts)
  177. ASSERT(alloc_size >= 0)
  178. if (alloc_size > o->stmts[i].alloc_size) {
  179. o->stmts[i].alloc_size = alloc_size;
  180. o->prealloc_size = -1;
  181. }
  182. }
  183. int NCDInterpBlock_StatementPreallocSize (NCDInterpBlock *o, int i)
  184. {
  185. DebugObject_Access(&o->d_obj);
  186. ASSERT(i >= 0)
  187. ASSERT(i < o->num_stmts)
  188. return o->stmts[i].alloc_size;
  189. }
  190. int NCDInterpBlock_PreallocSize (NCDInterpBlock *o)
  191. {
  192. DebugObject_Access(&o->d_obj);
  193. ASSERT(o->prealloc_size == -1 || o->prealloc_size >= 0)
  194. if (o->prealloc_size < 0 && !compute_prealloc(o)) {
  195. return -1;
  196. }
  197. return o->prealloc_size;
  198. }
  199. int NCDInterpBlock_StatementPreallocOffset (NCDInterpBlock *o, int i)
  200. {
  201. DebugObject_Access(&o->d_obj);
  202. ASSERT(i >= 0)
  203. ASSERT(i < o->num_stmts)
  204. ASSERT(o->prealloc_size >= 0)
  205. return o->stmts[i].prealloc_offset;
  206. }