Permalink
Browse files

.

  • Loading branch information...
1 parent 3f048a8 commit 0f3ae54d309631b1d0e9c56e84982f5d7c498b77 @nettoyeurny nettoyeurny committed Nov 18, 2011
Showing with 19 additions and 12 deletions.
  1. +13 −10 objc/PdBase.m
  2. +6 −2 objc/ringbuffer.c
View
@@ -38,7 +38,8 @@
static NSObject<PdReceiverDelegate> *delegate = nil;
-static ring_buffer *ringBuffer = NULL;
+static volatile ring_buffer *ringBuffer = NULL;
+static void *tempBuffer = NULL;
static NSArray *decodeList(int argc, t_atom *argv) {
NSMutableArray *list = [[NSMutableArray alloc] initWithCapacity:argc];
@@ -203,11 +204,9 @@ @implementation PdMessageHandler
-(void)pollQueue:(NSTimer *)timer {
size_t available = rb_available_to_read(ringBuffer);
if (!available) return;
- void *start = malloc(available);
- if (!start) return;
- rb_read_from_buffer(ringBuffer, start, available);
- void *end = start + available;
- void *buffer = start;
+ rb_read_from_buffer(ringBuffer, tempBuffer, available);
+ void *end = tempBuffer + available;
+ void *buffer = tempBuffer;
while (buffer < end) {
params *p = buffer;
buffer += S_PARAMS;
@@ -240,7 +239,6 @@ -(void)pollQueue:(NSTimer *)timer {
break;
}
}
- free(start);
}
@end
@@ -266,8 +264,13 @@ + (void)initialize {
// Only to called from main thread.
+ (size_t)setMessageBufferSize:(size_t)size {
if (!ringBuffer) {
- @synchronized(self) { // Still need to synchronize for visibility.
- ringBuffer = rb_create(size);
+ ringBuffer = rb_create(size);
+ if (!ringBuffer) return 0;
+ tempBuffer = malloc(size);
+ if (!tempBuffer) {
+ rb_free(ringBuffer);
+ ringBuffer = NULL;
+ return 0;
}
}
return ringBuffer->size;
@@ -280,7 +283,7 @@ + (void)setDelegate:(NSObject<PdReceiverDelegate> *)newDelegate {
[pollTimer invalidate]; // This also releases the timer.
pollTimer = nil;
} else {
- [self setMessageBufferSize:65536]; // Will do nothing if buffer is already initialized.
+ [self setMessageBufferSize:32768]; // Will do nothing if buffer is already initialized.
}
[newDelegate retain];
[delegate release];
View
@@ -37,11 +37,15 @@ size_t rb_available_to_write(ring_buffer *buffer) {
// Note: The largest possible result is buffer->size - 1 because
// we adopt the convention that read_idx == write_idx means that the
// buffer is empty.
- return (buffer->size + buffer->read_idx - buffer->write_idx - 1) % buffer->size;
+ return buffer
+ ? (buffer->size + buffer->read_idx - buffer->write_idx - 1) % buffer->size
+ : 0;
}
size_t rb_available_to_read(ring_buffer *buffer) {
- return (buffer->size + buffer->write_idx - buffer->read_idx) % buffer->size;
+ return buffer
+ ? (buffer->size + buffer->write_idx - buffer->read_idx) % buffer->size
+ : 0;
}
int rb_write_to_buffer(ring_buffer *buffer, const void *src, size_t len) {

0 comments on commit 0f3ae54

Please sign in to comment.