1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2 /* Copyright (c) 2021 Facebook */
9 #include "libbpf_internal.h"
16 size_t strs_data_max_len;
18 /* lookup index for each unique string in strings set */
19 struct hashmap *strs_hash;
22 static size_t strset_hash_fn(const void *key, void *ctx)
24 const struct strset *s = ctx;
25 const char *str = s->strs_data + (long)key;
30 static bool strset_equal_fn(const void *key1, const void *key2, void *ctx)
32 const struct strset *s = ctx;
33 const char *str1 = s->strs_data + (long)key1;
34 const char *str2 = s->strs_data + (long)key2;
36 return strcmp(str1, str2) == 0;
39 struct strset *strset__new(size_t max_data_sz, const char *init_data, size_t init_data_sz)
41 struct strset *set = calloc(1, sizeof(*set));
46 return ERR_PTR(-ENOMEM);
48 hash = hashmap__new(strset_hash_fn, strset_equal_fn, set);
52 set->strs_data_max_len = max_data_sz;
53 set->strs_hash = hash;
58 set->strs_data = malloc(init_data_sz);
62 memcpy(set->strs_data, init_data, init_data_sz);
63 set->strs_data_len = init_data_sz;
64 set->strs_data_cap = init_data_sz;
66 for (off = 0; off < set->strs_data_len; off += strlen(set->strs_data + off) + 1) {
67 /* hashmap__add() returns EEXIST if string with the same
68 * content already is in the hash map
70 err = hashmap__add(hash, (void *)off, (void *)off);
72 continue; /* duplicate */
84 void strset__free(struct strset *set)
86 if (IS_ERR_OR_NULL(set))
89 hashmap__free(set->strs_hash);
93 size_t strset__data_size(const struct strset *set)
95 return set->strs_data_len;
98 const char *strset__data(const struct strset *set)
100 return set->strs_data;
103 static void *strset_add_str_mem(struct strset *set, size_t add_sz)
105 return libbpf_add_mem(&set->strs_data, &set->strs_data_cap, 1,
106 set->strs_data_len, set->strs_data_max_len, add_sz);
109 /* Find string offset that corresponds to a given string *s*.
111 * - >0 offset into string data, if string is found;
112 * - -ENOENT, if string is not in the string data;
113 * - <0, on any other error.
115 int strset__find_str(struct strset *set, const char *s)
117 long old_off, new_off, len;
120 /* see strset__add_str() for why we do this */
122 p = strset_add_str_mem(set, len);
126 new_off = set->strs_data_len;
129 if (hashmap__find(set->strs_hash, (void *)new_off, (void **)&old_off))
135 /* Add a string s to the string data. If the string already exists, return its
136 * offset within string data.
138 * - > 0 offset into string data, on success;
141 int strset__add_str(struct strset *set, const char *s)
143 long old_off, new_off, len;
147 /* Hashmap keys are always offsets within set->strs_data, so to even
148 * look up some string from the "outside", we need to first append it
149 * at the end, so that it can be addressed with an offset. Luckily,
150 * until set->strs_data_len is incremented, that string is just a piece
151 * of garbage for the rest of the code, so no harm, no foul. On the
152 * other hand, if the string is unique, it's already appended and
153 * ready to be used, only a simple set->strs_data_len increment away.
156 p = strset_add_str_mem(set, len);
160 new_off = set->strs_data_len;
163 /* Now attempt to add the string, but only if the string with the same
164 * contents doesn't exist already (HASHMAP_ADD strategy). If such
165 * string exists, we'll get its offset in old_off (that's old_key).
167 err = hashmap__insert(set->strs_hash, (void *)new_off, (void *)new_off,
168 HASHMAP_ADD, (const void **)&old_off, NULL);
170 return old_off; /* duplicated string, return existing offset */
174 set->strs_data_len += len; /* new unique string, adjust data length */