NCDInterpBlock.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 <misc/balloc.h>
  33. #include <base/BLog.h>
  34. #include "NCDInterpBlock.h"
  35. #include <generated/blog_channel_ncd.h>
  36. static size_t djb2_hash (const unsigned char *str)
  37. {
  38. size_t hash = 5381;
  39. int c;
  40. while (c = *str++) {
  41. hash = ((hash << 5) + hash) + c;
  42. }
  43. return hash;
  44. }
  45. #include "NCDInterpBlock_hash.h"
  46. #include <structure/CHash_impl.h>
  47. int NCDInterpBlock_Init (NCDInterpBlock *o, NCDBlock *block)
  48. {
  49. if (NCDBlock_NumStatements(block) > INT_MAX) {
  50. BLog(BLOG_ERROR, "too many statements");
  51. goto fail0;
  52. }
  53. int num_stmts = NCDBlock_NumStatements(block);
  54. if (!(o->stmts = BAllocArray(num_stmts, sizeof(o->stmts[0])))) {
  55. BLog(BLOG_ERROR, "BAllocArray failed");
  56. goto fail0;
  57. }
  58. if (!NCDInterpBlock__Hash_Init(&o->hash, num_stmts)) {
  59. BLog(BLOG_ERROR, "NCDInterpBlock__Hash_Init failed");
  60. goto fail1;
  61. }
  62. o->num_stmts = 0;
  63. for (NCDStatement *s = NCDBlock_FirstStatement(block); s; s = NCDBlock_NextStatement(block, s)) {
  64. struct NCDInterpBlock__stmt *e = &o->stmts[o->num_stmts];
  65. e->name = NCDStatement_Name(s);
  66. if (e->name) {
  67. NCDInterpBlock__HashRef ref = NCDInterpBlock__Hash_Deref(o->stmts, o->num_stmts);
  68. NCDInterpBlock__Hash_InsertMulti(&o->hash, o->stmts, ref);
  69. }
  70. o->num_stmts++;
  71. }
  72. ASSERT(o->num_stmts == num_stmts)
  73. DebugObject_Init(&o->d_obj);
  74. return 1;
  75. fail1:
  76. BFree(o->stmts);
  77. fail0:
  78. return 0;
  79. }
  80. void NCDInterpBlock_Free (NCDInterpBlock *o)
  81. {
  82. DebugObject_Free(&o->d_obj);
  83. NCDInterpBlock__Hash_Free(&o->hash);
  84. BFree(o->stmts);
  85. }
  86. int NCDInterpBlock_FindStatement (NCDInterpBlock *o, int from_index, const char *name)
  87. {
  88. DebugObject_Access(&o->d_obj);
  89. ASSERT(from_index >= 0)
  90. ASSERT(from_index <= o->num_stmts)
  91. ASSERT(name)
  92. // We rely on that we get matching statements here in reverse order of insertion,
  93. // to properly return the greatest matching statement lesser than from_index.
  94. NCDInterpBlock__HashRef ref = NCDInterpBlock__Hash_Lookup(&o->hash, o->stmts, name);
  95. while (ref.link != NCDInterpBlock__HashNullLink()) {
  96. ASSERT(!strcmp(ref.ptr->name, name))
  97. if (ref.link < from_index) {
  98. return ref.link;
  99. }
  100. ref = NCDInterpBlock__Hash_GetNextEqual(&o->hash, o->stmts, ref);
  101. }
  102. return -1;
  103. }