-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshmbuffer.c
executable file
·130 lines (98 loc) · 2.85 KB
/
shmbuffer.c
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
/**
* Copyright (C) 2014 bitpick
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
**/
#include <sys/stat.h>
#include <fcntl.h>
#include "shmbuffer.h"
int shm_ringbuff_get_content_size(struct shmringbuff *buff)
{
int content = 0;
// no data in buffer
if(buff->buff_rpos == buff->buff_wpos)
return content;
// write pointer flipped
if(buff->buff_wpos < buff->buff_rpos)
{
content = buff->buff_end - buff->buff_rpos;
content += buff->buff_wpos - buff->buff_begin;
}
else
{
content = buff->buff_wpos - buff->buff_rpos;
}
return content;
}
int shm_ringbuff_get_data_address(struct shmringbuff *buff)
{
if(shmringbuff == NULL)
return -1;
return shmringbuff + sizeof(struct shmringbuff);
}
int shm_ringbuff_init(struct shmringbuff **buff, size_t size, char *mapfile, int prot, int flags)
{
int fd = 0;
fd = shm_open(mapfile, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR);
ftruncate(fd, sizeof(struct shmringbuff) + SHM_SIZE);
*buff = mmap(NULL, sizeof(struct shmringbuff) + SHM_SIZE,
PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
close(fd);
if(*buff == MAP_FAILED)
return -1;
*buff->seqnum = 0;
*buff->buffsize = SHM_SIZE;
*buff->size = sizeof(struct shmringbuff) + SHM_SIZE;
*buff->buff_begin = *buff + sizeof(struct shmringbuff);
*buff->buff_end = *buff->buff_begin + SHM_SIZE;
*buff->buff_rpos = *buff->buff_begin;
*buff->buff_wpos = *buff->buff_begin;
sem_init(*buff->sem, 1, 1);
return 0;
}
int shm_ringbuff_destroy(struct shmringbuff **buff)
{
if(*buff != NULL)
{
sem_wait(*buff->sem);
sem_destroy(*buff->sem);
return munmap(*buff, sizeof(struct shmringbuff) + SHM_SIZE);
}
return -1;
}
int shm_ringbuff_connect_ro(char *mapfile, struct shmringbuff **buff)
{
int fd;
struct stat st;
fd = shm_open(mapfile, O_RDONLY, 0);
fstat(fd, &st);
*buff = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
close(fd);
return 0;
}
int shm_ringbuff_connect_rw(char *mapfile, struct shmringbuff **buff)
{
int fd;
struct stat st;
fd = shm_open(mapfile, O_RDWR, 0);
fstat(fd, &st);
*buff = mmap(NULL, st.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
close(fd);
return 0;
}
int shm_ringbuff_read(struct shmringbuff *buff, char *readbuff, size_t readbuff_size)
{
return ringbuffer_read(readbuff, buff->buffer, readbuff_size);
}
int shm_ringbuff_write(struct shmringbuff`*buff, char *writebuff, size_t writebuff_size)
{
return ringbuffer_write(buff->buffer, writebuff, writebuff_size);
}