Rizin
unix-like reverse engineering framework and cli tools
ht_inc.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2016-2018 crowell
2 // SPDX-FileCopyrightText: 2016-2018 pancake <pancake@nopcode.org>
3 // SPDX-FileCopyrightText: 2016-2018 ret2libc <sirmy15@gmail.com>
4 // SPDX-License-Identifier: BSD-3-Clause
5 
6 #ifndef HT_TYPE
7 #error HT_TYPE should be defined before including this header
8 #endif
9 
10 #undef HtName_
11 #undef Ht_
12 #undef HT_
13 #undef KEY_TYPE
14 #undef VALUE_TYPE
15 #undef KEY_TO_HASH
16 #undef HT_NULL_VALUE
17 
18 #if HT_TYPE == 1
19 #define HtName_(name) name##PP
20 #define Ht_(name) ht_pp_##name
21 #define HT_(name) HtPP##name
22 #define KEY_TYPE void *
23 #define VALUE_TYPE void *
24 #define KEY_TO_HASH(x) ((ut32)(uintptr_t)(x))
25 #define HT_NULL_VALUE NULL
26 #elif HT_TYPE == 2
27 #define HtName_(name) name##UP
28 #define Ht_(name) ht_up_##name
29 #define HT_(name) HtUP##name
30 #define KEY_TYPE ut64
31 #define VALUE_TYPE void *
32 #define KEY_TO_HASH(x) ((ut32)(x))
33 #define HT_NULL_VALUE 0
34 #elif HT_TYPE == 3
35 #define HtName_(name) name##UU
36 #define Ht_(name) ht_uu_##name
37 #define HT_(name) HtUU##name
38 #define KEY_TYPE ut64
39 #define VALUE_TYPE ut64
40 #define KEY_TO_HASH(x) ((ut32)(x))
41 #define HT_NULL_VALUE 0
42 #else
43 #define HtName_(name) name##PU
44 #define Ht_(name) ht_pu_##name
45 #define HT_(name) HtPU##name
46 #define KEY_TYPE void *
47 #define VALUE_TYPE ut64
48 #define KEY_TO_HASH(x) ((ut32)(uintptr_t)(x))
49 #define HT_NULL_VALUE 0
50 #endif
51 
52 #include "ls.h"
53 #include <rz_types.h>
54 
55 /* Kv represents a single key/value element in the hashtable */
56 typedef struct Ht_(kv) {
57  KEY_TYPE key;
59  ut32 key_len;
60  ut32 value_len;
61 }
62 HT_(Kv);
63 
64 typedef void (*HT_(KvFreeFunc))(HT_(Kv) *);
65 typedef KEY_TYPE (*HT_(DupKey))(const KEY_TYPE);
66 typedef VALUE_TYPE (*HT_(DupValue))(const VALUE_TYPE);
67 typedef ut32 (*HT_(CalcSizeK))(const KEY_TYPE);
68 typedef ut32 (*HT_(CalcSizeV))(const VALUE_TYPE);
69 typedef ut32 (*HT_(HashFunction))(const KEY_TYPE);
70 typedef int (*HT_(ListComparator))(const KEY_TYPE, const KEY_TYPE);
71 typedef bool (*HT_(ForeachCallback))(void *user, const KEY_TYPE, const VALUE_TYPE);
72 
73 typedef struct Ht_(bucket_t) {
74  HT_(Kv) * arr;
75  ut32 count;
76 }
77 HT_(Bucket);
78 
79 /* Options contain all the settings of the hashtable */
80 typedef struct Ht_(options_t) {
81  HT_(ListComparator)
82  cmp; // Function for comparing values. Returns 0 if eq.
83  HT_(HashFunction)
84  hashfn; // Function for hashing items in the hash table.
85  HT_(DupKey)
86  dupkey; // Function for making a copy of key
87  HT_(DupValue)
88  dupvalue; // Function for making a copy of value
89  HT_(CalcSizeK)
90  calcsizeK; // Function to determine the key's size
91  HT_(CalcSizeV)
92  calcsizeV; // Function to determine the value's size
93  HT_(KvFreeFunc)
94  freefn; // Function to free the keyvalue store
95  size_t elem_size; // Size of each HtKv element (useful for subclassing like SdbKv)
96 }
97 HT_(Options);
98 
99 /* Ht is the hashtable structure */
100 typedef struct Ht_(t) {
101  ut32 size; // size of the hash table in buckets.
102  ut32 count; // number of stored elements.
103  HT_(Bucket) * table; // Actual table.
104  ut32 prime_idx;
105  HT_(Options)
106  opt;
107 }
108 HtName_(Ht);
109 
110 // Create a new Ht with the provided Options
111 RZ_API HtName_(Ht) * Ht_(new_opt)(HT_(Options) * opt);
112 // Destroy a hashtable and all of its entries.
113 RZ_API void Ht_(free)(HtName_(Ht) * ht);
114 // Insert a new Key-Value pair into the hashtable. If the key already exists, returns false.
115 RZ_API bool Ht_(insert)(HtName_(Ht) * ht, const KEY_TYPE key, VALUE_TYPE value);
116 // Insert a new Key-Value pair into the hashtable, or updates the value if the key already exists.
117 RZ_API bool Ht_(update)(HtName_(Ht) * ht, const KEY_TYPE key, VALUE_TYPE value);
118 // Update the key of an element in the hashtable
119 RZ_API bool Ht_(update_key)(HtName_(Ht) * ht, const KEY_TYPE old_key, const KEY_TYPE new_key);
120 // Delete a key from the hashtable.
121 RZ_API bool Ht_(delete)(HtName_(Ht) * ht, const KEY_TYPE key);
122 // Find the value corresponding to the matching key.
123 RZ_API VALUE_TYPE Ht_(find)(HtName_(Ht) * ht, const KEY_TYPE key, bool *found);
124 // Iterates over all elements in the hashtable, calling the cb function on each Kv.
125 // If the cb returns false, the iteration is stopped.
126 // cb should not modify the hashtable.
127 // NOTE: cb can delete the current element, but it should be avoided
128 RZ_API void Ht_(foreach)(HtName_(Ht) * ht, HT_(ForeachCallback) cb, void *user);
129 
130 RZ_API HT_(Kv) * Ht_(find_kv)(HtName_(Ht) * ht, const KEY_TYPE key, bool *found);
131 RZ_API bool Ht_(insert_kv)(HtName_(Ht) * ht, HT_(Kv) * kv, bool update);
static RzILOpEffect * cmp(cs_insn *insn, bool is_thumb)
Definition: arm_il32.c:942
static int value
Definition: cmd_api.c:93
#define RZ_API
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void count
Definition: sflib.h:98
uint32_t ut32
static KEY_TYPE dupkey(HtName_(Ht) *ht, const KEY_TYPE k)
Definition: ht_inc.c:30
static void freefn(HtName_(Ht) *ht, HT_(Kv) *kv)
Definition: ht_inc.c:46
static ut32 hashfn(HtName_(Ht) *ht, const KEY_TYPE k)
Definition: ht_inc.c:22
#define HtName_(name)
Definition: ht_inc.h:43
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
RZ_API bool Ht_() update(HtName_(Ht) *ht, const KEY_TYPE key, VALUE_TYPE value)
Definition: ht_inc.c:252
#define HT_(name)
Definition: ht_inc.h:45
RZ_API const KEY_TYPE bool * found
Definition: ht_inc.h:130
RZ_API VALUE_TYPE Ht_() find(HtName_(Ht) *ht, const KEY_TYPE key, bool *found)
Definition: ht_inc.c:330
RZ_API const KEY_TYPE key
Definition: ht_inc.h:130
RZ_API bool Ht_() insert(HtName_(Ht) *ht, const KEY_TYPE key, VALUE_TYPE value)
Definition: ht_inc.c:246
RZ_API bool Ht_() insert_kv(HtName_(Ht) *ht, HT_(Kv) *kv, bool update)
Definition: ht_inc.c:218
RZ_API bool Ht_() update_key(HtName_(Ht) *ht, const KEY_TYPE old_key, const KEY_TYPE new_key)
Definition: ht_inc.c:257
#define VALUE_TYPE
Definition: ht_inc.h:47
#define KEY_TYPE
Definition: ht_inc.h:46
#define Ht_(name)
Definition: ht_inc.h:44
voidpf void uLong size
Definition: ioapi.h:138
static int
Definition: sfsocketcall.h:114
#define bool
Definition: sysdefs.h:146
static const char * cb[]
Definition: z80_tab.h:176