NCDInterpProg.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /**
  2. * @file NCDInterpProg.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 <misc/balloc.h>
  32. #include <misc/hashfun.h>
  33. #include <base/BLog.h>
  34. #include "NCDInterpProg.h"
  35. #include <generated/blog_channel_ncd.h>
  36. #include "NCDInterpProg_hash.h"
  37. #include <structure/CHash_impl.h>
  38. int NCDInterpProg_Init (NCDInterpProg *o, NCDProgram *prog, NCDStringIndex *string_index, NCDPlaceholderDb *pdb, NCDModuleIndex *module_index, NCDMethodIndex *method_index)
  39. {
  40. ASSERT(prog)
  41. ASSERT(!NCDProgram_ContainsElemType(prog, NCDPROGRAMELEM_INCLUDE))
  42. ASSERT(!NCDProgram_ContainsElemType(prog, NCDPROGRAMELEM_INCLUDE_GUARD))
  43. ASSERT(string_index)
  44. ASSERT(pdb)
  45. ASSERT(module_index)
  46. ASSERT(method_index)
  47. if (NCDProgram_NumElems(prog) > INT_MAX) {
  48. BLog(BLOG_ERROR, "too many processes");
  49. goto fail0;
  50. }
  51. int num_procs = NCDProgram_NumElems(prog);
  52. if (!(o->procs = BAllocArray(num_procs, sizeof(o->procs[0])))) {
  53. BLog(BLOG_ERROR, "BAllocArray failed");
  54. goto fail0;
  55. }
  56. if (!NCDInterpProg__Hash_Init(&o->hash, num_procs)) {
  57. BLog(BLOG_ERROR, "NCDInterpProg__Hash_Init failed");
  58. goto fail1;
  59. }
  60. o->num_procs = 0;
  61. for (NCDProgramElem *elem = NCDProgram_FirstElem(prog); elem; elem = NCDProgram_NextElem(prog, elem)) {
  62. ASSERT(NCDProgramElem_Type(elem) == NCDPROGRAMELEM_PROCESS)
  63. NCDProcess *p = NCDProgramElem_Process(elem);
  64. struct NCDInterpProg__process *e = &o->procs[o->num_procs];
  65. e->name = NCDStringIndex_Get(string_index, NCDProcess_Name(p));
  66. if (e->name < 0) {
  67. BLog(BLOG_ERROR, "NCDStringIndex_Get failed");
  68. goto fail2;
  69. }
  70. if (!NCDInterpProcess_Init(&e->iprocess, p, string_index, pdb, module_index, method_index)) {
  71. BLog(BLOG_ERROR, "NCDInterpProcess_Init failed");
  72. goto fail2;
  73. }
  74. NCDInterpProg__HashRef ref = {e, o->num_procs};
  75. if (!NCDInterpProg__Hash_Insert(&o->hash, o->procs, ref, NULL)) {
  76. BLog(BLOG_ERROR, "duplicate process or template name: %s", NCDProcess_Name(p));
  77. NCDInterpProcess_Free(&e->iprocess);
  78. goto fail2;
  79. }
  80. o->num_procs++;
  81. }
  82. ASSERT(o->num_procs == num_procs)
  83. DebugObject_Init(&o->d_obj);
  84. return 1;
  85. fail2:
  86. while (o->num_procs-- > 0) {
  87. NCDInterpProcess_Free(&o->procs[o->num_procs].iprocess);
  88. }
  89. NCDInterpProg__Hash_Free(&o->hash);
  90. fail1:
  91. BFree(o->procs);
  92. fail0:
  93. return 0;
  94. }
  95. void NCDInterpProg_Free (NCDInterpProg *o)
  96. {
  97. DebugObject_Free(&o->d_obj);
  98. while (o->num_procs-- > 0) {
  99. NCDInterpProcess_Free(&o->procs[o->num_procs].iprocess);
  100. }
  101. NCDInterpProg__Hash_Free(&o->hash);
  102. BFree(o->procs);
  103. }
  104. NCDInterpProcess * NCDInterpProg_FindProcess (NCDInterpProg *o, NCD_string_id_t name)
  105. {
  106. DebugObject_Access(&o->d_obj);
  107. ASSERT(name >= 0)
  108. NCDInterpProg__HashRef ref = NCDInterpProg__Hash_Lookup(&o->hash, o->procs, name);
  109. if (ref.link == NCDInterpProg__HashNullLink()) {
  110. return NULL;
  111. }
  112. ASSERT(ref.ptr->name == name)
  113. return &ref.ptr->iprocess;
  114. }