CNDP  22.08.0
hmap.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright (c) 2019-2022 Intel Corporation
3  */
4 
5 #ifndef __HMAP_H_
6 #define __HMAP_H_
7 
14 // IWYU pragma: no_include <bits/stdint-uintn.h>
15 
16 #include <stdbool.h> // for bool
17 #include <string.h> // for NULL
18 #include <strings.h> // for strcasecmp
19 #include <stdint.h> // for uint32_t, int64_t, uint16_t, uint64_t, uint8_t
20 #include <stdio.h> // for FILE
21 #include <sys/queue.h> // for TAILQ_ENTRY
22 #include <pthread.h> // for pthread_mutex_t
23 
24 #include "cne_common.h" // for CNDP_API
25 #include "cne_log.h" // for CNE_LOG_ERR, CNE_NULL_RET
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 #define HMAP_MAX_NAME_SIZE 32
32 #define HMAP_MAX_KEY_SIZE 256
33 #define HMAP_STARTING_CAPACITY 32
34 #define HMAP_DEFAULT_CAPACITY 1024
36 struct hmap;
37 
38 // clang-format off
39 typedef enum {
40  HMAP_EMPTY_TYPE,
41  HMAP_STR_TYPE,
42  HMAP_U64_TYPE,
43  HMAP_U32_TYPE,
44  HMAP_U16_TYPE,
45  HMAP_U8_TYPE,
46  HMAP_NUM_TYPE,
47  HMAP_NUM64_TYPE,
48  HMAP_BOOLEAN_TYPE,
49  HMAP_POINTER_TYPE,
50  HMAP_NUM_TYPES
51 } hmap_type_t;
52 // clang-format on
53 
54 typedef union {
55  char *str;
56  uint64_t u64;
57  uint32_t u32;
58  uint16_t u16;
59  uint8_t u8;
60  int num;
61  int64_t num64;
62  bool boolean;
63  void *ptr;
64 } hmap_val_t;
65 
69 typedef struct hmap_kvp {
70  hmap_type_t type;
71  char *prefix;
72  char *key;
73  hmap_val_t v;
75 
76 typedef uint32_t (*hash_fn_t)(const char *prefix, const char *key);
77 typedef int (*cmp_fn_t)(const char *prefix, const char *key, const hmap_kvp_t *kvp);
78 typedef void (*free_fn_t)(hmap_kvp_t *kvp);
79 
80 typedef struct hmap_funcs {
81  hash_fn_t hash_fn;
82  cmp_fn_t cmp_fn;
83  free_fn_t free_fn;
84 } hmap_funcs_t;
85 
89 typedef struct hmap {
90  TAILQ_ENTRY(hmap) next;
92  uint32_t capacity;
93  uint32_t max_capacity;
94  uint32_t curr_capacity;
95  pthread_mutex_t mutex;
96  hmap_funcs_t fns;
99 
112 CNDP_API hmap_t *hmap_create(const char *name, uint32_t capacity, hmap_funcs_t *funcs);
113 
122 CNDP_API int hmap_destroy(hmap_t *hmap);
123 
132 CNDP_API int hmap_destroy_by_name(const char *name);
133 
146 CNDP_API hmap_kvp_t *hmap_kvp_lookup(hmap_t *hmap, const char *prefix, const char *key);
147 
162 CNDP_API int hmap_lookup(hmap_t *hmap, const char *prefix, const char *key, hmap_val_t *val);
163 
180 CNDP_API int hmap_add(hmap_t *hmap, hmap_type_t type, const char *prefix, const char *key,
181  hmap_val_t *val);
182 
195 CNDP_API int hmap_kvp_update(hmap_t *hmap, hmap_kvp_t *kvp, hmap_val_t *val);
196 
211 static inline int
212 hmap_add_string(hmap_t *hmap, const char *prefix, const char *key, const char *val)
213 {
214  hmap_val_t v = {.str = (char *)(uintptr_t)val};
215 
216  return hmap_add(hmap, HMAP_STR_TYPE, prefix, key, &v);
217 }
218 
219 static inline int
220 hmap_add_u64(hmap_t *hmap, const char *prefix, const char *key, uint64_t val)
221 {
222  hmap_val_t v = {.u64 = val};
223 
224  return hmap_add(hmap, HMAP_U64_TYPE, prefix, key, &v);
225 }
226 
227 static inline int
228 hmap_add_u32(hmap_t *hmap, const char *prefix, const char *key, uint32_t val)
229 {
230  hmap_val_t v = {.u32 = val};
231 
232  return hmap_add(hmap, HMAP_U32_TYPE, prefix, key, &v);
233 }
234 
235 static inline int
236 hmap_add_u16(hmap_t *hmap, const char *prefix, const char *key, uint16_t val)
237 {
238  hmap_val_t v = {.u16 = val};
239 
240  return hmap_add(hmap, HMAP_U16_TYPE, prefix, key, &v);
241 }
242 
243 static inline int
244 hmap_add_u8(hmap_t *hmap, const char *prefix, const char *key, uint8_t val)
245 {
246  hmap_val_t v = {.u8 = val};
247 
248  return hmap_add(hmap, HMAP_U8_TYPE, prefix, key, &v);
249 }
250 
251 static inline int
252 hmap_add_num(hmap_t *hmap, const char *prefix, const char *key, int val)
253 {
254  hmap_val_t v = {.num = val};
255 
256  return hmap_add(hmap, HMAP_NUM_TYPE, prefix, key, &v);
257 }
258 
259 static inline int
260 hmap_add_num64(hmap_t *hmap, const char *prefix, const char *key, int64_t val)
261 {
262  hmap_val_t v = {.num64 = val};
263 
264  return hmap_add(hmap, HMAP_NUM64_TYPE, prefix, key, &v);
265 }
266 
267 static inline int
268 hmap_add_bool(hmap_t *hmap, const char *prefix, const char *key, bool val)
269 {
270  hmap_val_t v = {.boolean = val};
271 
272  return hmap_add(hmap, HMAP_BOOLEAN_TYPE, prefix, key, &v);
273 }
274 
275 static inline int
276 hmap_add_pointer(hmap_t *hmap, const char *prefix, const char *key, void *val)
277 {
278  hmap_val_t v = {.ptr = val};
279 
280  return hmap_add(hmap, HMAP_POINTER_TYPE, prefix, key, &v);
281 }
282 
295 CNDP_API int hmap_del(hmap_t *hmap, const char *prefix, const char *key);
296 
309 CNDP_API int hmap_iterate(hmap_t *hmap, hmap_kvp_t **_kvp, uint32_t *next);
310 
321 CNDP_API void hmap_dump(FILE *f, hmap_t *hmap, int sort);
322 
331 static inline uint32_t
333 {
334  return hmap->capacity;
335 }
336 
345 static inline uint32_t
347 {
348  return hmap->curr_capacity;
349 }
350 
359 CNDP_API hmap_funcs_t *hmap_get_funcs(hmap_t *hmap);
360 
371 CNDP_API int hmap_set_funcs(hmap_t *hmap, hmap_funcs_t *funcs);
372 
387 static inline hmap_kvp_t *
388 __get_kvp(hmap_t *hmap, const char *prefix, const char *key, hmap_type_t type)
389 {
390  hmap_kvp_t *kvp;
391 
392  if (!hmap)
393  CNE_NULL_RET("get failed - hmap not defined %s(%s)\n", prefix ? prefix : "", key);
394 
395  kvp = hmap_kvp_lookup(hmap, prefix, key);
396  if (!kvp || kvp->type != type)
397  CNE_NULL_RET("get failed for %s(%s)\n", prefix ? prefix : "", key);
398 
399  return kvp;
400 }
401 
416 static inline int
417 hmap_get_bool(hmap_t *hmap, const char *prefix, const char *key, bool *val)
418 {
419  hmap_kvp_t *kvp = __get_kvp(hmap, prefix, key, HMAP_BOOLEAN_TYPE);
420 
421  if (!kvp || kvp->type != HMAP_BOOLEAN_TYPE)
422  return -1;
423 
424  if (val)
425  *val = kvp->v.boolean;
426 
427  return 0;
428 }
429 
444 static inline int
445 hmap_get_u64(hmap_t *hmap, const char *prefix, const char *key, uint64_t *val)
446 {
447  hmap_kvp_t *kvp = __get_kvp(hmap, prefix, key, HMAP_U64_TYPE);
448 
449  if (!kvp || kvp->type != HMAP_U64_TYPE)
450  return -1;
451 
452  if (val)
453  *val = kvp->v.u64;
454 
455  return 0;
456 }
457 
472 static inline int
473 hmap_get_u32(hmap_t *hmap, const char *prefix, const char *key, uint32_t *val)
474 {
475  hmap_kvp_t *kvp = __get_kvp(hmap, prefix, key, HMAP_U32_TYPE);
476 
477  if (!kvp || kvp->type != HMAP_U32_TYPE)
478  return -1;
479 
480  if (val)
481  *val = kvp->v.u32;
482 
483  return 0;
484 }
485 
500 static inline int
501 hmap_get_u16(hmap_t *hmap, const char *prefix, const char *key, uint16_t *val)
502 {
503  hmap_kvp_t *kvp = __get_kvp(hmap, prefix, key, HMAP_U16_TYPE);
504 
505  if (!kvp || kvp->type != HMAP_U16_TYPE)
506  return -1;
507 
508  if (val)
509  *val = kvp->v.u16;
510 
511  return 0;
512 }
513 
528 static inline int
529 hmap_get_u8(hmap_t *hmap, const char *prefix, const char *key, uint8_t *val)
530 {
531  hmap_kvp_t *kvp = __get_kvp(hmap, prefix, key, HMAP_U8_TYPE);
532 
533  if (!kvp || kvp->type != HMAP_U8_TYPE)
534  return -1;
535 
536  if (val)
537  *val = kvp->v.u8;
538 
539  return 0;
540 }
541 
556 static inline int
557 hmap_get_num(hmap_t *hmap, const char *prefix, const char *key, int *val)
558 {
559  hmap_kvp_t *kvp = __get_kvp(hmap, prefix, key, HMAP_NUM_TYPE);
560 
561  if (!kvp || kvp->type != HMAP_NUM_TYPE)
562  return -1;
563 
564  if (val)
565  *val = kvp->v.num;
566 
567  return 0;
568 }
569 
584 static inline int
585 hmap_get_num64(hmap_t *hmap, const char *prefix, const char *key, int64_t *val)
586 {
587  hmap_kvp_t *kvp = __get_kvp(hmap, prefix, key, HMAP_NUM64_TYPE);
588 
589  if (!kvp || kvp->type != HMAP_NUM64_TYPE)
590  return -1;
591 
592  if (val)
593  *val = kvp->v.num64;
594 
595  return 0;
596 }
597 
612 static inline int
613 hmap_get_string(hmap_t *hmap, const char *prefix, const char *key, char **val)
614 {
615  hmap_kvp_t *kvp = __get_kvp(hmap, prefix, key, HMAP_STR_TYPE);
616 
617  if (!kvp || kvp->type != HMAP_STR_TYPE)
618  return -1;
619 
620  if (val)
621  *val = kvp->v.str;
622 
623  return 0;
624 }
625 
640 static inline int
641 hmap_get_pointer(hmap_t *hmap, const char *prefix, const char *key, void **val)
642 {
643  hmap_kvp_t *kvp = __get_kvp(hmap, prefix, key, HMAP_POINTER_TYPE);
644 
645  if (!kvp || kvp->type != HMAP_POINTER_TYPE)
646  return -1;
647 
648  if (val)
649  *val = kvp->v.ptr;
650 
651  return 0;
652 }
653 
662 void hmap_list_dump(FILE *f, int sort);
663 
664 #ifdef __cplusplus
665 }
666 #endif
667 
668 #endif /* __HMAP_H_*/
#define CNE_NULL_RET(...)
Definition: cne_log.h:262
static int hmap_get_bool(hmap_t *hmap, const char *prefix, const char *key, bool *val)
Definition: hmap.h:417
CNDP_API void hmap_dump(FILE *f, hmap_t *hmap, int sort)
CNDP_API int hmap_del(hmap_t *hmap, const char *prefix, const char *key)
void hmap_list_dump(FILE *f, int sort)
static int hmap_get_pointer(hmap_t *hmap, const char *prefix, const char *key, void **val)
Definition: hmap.h:641
CNDP_API hmap_funcs_t * hmap_get_funcs(hmap_t *hmap)
CNDP_API hmap_kvp_t * hmap_kvp_lookup(hmap_t *hmap, const char *prefix, const char *key)
struct hmap_kvp hmap_kvp_t
CNDP_API int hmap_destroy(hmap_t *hmap)
CNDP_API int hmap_add(hmap_t *hmap, hmap_type_t type, const char *prefix, const char *key, hmap_val_t *val)
CNDP_API int hmap_set_funcs(hmap_t *hmap, hmap_funcs_t *funcs)
static uint32_t hmap_count(hmap_t *hmap)
Definition: hmap.h:346
CNDP_API int hmap_destroy_by_name(const char *name)
static int hmap_get_string(hmap_t *hmap, const char *prefix, const char *key, char **val)
Definition: hmap.h:613
static int hmap_get_num64(hmap_t *hmap, const char *prefix, const char *key, int64_t *val)
Definition: hmap.h:585
#define HMAP_MAX_NAME_SIZE
Definition: hmap.h:31
static uint32_t hmap_capacity(hmap_t *hmap)
Definition: hmap.h:332
CNDP_API int hmap_iterate(hmap_t *hmap, hmap_kvp_t **_kvp, uint32_t *next)
CNDP_API int hmap_lookup(hmap_t *hmap, const char *prefix, const char *key, hmap_val_t *val)
static int hmap_get_u32(hmap_t *hmap, const char *prefix, const char *key, uint32_t *val)
Definition: hmap.h:473
static int hmap_add_string(hmap_t *hmap, const char *prefix, const char *key, const char *val)
Definition: hmap.h:212
static int hmap_get_u16(hmap_t *hmap, const char *prefix, const char *key, uint16_t *val)
Definition: hmap.h:501
static hmap_kvp_t * __get_kvp(hmap_t *hmap, const char *prefix, const char *key, hmap_type_t type)
Definition: hmap.h:388
struct hmap hmap_t
CNDP_API int hmap_kvp_update(hmap_t *hmap, hmap_kvp_t *kvp, hmap_val_t *val)
static int hmap_get_u64(hmap_t *hmap, const char *prefix, const char *key, uint64_t *val)
Definition: hmap.h:445
CNDP_API hmap_t * hmap_create(const char *name, uint32_t capacity, hmap_funcs_t *funcs)
static int hmap_get_u8(hmap_t *hmap, const char *prefix, const char *key, uint8_t *val)
Definition: hmap.h:529
static int hmap_get_num(hmap_t *hmap, const char *prefix, const char *key, int *val)
Definition: hmap.h:557
Definition: hmap.h:69
hmap_type_t type
Definition: hmap.h:70
char * key
Definition: hmap.h:72
hmap_val_t v
Definition: hmap.h:73
char * prefix
Definition: hmap.h:71
Definition: hmap.h:89
uint32_t capacity
Definition: hmap.h:92
TAILQ_ENTRY(hmap) next
pthread_mutex_t mutex
Definition: hmap.h:95
uint32_t curr_capacity
Definition: hmap.h:94
uint32_t max_capacity
Definition: hmap.h:93
hmap_kvp_t * map
Definition: hmap.h:97
char name[HMAP_MAX_NAME_SIZE]
Definition: hmap.h:91
hmap_funcs_t fns
Definition: hmap.h:96