mirror of
https://github.com/Ryujinx/libsoundio.git
synced 2024-12-23 03:55:33 +00:00
fix not freeing mirrored memory on Windows
This commit is contained in:
parent
43d33e5a67
commit
db8eca47ab
61
src/os.cpp
61
src/os.cpp
|
@ -16,6 +16,7 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
#define SOUNDIO_OS_WINDOWS
|
#define SOUNDIO_OS_WINDOWS
|
||||||
|
@ -131,6 +132,14 @@ pthread_mutex_t init_mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||||
|
|
||||||
static int page_size;
|
static int page_size;
|
||||||
|
|
||||||
|
struct SoundIoOsMirroredMemoryPrivate {
|
||||||
|
SoundIoOsMirroredMemory pub;
|
||||||
|
#if defined(SOUNDIO_OS_WINDOWS)
|
||||||
|
HANDLE handle;
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
double soundio_os_get_time(void) {
|
double soundio_os_get_time(void) {
|
||||||
#if defined(SOUNDIO_OS_WINDOWS)
|
#if defined(SOUNDIO_OS_WINDOWS)
|
||||||
unsigned __int64 time;
|
unsigned __int64 time;
|
||||||
|
@ -603,19 +612,19 @@ int soundio_os_page_size(void) {
|
||||||
return page_size;
|
return page_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
int soundio_os_create_mirrored_memory(size_t *capacity, char **out_address) {
|
struct SoundIoOsMirroredMemory *soundio_os_create_mirrored_memory(size_t requested_capacity) {
|
||||||
*out_address = nullptr;
|
SoundIoOsMirroredMemoryPrivate *m = create<SoundIoOsMirroredMemoryPrivate>();
|
||||||
size_t requested_capacity = *capacity;
|
if (!m)
|
||||||
|
return nullptr;
|
||||||
|
SoundIoOsMirroredMemory *mem = &m->pub;
|
||||||
|
|
||||||
size_t actual_capacity = (requested_capacity / page_size) * page_size;
|
size_t actual_capacity = ceil(requested_capacity / (double)page_size) * page_size;
|
||||||
if (actual_capacity < requested_capacity)
|
|
||||||
actual_capacity += page_size;
|
|
||||||
|
|
||||||
#if defined(SOUNDIO_OS_WINDOWS)
|
#if defined(SOUNDIO_OS_WINDOWS)
|
||||||
BOOL ok;
|
BOOL ok;
|
||||||
HANDLE hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, actual_capacity * 2, NULL);
|
HANDLE hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, actual_capacity * 2, NULL);
|
||||||
if (!hMapFile)
|
if (!hMapFile)
|
||||||
return SoundIoErrorNoMem;
|
return nullptr;
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
// find a free address space with the correct size
|
// find a free address space with the correct size
|
||||||
|
@ -623,7 +632,7 @@ int soundio_os_create_mirrored_memory(size_t *capacity, char **out_address) {
|
||||||
if (!address) {
|
if (!address) {
|
||||||
ok = CloseHandle(hMapFile);
|
ok = CloseHandle(hMapFile);
|
||||||
assert(ok);
|
assert(ok);
|
||||||
return SoundIoErrorNoMem;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
// found a big enough address space. hopefully it will remain free
|
// found a big enough address space. hopefully it will remain free
|
||||||
|
@ -639,7 +648,7 @@ int soundio_os_create_mirrored_memory(size_t *capacity, char **out_address) {
|
||||||
} else {
|
} else {
|
||||||
ok = CloseHandle(hMapFile);
|
ok = CloseHandle(hMapFile);
|
||||||
assert(ok);
|
assert(ok);
|
||||||
return SoundIoErrorNoMem;
|
return nullptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -655,48 +664,56 @@ int soundio_os_create_mirrored_memory(size_t *capacity, char **out_address) {
|
||||||
} else {
|
} else {
|
||||||
ok = CloseHandle(hMapFile);
|
ok = CloseHandle(hMapFile);
|
||||||
assert(ok);
|
assert(ok);
|
||||||
return SoundIoErrorNoMem;
|
return nullptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
*out_address = address;
|
mem->address = address;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
char *address = (char*)mmap(NULL, actual_capacity * 2, PROT_NONE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
|
char *address = (char*)mmap(NULL, actual_capacity * 2, PROT_NONE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
|
||||||
if (address == MAP_FAILED)
|
if (address == MAP_FAILED)
|
||||||
return SoundIoErrorNoMem;
|
return nullptr;
|
||||||
|
|
||||||
char *other_address = (char*)mmap(address, actual_capacity, PROT_READ|PROT_WRITE,
|
char *other_address = (char*)mmap(address, actual_capacity, PROT_READ|PROT_WRITE,
|
||||||
MAP_ANONYMOUS|MAP_FIXED|MAP_SHARED, -1, 0);
|
MAP_ANONYMOUS|MAP_FIXED|MAP_SHARED, -1, 0);
|
||||||
if (other_address != address) {
|
if (other_address != address) {
|
||||||
munmap(address, 2 * actual_capacity);
|
munmap(address, 2 * actual_capacity);
|
||||||
return SoundIoErrorNoMem;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
other_address = (char*)mmap(address + actual_capacity, actual_capacity,
|
other_address = (char*)mmap(address + actual_capacity, actual_capacity,
|
||||||
PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_FIXED|MAP_SHARED, -1, 0);
|
PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_FIXED|MAP_SHARED, -1, 0);
|
||||||
if (other_address != address + actual_capacity) {
|
if (other_address != address + actual_capacity) {
|
||||||
munmap(address, 2 * actual_capacity);
|
munmap(address, 2 * actual_capacity);
|
||||||
return SoundIoErrorNoMem;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
*out_address = address;
|
mem->address = address;
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
*capacity = actual_capacity;
|
mem->capacity = actual_capacity;
|
||||||
|
return mem;
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void soundio_os_destroy_mirrored_memory(char *address, size_t capacity) {
|
void soundio_os_destroy_mirrored_memory(struct SoundIoOsMirroredMemory *mem) {
|
||||||
if (!address)
|
if (!mem)
|
||||||
|
return;
|
||||||
|
if (!mem->address)
|
||||||
return;
|
return;
|
||||||
#if defined(SOUNDIO_OS_WINDOWS)
|
#if defined(SOUNDIO_OS_WINDOWS)
|
||||||
// TODO
|
SoundIoOsMirroredMemoryPrivate *m = (SoundIoOsMirroredMemoryPrivate *)mem;
|
||||||
|
BOOL ok;
|
||||||
|
ok = UnmapViewOfFile(mem->address);
|
||||||
|
assert(ok);
|
||||||
|
ok = UnmapViewOfFile(mem->address + mem->capacity);
|
||||||
|
assert(ok);
|
||||||
|
ok = CloseHandle(m->handle);
|
||||||
|
assert(ok);
|
||||||
#else
|
#else
|
||||||
int err = munmap(address, 2 * capacity);
|
int err = munmap(mem->address, 2 * mem->capacity);
|
||||||
assert(!err);
|
assert(!err);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
16
src/os.hpp
16
src/os.hpp
|
@ -51,11 +51,15 @@ void soundio_os_cond_wait(struct SoundIoOsCond *cond,
|
||||||
|
|
||||||
int soundio_os_page_size(void);
|
int soundio_os_page_size(void);
|
||||||
|
|
||||||
// capacity is replaced with actual capacity which might be modified to be
|
// The size of this struct is not part of the API or ABI.
|
||||||
// a multiple of the system page size
|
struct SoundIoOsMirroredMemory {
|
||||||
int soundio_os_create_mirrored_memory(size_t *capacity, char **out_address);
|
size_t capacity;
|
||||||
// capacity should be the actual capacity value that was given via
|
char *address;
|
||||||
// soundio_os_create_mirrored_memory
|
};
|
||||||
void soundio_os_destroy_mirrored_memory(char *address, size_t capacity);
|
|
||||||
|
// returned capacity might be increased from capacity to be a multiple of the
|
||||||
|
// system page size
|
||||||
|
struct SoundIoOsMirroredMemory *soundio_os_create_mirrored_memory(size_t capacity);
|
||||||
|
void soundio_os_destroy_mirrored_memory(struct SoundIoOsMirroredMemory *mem);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -75,14 +75,13 @@ void soundio_ring_buffer_clear(struct SoundIoRingBuffer *rb) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int soundio_ring_buffer_init(struct SoundIoRingBuffer *rb, int requested_capacity) {
|
int soundio_ring_buffer_init(struct SoundIoRingBuffer *rb, int requested_capacity) {
|
||||||
int err;
|
rb->mem = soundio_os_create_mirrored_memory(requested_capacity);
|
||||||
size_t capacity = requested_capacity;
|
if (!rb->mem) {
|
||||||
if ((err = soundio_os_create_mirrored_memory(&capacity, &rb->address))) {
|
|
||||||
soundio_ring_buffer_deinit(rb);
|
soundio_ring_buffer_deinit(rb);
|
||||||
return err;
|
return SoundIoErrorNoMem;
|
||||||
}
|
}
|
||||||
|
rb->address = rb->mem->address;
|
||||||
rb->capacity = capacity;
|
rb->capacity = rb->mem->capacity;
|
||||||
rb->write_offset = 0;
|
rb->write_offset = 0;
|
||||||
rb->read_offset = 0;
|
rb->read_offset = 0;
|
||||||
|
|
||||||
|
@ -90,5 +89,6 @@ int soundio_ring_buffer_init(struct SoundIoRingBuffer *rb, int requested_capacit
|
||||||
}
|
}
|
||||||
|
|
||||||
void soundio_ring_buffer_deinit(struct SoundIoRingBuffer *rb) {
|
void soundio_ring_buffer_deinit(struct SoundIoRingBuffer *rb) {
|
||||||
soundio_os_destroy_mirrored_memory(rb->address, rb->capacity);
|
soundio_os_destroy_mirrored_memory(rb->mem);
|
||||||
|
rb->mem = nullptr;
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,12 +9,14 @@
|
||||||
#define SOUNDIO_RING_BUFFER_HPP
|
#define SOUNDIO_RING_BUFFER_HPP
|
||||||
|
|
||||||
#include "atomics.hpp"
|
#include "atomics.hpp"
|
||||||
|
#include "os.hpp"
|
||||||
|
|
||||||
struct SoundIoRingBuffer {
|
struct SoundIoRingBuffer {
|
||||||
char *address;
|
char *address;
|
||||||
int capacity;
|
int capacity;
|
||||||
atomic_long write_offset;
|
atomic_long write_offset;
|
||||||
atomic_long read_offset;
|
atomic_long read_offset;
|
||||||
|
SoundIoOsMirroredMemory *mem;
|
||||||
};
|
};
|
||||||
|
|
||||||
int soundio_ring_buffer_init(struct SoundIoRingBuffer *rb, int requested_capacity);
|
int soundio_ring_buffer_init(struct SoundIoRingBuffer *rb, int requested_capacity);
|
||||||
|
|
Loading…
Reference in a new issue