summaryrefslogtreecommitdiff
path: root/src/lib/shm_rdrbuff.c
diff options
context:
space:
mode:
authorDimitri Staessens <dimitri.staessens@ugent.be>2018-10-25 21:58:21 +0200
committerSander Vrijders <sander.vrijders@ugent.be>2018-10-26 08:52:31 +0200
commit96682a8284cc234207d2ba6a60b8f9939c17834f (patch)
tree98b523a2f05e0f5777a177bddc1926a7658244fc /src/lib/shm_rdrbuff.c
parent991470de0c7b59c69dd2de8110ba1ddef042a473 (diff)
downloadouroboros-96682a8284cc234207d2ba6a60b8f9939c17834f.tar.gz
ouroboros-96682a8284cc234207d2ba6a60b8f9939c17834f.zip
lib: Revise du buff API towards a memory allocator
This changes the API to the rdrbuff to treat it as a pool memory allocator. The head and tailspace to allocate in a buffer is now set system-wide instead of being passed as a parameter. Signed-off-by: Dimitri Staessens <dimitri.staessens@ugent.be> Signed-off-by: Sander Vrijders <sander.vrijders@ugent.be>
Diffstat (limited to 'src/lib/shm_rdrbuff.c')
-rw-r--r--src/lib/shm_rdrbuff.c37
1 files changed, 20 insertions, 17 deletions
diff --git a/src/lib/shm_rdrbuff.c b/src/lib/shm_rdrbuff.c
index e9ef9222..0d3faf17 100644
--- a/src/lib/shm_rdrbuff.c
+++ b/src/lib/shm_rdrbuff.c
@@ -45,6 +45,7 @@
#define SHM_FILE_SIZE (SHM_BLOCKS_SIZE + 2 * sizeof(size_t) \
+ sizeof(pthread_mutex_t) + 2 * sizeof(pthread_cond_t) \
+ sizeof(pid_t))
+#define DU_BUFF_OVERHEAD (DU_BUFF_HEADSPACE + DU_BUFF_TAILSPACE)
#define get_head_ptr(rdrb) \
idx_to_du_buff_ptr(rdrb, *rdrb->head)
@@ -277,14 +278,13 @@ void shm_rdrbuff_purge(void)
free(shm_rdrb_fn);
}
-ssize_t shm_rdrbuff_write(struct shm_rdrbuff * rdrb,
- size_t headspace,
- size_t tailspace,
- const uint8_t * data,
- size_t len)
+ssize_t shm_rdrbuff_alloc(struct shm_rdrbuff * rdrb,
+ size_t len,
+ uint8_t ** ptr,
+ struct shm_du_buff ** psdb)
{
struct shm_du_buff * sdb;
- size_t size = headspace + len + tailspace;
+ size_t size = DU_BUFF_OVERHEAD + len;
#ifdef SHM_RDRB_MULTI_BLOCK
size_t blocks = 0;
size_t padblocks = 0;
@@ -292,6 +292,7 @@ ssize_t shm_rdrbuff_write(struct shm_rdrbuff * rdrb,
ssize_t sz = size + sizeof(*sdb);
assert(rdrb);
+ assert(psdb);
#ifndef SHM_RDRB_MULTI_BLOCK
if (sz > SHM_RDRB_BLOCK_SIZE)
@@ -346,24 +347,24 @@ ssize_t shm_rdrbuff_write(struct shm_rdrbuff * rdrb,
pthread_mutex_unlock(rdrb->lock);
sdb->size = size;
- sdb->du_head = headspace;
+ sdb->du_head = DU_BUFF_HEADSPACE;
sdb->du_tail = sdb->du_head + len;
- if (data != NULL)
- memcpy(((uint8_t *) (sdb + 1)) + headspace, data, len);
+ *psdb = sdb;
+ if (ptr != NULL)
+ *ptr = (uint8_t *) (sdb + 1) + sdb->du_head;
return sdb->idx;
}
-ssize_t shm_rdrbuff_write_b(struct shm_rdrbuff * rdrb,
- size_t headspace,
- size_t tailspace,
- const uint8_t * data,
+ssize_t shm_rdrbuff_alloc_b(struct shm_rdrbuff * rdrb,
size_t len,
+ uint8_t ** ptr,
+ struct shm_du_buff ** psdb,
const struct timespec * abstime)
{
struct shm_du_buff * sdb;
- size_t size = headspace + len + tailspace;
+ size_t size = DU_BUFF_OVERHEAD + len;
#ifdef SHM_RDRB_MULTI_BLOCK
size_t blocks = 0;
size_t padblocks = 0;
@@ -372,6 +373,7 @@ ssize_t shm_rdrbuff_write_b(struct shm_rdrbuff * rdrb,
int ret = 0;
assert(rdrb);
+ assert(psdb);
#ifndef SHM_RDRB_MULTI_BLOCK
if (sz > SHM_RDRB_BLOCK_SIZE)
@@ -444,11 +446,12 @@ ssize_t shm_rdrbuff_write_b(struct shm_rdrbuff * rdrb,
return -ETIMEDOUT;
sdb->size = size;
- sdb->du_head = headspace;
+ sdb->du_head = DU_BUFF_HEADSPACE;
sdb->du_tail = sdb->du_head + len;
- if (data != NULL)
- memcpy(((uint8_t *) (sdb + 1)) + headspace, data, len);
+ *psdb = sdb;
+ if (ptr != NULL)
+ *ptr = (uint8_t *) (sdb + 1) + sdb->du_head;
return sdb->idx;
}