linux/tools/lib/bpf/strset.c
<<
>>
Prefs
   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
  12struct strset {
  13        void *strs_data;
  14        size_t strs_data_len;
  15        size_t strs_data_cap;
  16        size_t strs_data_max_len;
  17
  18        /* lookup index for each unique string in strings set */
  19        struct hashmap *strs_hash;
  20};
  21
  22static size_t strset_hash_fn(const void *key, void *ctx)
  23{
  24        const struct strset *s = ctx;
  25        const char *str = s->strs_data + (long)key;
  26
  27        return str_hash(str);
  28}
  29
  30static bool strset_equal_fn(const void *key1, const void *key2, void *ctx)
  31{
  32        const struct strset *s = ctx;
  33        const char *str1 = s->strs_data + (long)key1;
  34        const char *str2 = s->strs_data + (long)key2;
  35
  36        return strcmp(str1, str2) == 0;
  37}
  38
  39struct strset *strset__new(size_t max_data_sz, const char *init_data, size_t init_data_sz)
  40{
  41        struct strset *set = calloc(1, sizeof(*set));
  42        struct hashmap *hash;
  43        int err = -ENOMEM;
  44
  45        if (!set)
  46                return ERR_PTR(-ENOMEM);
  47
  48        hash = hashmap__new(strset_hash_fn, strset_equal_fn, set);
  49        if (IS_ERR(hash))
  50                goto err_out;
  51
  52        set->strs_data_max_len = max_data_sz;
  53        set->strs_hash = hash;
  54
  55        if (init_data) {
  56                long off;
  57
  58                set->strs_data = malloc(init_data_sz);
  59                if (!set->strs_data)
  60                        goto err_out;
  61
  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;
  65
  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
  69                         */
  70                        err = hashmap__add(hash, (void *)off, (void *)off);
  71                        if (err == -EEXIST)
  72                                continue; /* duplicate */
  73                        if (err)
  74                                goto err_out;
  75                }
  76        }
  77
  78        return set;
  79err_out:
  80        strset__free(set);
  81        return ERR_PTR(err);
  82}
  83
  84void strset__free(struct strset *set)
  85{
  86        if (IS_ERR_OR_NULL(set))
  87                return;
  88
  89        hashmap__free(set->strs_hash);
  90        free(set->strs_data);
  91}
  92
  93size_t strset__data_size(const struct strset *set)
  94{
  95        return set->strs_data_len;
  96}
  97
  98const char *strset__data(const struct strset *set)
  99{
 100        return set->strs_data;
 101}
 102
 103static void *strset_add_str_mem(struct strset *set, size_t add_sz)
 104{
 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);
 107}
 108
 109/* Find string offset that corresponds to a given string *s*.
 110 * Returns:
 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.
 114 */
 115int strset__find_str(struct strset *set, const char *s)
 116{
 117        long old_off, new_off, len;
 118        void *p;
 119
 120        /* see strset__add_str() for why we do this */
 121        len = strlen(s) + 1;
 122        p = strset_add_str_mem(set, len);
 123        if (!p)
 124                return -ENOMEM;
 125
 126        new_off = set->strs_data_len;
 127        memcpy(p, s, len);
 128
 129        if (hashmap__find(set->strs_hash, (void *)new_off, (void **)&old_off))
 130                return old_off;
 131
 132        return -ENOENT;
 133}
 134
 135/* Add a string s to the string data. If the string already exists, return its
 136 * offset within string data.
 137 * Returns:
 138 *   - > 0 offset into string data, on success;
 139 *   - < 0, on error.
 140 */
 141int strset__add_str(struct strset *set, const char *s)
 142{
 143        long old_off, new_off, len;
 144        void *p;
 145        int err;
 146
 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.
 154         */
 155        len = strlen(s) + 1;
 156        p = strset_add_str_mem(set, len);
 157        if (!p)
 158                return -ENOMEM;
 159
 160        new_off = set->strs_data_len;
 161        memcpy(p, s, len);
 162
 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).
 166         */
 167        err = hashmap__insert(set->strs_hash, (void *)new_off, (void *)new_off,
 168                              HASHMAP_ADD, (const void **)&old_off, NULL);
 169        if (err == -EEXIST)
 170                return old_off; /* duplicated string, return existing offset */
 171        if (err)
 172                return err;
 173
 174        set->strs_data_len += len; /* new unique string, adjust data length */
 175        return new_off;
 176}
 177