-
Notifications
You must be signed in to change notification settings - Fork 42
/
cbuffer.c
131 lines (107 loc) · 2.69 KB
/
cbuffer.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
131
/**
* Copyright (c) 2014, Willem-Hendrik Thiart
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <sys/mman.h>
#include <unistd.h>
#include "cbuffer.h"
#define fail() assert(0)
/** OSX needs some help here */
#ifndef MAP_ANONYMOUS
# define MAP_ANONYMOUS MAP_ANON
#endif
static void __create_buffer_mirror(cbuf_t* cb)
{
char path[] = "/tmp/cb-XXXXXX";
int fd, status;
void *address;
fd = mkstemp(path);
if (fd < 0)
fail();
status = unlink(path);
if (status)
fail();
status = ftruncate(fd, cb->size);
if (status)
fail();
/* create the array of data */
cb->data = mmap(NULL, cb->size << 1, PROT_NONE, MAP_ANONYMOUS | MAP_PRIVATE,
-1, 0);
if (cb->data == MAP_FAILED)
fail();
address = mmap(cb->data, cb->size, PROT_READ | PROT_WRITE,
MAP_FIXED | MAP_SHARED, fd, 0);
if (address != cb->data)
fail();
address = mmap(cb->data + cb->size, cb->size, PROT_READ | PROT_WRITE,
MAP_FIXED | MAP_SHARED, fd, 0);
if (address != cb->data + cb->size)
fail();
status = close(fd);
if (status)
fail();
}
cbuf_t *cbuf_new(const unsigned int order)
{
cbuf_t *me = malloc(sizeof(cbuf_t));
me->size = 1UL << order;
me->head = me->tail = 0;
__create_buffer_mirror(me);
return me;
}
void cbuf_free(cbuf_t *me)
{
munmap(me->data, me->size << 1);
free(me);
}
int cbuf_is_empty(const cbuf_t *me)
{
return me->head == me->tail;
}
int cbuf_offer(cbuf_t *me, const unsigned char *data, const int size)
{
/* prevent buffer from getting completely full or over commited */
if (cbuf_unusedspace(me) <= size)
return 0;
int written = cbuf_unusedspace(me);
written = size < written ? size : written;
memcpy(me->data + me->tail, data, written);
me->tail += written;
if (me->size < me->tail)
me->tail %= me->size;
return written;
}
unsigned char *cbuf_peek(const cbuf_t *me)
{
if (cbuf_is_empty(me))
return NULL;
return me->data + me->head;
}
unsigned char *cbuf_poll(cbuf_t *me, const unsigned int size)
{
if (cbuf_is_empty(me))
return NULL;
void *end = me->data + me->head;
me->head += size;
return end;
}
int cbuf_size(const cbuf_t *me)
{
return me->size;
}
int cbuf_usedspace(const cbuf_t *me)
{
if (me->head <= me->tail)
return me->tail - me->head;
else
return me->size - (me->head - me->tail);
}
int cbuf_unusedspace(const cbuf_t *me)
{
return me->size - cbuf_usedspace(me);
}