shm implemented as memfd syscall
zhangmeng
2023-07-27 157b3411dd123694ca29dd80fe9ecc683958ccab
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#ifndef __HASHTABLE_CWC22_H__
#define __HASHTABLE_CWC22_H__
 
#include <unistd.h>
 
struct shmht;
 
/*! \mainpage lib_shmht
 *
 * \section Introduction
 *
 * The lib_shmht is a full-functionall hashtable implementation in shared memory.<BR>
 * This library is designed for high-performace caches, but it can be used for
 * many pourpouses. <BR>
 * Design principles of the lib_shmht: <BR>
 * <b>no-reallocation</b> of the shared memory. So, the HT can't grow in size from it's creation, but you
 * can define the % of older entries that can be erased when a element has not enought
 * space.<BR>
 * <b>concurrency</b>: The accesses are controlled by a semaphore R/W lock implemntation, so not concurrency
 * problem is possible.<BR>
 * <b>performance</b>: The performance is the main target of this implementation.<BR>
 *
 * It has the next <b>limitations</b>: <BR>
 *  * Key size limited by compiling time.<BR>
 *  * The erased elements are the oldests, not the less used. This is to don't write (and lock the entire ht)
 * in all the reads.<BR>
 *
 * Please, check the design file if you want more specifical data.
 * 
 */
 
 
/*!   
 * @name                    shmht_hashtable
 * @param   name            Name of the HashTable.
 * @param   number          Number of records.
 * @param   size            Size of each record.
 * @param   hashfunction    function for hashing keys.
 * @param   key_eq_fn       function for determining key equality
 * @return                  newly created hashtable or NULL on failure
 *
 * This function, takes the existing shared memory, or creates if it does not
 * exists.
 * The name is the primary key of the shared memory hash table. If it exists, 
 * the create_hashtable will asociate with the apropiate shared memory.
 *
 */
 
struct shmht *create_shmht(char *name,
                unsigned int number,
                size_t size,
                unsigned int (*hashfunction) (void *),
                int (*key_eq_fn) (void *, void *));
 
/*!   
 * @name        shmht_insert
 * @param   h   the hashtable to insert into
 * @param   k   the key - hashtable claims ownership and will free on removal
 * @param   v   the value - does not claim ownership
 * @return      > zero for successful insertion, else for error.
 *
 * This function does not check for repeated insertions with a duplicate key.
 * The value returned when using a duplicate key is undefined.
 * If in doubt, remove before insert.
 * The size of this hashtable is fixed, so if the hashtable is full, the insert
 * will fail.
 */
 
int
shmht_insert (struct shmht *h, void *k, size_t key_size, void *v,
                  size_t value_size);
 
 
/*!   
 * @name        shmht_search
 * @param   h   the hashtable to search
 * @param   k   the key to search for  - does not claim ownership
 * @param key_size Size of the key.
 * @param returned_size [out], the size of the returned value.
 * @return      the value associated with the key, or NULL if none found
 * 
 * You should be careful, beacause, this function returns a pointer to the 
 * shared memory area. DO NOT FREE THIS POINTER!
 */
 
void *shmht_search (struct shmht *h, void *k, size_t key_size,
                        size_t * returned_size);
 
 
/*!   
 * @name        shmht_remove
 * @param   h   the hashtable to remove the item from
 * @param   k   the key to search for  - does not claim ownership
 * @param returned_size  [out] returns the size of the returned void *.
 * @return      the number of items removed.
 */
 
int shmht_remove (struct shmht *h, void *k, size_t key_size);
 
 
 
/*!   
 * @name        shmht_count
 * @param   h   the hashtable
 * @return      the number of items stored in the hashtable, -1 if something
 *              nasty happens.
 */
int shmht_count (struct shmht *h);
 
 
/*!   
 * @name        shmht_flush
 * @param   h   the hashtable
 * @return      0 if not problem, <0 if error.
 */
 
int shmht_flush (struct shmht *h);
 
/*!
 * @name        shmht_remove_older_entries
 * @param   h   the hashtable
 * @param   p   the % of older values to erase
 * @return      The number of deleted entries.
 */
 
int shmht_remove_older_entries (struct shmht *h, int p);
 
/*!   
 * @name        shmht_destroy
 * @param   h   the hashtable
 * 
 * Be careful with this operation, beacause all the processes that are currently
 * using the shared memory hash table will fail (it deletes the shared memory and
 * the semaphores used as mutex). 
 * If you have any doubt, use the hashtable_flush, instead of this function.
 */
 
int shmht_destroy (struct shmht *h);
 
#endif /* __HASHTABLE_CWC22_H__ */