26#include <linux/unistd.h>
33#if defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))
39#ifdef ENABLE_INDI_SHARED_MEMORY
41static pthread_mutex_t shared_buffer_mutex = PTHREAD_MUTEX_INITIALIZER;
45#define BLOB_SIZE_UNIT 0x100000
58static size_t allocation(
size_t storage)
67#ifdef ENABLE_INDI_SHARED_MEMORY
75#ifdef ENABLE_INDI_SHARED_MEMORY
77 if (sb == NULL)
goto ERROR;
83 if (sb->
fd == -1)
goto ERROR;
86 if (ret == -1)
goto ERROR;
90 if (sb->
mapstart == MAP_FAILED)
goto ERROR;
99 if (sb->
fd != -1) close(sb->
fd);
111#ifdef ENABLE_INDI_SHARED_MEMORY
113 if (sb == NULL)
goto ERROR;
120 if (sb->
mapstart == MAP_FAILED)
goto ERROR;
141#ifdef ENABLE_INDI_SHARED_MEMORY
152 perror(
"shared buffer munmap");
155 if (close(sb->
fd) == -1)
157 perror(
"shared buffer close");
167#ifdef ENABLE_INDI_SHARED_MEMORY
177 perror(
"shared buffer munmap");
194 sb = sharedBufferFind(ptr);
198 return realloc(ptr,
size);
218 size_t reallocated = allocation(
size);
225 int ret = ftruncate(sb->
fd, reallocated);
226 if (ret == -1)
return NULL;
229 void * remaped = mremap(sb->
mapstart, sb->
allocated, reallocated, MREMAP_MAYMOVE);
230 if (remaped == MAP_FAILED)
return NULL;
236 perror(
"shared buffer munmap");
239 void * remaped = mmap(0, reallocated, PROT_READ | PROT_WRITE, MAP_SHARED, sb->
fd, 0);
240 if (remaped == MAP_FAILED)
return NULL;
252#ifdef ENABLE_INDI_SHARED_MEMORY
253 void * ret = mmap(sb->
mapstart, sb->
allocated, PROT_READ, MAP_SHARED | MAP_FIXED, sb->
fd, 0);
254 if (ret == MAP_FAILED)
256 perror(
"remap readonly failed");
261 perror(
"seal unsupported on this platform");
268 sb = sharedBufferFind(ptr);
283#ifdef ENABLE_INDI_SHARED_MEMORY
285 sb = sharedBufferFind(ptr);
293#ifdef ENABLE_INDI_SHARED_MEMORY
298 pthread_mutex_lock(&shared_buffer_mutex);
311 pthread_mutex_unlock(&shared_buffer_mutex);
329 pthread_mutex_lock(&shared_buffer_mutex);
350 pthread_mutex_unlock(&shared_buffer_mutex);
357#ifdef ENABLE_INDI_SHARED_MEMORY
358 pthread_mutex_lock(&shared_buffer_mutex);
360 pthread_mutex_unlock(&shared_buffer_mutex);
void IDSharedBlobFree(void *ptr)
Free a buffer allocated using IDSharedBlobAlloc. Fall back to free for buffer that are not shared blo...
void * IDSharedBlobAttach(int fd, size_t size)
void * IDSharedBlobRealloc(void *ptr, size_t size)
Adjust the size of a buffer obtained using IDSharedBlobAlloc.
void * IDSharedBlobAlloc(size_t size)
Allocate a buffer suitable for fast exchange over local links. Warning : the buffer will be sealed (r...
void IDSharedBlobDettach(void *ptr)
Dettach a blob, but don't close its FD.
int IDSharedBlobGetFd(void *ptr)
Return the filedescriptor backing up the given shared buffer.
void IDSharedBlobSeal(void *ptr)
Seal (make readonly) a buffer allocated using IDSharedBlobAlloc. This is automatic when IDNewBlob.
struct shared_buffer * next
struct shared_buffer * prev