Ring buffer: use shmp (shared-memory pointers) for per-channel shm structures
[lttng-ust.git] / libringbuffer / shm.h
diff --git a/libringbuffer/shm.h b/libringbuffer/shm.h
new file mode 100644 (file)
index 0000000..390a5b2
--- /dev/null
@@ -0,0 +1,71 @@
+#ifndef _LIBRINGBUFFER_SHM_H
+#define _LIBRINGBUFFER_SHM_H
+
+/*
+ * libringbuffer/shm.h
+ *
+ * Copyright 2011 (c) - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+ *
+ * Dual LGPL v2.1/GPL v2 license.
+ */
+
+#include <stdint.h>
+#include "ust/core.h"
+
+#define SHM_MAGIC      0x54335433
+#define SHM_MAJOR      0
+#define SHM_MINOR      1
+
+/*
+ * Defining a max shm offset, for debugging purposes.
+ */
+#if (CAA_BITS_PER_LONG == 32)
+/* Define the maximum shared memory size to 128MB on 32-bit machines */
+#define MAX_SHM_SIZE   134217728
+#else
+/* Define the maximum shared memory size to 8GB on 64-bit machines */
+#define MAX_SHM_SIZE   8589934592
+#endif
+
+#define DECLARE_SHMP(type, name)       type *****name
+
+struct shm_header {
+       uint32_t magic;
+       uint8_t major;
+       uint8_t minor;
+       uint8_t bits_per_long;
+       size_t shm_size, shm_allocated;
+
+       DECLARE_SHMP(struct channel, chan);
+};
+
+#define shmp(shm_offset)               \
+       ((__typeof__(****(shm_offset))) (((char *) &(shm_offset)) + (ptrdiff_t) (shm_offset)))
+
+#define _shmp_abs(a)   ((a < 0) ? -(a) : (a))
+
+static inline
+void _set_shmp(ptrdiff_t *shm_offset, void *ptr)
+{
+       *shm_offset = (((char *) ptr) - ((char *) shm_offset));
+       assert(_shmp_abs(*shm_offset) < MAX_SHM_SIZE);
+}
+
+#define set_shmp(shm_offset, ptr)      \
+       _set_shmp((ptrdiff_t *) ****(shm_offset), ptr)
+
+/* Shared memory is already zeroed by shmget */
+/* *NOT* multithread-safe (should be protected by mutex) */
+static inline
+void *zalloc_shm(struct shm_header *shm_header, size_t len)
+{
+       void *ret;
+
+       if (shm_header->shm_size - shm_header->shm_allocated < len)
+               return NULL;
+       ret = (char *) shm_header + shm_header->shm_allocated;
+       shm_header->shm_allocated += len;
+       return ret;
+}
+
+#endif /* _LIBRINGBUFFER_SHM_H */
This page took 0.024858 seconds and 4 git commands to generate.