summaryrefslogtreecommitdiff
path: root/src/lib/shm_rbuff.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/shm_rbuff.c')
-rw-r--r--src/lib/shm_rbuff.c49
1 files changed, 27 insertions, 22 deletions
diff --git a/src/lib/shm_rbuff.c b/src/lib/shm_rbuff.c
index 361d5bb0..22cff41c 100644
--- a/src/lib/shm_rbuff.c
+++ b/src/lib/shm_rbuff.c
@@ -1,5 +1,5 @@
/*
- * Ouroboros - Copyright (C) 2016 - 2021
+ * Ouroboros - Copyright (C) 2016 - 2024
*
* Ring buffer implementations for incoming packets
*
@@ -26,22 +26,22 @@
#include <ouroboros/shm_rbuff.h>
#include <ouroboros/lockfile.h>
-#include <ouroboros/time_utils.h>
#include <ouroboros/errno.h>
#include <ouroboros/fccntl.h>
#include <ouroboros/pthread.h>
+#include <ouroboros/time.h>
-#include <sys/mman.h>
+#include <assert.h>
#include <fcntl.h>
+#include <signal.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <stdio.h>
-#include <stdint.h>
#include <unistd.h>
-#include <signal.h>
+#include <sys/mman.h>
#include <sys/stat.h>
-#include <assert.h>
-#include <stdbool.h>
#define FN_MAX_CHARS 255
@@ -69,20 +69,11 @@ struct shm_rbuff {
int flow_id; /* flow_id of the flow */
};
-void shm_rbuff_close(struct shm_rbuff * rb)
-{
- assert(rb);
-
- munmap(rb->shm_base, SHM_RB_FILE_SIZE);
-
- free(rb);
-}
-
#define MM_FLAGS (PROT_READ | PROT_WRITE)
-struct shm_rbuff * rbuff_create(pid_t pid,
- int flow_id,
- int flags)
+static struct shm_rbuff * rbuff_create(pid_t pid,
+ int flow_id,
+ int flags)
{
struct shm_rbuff * rb;
int fd;
@@ -99,7 +90,7 @@ struct shm_rbuff * rbuff_create(pid_t pid,
if (fd == -1)
goto fail_open;
- if ((flags & O_CREAT) && ftruncate(fd, SHM_RB_FILE_SIZE - 1) < 0)
+ if ((flags & O_CREAT) && ftruncate(fd, SHM_RB_FILE_SIZE) < 0)
goto fail_truncate;
shm_base = mmap(NULL, SHM_RB_FILE_SIZE, MM_FLAGS, MAP_SHARED, fd, 0);
@@ -130,6 +121,13 @@ struct shm_rbuff * rbuff_create(pid_t pid,
return NULL;
}
+static void rbuff_destroy(struct shm_rbuff * rb)
+{
+ munmap(rb->shm_base, SHM_RB_FILE_SIZE);
+
+ free(rb);
+}
+
struct shm_rbuff * shm_rbuff_create(pid_t pid,
int flow_id)
{
@@ -174,7 +172,7 @@ struct shm_rbuff * shm_rbuff_create(pid_t pid,
*rb->head = 0;
*rb->tail = 0;
- rb->pid = pid;
+ rb->pid = pid;
rb->flow_id = flow_id;
pthread_mutexattr_destroy(&mattr);
@@ -202,6 +200,13 @@ struct shm_rbuff * shm_rbuff_open(pid_t pid,
return rbuff_create(pid, flow_id, O_RDWR);
}
+void shm_rbuff_close(struct shm_rbuff * rb)
+{
+ assert(rb);
+
+ rbuff_destroy(rb);
+}
+
#if (defined(SHM_RBUFF_LOCKLESS) && \
(defined(__GNUC__) || defined (__clang__)))
#include "shm_rbuff_ll.c"