strset.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
  2. /* Copyright (c) 2021 Facebook */
  3. #include <stdint.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <errno.h>
  7. #include <linux/err.h>
  8. #include "hashmap.h"
  9. #include "libbpf_internal.h"
  10. #include "strset.h"
  11. struct strset {
  12. void *strs_data;
  13. size_t strs_data_len;
  14. size_t strs_data_cap;
  15. size_t strs_data_max_len;
  16. /* lookup index for each unique string in strings set */
  17. struct hashmap *strs_hash;
  18. };
  19. static size_t strset_hash_fn(const void *key, void *ctx)
  20. {
  21. const struct strset *s = ctx;
  22. const char *str = s->strs_data + (long)key;
  23. return str_hash(str);
  24. }
  25. static bool strset_equal_fn(const void *key1, const void *key2, void *ctx)
  26. {
  27. const struct strset *s = ctx;
  28. const char *str1 = s->strs_data + (long)key1;
  29. const char *str2 = s->strs_data + (long)key2;
  30. return strcmp(str1, str2) == 0;
  31. }
  32. struct strset *strset__new(size_t max_data_sz, const char *init_data, size_t init_data_sz)
  33. {
  34. struct strset *set = calloc(1, sizeof(*set));
  35. struct hashmap *hash;
  36. int err = -ENOMEM;
  37. if (!set)
  38. return ERR_PTR(-ENOMEM);
  39. hash = hashmap__new(strset_hash_fn, strset_equal_fn, set);
  40. if (IS_ERR(hash))
  41. goto err_out;
  42. set->strs_data_max_len = max_data_sz;
  43. set->strs_hash = hash;
  44. if (init_data) {
  45. long off;
  46. set->strs_data = malloc(init_data_sz);
  47. if (!set->strs_data)
  48. goto err_out;
  49. memcpy(set->strs_data, init_data, init_data_sz);
  50. set->strs_data_len = init_data_sz;
  51. set->strs_data_cap = init_data_sz;
  52. for (off = 0; off < set->strs_data_len; off += strlen(set->strs_data + off) + 1) {
  53. /* hashmap__add() returns EEXIST if string with the same
  54. * content already is in the hash map
  55. */
  56. err = hashmap__add(hash, (void *)off, (void *)off);
  57. if (err == -EEXIST)
  58. continue; /* duplicate */
  59. if (err)
  60. goto err_out;
  61. }
  62. }
  63. return set;
  64. err_out:
  65. strset__free(set);
  66. return ERR_PTR(err);
  67. }
  68. void strset__free(struct strset *set)
  69. {
  70. if (IS_ERR_OR_NULL(set))
  71. return;
  72. hashmap__free(set->strs_hash);
  73. free(set->strs_data);
  74. free(set);
  75. }
  76. size_t strset__data_size(const struct strset *set)
  77. {
  78. return set->strs_data_len;
  79. }
  80. const char *strset__data(const struct strset *set)
  81. {
  82. return set->strs_data;
  83. }
  84. static void *strset_add_str_mem(struct strset *set, size_t add_sz)
  85. {
  86. return libbpf_add_mem(&set->strs_data, &set->strs_data_cap, 1,
  87. set->strs_data_len, set->strs_data_max_len, add_sz);
  88. }
  89. /* Find string offset that corresponds to a given string *s*.
  90. * Returns:
  91. * - >0 offset into string data, if string is found;
  92. * - -ENOENT, if string is not in the string data;
  93. * - <0, on any other error.
  94. */
  95. int strset__find_str(struct strset *set, const char *s)
  96. {
  97. long old_off, new_off, len;
  98. void *p;
  99. /* see strset__add_str() for why we do this */
  100. len = strlen(s) + 1;
  101. p = strset_add_str_mem(set, len);
  102. if (!p)
  103. return -ENOMEM;
  104. new_off = set->strs_data_len;
  105. memcpy(p, s, len);
  106. if (hashmap__find(set->strs_hash, (void *)new_off, (void **)&old_off))
  107. return old_off;
  108. return -ENOENT;
  109. }
  110. /* Add a string s to the string data. If the string already exists, return its
  111. * offset within string data.
  112. * Returns:
  113. * - > 0 offset into string data, on success;
  114. * - < 0, on error.
  115. */
  116. int strset__add_str(struct strset *set, const char *s)
  117. {
  118. long old_off, new_off, len;
  119. void *p;
  120. int err;
  121. /* Hashmap keys are always offsets within set->strs_data, so to even
  122. * look up some string from the "outside", we need to first append it
  123. * at the end, so that it can be addressed with an offset. Luckily,
  124. * until set->strs_data_len is incremented, that string is just a piece
  125. * of garbage for the rest of the code, so no harm, no foul. On the
  126. * other hand, if the string is unique, it's already appended and
  127. * ready to be used, only a simple set->strs_data_len increment away.
  128. */
  129. len = strlen(s) + 1;
  130. p = strset_add_str_mem(set, len);
  131. if (!p)
  132. return -ENOMEM;
  133. new_off = set->strs_data_len;
  134. memcpy(p, s, len);
  135. /* Now attempt to add the string, but only if the string with the same
  136. * contents doesn't exist already (HASHMAP_ADD strategy). If such
  137. * string exists, we'll get its offset in old_off (that's old_key).
  138. */
  139. err = hashmap__insert(set->strs_hash, (void *)new_off, (void *)new_off,
  140. HASHMAP_ADD, (const void **)&old_off, NULL);
  141. if (err == -EEXIST)
  142. return old_off; /* duplicated string, return existing offset */
  143. if (err)
  144. return err;
  145. set->strs_data_len += len; /* new unique string, adjust data length */
  146. return new_off;
  147. }