Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(225)

Side by Side Diff: webrtc/modules/audio_processing/utility/ring_buffer.cc

Issue 1851873003: Moved the ringbuffer to be built using C++ (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@MoveRingBuffer_CL
Patch Set: Fixed warnings reported by cl upload Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/modules/audio_processing/utility/ring_buffer.c ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
(...skipping 24 matching lines...) Expand all
35 // If the region is contiguous, |data_ptr_bytes_2| will be zero. 35 // If the region is contiguous, |data_ptr_bytes_2| will be zero.
36 // If non-contiguous, |data_ptr_bytes_2| will be the size in bytes of the second 36 // If non-contiguous, |data_ptr_bytes_2| will be the size in bytes of the second
37 // region. Returns room available to be read or |element_count|, whichever is 37 // region. Returns room available to be read or |element_count|, whichever is
38 // smaller. 38 // smaller.
39 static size_t GetBufferReadRegions(RingBuffer* buf, 39 static size_t GetBufferReadRegions(RingBuffer* buf,
40 size_t element_count, 40 size_t element_count,
41 void** data_ptr_1, 41 void** data_ptr_1,
42 size_t* data_ptr_bytes_1, 42 size_t* data_ptr_bytes_1,
43 void** data_ptr_2, 43 void** data_ptr_2,
44 size_t* data_ptr_bytes_2) { 44 size_t* data_ptr_bytes_2) {
45
46 const size_t readable_elements = WebRtc_available_read(buf); 45 const size_t readable_elements = WebRtc_available_read(buf);
47 const size_t read_elements = (readable_elements < element_count ? 46 const size_t read_elements = (readable_elements < element_count ?
48 readable_elements : element_count); 47 readable_elements : element_count);
49 const size_t margin = buf->element_count - buf->read_pos; 48 const size_t margin = buf->element_count - buf->read_pos;
50 49
51 // Check to see if read is not contiguous. 50 // Check to see if read is not contiguous.
52 if (read_elements > margin) { 51 if (read_elements > margin) {
53 // Write data in two blocks that wrap the buffer. 52 // Write data in two blocks that wrap the buffer.
54 *data_ptr_1 = buf->data + buf->read_pos * buf->element_size; 53 *data_ptr_1 = buf->data + buf->read_pos * buf->element_size;
55 *data_ptr_bytes_1 = margin * buf->element_size; 54 *data_ptr_bytes_1 = margin * buf->element_size;
56 *data_ptr_2 = buf->data; 55 *data_ptr_2 = buf->data;
57 *data_ptr_bytes_2 = (read_elements - margin) * buf->element_size; 56 *data_ptr_bytes_2 = (read_elements - margin) * buf->element_size;
58 } else { 57 } else {
59 *data_ptr_1 = buf->data + buf->read_pos * buf->element_size; 58 *data_ptr_1 = buf->data + buf->read_pos * buf->element_size;
60 *data_ptr_bytes_1 = read_elements * buf->element_size; 59 *data_ptr_bytes_1 = read_elements * buf->element_size;
61 *data_ptr_2 = NULL; 60 *data_ptr_2 = NULL;
62 *data_ptr_bytes_2 = 0; 61 *data_ptr_bytes_2 = 0;
63 } 62 }
64 63
65 return read_elements; 64 return read_elements;
66 } 65 }
67 66
68 RingBuffer* WebRtc_CreateBuffer(size_t element_count, size_t element_size) { 67 RingBuffer* WebRtc_CreateBuffer(size_t element_count, size_t element_size) {
69 RingBuffer* self = NULL; 68 RingBuffer* self = NULL;
70 if (element_count == 0 || element_size == 0) { 69 if (element_count == 0 || element_size == 0) {
71 return NULL; 70 return NULL;
72 } 71 }
73 72
74 self = malloc(sizeof(RingBuffer)); 73 self = static_cast<RingBuffer*>(malloc(sizeof(RingBuffer)));
75 if (!self) { 74 if (!self) {
76 return NULL; 75 return NULL;
77 } 76 }
78 77
79 self->data = malloc(element_count * element_size); 78 self->data = static_cast<char*>(malloc(element_count * element_size));
80 if (!self->data) { 79 if (!self->data) {
81 free(self); 80 free(self);
82 self = NULL; 81 self = NULL;
83 return NULL; 82 return NULL;
84 } 83 }
85 84
86 self->element_count = element_count; 85 self->element_count = element_count;
87 self->element_size = element_size; 86 self->element_size = element_size;
88 WebRtc_InitBuffer(self); 87 WebRtc_InitBuffer(self);
89 88
90 return self; 89 return self;
91 } 90 }
92 91
93 void WebRtc_InitBuffer(RingBuffer* self) { 92 void WebRtc_InitBuffer(RingBuffer* self) {
94 self->read_pos = 0; 93 self->read_pos = 0;
95 self->write_pos = 0; 94 self->write_pos = 0;
96 self->rw_wrap = SAME_WRAP; 95 self->rw_wrap = SAME_WRAP;
97 96
98 // Initialize buffer to zeros 97 // Initialize buffer to zeros
99 memset(self->data, 0, self->element_count * self->element_size); 98 memset(self->data, 0, self->element_count * self->element_size);
100 } 99 }
101 100
102 void WebRtc_FreeBuffer(void* handle) { 101 void WebRtc_FreeBuffer(void* handle) {
103 RingBuffer* self = (RingBuffer*)handle; 102 RingBuffer* self = static_cast<RingBuffer*>(handle);
104 if (!self) { 103 if (!self) {
105 return; 104 return;
106 } 105 }
107 106
108 free(self->data); 107 free(self->data);
109 free(self); 108 free(self);
110 } 109 }
111 110
112 size_t WebRtc_ReadBuffer(RingBuffer* self, 111 size_t WebRtc_ReadBuffer(RingBuffer* self,
113 void** data_ptr, 112 void** data_ptr,
114 void* data, 113 void* data,
115 size_t element_count) { 114 size_t element_count) {
116
117 if (self == NULL) { 115 if (self == NULL) {
118 return 0; 116 return 0;
119 } 117 }
120 if (data == NULL) { 118 if (data == NULL) {
121 return 0; 119 return 0;
122 } 120 }
123 121
124 { 122 {
125 void* buf_ptr_1 = NULL; 123 void* buf_ptr_1 = NULL;
126 void* buf_ptr_2 = NULL; 124 void* buf_ptr_2 = NULL;
127 size_t buf_ptr_bytes_1 = 0; 125 size_t buf_ptr_bytes_1 = 0;
128 size_t buf_ptr_bytes_2 = 0; 126 size_t buf_ptr_bytes_2 = 0;
129 const size_t read_count = GetBufferReadRegions(self, 127 const size_t read_count = GetBufferReadRegions(self,
130 element_count, 128 element_count,
131 &buf_ptr_1, 129 &buf_ptr_1,
132 &buf_ptr_bytes_1, 130 &buf_ptr_bytes_1,
133 &buf_ptr_2, 131 &buf_ptr_2,
134 &buf_ptr_bytes_2); 132 &buf_ptr_bytes_2);
135 133
136 if (buf_ptr_bytes_2 > 0) { 134 if (buf_ptr_bytes_2 > 0) {
137 // We have a wrap around when reading the buffer. Copy the buffer data to 135 // We have a wrap around when reading the buffer. Copy the buffer data to
138 // |data| and point to it. 136 // |data| and point to it.
139 memcpy(data, buf_ptr_1, buf_ptr_bytes_1); 137 memcpy(data, buf_ptr_1, buf_ptr_bytes_1);
140 memcpy(((char*) data) + buf_ptr_bytes_1, buf_ptr_2, buf_ptr_bytes_2); 138 memcpy(static_cast<char*>(data) + buf_ptr_bytes_1, buf_ptr_2,
139 buf_ptr_bytes_2);
141 buf_ptr_1 = data; 140 buf_ptr_1 = data;
142 } else if (!data_ptr) { 141 } else if (!data_ptr) {
143 // No wrap, but a memcpy was requested. 142 // No wrap, but a memcpy was requested.
144 memcpy(data, buf_ptr_1, buf_ptr_bytes_1); 143 memcpy(data, buf_ptr_1, buf_ptr_bytes_1);
145 } 144 }
146 if (data_ptr) { 145 if (data_ptr) {
147 // |buf_ptr_1| == |data| in the case of a wrap. 146 // |buf_ptr_1| == |data| in the case of a wrap.
148 *data_ptr = buf_ptr_1; 147 *data_ptr = buf_ptr_1;
149 } 148 }
150 149
151 // Update read position 150 // Update read position
152 WebRtc_MoveReadPtr(self, (int) read_count); 151 WebRtc_MoveReadPtr(self, static_cast<int>(read_count));
153 152
154 return read_count; 153 return read_count;
155 } 154 }
156 } 155 }
157 156
158 size_t WebRtc_WriteBuffer(RingBuffer* self, 157 size_t WebRtc_WriteBuffer(RingBuffer* self,
159 const void* data, 158 const void* data,
160 size_t element_count) { 159 size_t element_count) {
161 if (!self) { 160 if (!self) {
162 return 0; 161 return 0;
(...skipping 27 matching lines...) Expand all
190 } 189 }
191 190
192 int WebRtc_MoveReadPtr(RingBuffer* self, int element_count) { 191 int WebRtc_MoveReadPtr(RingBuffer* self, int element_count) {
193 if (!self) { 192 if (!self) {
194 return 0; 193 return 0;
195 } 194 }
196 195
197 { 196 {
198 // We need to be able to take care of negative changes, hence use "int" 197 // We need to be able to take care of negative changes, hence use "int"
199 // instead of "size_t". 198 // instead of "size_t".
200 const int free_elements = (int) WebRtc_available_write(self); 199 const int free_elements = static_cast<int>(WebRtc_available_write(self));
201 const int readable_elements = (int) WebRtc_available_read(self); 200 const int readable_elements = static_cast<int>(WebRtc_available_read(self));
202 int read_pos = (int) self->read_pos; 201 int read_pos = static_cast<int>(self->read_pos);
203 202
204 if (element_count > readable_elements) { 203 if (element_count > readable_elements) {
205 element_count = readable_elements; 204 element_count = readable_elements;
206 } 205 }
207 if (element_count < -free_elements) { 206 if (element_count < -free_elements) {
208 element_count = -free_elements; 207 element_count = -free_elements;
209 } 208 }
210 209
211 read_pos += element_count; 210 read_pos += element_count;
212 if (read_pos > (int) self->element_count) { 211 if (read_pos > static_cast<int>(self->element_count)) {
213 // Buffer wrap around. Restart read position and wrap indicator. 212 // Buffer wrap around. Restart read position and wrap indicator.
214 read_pos -= (int) self->element_count; 213 read_pos -= static_cast<int>(self->element_count);
215 self->rw_wrap = SAME_WRAP; 214 self->rw_wrap = SAME_WRAP;
216 } 215 }
217 if (read_pos < 0) { 216 if (read_pos < 0) {
218 // Buffer wrap around. Restart read position and wrap indicator. 217 // Buffer wrap around. Restart read position and wrap indicator.
219 read_pos += (int) self->element_count; 218 read_pos += static_cast<int>(self->element_count);
220 self->rw_wrap = DIFF_WRAP; 219 self->rw_wrap = DIFF_WRAP;
221 } 220 }
222 221
223 self->read_pos = (size_t) read_pos; 222 self->read_pos = static_cast<size_t>(read_pos);
224 223
225 return element_count; 224 return element_count;
226 } 225 }
227 } 226 }
228 227
229 size_t WebRtc_available_read(const RingBuffer* self) { 228 size_t WebRtc_available_read(const RingBuffer* self) {
230 if (!self) { 229 if (!self) {
231 return 0; 230 return 0;
232 } 231 }
233 232
234 if (self->rw_wrap == SAME_WRAP) { 233 if (self->rw_wrap == SAME_WRAP) {
235 return self->write_pos - self->read_pos; 234 return self->write_pos - self->read_pos;
236 } else { 235 } else {
237 return self->element_count - self->read_pos + self->write_pos; 236 return self->element_count - self->read_pos + self->write_pos;
238 } 237 }
239 } 238 }
240 239
241 size_t WebRtc_available_write(const RingBuffer* self) { 240 size_t WebRtc_available_write(const RingBuffer* self) {
242 if (!self) { 241 if (!self) {
243 return 0; 242 return 0;
244 } 243 }
245 244
246 return self->element_count - WebRtc_available_read(self); 245 return self->element_count - WebRtc_available_read(self);
247 } 246 }
OLDNEW
« no previous file with comments | « webrtc/modules/audio_processing/utility/ring_buffer.c ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698