NCDPlaceholderDb.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**
  2. * @file NCDPlaceholderDb.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 <string.h>
  30. #include <limits.h>
  31. #include <stdlib.h>
  32. #include <misc/balloc.h>
  33. #include <misc/strdup.h>
  34. #include <base/BLog.h>
  35. #include <ncd/make_name_indices.h>
  36. #include "NCDPlaceholderDb.h"
  37. #include <generated/blog_channel_NCDPlaceholderDb.h>
  38. int NCDPlaceholderDb_Init (NCDPlaceholderDb *o, NCDStringIndex *string_index)
  39. {
  40. ASSERT(string_index)
  41. o->count = 0;
  42. o->capacity = 1;
  43. o->string_index = string_index;
  44. if (!(o->arr = BAllocArray(o->capacity, sizeof(o->arr[0])))) {
  45. BLog(BLOG_ERROR, "NCDPlaceholderDb_Init failed");
  46. return 0;
  47. }
  48. return 1;
  49. }
  50. void NCDPlaceholderDb_Free (NCDPlaceholderDb *o)
  51. {
  52. for (size_t i = 0; i < o->count; i++) {
  53. BFree(o->arr[i].varnames);
  54. }
  55. BFree(o->arr);
  56. }
  57. int NCDPlaceholderDb_AddVariable (NCDPlaceholderDb *o, const char *varname, int *out_plid)
  58. {
  59. ASSERT(varname)
  60. ASSERT(out_plid)
  61. ASSERT(o->count <= o->capacity)
  62. ASSERT(o->capacity > 0)
  63. if (o->count == o->capacity) {
  64. if (o->capacity > SIZE_MAX / 2) {
  65. BLog(BLOG_ERROR, "too many placeholder entries (cannot resize)");
  66. return 0;
  67. }
  68. size_t newcap = 2 * o->capacity;
  69. struct NCDPlaceholderDb__entry *newarr = BAllocArray(newcap, sizeof(newarr[0]));
  70. if (!newarr) {
  71. BLog(BLOG_ERROR, "BAllocArray failed");
  72. return 0;
  73. }
  74. memcpy(newarr, o->arr, o->count * sizeof(newarr[0]));
  75. BFree(o->arr);
  76. o->arr = newarr;
  77. o->capacity = newcap;
  78. }
  79. ASSERT(o->count < o->capacity)
  80. if (o->count > INT_MAX) {
  81. BLog(BLOG_ERROR, "too many placeholder entries (cannot fit integer)");
  82. return 0;
  83. }
  84. NCD_string_id_t *varnames;
  85. size_t num_names;
  86. if (!ncd_make_name_indices(o->string_index, varname, &varnames, &num_names)) {
  87. BLog(BLOG_ERROR, "ncd_make_name_indices failed");
  88. return 0;
  89. }
  90. *out_plid = o->count;
  91. o->arr[o->count].varnames = varnames;
  92. o->arr[o->count].num_names = num_names;
  93. o->count++;
  94. return 1;
  95. }
  96. void NCDPlaceholderDb_GetVariable (NCDPlaceholderDb *o, int plid, const NCD_string_id_t **out_varnames, size_t *out_num_names)
  97. {
  98. ASSERT(plid >= 0)
  99. ASSERT(plid < o->count)
  100. ASSERT(out_varnames)
  101. ASSERT(out_num_names)
  102. *out_varnames = o->arr[plid].varnames;
  103. *out_num_names = o->arr[plid].num_names;
  104. }