26 #include <linux/unistd.h>
36 #ifdef ENABLE_INDI_SHARED_MEMORY
41 #define BLOB_SIZE_UNIT 0x100000
43 static pthread_mutex_t shared_buffer_mutex = PTHREAD_MUTEX_INITIALIZER;
56 static size_t allocation(
size_t storage)
72 #ifdef ENABLE_INDI_SHARED_MEMORY
74 if (sb == NULL)
goto ERROR;
80 if (sb->
fd == -1)
goto ERROR;
83 if (ret == -1)
goto ERROR;
87 if (sb->
mapstart == MAP_FAILED)
goto ERROR;
96 if (sb->
fd != -1) close(sb->
fd);
109 if (sb == NULL)
goto ERROR;
116 if (sb->
mapstart == MAP_FAILED)
goto ERROR;
144 perror(
"shared buffer munmap");
147 if (close(sb->
fd) == -1)
149 perror(
"shared buffer close");
165 perror(
"shared buffer munmap");
179 sb = sharedBufferFind(ptr);
183 return realloc(ptr,
size);
200 size_t reallocated = allocation(
size);
207 int ret = ftruncate(sb->
fd, reallocated);
208 if (ret == -1)
return NULL;
211 void * remaped = mremap(sb->
mapstart, sb->
allocated, reallocated, MREMAP_MAYMOVE);
212 if (remaped == MAP_FAILED)
return NULL;
218 perror(
"shared buffer munmap");
221 void * remaped = mmap(0, reallocated, PROT_READ | PROT_WRITE, MAP_SHARED, sb->
fd, 0);
222 if (remaped == MAP_FAILED)
return NULL;
233 void * ret = mmap(sb->
mapstart, sb->
allocated, PROT_READ, MAP_SHARED | MAP_FIXED, sb->
fd, 0);
234 if (ret == MAP_FAILED)
236 perror(
"remap readonly failed");
244 sb = sharedBufferFind(ptr);
260 sb = sharedBufferFind(ptr);
269 pthread_mutex_lock(&shared_buffer_mutex);
282 pthread_mutex_unlock(&shared_buffer_mutex);
300 pthread_mutex_lock(&shared_buffer_mutex);
321 pthread_mutex_unlock(&shared_buffer_mutex);
327 pthread_mutex_lock(&shared_buffer_mutex);
329 pthread_mutex_unlock(&shared_buffer_mutex);
void IDSharedBlobFree(void *ptr)
void * IDSharedBlobAttach(int fd, size_t size)
void * IDSharedBlobRealloc(void *ptr, size_t size)
struct shared_buffer shared_buffer
void IDSharedBlobDettach(void *ptr)
void * IDSharedBlobAlloc(size_t size)
int IDSharedBlobGetFd(void *ptr)
void IDSharedBlobSeal(void *ptr)
struct shared_buffer * next
struct shared_buffer * prev