-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathring_buffer.cc
169 lines (140 loc) Β· 5.03 KB
/
ring_buffer.cc
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
+----------------------------------------------------------------------+
| Swoole |
+----------------------------------------------------------------------+
| This source file is subject to version 2.0 of the Apache license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.apache.org/licenses/LICENSE-2.0.html |
| If you did not receive a copy of the Apache2.0 license and are unable|
| to obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Tianfeng Han <[email protected]> |
+----------------------------------------------------------------------+
*/
#include "swoole.h"
#include "swoole_memory.h"
namespace swoole {
struct RingBufferImpl {
void *memory;
bool shared;
uint8_t status;
uint32_t size;
uint32_t alloc_offset;
uint32_t collect_offset;
uint32_t alloc_count;
sw_atomic_t free_count;
void collect();
};
struct RingBufferItem {
uint16_t lock;
uint16_t index;
uint32_t length;
char data[0];
};
#ifdef SW_RINGBUFFER_DEBUG
static void swRingBuffer_print(swRingBuffer *object, char *prefix);
static void swRingBuffer_print(swRingBuffer *object, char *prefix) {
printf("%s: size=%d, status=%d, alloc_count=%d, free_count=%d, offset=%d, next_offset=%d\n",
prefix,
impl->size,
impl->status,
impl->alloc_count,
impl->free_count,
impl->alloc_offset,
impl->collect_offset);
}
#endif
RingBuffer::RingBuffer(uint32_t size, bool shared) {
size = SW_MEM_ALIGNED_SIZE(size);
void *mem = (shared == 1) ? sw_shm_malloc(size) : sw_malloc(size);
if (mem == nullptr) {
throw std::bad_alloc();
}
impl = (RingBufferImpl *) mem;
mem = (char *) mem + sizeof(*impl);
sw_memset_zero(impl, sizeof(*impl));
impl->size = size - sizeof(impl);
impl->shared = shared;
impl->memory = mem;
swoole_debug("memory: ptr=%p", mem);
}
void RingBufferImpl::collect() {
for (uint32_t i = 0; i < free_count; i++) {
RingBufferItem *item = (RingBufferItem*) ((char*) memory + collect_offset);
if (item->lock == 0) {
uint32_t n_size = item->length + sizeof(RingBufferItem);
collect_offset += n_size;
if (collect_offset + sizeof(RingBufferItem) > size || collect_offset >= size) {
collect_offset = 0;
status = 0;
}
sw_atomic_fetch_sub(&free_count, 1);
} else {
break;
}
}
}
void *RingBuffer::alloc(uint32_t size) {
assert(size > 0);
RingBufferItem *item;
uint32_t capacity;
size = SW_MEM_ALIGNED_SIZE(size);
uint32_t alloc_size = size + sizeof(RingBufferItem);
if (impl->free_count > 0) {
impl->collect();
}
if (impl->status == 0) {
if (impl->alloc_offset + alloc_size >= (impl->size - sizeof(RingBufferItem))) {
uint32_t skip_n = impl->size - impl->alloc_offset;
if (skip_n >= sizeof(RingBufferItem)) {
item = (RingBufferItem *) ((char *) impl->memory + impl->alloc_offset);
item->lock = 0;
item->length = skip_n - sizeof(RingBufferItem);
sw_atomic_t *free_count = &impl->free_count;
sw_atomic_fetch_add(free_count, 1);
}
impl->alloc_offset = 0;
impl->status = 1;
capacity = impl->collect_offset - impl->alloc_offset;
} else {
capacity = impl->size - impl->alloc_offset;
}
} else {
capacity = impl->collect_offset - impl->alloc_offset;
}
if (capacity < alloc_size) {
return nullptr;
}
item = (RingBufferItem *) ((char *) impl->memory + impl->alloc_offset);
item->lock = 1;
item->length = size;
item->index = impl->alloc_count;
impl->alloc_offset += alloc_size;
impl->alloc_count++;
swoole_debug("alloc: ptr=%p", (void *) (item->data - (char *) impl->memory));
return item->data;
}
void RingBuffer::free(void *ptr) {
RingBufferItem *item = (RingBufferItem *) ((char *) ptr - sizeof(RingBufferItem));
assert(ptr >= impl->memory);
assert((char *) ptr <= (char *) impl->memory + impl->size);
assert(item->lock == 1);
if (item->lock != 1) {
swoole_debug("invalid free: index=%d, ptr=%p", item->index, (void *) (item->data - (char *) impl->memory));
} else {
item->lock = 0;
}
swoole_debug("free: ptr=%p", (void *) (item->data - (char *) impl->memory));
sw_atomic_t *free_count = &impl->free_count;
sw_atomic_fetch_add(free_count, 1);
}
RingBuffer::~RingBuffer() {
if (impl->shared) {
sw_shm_free(impl);
} else {
sw_free(impl);
}
}
}