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
#ifndef __LIST_IN_SHM_H__
#define __LIST_IN_SHM_M__
 
#ifdef __cplusplus 
extern "C" { 
#endif
 
#include <sys/types.h>
#include "stdint.h"
#include "errno.h"
#include <unistd.h>
#include <fcntl.h>           
#include <sys/stat.h>        
#include <semaphore.h>
#include "stdio.h"
#include <string.h>
 
 
typedef struct sm_list_s
{
  uint32_t n_off;
  uint32_t p_off;
  uint32_t c ;
//  uint32_t mc;
} sm_list_t;
 
 
typedef struct node_sm_s
{
    uint32_t p_off ;
    uint32_t n_off ;    
    uint32_t data;
}node_sm_t ;
 
typedef struct list_in_shm_handle_s {
   int fd;
   uint64_t size;
   sem_t * sem; 
   uint8_t * ptr ;
   sm_list_t *pFreeListStruct;
   sm_list_t *pList;
   uint8_t *pStart;
   
}list_in_shm_handle_t;
 
 
#define GET_OFFSET(baseptr,ptr) ((uint8_t *)ptr-(uint8_t *)baseptr)
#define GET_PTR(baseptr,offset) ((uint8_t *)baseptr+offset)
#define FROM_FREE_LIST 0x01
#define FROM_LIST      0x02
#define FROM_FRONT     0x10
#define FROM_BACK      0x20
 
int list_in_shm_init(list_in_shm_handle_t * h, char * name, uint32_t size,uint32_t num,char *nameSem,uint8_t init );
int list_in_shm_insert_node(sm_list_t * plst, uint8_t * basePtr , node_sm_t * node ,uint8_t front);
node_sm_t *list_in_shm_get_node(sm_list_t * plst,uint8_t * basePtr,uint8_t front);
node_sm_t * get_node_from_shm(list_in_shm_handle_t * h,uint8_t flags );
int put_node_in_list(list_in_shm_handle_t * h,node_sm_t *node,uint8_t flags);
void list_in_shm_finish(list_in_shm_handle_t * h);
 
#ifdef __cplusplus 
#endif
 
#endif