From 2b0744816f848aa08bb4a29e898773252b962da4 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 6 Jul 2015 14:10:26 -0700 Subject: [PATCH] POSIX ring buffer works on OSX and is simpler --- README.md | 1 - src/ring_buffer.cpp | 25 ++++++++----------------- 2 files changed, 8 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 849b051..3aff8a1 100644 --- a/README.md +++ b/README.md @@ -99,7 +99,6 @@ make ## Roadmap - 0. sine example working with dummy backend osx 0. pipe record to playback example working with dummy linux, osx, windows 0. pipe record to playback example working with pulseaudio linux 0. implement CoreAudio (OSX) backend, get examples working diff --git a/src/ring_buffer.cpp b/src/ring_buffer.cpp index e83ab76..cda1591 100644 --- a/src/ring_buffer.cpp +++ b/src/ring_buffer.cpp @@ -16,6 +16,10 @@ #include #include +#ifndef MAP_ANONYMOUS +#define MAP_ANONYMOUS MAP_ANON +#endif + struct SoundIoRingBuffer *soundio_ring_buffer_create(struct SoundIo *soundio, int requested_capacity) { SoundIoRingBuffer *rb = create(); @@ -88,34 +92,21 @@ int soundio_ring_buffer_init(struct SoundIoRingBuffer *rb, int requested_capacit rb->write_offset = 0; rb->read_offset = 0; - char shm_path[] = "/dev/shm/ring-buffer-XXXXXX"; - int fd = mkstemp(shm_path); - if (fd < 0) - return SoundIoErrorSystemResources; - - if (unlink(shm_path)) - return SoundIoErrorSystemResources; - - if (ftruncate(fd, rb->capacity)) - return SoundIoErrorNoMem; - - rb->address = (char*)mmap(NULL, rb->capacity * 2, PROT_NONE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); + rb->address = (char*)mmap(NULL, rb->capacity * 2, PROT_NONE, + MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); if (rb->address == MAP_FAILED) return SoundIoErrorNoMem; char *other_address = (char*)mmap(rb->address, rb->capacity, - PROT_READ|PROT_WRITE, MAP_FIXED|MAP_SHARED, fd, 0); + PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_FIXED|MAP_SHARED, -1, 0); if (other_address != rb->address) return SoundIoErrorNoMem; other_address = (char*)mmap(rb->address + rb->capacity, rb->capacity, - PROT_READ|PROT_WRITE, MAP_FIXED|MAP_SHARED, fd, 0); + PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_FIXED|MAP_SHARED, -1, 0); if (other_address != rb->address + rb->capacity) return SoundIoErrorNoMem; - if (close(fd)) - return SoundIoErrorSystemResources; - return 0; }