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
#ifndef __HASHTABLE_PRIVATE_CWC22_H__
#define __HASHTABLE_PRIVATE_CWC22_H__
 
#include "shmht.h"
#include <fcntl.h>           /* For O_* constants */
#include <sys/stat.h>        /* For mode constants */
#include <semaphore.h>
 
//Max size of a key = > By default 512 bytes.
#define MAX_KEY_SIZE 512
/*****************************************************************************/
 
struct entry
{
    //Marks if the entry is used or not.
    int used;
    //Store the key in a char array, later, transform to a void *, and the 
    //compare function will be who treat it as it is. 
    // BE CAREFULL with keys with longer sizes, the behaviour is not defined!!.
    char k[MAX_KEY_SIZE];
    //key_size
    size_t key_size;
    //Hash of the key.
    unsigned int h;
    //Offset of the next. (Must be in collisions)
    unsigned int next;
    //offset of the bucket where the entry is stored on.
    int bucket;
    //The size of the stored in the bucket. (This is to allow storing variable size
    //items, maximun, the size of the bucket). We only copy to the destiny, the
    //stored size, not all the bucket. [optimization]
    int bucket_stored_size;
    //Position where the entry is stored on. In the entries and in the colisions.
    int position;
    //Seconds from epoch. This is the creation time.
    //We use this value for deleting the older values, we use seconds, beacause
    //is an aproximate cleaning (designed for cache pourposes).
    long sec;
};
 
//Struct only with the flag of used / not.
struct bucket
{
    int used;
};
 
 
 
struct internal_hashtable
{
    unsigned int tablelength;
    unsigned int registry_max_size;
        char  * sem_name;     
        sem_t * sem;     
    unsigned int fd;
    unsigned int entrycount;
    unsigned int primeindex;
};
 
 
struct shmht
{
    //Those values are updated at creation time, so, they MUST be pointers.
    //Are process related values, so to each process return his pointers.
    void *internal_ht;
    void *entrypoint;
    void *collisionentries;
    void *bucketmarket;
 
    // Functions related to the data type stored.
    unsigned int (*hashfn) (void *k);
    int (*eqfn) (void *k1, void *k2);
 
};
 
/*****************************************************************************/
static unsigned int hash (struct shmht *h, void *k);
 
/*****************************************************************************/
/* indexFor */
static inline unsigned int
indexFor (unsigned int tablelength, unsigned int hashvalue)
{
    return (hashvalue % tablelength);
};
 
 
/*****************************************************************************/
 
#endif /* __HASHTABLE_PRIVATE_CWC22_H__ */