From fc8f6cebd1926f6b57968f83161e4d6495fdc06d Mon Sep 17 00:00:00 2001 From: Marek Chalupa Date: Wed, 19 Apr 2023 16:25:10 +0200 Subject: [PATCH 01/34] Rename {shm/shamon} -> vms The project is not called Shamon anymore. --- include/vamos-buffers/core/arbiter.h | 70 ++--- include/vamos-buffers/core/event.h | 30 +- include/vamos-buffers/core/list-embedded.h | 58 ++-- include/vamos-buffers/core/list.h | 42 +-- include/vamos-buffers/core/monitor.h | 56 ++-- include/vamos-buffers/core/par_queue.h | 44 +-- include/vamos-buffers/core/queue_spsc.h | 42 +-- include/vamos-buffers/core/shamon.h | 18 +- include/vamos-buffers/core/shm_string.h | 36 --- include/vamos-buffers/core/source.h | 2 +- include/vamos-buffers/core/spsc_ringbuf.h | 40 +-- include/vamos-buffers/core/stream.h | 118 +++---- include/vamos-buffers/core/vector-aligned.h | 20 +- include/vamos-buffers/core/vector-macro.h | 2 +- include/vamos-buffers/core/vector.h | 30 +- ...{shm_string-macro.h => vms_string-macro.h} | 0 include/vamos-buffers/core/vms_string.h | 36 +++ include/vamos-buffers/shmbuf/buffer.h | 32 +- .../vamos-buffers/streams/stream-drregex.h | 16 +- include/vamos-buffers/streams/stream-funs.h | 30 +- .../vamos-buffers/streams/stream-generic.h | 16 +- include/vamos-buffers/streams/stream-regex.h | 18 +- .../vamos-buffers/streams/stream-regexrw.h | 16 +- include/vamos-buffers/streams/streams.h | 8 +- src/core/CMakeLists.txt | 2 +- src/core/arbiter.c | 288 +++++++++--------- src/core/event.c | 16 +- src/core/legacy/queue-test.c | 28 +- src/core/legacy/queue.c | 16 +- src/core/legacy/queue.h | 18 +- src/core/list-embedded.c | 20 +- src/core/list.c | 38 +-- src/core/monitor.c | 82 ++--- src/core/par_queue.c | 74 ++--- src/core/queue_spsc.c | 38 +-- src/core/shamon.c | 126 ++++---- src/core/source.c | 2 +- src/core/spsc_ringbuf.c | 38 +-- src/core/stream.c | 130 ++++---- src/core/vector-aligned.c | 26 +- src/core/vector.c | 26 +- src/core/{shm_string.c => vms_string.c} | 28 +- src/shmbuf/buffer-aux.c | 34 +-- src/shmbuf/buffer-control.c | 34 +-- src/shmbuf/buffer-dbg.c | 82 ++--- src/shmbuf/buffer-local.c | 6 +- src/shmbuf/buffer-private.h | 16 +- src/shmbuf/buffer-sub.c | 4 +- src/shmbuf/buffer.c | 62 ++-- src/shmbuf/shm.c | 34 +-- src/shmbuf/shm.h | 16 +- src/shmbuf/utils.c | 4 +- src/streams/legacy/stream-fastbuf-io.c | 38 +-- src/streams/legacy/stream-fastbuf-io.h | 24 +- src/streams/legacy/stream-fds.c | 44 +-- src/streams/legacy/stream-fds.h | 26 +- src/streams/legacy/stream-stdin.c | 24 +- src/streams/legacy/stream-stdin.h | 26 +- src/streams/stream-drregex.c | 14 +- src/streams/stream-funs.c | 20 +- src/streams/stream-generic.c | 14 +- src/streams/stream-regex.c | 16 +- src/streams/stream-regexrw.c | 14 +- src/streams/streams.c | 20 +- tests/arbiter-buffer.c | 54 ++-- tests/bench/ringbufs.c | 52 ++-- tests/fetch-test-2.c | 42 +-- tests/fetch-test-3.c | 44 +-- tests/fetch-test.c | 20 +- tests/list-test.c | 58 ++-- tests/queue-par-cbmc.c | 14 +- tests/queue-par-cbmc_2.c | 14 +- tests/queue-par-cbmc_3.c | 22 +- tests/queue-par-test-2.c | 154 +++++----- tests/queue-par-test.c | 14 +- tests/spsc-ringbuf-1.c | 156 +++++----- tests/spsc-ringbuf-cbmc-1.c | 102 +++---- 77 files changed, 1547 insertions(+), 1547 deletions(-) delete mode 100644 include/vamos-buffers/core/shm_string.h rename include/vamos-buffers/core/{shm_string-macro.h => vms_string-macro.h} (100%) create mode 100644 include/vamos-buffers/core/vms_string.h rename src/core/{shm_string.c => vms_string.c} (58%) diff --git a/include/vamos-buffers/core/arbiter.h b/include/vamos-buffers/core/arbiter.h index 1e3a8fb4..cfc2d054 100644 --- a/include/vamos-buffers/core/arbiter.h +++ b/include/vamos-buffers/core/arbiter.h @@ -16,65 +16,65 @@ #include "stream.h" -typedef struct _shm_arbiter_buffer shm_arbiter_buffer; -typedef struct _shm_event shm_event; +typedef struct _vms_arbiter_buffer vms_arbiter_buffer; +typedef struct _vms_event vms_event; -void shm_arbiter_buffer_init(shm_arbiter_buffer *buffer, shm_stream *stream, +void vms_arbiter_buffer_init(vms_arbiter_buffer *buffer, vms_stream *stream, size_t out_event_size, size_t capacity); -shm_arbiter_buffer *shm_arbiter_buffer_create(shm_stream *stream, +vms_arbiter_buffer *vms_arbiter_buffer_create(vms_stream *stream, size_t out_event_size, size_t capacity); -size_t shm_arbiter_buffer_set_drop_space_threshold(shm_arbiter_buffer *buffer, +size_t vms_arbiter_buffer_set_drop_space_threshold(vms_arbiter_buffer *buffer, size_t thr); -void shm_arbiter_buffer_free(shm_arbiter_buffer *buffer); -void shm_arbiter_buffer_destroy(shm_arbiter_buffer *buffer); -void shm_arbiter_buffer_set_active(shm_arbiter_buffer *buffer, bool val); -size_t shm_arbiter_buffer_elem_size(shm_arbiter_buffer *q); -shm_stream *shm_arbiter_buffer_stream(shm_arbiter_buffer *buffer); -bool shm_arbiter_buffer_active(shm_arbiter_buffer *buffer); -size_t shm_arbiter_buffer_size(shm_arbiter_buffer *buffer); -size_t shm_arbiter_buffer_capacity(shm_arbiter_buffer *buffer); -size_t shm_arbiter_buffer_free_space(shm_arbiter_buffer *buffer); -size_t shm_arbiter_buffer_sizeof(void); +void vms_arbiter_buffer_free(vms_arbiter_buffer *buffer); +void vms_arbiter_buffer_destroy(vms_arbiter_buffer *buffer); +void vms_arbiter_buffer_set_active(vms_arbiter_buffer *buffer, bool val); +size_t vms_arbiter_buffer_elem_size(vms_arbiter_buffer *q); +vms_stream *vms_arbiter_buffer_stream(vms_arbiter_buffer *buffer); +bool vms_arbiter_buffer_active(vms_arbiter_buffer *buffer); +size_t vms_arbiter_buffer_size(vms_arbiter_buffer *buffer); +size_t vms_arbiter_buffer_capacity(vms_arbiter_buffer *buffer); +size_t vms_arbiter_buffer_free_space(vms_arbiter_buffer *buffer); +size_t vms_arbiter_buffer_sizeof(void); /* writer's API */ -void shm_arbiter_buffer_push(shm_arbiter_buffer *q, const void *elem, +void vms_arbiter_buffer_push(vms_arbiter_buffer *q, const void *elem, size_t size); /* cannot be mixed with push */ -void *shm_arbiter_buffer_write_ptr(shm_arbiter_buffer *q); -void shm_arbiter_buffer_write_finish(shm_arbiter_buffer *q); -void shm_arbiter_buffer_get_str(shm_arbiter_buffer *q, size_t elem); +void *vms_arbiter_buffer_write_ptr(vms_arbiter_buffer *q); +void vms_arbiter_buffer_write_finish(vms_arbiter_buffer *q); +void vms_arbiter_buffer_get_str(vms_arbiter_buffer *q, size_t elem); /* reader's API */ /* multiple threads can use top and peek if none of them uses drop/pop * at the time */ -shm_event *shm_arbiter_buffer_top(shm_arbiter_buffer *buffer); -size_t shm_arbiter_buffer_peek(shm_arbiter_buffer *buffer, size_t n, +vms_event *vms_arbiter_buffer_top(vms_arbiter_buffer *buffer); +size_t vms_arbiter_buffer_peek(vms_arbiter_buffer *buffer, size_t n, void **data1, size_t *size1, void **data2, size_t *size2); /* peek 1 event */ -size_t shm_arbiter_buffer_peek1(shm_arbiter_buffer *buffer, void **data); -size_t shm_arbiter_buffer_drop(shm_arbiter_buffer *buffer, size_t n); -size_t shm_arbiter_buffer_drop_older_than(shm_arbiter_buffer *buffer, - shm_eventid id); -bool shm_arbiter_buffer_pop(shm_arbiter_buffer *q, void *buff); +size_t vms_arbiter_buffer_peek1(vms_arbiter_buffer *buffer, void **data); +size_t vms_arbiter_buffer_drop(vms_arbiter_buffer *buffer, size_t n); +size_t vms_arbiter_buffer_drop_older_than(vms_arbiter_buffer *buffer, + vms_eventid id); +bool vms_arbiter_buffer_pop(vms_arbiter_buffer *q, void *buff); -void *stream_fetch(shm_stream *stream, shm_arbiter_buffer *buffer); +void *stream_fetch(vms_stream *stream, vms_arbiter_buffer *buffer); -void *stream_filter_fetch(shm_stream *stream, shm_arbiter_buffer *buffer, - shm_stream_filter_fn filter); +void *stream_filter_fetch(vms_stream *stream, vms_arbiter_buffer *buffer, + vms_stream_filter_fn filter); -bool shm_arbiter_buffer_is_done(shm_arbiter_buffer *buffer); +bool vms_arbiter_buffer_is_done(vms_arbiter_buffer *buffer); -size_t shm_arbiter_buffer_dropped_num(shm_arbiter_buffer *buffer); -size_t shm_arbiter_buffer_dropped_times(shm_arbiter_buffer *buffer); -size_t shm_arbiter_buffer_written_num(shm_arbiter_buffer *buffer); +size_t vms_arbiter_buffer_dropped_num(vms_arbiter_buffer *buffer); +size_t vms_arbiter_buffer_dropped_times(vms_arbiter_buffer *buffer); +size_t vms_arbiter_buffer_written_num(vms_arbiter_buffer *buffer); -void shm_arbiter_buffer_notify_dropped(shm_arbiter_buffer *buffer, +void vms_arbiter_buffer_notify_dropped(vms_arbiter_buffer *buffer, uint64_t begin_id, uint64_t end_id); -void shm_arbiter_buffer_dump_stats(shm_arbiter_buffer *buffer); +void vms_arbiter_buffer_dump_stats(vms_arbiter_buffer *buffer); #endif /* SHAMON_ARBITER_H_ */ diff --git a/include/vamos-buffers/core/event.h b/include/vamos-buffers/core/event.h index ac5e92d0..2a9de8d2 100644 --- a/include/vamos-buffers/core/event.h +++ b/include/vamos-buffers/core/event.h @@ -5,21 +5,21 @@ #include #include -typedef uint64_t shm_kind; -typedef uint64_t shm_eventid; -typedef struct _shm_stream shm_stream; +typedef uint64_t vms_kind; +typedef uint64_t vms_eventid; +typedef struct _vms_stream vms_stream; // TODO: make this opaque? but how to do the inheritance, then? Via // pointers to an interface? -typedef struct _shm_event { - shm_kind kind; - shm_eventid id; -} shm_event; +typedef struct _vms_event { + vms_kind kind; + vms_eventid id; +} vms_event; -typedef struct _shm_event_default_hole { - shm_event base; +typedef struct _vms_event_default_hole { + vms_event base; size_t n; /* number of dropped events */ -} shm_event_default_hole; +} vms_event_default_hole; /* Must be called before using event API. * It is called from shamon_create */ @@ -28,12 +28,12 @@ void initialize_events(void) __attribute__((deprecated)); void deinitialize_events(void) __attribute__((deprecated)); // EVENTS -shm_eventid shm_event_id(shm_event *event); -shm_kind shm_event_kind(shm_event *event); +vms_eventid vms_event_id(vms_event *event); +vms_kind vms_event_kind(vms_event *event); // DROP EVENT -bool shm_event_is_hole(shm_event *); -shm_kind shm_get_hole_kind(void); -shm_kind shm_get_last_special_kind(void); +bool vms_event_is_hole(vms_event *); +vms_kind vms_get_hole_kind(void); +vms_kind vms_get_last_special_kind(void); #endif // SHAMON_EVENT_H_ diff --git a/include/vamos-buffers/core/list-embedded.h b/include/vamos-buffers/core/list-embedded.h index 3d503409..f8cd044a 100644 --- a/include/vamos-buffers/core/list-embedded.h +++ b/include/vamos-buffers/core/list-embedded.h @@ -4,43 +4,43 @@ #include /* circular embedded doubly-linked list */ -typedef struct _shm_list_embedded { - struct _shm_list_embedded *next; - struct _shm_list_embedded *prev; -} shm_list_embedded; - -void shm_list_embedded_init(shm_list_embedded *list); -void shm_list_embedded_insert_after(shm_list_embedded *list, - shm_list_embedded *elm); -void shm_list_embedded_insert_list(shm_list_embedded *list, - shm_list_embedded *other); -void shm_list_embedded_remove(shm_list_embedded *elm); -size_t shm_list_embedded_size(const shm_list_embedded *list); -_Bool shm_list_embedded_empty(const shm_list_embedded *list); - -#define shm_list_embedded_object_of(ptr, tyobj, member) \ +typedef struct _vms_list_embedded { + struct _vms_list_embedded *next; + struct _vms_list_embedded *prev; +} vms_list_embedded; + +void vms_list_embedded_init(vms_list_embedded *list); +void vms_list_embedded_insert_after(vms_list_embedded *list, + vms_list_embedded *elm); +void vms_list_embedded_insert_list(vms_list_embedded *list, + vms_list_embedded *other); +void vms_list_embedded_remove(vms_list_embedded *elm); +size_t vms_list_embedded_size(const vms_list_embedded *list); +_Bool vms_list_embedded_empty(const vms_list_embedded *list); + +#define vms_list_embedded_object_of(ptr, tyobj, member) \ (__typeof__(tyobj))((char *)(ptr)-offsetof(__typeof__(*tyobj), member)) -#define shm_list_embedded_foreach(pos, head, member) \ - for (pos = shm_list_embedded_object_of((head)->next, pos, member); \ +#define vms_list_embedded_foreach(pos, head, member) \ + for (pos = vms_list_embedded_object_of((head)->next, pos, member); \ &pos->member != (head); \ - pos = shm_list_embedded_object_of(pos->member.next, pos, member)) + pos = vms_list_embedded_object_of(pos->member.next, pos, member)) -#define shm_list_embedded_foreach_safe(pos, tmp, head, member) \ - for (pos = shm_list_embedded_object_of((head)->next, pos, member), \ - tmp = shm_list_embedded_object_of((pos)->member.next, tmp, member); \ +#define vms_list_embedded_foreach_safe(pos, tmp, head, member) \ + for (pos = vms_list_embedded_object_of((head)->next, pos, member), \ + tmp = vms_list_embedded_object_of((pos)->member.next, tmp, member); \ &pos->member != (head); pos = tmp, \ - tmp = shm_list_embedded_object_of(pos->member.next, tmp, member)) + tmp = vms_list_embedded_object_of(pos->member.next, tmp, member)) -#define shm_list_embedded_foreach_reverse(pos, head, member) \ - for (pos = shm_list_embedded_object_of((head)->prev, pos, member); \ +#define vms_list_embedded_foreach_reverse(pos, head, member) \ + for (pos = vms_list_embedded_object_of((head)->prev, pos, member); \ &pos->member != (head); \ - pos = shm_list_embedded_object_of(pos->member.prev, pos, member)) + pos = vms_list_embedded_object_of(pos->member.prev, pos, member)) -#define shm_list_embedded_foreach_reverse_safe(pos, tmp, head, member) \ - for (pos = shm_list_embedded_object_of((head)->prev, pos, member), \ - tmp = shm_list_embedded_object_of((pos)->member.prev, tmp, member); \ +#define vms_list_embedded_foreach_reverse_safe(pos, tmp, head, member) \ + for (pos = vms_list_embedded_object_of((head)->prev, pos, member), \ + tmp = vms_list_embedded_object_of((pos)->member.prev, tmp, member); \ &pos->member != (head); pos = tmp, \ - tmp = shm_list_embedded_object_of(pos->member.prev, tmp, member)) + tmp = vms_list_embedded_object_of(pos->member.prev, tmp, member)) #endif /* SHAMON_LIST_EMBEDDED_H_ */ diff --git a/include/vamos-buffers/core/list.h b/include/vamos-buffers/core/list.h index d6a162a1..dae5ad06 100644 --- a/include/vamos-buffers/core/list.h +++ b/include/vamos-buffers/core/list.h @@ -4,33 +4,33 @@ #include /* non-circular doubly-linked-list */ -typedef struct _shm_list_elem { - struct _shm_list_elem *next, *prev; +typedef struct _vms_list_elem { + struct _vms_list_elem *next, *prev; void *data; -} shm_list_elem; +} vms_list_elem; -typedef struct _shm_list { - shm_list_elem *first; - shm_list_elem *last; +typedef struct _vms_list { + vms_list_elem *first; + vms_list_elem *last; size_t size; -} shm_list; +} vms_list; -typedef void(shm_list_elem_destroy_fn)(void *); +typedef void(vms_list_elem_destroy_fn)(void *); -void shm_list_init(shm_list *list); -void shm_list_destroy(shm_list *list, shm_list_elem_destroy_fn destroy); +void vms_list_init(vms_list *list); +void vms_list_destroy(vms_list *list, vms_list_elem_destroy_fn destroy); -size_t shm_list_prepend(shm_list *list, void *elem); -size_t shm_list_append(shm_list *list, void *elem); -size_t shm_list_append_elem(shm_list *list, shm_list_elem *elem); -size_t shm_list_insert_after(shm_list *list, shm_list_elem *elem, void *data); -size_t shm_list_insert_before(shm_list *list, shm_list_elem *elem, void *data); -size_t shm_list_insert_elem_after(shm_list *list, shm_list_elem *elem, - shm_list_elem *new); -size_t shm_list_remove(shm_list *list, shm_list_elem *elem); -size_t shm_list_size(shm_list *list); +size_t vms_list_prepend(vms_list *list, void *elem); +size_t vms_list_append(vms_list *list, void *elem); +size_t vms_list_append_elem(vms_list *list, vms_list_elem *elem); +size_t vms_list_insert_after(vms_list *list, vms_list_elem *elem, void *data); +size_t vms_list_insert_before(vms_list *list, vms_list_elem *elem, void *data); +size_t vms_list_insert_elem_after(vms_list *list, vms_list_elem *elem, + vms_list_elem *new); +size_t vms_list_remove(vms_list *list, vms_list_elem *elem); +size_t vms_list_size(vms_list *list); -shm_list_elem *shm_list_first(shm_list *list); -shm_list_elem *shm_list_last(shm_list *list); +vms_list_elem *vms_list_first(vms_list *list); +vms_list_elem *vms_list_last(vms_list *list); #endif /* SHAMON_LIST_H_ */ diff --git a/include/vamos-buffers/core/monitor.h b/include/vamos-buffers/core/monitor.h index a67fd08e..4a6bfdec 100644 --- a/include/vamos-buffers/core/monitor.h +++ b/include/vamos-buffers/core/monitor.h @@ -9,57 +9,57 @@ #include #include -typedef struct _shm_monitor_buffer shm_monitor_buffer; -typedef struct _shm_event shm_event; +typedef struct _vms_monitor_buffer vms_monitor_buffer; +typedef struct _vms_event vms_event; -void shm_monitor_buffer_init(shm_monitor_buffer *buffer, size_t event_size, +void vms_monitor_buffer_init(vms_monitor_buffer *buffer, size_t event_size, size_t capacity); -shm_monitor_buffer *shm_monitor_buffer_create(size_t event_size, +vms_monitor_buffer *vms_monitor_buffer_create(size_t event_size, size_t capacity); -void shm_monitor_set_finished(shm_monitor_buffer *buffer); +void vms_monitor_set_finished(vms_monitor_buffer *buffer); -void shm_monitor_buffer_free(shm_monitor_buffer *buffer); -void shm_monitor_buffer_destroy(shm_monitor_buffer *buffer); +void vms_monitor_buffer_free(vms_monitor_buffer *buffer); +void vms_monitor_buffer_destroy(vms_monitor_buffer *buffer); -size_t shm_monitor_buffer_elem_size(shm_monitor_buffer *q); -bool shm_monitor_buffer_active(shm_monitor_buffer *buffer); -size_t shm_monitor_buffer_size(shm_monitor_buffer *buffer); -size_t shm_monitor_buffer_capacity(shm_monitor_buffer *buffer); -size_t shm_monitor_buffer_free_space(shm_monitor_buffer *buffer); -// size_t shm_monitor_buffer_sizeof(void); +size_t vms_monitor_buffer_elem_size(vms_monitor_buffer *q); +bool vms_monitor_buffer_active(vms_monitor_buffer *buffer); +size_t vms_monitor_buffer_size(vms_monitor_buffer *buffer); +size_t vms_monitor_buffer_capacity(vms_monitor_buffer *buffer); +size_t vms_monitor_buffer_free_space(vms_monitor_buffer *buffer); +// size_t vms_monitor_buffer_sizeof(void); /* writer's API */ /* Get write pointer to the buffer, block until space is available */ -void *shm_monitor_buffer_write_ptr(shm_monitor_buffer *q); +void *vms_monitor_buffer_write_ptr(vms_monitor_buffer *q); /* Get write pointer to the buffer or null if no space is available */ -void *shm_monitor_buffer_write_ptr_or_null(shm_monitor_buffer *q); -void shm_monitor_buffer_write_finish(shm_monitor_buffer *q); -/*void shm_monitor_buffer_get_str(shm_monitor_buffer *q, size_t elem); */ +void *vms_monitor_buffer_write_ptr_or_null(vms_monitor_buffer *q); +void vms_monitor_buffer_write_finish(vms_monitor_buffer *q); +/*void vms_monitor_buffer_get_str(vms_monitor_buffer *q, size_t elem); */ /* reader's API */ -void *fetch_arbiter_stream(shm_monitor_buffer *buffer); -void shm_monitor_buffer_consume(shm_monitor_buffer *buffer, size_t n); +void *fetch_arbiter_stream(vms_monitor_buffer *buffer); +void vms_monitor_buffer_consume(vms_monitor_buffer *buffer, size_t n); /* multiple threads can use top and peek if none of them uses drop/pop * at the time -shm_event *shm_monitor_buffer_top(shm_monitor_buffer *buffer); -size_t shm_monitor_buffer_peek(shm_monitor_buffer *buffer, size_t n, +vms_event *vms_monitor_buffer_top(vms_monitor_buffer *buffer); +size_t vms_monitor_buffer_peek(vms_monitor_buffer *buffer, size_t n, void **data1, size_t *size1, void **data2, size_t *size2); */ /* peek 1 event -size_t shm_monitor_buffer_peek1(shm_monitor_buffer *buffer, void **data); -size_t shm_monitor_buffer_drop(shm_monitor_buffer *buffer, size_t n); -bool shm_monitor_buffer_pop(shm_monitor_buffer *q, void *buff); +size_t vms_monitor_buffer_peek1(vms_monitor_buffer *buffer, void **data); +size_t vms_monitor_buffer_drop(vms_monitor_buffer *buffer, size_t n); +bool vms_monitor_buffer_pop(vms_monitor_buffer *q, void *buff); -void *stream_fetch(shm_stream *stream, shm_monitor_buffer *buffer); +void *stream_fetch(vms_stream *stream, vms_monitor_buffer *buffer); -void *stream_filter_fetch(shm_stream *stream, shm_monitor_buffer *buffer, - shm_stream_filter_fn filter); +void *stream_filter_fetch(vms_stream *stream, vms_monitor_buffer *buffer, + vms_stream_filter_fn filter); */ /* -void shm_monitor_buffer_notify_dropped(shm_monitor_buffer *buffer, +void vms_monitor_buffer_notify_dropped(vms_monitor_buffer *buffer, uint64_t begin_id, uint64_t end_id); */ diff --git a/include/vamos-buffers/core/par_queue.h b/include/vamos-buffers/core/par_queue.h index 78b71325..c71b34e5 100644 --- a/include/vamos-buffers/core/par_queue.h +++ b/include/vamos-buffers/core/par_queue.h @@ -6,46 +6,46 @@ #include "spsc_ringbuf.h" -typedef struct _shm_event shm_event; +typedef struct _vms_event vms_event; /** - * shm_par_queue is a single-producer single-consumer (SPSC) lock-free + * vms_par_queue is a single-producer single-consumer (SPSC) lock-free * concurrent queue. */ -typedef struct _shm_par_queue { - shm_spsc_ringbuf ringbuf; +typedef struct _vms_par_queue { + vms_spsc_ringbuf ringbuf; size_t elem_size; size_t capacity; unsigned char *data; char __padding[CACHELINE_SIZE - 2 * sizeof(size_t) - sizeof(unsigned char *)]; -} shm_par_queue; - -void shm_par_queue_init(shm_par_queue *q, size_t capacity, size_t elem_size); -void shm_par_queue_destroy(shm_par_queue *q); -bool shm_par_queue_push(shm_par_queue *q, const void *elem, size_t size); -bool shm_par_queue_pop(shm_par_queue *q, void *buff); -void shm_par_queue_drop(shm_par_queue *q, size_t k); -size_t shm_par_queue_size(shm_par_queue *q); -size_t shm_par_queue_elem_size(shm_par_queue *q); -size_t shm_par_queue_capacity(shm_par_queue *q); -size_t shm_par_queue_free_num(shm_par_queue *q); -shm_event *shm_par_queue_top(shm_par_queue *q); -size_t shm_par_queue_peek(shm_par_queue *q, size_t n, void **ptr1, size_t *len1, +} vms_par_queue; + +void vms_par_queue_init(vms_par_queue *q, size_t capacity, size_t elem_size); +void vms_par_queue_destroy(vms_par_queue *q); +bool vms_par_queue_push(vms_par_queue *q, const void *elem, size_t size); +bool vms_par_queue_pop(vms_par_queue *q, void *buff); +void vms_par_queue_drop(vms_par_queue *q, size_t k); +size_t vms_par_queue_size(vms_par_queue *q); +size_t vms_par_queue_elem_size(vms_par_queue *q); +size_t vms_par_queue_capacity(vms_par_queue *q); +size_t vms_par_queue_free_num(vms_par_queue *q); +vms_event *vms_par_queue_top(vms_par_queue *q); +size_t vms_par_queue_peek(vms_par_queue *q, size_t n, void **ptr1, size_t *len1, void **ptr2, size_t *len2); /* peek 1 event */ -size_t shm_par_queue_peek1(shm_par_queue *q, void **data); +size_t vms_par_queue_peek1(vms_par_queue *q, void **data); /* peek at a given element. k = 0 is the first elem (top), k = 1 the second, ... */ -shm_event *shm_par_queue_peek_at(shm_par_queue *q, size_t k); +vms_event *vms_par_queue_peek_at(vms_par_queue *q, size_t k); /* peek at a given element or the last possible readable element * if there is not enough elements. 'k' is set to the index (starting from 0) * of the peeked element. Returns NULL only if the queue is empty. */ -shm_event *shm_par_queue_peek_atmost_at(shm_par_queue *q, size_t *k); +vms_event *vms_par_queue_peek_atmost_at(vms_par_queue *q, size_t *k); -void *shm_par_queue_write_ptr(shm_par_queue *q); -void shm_par_queue_write_finish(shm_par_queue *q); +void *vms_par_queue_write_ptr(vms_par_queue *q); +void vms_par_queue_write_finish(vms_par_queue *q); #endif /* SHAMON_PARALLEL_QUEUE_H */ diff --git a/include/vamos-buffers/core/queue_spsc.h b/include/vamos-buffers/core/queue_spsc.h index b98985f7..804e4f71 100644 --- a/include/vamos-buffers/core/queue_spsc.h +++ b/include/vamos-buffers/core/queue_spsc.h @@ -4,13 +4,13 @@ #include #include -typedef struct _shm_event shm_event; +typedef struct _vms_event vms_event; /** - * shm_queue_spsc is a single-producer single-consumer (SPSC) lock-free + * vms_queue_spsc is a single-producer single-consumer (SPSC) lock-free * concurrent queue. */ -typedef struct _shm_queue_spsc { +typedef struct _vms_queue_spsc { size_t capacity; /* modifications of this variable are going to be atomic, * but that is the all atomicity that we need! */ @@ -21,52 +21,52 @@ typedef struct _shm_queue_spsc { size_t partial_head; size_t writing_k_elems; #endif -} shm_queue_spsc; +} vms_queue_spsc; -void shm_queue_spsc_init(shm_queue_spsc *q, size_t capacity); -void shm_queue_spsc_destroy(shm_queue_spsc *q); +void vms_queue_spsc_init(vms_queue_spsc *q, size_t capacity); +void vms_queue_spsc_destroy(vms_queue_spsc *q); /* ask to write `k` elements, returns the offset where to start writing and saves to `k` the actual number of elements that can be written continuously from that offset (i.e., if *k == 0, then the queue is empty) -size_t shm_queue_spsc_write_offsets(shm_queue_spsc *q, size_t k, +size_t vms_queue_spsc_write_offsets(vms_queue_spsc *q, size_t k, size_t *len, size_t *wrapped_len); */ /* get next offset for writing the next element. \return true if * the write is possible, or false (which meanst that the queue is full) */ -bool shm_queue_spsc_write_offset(shm_queue_spsc *q, size_t *offset); +bool vms_queue_spsc_write_offset(vms_queue_spsc *q, size_t *offset); /* notify queue that a write was finished and that `written_num` * offsets were used */ -void shm_queue_spsc_writes_finish(shm_queue_spsc *q, size_t written_num); +void vms_queue_spsc_writes_finish(vms_queue_spsc *q, size_t written_num); /* notify queue that a single write was finished -- must match a single call of - * shm_queue_spcs_write_offset (calls to shm_queue_spcs_write_offsets must be - * "finished" by shm_queue_spcs_writes_finish */ -void shm_queue_spsc_write_finish(shm_queue_spsc *q); + * vms_queue_spcs_write_offset (calls to vms_queue_spcs_write_offsets must be + * "finished" by vms_queue_spcs_writes_finish */ +void vms_queue_spsc_write_finish(vms_queue_spsc *q); -size_t shm_queue_spsc_consume(shm_queue_spsc *q, size_t k); +size_t vms_queue_spsc_consume(vms_queue_spsc *q, size_t k); -size_t shm_queue_spsc_capacity(shm_queue_spsc *q); -size_t shm_queue_spsc_size(shm_queue_spsc *q); -size_t shm_queue_spsc_free_num(shm_queue_spsc *q); +size_t vms_queue_spsc_capacity(vms_queue_spsc *q); +size_t vms_queue_spsc_size(vms_queue_spsc *q); +size_t vms_queue_spsc_free_num(vms_queue_spsc *q); /* returns the number of elements in the queue. Note that it is not safe to access the whole range of offsets from [offset, offset+retval] as there can be a wrap-around. Either count with the possible wrap-around, - or use shm_queue_spsc_peek() when reading more than one offset. */ -size_t shm_queue_spsc_read_offset(shm_queue_spsc *q, size_t *offset); + or use vms_queue_spsc_peek() when reading more than one offset. */ +size_t vms_queue_spsc_read_offset(vms_queue_spsc *q, size_t *offset); -size_t shm_queue_spsc_peek(shm_queue_spsc *q, size_t n, size_t *off1, +size_t vms_queue_spsc_peek(vms_queue_spsc *q, size_t n, size_t *off1, size_t *len1, size_t *off2, size_t *len2); /* peek at a given element. k = 0 is the first elem (top), k = 1 the second, ... */ -size_t shm_queue_spsc_peek_at(shm_queue_spsc *q, size_t k); +size_t vms_queue_spsc_peek_at(vms_queue_spsc *q, size_t k); /* peek at a given element or the last possible readable element * if there is not enough elements. 'k' is set to the index (starting from 0) * of the peeked element. Returns the number of elements in the queue. */ -size_t shm_queue_spsc_peek_atmost_at(shm_queue_spsc *q, size_t *k, +size_t vms_queue_spsc_peek_atmost_at(vms_queue_spsc *q, size_t *k, size_t *offset); #endif /* SHAMON_QUEUE_SPSC_H */ diff --git a/include/vamos-buffers/core/shamon.h b/include/vamos-buffers/core/shamon.h index a0e940a1..94e26b17 100644 --- a/include/vamos-buffers/core/shamon.h +++ b/include/vamos-buffers/core/shamon.h @@ -5,12 +5,12 @@ #include typedef struct _shamon shamon; -typedef struct _shm_event shm_event; -typedef struct _shm_stream shm_stream; -typedef struct _shm_vector shm_vector; +typedef struct _vms_event vms_event; +typedef struct _vms_stream vms_stream; +typedef struct _vms_vector vms_vector; -typedef shm_event *(*shamon_process_events_fn)(shm_vector *buffers, void *data, - shm_stream **); +typedef vms_event *(*shamon_process_events_fn)(vms_vector *buffers, void *data, + vms_stream **); shamon *shamon_create(shamon_process_events_fn process_events, void *process_events_data); @@ -19,10 +19,10 @@ bool shamon_is_ready(shamon *); /* for error handling only... */ void shamon_detach(shamon *shmn); -void shamon_add_stream(shamon *shmn, shm_stream *stream, +void shamon_add_stream(shamon *shmn, vms_stream *stream, size_t buffer_capacity); -shm_event *shamon_get_next_ev(shamon *, shm_stream **); -shm_vector *shamon_get_buffers(shamon *); -shm_stream **shamon_get_streams(shamon *, size_t *); +vms_event *shamon_get_next_ev(shamon *, vms_stream **); +vms_vector *shamon_get_buffers(shamon *); +vms_stream **shamon_get_streams(shamon *, size_t *); #endif // SHAMON_H_ diff --git a/include/vamos-buffers/core/shm_string.h b/include/vamos-buffers/core/shm_string.h deleted file mode 100644 index b28a2b5c..00000000 --- a/include/vamos-buffers/core/shm_string.h +++ /dev/null @@ -1,36 +0,0 @@ -#ifndef SHAMON_STRING_H_ -#define SHAMON_STRING_H_ - -#include - -#include "shm_string-macro.h" - -typedef struct _shm_string_ref { - size_t size; - const char *data; -} shm_string_ref; - -typedef struct _shm_string { - STRING(data); -} shm_string; - -/* Either the user can manipulate the string manually, - * with the macros from shm_string-macro.h - * or with the functions below. The macros are used - on the data of the string, e.g., - STRING_RESIZE(s->data). Note that macros are not applicable - always (they just expand to code that may be syntactically wrong - in some contexts). */ -void shm_string_init(shm_string *s); -void shm_string_destroy(shm_string *s); -void shm_string_clear(shm_string *s); -size_t shm_string_append(shm_string *s, char c); -char *shm_string_extend(shm_string *s); -size_t shm_string_pop(shm_string *s); -size_t shm_string_size(shm_string *s); -char *shm_string_at_ptr(shm_string *s, ssize_t idx); -char shm_string_at(shm_string *s, ssize_t idx); -void shm_string_swap(shm_string *s, shm_string *with); -void shm_string_grow(shm_string *s, size_t size); - -#endif /* SHAMON_VECTOR_H_ */ diff --git a/include/vamos-buffers/core/source.h b/include/vamos-buffers/core/source.h index 746102cf..32af5544 100644 --- a/include/vamos-buffers/core/source.h +++ b/include/vamos-buffers/core/source.h @@ -5,7 +5,7 @@ struct event_record { char name[64]; - shm_kind kind; + vms_kind kind; size_t size; unsigned char signature[32]; }; diff --git a/include/vamos-buffers/core/spsc_ringbuf.h b/include/vamos-buffers/core/spsc_ringbuf.h index 83e0e9d1..402513e6 100644 --- a/include/vamos-buffers/core/spsc_ringbuf.h +++ b/include/vamos-buffers/core/spsc_ringbuf.h @@ -10,7 +10,7 @@ /** * Single-producer single-consumer (SPSC) lock-free concurrent ring-buffer. */ -typedef struct _shm_spsc_ringbuf { +typedef struct _vms_spsc_ringbuf { /* capacity needs to be also aligned as it is read by both reader and writer */ /* TODO: cache capacity locally (do a proxy to acces the ringbuf? @@ -35,30 +35,30 @@ typedef struct _shm_spsc_ringbuf { size_t n; } write_in_progress; #endif -} shm_spsc_ringbuf; +} vms_spsc_ringbuf; -void shm_spsc_ringbuf_init(shm_spsc_ringbuf *b, size_t capacity); -void shm_spsc_ringbuf_consume(shm_spsc_ringbuf *b, size_t n); -size_t shm_spsc_ringbuf_consume_upto(shm_spsc_ringbuf *b, size_t n); -size_t shm_spsc_ringbuf_write_off(shm_spsc_ringbuf *b, size_t *n, +void vms_spsc_ringbuf_init(vms_spsc_ringbuf *b, size_t capacity); +void vms_spsc_ringbuf_consume(vms_spsc_ringbuf *b, size_t n); +size_t vms_spsc_ringbuf_consume_upto(vms_spsc_ringbuf *b, size_t n); +size_t vms_spsc_ringbuf_write_off(vms_spsc_ringbuf *b, size_t *n, size_t *wrap_n); -size_t shm_spsc_ringbuf_write_off_nowrap(shm_spsc_ringbuf *b, size_t *n); -size_t shm_spsc_ringbuf_acquire(shm_spsc_ringbuf *b, size_t *n, size_t *wrap); -size_t shm_spsc_ringbuf_acquire_nowrap(shm_spsc_ringbuf *b, size_t *n); -void shm_spsc_ringbuf_write_finish(shm_spsc_ringbuf *q, size_t n); +size_t vms_spsc_ringbuf_write_off_nowrap(vms_spsc_ringbuf *b, size_t *n); +size_t vms_spsc_ringbuf_acquire(vms_spsc_ringbuf *b, size_t *n, size_t *wrap); +size_t vms_spsc_ringbuf_acquire_nowrap(vms_spsc_ringbuf *b, size_t *n); +void vms_spsc_ringbuf_write_finish(vms_spsc_ringbuf *q, size_t n); -size_t shm_spsc_ringbuf_size(shm_spsc_ringbuf *b); -size_t shm_spsc_ringbuf_max_size(shm_spsc_ringbuf *b); +size_t vms_spsc_ringbuf_size(vms_spsc_ringbuf *b); +size_t vms_spsc_ringbuf_max_size(vms_spsc_ringbuf *b); /* capacity == max_size + 1, because we use one element as a separator. Capacity - 1 is the maximal offset that the ringbuf considers. */ -size_t shm_spsc_ringbuf_capacity(shm_spsc_ringbuf *b); -size_t shm_spsc_ringbuf_free_num(shm_spsc_ringbuf *b); -bool shm_spsc_ringbuf_full(shm_spsc_ringbuf *b); -bool shm_spsc_ringbuf_empty(shm_spsc_ringbuf *b); +size_t vms_spsc_ringbuf_capacity(vms_spsc_ringbuf *b); +size_t vms_spsc_ringbuf_free_num(vms_spsc_ringbuf *b); +bool vms_spsc_ringbuf_full(vms_spsc_ringbuf *b); +bool vms_spsc_ringbuf_empty(vms_spsc_ringbuf *b); -size_t shm_spsc_ringbuf_read_off_nowrap(shm_spsc_ringbuf *b, size_t *n); -size_t shm_spsc_ringbuf_read_acquire(shm_spsc_ringbuf *b, size_t *n); -void *shm_spsc_ringbuf_top(shm_spsc_ringbuf *b); -size_t shm_spsc_ringbuf_peek(shm_spsc_ringbuf *b, size_t n, size_t *off, +size_t vms_spsc_ringbuf_read_off_nowrap(vms_spsc_ringbuf *b, size_t *n); +size_t vms_spsc_ringbuf_read_acquire(vms_spsc_ringbuf *b, size_t *n); +void *vms_spsc_ringbuf_top(vms_spsc_ringbuf *b); +size_t vms_spsc_ringbuf_peek(vms_spsc_ringbuf *b, size_t n, size_t *off, size_t *len1, size_t *len2); #endif /* SHAMON_SPSC_RINGBUF_H */ diff --git a/include/vamos-buffers/core/stream.h b/include/vamos-buffers/core/stream.h index 1edc28d2..8be81e34 100644 --- a/include/vamos-buffers/core/stream.h +++ b/include/vamos-buffers/core/stream.h @@ -5,26 +5,26 @@ #include "source.h" #include "vector-macro.h" -typedef struct _shm_arbiter_buffer shm_arbiter_buffer; +typedef struct _vms_arbiter_buffer vms_arbiter_buffer; -typedef size_t (*shm_stream_buffer_events_fn)(struct _shm_stream *, - shm_arbiter_buffer *buffer); -typedef bool (*shm_stream_is_ready_fn)(struct _shm_stream *); -typedef void (*shm_stream_destroy_fn)(struct _shm_stream *); +typedef size_t (*vms_stream_buffer_events_fn)(struct _vms_stream *, + vms_arbiter_buffer *buffer); +typedef bool (*vms_stream_is_ready_fn)(struct _vms_stream *); +typedef void (*vms_stream_destroy_fn)(struct _vms_stream *); -typedef bool (*shm_stream_filter_fn)(shm_stream *, shm_event *); -typedef void (*shm_stream_alter_fn)(shm_stream *, shm_event *, shm_event *); -typedef void (*shm_stream_hole_init_fn)(shm_event *); -typedef void (*shm_stream_hole_update_fn)(shm_event *, shm_event *); +typedef bool (*vms_stream_filter_fn)(vms_stream *, vms_event *); +typedef void (*vms_stream_alter_fn)(vms_stream *, vms_event *, vms_event *); +typedef void (*vms_stream_hole_init_fn)(vms_event *); +typedef void (*vms_stream_hole_update_fn)(vms_event *, vms_event *); -typedef struct _shm_stream_hole_handling { +typedef struct _vms_stream_hole_handling { size_t hole_event_size; - shm_stream_hole_init_fn init; - shm_stream_hole_update_fn update; -} shm_stream_hole_handling; + vms_stream_hole_init_fn init; + vms_stream_hole_update_fn update; +} vms_stream_hole_handling; // TODO: make this opaque -typedef struct _shm_stream { +typedef struct _vms_stream { uint64_t id; char *name; char *type; @@ -37,14 +37,14 @@ typedef struct _shm_stream { /* cached info about events in 'incoming_events_buffer' */ struct event_record *events_cache; /* callbacks */ - shm_stream_is_ready_fn is_ready; - shm_stream_filter_fn filter; - shm_stream_alter_fn alter; - shm_stream_destroy_fn destroy; - shm_stream_hole_handling hole_handling; + vms_stream_is_ready_fn is_ready; + vms_stream_filter_fn filter; + vms_stream_alter_fn alter; + vms_stream_destroy_fn destroy; + vms_stream_hole_handling hole_handling; /* substreams of this stream and the link to the parent */ - shm_stream *parent_stream; - VEC(substreams, struct _shm_stream *); + vms_stream *parent_stream; + VEC(substreams, struct _vms_stream *); #ifndef NDEBUG /* for checking consistency */ size_t last_event_id; @@ -56,69 +56,69 @@ typedef struct _shm_stream { size_t dropped_events; size_t slept_waiting_for_ev; #endif -} shm_stream; +} vms_stream; -void shm_stream_init(shm_stream *stream, struct buffer *incoming_events_buffer, - size_t event_size, shm_stream_is_ready_fn is_ready, - shm_stream_filter_fn filter, shm_stream_alter_fn alter, - shm_stream_destroy_fn destroy, - const shm_stream_hole_handling *hole_handling, +void vms_stream_init(vms_stream *stream, struct buffer *incoming_events_buffer, + size_t event_size, vms_stream_is_ready_fn is_ready, + vms_stream_filter_fn filter, vms_stream_alter_fn alter, + vms_stream_destroy_fn destroy, + const vms_stream_hole_handling *hole_handling, const char *const type, const char *const name); -const char *shm_stream_get_name(shm_stream *); -const char *shm_stream_get_type(shm_stream *); -size_t shm_stream_event_size(shm_stream *); -size_t shm_stream_id(shm_stream *); +const char *vms_stream_get_name(vms_stream *); +const char *vms_stream_get_type(vms_stream *); +size_t vms_stream_event_size(vms_stream *); +size_t vms_stream_id(vms_stream *); -struct event_record *shm_stream_get_avail_events(shm_stream *, size_t *); -struct event_record *shm_stream_get_event_record(shm_stream *, shm_kind); -struct event_record *shm_stream_get_event_record_no_cache(shm_stream *, - shm_kind); +struct event_record *vms_stream_get_avail_events(vms_stream *, size_t *); +struct event_record *vms_stream_get_event_record(vms_stream *, vms_kind); +struct event_record *vms_stream_get_event_record_no_cache(vms_stream *, + vms_kind); /* register to receiving events particular events */ -int shm_stream_register_event(shm_stream *, const char *, shm_kind); -int shm_stream_register_events(shm_stream *, size_t sz, ...); -int shm_stream_register_all_events(shm_stream *); +int vms_stream_register_event(vms_stream *, const char *, vms_kind); +int vms_stream_register_events(vms_stream *, size_t sz, ...); +int vms_stream_register_all_events(vms_stream *); -_Bool shm_stream_has_new_substreams(shm_stream *stream); -_Bool shm_stream_is_substream(shm_stream *stream); +_Bool vms_stream_has_new_substreams(vms_stream *stream); +_Bool vms_stream_is_substream(vms_stream *stream); -shm_stream *shm_stream_create_substream( - shm_stream *stream, shm_stream_is_ready_fn is_ready, - shm_stream_filter_fn filter, shm_stream_alter_fn alter, - shm_stream_destroy_fn destroy, shm_stream_hole_handling *hole_handling); +vms_stream *vms_stream_create_substream( + vms_stream *stream, vms_stream_is_ready_fn is_ready, + vms_stream_filter_fn filter, vms_stream_alter_fn alter, + vms_stream_destroy_fn destroy, vms_stream_hole_handling *hole_handling); /* the number of elements in the (shared memory) buffer of the stream */ -size_t shm_stream_buffer_size(shm_stream *); +size_t vms_stream_buffer_size(vms_stream *); /* the capacity the (shared memory) buffer of the stream */ -size_t shm_stream_buffer_capacity(shm_stream *); +size_t vms_stream_buffer_capacity(vms_stream *); -void *shm_stream_read_events(shm_stream *, size_t *); -bool shm_stream_consume(shm_stream *stream, size_t num); -const char *shm_stream_get_str(shm_stream *stream, uint64_t elem); +void *vms_stream_read_events(vms_stream *, size_t *); +bool vms_stream_consume(vms_stream *stream, size_t num); +const char *vms_stream_get_str(vms_stream *stream, uint64_t elem); -void shm_stream_notify_last_processed_id(shm_stream *stream, shm_eventid id); -bool shm_stream_is_ready(shm_stream *); -void shm_stream_attach(shm_stream *stream); -void shm_stream_detach(shm_stream *stream); +void vms_stream_notify_last_processed_id(vms_stream *stream, vms_eventid id); +bool vms_stream_is_ready(vms_stream *); +void vms_stream_attach(vms_stream *stream); +void vms_stream_detach(vms_stream *stream); -void shm_stream_prepare_hole_event(shm_stream *stream, shm_event *ev, size_t id, +void vms_stream_prepare_hole_event(vms_stream *stream, vms_event *ev, size_t id, uint64_t n); /* * returns true if there will come no events in the future, false otherwise * */ -bool shm_stream_is_finished(shm_stream *); +bool vms_stream_is_finished(vms_stream *); -void shm_stream_notify_dropped(shm_stream *stream, uint64_t begin_id, +void vms_stream_notify_dropped(vms_stream *stream, uint64_t begin_id, uint64_t end_id); -void shm_stream_destroy(shm_stream *stream); +void vms_stream_destroy(vms_stream *stream); #ifndef NDEBUG -inline size_t shm_stream_last_event_id(shm_stream *s) { +inline size_t vms_stream_last_event_id(vms_stream *s) { return s->last_event_id; } #endif -void shm_stream_dump_events(shm_stream *stream); +void vms_stream_dump_events(vms_stream *stream); #endif // SHAMON_STREAMS_H diff --git a/include/vamos-buffers/core/vector-aligned.h b/include/vamos-buffers/core/vector-aligned.h index 1e14ac89..f082518c 100644 --- a/include/vamos-buffers/core/vector-aligned.h +++ b/include/vamos-buffers/core/vector-aligned.h @@ -5,19 +5,19 @@ #include "vector.h" -/* shm_vector that allocates aligned memory */ -typedef struct _shm_vector_aligned { - shm_vector vec; +/* vms_vector that allocates aligned memory */ +typedef struct _vms_vector_aligned { + vms_vector vec; size_t alignment; -} shm_vector_aligned; +} vms_vector_aligned; -void shm_vector_aligned_init(shm_vector_aligned *vec, size_t elem_size, +void vms_vector_aligned_init(vms_vector_aligned *vec, size_t elem_size, size_t alignment); -size_t shm_vector_aligned_push(shm_vector *vec, void *elem); -void *shm_vector_aligned_extend(shm_vector *vec); -void shm_vector_aligned_swap(shm_vector *vec, shm_vector *with); -void shm_vector_aligned_resize(shm_vector *vec, size_t size); +size_t vms_vector_aligned_push(vms_vector *vec, void *elem); +void *vms_vector_aligned_extend(vms_vector *vec); +void vms_vector_aligned_swap(vms_vector *vec, vms_vector *with); +void vms_vector_aligned_resize(vms_vector *vec, size_t size); -/* the rest of functions is inherited from shm_vector, use those */ +/* the rest of functions is inherited from vms_vector, use those */ #endif /* SHAMON_VECTOR_ALIGNED_H_ */ diff --git a/include/vamos-buffers/core/vector-macro.h b/include/vamos-buffers/core/vector-macro.h index 5456f323..eec04449 100644 --- a/include/vamos-buffers/core/vector-macro.h +++ b/include/vamos-buffers/core/vector-macro.h @@ -46,7 +46,7 @@ * * VEC() can be usually also used in struct declarations, but not always. * For example if a struct is opaque and the vector must be accessed from - * outside. In such cases, we provide `shm_vector` data type. + * outside. In such cases, we provide `vms_vector` data type. */ #include diff --git a/include/vamos-buffers/core/vector.h b/include/vamos-buffers/core/vector.h index e582fa70..0d5e56de 100644 --- a/include/vamos-buffers/core/vector.h +++ b/include/vamos-buffers/core/vector.h @@ -10,18 +10,18 @@ #include -typedef struct _shm_vector { +typedef struct _vms_vector { size_t size; size_t element_size; size_t alloc_size; void *data; -} shm_vector; +} vms_vector; -void shm_vector_init(shm_vector *vec, size_t elem_size); -void shm_vector_destroy(shm_vector *vec); +void vms_vector_init(vms_vector *vec, size_t elem_size); +void vms_vector_destroy(vms_vector *vec); -size_t shm_vector_size(shm_vector *vec); -size_t shm_vector_elem_size(shm_vector *vec); +size_t vms_vector_size(vms_vector *vec); +size_t vms_vector_elem_size(vms_vector *vec); /** @@ -30,13 +30,13 @@ size_t shm_vector_elem_size(shm_vector *vec); * In debug mode, OOB checks are performed. * In release mode, if `idx` is OOB, the behavior is undefined. */ -void *shm_vector_at(shm_vector *vec, size_t idx); +void *vms_vector_at(vms_vector *vec, size_t idx); /** * Return the pointer to the element at index `idx`. * If `idx` is OOB, return NULL. */ -void *shm_vector_at_checked(shm_vector *vec, size_t idx); +void *vms_vector_at_checked(vms_vector *vec, size_t idx); /** @@ -44,7 +44,7 @@ void *shm_vector_at_checked(shm_vector *vec, size_t idx); * Memory is reallocated if needed. By default, the new memory can store * 10 more elements than the previously allocated memory. */ -size_t shm_vector_push(shm_vector *vec, void *elem); +size_t vms_vector_push(vms_vector *vec, void *elem); /** @@ -53,31 +53,31 @@ size_t shm_vector_push(shm_vector *vec, void *elem); * The returned pointer should be used to write the element into the vector, * e.g.: * - * int *ptr = shm_vector_extend(vec); + * int *ptr = vms_vector_extend(vec); * *ptr = 1; * * If there is not enough space to write a single element, the memory is * reallocated, by default to hold 10 new elements. */ -void *shm_vector_extend(shm_vector *vec); +void *vms_vector_extend(vms_vector *vec); /** * Remove the last element from the vector (decrease its size by 1) */ -size_t shm_vector_pop(shm_vector *vec); +size_t vms_vector_pop(vms_vector *vec); /** * Return the pointer to the top element or NULL * if the vector is empty. */ -void *shm_vector_top(shm_vector *vec); +void *vms_vector_top(vms_vector *vec); /** * Swap the contents of two vectors. */ -void shm_vector_swap(shm_vector *vec, shm_vector *with); +void vms_vector_swap(vms_vector *vec, vms_vector *with); /** @@ -93,6 +93,6 @@ void shm_vector_swap(shm_vector *vec, shm_vector *with); * as the user knows only that there is space for `size` elements * but nothing more. */ -void shm_vector_resize(shm_vector *vec, size_t size); +void vms_vector_resize(vms_vector *vec, size_t size); #endif /* SHAMON_VECTOR_H_ */ diff --git a/include/vamos-buffers/core/shm_string-macro.h b/include/vamos-buffers/core/vms_string-macro.h similarity index 100% rename from include/vamos-buffers/core/shm_string-macro.h rename to include/vamos-buffers/core/vms_string-macro.h diff --git a/include/vamos-buffers/core/vms_string.h b/include/vamos-buffers/core/vms_string.h new file mode 100644 index 00000000..6c7b495d --- /dev/null +++ b/include/vamos-buffers/core/vms_string.h @@ -0,0 +1,36 @@ +#ifndef SHAMON_STRING_H_ +#define SHAMON_STRING_H_ + +#include + +#include "vms_string-macro.h" + +typedef struct _vms_string_ref { + size_t size; + const char *data; +} vms_string_ref; + +typedef struct _vms_string { + STRING(data); +} vms_string; + +/* Either the user can manipulate the string manually, + * with the macros from vms_string-macro.h + * or with the functions below. The macros are used + on the data of the string, e.g., + STRING_RESIZE(s->data). Note that macros are not applicable + always (they just expand to code that may be syntactically wrong + in some contexts). */ +void vms_string_init(vms_string *s); +void vms_string_destroy(vms_string *s); +void vms_string_clear(vms_string *s); +size_t vms_string_append(vms_string *s, char c); +char *vms_string_extend(vms_string *s); +size_t vms_string_pop(vms_string *s); +size_t vms_string_size(vms_string *s); +char *vms_string_at_ptr(vms_string *s, ssize_t idx); +char vms_string_at(vms_string *s, ssize_t idx); +void vms_string_swap(vms_string *s, vms_string *with); +void vms_string_grow(vms_string *s, size_t size); + +#endif /* SHAMON_VECTOR_H_ */ diff --git a/include/vamos-buffers/shmbuf/buffer.h b/include/vamos-buffers/shmbuf/buffer.h index a7ab8555..1943a24d 100644 --- a/include/vamos-buffers/shmbuf/buffer.h +++ b/include/vamos-buffers/shmbuf/buffer.h @@ -79,23 +79,23 @@ int buffer_register_all_events(struct buffer *b); * dbg buffers */ -typedef struct _vms_shm_dbg_buffer vms_shm_dbg_buffer; -vms_shm_dbg_buffer *vms_shm_dbg_buffer_create(const char *key, size_t capacity, +typedef struct _vms_vms_dbg_buffer vms_vms_dbg_buffer; +vms_vms_dbg_buffer *vms_vms_dbg_buffer_create(const char *key, size_t capacity, uint16_t key_size, uint16_t element_size); -vms_shm_dbg_buffer *vms_shm_dbg_buffer_get(const char *key); -void vms_shm_dbg_buffer_release(vms_shm_dbg_buffer *); -void vms_shm_dbg_buffer_destroy(vms_shm_dbg_buffer *); - -size_t vms_shm_dbg_buffer_size(vms_shm_dbg_buffer *b); -size_t vms_shm_dbg_buffer_capacity(vms_shm_dbg_buffer *b); -size_t vms_shm_dbg_buffer_key_size(vms_shm_dbg_buffer *b); -size_t vms_shm_dbg_buffer_value_size(vms_shm_dbg_buffer *b); -size_t vms_shm_dbg_buffer_rec_size(vms_shm_dbg_buffer *b); -unsigned char *vms_shm_dbg_buffer_data(vms_shm_dbg_buffer *b); - -void vms_shm_dbg_buffer_inc_size(vms_shm_dbg_buffer *b, size_t size); -size_t vms_shm_dbg_buffer_version(vms_shm_dbg_buffer *b); -void vms_shm_dbg_buffer_bump_version(vms_shm_dbg_buffer *b); +vms_vms_dbg_buffer *vms_vms_dbg_buffer_get(const char *key); +void vms_vms_dbg_buffer_release(vms_vms_dbg_buffer *); +void vms_vms_dbg_buffer_destroy(vms_vms_dbg_buffer *); + +size_t vms_vms_dbg_buffer_size(vms_vms_dbg_buffer *b); +size_t vms_vms_dbg_buffer_capacity(vms_vms_dbg_buffer *b); +size_t vms_vms_dbg_buffer_key_size(vms_vms_dbg_buffer *b); +size_t vms_vms_dbg_buffer_value_size(vms_vms_dbg_buffer *b); +size_t vms_vms_dbg_buffer_rec_size(vms_vms_dbg_buffer *b); +unsigned char *vms_vms_dbg_buffer_data(vms_vms_dbg_buffer *b); + +void vms_vms_dbg_buffer_inc_size(vms_vms_dbg_buffer *b, size_t size); +size_t vms_vms_dbg_buffer_version(vms_vms_dbg_buffer *b); +void vms_vms_dbg_buffer_bump_version(vms_vms_dbg_buffer *b); #endif /* SHAMON_SHM_BUFFER_H */ diff --git a/include/vamos-buffers/streams/stream-drregex.h b/include/vamos-buffers/streams/stream-drregex.h index a5ee061b..b8696ef6 100644 --- a/include/vamos-buffers/streams/stream-drregex.h +++ b/include/vamos-buffers/streams/stream-drregex.h @@ -6,8 +6,8 @@ #define DRREGEX_ONLY_ARGS -typedef struct _shm_event_drregex { - shm_event base; +typedef struct _vms_event_drregex { + vms_event base; #ifndef DRREGEX_ONLY_ARGS bool write; /* true = write, false = read */ int fd; @@ -15,12 +15,12 @@ typedef struct _shm_event_drregex { #endif /* the event arguments */ unsigned char args[]; -} shm_event_drregex; +} vms_event_drregex; -typedef struct _shm_stream_drregex { - shm_stream base; +typedef struct _vms_stream_drregex { + vms_stream base; struct buffer *shmbuffer; -} shm_stream_drregex; +} vms_stream_drregex; -shm_stream *shm_create_drregex_stream(const char *key, const char *name); -void shm_destroy_drregex_stream(shm_stream_drregex *ss); +vms_stream *vms_create_drregex_stream(const char *key, const char *name); +void vms_destroy_drregex_stream(vms_stream_drregex *ss); diff --git a/include/vamos-buffers/streams/stream-funs.h b/include/vamos-buffers/streams/stream-funs.h index c51ac20c..802d4abd 100644 --- a/include/vamos-buffers/streams/stream-funs.h +++ b/include/vamos-buffers/streams/stream-funs.h @@ -4,34 +4,34 @@ #include "vamos-buffers/core/event.h" #include "vamos-buffers/core/stream.h" -typedef struct _shm_event_funcall { - shm_event base; +typedef struct _vms_event_funcall { + vms_event base; /* the event arguments */ char signature[8]; unsigned char args[]; -} shm_event_funcall; +} vms_event_funcall; -typedef struct _shm_stream_funs { - shm_stream base; +typedef struct _vms_stream_funs { + vms_stream base; struct buffer *shmbuffer; struct event_record *events; /* buffer for reading and publishing the event */ - shm_event_funcall *ev; + vms_event_funcall *ev; void *ev_buff; size_t spec_count; -} shm_stream_funs; +} vms_stream_funs; -bool funs_has_event(shm_stream *stream); +bool funs_has_event(vms_stream *stream); -shm_event_funcall *funs_get_next_event(shm_stream *stream); +vms_event_funcall *funs_get_next_event(vms_stream *stream); -shm_stream *shm_create_funs_stream(const char *key, const char *name); +vms_stream *vms_create_funs_stream(const char *key, const char *name); -struct event_record *shm_funs_stream_get_event_spec(shm_stream_funs *stream, - shm_kind kind); +struct event_record *vms_funs_stream_get_event_spec(vms_stream_funs *stream, + vms_kind kind); -void shm_event_funcall_release(shm_event_funcall *fev); +void vms_event_funcall_release(vms_event_funcall *fev); -const char *shm_stream_funs_get_str(shm_stream_funs *fstream, uint64_t elem); +const char *vms_stream_funs_get_str(vms_stream_funs *fstream, uint64_t elem); -void shm_destroy_funs_stream(shm_stream_funs *ss); +void vms_destroy_funs_stream(vms_stream_funs *ss); diff --git a/include/vamos-buffers/streams/stream-generic.h b/include/vamos-buffers/streams/stream-generic.h index 78feb2a5..d561614b 100644 --- a/include/vamos-buffers/streams/stream-generic.h +++ b/include/vamos-buffers/streams/stream-generic.h @@ -13,18 +13,18 @@ struct buffer; /* A generic stream for events stored in shared memory buffer, no filter nor modification of events supported (unless done manually). */ -typedef struct _shm_event_generic { - shm_event base; +typedef struct _vms_event_generic { + vms_event base; /* the event arguments */ unsigned char args[]; -} shm_event_generic; +} vms_event_generic; -typedef struct _shm_stream_generic { - shm_stream base; +typedef struct _vms_stream_generic { + vms_stream base; struct buffer *shmbuffer; -} shm_stream_generic; +} vms_stream_generic; -shm_stream *shm_create_generic_stream(const char *key, const char *name, - shm_stream_hole_handling *hole_handling); +vms_stream *vms_create_generic_stream(const char *key, const char *name, + vms_stream_hole_handling *hole_handling); #endif /* SHMN_STREAM_GENERIC_H_ */ diff --git a/include/vamos-buffers/streams/stream-regex.h b/include/vamos-buffers/streams/stream-regex.h index 075f7983..761117db 100644 --- a/include/vamos-buffers/streams/stream-regex.h +++ b/include/vamos-buffers/streams/stream-regex.h @@ -5,18 +5,18 @@ #include "vamos-buffers/shmbuf/buffer.h" #include "vamos-buffers/core/stream.h" -typedef struct _shm_event_regex { - shm_event base; +typedef struct _vms_event_regex { + vms_event base; /* the event arguments */ unsigned char args[]; -} shm_event_regex; +} vms_event_regex; -typedef struct _shm_stream_sregex { - shm_stream base; +typedef struct _vms_stream_sregex { + vms_stream base; struct buffer *shmbuffer; -} shm_stream_sregex; +} vms_stream_sregex; -shm_stream *shm_create_sregex_stream( +vms_stream *vms_create_sregex_stream( const char *key, const char *name, - const shm_stream_hole_handling *hole_handling); -void shm_destroy_sregex_stream(shm_stream_sregex *ss); + const vms_stream_hole_handling *hole_handling); +void vms_destroy_sregex_stream(vms_stream_sregex *ss); diff --git a/include/vamos-buffers/streams/stream-regexrw.h b/include/vamos-buffers/streams/stream-regexrw.h index 119c667e..f6aa3f2c 100644 --- a/include/vamos-buffers/streams/stream-regexrw.h +++ b/include/vamos-buffers/streams/stream-regexrw.h @@ -5,16 +5,16 @@ #include "vamos-buffers/shmbuf/buffer.h" #include "vamos-buffers/core/stream.h" -typedef struct _shm_event_regexrw { - shm_event base; +typedef struct _vms_event_regexrw { + vms_event base; /* the event arguments */ unsigned char args[]; -} shm_event_regexrw; +} vms_event_regexrw; -typedef struct _shm_stream_sregexrw { - shm_stream base; +typedef struct _vms_stream_sregexrw { + vms_stream base; struct buffer *shmbuffer; -} shm_stream_sregexrw; +} vms_stream_sregexrw; -shm_stream *shm_create_sregexrw_stream(const char *key, const char *name); -void shm_destroy_sregexrw_stream(shm_stream_sregexrw *ss); +vms_stream *vms_create_sregexrw_stream(const char *key, const char *name); +void vms_destroy_sregexrw_stream(vms_stream_sregexrw *ss); diff --git a/include/vamos-buffers/streams/streams.h b/include/vamos-buffers/streams/streams.h index bae0d0e1..3a6de074 100644 --- a/include/vamos-buffers/streams/streams.h +++ b/include/vamos-buffers/streams/streams.h @@ -3,10 +3,10 @@ #include "vamos-buffers/core/stream.h" -shm_stream *shm_stream_create_from_argv( +vms_stream *vms_stream_create_from_argv( const char *stream_name, int argc, char *argv[], - const shm_stream_hole_handling *hole_handling); -shm_stream *shm_stream_create(const char *stream_name, const char *spec, - const shm_stream_hole_handling *hole_handling); + const vms_stream_hole_handling *hole_handling); +vms_stream *vms_stream_create(const char *stream_name, const char *spec, + const vms_stream_hole_handling *hole_handling); #endif /* SHAMON_STREAMS_H_ */ diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 6cf6bd89..d1c1c9ca 100755 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -3,7 +3,7 @@ add_library(vamos-buffers-list STATIC list.c list-embedded.c) add_library(vamos-buffers-event STATIC event.c) add_library(vamos-buffers-queue-spsc STATIC queue_spsc.c) add_library(vamos-buffers-vector STATIC vector.c vector-aligned.c) -add_library(vamos-buffers-string STATIC shm_string.c) +add_library(vamos-buffers-string STATIC vms_string.c) add_library(vamos-buffers-source STATIC source.c) add_library(vamos-buffers-stream STATIC stream.c) add_library(vamos-buffers-arbiter STATIC arbiter.c) diff --git a/src/core/arbiter.c b/src/core/arbiter.c index e57b2b6e..02f6d188 100644 --- a/src/core/arbiter.c +++ b/src/core/arbiter.c @@ -9,8 +9,8 @@ #define DROP_SPACE_DEFAULT_THRESHOLD 1 -typedef struct _shm_arbiter_buffer { - shm_par_queue buffer; // the buffer itself +typedef struct _vms_arbiter_buffer { + vms_par_queue buffer; // the buffer itself size_t drop_space_threshold; // the number of elements to keep free before // pushing dropped() event size_t dropped_num; // the number of dropped events @@ -24,44 +24,44 @@ typedef struct _shm_arbiter_buffer { int last_was_drop; // true if the last event written was drop() size_t waited_to_push; // how many times the buffer waited to push #endif - shm_eventid drop_begin_id; // the id of the next 'dropped' event + vms_eventid drop_begin_id; // the id of the next 'dropped' event - shm_stream *stream; // the source for the buffer - shm_event *hole_event; + vms_stream *stream; // the source for the buffer + vms_event *hole_event; bool active; // true while the events are being queued -} shm_arbiter_buffer; +} vms_arbiter_buffer; -size_t shm_arbiter_buffer_sizeof(void) { return sizeof(shm_arbiter_buffer); } +size_t vms_arbiter_buffer_sizeof(void) { return sizeof(vms_arbiter_buffer); } -void *shm_arbiter_buffer_write_ptr(shm_arbiter_buffer *q) { - return shm_par_queue_write_ptr(&q->buffer); +void *vms_arbiter_buffer_write_ptr(vms_arbiter_buffer *q) { + return vms_par_queue_write_ptr(&q->buffer); } -void shm_arbiter_buffer_write_finish(shm_arbiter_buffer *q) { +void vms_arbiter_buffer_write_finish(vms_arbiter_buffer *q) { #ifdef DUMP_STATS ++q->written_num; #endif - shm_par_queue_write_finish(&q->buffer); + vms_par_queue_write_finish(&q->buffer); } -void shm_arbiter_buffer_finish_push(shm_arbiter_buffer *q); +void vms_arbiter_buffer_finish_push(vms_arbiter_buffer *q); -size_t shm_arbiter_buffer_size(shm_arbiter_buffer *buffer) { - return shm_par_queue_size(&buffer->buffer); +size_t vms_arbiter_buffer_size(vms_arbiter_buffer *buffer) { + return vms_par_queue_size(&buffer->buffer); } -size_t shm_arbiter_buffer_free_space(shm_arbiter_buffer *buffer) { - return shm_par_queue_free_num(&buffer->buffer); +size_t vms_arbiter_buffer_free_space(vms_arbiter_buffer *buffer) { + return vms_par_queue_free_num(&buffer->buffer); } -size_t shm_arbiter_buffer_capacity(shm_arbiter_buffer *buffer) { - return shm_par_queue_capacity(&buffer->buffer); +size_t vms_arbiter_buffer_capacity(vms_arbiter_buffer *buffer) { + return vms_par_queue_capacity(&buffer->buffer); } -size_t shm_arbiter_buffer_set_drop_space_threshold(shm_arbiter_buffer *buffer, +size_t vms_arbiter_buffer_set_drop_space_threshold(vms_arbiter_buffer *buffer, size_t thr) { assert(thr >= 1); - assert(thr < shm_arbiter_buffer_capacity(buffer)); + assert(thr < vms_arbiter_buffer_capacity(buffer)); size_t old = buffer->drop_space_threshold; buffer->drop_space_threshold = thr; @@ -70,22 +70,22 @@ size_t shm_arbiter_buffer_set_drop_space_threshold(shm_arbiter_buffer *buffer, /* drop an event and notify buffer the buffer that it may free up * the payload of this and older events */ -size_t shm_arbiter_buffer_drop(shm_arbiter_buffer *buffer, size_t k) { +size_t vms_arbiter_buffer_drop(vms_arbiter_buffer *buffer, size_t k) { #ifdef DUMP_STATS buffer->volunt_dropped_num_asked += k; #endif --k; /* peek_*_at takes index from 0 */ - shm_event *ev = shm_par_queue_peek_atmost_at(&buffer->buffer, &k); + vms_event *ev = vms_par_queue_peek_atmost_at(&buffer->buffer, &k); if (!ev) return 0; /* empty queue */ - shm_eventid last_id = shm_event_id(ev); + vms_eventid last_id = vms_event_id(ev); #ifndef NDEBUG size_t n = #endif ++k; /* k is index, we must increase it back by one */ - shm_par_queue_drop(&buffer->buffer, k); + vms_par_queue_drop(&buffer->buffer, k); assert(n == k && "Something changed the queue in between"); - shm_stream_notify_last_processed_id(buffer->stream, last_id); + vms_stream_notify_last_processed_id(buffer->stream, last_id); #ifdef DUMP_STATS buffer->volunt_dropped_num += k; #endif @@ -94,26 +94,26 @@ size_t shm_arbiter_buffer_drop(shm_arbiter_buffer *buffer, size_t k) { /* Drop all events with ID less or equal to the one of ev. * Return how many events were dropped */ -size_t shm_arbiter_buffer_drop_older_than(shm_arbiter_buffer *buffer, - shm_eventid id) { +size_t vms_arbiter_buffer_drop_older_than(vms_arbiter_buffer *buffer, + vms_eventid id) { /* we first must find the event in the queue */ void *ptr1, *ptr2; size_t len1, len2; const size_t n = - shm_par_queue_peek(&buffer->buffer, 0, &ptr1, &len1, &ptr2, &len2); + vms_par_queue_peek(&buffer->buffer, 0, &ptr1, &len1, &ptr2, &len2); if (n == 0) return 0; - const size_t elem_size = shm_par_queue_elem_size(&buffer->buffer); + const size_t elem_size = vms_par_queue_elem_size(&buffer->buffer); size_t k; /* the number of events to be dropped */ size_t bot = 0, top; unsigned char *events; assert(len1 > 0 && "Underflow"); top = len1 - 1; - shm_event *ev = (shm_event *)((unsigned char *)ptr1 + (top)*elem_size); + vms_event *ev = (vms_event *)((unsigned char *)ptr1 + (top)*elem_size); /* in which part of the buffer may the event be? */ - if (id <= shm_event_id(ev)) { + if (id <= vms_event_id(ev)) { events = ptr1; } else if (len2 > 0) { events = ptr2; @@ -124,8 +124,8 @@ size_t shm_arbiter_buffer_drop_older_than(shm_arbiter_buffer *buffer, k = len1; /* now consume everything up to the found event */ if (k > 0) { - shm_par_queue_drop(&buffer->buffer, k); - shm_stream_notify_last_processed_id(buffer->stream, id); + vms_par_queue_drop(&buffer->buffer, k); + vms_stream_notify_last_processed_id(buffer->stream, id); #ifdef DUMP_STATS buffer->volunt_dropped_num_asked += k; @@ -147,8 +147,8 @@ size_t shm_arbiter_buffer_drop_older_than(shm_arbiter_buffer *buffer, /* the search should terminate much earlier then after n steps */ assert(steps++ < n && "BUG in binary search"); - ev = (shm_event *)(events + pivot * elem_size); - if (id < shm_event_id(ev)) { + ev = (vms_event *)(events + pivot * elem_size); + if (id < vms_event_id(ev)) { if (pivot == 0) { assert(bot == 0); assert(top == 1); @@ -160,7 +160,7 @@ size_t shm_arbiter_buffer_drop_older_than(shm_arbiter_buffer *buffer, } else { if (bot == pivot) { assert(top == bot + 1); - if (shm_event_id(ev) == id) { + if (vms_event_id(ev) == id) { top = bot; } else { bot = top; @@ -172,8 +172,8 @@ size_t shm_arbiter_buffer_drop_older_than(shm_arbiter_buffer *buffer, assert(bot <= top); if (top == bot) { - ev = (shm_event *)(events + top * elem_size); - if (id < shm_event_id(ev)) { + ev = (vms_event *)(events + top * elem_size); + if (id < vms_event_id(ev)) { /* the found event is the one to the left */ if (bot == 0) { /* There is no event to the left in this part of the buffer. @@ -199,8 +199,8 @@ size_t shm_arbiter_buffer_drop_older_than(shm_arbiter_buffer *buffer, /* now consume everything up to the found event */ if (k > 0) { - shm_par_queue_drop(&buffer->buffer, k); - shm_stream_notify_last_processed_id(buffer->stream, id); + vms_par_queue_drop(&buffer->buffer, k); + vms_stream_notify_last_processed_id(buffer->stream, id); #ifdef DUMP_STATS buffer->volunt_dropped_num_asked += k; @@ -211,38 +211,38 @@ size_t shm_arbiter_buffer_drop_older_than(shm_arbiter_buffer *buffer, return k; } -bool shm_arbiter_buffer_active(shm_arbiter_buffer *buffer) { +bool vms_arbiter_buffer_active(vms_arbiter_buffer *buffer) { return buffer->active; } -void shm_arbiter_buffer_set_active(shm_arbiter_buffer *buffer, bool val) { +void vms_arbiter_buffer_set_active(vms_arbiter_buffer *buffer, bool val) { buffer->active = val; if (val) { - shm_stream_attach(buffer->stream); + vms_stream_attach(buffer->stream); } else { - shm_stream_attach(buffer->stream); + vms_stream_attach(buffer->stream); } } -shm_stream *shm_arbiter_buffer_stream(shm_arbiter_buffer *buffer) { +vms_stream *vms_arbiter_buffer_stream(vms_arbiter_buffer *buffer) { return buffer->stream; } -size_t shm_arbiter_buffer_dropped_num(shm_arbiter_buffer *buffer) { +size_t vms_arbiter_buffer_dropped_num(vms_arbiter_buffer *buffer) { return buffer->total_dropped_num; } -size_t shm_arbiter_buffer_dropped_times(shm_arbiter_buffer *buffer) { +size_t vms_arbiter_buffer_dropped_times(vms_arbiter_buffer *buffer) { return buffer->total_dropped_times; } #ifdef DUMP_STATS -size_t shm_arbiter_buffer_written_num(shm_arbiter_buffer *buffer) { +size_t vms_arbiter_buffer_written_num(vms_arbiter_buffer *buffer) { return buffer->written_num; } #endif -void shm_arbiter_buffer_init(shm_arbiter_buffer *buffer, shm_stream *stream, +void vms_arbiter_buffer_init(vms_arbiter_buffer *buffer, vms_stream *stream, size_t out_event_size, size_t capacity) { assert(ADDR_IS_CACHE_ALIGNED(buffer) && "The memory for the buffer is missaligned"); @@ -257,7 +257,7 @@ void shm_arbiter_buffer_init(shm_arbiter_buffer *buffer, shm_stream *stream, if (hole_event_size > 0 && event_size < hole_event_size) event_size = hole_event_size; - shm_par_queue_init(&buffer->buffer, capacity, event_size); + vms_par_queue_init(&buffer->buffer, capacity, event_size); buffer->drop_space_threshold = DROP_SPACE_DEFAULT_THRESHOLD; buffer->hole_event = xalloc(stream->hole_handling.hole_event_size); @@ -276,34 +276,34 @@ void shm_arbiter_buffer_init(shm_arbiter_buffer *buffer, shm_stream *stream, #endif } -shm_arbiter_buffer *shm_arbiter_buffer_create(shm_stream *stream, +vms_arbiter_buffer *vms_arbiter_buffer_create(vms_stream *stream, size_t out_event_size, size_t capacity) { /* some elements are cache-aligned, so we must make sure * that the memory is not allocated mis-aligned */ - shm_arbiter_buffer *b = - xalloc_aligned(shm_arbiter_buffer_sizeof(), CACHELINE_SIZE); - shm_arbiter_buffer_init(b, stream, out_event_size, capacity); + vms_arbiter_buffer *b = + xalloc_aligned(vms_arbiter_buffer_sizeof(), CACHELINE_SIZE); + vms_arbiter_buffer_init(b, stream, out_event_size, capacity); return b; } -void shm_arbiter_buffer_free(shm_arbiter_buffer *buffer) { - shm_arbiter_buffer_destroy(buffer); +void vms_arbiter_buffer_free(vms_arbiter_buffer *buffer) { + vms_arbiter_buffer_destroy(buffer); free(buffer); } -void shm_arbiter_buffer_destroy(shm_arbiter_buffer *buffer) { - shm_par_queue_destroy(&buffer->buffer); +void vms_arbiter_buffer_destroy(vms_arbiter_buffer *buffer) { + vms_par_queue_destroy(&buffer->buffer); free(buffer->hole_event); } -size_t shm_arbiter_buffer_elem_size(shm_arbiter_buffer *q) { - return shm_par_queue_elem_size(&q->buffer); +size_t vms_arbiter_buffer_elem_size(vms_arbiter_buffer *q) { + return vms_par_queue_elem_size(&q->buffer); } -void shm_arbiter_buffer_push(shm_arbiter_buffer *buffer, const void *elem, +void vms_arbiter_buffer_push(vms_arbiter_buffer *buffer, const void *elem, size_t size) { - assert(shm_arbiter_buffer_active(buffer)); - while (!shm_par_queue_push(&buffer->buffer, elem, size)) { + assert(vms_arbiter_buffer_active(buffer)); + while (!vms_par_queue_push(&buffer->buffer, elem, size)) { #ifdef DUMP_STATS ++buffer->waited_to_push; #endif @@ -312,24 +312,24 @@ void shm_arbiter_buffer_push(shm_arbiter_buffer *buffer, const void *elem, } #if 0 -void shm_arbiter_buffer_push(shm_arbiter_buffer *buffer, const void *elem, +void vms_arbiter_buffer_push(vms_arbiter_buffer *buffer, const void *elem, size_t size) { - assert(shm_arbiter_buffer_active(buffer)); - shm_par_queue *queue = &buffer->buffer; + assert(vms_arbiter_buffer_active(buffer)); + vms_par_queue *queue = &buffer->buffer; if (buffer->dropped_num > 0) { - if (shm_par_queue_free_num(queue) < 2) { + if (vms_par_queue_free_num(queue) < 2) { ++buffer->dropped_num; } else { - shm_stream_prepare_hole_event(buffer->stream, buffer->hole_event, + vms_stream_prepare_hole_event(buffer->stream, buffer->hole_event, buffer->drop_begin_id, buffer->dropped_num); assert(buffer->stream->hole_handling.hole_event_size <= - shm_par_queue_elem_size(queue)); + vms_par_queue_elem_size(queue)); #ifndef NDEBUG bool ret = #endif - shm_par_queue_push( + vms_par_queue_push( queue, buffer->hole_event, buffer->stream->hole_handling.hole_event_size); #ifdef DUMP_STATS @@ -339,7 +339,7 @@ void shm_arbiter_buffer_push(shm_arbiter_buffer *buffer, const void *elem, #ifndef NDEBUG ret = #endif - shm_par_queue_push(&buffer->buffer, elem, size); + vms_par_queue_push(&buffer->buffer, elem, size); #ifdef DUMP_STATS ++buffer->written_num; #endif @@ -349,13 +349,13 @@ void shm_arbiter_buffer_push(shm_arbiter_buffer *buffer, const void *elem, buffer->dropped_num = 0; /* the end id may not be precise, but we need just the upper bound */ - shm_arbiter_buffer_notify_dropped(buffer, buffer->drop_begin_id, - shm_event_id((shm_event *)elem) - + vms_arbiter_buffer_notify_dropped(buffer, buffer->drop_begin_id, + vms_event_id((vms_event *)elem) - 1); } } else { - if (!shm_par_queue_push(&buffer->buffer, elem, size)) { - buffer->drop_begin_id = shm_event_id((shm_event *)elem); + if (!vms_par_queue_push(&buffer->buffer, elem, size)) { + buffer->drop_begin_id = vms_event_id((vms_event *)elem); ++buffer->dropped_num; } #ifdef DUMP_STATS @@ -369,27 +369,27 @@ void shm_arbiter_buffer_push(shm_arbiter_buffer *buffer, const void *elem, /* NOTE: does not notify about processing the event, must * be done manually once all the work with data is done */ -bool shm_arbiter_buffer_pop(shm_arbiter_buffer *buffer, void *elem) { - return shm_par_queue_pop(&buffer->buffer, elem); +bool vms_arbiter_buffer_pop(vms_arbiter_buffer *buffer, void *elem) { + return vms_par_queue_pop(&buffer->buffer, elem); } -shm_event *shm_arbiter_buffer_top(shm_arbiter_buffer *buffer) { - return shm_par_queue_top(&buffer->buffer); +vms_event *vms_arbiter_buffer_top(vms_arbiter_buffer *buffer) { + return vms_par_queue_top(&buffer->buffer); } -size_t shm_arbiter_buffer_peek(shm_arbiter_buffer *buffer, size_t n, +size_t vms_arbiter_buffer_peek(vms_arbiter_buffer *buffer, size_t n, void **data1, size_t *size1, void **data2, size_t *size2) { - return shm_par_queue_peek(&buffer->buffer, n, data1, size1, data2, size2); + return vms_par_queue_peek(&buffer->buffer, n, data1, size1, data2, size2); } -size_t shm_arbiter_buffer_peek1(shm_arbiter_buffer *buffer, void **data) { - return shm_par_queue_peek1(&buffer->buffer, data); +size_t vms_arbiter_buffer_peek1(vms_arbiter_buffer *buffer, void **data) { + return vms_par_queue_peek1(&buffer->buffer, data); } /* get an event from the stream, block until there is some and return it * or return NULL if the stream ended */ -static void *get_event(shm_stream *stream) { +static void *get_event(vms_stream *stream) { /* TODO: if there is no filtering and modifications, we can push multiple events forward. if there are filtering and modifications, we could have an additional thread to handle the load of data if we copy them in chunks @@ -400,7 +400,7 @@ static void *get_event(shm_stream *stream) { void *ev; while (1) { /* wait for the event */ - ev = shm_stream_read_events(stream, &num); + ev = vms_stream_read_events(stream, &num); if (ev) { #ifdef DUMP_STATS ++stream->read_events; @@ -424,7 +424,7 @@ static void *get_event(shm_stream *stream) { /* checking for the readiness is not cheap, * so do it only after we haven't read any * event for some time */ - if (!shm_stream_is_ready(stream)) { + if (!vms_stream_is_ready(stream)) { return NULL; } } @@ -434,40 +434,40 @@ static void *get_event(shm_stream *stream) { assert(0 && "Unreachable"); } -static void push_dropped_event(shm_stream *stream, shm_arbiter_buffer *buffer, +static void push_dropped_event(vms_stream *stream, vms_arbiter_buffer *buffer, size_t notify_id) { - shm_stream_prepare_hole_event(stream, buffer->hole_event, notify_id, + vms_stream_prepare_hole_event(stream, buffer->hole_event, notify_id, buffer->dropped_num); - shm_par_queue_push(&buffer->buffer, buffer->hole_event, + vms_par_queue_push(&buffer->buffer, buffer->hole_event, stream->hole_handling.hole_event_size); #ifdef DUMP_STATS ++buffer->written_num; #endif buffer->total_dropped_num += buffer->dropped_num; ++buffer->total_dropped_times; - shm_arbiter_buffer_notify_dropped(buffer, buffer->drop_begin_id, notify_id); - assert(shm_arbiter_buffer_free_space(buffer) > 0); + vms_arbiter_buffer_notify_dropped(buffer, buffer->drop_begin_id, notify_id); + assert(vms_arbiter_buffer_free_space(buffer) > 0); /* printf("PUSHED DROPPED event { kind = %lu, id = %lu, n = %lu}\n", - ((shm_event*)&dropped_ev)->kind, - ((shm_event*)&dropped_ev)->id, + ((vms_event*)&dropped_ev)->kind, + ((vms_event*)&dropped_ev)->id, dropped_ev.n); */ } -void *handle_stream_end(shm_stream *stream, shm_arbiter_buffer *buffer, +void *handle_stream_end(vms_stream *stream, vms_arbiter_buffer *buffer, size_t last_ev_id) { uint64_t sleep_time = SLEEP_TIME_INIT_NS; while (buffer->dropped_num > 0) { assert(buffer->drop_space_threshold < - shm_arbiter_buffer_capacity(buffer)); - if (shm_arbiter_buffer_free_space(buffer) > + vms_arbiter_buffer_capacity(buffer)); + if (vms_arbiter_buffer_free_space(buffer) > buffer->drop_space_threshold) { /* the end id may not be precise, but we need just the upper * bound */ push_dropped_event(stream, buffer, last_ev_id - 1); - assert(shm_arbiter_buffer_free_space(buffer) > 0); + assert(vms_arbiter_buffer_free_space(buffer) > 0); #ifdef DUMP_STATS buffer->last_was_drop = 1; #endif @@ -481,13 +481,13 @@ void *handle_stream_end(shm_stream *stream, shm_arbiter_buffer *buffer, assert(buffer->dropped_num > 0); } } - assert(!shm_stream_is_ready(stream)); + assert(!vms_stream_is_ready(stream)); assert(buffer->dropped_num == 0); return NULL; /* stream ended */ } -static inline void send_dropped_event(shm_stream *stream, - shm_arbiter_buffer *buffer, +static inline void send_dropped_event(vms_stream *stream, + vms_arbiter_buffer *buffer, size_t last_ev_id) { assert(last_ev_id == buffer->dropped_num + buffer->drop_begin_id && "Drop IDs are wrong"); @@ -495,16 +495,16 @@ static inline void send_dropped_event(shm_stream *stream, buffer->dropped_num = 0; /* printf("FETCH: stopped dropping { kind = %lu, id = %lu}\n", - ((shm_event*)ev)->kind, - ((shm_event*)ev)->id); + ((vms_event*)ev)->kind, + ((vms_event*)ev)->id); */ - assert(shm_arbiter_buffer_free_space(buffer) > 0); + assert(vms_arbiter_buffer_free_space(buffer) > 0); } -static inline void start_dropping(shm_stream *stream, - shm_arbiter_buffer *buffer, - shm_event *event) { - buffer->drop_begin_id = shm_event_id(event); +static inline void start_dropping(vms_stream *stream, + vms_arbiter_buffer *buffer, + vms_event *event) { + buffer->drop_begin_id = vms_event_id(event); assert(buffer->dropped_num == 0); ++buffer->dropped_num; @@ -513,15 +513,15 @@ static inline void start_dropping(shm_stream *stream, stream->hole_handling.update(buffer->hole_event, event); /* printf("FETCH: start dropping { kind = %lu, id = %lu}\n", - ((shm_event*)ev)->kind, - ((shm_event*)ev)->id); + ((vms_event*)ev)->kind, + ((vms_event*)ev)->id); */ - shm_stream_consume(stream, 1); + vms_stream_consume(stream, 1); } -static inline void continue_dropping(shm_stream *stream, - shm_arbiter_buffer *buffer, - shm_event *event) { +static inline void continue_dropping(vms_stream *stream, + vms_arbiter_buffer *buffer, + vms_event *event) { ++buffer->dropped_num; stream->hole_handling.update(buffer->hole_event, event); @@ -529,24 +529,24 @@ static inline void continue_dropping(shm_stream *stream, * long time to generate the dropped event */ /* FIXME: % is slow... and make it configurable */ if (buffer->dropped_num % 10000 == 0) { - shm_arbiter_buffer_notify_dropped(buffer, buffer->drop_begin_id, - shm_event_id(event) - 1); + vms_arbiter_buffer_notify_dropped(buffer, buffer->drop_begin_id, + vms_event_id(event) - 1); } /* consume the dropped event */ - shm_stream_consume(stream, 1); + vms_stream_consume(stream, 1); } /* return true if the event should be dropped and false if the event should be forwarded */ -static bool handle_dropping_event(shm_stream *stream, - shm_arbiter_buffer *buffer, - shm_event *event) { +static bool handle_dropping_event(vms_stream *stream, + vms_arbiter_buffer *buffer, + vms_event *event) { if (buffer->dropped_num > 0) { assert(buffer->drop_space_threshold < - shm_arbiter_buffer_capacity(buffer)); - if (shm_arbiter_buffer_free_space(buffer) > + vms_arbiter_buffer_capacity(buffer)); + if (vms_arbiter_buffer_free_space(buffer) > buffer->drop_space_threshold) { - send_dropped_event(stream, buffer, shm_event_id(event)); + send_dropped_event(stream, buffer, vms_event_id(event)); return false; /* forward the current event */ } @@ -555,18 +555,18 @@ static bool handle_dropping_event(shm_stream *stream, } assert(buffer->dropped_num == 0); - if (shm_arbiter_buffer_free_space(buffer) == 0) { + if (vms_arbiter_buffer_free_space(buffer) == 0) { start_dropping(stream, buffer, event); return true; } - assert(shm_arbiter_buffer_free_space(buffer) > 0); + assert(vms_arbiter_buffer_free_space(buffer) > 0); assert(buffer->dropped_num == 0); return false; } /* wait for an event on the 'stream' */ -void *stream_fetch(shm_stream *stream, shm_arbiter_buffer *buffer) { +void *stream_fetch(vms_stream *stream, vms_arbiter_buffer *buffer) { void *ev; size_t last_ev_id = 1; while (1) { @@ -576,14 +576,14 @@ void *stream_fetch(shm_stream *stream, shm_arbiter_buffer *buffer) { } assert(ev && "Dont have event"); - assert(!shm_event_is_hole((shm_event *)ev) && "Got dropped event"); + assert(!vms_event_is_hole((vms_event *)ev) && "Got dropped event"); - last_ev_id = shm_event_id(ev); + last_ev_id = vms_event_id(ev); assert(last_ev_id == ++stream->last_event_id && "IDs are inconsistent"); /* printf("FETCH: read event { kind = %lu, id = %lu}\n", - ((shm_event*)ev)->kind, - ((shm_event*)ev)->id); + ((vms_event*)ev)->kind, + ((vms_event*)ev)->id); */ if (!handle_dropping_event(stream, buffer, ev)) { @@ -598,8 +598,8 @@ void *stream_fetch(shm_stream *stream, shm_arbiter_buffer *buffer) { } /* FIXME: do not duplicate the code */ -void *stream_filter_fetch(shm_stream *stream, shm_arbiter_buffer *buffer, - shm_stream_filter_fn filter) { +void *stream_filter_fetch(vms_stream *stream, vms_arbiter_buffer *buffer, + vms_stream_filter_fn filter) { void *ev; size_t last_ev_id = 1; while (1) { @@ -609,21 +609,21 @@ void *stream_filter_fetch(shm_stream *stream, shm_arbiter_buffer *buffer, } assert(ev && "Dont have event"); - assert(!shm_event_is_hole((shm_event *)ev) && "Got hole event"); + assert(!vms_event_is_hole((vms_event *)ev) && "Got hole event"); - last_ev_id = shm_event_id(ev); + last_ev_id = vms_event_id(ev); assert(last_ev_id == ++stream->last_event_id && "IDs are inconsistent"); if (filter && !filter(stream, ev)) { /* consume the filtered event */ - shm_stream_consume(stream, 1); + vms_stream_consume(stream, 1); continue; } /* printf("FETCH: read event { kind = %lu, id = %lu}\n", - ((shm_event*)ev)->kind, - ((shm_event*)ev)->id); + ((vms_event*)ev)->kind, + ((vms_event*)ev)->id); */ if (!handle_dropping_event(stream, buffer, ev)) { @@ -638,12 +638,12 @@ void *stream_filter_fetch(shm_stream *stream, shm_arbiter_buffer *buffer, } -bool shm_arbiter_buffer_is_done(shm_arbiter_buffer *buffer) { +bool vms_arbiter_buffer_is_done(vms_arbiter_buffer *buffer) { /* XXX: should we rather use a flag that we set to true when stream-fetch * knows that the stream is done? */ return (buffer->dropped_num == 0 && - shm_par_queue_size(&buffer->buffer) == 0) && - !shm_stream_is_ready(buffer->stream) && + vms_par_queue_size(&buffer->buffer) == 0) && + !vms_stream_is_ready(buffer->stream) && /* there is a race when the buffer may be empty, but after checking * the first part of the condition above (resulting in true) a new * event is put there. Then the SHM buffer gets destroyed and the @@ -652,13 +652,13 @@ bool shm_arbiter_buffer_is_done(shm_arbiter_buffer *buffer) { * event in it. So re-check once more that the buffer is really empty * after the stream was destroyed */ (buffer->dropped_num == 0 && - shm_par_queue_size(&buffer->buffer) == 0); + vms_par_queue_size(&buffer->buffer) == 0); } -void shm_arbiter_buffer_notify_dropped(shm_arbiter_buffer *buffer, +void vms_arbiter_buffer_notify_dropped(vms_arbiter_buffer *buffer, uint64_t begin_id, uint64_t end_id) { - shm_stream_notify_dropped(buffer->stream, begin_id, end_id); + vms_stream_notify_dropped(buffer->stream, begin_id, end_id); } #ifdef DUMP_STATS @@ -669,8 +669,8 @@ void shm_arbiter_buffer_notify_dropped(shm_arbiter_buffer *buffer, } #define COLOR_RESET fprintf(stderr, "\033[0m"); -void shm_arbiter_buffer_dump_stats(shm_arbiter_buffer *buffer) { - shm_stream *s = buffer->stream; +void vms_arbiter_buffer_dump_stats(vms_arbiter_buffer *buffer) { + vms_stream *s = buffer->stream; fprintf(stderr, "-- Buffer for stream %lu (%s) --\n", s->id, s->name); fprintf(stderr, " Stream read %lu events from SHM\n", s->read_events); COLOR_RED_IF(s->read_events != s->consumed_events) diff --git a/src/core/event.c b/src/core/event.c index b7ce27ba..69b7c953 100644 --- a/src/core/event.c +++ b/src/core/event.c @@ -4,8 +4,8 @@ #include "vamos-buffers/core/event.h" -static const shm_kind hole_kind = 1; -static const shm_kind last_special_kind = 1; +static const vms_kind hole_kind = 1; +static const vms_kind last_special_kind = 1; void initialize_events(void) { assert(hole_kind == 1 && "We assume that the 'hole_kind' is 1 for now"); @@ -14,22 +14,22 @@ void initialize_events(void) { void deinitialize_events(void) {} -bool shm_event_is_hole(shm_event *ev) { +bool vms_event_is_hole(vms_event *ev) { assert(hole_kind > 0); - return shm_event_kind(ev) == hole_kind; + return vms_event_kind(ev) == hole_kind; } -shm_kind shm_get_hole_kind(void) { +vms_kind vms_get_hole_kind(void) { assert(hole_kind > 0); assert(last_special_kind >= hole_kind); return hole_kind; } -shm_kind shm_get_last_special_kind(void) { +vms_kind vms_get_last_special_kind(void) { assert(last_special_kind > 0); return last_special_kind; } -shm_eventid shm_event_id(shm_event *event) { return event->id; } +vms_eventid vms_event_id(vms_event *event) { return event->id; } -shm_kind shm_event_kind(shm_event *event) { return event->kind; } +vms_kind vms_event_kind(vms_event *event) { return event->kind; } diff --git a/src/core/legacy/queue-test.c b/src/core/legacy/queue-test.c index bbecf970..e58bfc8a 100644 --- a/src/core/legacy/queue-test.c +++ b/src/core/legacy/queue-test.c @@ -6,20 +6,20 @@ int main(void) { int i; int num[] = {0, 1, 2, 3}; - shm_queue q; - shm_queue_init(&q, 3, sizeof(int)); + vms_queue q; + vms_queue_init(&q, 3, sizeof(int)); - assert(shm_queue_pop(&q, &i) == false); - assert(shm_queue_pop(&q, &i) == false); - assert(shm_queue_push(&q, &num[1]) == true); - assert(shm_queue_push(&q, &num[2]) == true); - assert(shm_queue_push(&q, &num[3]) == true); - assert(shm_queue_push(&q, &num[3]) == false); - assert(shm_queue_pop(&q, &i) == true && i == 1); - assert(shm_queue_push(&q, &num[3]) == true); - assert(shm_queue_pop(&q, &i) == true && i == 2); - assert(shm_queue_pop(&q, &i) == true && i == 3); - assert(shm_queue_pop(&q, &i) == true && i == 3); + assert(vms_queue_pop(&q, &i) == false); + assert(vms_queue_pop(&q, &i) == false); + assert(vms_queue_push(&q, &num[1]) == true); + assert(vms_queue_push(&q, &num[2]) == true); + assert(vms_queue_push(&q, &num[3]) == true); + assert(vms_queue_push(&q, &num[3]) == false); + assert(vms_queue_pop(&q, &i) == true && i == 1); + assert(vms_queue_push(&q, &num[3]) == true); + assert(vms_queue_pop(&q, &i) == true && i == 2); + assert(vms_queue_pop(&q, &i) == true && i == 3); + assert(vms_queue_pop(&q, &i) == true && i == 3); - shm_queue_destroy(&q); + vms_queue_destroy(&q); } diff --git a/src/core/legacy/queue.c b/src/core/legacy/queue.c index d97c72ec..7dfbe5bf 100644 --- a/src/core/legacy/queue.c +++ b/src/core/legacy/queue.c @@ -5,7 +5,7 @@ #include #include -void shm_queue_init(shm_queue *q, size_t size, size_t elem_size) { +void vms_queue_init(vms_queue *q, size_t size, size_t elem_size) { assert(q); assert(size > 0); assert(elem_size > 0); @@ -16,13 +16,13 @@ void shm_queue_init(shm_queue *q, size_t size, size_t elem_size) { q->data = malloc(size * elem_size); } -void shm_queue_destroy(shm_queue *q) { free(q->data); } +void vms_queue_destroy(vms_queue *q) { free(q->data); } // allocate space for one element // and return the pointer to this element. // If there is no room, NULL is returned. // The returned memory can be filled manually. -void *shm_queue_extend(shm_queue *q) { +void *vms_queue_extend(vms_queue *q) { // queue is full if (q->elem_num == q->size) return NULL; @@ -40,8 +40,8 @@ void *shm_queue_extend(shm_queue *q) { return pos; } -bool shm_queue_push(shm_queue *q, const void *elem) { - void *pos = shm_queue_extend(q); +bool vms_queue_push(vms_queue *q, const void *elem) { + void *pos = vms_queue_extend(q); if (pos) { memcpy(pos, elem, q->elem_size); return true; @@ -49,7 +49,7 @@ bool shm_queue_push(shm_queue *q, const void *elem) { return false; } -bool shm_queue_pop(shm_queue *q, void *buff) { +bool vms_queue_pop(vms_queue *q, void *buff) { if (q->elem_num == 0) { return false; } @@ -63,6 +63,6 @@ bool shm_queue_pop(shm_queue *q, void *buff) { return true; } -size_t shm_queue_max_size(shm_queue *q) { return q->size; } +size_t vms_queue_max_size(vms_queue *q) { return q->size; } -size_t shm_queue_size(shm_queue *q) { return q->elem_num; } +size_t vms_queue_size(vms_queue *q) { return q->elem_num; } diff --git a/src/core/legacy/queue.h b/src/core/legacy/queue.h index 96cd78d1..2782a7ed 100644 --- a/src/core/legacy/queue.h +++ b/src/core/legacy/queue.h @@ -4,24 +4,24 @@ #include #include -typedef struct _shm_queue { +typedef struct _vms_queue { size_t size; size_t elem_num; size_t elem_size; size_t head, tail; unsigned char *data; -} shm_queue; +} vms_queue; -void shm_queue_init(shm_queue *q, size_t size, size_t elem_size); -void shm_queue_destroy(shm_queue *q); -bool shm_queue_push(shm_queue *q, const void *elem); -bool shm_queue_pop(shm_queue *q, void *buff); -size_t shm_queue_size(shm_queue *q); -size_t shm_queue_max_size(shm_queue *q); +void vms_queue_init(vms_queue *q, size_t size, size_t elem_size); +void vms_queue_destroy(vms_queue *q); +bool vms_queue_push(vms_queue *q, const void *elem); +bool vms_queue_pop(vms_queue *q, void *buff); +size_t vms_queue_size(vms_queue *q); +size_t vms_queue_max_size(vms_queue *q); // allocate space for one element // and return the pointer to this element. // If there is no room, NULL is returned. // The returned memory can be filled manually. -void *shm_queue_extend(shm_queue *q); +void *vms_queue_extend(vms_queue *q); #endif // SHAMON_QUEUE_H diff --git a/src/core/list-embedded.c b/src/core/list-embedded.c index 6983356b..2ba80072 100644 --- a/src/core/list-embedded.c +++ b/src/core/list-embedded.c @@ -4,22 +4,22 @@ #include "vamos-buffers/core/list-embedded.h" -void shm_list_embedded_init(shm_list_embedded *list) { +void vms_list_embedded_init(vms_list_embedded *list) { list->prev = list; list->next = list; } -void shm_list_embedded_insert_after(shm_list_embedded *list, - shm_list_embedded *elm) { +void vms_list_embedded_insert_after(vms_list_embedded *list, + vms_list_embedded *elm) { elm->prev = list; elm->next = list->next; list->next = elm; elm->next->prev = elm; } -void shm_list_embedded_insert_list(shm_list_embedded *list, - shm_list_embedded *other) { - if (shm_list_embedded_empty(other)) +void vms_list_embedded_insert_list(vms_list_embedded *list, + vms_list_embedded *other) { + if (vms_list_embedded_empty(other)) return; other->next->prev = list; @@ -28,16 +28,16 @@ void shm_list_embedded_insert_list(shm_list_embedded *list, list->next = other->next; } -void shm_list_embedded_remove(shm_list_embedded *elm) { +void vms_list_embedded_remove(vms_list_embedded *elm) { elm->prev->next = elm->next; elm->next->prev = elm->prev; elm->prev = NULL; elm->next = NULL; } -size_t shm_list_embedded_size(const shm_list_embedded *list) { +size_t vms_list_embedded_size(const vms_list_embedded *list) { size_t n = 0; - shm_list_embedded *cur = list->next; + vms_list_embedded *cur = list->next; while (cur != list) { cur = cur->next; @@ -47,6 +47,6 @@ size_t shm_list_embedded_size(const shm_list_embedded *list) { return n; } -_Bool shm_list_embedded_empty(const shm_list_embedded *list) { +_Bool vms_list_embedded_empty(const vms_list_embedded *list) { return list->next == list; } diff --git a/src/core/list.c b/src/core/list.c index 392b08a7..89c1c2f2 100644 --- a/src/core/list.c +++ b/src/core/list.c @@ -4,10 +4,10 @@ #include "vamos-buffers/core/list.h" -void shm_list_init(shm_list *list) { memset(list, 0, sizeof(shm_list)); } +void vms_list_init(vms_list *list) { memset(list, 0, sizeof(vms_list)); } -void shm_list_destroy(shm_list *list, shm_list_elem_destroy_fn destroy) { - shm_list_elem *cur = list->first; +void vms_list_destroy(vms_list *list, vms_list_elem_destroy_fn destroy) { + vms_list_elem *cur = list->first; while (cur) { if (destroy) destroy(cur->data); @@ -16,8 +16,8 @@ void shm_list_destroy(shm_list *list, shm_list_elem_destroy_fn destroy) { } } -size_t shm_list_insert_elem_after(shm_list *list, shm_list_elem *elem, - shm_list_elem *new) { +size_t vms_list_insert_elem_after(vms_list *list, vms_list_elem *elem, + vms_list_elem *new) { assert(elem); new->next = elem->next; new->prev = elem; @@ -32,17 +32,17 @@ size_t shm_list_insert_elem_after(shm_list *list, shm_list_elem *elem, return ++list->size; } -size_t shm_list_insert_after(shm_list *list, shm_list_elem *elem, void *data) { +size_t vms_list_insert_after(vms_list *list, vms_list_elem *elem, void *data) { assert(elem); - shm_list_elem *new = malloc(sizeof(*new)); + vms_list_elem *new = malloc(sizeof(*new)); assert(new); new->data = data; - return shm_list_insert_elem_after(list, elem, new); + return vms_list_insert_elem_after(list, elem, new); } -size_t shm_list_insert_before(shm_list *list, shm_list_elem *elem, void *data) { +size_t vms_list_insert_before(vms_list *list, vms_list_elem *elem, void *data) { assert(elem); - shm_list_elem *new = malloc(sizeof(*new)); + vms_list_elem *new = malloc(sizeof(*new)); assert(new); new->data = data; new->next = elem; @@ -60,9 +60,9 @@ size_t shm_list_insert_before(shm_list *list, shm_list_elem *elem, void *data) { return ++list->size; } -size_t shm_list_prepend(shm_list *list, void *data) { +size_t vms_list_prepend(vms_list *list, void *data) { assert(!list->first || list->first->prev == NULL); - shm_list_elem *new = malloc(sizeof(*new)); + vms_list_elem *new = malloc(sizeof(*new)); assert(new); new->data = data; new->next = list->first; @@ -75,9 +75,9 @@ size_t shm_list_prepend(shm_list *list, void *data) { return ++list->size; } -size_t shm_list_append(shm_list *list, void *data) { +size_t vms_list_append(vms_list *list, void *data) { assert(list->last == NULL || list->last->next == NULL); - shm_list_elem *new = malloc(sizeof(*new)); + vms_list_elem *new = malloc(sizeof(*new)); assert(new); new->data = data; new->next = NULL; @@ -92,7 +92,7 @@ size_t shm_list_append(shm_list *list, void *data) { return ++list->size; } -size_t shm_list_append_elem(shm_list *list, shm_list_elem *elem) { +size_t vms_list_append_elem(vms_list *list, vms_list_elem *elem) { assert(list->last == NULL || list->last->next == NULL); assert(elem); elem->next = NULL; @@ -107,7 +107,7 @@ size_t shm_list_append_elem(shm_list *list, shm_list_elem *elem) { return ++list->size; } -size_t shm_list_remove(shm_list *list, shm_list_elem *elem) { +size_t vms_list_remove(vms_list *list, vms_list_elem *elem) { assert(elem); assert(list->size > 0); assert(list->first); @@ -143,18 +143,18 @@ size_t shm_list_remove(shm_list *list, shm_list_elem *elem) { return --list->size; } -size_t shm_list_size(shm_list *list) { +size_t vms_list_size(vms_list *list) { assert(list->size > 0 || (!list->first && !list->last)); assert(!list->first == !list->last); return list->size; } -shm_list_elem *shm_list_first(shm_list *list) { +vms_list_elem *vms_list_first(vms_list *list) { assert(!list->first == !list->last); return list->first; } -shm_list_elem *shm_list_last(shm_list *list) { +vms_list_elem *vms_list_last(vms_list *list) { assert(!list->first == !list->last); return list->last; } diff --git a/src/core/monitor.c b/src/core/monitor.c index 2060de00..8696ba95 100644 --- a/src/core/monitor.c +++ b/src/core/monitor.c @@ -8,79 +8,79 @@ #include "vamos-buffers/core/par_queue.h" #include "vamos-buffers/core/utils.h" -typedef struct _shm_monitor_buffer { - shm_par_queue buffer; // the buffer itself +typedef struct _vms_monitor_buffer { + vms_par_queue buffer; // the buffer itself bool finished; // the arbiter has finished? -} shm_monitor_buffer; +} vms_monitor_buffer; -size_t shm_monitor_buffer_sizeof(void) { return sizeof(shm_monitor_buffer); } -size_t shm_monitor_buffer_size(shm_monitor_buffer *buffer) { - return shm_par_queue_size(&buffer->buffer); +size_t vms_monitor_buffer_sizeof(void) { return sizeof(vms_monitor_buffer); } +size_t vms_monitor_buffer_size(vms_monitor_buffer *buffer) { + return vms_par_queue_size(&buffer->buffer); } -size_t shm_monitor_buffer_free_space(shm_monitor_buffer *buffer) { - return shm_par_queue_free_num(&buffer->buffer); +size_t vms_monitor_buffer_free_space(vms_monitor_buffer *buffer) { + return vms_par_queue_free_num(&buffer->buffer); } -size_t shm_monitor_buffer_capacity(shm_monitor_buffer *buffer) { - return shm_par_queue_capacity(&buffer->buffer); +size_t vms_monitor_buffer_capacity(vms_monitor_buffer *buffer) { + return vms_par_queue_capacity(&buffer->buffer); } -void shm_monitor_buffer_init(shm_monitor_buffer *buffer, size_t event_size, +void vms_monitor_buffer_init(vms_monitor_buffer *buffer, size_t event_size, size_t capacity) { - shm_par_queue_init(&buffer->buffer, capacity, event_size); + vms_par_queue_init(&buffer->buffer, capacity, event_size); buffer->finished = false; } -void shm_monitor_set_finished(shm_monitor_buffer *buffer) { +void vms_monitor_set_finished(vms_monitor_buffer *buffer) { buffer->finished = true; } -shm_monitor_buffer *shm_monitor_buffer_create(size_t event_size, +vms_monitor_buffer *vms_monitor_buffer_create(size_t event_size, size_t capacity) { - shm_monitor_buffer *b = malloc(shm_monitor_buffer_sizeof()); + vms_monitor_buffer *b = malloc(vms_monitor_buffer_sizeof()); assert(b && "Malloc failed"); - shm_monitor_buffer_init(b, event_size, capacity); + vms_monitor_buffer_init(b, event_size, capacity); return b; } -void shm_monitor_buffer_free(shm_monitor_buffer *buffer) { - shm_monitor_buffer_destroy(buffer); +void vms_monitor_buffer_free(vms_monitor_buffer *buffer) { + vms_monitor_buffer_destroy(buffer); free(buffer); } -void shm_monitor_buffer_destroy(shm_monitor_buffer *buffer) { - shm_par_queue_destroy(&buffer->buffer); +void vms_monitor_buffer_destroy(vms_monitor_buffer *buffer) { + vms_par_queue_destroy(&buffer->buffer); } -size_t shm_monitor_buffer_elem_size(shm_monitor_buffer *q) { - return shm_par_queue_elem_size(&q->buffer); +size_t vms_monitor_buffer_elem_size(vms_monitor_buffer *q) { + return vms_par_queue_elem_size(&q->buffer); } -shm_event *shm_monitor_buffer_top(shm_monitor_buffer *buffer) { - return shm_par_queue_top(&buffer->buffer); +vms_event *vms_monitor_buffer_top(vms_monitor_buffer *buffer) { + return vms_par_queue_top(&buffer->buffer); } -size_t shm_monitor_buffer_peek(shm_monitor_buffer *buffer, size_t n, +size_t vms_monitor_buffer_peek(vms_monitor_buffer *buffer, size_t n, void **data1, size_t *size1, void **data2, size_t *size2) { - return shm_par_queue_peek(&buffer->buffer, n, data1, size1, data2, size2); + return vms_par_queue_peek(&buffer->buffer, n, data1, size1, data2, size2); } -size_t shm_monitor_buffer_peek1(shm_monitor_buffer *buffer, void **data) { - return shm_par_queue_peek1(&buffer->buffer, data); +size_t vms_monitor_buffer_peek1(vms_monitor_buffer *buffer, void **data) { + return vms_par_queue_peek1(&buffer->buffer, data); } -void *shm_monitor_buffer_write_ptr_or_null(shm_monitor_buffer *q) { +void *vms_monitor_buffer_write_ptr_or_null(vms_monitor_buffer *q) { assert(!q->finished && "Asking a pointer from a finished buffer"); - return shm_par_queue_write_ptr(&q->buffer); + return vms_par_queue_write_ptr(&q->buffer); } -void *shm_monitor_buffer_write_ptr(shm_monitor_buffer *q) { +void *vms_monitor_buffer_write_ptr(vms_monitor_buffer *q) { assert(!q->finished && "Asking a pointer from a finished buffer"); - void *ptr = shm_par_queue_write_ptr(&q->buffer); + void *ptr = vms_par_queue_write_ptr(&q->buffer); if (ptr) return ptr; @@ -101,28 +101,28 @@ void *shm_monitor_buffer_write_ptr(shm_monitor_buffer *q) { #endif assert(!q->finished && "Asking a pointer from a finished buffer"); - ptr = shm_par_queue_write_ptr(&q->buffer); + ptr = vms_par_queue_write_ptr(&q->buffer); } while (!ptr); assert(ptr); return ptr; } -void shm_monitor_buffer_write_finish(shm_monitor_buffer *q) { +void vms_monitor_buffer_write_finish(vms_monitor_buffer *q) { assert(!q->finished && "Asking a pointer from a finished buffer"); - shm_par_queue_write_finish(&q->buffer); + vms_par_queue_write_finish(&q->buffer); } /* get an event from the stream, block until there is some and return it * or return NULL if the stream ended */ -void *fetch_arbiter_stream(shm_monitor_buffer *buffer) { +void *fetch_arbiter_stream(vms_monitor_buffer *buffer) { size_t sleep_time = SLEEP_TIME_INIT_NS; size_t spinned = 0; void *ev; while (1) { /* wait for the event */ - if ((ev = shm_monitor_buffer_top(buffer))) { + if ((ev = vms_monitor_buffer_top(buffer))) { return ev; } @@ -144,13 +144,13 @@ void *fetch_arbiter_stream(shm_monitor_buffer *buffer) { abort(); } -void shm_monitor_buffer_consume(shm_monitor_buffer *buffer, size_t k) { - shm_par_queue_drop(&buffer->buffer, k); +void vms_monitor_buffer_consume(vms_monitor_buffer *buffer, size_t k) { + vms_par_queue_drop(&buffer->buffer, k); } /* wait for an event on the 'stream' -void shm_monitor_buffer_notify_dropped(shm_monitor_buffer *buffer, +void vms_monitor_buffer_notify_dropped(vms_monitor_buffer *buffer, uint64_t begin_id, uint64_t end_id) { - shm_stream_notify_dropped(buffer->stream, begin_id, end_id); + vms_stream_notify_dropped(buffer->stream, begin_id, end_id); } */ diff --git a/src/core/par_queue.c b/src/core/par_queue.c index a171fdfd..3d94caf0 100644 --- a/src/core/par_queue.c +++ b/src/core/par_queue.c @@ -9,12 +9,12 @@ #define __predict_false(x) __builtin_expect((x) != 0, 0) #define __predict_true(x) __builtin_expect((x) != 0, 1) -void shm_par_queue_init(shm_par_queue *q, size_t capacity, size_t elem_size) { +void vms_par_queue_init(vms_par_queue *q, size_t capacity, size_t elem_size) { assert(q); assert(capacity > 0); assert(elem_size > 0); - shm_spsc_ringbuf_init(&q->ringbuf, capacity + 1); + vms_spsc_ringbuf_init(&q->ringbuf, capacity + 1); q->capacity = capacity; q->elem_size = elem_size; @@ -25,79 +25,79 @@ void shm_par_queue_init(shm_par_queue *q, size_t capacity, size_t elem_size) { } } -void shm_par_queue_destroy(shm_par_queue *q) { free(q->data); } +void vms_par_queue_destroy(vms_par_queue *q) { free(q->data); } /* Pointer to the next writable slot */ -void *shm_par_queue_write_ptr(shm_par_queue *q) { +void *vms_par_queue_write_ptr(vms_par_queue *q) { size_t n; - size_t off = shm_spsc_ringbuf_write_off_nowrap(&q->ringbuf, &n); + size_t off = vms_spsc_ringbuf_write_off_nowrap(&q->ringbuf, &n); if (__predict_true(n > 0)) { return q->data + (off * q->elem_size); } return NULL; } -void shm_par_queue_write_finish(shm_par_queue *q) { - shm_spsc_ringbuf_write_finish(&q->ringbuf, 1); +void vms_par_queue_write_finish(vms_par_queue *q) { + vms_spsc_ringbuf_write_finish(&q->ringbuf, 1); } /* push an element into the queue. * 'size' is the actual size of the pushed element * and it must hold that 'size' <= 'elem_size' */ -bool shm_par_queue_push(shm_par_queue *q, const void *elem, size_t size) { +bool vms_par_queue_push(vms_par_queue *q, const void *elem, size_t size) { assert(q->elem_size >= size && "Size does not fit the slot"); - void *ptr = shm_par_queue_write_ptr(q); + void *ptr = vms_par_queue_write_ptr(q); if (__predict_true(ptr)) { memcpy(ptr, elem, size); - shm_par_queue_write_finish(q); + vms_par_queue_write_finish(q); return true; } return false; } -bool shm_par_queue_pop(shm_par_queue *q, void *buff) { +bool vms_par_queue_pop(vms_par_queue *q, void *buff) { size_t n; - const size_t off = shm_spsc_ringbuf_read_off_nowrap(&q->ringbuf, &n); + const size_t off = vms_spsc_ringbuf_read_off_nowrap(&q->ringbuf, &n); if (__predict_true(n > 0)) { memcpy(buff, q->data + (off * q->elem_size), q->elem_size); - shm_spsc_ringbuf_consume(&q->ringbuf, 1); + vms_spsc_ringbuf_consume(&q->ringbuf, 1); return true; } return false; } -void shm_par_queue_drop(shm_par_queue *q, size_t k) { - shm_spsc_ringbuf_consume(&q->ringbuf, k); +void vms_par_queue_drop(vms_par_queue *q, size_t k) { + vms_spsc_ringbuf_consume(&q->ringbuf, k); } -size_t shm_par_queue_free_num(shm_par_queue *q) { - return shm_spsc_ringbuf_free_num(&q->ringbuf); +size_t vms_par_queue_free_num(vms_par_queue *q) { + return vms_spsc_ringbuf_free_num(&q->ringbuf); } -size_t shm_par_queue_capacity(shm_par_queue *q) { return q->capacity; } +size_t vms_par_queue_capacity(vms_par_queue *q) { return q->capacity; } -size_t shm_par_queue_size(shm_par_queue *q) { - return shm_spsc_ringbuf_size(&q->ringbuf); +size_t vms_par_queue_size(vms_par_queue *q) { + return vms_spsc_ringbuf_size(&q->ringbuf); } -size_t shm_par_queue_elem_size(shm_par_queue *q) { return q->elem_size; } +size_t vms_par_queue_elem_size(vms_par_queue *q) { return q->elem_size; } -shm_event *shm_par_queue_top(shm_par_queue *q) { +vms_event *vms_par_queue_top(vms_par_queue *q) { size_t n; - const size_t off = shm_spsc_ringbuf_read_off_nowrap(&q->ringbuf, &n); + const size_t off = vms_spsc_ringbuf_read_off_nowrap(&q->ringbuf, &n); if (__predict_true(n > 0)) { - return (shm_event *)(q->data + (off * q->elem_size)); + return (vms_event *)(q->data + (off * q->elem_size)); } - return (shm_event *)0; + return (vms_event *)0; } /* n == 0 means arbitrary n */ -size_t shm_par_queue_peek(shm_par_queue *q, size_t n, void **ptr1, size_t *len1, +size_t vms_par_queue_peek(vms_par_queue *q, size_t n, void **ptr1, size_t *len1, void **ptr2, size_t *len2) { size_t off; - const size_t cur_elem_num = shm_spsc_ringbuf_peek( + const size_t cur_elem_num = vms_spsc_ringbuf_peek( &q->ringbuf, n == 0 ? ~((size_t)0) : n, &off, len1, len2); if (__predict_true(cur_elem_num > 0)) { *ptr1 = q->data + (off * q->elem_size); @@ -110,38 +110,38 @@ size_t shm_par_queue_peek(shm_par_queue *q, size_t n, void **ptr1, size_t *len1, } /* peak1 -- it is like top + return the number of elements */ -size_t shm_par_queue_peek1(shm_par_queue *q, void **data) { +size_t vms_par_queue_peek1(vms_par_queue *q, void **data) { size_t n; - const size_t off = shm_spsc_ringbuf_read_off_nowrap(&q->ringbuf, &n); + const size_t off = vms_spsc_ringbuf_read_off_nowrap(&q->ringbuf, &n); if (__predict_true(n > 0)) { *data = q->data + (off * q->elem_size); } return n; } -shm_event *shm_par_queue_peek_at(shm_par_queue *q, size_t k) { +vms_event *vms_par_queue_peek_at(vms_par_queue *q, size_t k) { unsigned char *ptr1, *ptr2; size_t len1, len2; - size_t n = shm_par_queue_peek(q, k == (~((size_t)0)) ? k : k + 1, + size_t n = vms_par_queue_peek(q, k == (~((size_t)0)) ? k : k + 1, (void **)&ptr1, &len1, (void **)&ptr2, &len2); if (n <= k) { return NULL; } if (k < len1) { - return (shm_event *)(ptr1 + (k * q->elem_size)); + return (vms_event *)(ptr1 + (k * q->elem_size)); } k -= len1; assert(k < len2); - return (shm_event *)(ptr2 + (k * q->elem_size)); + return (vms_event *)(ptr2 + (k * q->elem_size)); } -shm_event *shm_par_queue_peek_atmost_at(shm_par_queue *q, size_t *want_k) { +vms_event *vms_par_queue_peek_atmost_at(vms_par_queue *q, size_t *want_k) { void *ptr1, *ptr2; size_t len1, len2; size_t k = *want_k; - size_t n = shm_par_queue_peek(q, k == (~((size_t)0)) ? k : k + 1, &ptr1, + size_t n = vms_par_queue_peek(q, k == (~((size_t)0)) ? k : k + 1, &ptr1, &len1, &ptr2, &len2); if (n == 0) { *want_k = 0; @@ -152,9 +152,9 @@ shm_event *shm_par_queue_peek_atmost_at(shm_par_queue *q, size_t *want_k) { *want_k = k; } if (k < len1) { - return (shm_event *)((unsigned char *)ptr1 + (k * q->elem_size)); + return (vms_event *)((unsigned char *)ptr1 + (k * q->elem_size)); } k -= len1; assert(k < len2); - return (shm_event *)((unsigned char *)ptr2 + (k * q->elem_size)); + return (vms_event *)((unsigned char *)ptr2 + (k * q->elem_size)); } diff --git a/src/core/queue_spsc.c b/src/core/queue_spsc.c index af5b2ce3..0c17bbe8 100644 --- a/src/core/queue_spsc.c +++ b/src/core/queue_spsc.c @@ -9,7 +9,7 @@ #define __predict_false(x) __builtin_expect((x) != 0, 0) #define __predict_true(x) __builtin_expect((x) != 0, 1) -static inline void elem_num_inc(shm_queue_spsc *q, int k) { +static inline void elem_num_inc(vms_queue_spsc *q, int k) { /* Do ++q->elem_num atomically. */ /* The increment must come after everything is done. The release order makes sure that the written element @@ -17,16 +17,16 @@ static inline void elem_num_inc(shm_queue_spsc *q, int k) { atomic_fetch_add_explicit(&q->elem_num, k, memory_order_release); } -static inline void elem_num_dec(shm_queue_spsc *q, int k) { +static inline void elem_num_dec(vms_queue_spsc *q, int k) { /* Do q->elem_num -= k atomically. */ atomic_fetch_sub_explicit(&q->elem_num, k, memory_order_acquire); } -static inline size_t elem_num(shm_queue_spsc *q) { +static inline size_t elem_num(vms_queue_spsc *q) { return atomic_load_explicit(&q->elem_num, memory_order_relaxed); } -void shm_queue_spsc_init(shm_queue_spsc *q, size_t capacity) { +void vms_queue_spsc_init(vms_queue_spsc *q, size_t capacity) { assert(q); assert(capacity > 0); q->elem_num = 0; @@ -34,12 +34,12 @@ void shm_queue_spsc_init(shm_queue_spsc *q, size_t capacity) { q->head = q->tail = 0; } -void shm_queue_spsc_destroy(shm_queue_spsc *q) { +void vms_queue_spsc_destroy(vms_queue_spsc *q) { q->capacity = 0; q->elem_num = 0; } -bool shm_queue_spsc_write_offset(shm_queue_spsc *q, size_t *offset) { +bool vms_queue_spsc_write_offset(vms_queue_spsc *q, size_t *offset) { if (__predict_false(elem_num(q) == q->capacity)) { return false; } @@ -54,7 +54,7 @@ bool shm_queue_spsc_write_offset(shm_queue_spsc *q, size_t *offset) { return true; } -void shm_queue_spsc_writes_finish(shm_queue_spsc *q, size_t written_num) { +void vms_queue_spsc_writes_finish(vms_queue_spsc *q, size_t written_num) { assert(q->partial_head == q->head && "Inconsistent head"); assert(written_num <= q->writing_k_elems); assert(q->head + written_num <= q->capacity); @@ -69,11 +69,11 @@ void shm_queue_spsc_writes_finish(shm_queue_spsc *q, size_t written_num) { elem_num_inc(q, written_num); } -void shm_queue_spsc_write_finish(shm_queue_spsc *q) { - shm_queue_spsc_writes_finish(q, 1); +void vms_queue_spsc_write_finish(vms_queue_spsc *q) { + vms_queue_spsc_writes_finish(q, 1); } -size_t shm_queue_spsc_consume(shm_queue_spsc *q, size_t k) { +size_t vms_queue_spsc_consume(vms_queue_spsc *q, size_t k) { assert(k > 0); size_t num = elem_num(q); if (__predict_false(num < k)) { @@ -90,15 +90,15 @@ size_t shm_queue_spsc_consume(shm_queue_spsc *q, size_t k) { return k; } -size_t shm_queue_spsc_free_num(shm_queue_spsc *q) { +size_t vms_queue_spsc_free_num(vms_queue_spsc *q) { return q->capacity - elem_num(q); } -size_t shm_queue_spsc_capacity(shm_queue_spsc *q) { return q->capacity; } +size_t vms_queue_spsc_capacity(vms_queue_spsc *q) { return q->capacity; } -size_t shm_queue_spsc_size(shm_queue_spsc *q) { return elem_num(q); } +size_t vms_queue_spsc_size(vms_queue_spsc *q) { return elem_num(q); } -size_t shm_queue_spsc_peek(shm_queue_spsc *q, size_t n, size_t *off1, +size_t vms_queue_spsc_peek(vms_queue_spsc *q, size_t n, size_t *off1, size_t *len1, size_t *off2, size_t *len2) { size_t cur_elem_num = elem_num(q); if (n > cur_elem_num) @@ -120,27 +120,27 @@ size_t shm_queue_spsc_peek(shm_queue_spsc *q, size_t n, size_t *off1, } /* peak1 -- it is like top + return the number of elements */ -size_t shm_queue_spsc_read_offset(shm_queue_spsc *q, size_t *offset) { +size_t vms_queue_spsc_read_offset(vms_queue_spsc *q, size_t *offset) { *offset = q->tail; return elem_num(q); } /* -size_t shm_queue_spsc_peek_at(shm_queue_spsc *q, size_t idx) { +size_t vms_queue_spsc_peek_at(vms_queue_spsc *q, size_t idx) { size_t cur_elem_num = elem_num(q); if (idx >= cur_elem_num) return NULL; size_t end = k + q->tail; if (end >= q->capacity) { - return (shm_event *)(q->data + (end - q->capacity) * q->elem_size); + return (vms_event *)(q->data + (end - q->capacity) * q->elem_size); } - return (shm_event *)(q->data + (q->tail + k) * q->elem_size); + return (vms_event *)(q->data + (q->tail + k) * q->elem_size); } */ -size_t shm_queue_spsc_peek_atmost_at(shm_queue_spsc *q, size_t *want_k, +size_t vms_queue_spsc_peek_atmost_at(vms_queue_spsc *q, size_t *want_k, size_t *offset) { size_t num = elem_num(q); if (__predict_false(num == 0)) diff --git a/src/core/shamon.c b/src/core/shamon.c index aff02686..af095323 100644 --- a/src/core/shamon.c +++ b/src/core/shamon.c @@ -18,36 +18,36 @@ #include "vamos-buffers/core/vector-macro.h" typedef struct _shamon { - VEC(streams, shm_stream *); - /* shm_arbiter_buffers stored in this vector assume + VEC(streams, vms_stream *); + /* vms_arbiter_buffers stored in this vector assume * they are aligned in memory, so use aligned vector */ - shm_vector_aligned buffers; + vms_vector_aligned buffers; VEC(buffer_threads, thrd_t); /* callbacks and their data */ shamon_process_events_fn process_events; void *process_events_data; /* the memory for passing the next event in the default process_events handler */ - shm_event *_ev; + vms_event *_ev; size_t _ev_size; } shamon; -#define _buffers(shmn) ((shm_vector *)&shmn->buffers) +#define _buffers(shmn) ((vms_vector *)&shmn->buffers) #define SLEEP_NS_INIT (50) #define SLEEP_THRESHOLD_NS (10000000) static int default_buffer_manager_thrd(void *data) { - shm_arbiter_buffer *buffer = (shm_arbiter_buffer *)data; - shm_stream *stream = shm_arbiter_buffer_stream(buffer); - register shm_stream_alter_fn alter = stream->alter; - register shm_stream_filter_fn filter = stream->filter; + vms_arbiter_buffer *buffer = (vms_arbiter_buffer *)data; + vms_stream *stream = vms_arbiter_buffer_stream(buffer); + register vms_stream_alter_fn alter = stream->alter; + register vms_stream_filter_fn filter = stream->filter; // wait for buffer->active - while (!shm_arbiter_buffer_active(buffer)) _mm_pause(); + while (!vms_arbiter_buffer_active(buffer)) _mm_pause(); printf("Running fetch & autodrop for stream %s\n", - shm_stream_get_name(stream)); + vms_stream_get_name(stream)); void *ev, *out; while (1) { @@ -56,74 +56,74 @@ static int default_buffer_manager_thrd(void *data) { break; } if (filter && !filter(stream, ev)) { - shm_stream_consume(stream, 1); + vms_stream_consume(stream, 1); continue; } - out = shm_arbiter_buffer_write_ptr(buffer); + out = vms_arbiter_buffer_write_ptr(buffer); assert(out && "No space in the buffer"); alter(stream, ev, out); - shm_arbiter_buffer_write_finish(buffer); - shm_stream_consume(stream, 1); + vms_arbiter_buffer_write_finish(buffer); + vms_stream_consume(stream, 1); } // TODO: we should check if the stream is finished and remove it // in that case printf("BMM for stream %lu (%s) exits\n", stream->id, - shm_stream_get_type(stream)); + vms_stream_get_type(stream)); thrd_exit(EXIT_SUCCESS); } -static shm_event *default_process_events(shm_vector *buffers, void *data, - shm_stream **streamret) { +static vms_event *default_process_events(vms_vector *buffers, void *data, + vms_stream **streamret) { assert(buffers); assert(data); shamon *shmn = (shamon *)data; - shm_stream *stream; + vms_stream *stream; // use static counter to do round robin -- so that some stream // does not starve static unsigned i = 0; - shm_arbiter_buffer *buffer = NULL; + vms_arbiter_buffer *buffer = NULL; size_t qsize; // reset counter if we're at the end - if (i >= shm_vector_size(buffers)) + if (i >= vms_vector_size(buffers)) i = 0; - shm_event *inevent; + vms_event *inevent; - while (i < shm_vector_size(buffers)) { - buffer = ((shm_arbiter_buffer *)shm_vector_at(buffers, i)); - if (!shm_arbiter_buffer_active(buffer)) + while (i < vms_vector_size(buffers)) { + buffer = ((vms_arbiter_buffer *)vms_vector_at(buffers, i)); + if (!vms_arbiter_buffer_active(buffer)) continue; assert(buffer); - stream = shm_arbiter_buffer_stream(buffer); + stream = vms_arbiter_buffer_stream(buffer); ++i; - qsize = shm_arbiter_buffer_peek1(buffer, (void **)&inevent); + qsize = vms_arbiter_buffer_peek1(buffer, (void **)&inevent); if (qsize > 0) { assert(shmn->_ev); - const uint64_t c = shm_arbiter_buffer_capacity(buffer); + const uint64_t c = vms_arbiter_buffer_capacity(buffer); /* is the buffer full from 75 or more percent? */ if (qsize > 0.8 * c) { /* drop half of the buffer */ - shm_eventid id = shm_event_id(inevent); - if (!shm_arbiter_buffer_drop(buffer, c / 2)) { + vms_eventid id = vms_event_id(inevent); + if (!vms_arbiter_buffer_drop(buffer, c / 2)) { assert(0 && "Failed dropping events"); } - shm_stream_prepare_hole_event(stream, shmn->_ev, id, c / 2); + vms_stream_prepare_hole_event(stream, shmn->_ev, id, c / 2); *streamret = stream; return shmn->_ev; } /* TODO: ideally, we do not copy the event here but pass it directly * to the monitor */ - memcpy(shmn->_ev, inevent, shm_arbiter_buffer_elem_size(buffer)); + memcpy(shmn->_ev, inevent, vms_arbiter_buffer_elem_size(buffer)); #ifndef NDEBUG size_t n = #endif - shm_arbiter_buffer_drop(buffer, 1); + vms_arbiter_buffer_drop(buffer, 1); assert(n == 1); *streamret = stream; return shmn->_ev; @@ -141,10 +141,10 @@ shamon *shamon_create(shamon_process_events_fn process_events, assert(shmn); VEC_INIT(shmn->streams); - shm_vector_aligned_init(&shmn->buffers, shm_arbiter_buffer_sizeof(), + vms_vector_aligned_init(&shmn->buffers, vms_arbiter_buffer_sizeof(), CACHELINE_SIZE); VEC_INIT(shmn->buffer_threads); - shmn->_ev_size = sizeof(shm_event_default_hole); + shmn->_ev_size = sizeof(vms_event_default_hole); shmn->_ev = malloc(shmn->_ev_size); shmn->process_events = process_events ? process_events : default_process_events; @@ -153,9 +153,9 @@ shamon *shamon_create(shamon_process_events_fn process_events, return shmn; } -shm_vector *shamon_get_buffers(shamon *shmn) { return _buffers(shmn); } +vms_vector *shamon_get_buffers(shamon *shmn) { return _buffers(shmn); } -shm_stream **shamon_get_streams(shamon *shmn, size_t *size) { +vms_stream **shamon_get_streams(shamon *shmn, size_t *size) { *size = VEC_SIZE(shmn->streams); return shmn->streams; } @@ -165,42 +165,42 @@ shm_stream **shamon_get_streams(shamon *shmn, size_t *size) { * the program is about to be killed */ void shamon_detach(shamon *shmn) { for (size_t i = 0; i < VEC_SIZE(shmn->streams); ++i) { - shm_stream_detach(shmn->streams[i]); + vms_stream_detach(shmn->streams[i]); } } void shamon_destroy(shamon *shmn) { - assert(VEC_SIZE(shmn->buffer_threads) == shm_vector_size(_buffers(shmn))); + assert(VEC_SIZE(shmn->buffer_threads) == vms_vector_size(_buffers(shmn))); for (size_t i = 0; i < VEC_SIZE(shmn->buffer_threads); ++i) { thrd_join(shmn->buffer_threads[i], NULL); /* - shm_arbiter_buffer *buff - = *((shm_arbiter_buffer **)shm_vector_at(_buffers(shmn), i)); - shm_arbiter_buffer_set_active(buff, false); - shm_arbiter_buffer_destroy(buff); + vms_arbiter_buffer *buff + = *((vms_arbiter_buffer **)vms_vector_at(_buffers(shmn), i)); + vms_arbiter_buffer_set_active(buff, false); + vms_arbiter_buffer_destroy(buff); */ } for (size_t i = 0; i < VEC_SIZE(shmn->streams); ++i) { - if (!shm_stream_is_substream(shmn->streams[i])) { - shm_stream_destroy(shmn->streams[i]); + if (!vms_stream_is_substream(shmn->streams[i])) { + vms_stream_destroy(shmn->streams[i]); } } VEC_DESTROY(shmn->streams); VEC_DESTROY(shmn->buffer_threads); - shm_vector_destroy(_buffers(shmn)); + vms_vector_destroy(_buffers(shmn)); free(shmn->_ev); free(shmn); } bool shamon_is_ready(shamon *shmn) { for (size_t i = 0; i < VEC_SIZE(shmn->streams); ++i) { - shm_stream *s = shmn->streams[i]; - if (shm_stream_is_ready(s) || shm_stream_has_new_substreams(s)) { + vms_stream *s = shmn->streams[i]; + if (vms_stream_is_ready(s) || vms_stream_has_new_substreams(s)) { return true; } else { - shm_arbiter_buffer *buff = shm_vector_at(_buffers(shmn), i); - if (shm_arbiter_buffer_size(buff) > 0) { + vms_arbiter_buffer *buff = vms_vector_at(_buffers(shmn), i); + if (vms_arbiter_buffer_size(buff) > 0) { return true; } } @@ -209,18 +209,18 @@ bool shamon_is_ready(shamon *shmn) { return false; } -shm_event *shamon_get_next_ev(shamon *shmn, shm_stream **streamret) { +vms_event *shamon_get_next_ev(shamon *shmn, vms_stream **streamret) { for (size_t i = 0; i < VEC_SIZE(shmn->streams); ++i) { - shm_stream *s = shmn->streams[i]; - shm_stream *new_stream = - shm_stream_create_substream(s, NULL, NULL, NULL, NULL, NULL); + vms_stream *s = shmn->streams[i]; + vms_stream *new_stream = + vms_stream_create_substream(s, NULL, NULL, NULL, NULL, NULL); if (new_stream) { fprintf(stderr, "Stream %lu has a new dynamic substream\n", - shm_stream_id(s)); - shm_stream_register_all_events(new_stream); - shm_arbiter_buffer *buffer = shm_vector_at(_buffers(shmn), i); + vms_stream_id(s)); + vms_stream_register_all_events(new_stream); + vms_arbiter_buffer *buffer = vms_vector_at(_buffers(shmn), i); shamon_add_stream(shmn, new_stream, - shm_arbiter_buffer_capacity(buffer)); + vms_arbiter_buffer_capacity(buffer)); } } @@ -228,7 +228,7 @@ shm_event *shamon_get_next_ev(shamon *shmn, shm_stream **streamret) { streamret); } -void shamon_add_stream(shamon *shmn, shm_stream *stream, +void shamon_add_stream(shamon *shmn, vms_stream *stream, size_t buffer_capacity) { for (unsigned i = 0; i < VEC_SIZE(shmn->streams); ++i) { if (strcmp(shmn->streams[i]->name, stream->name) == 0) { @@ -239,7 +239,7 @@ void shamon_add_stream(shamon *shmn, shm_stream *stream, VEC_PUSH(shmn->streams, &stream); assert(shmn->streams[VEC_SIZE(shmn->streams) - 1] == stream && - "BUG: shm_vector_push"); + "BUG: vms_vector_push"); size_t strm_event_size = stream->hole_handling.hole_event_size; if (stream->event_size > strm_event_size) @@ -249,9 +249,9 @@ void shamon_add_stream(shamon *shmn, shm_stream *stream, shmn->_ev_size = strm_event_size; } - shm_arbiter_buffer *buffer = shm_vector_aligned_extend(_buffers(shmn)); + vms_arbiter_buffer *buffer = vms_vector_aligned_extend(_buffers(shmn)); /* 0 as the output event size means same as the stream */ - shm_arbiter_buffer_init(buffer, stream, + vms_arbiter_buffer_init(buffer, stream, /* output event size = */ 0, buffer_capacity); thrd_t thread_id; @@ -261,5 +261,5 @@ void shamon_add_stream(shamon *shmn, shm_stream *stream, printf("Added a stream id %lu: '%s'\n", VEC_SIZE(shmn->streams) - 1, stream->type); - shm_arbiter_buffer_set_active(buffer, true); + vms_arbiter_buffer_set_active(buffer, true); } diff --git a/src/core/source.c b/src/core/source.c index 849d80cc..59a36448 100644 --- a/src/core/source.c +++ b/src/core/source.c @@ -48,7 +48,7 @@ static inline void init_record(struct event_record *ev, const char *name, ev->signature[max_sig_size] = '\0'; ev->size = - signature_get_size((const unsigned char *)sig) + sizeof(shm_event); + signature_get_size((const unsigned char *)sig) + sizeof(vms_event); ev->kind = 0; } diff --git a/src/core/spsc_ringbuf.c b/src/core/spsc_ringbuf.c index 4972f326..1c87909c 100644 --- a/src/core/spsc_ringbuf.c +++ b/src/core/spsc_ringbuf.c @@ -6,7 +6,7 @@ #define __predict_false(x) __builtin_expect((x) != 0, 0) #define __predict_true(x) __builtin_expect((x) != 0, 1) -void shm_spsc_ringbuf_init(shm_spsc_ringbuf *b, size_t capacity) { +void vms_spsc_ringbuf_init(vms_spsc_ringbuf *b, size_t capacity) { /* we use one element as a separator */ assert(capacity > 0); assert(capacity < SIZE_MAX - 1 && "Arith. in operations can overflow."); @@ -94,40 +94,40 @@ static inline size_t get_free_num(size_t head, size_t tail, size_t capacity) { return ret; } -size_t shm_spsc_ringbuf_capacity(shm_spsc_ringbuf *b) { +size_t vms_spsc_ringbuf_capacity(vms_spsc_ringbuf *b) { /* we use one element as a separator */ return b->capacity; } -size_t shm_spsc_ringbuf_max_size(shm_spsc_ringbuf *b) { +size_t vms_spsc_ringbuf_max_size(vms_spsc_ringbuf *b) { /* we use one element as a separator */ return b->capacity - 1; } -size_t shm_spsc_ringbuf_size(shm_spsc_ringbuf *b) { +size_t vms_spsc_ringbuf_size(vms_spsc_ringbuf *b) { return get_written_num(atomic_load_explicit(&b->head, memory_order_relaxed), atomic_load_explicit(&b->tail, memory_order_relaxed), b->capacity); } -bool shm_spsc_ringbuf_empty(shm_spsc_ringbuf *b) { +bool vms_spsc_ringbuf_empty(vms_spsc_ringbuf *b) { return _is_empty(atomic_load_explicit(&b->head, memory_order_relaxed), atomic_load_explicit(&b->tail, memory_order_relaxed)); } -size_t shm_spsc_ringbuf_free_num(shm_spsc_ringbuf *b) { +size_t vms_spsc_ringbuf_free_num(vms_spsc_ringbuf *b) { return get_free_num(atomic_load_explicit(&b->head, memory_order_relaxed), atomic_load_explicit(&b->tail, memory_order_relaxed), b->capacity); } /** - * @brief shm_spsc_ringbuf_full checks if the ringbuffer is full. + * @brief vms_spsc_ringbuf_full checks if the ringbuffer is full. * It ignores cached head/tail and queries the shared head/tail directly. * However, no synchronization is used (the memory order is relaxed). * @return true if the ringbuf is full else false */ -bool shm_spsc_ringbuf_full(shm_spsc_ringbuf *b) { +bool vms_spsc_ringbuf_full(vms_spsc_ringbuf *b) { size_t head = atomic_load_explicit(&b->head, memory_order_relaxed); size_t tail = atomic_load_explicit(&b->tail, memory_order_relaxed); return __predict_false(_is_full(head, tail, b->capacity)); @@ -167,7 +167,7 @@ static inline size_t get_write_off(size_t head, size_t tail, size_t capacity, return tail - head - 1; } -size_t shm_spsc_ringbuf_write_off(shm_spsc_ringbuf *b, size_t *n, +size_t vms_spsc_ringbuf_write_off(vms_spsc_ringbuf *b, size_t *n, size_t *wrap_n) { const size_t head = atomic_load_explicit(&b->head, memory_order_acquire); @@ -184,7 +184,7 @@ size_t shm_spsc_ringbuf_write_off(shm_spsc_ringbuf *b, size_t *n, return head; } -size_t shm_spsc_ringbuf_write_off_nowrap(shm_spsc_ringbuf *b, size_t *n) { +size_t vms_spsc_ringbuf_write_off_nowrap(vms_spsc_ringbuf *b, size_t *n) { const size_t head = atomic_load_explicit(&b->head, memory_order_acquire); /* Update the cache if seen_tail is 0 (which very likely means it has not been updated yet, @@ -205,7 +205,7 @@ size_t shm_spsc_ringbuf_write_off_nowrap(shm_spsc_ringbuf *b, size_t *n) { } /* Ask for at least *n elements */ -size_t shm_spsc_ringbuf_acquire(shm_spsc_ringbuf *b, size_t *n, +size_t vms_spsc_ringbuf_acquire(vms_spsc_ringbuf *b, size_t *n, size_t *wrap_n) { const size_t head = atomic_load_explicit(&b->head, memory_order_acquire); @@ -224,7 +224,7 @@ size_t shm_spsc_ringbuf_acquire(shm_spsc_ringbuf *b, size_t *n, } /* Ask for at least *n elements */ -size_t shm_spsc_ringbuf_acquire_nowrap(shm_spsc_ringbuf *b, size_t *n) { +size_t vms_spsc_ringbuf_acquire_nowrap(vms_spsc_ringbuf *b, size_t *n) { const size_t head = atomic_load_explicit(&b->head, memory_order_acquire); size_t req = *n; @@ -241,7 +241,7 @@ size_t shm_spsc_ringbuf_acquire_nowrap(shm_spsc_ringbuf *b, size_t *n) { return head; } -void shm_spsc_ringbuf_write_finish(shm_spsc_ringbuf *b, size_t n) { +void vms_spsc_ringbuf_write_finish(vms_spsc_ringbuf *b, size_t n) { assert(n <= b->capacity); assert((b->capacity < (~((size_t)0)) - n) && "Possible overflow"); #ifndef NDEBUG @@ -265,7 +265,7 @@ void shm_spsc_ringbuf_write_finish(shm_spsc_ringbuf *b, size_t n) { atomic_store_explicit(&b->head, new_head, memory_order_release); } -size_t shm_spsc_ringbuf_read_off_nowrap(shm_spsc_ringbuf *b, size_t *n) { +size_t vms_spsc_ringbuf_read_off_nowrap(vms_spsc_ringbuf *b, size_t *n) { const size_t tail = atomic_load_explicit(&b->tail, memory_order_acquire); size_t tmp = get_written_num(b->seen_head, tail, b->capacity); if (tmp == 0) { @@ -278,7 +278,7 @@ size_t shm_spsc_ringbuf_read_off_nowrap(shm_spsc_ringbuf *b, size_t *n) { return tail; } -size_t shm_spsc_ringbuf_read_acquire(shm_spsc_ringbuf *b, size_t *n) { +size_t vms_spsc_ringbuf_read_acquire(vms_spsc_ringbuf *b, size_t *n) { const size_t tail = atomic_load_explicit(&b->tail, memory_order_acquire); size_t tmp = get_written_num(b->seen_head, tail, b->capacity); if (tmp < *n) { @@ -294,7 +294,7 @@ size_t shm_spsc_ringbuf_read_acquire(shm_spsc_ringbuf *b, size_t *n) { /* * Consume n items from the ringbuffer. There must be at least n items. */ -void shm_spsc_ringbuf_consume(shm_spsc_ringbuf *b, size_t n) { +void vms_spsc_ringbuf_consume(vms_spsc_ringbuf *b, size_t n) { assert(n > 0 && "Consume 0 elems"); assert(n <= b->capacity); assert((b->capacity < (~((size_t)0)) - n) && "Possible overflow"); @@ -312,7 +312,7 @@ void shm_spsc_ringbuf_consume(shm_spsc_ringbuf *b, size_t n) { * Consume up to n items from the ringbuffer. Return the number of consumed * events. */ -size_t shm_spsc_ringbuf_consume_upto(shm_spsc_ringbuf *b, size_t n) { +size_t vms_spsc_ringbuf_consume_upto(vms_spsc_ringbuf *b, size_t n) { assert(n <= b->capacity); assert((b->capacity < (~((size_t)0)) - n) && "Possible overflow"); @@ -328,14 +328,14 @@ size_t shm_spsc_ringbuf_consume_upto(shm_spsc_ringbuf *b, size_t n) { } if (n > 0) { - shm_spsc_ringbuf_consume(b, n); + vms_spsc_ringbuf_consume(b, n); } return n; } /* If return value is 0, values *off, *len1 and *len2 may not have been set */ -size_t shm_spsc_ringbuf_peek(shm_spsc_ringbuf *b, size_t n, size_t *off, +size_t vms_spsc_ringbuf_peek(vms_spsc_ringbuf *b, size_t n, size_t *off, size_t *len1, size_t *len2) { const size_t tail = atomic_load_explicit(&b->tail, memory_order_acquire); size_t head = b->seen_head; diff --git a/src/core/stream.c b/src/core/stream.c index a0ca1564..2c4010c8 100644 --- a/src/core/stream.c +++ b/src/core/stream.c @@ -15,28 +15,28 @@ /* FIXME: we duplicate the counter in stream here. Maybe rather set the funs to * NULL? */ -static void default_hole_init(shm_event *ev) { - ev->kind = shm_get_hole_kind(); - ((shm_event_default_hole *)ev)->n = 0; +static void default_hole_init(vms_event *ev) { + ev->kind = vms_get_hole_kind(); + ((vms_event_default_hole *)ev)->n = 0; } -static void default_hole_update(shm_event *hole, shm_event *ev) { +static void default_hole_update(vms_event *hole, vms_event *ev) { (void)ev; - ++((shm_event_default_hole *)hole)->n; + ++((vms_event_default_hole *)hole)->n; } -static shm_stream_hole_handling default_hole_handling = { - .hole_event_size = sizeof(shm_event_default_hole), +static vms_stream_hole_handling default_hole_handling = { + .hole_event_size = sizeof(vms_event_default_hole), .init = default_hole_init, .update = default_hole_update}; static uint64_t last_stream_id = 0; -void shm_stream_init(shm_stream *stream, struct buffer *incoming_events_buffer, - size_t event_size, shm_stream_is_ready_fn is_ready, - shm_stream_filter_fn filter, shm_stream_alter_fn alter, - shm_stream_destroy_fn destroy, - const shm_stream_hole_handling *hole_handling, +void vms_stream_init(vms_stream *stream, struct buffer *incoming_events_buffer, + size_t event_size, vms_stream_is_ready_fn is_ready, + vms_stream_filter_fn filter, vms_stream_alter_fn alter, + vms_stream_destroy_fn destroy, + const vms_stream_hole_handling *hole_handling, const char *const type, const char *const name) { stream->id = ++last_stream_id; stream->event_size = event_size; @@ -72,18 +72,18 @@ void shm_stream_init(shm_stream *stream, struct buffer *incoming_events_buffer, VEC_INIT(stream->substreams); } -static void shm_substream_destroy(shm_stream *stream) { - shm_stream_detach(stream); +static void vms_substream_destroy(vms_stream *stream) { + vms_stream_detach(stream); for (unsigned i = 0; i < VEC_SIZE(stream->substreams); ++i) { - shm_stream *sub = stream->substreams[i]; - if (shm_stream_is_ready(sub)) { + vms_stream *sub = stream->substreams[i]; + if (vms_stream_is_ready(sub)) { fprintf(stderr, "warn: destroying substream %lu of %s (%lu) which is still " "ready\n", - shm_stream_id(sub), stream->name, shm_stream_id(stream)); + vms_stream_id(sub), stream->name, vms_stream_id(stream)); } - shm_substream_destroy(sub); + vms_substream_destroy(sub); } if (stream->destroy) { @@ -98,18 +98,18 @@ static void shm_substream_destroy(shm_stream *stream) { free(stream); } -void shm_stream_destroy(shm_stream *stream) { - shm_stream_detach(stream); +void vms_stream_destroy(vms_stream *stream) { + vms_stream_detach(stream); for (unsigned i = 0; i < VEC_SIZE(stream->substreams); ++i) { - shm_stream *sub = stream->substreams[i]; - if (shm_stream_is_ready(sub)) { + vms_stream *sub = stream->substreams[i]; + if (vms_stream_is_ready(sub)) { fprintf(stderr, "warn: destroying substream %lu of %s (%lu) which is still " "ready\n", - shm_stream_id(sub), stream->name, shm_stream_id(stream)); + vms_stream_id(sub), stream->name, vms_stream_id(stream)); } - shm_substream_destroy(sub); + vms_substream_destroy(sub); } if (stream->destroy) { @@ -124,32 +124,32 @@ void shm_stream_destroy(shm_stream *stream) { free(stream); } -size_t shm_stream_id(shm_stream *stream) { return stream->id; } +size_t vms_stream_id(vms_stream *stream) { return stream->id; } -const char *shm_stream_get_name(shm_stream *stream) { +const char *vms_stream_get_name(vms_stream *stream) { assert(stream); return stream->name; } -const char *shm_stream_get_type(shm_stream *stream) { +const char *vms_stream_get_type(vms_stream *stream) { assert(stream); return stream->type; } -_Bool shm_stream_has_new_substreams(shm_stream *stream) { +_Bool vms_stream_has_new_substreams(vms_stream *stream) { return buffer_get_sub_buffers_no(stream->incoming_events_buffer) > stream->substreams_no; } -_Bool shm_stream_is_substream(shm_stream *stream) { +_Bool vms_stream_is_substream(vms_stream *stream) { return stream->parent_stream != NULL; } -shm_stream *shm_stream_create_substream( - shm_stream *stream, shm_stream_is_ready_fn is_ready, - shm_stream_filter_fn filter, shm_stream_alter_fn alter, - shm_stream_destroy_fn destroy, shm_stream_hole_handling *hole_handling) { - if (!shm_stream_has_new_substreams(stream)) { +vms_stream *vms_stream_create_substream( + vms_stream *stream, vms_stream_is_ready_fn is_ready, + vms_stream_filter_fn filter, vms_stream_alter_fn alter, + vms_stream_destroy_fn destroy, vms_stream_hole_handling *hole_handling) { + if (!vms_stream_has_new_substreams(stream)) { return NULL; } size_t substream_no = ++stream->substreams_no; @@ -158,20 +158,20 @@ shm_stream *shm_stream_create_substream( struct buffer *shmbuffer = get_shared_buffer(key); assert(shmbuffer && "Getting the shm buffer failed"); - shm_stream *substream = xalloc(sizeof *substream); + vms_stream *substream = xalloc(sizeof *substream); size_t elem_size = buffer_elem_size(shmbuffer); assert(elem_size > 0); char *substream_name = - get_sub_buffer_key(shm_stream_get_name(stream), substream_no); + get_sub_buffer_key(vms_stream_get_name(stream), substream_no); - shm_stream_init( + vms_stream_init( substream, shmbuffer, elem_size, is_ready ? is_ready : stream->is_ready, filter ? filter : stream->filter, alter ? alter : stream->alter, destroy ? destroy : stream->destroy, hole_handling ? hole_handling : &stream->hole_handling, - shm_stream_get_type(stream), substream_name); + vms_stream_get_type(stream), substream_name); substream->parent_stream = stream; free(substream_name); @@ -180,14 +180,14 @@ shm_stream *shm_stream_create_substream( return substream; } -struct event_record *shm_stream_get_avail_events(shm_stream *s, size_t *sz) { +struct event_record *vms_stream_get_avail_events(vms_stream *s, size_t *sz) { return buffer_get_avail_events(s->incoming_events_buffer, sz); } #define MAX_EVENTS_CACHE_SIZE 128 -static shm_kind get_max_kind(struct event_record *recs, size_t size) { - shm_kind ret = 0; +static vms_kind get_max_kind(struct event_record *recs, size_t size) { + vms_kind ret = 0; for (size_t i = 0; i < size; ++i) { if (recs[i].kind > ret) ret = recs[i].kind; @@ -196,8 +196,8 @@ static shm_kind get_max_kind(struct event_record *recs, size_t size) { return ret; } -struct event_record *shm_stream_get_event_record(shm_stream *stream, - shm_kind kind) { +struct event_record *vms_stream_get_event_record(vms_stream *stream, + vms_kind kind) { assert(kind > 0 && "Got invalid kind"); struct event_record *rec = NULL; if (stream->events_cache) { @@ -206,7 +206,7 @@ struct event_record *shm_stream_get_event_record(shm_stream *stream, assert(rec->kind == kind); return rec; } else { - return shm_stream_get_event_record_no_cache(stream, kind); + return vms_stream_get_event_record_no_cache(stream, kind); } } else { /* create cache */ @@ -232,8 +232,8 @@ struct event_record *shm_stream_get_event_record(shm_stream *stream, } } -struct event_record *shm_stream_get_event_record_no_cache(shm_stream *stream, - shm_kind kind) { +struct event_record *vms_stream_get_event_record_no_cache(vms_stream *stream, + vms_kind kind) { size_t sz; struct event_record *recs = buffer_get_avail_events(stream->incoming_events_buffer, &sz); @@ -245,17 +245,17 @@ struct event_record *shm_stream_get_event_record_no_cache(shm_stream *stream, } /* the number of elements in the (shared memory) buffer of the stream */ -size_t shm_stream_buffer_size(shm_stream *s) { +size_t vms_stream_buffer_size(vms_stream *s) { return buffer_size(s->incoming_events_buffer); } /* the capacity the (shared memory) buffer of the stream */ -size_t shm_stream_buffer_capacity(shm_stream *s) { +size_t vms_stream_buffer_capacity(vms_stream *s) { return buffer_capacity(s->incoming_events_buffer); } /* FIXME: no longer related to stream */ -void shm_stream_prepare_hole_event(shm_stream *stream, shm_event *hole_event, +void vms_stream_prepare_hole_event(vms_stream *stream, vms_event *hole_event, size_t id, uint64_t n) { assert(hole_event->kind > 0 && "init fun set wrong kind"); hole_event->id = id; @@ -267,38 +267,38 @@ void shm_stream_prepare_hole_event(shm_stream *stream, shm_event *hole_event, #endif } -bool shm_stream_is_ready(shm_stream *s) { return s->is_ready(s); } +bool vms_stream_is_ready(vms_stream *s) { return s->is_ready(s); } -bool shm_stream_is_finished(shm_stream *s) { - return shm_stream_buffer_size(s) == 0 && !shm_stream_is_ready(s); +bool vms_stream_is_finished(vms_stream *s) { + return vms_stream_buffer_size(s) == 0 && !vms_stream_is_ready(s); } -void *shm_stream_read_events(shm_stream *s, size_t *num) { +void *vms_stream_read_events(vms_stream *s, size_t *num) { /* the buffer may be already destroyed on the client's side, * but still may have some events to read */ - /* assert(shm_stream_is_ready(s)); */ + /* assert(vms_stream_is_ready(s)); */ return buffer_read_pointer(s->incoming_events_buffer, num); } -bool shm_stream_consume(shm_stream *stream, size_t num) { +bool vms_stream_consume(vms_stream *stream, size_t num) { #ifdef DUMP_STATS stream->consumed_events += num; #endif return buffer_drop_k(stream->incoming_events_buffer, num); } -const char *shm_stream_get_str(shm_stream *stream, uint64_t elem) { +const char *vms_stream_get_str(vms_stream *stream, uint64_t elem) { return buffer_get_str(stream->incoming_events_buffer, elem); } -size_t shm_stream_event_size(shm_stream *s) { return s->event_size; } +size_t vms_stream_event_size(vms_stream *s) { return s->event_size; } -int shm_stream_register_event(shm_stream *stream, const char *name, +int vms_stream_register_event(vms_stream *stream, const char *name, size_t kind) { return buffer_register_event(stream->incoming_events_buffer, name, kind); } -int shm_stream_register_events(shm_stream *stream, size_t ev_nums, ...) { +int vms_stream_register_events(vms_stream *stream, size_t ev_nums, ...) { va_list ap; va_start(ap, ev_nums); int ret = @@ -307,28 +307,28 @@ int shm_stream_register_events(shm_stream *stream, size_t ev_nums, ...) { return ret; } -int shm_stream_register_all_events(shm_stream *stream) { +int vms_stream_register_all_events(vms_stream *stream) { return buffer_register_all_events(stream->incoming_events_buffer); } -void shm_stream_notify_last_processed_id(shm_stream *stream, shm_eventid id) { +void vms_stream_notify_last_processed_id(vms_stream *stream, vms_eventid id) { buffer_set_last_processed_id(stream->incoming_events_buffer, id); } -void shm_stream_notify_dropped(shm_stream *stream, uint64_t begin_id, +void vms_stream_notify_dropped(vms_stream *stream, uint64_t begin_id, uint64_t end_id) { buffer_notify_dropped(stream->incoming_events_buffer, begin_id, end_id); } -void shm_stream_attach(shm_stream *stream) { +void vms_stream_attach(vms_stream *stream) { buffer_set_attached(stream->incoming_events_buffer, true); } -void shm_stream_detach(shm_stream *stream) { +void vms_stream_detach(vms_stream *stream) { buffer_set_attached(stream->incoming_events_buffer, false); } -void shm_stream_dump_events(shm_stream *stream) { +void vms_stream_dump_events(vms_stream *stream) { size_t evs_num; struct event_record *events = buffer_get_avail_events(stream->incoming_events_buffer, &evs_num); diff --git a/src/core/vector-aligned.c b/src/core/vector-aligned.c index 12b5b2ea..cd212e48 100644 --- a/src/core/vector-aligned.c +++ b/src/core/vector-aligned.c @@ -5,24 +5,24 @@ #include "vamos-buffers/core/vector-aligned.h" #include "vamos-buffers/core/utils.h" -void shm_vector_aligned_init(shm_vector_aligned *vec, size_t elem_size, +void vms_vector_aligned_init(vms_vector_aligned *vec, size_t elem_size, size_t alignment) { assert(alignment > 0); - shm_vector_init(&vec->vec, elem_size); + vms_vector_init(&vec->vec, elem_size); vec->alignment = alignment; } -void shm_vector_aligned_swap(shm_vector *vec, shm_vector *with) { - shm_vector_swap(vec, with); +void vms_vector_aligned_swap(vms_vector *vec, vms_vector *with) { + vms_vector_swap(vec, with); - shm_vector_aligned *avec = (shm_vector_aligned *)vec; + vms_vector_aligned *avec = (vms_vector_aligned *)vec; size_t alignment = avec->alignment; - avec->alignment = ((shm_vector_aligned *)with)->alignment; - ((shm_vector_aligned *)with)->alignment = alignment; + avec->alignment = ((vms_vector_aligned *)with)->alignment; + ((vms_vector_aligned *)with)->alignment = alignment; } -void shm_vector_aligned_resize(shm_vector *vec, size_t size) { - shm_vector_aligned *avec = (shm_vector_aligned *)vec; +void vms_vector_aligned_resize(vms_vector *vec, size_t size) { + vms_vector_aligned *avec = (vms_vector_aligned *)vec; if (vec->size >= size) return; @@ -42,8 +42,8 @@ void shm_vector_aligned_resize(shm_vector *vec, size_t size) { vec->size = size; } -void *shm_vector_aligned_extend(shm_vector *vec) { - shm_vector_aligned *avec = (shm_vector_aligned *)vec; +void *vms_vector_aligned_extend(vms_vector *vec) { + vms_vector_aligned *avec = (vms_vector_aligned *)vec; if (vec->size >= vec->alloc_size) { // TODO: exp. growth? @@ -63,7 +63,7 @@ void *shm_vector_aligned_extend(shm_vector *vec) { return addr; } -size_t shm_vector_aligned_push(shm_vector *vec, void *elem) { - memcpy(shm_vector_aligned_extend(vec), elem, vec->element_size); +size_t vms_vector_aligned_push(vms_vector *vec, void *elem) { + memcpy(vms_vector_aligned_extend(vec), elem, vec->element_size); return vec->size; } diff --git a/src/core/vector.c b/src/core/vector.c index 8a967403..17a4f405 100644 --- a/src/core/vector.c +++ b/src/core/vector.c @@ -4,15 +4,15 @@ #include #include -void shm_vector_init(shm_vector *vec, size_t elem_size) { +void vms_vector_init(vms_vector *vec, size_t elem_size) { assert(elem_size > 0); memset(vec, 0, sizeof(*vec)); vec->element_size = elem_size; } -void shm_vector_destroy(shm_vector *vec) { free(vec->data); } +void vms_vector_destroy(vms_vector *vec) { free(vec->data); } -void shm_vector_swap(shm_vector *vec, shm_vector *with) { +void vms_vector_swap(vms_vector *vec, vms_vector *with) { assert(vec->element_size == with->element_size); void *data = vec->data; size_t size = vec->size; @@ -25,7 +25,7 @@ void shm_vector_swap(shm_vector *vec, shm_vector *with) { with->alloc_size = asize; } -void shm_vector_resize(shm_vector *vec, size_t size) { +void vms_vector_resize(vms_vector *vec, size_t size) { if (vec->size >= size) { return; } @@ -46,7 +46,7 @@ void shm_vector_resize(shm_vector *vec, size_t size) { vec->size = size; } -void *shm_vector_extend(shm_vector *vec) { +void *vms_vector_extend(vms_vector *vec) { if (vec->size >= vec->alloc_size) { // TODO: exp. growth? vec->alloc_size += 10; @@ -62,30 +62,30 @@ void *shm_vector_extend(shm_vector *vec) { return addr; } -size_t shm_vector_push(shm_vector *vec, void *elem) { - memcpy(shm_vector_extend(vec), elem, vec->element_size); +size_t vms_vector_push(vms_vector *vec, void *elem) { + memcpy(vms_vector_extend(vec), elem, vec->element_size); return vec->size; } -size_t shm_vector_pop(shm_vector *vec) { +size_t vms_vector_pop(vms_vector *vec) { assert(vec->size > 0); return --vec->size; } -size_t shm_vector_size(shm_vector *vec) { return vec->size; } +size_t vms_vector_size(vms_vector *vec) { return vec->size; } -size_t shm_vector_elem_size(shm_vector *vec) { +size_t vms_vector_elem_size(vms_vector *vec) { assert(0 < vec->element_size); return vec->element_size; } -void *shm_vector_at(shm_vector *vec, size_t idx) { +void *vms_vector_at(vms_vector *vec, size_t idx) { assert(idx < vec->size); assert(0 < vec->element_size); return (void *)(((unsigned char *)vec->data) + idx * vec->element_size); } -void *shm_vector_at_checked(shm_vector *vec, size_t idx) { +void *vms_vector_at_checked(vms_vector *vec, size_t idx) { assert(0 < vec->element_size); if (idx < vec->size) { @@ -95,7 +95,7 @@ void *shm_vector_at_checked(shm_vector *vec, size_t idx) { return NULL; } -void *shm_vector_top(shm_vector *vec) { +void *vms_vector_top(vms_vector *vec) { if (vec->size == 0) return NULL; assert(0 < vec->element_size); diff --git a/src/core/shm_string.c b/src/core/vms_string.c similarity index 58% rename from src/core/shm_string.c rename to src/core/vms_string.c index a3e52634..0a48f88b 100644 --- a/src/core/shm_string.c +++ b/src/core/vms_string.c @@ -1,19 +1,19 @@ -#include "vamos-buffers/core/shm_string.h" +#include "vamos-buffers/core/vms_string.h" #include #include #include -#include "vamos-buffers/core/shm_string-macro.h" +#include "vamos-buffers/core/vms_string-macro.h" -void shm_string_init(shm_string *s) { +void vms_string_init(vms_string *s) { STRING_INIT(s->data); memset(s, 0, sizeof(*s)); } -void shm_string_destroy(shm_string *s) { STRING_DESTROY(s->data); } +void vms_string_destroy(vms_string *s) { STRING_DESTROY(s->data); } -void shm_string_swap(shm_string *s, shm_string *with) { +void vms_string_swap(vms_string *s, vms_string *with) { char *data = s->data; size_t size = STRING_SIZE(s->data); size_t asize = STRING_ALLOC_SIZE(s->data); @@ -26,34 +26,34 @@ void shm_string_swap(shm_string *s, shm_string *with) { STRING_ALLOC_SIZE(with->data) = asize; } -void shm_string_grow(shm_string *s, size_t size) { +void vms_string_grow(vms_string *s, size_t size) { if (STRING_SIZE(s->data) >= size) return; STRING_GROW(s->data, size); } -char *shm_string_extend(shm_string *s) { +char *vms_string_extend(vms_string *s) { char *ret; STRING_EXTEND(s->data, ret); return ret; } -size_t shm_string_append(shm_string *s, char c) { +size_t vms_string_append(vms_string *s, char c) { STRING_APPEND(s->data, c); return STRING_SIZE(s->data); } -size_t shm_string_pop(shm_string *s) { return --STRING_SIZE(s->data); } +size_t vms_string_pop(vms_string *s) { return --STRING_SIZE(s->data); } -void shm_string_clear(shm_string *s) { STRING_SIZE(s->data) = 0; } +void vms_string_clear(vms_string *s) { STRING_SIZE(s->data) = 0; } -size_t shm_string_size(shm_string *s) { return STRING_SIZE(s->data); } +size_t vms_string_size(vms_string *s) { return STRING_SIZE(s->data); } /* * Return the pointer to the element at index 'idx' */ -char *shm_string_at_ptr(shm_string *s, ssize_t idx) { +char *vms_string_at_ptr(vms_string *s, ssize_t idx) { if (idx < 0) { assert((size_t)(-idx) <= STRING_SIZE(s->data) && "idx OOB"); return &s->data[STRING_SIZE(s->data) + idx]; @@ -62,6 +62,6 @@ char *shm_string_at_ptr(shm_string *s, ssize_t idx) { return &s->data[idx]; } -char shm_string_at(shm_string *s, ssize_t idx) { - return *shm_string_at_ptr(s, idx); +char vms_string_at(vms_string *s, ssize_t idx) { + return *vms_string_at_ptr(s, idx); } diff --git a/src/shmbuf/buffer-aux.c b/src/shmbuf/buffer-aux.c index 5dc28a5f..1889c21d 100644 --- a/src/shmbuf/buffer-aux.c +++ b/src/shmbuf/buffer-aux.c @@ -48,8 +48,8 @@ void aux_buffer_destroy(struct aux_buffer *buffer) { aux_buffer_release(buffer); - if (shamon_shm_unlink(key) != 0) { - perror("aux_buffer_destroy: shm_unlink failure"); + if (vms_shm_unlink(key) != 0) { + perror("aux_buffer_destroy: vms_unlink failure"); } } */ @@ -65,9 +65,9 @@ static struct aux_buffer *new_aux_buffer(struct buffer *buff, size_t size) { /* printf("Initializing aux buffer %s\n", key); */ - int fd = shamon_shm_open(key, O_RDWR | O_CREAT, buff->mode); + int fd = vms_shm_open(key, O_RDWR | O_CREAT, buff->mode); if (fd < 0) { - perror("shm_open"); + perror("vms_open"); abort(); } @@ -82,8 +82,8 @@ static struct aux_buffer *new_aux_buffer(struct buffer *buff, size_t size) { if (close(fd) == -1) { perror("closing fd after mmap failure"); } - if (shamon_shm_unlink(key) != 0) { - perror("shm_unlink after mmap failure"); + if (vms_shm_unlink(key) != 0) { + perror("vms_unlink after mmap failure"); } abort(); } @@ -98,8 +98,8 @@ static struct aux_buffer *new_aux_buffer(struct buffer *buff, size_t size) { VEC_PUSH(buff->aux_buffers, &ab); assert(VEC_TOP(buff->aux_buffers) == ab); - shm_list_append(&buff->aux_buffers_age, ab); - assert(shm_list_last(&buff->aux_buffers_age)->data == ab); + vms_list_append(&buff->aux_buffers_age, ab); + assert(vms_list_last(&buff->aux_buffers_age)->data == ab); buff->cur_aux_buff = ab; return ab; @@ -128,7 +128,7 @@ struct aux_buffer *writer_get_aux_buffer(struct buffer *buff, size_t size) { aux_buffer_free_space(buff->cur_aux_buff) < size) { /* try to find a free buffer */ struct aux_buffer *ab; - shm_list_elem *cur = shm_list_first(&buff->aux_buffers_age); + vms_list_elem *cur = vms_list_first(&buff->aux_buffers_age); while (cur) { ab = (struct aux_buffer *)cur->data; if (ab->last_event_id <= buff->shmbuffer->info.last_processed_id || @@ -139,10 +139,10 @@ struct aux_buffer *writer_get_aux_buffer(struct buffer *buff, size_t size) { ab->last_event_id = ~(0LL); } if (ab->reusable && ab->size >= size) { - assert(shm_list_last(&buff->aux_buffers_age)->data == + assert(vms_list_last(&buff->aux_buffers_age)->data == buff->cur_aux_buff); - shm_list_remove(&buff->aux_buffers_age, cur); - shm_list_append_elem(&buff->aux_buffers_age, cur); + vms_list_remove(&buff->aux_buffers_age, cur); + vms_list_append_elem(&buff->aux_buffers_age, cur); buff->cur_aux_buff = ab; ab->reusable = false; return ab; @@ -156,7 +156,7 @@ struct aux_buffer *writer_get_aux_buffer(struct buffer *buff, size_t size) { /* use the current buffer */ /* it must always be the last in the aux_buffers_age */ - assert(shm_list_last(&buff->aux_buffers_age)->data == buff->cur_aux_buff); + assert(vms_list_last(&buff->aux_buffers_age)->data == buff->cur_aux_buff); return buff->cur_aux_buff; } @@ -182,9 +182,9 @@ struct aux_buffer *reader_get_aux_buffer(struct buffer *buff, size_t idx) { // printf("Getting aux buffer %s\n", key); - int fd = shamon_shm_open(key, O_RDWR, S_IRWXU); + int fd = vms_shm_open(key, O_RDWR, S_IRWXU); if (fd < 0) { - perror("shm_open"); + perror("vms_open"); abort(); } @@ -202,8 +202,8 @@ struct aux_buffer *reader_get_aux_buffer(struct buffer *buff, size_t idx) { if (close(fd) == -1) { perror("closing fd after mmap failure"); } - if (shamon_shm_unlink(key) != 0) { - perror("shm_unlink after mmap failure"); + if (vms_shm_unlink(key) != 0) { + perror("vms_unlink after mmap failure"); } abort(); } diff --git a/src/shmbuf/buffer-control.c b/src/shmbuf/buffer-control.c index 8ba40fc4..b18f60ce 100644 --- a/src/shmbuf/buffer-control.c +++ b/src/shmbuf/buffer-control.c @@ -9,12 +9,12 @@ HIDE_SYMBOL struct source_control *get_shared_control_buffer(const char *buff_key) { char key[SHM_NAME_MAXLEN]; - shamon_map_ctrl_key(buff_key, key); + vms_shm_map_ctrl_key(buff_key, key); /* fprintf(stderr, "getting control buffer '%s'\n", key); */ - int fd = shamon_shm_open(key, O_RDWR | O_CREAT, S_IRWXU); + int fd = vms_shm_open(key, O_RDWR | O_CREAT, S_IRWXU); if (fd < 0) { - perror("shm_open"); + perror("vms_open"); return NULL; } @@ -31,8 +31,8 @@ struct source_control *get_shared_control_buffer(const char *buff_key) { if (close(fd) == -1) { perror("closing fd after mmap failure"); } - if (shamon_shm_unlink(key) != 0) { - perror("shm_unlink after mmap failure"); + if (vms_shm_unlink(key) != 0) { + perror("vms_unlink after mmap failure"); } return NULL; } @@ -45,21 +45,21 @@ HIDE_SYMBOL struct source_control *create_shared_control_buffer( const char *buff_key, mode_t mode, const struct source_control *control) { char key[SHM_NAME_MAXLEN]; - shamon_map_ctrl_key(buff_key, key); + vms_shm_map_ctrl_key(buff_key, key); size_t size = control->size; /* fprintf(stderr, "Initializing control buffer '%s' of size '%lu'\n", key, * size); */ char tmpkey[SHM_NAME_MAXLEN] = ""; - if (shamon_get_tmp_key(key, tmpkey, SHM_NAME_MAXLEN) == -1) { + if (vms_shm_get_tmp_key(key, tmpkey, SHM_NAME_MAXLEN) == -1) { fprintf(stderr, "Failed creating a tmpkey for '%s'\n", key); return NULL; } - int fd = shamon_shm_open(tmpkey, O_RDWR | O_CREAT, mode); + int fd = vms_shm_open(tmpkey, O_RDWR | O_CREAT, mode); if (fd < 0) { - perror("shm_open"); + perror("vms_open"); return NULL; } @@ -82,22 +82,22 @@ struct source_control *create_shared_control_buffer( if (close(fd) == -1) { perror("closing fd after mmap failure"); } - if (shamon_shm_unlink(key) != 0) { - perror("shm_unlink after mmap failure"); + if (vms_shm_unlink(key) != 0) { + perror("vms_unlink after mmap failure"); } return NULL; } memcpy(mem, control, size); - if (shamon_shm_rename(tmpkey, key) < 0) { + if (vms_shm_rename(tmpkey, key) < 0) { perror("renaming SHM file"); if (close(fd) == -1) { perror("closing fd after mmap failure"); } - if (shamon_shm_unlink(tmpkey) != 0) { - perror("shm_unlink after mmap failure"); + if (vms_shm_unlink(tmpkey) != 0) { + perror("vms_unlink after mmap failure"); } if (munmap(mem, size) != 0) { perror("munmap of control buffer"); @@ -123,8 +123,8 @@ void destroy_shared_control_buffer(const char *buffkey, release_shared_control_buffer(buffer); char key[SHM_NAME_MAXLEN]; - shamon_map_ctrl_key(buffkey, key); - if (shamon_shm_unlink(key) != 0) { - perror("destroy_shared_control_buffer: shm_unlink failure"); + vms_shm_map_ctrl_key(buffkey, key); + if (vms_shm_unlink(key) != 0) { + perror("destroy_shared_control_buffer: vms_unlink failure"); } } diff --git a/src/shmbuf/buffer-dbg.c b/src/shmbuf/buffer-dbg.c index e9ed13ce..00288baa 100644 --- a/src/shmbuf/buffer-dbg.c +++ b/src/shmbuf/buffer-dbg.c @@ -6,11 +6,11 @@ #include "shm.h" /** - * @brief The __shm_dbg_buffer class + * @brief The __vms_dbg_buffer class * - * vms_shm_dbg_buffer is a versioned array of key+value records + * vms_vms_dbg_buffer is a versioned array of key+value records */ -struct __shm_dbg_buffer { +struct __vms_dbg_buffer { CACHELINE_ALIGNED struct { /* allocation size of the buffer */ size_t allocation_size; @@ -30,18 +30,18 @@ struct __shm_dbg_buffer { unsigned char data[]; }; -typedef struct _vms_shm_dbg_buffer { +typedef struct _vms_vms_dbg_buffer { /* shared-memory buffer */ - struct __shm_dbg_buffer *buffer; + struct __vms_dbg_buffer *buffer; /* mmap fd, key, etc. */ int fd; char *key; /* data pointer */ unsigned char *data; /* = buffer->data */ -} vms_shm_dbg_buffer; +} vms_vms_dbg_buffer; -static void dbg_buffer_init(struct __shm_dbg_buffer *b, size_t allocation_size, +static void dbg_buffer_init(struct __vms_dbg_buffer *b, size_t allocation_size, size_t capacity, uint16_t key_size, uint16_t value_size) { b->info.allocation_size = allocation_size; @@ -52,26 +52,26 @@ static void dbg_buffer_init(struct __shm_dbg_buffer *b, size_t allocation_size, b->info.version = 0; } -vms_shm_dbg_buffer *vms_shm_dbg_buffer_create(const char *key, size_t capacity, +vms_vms_dbg_buffer *vms_vms_dbg_buffer_create(const char *key, size_t capacity, uint16_t key_size, uint16_t value_size) { printf("Initializing dbg buffer '%s' of capacity '%lu'\n", key, capacity); char tmpkey[SHM_NAME_MAXLEN] = ""; - if (shamon_get_tmp_key(key, tmpkey, SHM_NAME_MAXLEN) == -1) { + if (vms_shm_get_tmp_key(key, tmpkey, SHM_NAME_MAXLEN) == -1) { fprintf(stderr, "Failed creating a tmpkey for '%s'\n", key); return NULL; } - int fd = shamon_shm_open(tmpkey, O_RDWR | O_CREAT, S_IRWXU); + int fd = vms_shm_open(tmpkey, O_RDWR | O_CREAT, S_IRWXU); if (fd < 0) { - perror("shm_open"); + perror("vms_open"); return NULL; } - size_t allocation_size = compute_shm_buffer_size( - sizeof(struct __shm_dbg_buffer), key_size + value_size, capacity); - assert(allocation_size > sizeof(struct __shm_dbg_buffer)); + size_t allocation_size = compute_vms_buffer_size( + sizeof(struct __vms_dbg_buffer), key_size + value_size, capacity); + assert(allocation_size > sizeof(struct __vms_dbg_buffer)); if ((ftruncate(fd, allocation_size)) == -1) { perror("ftruncate"); @@ -85,21 +85,21 @@ vms_shm_dbg_buffer *vms_shm_dbg_buffer_create(const char *key, size_t capacity, if (close(fd) == -1) { perror("closing fd after mmap failure"); } - if (shamon_shm_unlink(key) != 0) { - perror("shm_unlink after mmap failure"); + if (vms_shm_unlink(key) != 0) { + perror("vms_unlink after mmap failure"); } return NULL; } - vms_shm_dbg_buffer *buff = (vms_shm_dbg_buffer *)xalloc(sizeof(*buff)); - buff->buffer = (struct __shm_dbg_buffer *)mem; + vms_vms_dbg_buffer *buff = (vms_vms_dbg_buffer *)xalloc(sizeof(*buff)); + buff->buffer = (struct __vms_dbg_buffer *)mem; dbg_buffer_init(buff->buffer, allocation_size, capacity, key_size, value_size); buff->data = buff->buffer->data; buff->key = xstrdup(key); buff->fd = fd; - if (shamon_shm_rename(tmpkey, key) < 0) { + if (vms_shm_rename(tmpkey, key) < 0) { perror("renaming SHM file"); free(buff); @@ -107,8 +107,8 @@ vms_shm_dbg_buffer *vms_shm_dbg_buffer_create(const char *key, size_t capacity, if (close(fd) == -1) { perror("closing fd after mmap failure"); } - if (shamon_shm_unlink(tmpkey) != 0) { - perror("shm_unlink after mmap failure"); + if (vms_shm_unlink(tmpkey) != 0) { + perror("vms_unlink after mmap failure"); } if (munmap(mem, allocation_size) != 0) { perror("munmap of control buffer"); @@ -119,16 +119,16 @@ vms_shm_dbg_buffer *vms_shm_dbg_buffer_create(const char *key, size_t capacity, return buff; } -vms_shm_dbg_buffer *vms_shm_dbg_buffer_get(const char *key) { +vms_vms_dbg_buffer *vms_vms_dbg_buffer_get(const char *key) { printf("Getting dbg buffer '%s'\n", key); - int fd = shamon_shm_open(key, O_RDWR | O_CREAT, S_IRWXU); + int fd = vms_shm_open(key, O_RDWR | O_CREAT, S_IRWXU); if (fd < 0) { - perror("shm_open"); + perror("vms_open"); return NULL; } - struct __shm_dbg_buffer tmp; + struct __vms_dbg_buffer tmp; if (pread(fd, &tmp, sizeof(tmp), 0) == -1) { perror("reading info about dbg buffer"); return NULL; @@ -150,8 +150,8 @@ vms_shm_dbg_buffer *vms_shm_dbg_buffer_get(const char *key) { return NULL; } - vms_shm_dbg_buffer *buff = (vms_shm_dbg_buffer *)xalloc(sizeof(*buff)); - buff->buffer = (struct __shm_dbg_buffer *)mem; + vms_vms_dbg_buffer *buff = (vms_vms_dbg_buffer *)xalloc(sizeof(*buff)); + buff->buffer = (struct __vms_dbg_buffer *)mem; buff->data = buff->buffer->data; buff->key = xstrdup(key); buff->fd = fd; @@ -159,7 +159,7 @@ vms_shm_dbg_buffer *vms_shm_dbg_buffer_get(const char *key) { return buff; } -void vms_shm_dbg_buffer_release(vms_shm_dbg_buffer *buff) { +void vms_vms_dbg_buffer_release(vms_vms_dbg_buffer *buff) { if (close(buff->fd) == -1) { perror("closing fd when releasing dbg buffer"); } @@ -171,41 +171,41 @@ void vms_shm_dbg_buffer_release(vms_shm_dbg_buffer *buff) { free(buff); } -void vms_shm_dbg_buffer_destroy(vms_shm_dbg_buffer *buff) { - if (shamon_shm_unlink(buff->key) != 0) { - perror("shm_unlink when destroying a dbg buffer"); +void vms_vms_dbg_buffer_destroy(vms_vms_dbg_buffer *buff) { + if (vms_shm_unlink(buff->key) != 0) { + perror("vms_unlink when destroying a dbg buffer"); } - vms_shm_dbg_buffer_release(buff); + vms_vms_dbg_buffer_release(buff); } -size_t vms_shm_dbg_buffer_size(vms_shm_dbg_buffer *b) { +size_t vms_vms_dbg_buffer_size(vms_vms_dbg_buffer *b) { return b->buffer->info.size; } -size_t vms_shm_dbg_buffer_capacity(vms_shm_dbg_buffer *b) { +size_t vms_vms_dbg_buffer_capacity(vms_vms_dbg_buffer *b) { return b->buffer->info.capacity; } -size_t vms_shm_dbg_buffer_key_size(vms_shm_dbg_buffer *b) { +size_t vms_vms_dbg_buffer_key_size(vms_vms_dbg_buffer *b) { return b->buffer->info.key_size; } -size_t vms_shm_dbg_buffer_value_size(vms_shm_dbg_buffer *b) { +size_t vms_vms_dbg_buffer_value_size(vms_vms_dbg_buffer *b) { return b->buffer->info.value_size; } -size_t vms_shm_dbg_buffer_rec_size(vms_shm_dbg_buffer *b) { +size_t vms_vms_dbg_buffer_rec_size(vms_vms_dbg_buffer *b) { return b->buffer->info.key_size + b->buffer->info.value_size; } -unsigned char *vms_shm_dbg_buffer_data(vms_shm_dbg_buffer *b) { +unsigned char *vms_vms_dbg_buffer_data(vms_vms_dbg_buffer *b) { return b->data; } -void vms_shm_dbg_buffer_inc_size(vms_shm_dbg_buffer *b, size_t size) { +void vms_vms_dbg_buffer_inc_size(vms_vms_dbg_buffer *b, size_t size) { b->buffer->info.size += size; } -size_t vms_shm_dbg_buffer_version(vms_shm_dbg_buffer *b) { +size_t vms_vms_dbg_buffer_version(vms_vms_dbg_buffer *b) { return b->buffer->info.version; } -void vms_shm_dbg_buffer_bump_version(vms_shm_dbg_buffer *b) { +void vms_vms_dbg_buffer_bump_version(vms_vms_dbg_buffer *b) { ++b->buffer->info.version; } diff --git a/src/shmbuf/buffer-local.c b/src/shmbuf/buffer-local.c index 51044b7d..7e86060b 100644 --- a/src/shmbuf/buffer-local.c +++ b/src/shmbuf/buffer-local.c @@ -29,7 +29,7 @@ struct buffer *initialize_local_buffer(const char *key, size_t elem_size, printf("Initializing LOCAL buffer '%s' with elem size '%lu'\n", key, elem_size); void *mem; - const size_t memsize = compute_shm_size(elem_size, capacity); + const size_t memsize = compute_vms_size(elem_size, capacity); int succ = posix_memalign(&mem, 64, memsize); if (succ != 0) { perror("allocation failure"); @@ -48,7 +48,7 @@ struct buffer *initialize_local_buffer(const char *key, size_t elem_size, /* ringbuf has one dummy element */ buff->shmbuffer->info.capacity = capacity; buff->shmbuffer->info.allocated_size = memsize; - shm_spsc_ringbuf_init(_ringbuf(buff), capacity + 1); + vms_spsc_ringbuf_init(_ringbuf(buff), capacity + 1); printf(" .. buffer allocated size = %lu, capacity = %lu\n", buff->shmbuffer->info.allocated_size, buff->shmbuffer->info.capacity); @@ -59,7 +59,7 @@ struct buffer *initialize_local_buffer(const char *key, size_t elem_size, buff->key = strdup(key); VEC_INIT(buff->aux_buffers); - shm_list_init(&buff->aux_buffers_age); + vms_list_init(&buff->aux_buffers_age); buff->aux_buf_idx = 0; buff->cur_aux_buff = NULL; buff->fd = -1; diff --git a/src/shmbuf/buffer-private.h b/src/shmbuf/buffer-private.h index 3aa4d0c1..4c667626 100644 --- a/src/shmbuf/buffer-private.h +++ b/src/shmbuf/buffer-private.h @@ -26,17 +26,17 @@ struct buffer; struct dropped_range { /* the range of autodropped events (for garbage collection) */ - shm_eventid begin; - shm_eventid end; + vms_eventid begin; + vms_eventid end; }; struct buffer_info { - shm_spsc_ringbuf ringbuf; + vms_spsc_ringbuf ringbuf; size_t allocated_size; size_t capacity; size_t elem_size; - shm_eventid last_processed_id; + vms_eventid last_processed_id; struct dropped_range dropped_ranges[DROPPED_RANGES_NUM]; size_t dropped_ranges_next; _Atomic _Bool dropped_ranges_lock; /* spin lock */ @@ -65,7 +65,7 @@ struct aux_buffer { /* TODO: cache the shared state in local state (e.g., elem_size, etc.). Maybe we could also inline - the shm_spsc_ringbuf so that we can keep local cache */ + the vms_spsc_ringbuf so that we can keep local cache */ struct buffer { struct shmbuffer *shmbuffer; struct source_control *control; @@ -74,7 +74,7 @@ struct buffer { /* the known aux_buffers that might still be needed */ VEC(aux_buffers, struct aux_buffer *); size_t aux_buf_idx; - shm_list aux_buffers_age; + vms_list aux_buffers_age; /* shm filedescriptor */ int fd; /* shm key */ @@ -94,8 +94,8 @@ struct buffer *initialize_shared_buffer(const char *key, mode_t mode, struct buffer *get_shared_buffer(const char *key); struct buffer *try_get_shared_buffer(const char *key, size_t retry); -size_t compute_shm_size(size_t elem_size, size_t capacity); -size_t compute_shm_buffer_size(size_t nondata_size, size_t elem_size, +size_t compute_vms_size(size_t elem_size, size_t capacity); +size_t compute_vms_buffer_size(size_t nondata_size, size_t elem_size, size_t capacity); /*** LOCAL buffers ***/ diff --git a/src/shmbuf/buffer-sub.c b/src/shmbuf/buffer-sub.c index 031dcd32..1846c777 100644 --- a/src/shmbuf/buffer-sub.c +++ b/src/shmbuf/buffer-sub.c @@ -105,8 +105,8 @@ void release_shared_sub_buffer(struct buffer *buff) { } VEC_DESTROY(buff->aux_buffers); - if (shamon_shm_unlink(buff->key) != 0) { - perror("release_shared_sub_buffer: shm_unlink failure"); + if (vms_shm_unlink(buff->key) != 0) { + perror("release_shared_sub_buffer: vms_unlink failure"); } destroy_shared_control_buffer(buff->key, buff->control); diff --git a/src/shmbuf/buffer.c b/src/shmbuf/buffer.c index aef5c1c0..5f21fab2 100644 --- a/src/shmbuf/buffer.c +++ b/src/shmbuf/buffer.c @@ -36,7 +36,7 @@ size_t buffer_capacity(struct buffer *buff) { } size_t buffer_size(struct buffer *buff) { - return shm_spsc_ringbuf_size(_ringbuf(buff)); + return vms_spsc_ringbuf_size(_ringbuf(buff)); } size_t buffer_elem_size(struct buffer *buff) { @@ -50,7 +50,7 @@ int buffer_get_key_path(struct buffer *buff, char keypath[], if (SHM_NAME_MAXLEN <= keypathsize) return -1; - if (shm_mapname(buff->key, keypath) == NULL) { + if (vms_shm_mapname(buff->key, keypath) == NULL) { return -2; } @@ -63,11 +63,11 @@ int buffer_get_ctrl_key_path(struct buffer *buff, char keypath[], return -1; char ctrlkey[SHM_NAME_MAXLEN]; - if (shamon_map_ctrl_key(buff->key, ctrlkey) == NULL) { + if (vms_shm_map_ctrl_key(buff->key, ctrlkey) == NULL) { return -3; } - if (shm_mapname(ctrlkey, keypath) == NULL) { + if (vms_shm_mapname(ctrlkey, keypath) == NULL) { return -2; } @@ -89,7 +89,7 @@ struct buffer *initialize_shared_buffer(const char *key, mode_t mode, assert(capacity > 0 && "Capacity is 0"); /* the ringbuffer has one unusable dummy element, so increase the capacity * by one */ - const size_t memsize = compute_shm_size(elem_size, capacity + 1); + const size_t memsize = compute_vms_size(elem_size, capacity + 1); #ifndef NDEBUG fprintf(stderr, @@ -104,14 +104,14 @@ struct buffer *initialize_shared_buffer(const char *key, mode_t mode, * we avoid a race when the other side opens the SHM file and reads from * it before it is fully initialized */ char tmpkey[SHM_NAME_MAXLEN] = ""; - if (shamon_get_tmp_key(key, tmpkey, SHM_NAME_MAXLEN) == -1) { + if (vms_shm_get_tmp_key(key, tmpkey, SHM_NAME_MAXLEN) == -1) { fprintf(stderr, "Failed creating a tmpkey for '%s'\n", key); return NULL; } - int fd = shamon_shm_open(tmpkey, O_RDWR | O_CREAT | O_TRUNC, mode); + int fd = vms_shm_open(tmpkey, O_RDWR | O_CREAT | O_TRUNC, mode); if (fd < 0) { - perror("shm_open"); + perror("vms_open"); return NULL; } @@ -126,8 +126,8 @@ struct buffer *initialize_shared_buffer(const char *key, mode_t mode, if (close(fd) == -1) { perror("closing fd after mmap failure"); } - if (shamon_shm_unlink(tmpkey) != 0) { - perror("shm_unlink after mmap failure"); + if (vms_shm_unlink(tmpkey) != 0) { + perror("vms_unlink after mmap failure"); } return NULL; } @@ -142,7 +142,7 @@ struct buffer *initialize_shared_buffer(const char *key, mode_t mode, buff->shmbuffer->info.allocated_size = memsize; buff->shmbuffer->info.capacity = capacity; /* ringbuf has one dummy element and we allocated the space for it */ - shm_spsc_ringbuf_init(_ringbuf(buff), capacity + 1); + vms_spsc_ringbuf_init(_ringbuf(buff), capacity + 1); buff->shmbuffer->info.elem_size = elem_size; buff->shmbuffer->info.last_processed_id = 0; buff->shmbuffer->info.dropped_ranges_next = 0; @@ -169,7 +169,7 @@ struct buffer *initialize_shared_buffer(const char *key, mode_t mode, buff->key = strdup(key); VEC_INIT(buff->aux_buffers); - shm_list_init(&buff->aux_buffers_age); + vms_list_init(&buff->aux_buffers_age); buff->aux_buf_idx = 0; buff->cur_aux_buff = NULL; buff->fd = fd; @@ -177,14 +177,14 @@ struct buffer *initialize_shared_buffer(const char *key, mode_t mode, buff->mode = mode; buff->last_subbufer_no = 0; - if (shamon_shm_rename(tmpkey, key) < 0) { + if (vms_shm_rename(tmpkey, key) < 0) { perror("renaming SHM file"); if (close(fd) == -1) { perror("closing fd after mmap failure"); } - if (shamon_shm_unlink(tmpkey) != 0) { - perror("shm_unlink after mmap failure"); + if (vms_shm_unlink(tmpkey) != 0) { + perror("vms_unlink after mmap failure"); } free(buff); return NULL; @@ -233,7 +233,7 @@ struct buffer *try_get_shared_buffer(const char *key, size_t retry) { int fd = -1; ++retry; do { - fd = shamon_shm_open(key, O_RDWR, S_IRWXU); + fd = vms_shm_open(key, O_RDWR, S_IRWXU); if (fd >= 0) { break; } @@ -241,7 +241,7 @@ struct buffer *try_get_shared_buffer(const char *key, size_t retry) { } while (--retry > 0); if (fd == -1) { - perror("shm_open"); + perror("vms_open"); fprintf(stderr, "Failed getting shared buffer '%s'\n", key); return NULL; } @@ -310,8 +310,8 @@ struct buffer *try_get_shared_buffer(const char *key, size_t retry) { if (close(fd) == -1) { perror("closing fd after mmap failure"); } - if (shamon_shm_unlink(key) != 0) { - perror("shm_unlink after mmap failure"); + if (vms_shm_unlink(key) != 0) { + perror("vms_unlink after mmap failure"); } return NULL; } @@ -336,7 +336,7 @@ void buffer_set_attached(struct buffer *buff, bool val) { } /* set the ID of the last processed event */ -void buffer_set_last_processed_id(struct buffer *buff, shm_eventid id) { +void buffer_set_last_processed_id(struct buffer *buff, vms_eventid id) { assert(buff->shmbuffer->info.last_processed_id <= id && "The IDs are not monotonic"); buff->shmbuffer->info.last_processed_id = id; @@ -386,8 +386,8 @@ void destroy_shared_buffer(struct buffer *buff) { if (close(buff->fd) == -1) { perror("destroy_shared_buffer: failed closing mmap fd"); } - if (shamon_shm_unlink(buff->key) != 0) { - perror("destroy_shared_buffer: shm_unlink failure"); + if (vms_shm_unlink(buff->key) != 0) { + perror("destroy_shared_buffer: vms_unlink failure"); } destroy_shared_control_buffer(buff->key, buff->control); @@ -398,7 +398,7 @@ void destroy_shared_buffer(struct buffer *buff) { void *buffer_read_pointer(struct buffer *buff, size_t *size) { struct buffer_info *info = &buff->shmbuffer->info; - size_t tail = shm_spsc_ringbuf_read_off_nowrap(&info->ringbuf, size); + size_t tail = vms_spsc_ringbuf_read_off_nowrap(&info->ringbuf, size); if (*size == 0) return NULL; /* TODO: get rid of the multiplication, @@ -407,11 +407,11 @@ void *buffer_read_pointer(struct buffer *buff, size_t *size) { } bool buffer_drop_k(struct buffer *buff, size_t k) { - return shm_spsc_ringbuf_consume_upto(_ringbuf(buff), k) == k; + return vms_spsc_ringbuf_consume_upto(_ringbuf(buff), k) == k; } size_t buffer_consume(struct buffer *buff, size_t k) { - return shm_spsc_ringbuf_consume_upto(_ringbuf(buff), k); + return vms_spsc_ringbuf_consume_upto(_ringbuf(buff), k); } /* buffer_push broken down into several operations: @@ -435,7 +435,7 @@ void *buffer_start_push(struct buffer *buff) { assert(!info->destroyed && "Writing to a destroyed buffer"); size_t n; - size_t off = shm_spsc_ringbuf_write_off_nowrap(_ringbuf(buff), &n); + size_t off = vms_spsc_ringbuf_write_off_nowrap(_ringbuf(buff), &n); if (n == 0) { /* ++info->dropped; */ return NULL; @@ -493,7 +493,7 @@ void *buffer_partial_push_str_n(struct buffer *buff, void *prev_push, void buffer_finish_push(struct buffer *buff) { assert(!buff->shmbuffer->info.destroyed && "Writing to a destroyed buffer"); - shm_spsc_ringbuf_write_finish(_ringbuf(buff), 1); + vms_spsc_ringbuf_write_finish(_ringbuf(buff), 1); } bool buffer_push(struct buffer *buff, const void *elem, size_t size) { @@ -519,7 +519,7 @@ bool buffer_pop(struct buffer *buff, void *dst) { void *pos = buffer_read_pointer(buff, &size); if (size > 0) { memcpy(dst, pos, buff->shmbuffer->info.elem_size); - shm_spsc_ringbuf_consume(_ringbuf(buff), 1); + vms_spsc_ringbuf_consume(_ringbuf(buff), 1); return true; } @@ -530,7 +530,7 @@ size_t _buffer_push_strn(struct buffer *buff, const void *data, size_t size) { struct aux_buffer *ab = writer_get_aux_buffer(buff, size); assert(ab); assert(ab == buff->cur_aux_buff); - assert(shm_list_last(&buff->aux_buffers_age)->data == buff->cur_aux_buff); + assert(vms_list_last(&buff->aux_buffers_age)->data == buff->cur_aux_buff); size_t off = ab->head; assert(off < (1LU << 32)); @@ -607,7 +607,7 @@ int buffer_register_events(struct buffer *b, size_t ev_nums, ...) { for (size_t i = 0; i < ev_nums; ++i) { const char *name = va_arg(ap, const char *); - shm_kind kind = va_arg(ap, shm_kind); + vms_kind kind = va_arg(ap, vms_kind); if (buffer_register_event(b, name, kind) < 0) { va_end(ap); return -1; @@ -624,7 +624,7 @@ int buffer_register_all_events(struct buffer *b) { const size_t ev_nums = source_control_get_records_num(b->control); for (size_t i = 0; i < ev_nums; ++i) { - recs[i].kind = 1 + i + shm_get_last_special_kind(); + recs[i].kind = 1 + i + vms_get_last_special_kind(); } return 0; diff --git a/src/shmbuf/shm.c b/src/shmbuf/shm.c index def2d041..f1308d93 100644 --- a/src/shmbuf/shm.c +++ b/src/shmbuf/shm.c @@ -11,12 +11,12 @@ #include #include -const char *shm_dir = "/dev/shm/"; -const size_t shm_dirlen = 9; +const char *SHM_DIR = "/dev/shm/"; +const size_t SHM_DIRlen = 9; -/* adapted function from musl project, src/mman/shm_open.c +/* adapted function from musl project, src/mman/vms_open.c * and from libc sysdeps/posix/shm-directory.h */ -char *shm_mapname(const char *name, char *buf) { +char *vms_shm_mapname(const char *name, char *buf) { assert(name[0] == '/'); /* Construct the filename. */ while (name[0] == '/') ++name; @@ -27,18 +27,18 @@ char *shm_mapname(const char *name, char *buf) { assert(0 && "invalid shm name"); return 0; } - if (SHM_NAME_MAXLEN <= shm_dirlen + namelen) { + if (SHM_NAME_MAXLEN <= SHM_DIRlen + namelen) { assert(0 && "buffer too short"); return 0; } - memcpy(buf, shm_dir, shm_dirlen); - memcpy(buf + shm_dirlen, name, namelen); - buf[shm_dirlen + namelen] = '\0'; + memcpy(buf, SHM_DIR, SHM_DIRlen); + memcpy(buf + SHM_DIRlen, name, namelen); + buf[SHM_DIRlen + namelen] = '\0'; return buf; } -char *shamon_map_ctrl_key(const char *buffkey, char key[SHM_NAME_MAXLEN]) { +char *vms_shm_map_ctrl_key(const char *buffkey, char key[SHM_NAME_MAXLEN]) { const size_t tmplen = strlen(buffkey); assert(tmplen < SHM_NAME_MAXLEN - 6); memcpy(key, buffkey, tmplen); @@ -49,14 +49,14 @@ char *shamon_map_ctrl_key(const char *buffkey, char key[SHM_NAME_MAXLEN]) { return key; } -int shamon_shm_open(const char *key, int flags, mode_t mode) { +int vms_shm_open(const char *key, int flags, mode_t mode) { char name[SHM_NAME_MAXLEN]; - if (shm_mapname(key, name) == 0) + if (vms_shm_mapname(key, name) == 0) abort(); return open(name, flags | O_NOFOLLOW | O_CLOEXEC | O_NONBLOCK, mode); } -int shamon_get_tmp_key(const char *key, char *buf, size_t bufsize) { +int vms_shm_get_tmp_key(const char *key, char *buf, size_t bufsize) { if (strlen(key) + 5 > bufsize) { return -1; } @@ -66,22 +66,22 @@ int shamon_get_tmp_key(const char *key, char *buf, size_t bufsize) { return 0; } -int shamon_shm_rename(const char *old_key, const char *new_key) { +int vms_shm_rename(const char *old_key, const char *new_key) { char old_name[SHM_NAME_MAXLEN]; char new_name[SHM_NAME_MAXLEN]; - if (shm_mapname(old_key, old_name) == 0) { + if (vms_shm_mapname(old_key, old_name) == 0) { return -1; } - if (shm_mapname(new_key, new_name) == 0) { + if (vms_shm_mapname(new_key, new_name) == 0) { return -1; } return rename(old_name, new_name); } -int shamon_shm_unlink(const char *key) { +int vms_shm_unlink(const char *key) { /* fprintf(stderr, "UNLINK: %s\n", key); */ char name[SHM_NAME_MAXLEN]; - if (shm_mapname(key, name) == 0) + if (vms_shm_mapname(key, name) == 0) abort(); return unlink(name); } diff --git a/src/shmbuf/shm.h b/src/shmbuf/shm.h index 408fc8d5..b62fbfcf 100644 --- a/src/shmbuf/shm.h +++ b/src/shmbuf/shm.h @@ -6,15 +6,15 @@ #define SHM_NAME_MAXLEN 256 -const char *shamon_shm_default_key(void); -const char *shamon_shm_default_ctrl_key(void); +const char *shamon_vms_default_key(void); +const char *shamon_vms_default_ctrl_key(void); -int shamon_shm_open(const char *key, int flags, mode_t mode); -int shamon_shm_rename(const char *old_key, const char *new_key); -int shamon_shm_unlink(const char *key); +int vms_shm_open(const char *key, int flags, mode_t mode); +int vms_shm_rename(const char *old_key, const char *new_key); +int vms_shm_unlink(const char *key); -char *shm_mapname(const char *name, char *buf); -int shamon_get_tmp_key(const char *key, char *buf, size_t bufsize); -char *shamon_map_ctrl_key(const char *key, char name[SHM_NAME_MAXLEN]); +char *vms_shm_mapname(const char *name, char *buf); +int vms_shm_get_tmp_key(const char *key, char *buf, size_t bufsize); +char *vms_shm_map_ctrl_key(const char *key, char name[SHM_NAME_MAXLEN]); #endif /* SHAMON_SHM_H */ diff --git a/src/shmbuf/utils.c b/src/shmbuf/utils.c index 4717cc10..440f0285 100644 --- a/src/shmbuf/utils.c +++ b/src/shmbuf/utils.c @@ -3,7 +3,7 @@ #include "buffer-private.h" -size_t compute_shm_size(size_t elem_size, size_t capacity) { +size_t compute_vms_size(size_t elem_size, size_t capacity) { /* compute how much memory we need */ size_t size = (elem_size * capacity) + sizeof(struct shmbuffer); /* round it up to page size. @@ -24,7 +24,7 @@ size_t compute_shm_size(size_t elem_size, size_t capacity) { return size; } -size_t compute_shm_buffer_size(size_t nondata_size, size_t elem_size, +size_t compute_vms_buffer_size(size_t nondata_size, size_t elem_size, size_t capacity) { /* compute how much memory we need */ size_t size = (elem_size * capacity) + nondata_size; diff --git a/src/streams/legacy/stream-fastbuf-io.c b/src/streams/legacy/stream-fastbuf-io.c index ee3db0d6..070254e1 100644 --- a/src/streams/legacy/stream-fastbuf-io.c +++ b/src/streams/legacy/stream-fastbuf-io.c @@ -7,7 +7,7 @@ #include #include -#include "fastbuf/shm_monitor.h" +#include "fastbuf/vms_monitor.h" // FIXME: do these local to a stream static int monitoring_active = 0; @@ -82,11 +82,11 @@ int register_monitored_thread(monitor_buffer buffer) { thrd_create(&thrd, &monitoring_thread, buffer); } -static bool io_has_event(shm_stream *stream) { - // shm_stream_io *fs = (shm_stream_io *) stream; +static bool io_has_event(vms_stream *stream) { + // vms_stream_io *fs = (vms_stream_io *) stream; //// dispatch pending events if available - // if (shm_queue_size(fs->pending_events) > 0) + // if (vms_queue_size(fs->pending_events) > 0) // return true; //// check for new events @@ -98,43 +98,43 @@ static bool io_has_event(shm_stream *stream) { // size_t num = read_events(fs); // // num can be 0 if all io get closed (which // // is what poll detects) - // assert(num == 0 || shm_queue_size(fs->pending_events)); + // assert(num == 0 || vms_queue_size(fs->pending_events)); // } - // return shm_queue_size(fs->pending_events); + // return vms_queue_size(fs->pending_events); return true; } -static shm_event_io *io_get_next_event(shm_stream *stream) { - // shm_stream_io *ss = (shm_stream_io *) stream; - // static shm_event_fd_in ev; - // if (shm_queue_pop(ss->pending_events, &ev)) +static vms_event_io *io_get_next_event(vms_stream *stream) { + // vms_stream_io *ss = (vms_stream_io *) stream; + // static vms_event_fd_in ev; + // if (vms_queue_pop(ss->pending_events, &ev)) // return &ev; return NULL; } -shm_stream *shm_create_io_stream(pid_t pid) { +vms_stream *vms_create_io_stream(pid_t pid) { char *name = malloc(21); // FIXME: we leak this assert(name); snprintf(name, 21, "io-stream(%d)", pid); - shm_stream_io *ss = malloc(sizeof *ss); + vms_stream_io *ss = malloc(sizeof *ss); assert(ss); - shm_stream_init((shm_stream *)ss, sizeof(shm_event_io), - (shm_stream_has_event_fn)io_has_event, - (shm_stream_get_next_event_fn)io_get_next_event, name); + vms_stream_init((vms_stream *)ss, sizeof(vms_event_io), + (vms_stream_has_event_fn)io_has_event, + (vms_stream_get_next_event_fn)io_get_next_event, name); ss->ev_kind_in = - shm_mk_event_kind("io-in", sizeof(shm_event_io), NULL, NULL); + vms_mk_event_kind("io-in", sizeof(vms_event_io), NULL, NULL); ss->ev_kind_out = - shm_mk_event_kind("io-out", sizeof(shm_event_io), NULL, NULL); + vms_mk_event_kind("io-out", sizeof(vms_event_io), NULL, NULL); monitoring_active = 1; monitored_process proc = attach_to_process(pid, ®ister_monitored_thread); // wait_for_process(proc); - return (shm_stream *)ss; + return (vms_stream *)ss; } -void shm_destroy_io_stream(shm_stream_io *s) { monitoring_active = 0; } +void vms_destroy_io_stream(vms_stream_io *s) { monitoring_active = 0; } diff --git a/src/streams/legacy/stream-fastbuf-io.h b/src/streams/legacy/stream-fastbuf-io.h index 4d45e36c..398f4707 100644 --- a/src/streams/legacy/stream-fastbuf-io.h +++ b/src/streams/legacy/stream-fastbuf-io.h @@ -5,25 +5,25 @@ #include "event.h" #include "stream.h" -typedef struct _shm_event_io { +typedef struct _vms_event_io { // mandatory - shm_event base; + vms_event base; // data int fd; uint64_t time; - shm_string_ref str_ref; -} shm_event_io; + vms_string_ref str_ref; +} vms_event_io; -typedef struct _shm_stream_io { - shm_stream base; +typedef struct _vms_stream_io { + vms_stream base; pid_t pid; // the kind that we assign to events. If a stream can have only one // kind of events, we can move this to base (derive it from the stream name) - shm_kind ev_kind_in; - shm_kind ev_kind_out; -} shm_stream_io; + vms_kind ev_kind_in; + vms_kind ev_kind_out; +} vms_stream_io; -shm_stream *shm_create_io_stream(pid_t pid); -void shm_destroy_io_stream(shm_stream_io *s); +vms_stream *vms_create_io_stream(pid_t pid); +void vms_destroy_io_stream(vms_stream_io *s); -// void shm_destroy_io_stream(shm_stream_stdin *ss); +// void vms_destroy_io_stream(vms_stream_stdin *ss); diff --git a/src/streams/legacy/stream-fds.c b/src/streams/legacy/stream-fds.c index c39a7e41..c5f02b55 100644 --- a/src/streams/legacy/stream-fds.c +++ b/src/streams/legacy/stream-fds.c @@ -8,15 +8,15 @@ #include "arbiter.h" -static size_t read_events(shm_stream_fds *ss, shm_arbiter_buffer *buffer) { +static size_t read_events(vms_stream_fds *ss, vms_arbiter_buffer *buffer) { size_t read_ev = 0; size_t remove_num = 0; - shm_event_fd_in ev; + vms_event_fd_in ev; for (unsigned i = 0; i < ss->fds_num; ++i) { struct pollfd *pfd = ss->fds + i; if (pfd->revents & POLLIN) { - shm_string *str = ss->fds_buffer + i; + vms_string *str = ss->fds_buffer + i; // FIXME: set O_NONBLOCK and read all the data right now // to have the right timestamps? ssize_t len = read(pfd->fd, str->data, str->alloc_size); @@ -32,13 +32,13 @@ static size_t read_events(shm_stream_fds *ss, shm_arbiter_buffer *buffer) { } else { assert(len > 0); ev.time = clock(); - // ev.base.stream = (shm_stream *) ss; + // ev.base.stream = (vms_stream *) ss; ev.base.kind = ss->ev_kind_in; - ev.base.id = shm_stream_get_next_id((shm_stream *)ss); + ev.base.id = vms_stream_get_next_id((vms_stream *)ss); ev.fd = pfd->fd; ev.str_ref.size = len; ev.str_ref.data = str->data; - shm_arbiter_buffer_push(buffer, &ev, sizeof(ev)); + vms_arbiter_buffer_push(buffer, &ev, sizeof(ev)); ++read_ev; } } @@ -54,7 +54,7 @@ static size_t read_events(shm_stream_fds *ss, shm_arbiter_buffer *buffer) { struct pollfd *new_fds = malloc(ss->fds_num * sizeof(struct pollfd)); assert(new_fds); // we could just trim the memory, but... - shm_string *new_fds_buffer = malloc(ss->fds_num * sizeof(shm_string)); + vms_string *new_fds_buffer = malloc(ss->fds_num * sizeof(vms_string)); assert(new_fds_buffer); unsigned idx = 0; @@ -74,9 +74,9 @@ static size_t read_events(shm_stream_fds *ss, shm_arbiter_buffer *buffer) { return read_ev; } -static size_t fds_buffer_events(shm_stream *stream, - shm_arbiter_buffer *buffer) { - shm_stream_fds *fs = (shm_stream_fds *)stream; +static size_t fds_buffer_events(vms_stream *stream, + vms_arbiter_buffer *buffer) { + vms_stream_fds *fs = (vms_stream_fds *)stream; int ret = poll(fs->fds, fs->fds_num, 0); if (ret == -1) { @@ -88,33 +88,33 @@ static size_t fds_buffer_events(shm_stream *stream, return 0; } -static bool fds_is_ready(shm_stream *stream) { - return ((shm_stream_fds *)stream)->fds_num > 0; +static bool fds_is_ready(vms_stream *stream) { + return ((vms_stream_fds *)stream)->fds_num > 0; } -shm_stream *shm_create_fds_stream() { - shm_stream_fds *ss = malloc(sizeof *ss); - shm_stream_init((shm_stream *)ss, sizeof(shm_event_fd_in), +vms_stream *vms_create_fds_stream() { + vms_stream_fds *ss = malloc(sizeof *ss); + vms_stream_init((vms_stream *)ss, sizeof(vms_event_fd_in), fds_buffer_events, NULL, fds_is_ready, "fds-stream"); - ss->ev_kind_in = shm_mk_event_kind("fd-in", (shm_stream *)ss, - sizeof(shm_event_fd_in), NULL, NULL); + ss->ev_kind_in = vms_mk_event_kind("fd-in", (vms_stream *)ss, + sizeof(vms_event_fd_in), NULL, NULL); ss->fds = NULL; ss->fds_size = 0; ss->fds_num = 0; ss->fds_buffer = NULL; - shm_queue_init(&ss->pending_events, 32, sizeof(shm_event_fd_in)); + vms_queue_init(&ss->pending_events, 32, sizeof(vms_event_fd_in)); - return (shm_stream *)ss; + return (vms_stream *)ss; } -void shm_stream_fds_add_fd(shm_stream_fds *stream, int fd) { +void vms_stream_fds_add_fd(vms_stream_fds *stream, int fd) { size_t idx = stream->fds_num++; if (idx >= stream->fds_size) { stream->fds_size += 8; stream->fds = realloc(stream->fds, sizeof(struct pollfd) * stream->fds_size); stream->fds_buffer = - realloc(stream->fds_buffer, sizeof(shm_string) * stream->fds_size); + realloc(stream->fds_buffer, sizeof(vms_string) * stream->fds_size); } assert(stream->fds_num < stream->fds_size); @@ -127,7 +127,7 @@ void shm_stream_fds_add_fd(shm_stream_fds *stream, int fd) { stream->fds_buffer[idx].size = 0; } -void shm_destroy_fds_stream(shm_stream_fds *ss) { +void vms_destroy_fds_stream(vms_stream_fds *ss) { for (unsigned i = 0; i < ss->fds_num; ++i) { free(ss->fds_buffer[i].data); } diff --git a/src/streams/legacy/stream-fds.h b/src/streams/legacy/stream-fds.h index 6547cefe..ed999552 100644 --- a/src/streams/legacy/stream-fds.h +++ b/src/streams/legacy/stream-fds.h @@ -6,17 +6,17 @@ #include "queue.h" #include "stream.h" -typedef struct _shm_event_fd_in { +typedef struct _vms_event_fd_in { // mandatory - shm_event base; + vms_event base; // data int fd; uint64_t time; - shm_string_ref str_ref; -} shm_event_fd_in; + vms_string_ref str_ref; +} vms_event_fd_in; -typedef struct _shm_stream_fds { - shm_stream base; +typedef struct _vms_stream_fds { + vms_stream base; // user can add here some auxiliary data if needed // ... // the pointer to the read lines @@ -24,7 +24,7 @@ typedef struct _shm_stream_fds { // the kind that we assign to events. If a stream can have only one // kind of events, we can move this to base (derive it // from the stream name) - shm_kind ev_kind_in; + vms_kind ev_kind_in; // filedescriptors that we track struct pollfd *fds; // allocated size @@ -32,12 +32,12 @@ typedef struct _shm_stream_fds { // number of filedescriptors size_t fds_num; // buffer for each fd for reading - shm_string *fds_buffer; + vms_string *fds_buffer; // events to be pushed to the monitor - shm_queue pending_events; -} shm_stream_fds; + vms_queue pending_events; +} vms_stream_fds; -shm_stream *shm_create_fds_stream(); -void shm_stream_fds_add_fd(shm_stream_fds *stream, int fd); +vms_stream *vms_create_fds_stream(); +void vms_stream_fds_add_fd(vms_stream_fds *stream, int fd); -void shm_destroy_fds_stream(shm_stream_fds *ss); +void vms_destroy_fds_stream(vms_stream_fds *ss); diff --git a/src/streams/legacy/stream-stdin.c b/src/streams/legacy/stream-stdin.c index 31a0959d..20ad927c 100644 --- a/src/streams/legacy/stream-stdin.c +++ b/src/streams/legacy/stream-stdin.c @@ -7,11 +7,11 @@ #include "arbiter.h" -bool stdin_is_ready(shm_stream *stream) { return true; } +bool stdin_is_ready(vms_stream *stream) { return true; } -void stdin_copy_event(shm_stream *stream, shm_event *in, shm_event *out) { - static shm_event_stdin ev; - shm_stream_stdin *ss = (shm_stream_stdin *)stream; +void stdin_copy_event(vms_stream *stream, vms_event *in, vms_event *out) { + static vms_event_stdin ev; + vms_stream_stdin *ss = (vms_stream_stdin *)stream; ssize_t len = getline(&ss->line, &ss->line_len, stdin); // TODO: return end-of-stream event @@ -26,22 +26,22 @@ void stdin_copy_event(shm_stream *stream, shm_event *in, shm_event *out) { ev.str_ref.size = len; ev.str_ref.data = ss->line; - shm_arbiter_buffer_push(buffer, &ev, sizeof(ev)); + vms_arbiter_buffer_push(buffer, &ev, sizeof(ev)); return 1; } -shm_stream *shm_create_stdin_stream() { - shm_stream_stdin *ss = malloc(sizeof *ss); - shm_stream_init((shm_stream *)ss, sizeof(shm_event_stdin), stdin_is_ready, +vms_stream *vms_create_stdin_stream() { + vms_stream_stdin *ss = malloc(sizeof *ss); + vms_stream_init((vms_stream *)ss, sizeof(vms_event_stdin), stdin_is_ready, /* filter */ NULL, copy_event, "stdin-stream"); ss->line = NULL; ss->line_len = 0; - ss->ev_kind = shm_mk_event_kind("stdin", (shm_stream *)ss, - sizeof(shm_event_stdin), NULL, NULL); - return (shm_stream *)ss; + ss->ev_kind = vms_mk_event_kind("stdin", (vms_stream *)ss, + sizeof(vms_event_stdin), NULL, NULL); + return (vms_stream *)ss; } -void shm_destroy_stdin_stream(shm_stream_stdin *ss) { +void vms_destroy_stdin_stream(vms_stream_stdin *ss) { free(ss->line); free(ss); } diff --git a/src/streams/legacy/stream-stdin.h b/src/streams/legacy/stream-stdin.h index af2feeb8..28f03e56 100644 --- a/src/streams/legacy/stream-stdin.h +++ b/src/streams/legacy/stream-stdin.h @@ -5,17 +5,17 @@ #include "event.h" #include "stream.h" -typedef struct _shm_event_stdin { +typedef struct _vms_event_stdin { // mandatory - shm_event base; + vms_event base; // data int fd; uint64_t time; - shm_string_ref str_ref; -} shm_event_stdin; + vms_string_ref str_ref; +} vms_event_stdin; -typedef struct _shm_stream_stdin { - shm_stream base; +typedef struct _vms_stream_stdin { + vms_stream base; // user can add here some auxiliary data if needed // ... // the pointer to the read lines @@ -24,14 +24,14 @@ typedef struct _shm_stream_stdin { // the kind that we assign to events. If a stream can have only one // kind of events, we can move this to base (derive it // from the stream name) - shm_kind ev_kind; - shm_eventid last_event_id; -} shm_stream_stdin; + vms_kind ev_kind; + vms_eventid last_event_id; +} vms_stream_stdin; -bool stdin_has_event(shm_stream *stream); +bool stdin_has_event(vms_stream *stream); -size_t stdin_buffer_events(shm_stream *stream, shm_arbiter_buffer *buffer); +size_t stdin_buffer_events(vms_stream *stream, vms_arbiter_buffer *buffer); -shm_stream *shm_create_stdin_stream(); +vms_stream *vms_create_stdin_stream(); -void shm_destroy_stdin_stream(shm_stream_stdin *ss); +void vms_destroy_stdin_stream(vms_stream_stdin *ss); diff --git a/src/streams/stream-drregex.c b/src/streams/stream-drregex.c index fa9f3252..5c1d95e9 100644 --- a/src/streams/stream-drregex.c +++ b/src/streams/stream-drregex.c @@ -8,26 +8,26 @@ #include "vamos-buffers/shmbuf/buffer.h" -bool drregex_is_ready(shm_stream *stream) { - struct buffer *b = ((shm_stream_drregex *)stream)->shmbuffer; +bool drregex_is_ready(vms_stream *stream) { + struct buffer *b = ((vms_stream_drregex *)stream)->shmbuffer; /* buffer must be ready or it may not be ready anymore, but it * still has some data that we haven't read */ return buffer_is_ready(b) || buffer_size(b) > 0; } -void drregex_alter(shm_stream *stream, shm_event *in, shm_event *out) { +void drregex_alter(vms_stream *stream, vms_event *in, vms_event *out) { memcpy(out, in, stream->event_size); } -shm_stream *shm_create_drregex_stream(const char *key, const char *name) { - shm_stream_drregex *ss = malloc(sizeof *ss); +vms_stream *vms_create_drregex_stream(const char *key, const char *name) { + vms_stream_drregex *ss = malloc(sizeof *ss); struct buffer *shmbuffer = get_shared_buffer(key); assert(shmbuffer && "Getting the shm buffer failed"); size_t elem_size = buffer_elem_size(shmbuffer); assert(elem_size > 0); - shm_stream_init((shm_stream *)ss, shmbuffer, elem_size, drregex_is_ready, + vms_stream_init((vms_stream *)ss, shmbuffer, elem_size, drregex_is_ready, NULL, drregex_alter, NULL, NULL, "drregex-stream", name); ss->shmbuffer = shmbuffer; - return (shm_stream *)ss; + return (vms_stream *)ss; } diff --git a/src/streams/stream-funs.c b/src/streams/stream-funs.c index 333abdca..b6253bf5 100644 --- a/src/streams/stream-funs.c +++ b/src/streams/stream-funs.c @@ -8,41 +8,41 @@ #include "vamos-buffers/shmbuf/buffer.h" -bool funs_is_ready(shm_stream *stream) { - struct buffer *b = ((shm_stream_funs *)stream)->shmbuffer; +bool funs_is_ready(vms_stream *stream) { + struct buffer *b = ((vms_stream_funs *)stream)->shmbuffer; /* buffer must be ready or it may not be ready anymore, but it * still has some data that we haven't read */ return buffer_is_ready(b) || buffer_size(b) > 0; } -void funs_alter(shm_stream *stream, shm_event *in, shm_event *out) { +void funs_alter(vms_stream *stream, vms_event *in, vms_event *out) { memcpy(out, in, stream->event_size); } size_t stream_mk_event_kinds(const char *stream_name, struct buffer *shmbuffer, size_t *max_ev_size); -shm_stream *shm_create_funs_stream(const char *key, const char *name) { - shm_stream_funs *ss = malloc(sizeof *ss); +vms_stream *vms_create_funs_stream(const char *key, const char *name) { + vms_stream_funs *ss = malloc(sizeof *ss); struct buffer *shmbuffer = get_shared_buffer(key); assert(shmbuffer && "Getting the shm buffer failed"); size_t elem_size = buffer_elem_size(shmbuffer); assert(elem_size > 0); - shm_stream_init((shm_stream *)ss, shmbuffer, elem_size, funs_is_ready, NULL, + vms_stream_init((vms_stream *)ss, shmbuffer, elem_size, funs_is_ready, NULL, funs_alter, NULL, NULL, "funs-stream", name); ss->shmbuffer = shmbuffer; ss->ev_buff = NULL; - return (shm_stream *)ss; + return (vms_stream *)ss; } -const char *shm_stream_funs_get_str(shm_stream_funs *fstream, uint64_t elem) { +const char *vms_stream_funs_get_str(vms_stream_funs *fstream, uint64_t elem) { return buffer_get_str(fstream->shmbuffer, elem); } -struct event_record *shm_funs_stream_get_event_spec(shm_stream_funs *stream, - shm_kind kind) { +struct event_record *vms_funs_stream_get_event_spec(vms_stream_funs *stream, + vms_kind kind) { for (size_t i = 0; i < stream->spec_count; ++i) if (stream->events[i].kind == kind) return stream->events + i; diff --git a/src/streams/stream-generic.c b/src/streams/stream-generic.c index 64eb179b..dc70fa34 100644 --- a/src/streams/stream-generic.c +++ b/src/streams/stream-generic.c @@ -12,28 +12,28 @@ /* A generic stream for events stored in shared memory buffer, no filter nor modification of events supported (unless done manually). */ -static bool generic_is_ready(shm_stream *stream) { +static bool generic_is_ready(vms_stream *stream) { struct buffer *b = stream->incoming_events_buffer; /* buffer must be ready or it may not be ready anymore, but it * still has some data that we haven't read */ return buffer_is_ready(b) || buffer_size(b) > 0; } -static void generic_alter(shm_stream *stream, shm_event *in, shm_event *out) { +static void generic_alter(vms_stream *stream, vms_event *in, vms_event *out) { memcpy(out, in, stream->event_size); } -shm_stream *shm_create_generic_stream(const char *key, const char *name, - shm_stream_hole_handling *hole_handling) { - shm_stream_generic *ss = malloc(sizeof *ss); +vms_stream *vms_create_generic_stream(const char *key, const char *name, + vms_stream_hole_handling *hole_handling) { + vms_stream_generic *ss = malloc(sizeof *ss); struct buffer *shmbuffer = get_shared_buffer(key); assert(shmbuffer && "Getting the shm buffer failed"); size_t elem_size = buffer_elem_size(shmbuffer); assert(elem_size > 0); - shm_stream_init((shm_stream *)ss, shmbuffer, elem_size, generic_is_ready, + vms_stream_init((vms_stream *)ss, shmbuffer, elem_size, generic_is_ready, NULL, generic_alter, NULL, hole_handling, "generic-stream", name); ss->shmbuffer = shmbuffer; - return (shm_stream *)ss; + return (vms_stream *)ss; } diff --git a/src/streams/stream-regex.c b/src/streams/stream-regex.c index c5463dad..fa868e2b 100644 --- a/src/streams/stream-regex.c +++ b/src/streams/stream-regex.c @@ -8,29 +8,29 @@ #include "vamos-buffers/core/arbiter.h" #include "vamos-buffers/shmbuf/buffer.h" -bool sregex_is_ready(shm_stream *stream) { - struct buffer *b = ((shm_stream_sregex *)stream)->shmbuffer; +bool sregex_is_ready(vms_stream *stream) { + struct buffer *b = ((vms_stream_sregex *)stream)->shmbuffer; /* buffer must be ready or it may not be ready anymore, but it * still has some data that we haven't read */ return buffer_is_ready(b) || buffer_size(b) > 0; } -void sregex_alter(shm_stream *stream, shm_event *in, shm_event *out) { +void sregex_alter(vms_stream *stream, vms_event *in, vms_event *out) { memcpy(out, in, stream->event_size); } -shm_stream *shm_create_sregex_stream( +vms_stream *vms_create_sregex_stream( const char *key, const char *name, - const shm_stream_hole_handling *hole_handling) { - shm_stream_sregex *ss = malloc(sizeof *ss); + const vms_stream_hole_handling *hole_handling) { + vms_stream_sregex *ss = malloc(sizeof *ss); struct buffer *shmbuffer = get_shared_buffer(key); assert(shmbuffer && "Getting the shm buffer failed"); size_t elem_size = buffer_elem_size(shmbuffer); assert(elem_size > 0); - shm_stream_init((shm_stream *)ss, shmbuffer, elem_size, sregex_is_ready, + vms_stream_init((vms_stream *)ss, shmbuffer, elem_size, sregex_is_ready, /* filter = */ NULL, sregex_alter, NULL, hole_handling, "regex-stream", name); ss->shmbuffer = shmbuffer; - return (shm_stream *)ss; + return (vms_stream *)ss; } diff --git a/src/streams/stream-regexrw.c b/src/streams/stream-regexrw.c index 8fc94221..eb88a6d7 100644 --- a/src/streams/stream-regexrw.c +++ b/src/streams/stream-regexrw.c @@ -7,26 +7,26 @@ #include "vamos-buffers/streams/stream-regexrw.h" #include "vamos-buffers/core/arbiter.h" -bool sregexrw_is_ready(shm_stream *stream) { - struct buffer *b = ((shm_stream_sregexrw *)stream)->shmbuffer; +bool sregexrw_is_ready(vms_stream *stream) { + struct buffer *b = ((vms_stream_sregexrw *)stream)->shmbuffer; /* buffer must be ready or it may not be ready anymore, but it * still has some data that we haven't read */ return buffer_is_ready(b) || buffer_size(b) > 0; } -void sregexrw_alter(shm_stream *stream, shm_event *in, shm_event *out) { +void sregexrw_alter(vms_stream *stream, vms_event *in, vms_event *out) { memcpy(out, in, stream->event_size); } -shm_stream *shm_create_sregexrw_stream(const char *key, const char *name) { - shm_stream_sregexrw *ss = malloc(sizeof *ss); +vms_stream *vms_create_sregexrw_stream(const char *key, const char *name) { + vms_stream_sregexrw *ss = malloc(sizeof *ss); struct buffer *shmbuffer = get_shared_buffer(key); assert(shmbuffer && "Getting the shm buffer failed"); size_t elem_size = buffer_elem_size(shmbuffer); assert(elem_size > 0); - shm_stream_init((shm_stream *)ss, shmbuffer, elem_size, sregexrw_is_ready, + vms_stream_init((vms_stream *)ss, shmbuffer, elem_size, sregexrw_is_ready, NULL, sregexrw_alter, NULL, NULL, "regexrw-stream", name); ss->shmbuffer = shmbuffer; - return (shm_stream *)ss; + return (vms_stream *)ss; } diff --git a/src/streams/streams.c b/src/streams/streams.c index f385a73e..ed6c7836 100644 --- a/src/streams/streams.c +++ b/src/streams/streams.c @@ -59,8 +59,8 @@ static struct stream_rec avail_streams[] = { {NULL, NULL} /* to mark the end */ }; -shm_stream *shm_stream_create(const char *stream_name, const char *spec, - shm_stream_hole_handling *hole_handling) { +vms_stream *vms_stream_create(const char *stream_name, const char *spec, + vms_stream_hole_handling *hole_handling) { const char *next = find_next_part(spec); /* skip the name */ if (!next) { fprintf(stderr, @@ -92,7 +92,7 @@ shm_stream *shm_stream_create(const char *stream_name, const char *spec, return NULL; } - return shm_create_funs_stream(key, stream_name); + return vms_create_funs_stream(key, stream_name); } else if (strncmp(source, "regex", 6) == 0) { if (!next || *next == 0) { fprintf( @@ -108,7 +108,7 @@ shm_stream *shm_stream_create(const char *stream_name, const char *spec, "parameter (FOR NOW)\n"); } - return shm_create_sregex_stream(key, stream_name, hole_handling); + return vms_create_sregex_stream(key, stream_name, hole_handling); } else if (strncmp(source, "regexrw", 8) == 0) { if (!next || *next == 0) { fprintf( @@ -124,7 +124,7 @@ shm_stream *shm_stream_create(const char *stream_name, const char *spec, "parameter (FOR NOW)\n"); } - return shm_create_sregexrw_stream(key, stream_name); + return vms_create_sregexrw_stream(key, stream_name); } else if (strncmp(source, "drregex", 8) == 0) { if (!next || *next == 0) { fprintf( @@ -140,7 +140,7 @@ shm_stream *shm_stream_create(const char *stream_name, const char *spec, "parameter (FOR NOW)\n"); } - return shm_create_drregex_stream(key, stream_name); + return vms_create_drregex_stream(key, stream_name); } else if (strncmp(source, "generic", 8) == 0) { if (!next || *next == 0) { fprintf( @@ -156,7 +156,7 @@ shm_stream *shm_stream_create(const char *stream_name, const char *spec, "parameter (FOR NOW)\n"); } - return shm_create_generic_stream(key, stream_name, hole_handling); + return vms_create_generic_stream(key, stream_name, hole_handling); } fprintf(stderr, "Unknown stream. Available streams:\n"); @@ -169,9 +169,9 @@ shm_stream *shm_stream_create(const char *stream_name, const char *spec, return NULL; } -shm_stream *shm_stream_create_from_argv( +vms_stream *vms_stream_create_from_argv( const char *stream_name, int argc, char *argv[], - shm_stream_hole_handling *hole_handling) { + vms_stream_hole_handling *hole_handling) { const char *spec = get_spec(stream_name, argc, argv); if (!spec) { fprintf(stderr, "error: did not find spec for stream '%s'\n", @@ -179,5 +179,5 @@ shm_stream *shm_stream_create_from_argv( return NULL; } - return shm_stream_create(stream_name, spec, hole_handling); + return vms_stream_create(stream_name, spec, hole_handling); } diff --git a/tests/arbiter-buffer.c b/tests/arbiter-buffer.c index 90d19eb6..31ef37ef 100644 --- a/tests/arbiter-buffer.c +++ b/tests/arbiter-buffer.c @@ -11,13 +11,13 @@ #include "vamos-buffers/shmbuf/buffer.h" #include "vamos-buffers/core/stream.h" -static bool is_ready(shm_stream *s) { +static bool is_ready(vms_stream *s) { (void)s; return false; } struct event { - shm_event base; + vms_event base; int i; }; @@ -30,53 +30,53 @@ int main(void) { initialize_local_buffer("/dummy", sizeof(struct event), 30, NULL); assert(lbuffer); - shm_stream dummy_stream; - shm_stream *stream = &dummy_stream; + vms_stream dummy_stream; + vms_stream *stream = &dummy_stream; - shm_stream_init(stream, lbuffer, sizeof(struct event), is_ready, NULL, NULL, + vms_stream_init(stream, lbuffer, sizeof(struct event), is_ready, NULL, NULL, NULL, NULL, "dummy-stream", "dummy"); - shm_arbiter_buffer *b = - shm_arbiter_buffer_create(stream, sizeof(struct event), 20); - shm_arbiter_buffer_set_active(b, true); + vms_arbiter_buffer *b = + vms_arbiter_buffer_create(stream, sizeof(struct event), 20); + vms_arbiter_buffer_set_active(b, true); struct event ev; for (int i = 1; i < 21; ++i) { ev.base.id = i; ev.i = i; - shm_arbiter_buffer_push(b, &ev, sizeof(struct event)); + vms_arbiter_buffer_push(b, &ev, sizeof(struct event)); } - assert(shm_arbiter_buffer_size(b) == 20); - assert(shm_arbiter_buffer_drop_older_than(b, 10) == 10); - assert(shm_arbiter_buffer_size(b) == 10); - assert(shm_arbiter_buffer_drop(b, 20) == 10); - assert(shm_arbiter_buffer_size(b) == 0); + assert(vms_arbiter_buffer_size(b) == 20); + assert(vms_arbiter_buffer_drop_older_than(b, 10) == 10); + assert(vms_arbiter_buffer_size(b) == 10); + assert(vms_arbiter_buffer_drop(b, 20) == 10); + assert(vms_arbiter_buffer_size(b) == 0); for (int i = 1; i < 21; ++i) { ev.base.id = 20 + 2 * i; ev.i = i; - shm_arbiter_buffer_push(b, &ev, sizeof(struct event)); + vms_arbiter_buffer_push(b, &ev, sizeof(struct event)); } - assert(shm_arbiter_buffer_size(b) == 20); - assert(shm_arbiter_buffer_drop_older_than(b, 39) == 9); - assert(shm_arbiter_buffer_size(b) == 11); - assert(shm_arbiter_buffer_drop_older_than(b, 58) == 10); - assert(shm_arbiter_buffer_size(b) == 1); - assert(shm_arbiter_buffer_drop_older_than(b, 70) == 1); - assert(shm_arbiter_buffer_size(b) == 0); + assert(vms_arbiter_buffer_size(b) == 20); + assert(vms_arbiter_buffer_drop_older_than(b, 39) == 9); + assert(vms_arbiter_buffer_size(b) == 11); + assert(vms_arbiter_buffer_drop_older_than(b, 58) == 10); + assert(vms_arbiter_buffer_size(b) == 1); + assert(vms_arbiter_buffer_drop_older_than(b, 70) == 1); + assert(vms_arbiter_buffer_size(b) == 0); for (int i = 1; i < 21; ++i) { ev.base.id = 40 + 2 * i; ev.i = i; - shm_arbiter_buffer_push(b, &ev, sizeof(struct event)); + vms_arbiter_buffer_push(b, &ev, sizeof(struct event)); } - assert(shm_arbiter_buffer_drop_older_than(b, 3) == 0); - assert(shm_arbiter_buffer_size(b) == 20); - assert(shm_arbiter_buffer_drop_older_than(b, 90) == 20); - assert(shm_arbiter_buffer_size(b) == 0); + assert(vms_arbiter_buffer_drop_older_than(b, 3) == 0); + assert(vms_arbiter_buffer_size(b) == 20); + assert(vms_arbiter_buffer_drop_older_than(b, 90) == 20); + assert(vms_arbiter_buffer_size(b) == 0); return 0; } diff --git a/tests/bench/ringbufs.c b/tests/bench/ringbufs.c index fe3d431a..c49a4cff 100644 --- a/tests/bench/ringbufs.c +++ b/tests/bench/ringbufs.c @@ -104,26 +104,26 @@ static void run_local_shmbuf_push_pop_st() { #endif static void run_par_queue_push_pop_st(void) { - shm_par_queue q; - shm_par_queue_init(&q, N, sizeof(int)); + vms_par_queue q; + vms_par_queue_init(&q, N, sizeof(int)); struct timespec start, mid, end; double elapsed = 0; clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start); for (int i = 0; i < N; ++i) { - shm_par_queue_push(&q, &i, sizeof(int)); + vms_par_queue_push(&q, &i, sizeof(int)); } clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &mid); elapsed += report_time("[par-queue] single-threaded, push", &start, &mid); - assert(shm_par_queue_size(&q) == N); + assert(vms_par_queue_size(&q) == N); int j; clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &mid); for (int i = 0; i < N; ++i) { - shm_par_queue_pop(&q, &j); + vms_par_queue_pop(&q, &j); } clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end); @@ -132,13 +132,13 @@ static void run_par_queue_push_pop_st(void) { "\033[34m[par-queue], single-threaded, push/pop: %lf seconds.\033[0m\n", elapsed); - assert(shm_par_queue_size(&q) == 0); - shm_par_queue_destroy(&q); + assert(vms_par_queue_size(&q) == 0); + vms_par_queue_destroy(&q); } static void run_queue_spsc_push_pop_st(void) { - shm_queue_spsc q; - shm_queue_spsc_init(&q, N); + vms_queue_spsc q; + vms_queue_spsc_init(&q, N); int *buff = malloc(sizeof(int) * N); assert(buff); @@ -150,10 +150,10 @@ static void run_queue_spsc_push_pop_st(void) { clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start); for (int i = 0; i < N; ++i) { - shm_queue_spsc_write_offset(&q, &off); + vms_queue_spsc_write_offset(&q, &off); assert(off == (unsigned)i); buff[off] = i; - shm_queue_spsc_write_finish(&q); + vms_queue_spsc_write_finish(&q); } clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &mid); @@ -161,10 +161,10 @@ static void run_queue_spsc_push_pop_st(void) { clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &mid); for (int i = 0; i < N; ++i) { - shm_queue_spsc_read_offset(&q, &off); + vms_queue_spsc_read_offset(&q, &off); assert(off == (unsigned)i); memcpy(&j, buff + i, sizeof(int)); - shm_queue_spsc_consume(&q, 1); + vms_queue_spsc_consume(&q, 1); } clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end); @@ -186,18 +186,18 @@ struct thr_data { static int par_queue_push_pop_1_writer(void *arg) { struct thr_data *data = (struct thr_data *)arg; - shm_par_queue *q = (shm_par_queue *)data->ringbuffer; + vms_par_queue *q = (vms_par_queue *)data->ringbuffer; int i; struct timespec start, mid; int *addr; clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start); for (i = 0; i < N; ++i) { - while (!(addr = shm_par_queue_write_ptr(q))) { + while (!(addr = vms_par_queue_write_ptr(q))) { ++data->writer_waited; } *addr = i; - shm_par_queue_write_finish(q); + vms_par_queue_write_finish(q); } clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &mid); @@ -207,8 +207,8 @@ static int par_queue_push_pop_1_writer(void *arg) { } static void run_par_queue_push_pop_1(void) { - shm_par_queue q; - shm_par_queue_init(&q, N, sizeof(int)); + vms_par_queue q; + vms_par_queue_init(&q, N, sizeof(int)); thrd_t tid; struct thr_data data = { @@ -222,7 +222,7 @@ static void run_par_queue_push_pop_1(void) { clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &mid); while (n < N) { - if (shm_par_queue_pop(&q, &j) == false) { + if (vms_par_queue_pop(&q, &j) == false) { ++reader_waited; continue; } @@ -242,7 +242,7 @@ static void run_par_queue_push_pop_1(void) { static int queue_spsc_push_pop_1_writer(void *arg) { struct thr_data *data = (struct thr_data *)arg; - shm_queue_spsc *q = (shm_queue_spsc *)data->ringbuffer; + vms_queue_spsc *q = (vms_queue_spsc *)data->ringbuffer; int *buff = data->data_buffer; int i; @@ -250,11 +250,11 @@ static int queue_spsc_push_pop_1_writer(void *arg) { struct timespec start, mid; clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start); for (i = 0; i < N; ++i) { - while (!shm_queue_spsc_write_offset(q, &off)) { + while (!vms_queue_spsc_write_offset(q, &off)) { ++data->writer_waited; } buff[off] = i; - shm_queue_spsc_write_finish(q); + vms_queue_spsc_write_finish(q); } clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &mid); @@ -264,8 +264,8 @@ static int queue_spsc_push_pop_1_writer(void *arg) { } static void run_queue_spsc_push_pop_1(void) { - shm_queue_spsc q; - shm_queue_spsc_init(&q, N); + vms_queue_spsc q; + vms_queue_spsc_init(&q, N); int *buff = malloc(sizeof(int) * N); assert(buff); @@ -282,12 +282,12 @@ static void run_queue_spsc_push_pop_1(void) { clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &mid); while (n < N) { - if (shm_queue_spsc_read_offset(&q, &off) == 0) { + if (vms_queue_spsc_read_offset(&q, &off) == 0) { ++reader_waited; continue; } memcpy(&j, buff + n, sizeof(int)); - shm_queue_spsc_consume(&q, 1); + vms_queue_spsc_consume(&q, 1); ++n; } clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end); diff --git a/tests/fetch-test-2.c b/tests/fetch-test-2.c index 5909da95..4d6449bb 100644 --- a/tests/fetch-test-2.c +++ b/tests/fetch-test-2.c @@ -15,13 +15,13 @@ static int stream_ready = 1; static int main_finished = 0; -static bool is_ready(shm_stream* s) { +static bool is_ready(vms_stream* s) { (void)s; return !!stream_ready; } struct event { - shm_event base; + vms_event base; size_t n; }; @@ -29,7 +29,7 @@ static size_t failed_push = 0; void* filler_thread(void* data) { struct buffer* buffer = (struct buffer*)data; struct event ev; - ev.base.kind = shm_get_last_special_kind() + 1; + ev.base.kind = vms_get_last_special_kind() + 1; for (size_t i = 1; i <= 10000; ++i) { ev.base.id = i; ev.n = i; @@ -44,14 +44,14 @@ void* filler_thread(void* data) { } void* reader_thread(void* data) { - shm_arbiter_buffer* arbiter_buffer_r = (shm_arbiter_buffer*)data; + vms_arbiter_buffer* arbiter_buffer_r = (vms_arbiter_buffer*)data; struct event* ev; size_t num; size_t next_id = 1; while (1) { - num = shm_arbiter_buffer_peek1(arbiter_buffer_r, (void**)&ev); + num = vms_arbiter_buffer_peek1(arbiter_buffer_r, (void**)&ev); if (num > 0) { - if (shm_event_is_hole((shm_event*)ev)) { + if (vms_event_is_hole((vms_event*)ev)) { next_id = 0; } else { assert(ev->n > 0); @@ -63,16 +63,16 @@ void* reader_thread(void* data) { if (ev->n != next_id) { fprintf(stderr, "%lu != %lu\n", ev->n, next_id); } - assert(next_id == shm_event_id((shm_event*)ev)); + assert(next_id == vms_event_id((vms_event*)ev)); assert(ev->n == next_id); - assert(ev->n == shm_event_id((shm_event*)ev)); + assert(ev->n == vms_event_id((vms_event*)ev)); ++next_id; } /* printf("Abuf Event: {{%lu, %lu}, %lu}\n", ev->base.id, ev->base.kind, ev->n); */ - assert(shm_arbiter_buffer_drop(arbiter_buffer_r, 1) == 1); + assert(vms_arbiter_buffer_drop(arbiter_buffer_r, 1) == 1); } if (num == 0 && stream_ready == 0 && main_finished == 1) break; @@ -85,22 +85,22 @@ int main(void) { struct buffer* buffer = initialize_local_buffer("/dummy", sizeof(struct event), 30, NULL); assert(buffer); - shm_stream dummy_stream; - shm_stream* stream = &dummy_stream; + vms_stream dummy_stream; + vms_stream* stream = &dummy_stream; - shm_stream_init(stream, buffer, sizeof(struct event), is_ready, NULL, NULL, + vms_stream_init(stream, buffer, sizeof(struct event), is_ready, NULL, NULL, NULL, NULL, "dummy-stream", "dummy"); /* into this buffer, stream_fetch() will push dropped() events if any */ - shm_arbiter_buffer* arbiter_buffer = - shm_arbiter_buffer_create(stream, sizeof(struct event), 10); + vms_arbiter_buffer* arbiter_buffer = + vms_arbiter_buffer_create(stream, sizeof(struct event), 10); /* into this buffer, we will push events here and read them */ - shm_arbiter_buffer* arbiter_buffer_r = - shm_arbiter_buffer_create(stream, sizeof(struct event), 3); + vms_arbiter_buffer* arbiter_buffer_r = + vms_arbiter_buffer_create(stream, sizeof(struct event), 3); - shm_arbiter_buffer_set_active(arbiter_buffer, 1); - shm_arbiter_buffer_set_active(arbiter_buffer_r, 1); + vms_arbiter_buffer_set_active(arbiter_buffer, 1); + vms_arbiter_buffer_set_active(arbiter_buffer_r, 1); pthread_t tid, tida; pthread_create(&tid, NULL, filler_thread, buffer); @@ -110,7 +110,7 @@ int main(void) { for (size_t i = 1; i <= 10000; ++i) { ev = stream_fetch(stream, arbiter_buffer); /* there should be no dropped event generated */ - assert(shm_arbiter_buffer_size(arbiter_buffer) == 0); + assert(vms_arbiter_buffer_size(arbiter_buffer) == 0); assert(ev); assert(ev->n == i); @@ -118,8 +118,8 @@ int main(void) { // printf("POP Event: {{%lu, %lu}, %lu}\n", ev->base.id, ev->base.kind, // ev->n); - shm_arbiter_buffer_push(arbiter_buffer_r, ev, sizeof(struct event)); - shm_stream_consume(stream, 1); + vms_arbiter_buffer_push(arbiter_buffer_r, ev, sizeof(struct event)); + vms_stream_consume(stream, 1); } ev = stream_fetch(stream, arbiter_buffer); diff --git a/tests/fetch-test-3.c b/tests/fetch-test-3.c index 244de255..a8464963 100644 --- a/tests/fetch-test-3.c +++ b/tests/fetch-test-3.c @@ -15,13 +15,13 @@ static int stream_ready = 1; static int main_finished = 0; -static bool is_ready(shm_stream *s) { +static bool is_ready(vms_stream *s) { (void)s; return !!stream_ready; } struct event { - shm_event base; + vms_event base; size_t n; }; @@ -29,7 +29,7 @@ static size_t failed_push = 0; void *filler_thread(void *data) { struct buffer *buffer = (struct buffer *)data; struct event ev; - ev.base.kind = shm_get_last_special_kind() + 1; + ev.base.kind = vms_get_last_special_kind() + 1; for (size_t i = 0; i < 10000; ++i) { ev.base.id = i + 1; ev.n = i; @@ -42,26 +42,26 @@ void *filler_thread(void *data) { } void *reader_thread(void *data) { - shm_arbiter_buffer *arbiter_buffer_r = (shm_arbiter_buffer *)data; + vms_arbiter_buffer *arbiter_buffer_r = (vms_arbiter_buffer *)data; struct event *ev; size_t num; size_t last_id = 0; size_t total = 0; while (1) { - num = shm_arbiter_buffer_peek1(arbiter_buffer_r, (void **)&ev); + num = vms_arbiter_buffer_peek1(arbiter_buffer_r, (void **)&ev); if (num > 0) { - assert(!shm_event_is_hole((shm_event *)ev)); + assert(!vms_event_is_hole((vms_event *)ev)); assert(ev->n == last_id); - assert(++last_id == shm_event_id((shm_event *)ev)); + assert(++last_id == vms_event_id((vms_event *)ev)); ++total; /* printf("Abuf Event: {{%lu, %lu}, %lu}\n", ev->base.id, ev->base.kind, ev->n); */ - assert(shm_arbiter_buffer_drop(arbiter_buffer_r, 1) == 1); + assert(vms_arbiter_buffer_drop(arbiter_buffer_r, 1) == 1); } if (stream_ready == 0 && main_finished == 1 && - shm_arbiter_buffer_size(arbiter_buffer_r) == 0) + vms_arbiter_buffer_size(arbiter_buffer_r) == 0) break; } @@ -76,22 +76,22 @@ int main(void) { struct buffer *buffer = initialize_local_buffer("/dummy", sizeof(struct event), 30, NULL); assert(buffer); - shm_stream dummy_stream; - shm_stream *stream = &dummy_stream; + vms_stream dummy_stream; + vms_stream *stream = &dummy_stream; - shm_stream_init(stream, buffer, sizeof(struct event), is_ready, NULL, NULL, + vms_stream_init(stream, buffer, sizeof(struct event), is_ready, NULL, NULL, NULL, NULL, "dummy-stream", "dummy"); /* into this buffer, stream_fetch() will push dropped() events if any */ - shm_arbiter_buffer *arbiter_buffer = - shm_arbiter_buffer_create(stream, sizeof(struct event), 10); + vms_arbiter_buffer *arbiter_buffer = + vms_arbiter_buffer_create(stream, sizeof(struct event), 10); /* into this buffer, we will push events here and read them */ - shm_arbiter_buffer *arbiter_buffer_r = - shm_arbiter_buffer_create(stream, sizeof(struct event), 3); + vms_arbiter_buffer *arbiter_buffer_r = + vms_arbiter_buffer_create(stream, sizeof(struct event), 3); - shm_arbiter_buffer_set_active(arbiter_buffer, 1); - shm_arbiter_buffer_set_active(arbiter_buffer_r, 1); + vms_arbiter_buffer_set_active(arbiter_buffer, 1); + vms_arbiter_buffer_set_active(arbiter_buffer_r, 1); pthread_t tid, tida; pthread_create(&tid, NULL, filler_thread, buffer); @@ -103,7 +103,7 @@ int main(void) { ev = stream_fetch(stream, arbiter_buffer); /* there should be no dropped event generated * since we are outputing the events into another buffer */ - assert(shm_arbiter_buffer_size(arbiter_buffer) == 0); + assert(vms_arbiter_buffer_size(arbiter_buffer) == 0); assert(ev); assert(ev->n == i); @@ -112,14 +112,14 @@ int main(void) { // ev->n); /* this function also automatically drops events */ - while (!(out = shm_arbiter_buffer_write_ptr(arbiter_buffer_r))) { + while (!(out = vms_arbiter_buffer_write_ptr(arbiter_buffer_r))) { ++waiting_for_arbbuf; } *out = *ev; - shm_arbiter_buffer_write_finish(arbiter_buffer_r); + vms_arbiter_buffer_write_finish(arbiter_buffer_r); - shm_stream_consume(stream, 1); + vms_stream_consume(stream, 1); } ev = stream_fetch(stream, arbiter_buffer); diff --git a/tests/fetch-test.c b/tests/fetch-test.c index 972729b4..ffa9759c 100644 --- a/tests/fetch-test.c +++ b/tests/fetch-test.c @@ -14,20 +14,20 @@ #include "shmbuf/buffer-private.h" static int stream_ready = 1; -static bool is_ready(shm_stream *s) { +static bool is_ready(vms_stream *s) { (void)s; return !!stream_ready; } struct event { - shm_event base; + vms_event base; int n; }; void *filler_thread(void *data) { struct buffer *buffer = (struct buffer *)data; struct event ev; - ev.base.kind = shm_get_last_special_kind() + 1; + ev.base.kind = vms_get_last_special_kind() + 1; for (int i = 0; i < 4; ++i) { ev.base.id = i + 1; ev.n = i; @@ -43,15 +43,15 @@ int main(void) { struct buffer *buffer = initialize_local_buffer("/dummy", sizeof(struct event), 30, NULL); assert(buffer); - shm_stream dummy_stream; - shm_stream *stream = &dummy_stream; + vms_stream dummy_stream; + vms_stream *stream = &dummy_stream; - shm_stream_init(stream, buffer, sizeof(struct event), is_ready, NULL, NULL, + vms_stream_init(stream, buffer, sizeof(struct event), is_ready, NULL, NULL, NULL, NULL, "dummy-stream", "dummy"); - shm_arbiter_buffer *arbiter_buffer = - shm_arbiter_buffer_create(stream, sizeof(int), 3); - shm_arbiter_buffer_set_active(arbiter_buffer, 1); + vms_arbiter_buffer *arbiter_buffer = + vms_arbiter_buffer_create(stream, sizeof(int), 3); + vms_arbiter_buffer_set_active(arbiter_buffer, 1); pthread_t tid; pthread_create(&tid, NULL, filler_thread, buffer); @@ -63,7 +63,7 @@ int main(void) { assert(ev->n == i); // printf("POP Event: {{%lu, %lu}, %d}\n", ev->base.id, ev->base.kind, // ev->n); - shm_stream_consume(stream, 1); + vms_stream_consume(stream, 1); } ev = stream_fetch(stream, arbiter_buffer); diff --git a/tests/list-test.c b/tests/list-test.c index e36a9482..9615fc1c 100644 --- a/tests/list-test.c +++ b/tests/list-test.c @@ -5,25 +5,25 @@ int main(void) { int num[] = {0, 1, 2, 3}; - shm_list l; - shm_list_init(&l); - assert(shm_list_size(&l) == 0); - assert(shm_list_append(&l, num + 0) == 1); - assert(shm_list_size(&l) == 1); - assert(shm_list_remove(&l, l.first) == 0); - assert(shm_list_size(&l) == 0); - assert(shm_list_prepend(&l, num + 0) == 1); - assert(shm_list_size(&l) == 1); - assert(shm_list_remove(&l, l.last) == 0); - assert(shm_list_size(&l) == 0); + vms_list l; + vms_list_init(&l); + assert(vms_list_size(&l) == 0); + assert(vms_list_append(&l, num + 0) == 1); + assert(vms_list_size(&l) == 1); + assert(vms_list_remove(&l, l.first) == 0); + assert(vms_list_size(&l) == 0); + assert(vms_list_prepend(&l, num + 0) == 1); + assert(vms_list_size(&l) == 1); + assert(vms_list_remove(&l, l.last) == 0); + assert(vms_list_size(&l) == 0); for (size_t i = 0; i < 4; ++i) - assert(shm_list_append(&l, num + i) == i + 1); + assert(vms_list_append(&l, num + i) == i + 1); - shm_list_elem *cur = l.first; + vms_list_elem *cur = l.first; int i = 0; int sum = 0; - shm_list_elem *x = NULL; + vms_list_elem *x = NULL; while (cur) { assert(cur->data == num + i); if (i == 2) @@ -34,7 +34,7 @@ int main(void) { } assert(sum == 6); - assert(shm_list_remove(&l, x) == 3); + assert(vms_list_remove(&l, x) == 3); cur = l.first; while (cur) { sum += *((int *)cur->data); @@ -42,24 +42,24 @@ int main(void) { } assert(sum == 10); // TODO: we leak it, destroy assumes that the list is dynamically allocated - // shm_list_destroy(&l, NULL); + // vms_list_destroy(&l, NULL); - shm_list_init(&l); + vms_list_init(&l); for (size_t i = 0; i < 4; ++i) - assert(shm_list_append(&l, num + i) == i + 1); + assert(vms_list_append(&l, num + i) == i + 1); for (size_t i = 0; i < 4; ++i) - assert(shm_list_remove(&l, shm_list_last(&l)) == 3 - i); - assert(shm_list_size(&l) == 0); + assert(vms_list_remove(&l, vms_list_last(&l)) == 3 - i); + assert(vms_list_size(&l) == 0); for (size_t i = 0; i < 4; ++i) - assert(shm_list_append(&l, num + i) == i + 1); + assert(vms_list_append(&l, num + i) == i + 1); for (size_t i = 0; i < 4; ++i) - assert(shm_list_remove(&l, shm_list_first(&l)) == 3 - i); - assert(shm_list_size(&l) == 0); + assert(vms_list_remove(&l, vms_list_first(&l)) == 3 - i); + assert(vms_list_size(&l) == 0); for (size_t i = 0; i < 4; ++i) - assert(shm_list_append(&l, num + i) == i + 1); - assert(shm_list_remove(&l, shm_list_first(&l)->next->next) == 3); - assert(shm_list_remove(&l, shm_list_first(&l)->next->next) == 2); - assert(shm_list_last(&l) == shm_list_first(&l)->next); - assert(shm_list_remove(&l, shm_list_first(&l)->next) == 1); - assert(shm_list_last(&l) == shm_list_first(&l)); + assert(vms_list_append(&l, num + i) == i + 1); + assert(vms_list_remove(&l, vms_list_first(&l)->next->next) == 3); + assert(vms_list_remove(&l, vms_list_first(&l)->next->next) == 2); + assert(vms_list_last(&l) == vms_list_first(&l)->next); + assert(vms_list_remove(&l, vms_list_first(&l)->next) == 1); + assert(vms_list_last(&l) == vms_list_first(&l)); } diff --git a/tests/queue-par-cbmc.c b/tests/queue-par-cbmc.c index 6b80a5aa..d36fd334 100644 --- a/tests/queue-par-cbmc.c +++ b/tests/queue-par-cbmc.c @@ -7,10 +7,10 @@ int buffer[4]; void *reader(void *data) { - shm_par_queue *q = (shm_par_queue *)data; + vms_par_queue *q = (vms_par_queue *)data; int n = 1; while (n < 4) { - if (shm_par_queue_pop(q, &buffer[n])) { + if (vms_par_queue_pop(q, &buffer[n])) { ++n; } } @@ -18,17 +18,17 @@ void *reader(void *data) { } void *writer(void *data) { - shm_par_queue *q = (shm_par_queue *)data; + vms_par_queue *q = (vms_par_queue *)data; for (int i = 1; i < 4; ++i) { - assert(shm_par_queue_push(q, &i, sizeof(i))); + assert(vms_par_queue_push(q, &i, sizeof(i))); } pthread_exit(0); } int main(void) { - shm_par_queue q; - shm_par_queue_init(&q, 3, sizeof(int)); + vms_par_queue q; + vms_par_queue_init(&q, 3, sizeof(int)); pthread_t r, w; pthread_create(&r, NULL, reader, &q); @@ -41,5 +41,5 @@ int main(void) { } // CBMC does not handle calls to free() with threads... - // shm_par_queue_destroy(&q); + // vms_par_queue_destroy(&q); } diff --git a/tests/queue-par-cbmc_2.c b/tests/queue-par-cbmc_2.c index c43b6066..4c1e781c 100644 --- a/tests/queue-par-cbmc_2.c +++ b/tests/queue-par-cbmc_2.c @@ -7,10 +7,10 @@ int buffer[4]; void *reader(void *data) { - shm_par_queue *q = (shm_par_queue *)data; + vms_par_queue *q = (vms_par_queue *)data; int n = 0; while (n < 3) { - if (shm_par_queue_pop(q, &buffer[n + 1])) { + if (vms_par_queue_pop(q, &buffer[n + 1])) { ++n; } } @@ -19,10 +19,10 @@ void *reader(void *data) { void *writer(void *data) { int num[] = {0, 1, 2, 3}; - shm_par_queue *q = (shm_par_queue *)data; + vms_par_queue *q = (vms_par_queue *)data; int n = 0; while (n < 3) { - if (shm_par_queue_push(q, &num[n + 1], sizeof(int))) { + if (vms_par_queue_push(q, &num[n + 1], sizeof(int))) { ++n; } } @@ -31,8 +31,8 @@ void *writer(void *data) { } int main(void) { - shm_par_queue q; - shm_par_queue_init(&q, 1, sizeof(int)); + vms_par_queue q; + vms_par_queue_init(&q, 1, sizeof(int)); pthread_t r, w; pthread_create(&r, NULL, reader, &q); @@ -45,5 +45,5 @@ int main(void) { } // CBMC does not handle calls to free() with threads... - // shm_par_queue_destroy(&q); + // vms_par_queue_destroy(&q); } diff --git a/tests/queue-par-cbmc_3.c b/tests/queue-par-cbmc_3.c index 7f7fc5d0..a174c456 100644 --- a/tests/queue-par-cbmc_3.c +++ b/tests/queue-par-cbmc_3.c @@ -6,38 +6,38 @@ extern bool nondet(void); #define CAPACITY 2 int main(void) { - shm_par_queue q; - shm_par_queue_init(&q, CAPACITY, sizeof(int)); + vms_par_queue q; + vms_par_queue_init(&q, CAPACITY, sizeof(int)); size_t elem_num = 0; int x = 0xbee; while (1) { if (nondet()) { /* POP */ - bool ret = shm_par_queue_pop(&q, &x); + bool ret = vms_par_queue_pop(&q, &x); if (ret) { assert(elem_num > 0); --elem_num; - assert(shm_par_queue_size(&q) < CAPACITY); + assert(vms_par_queue_size(&q) < CAPACITY); assert(x == 0xbee); } else { assert(elem_num == 0); - assert(shm_par_queue_size(&q) == 0); + assert(vms_par_queue_size(&q) == 0); } } else { /* PUSH */ - if (shm_par_queue_push(&q, &x, sizeof(int))) { + if (vms_par_queue_push(&q, &x, sizeof(int))) { ++elem_num; assert(elem_num <= CAPACITY); - assert(shm_par_queue_size(&q) <= CAPACITY); + assert(vms_par_queue_size(&q) <= CAPACITY); } else { assert(elem_num == CAPACITY); - assert(shm_par_queue_size(&q) == CAPACITY); - assert(shm_par_queue_size(&q) == shm_par_queue_capacity(&q)); + assert(vms_par_queue_size(&q) == CAPACITY); + assert(vms_par_queue_size(&q) == vms_par_queue_capacity(&q)); } - assert(shm_par_queue_size(&q) > 0); + assert(vms_par_queue_size(&q) > 0); } } - shm_par_queue_destroy(&q); + vms_par_queue_destroy(&q); } diff --git a/tests/queue-par-test-2.c b/tests/queue-par-test-2.c index 06125b59..e32cce5d 100644 --- a/tests/queue-par-test-2.c +++ b/tests/queue-par-test-2.c @@ -8,123 +8,123 @@ int main(void) { size_t i; int *p; int num[] = {0, 1, 2, 3, 4, 5, 6}; - shm_par_queue q; - shm_par_queue_init(&q, 3, sizeof(int)); - assert(shm_par_queue_size(&q) == 0); - assert(shm_par_queue_free_num(&q) == 3); - assert(shm_par_queue_peek_at(&q, 2) == NULL); + vms_par_queue q; + vms_par_queue_init(&q, 3, sizeof(int)); + assert(vms_par_queue_size(&q) == 0); + assert(vms_par_queue_free_num(&q) == 3); + assert(vms_par_queue_peek_at(&q, 2) == NULL); i = 2; - assert(shm_par_queue_peek_atmost_at(&q, &i) == NULL); + assert(vms_par_queue_peek_atmost_at(&q, &i) == NULL); assert(i == 0); - assert(shm_par_queue_push(&q, num + 1, sizeof(int)) == true); - assert(shm_par_queue_free_num(&q) == 2); - assert(shm_par_queue_size(&q) == 1); - assert(shm_par_queue_peek_at(&q, 2) == NULL); + assert(vms_par_queue_push(&q, num + 1, sizeof(int)) == true); + assert(vms_par_queue_free_num(&q) == 2); + assert(vms_par_queue_size(&q) == 1); + assert(vms_par_queue_peek_at(&q, 2) == NULL); i = 2; - assert((p = (int *)shm_par_queue_peek_atmost_at(&q, &i)) != NULL); + assert((p = (int *)vms_par_queue_peek_atmost_at(&q, &i)) != NULL); assert(i == 0); assert(*p == 1); - assert(shm_par_queue_push(&q, num + 2, sizeof(int)) == true); - assert(shm_par_queue_free_num(&q) == 1); - assert(shm_par_queue_size(&q) == 2); - assert(shm_par_queue_peek_at(&q, 2) == NULL); + assert(vms_par_queue_push(&q, num + 2, sizeof(int)) == true); + assert(vms_par_queue_free_num(&q) == 1); + assert(vms_par_queue_size(&q) == 2); + assert(vms_par_queue_peek_at(&q, 2) == NULL); i = 2; - assert((p = (int *)shm_par_queue_peek_atmost_at(&q, &i)) != NULL); + assert((p = (int *)vms_par_queue_peek_atmost_at(&q, &i)) != NULL); assert(i == 1); assert(*p == 2); - assert(shm_par_queue_push(&q, num + 3, sizeof(int)) == true); - assert(shm_par_queue_free_num(&q) == 0); - assert(shm_par_queue_size(&q) == 3); - assert((p = (int *)shm_par_queue_peek_at(&q, 2)) != NULL); + assert(vms_par_queue_push(&q, num + 3, sizeof(int)) == true); + assert(vms_par_queue_free_num(&q) == 0); + assert(vms_par_queue_size(&q) == 3); + assert((p = (int *)vms_par_queue_peek_at(&q, 2)) != NULL); assert(*p == 3); i = 2; - assert((p = (int *)shm_par_queue_peek_atmost_at(&q, &i)) != NULL); + assert((p = (int *)vms_par_queue_peek_atmost_at(&q, &i)) != NULL); assert(i == 2); assert(*p == 3); i = 5; - assert((p = (int *)shm_par_queue_peek_atmost_at(&q, &i)) != NULL); + assert((p = (int *)vms_par_queue_peek_atmost_at(&q, &i)) != NULL); assert(i == 2); assert(*p == 3); i = 0; - assert((p = (int *)shm_par_queue_peek_atmost_at(&q, &i)) != NULL); + assert((p = (int *)vms_par_queue_peek_atmost_at(&q, &i)) != NULL); assert(i == 0); assert(*p == 1); - assert((p = (int *)shm_par_queue_peek_at(&q, 0)) != NULL); + assert((p = (int *)vms_par_queue_peek_at(&q, 0)) != NULL); assert(*p == 1); i = 1; - assert((p = (int *)shm_par_queue_peek_atmost_at(&q, &i)) != NULL); + assert((p = (int *)vms_par_queue_peek_atmost_at(&q, &i)) != NULL); assert(i == 1); assert(*p == 2); - assert((p = (int *)shm_par_queue_peek_at(&q, 1)) != NULL); + assert((p = (int *)vms_par_queue_peek_at(&q, 1)) != NULL); assert(*p == 2); - assert(shm_par_queue_push(&q, num + 4, sizeof(int)) == false); - assert(shm_par_queue_free_num(&q) == 0); - assert(shm_par_queue_size(&q) == 3); - for (i = 0; i < shm_par_queue_size(&q); ++i) { + assert(vms_par_queue_push(&q, num + 4, sizeof(int)) == false); + assert(vms_par_queue_free_num(&q) == 0); + assert(vms_par_queue_size(&q) == 3); + for (i = 0; i < vms_par_queue_size(&q); ++i) { size_t j = i; - p = (int *)shm_par_queue_peek_at(&q, i); + p = (int *)vms_par_queue_peek_at(&q, i); assert(*p == (int)i + 1); - p = (int *)shm_par_queue_peek_atmost_at(&q, &j); + p = (int *)vms_par_queue_peek_atmost_at(&q, &j); assert(j == i); assert(*p == (int)i + 1); } - assert(shm_par_queue_pop(&q, &i) == true && i == 1); - assert(shm_par_queue_size(&q) == 2); - assert(shm_par_queue_free_num(&q) == 1); - assert(shm_par_queue_pop(&q, &i) == true && i == 2); - assert(shm_par_queue_size(&q) == 1); - assert(shm_par_queue_free_num(&q) == 2); + assert(vms_par_queue_pop(&q, &i) == true && i == 1); + assert(vms_par_queue_size(&q) == 2); + assert(vms_par_queue_free_num(&q) == 1); + assert(vms_par_queue_pop(&q, &i) == true && i == 2); + assert(vms_par_queue_size(&q) == 1); + assert(vms_par_queue_free_num(&q) == 2); - assert((p = (int *)shm_par_queue_peek_at(&q, 1)) == NULL); + assert((p = (int *)vms_par_queue_peek_at(&q, 1)) == NULL); i = 1; - assert((p = (int *)shm_par_queue_peek_atmost_at(&q, &i)) != NULL); + assert((p = (int *)vms_par_queue_peek_atmost_at(&q, &i)) != NULL); assert(i == 0); assert(*p == 3); - assert((p = (int *)shm_par_queue_peek_at(&q, 0)) != NULL); + assert((p = (int *)vms_par_queue_peek_at(&q, 0)) != NULL); assert(*p == 3); i = 0; - assert((p = (int *)shm_par_queue_peek_atmost_at(&q, &i)) != NULL); + assert((p = (int *)vms_par_queue_peek_atmost_at(&q, &i)) != NULL); assert(i == 0); assert(*p == 3); i = 2; - assert((p = (int *)shm_par_queue_peek_atmost_at(&q, &i)) != NULL); + assert((p = (int *)vms_par_queue_peek_atmost_at(&q, &i)) != NULL); assert(i == 0); assert(*p == 3); - assert(shm_par_queue_pop(&q, &i) == true && i == 3); - assert(shm_par_queue_free_num(&q) == 3); - assert(shm_par_queue_size(&q) == 0); - assert((p = (int *)shm_par_queue_peek_at(&q, 1)) == NULL); + assert(vms_par_queue_pop(&q, &i) == true && i == 3); + assert(vms_par_queue_free_num(&q) == 3); + assert(vms_par_queue_size(&q) == 0); + assert((p = (int *)vms_par_queue_peek_at(&q, 1)) == NULL); i = 1; - assert((p = (int *)shm_par_queue_peek_atmost_at(&q, &i)) == NULL); + assert((p = (int *)vms_par_queue_peek_atmost_at(&q, &i)) == NULL); assert(i == 0); - assert((p = (int *)shm_par_queue_peek_at(&q, 0)) == NULL); + assert((p = (int *)vms_par_queue_peek_at(&q, 0)) == NULL); i = 0; - assert((p = (int *)shm_par_queue_peek_atmost_at(&q, &i)) == NULL); + assert((p = (int *)vms_par_queue_peek_atmost_at(&q, &i)) == NULL); assert(i == 0); - assert(shm_par_queue_push(&q, num + 0, sizeof(int)) == true); - assert(shm_par_queue_push(&q, num + 1, sizeof(int)) == true); - assert(shm_par_queue_pop(&q, &i) == true && i == 0); - assert(shm_par_queue_push(&q, num + 2, sizeof(int)) == true); - assert(shm_par_queue_push(&q, num + 3, sizeof(int)) == true); - assert(shm_par_queue_push(&q, num + 4, sizeof(int)) == false); - assert(shm_par_queue_pop(&q, &i) == true && i == 1); - assert(shm_par_queue_pop(&q, &i) == true && i == 2); - assert(shm_par_queue_pop(&q, &i) == true && i == 3); - assert(shm_par_queue_size(&q) == 0); - assert(shm_par_queue_push(&q, num + 2, sizeof(int)) == true); - assert(shm_par_queue_push(&q, num + 3, sizeof(int)) == true); - assert(shm_par_queue_push(&q, num + 4, sizeof(int)) == true); - assert(shm_par_queue_push(&q, num + 5, sizeof(int)) == false); + assert(vms_par_queue_push(&q, num + 0, sizeof(int)) == true); + assert(vms_par_queue_push(&q, num + 1, sizeof(int)) == true); + assert(vms_par_queue_pop(&q, &i) == true && i == 0); + assert(vms_par_queue_push(&q, num + 2, sizeof(int)) == true); + assert(vms_par_queue_push(&q, num + 3, sizeof(int)) == true); + assert(vms_par_queue_push(&q, num + 4, sizeof(int)) == false); + assert(vms_par_queue_pop(&q, &i) == true && i == 1); + assert(vms_par_queue_pop(&q, &i) == true && i == 2); + assert(vms_par_queue_pop(&q, &i) == true && i == 3); + assert(vms_par_queue_size(&q) == 0); + assert(vms_par_queue_push(&q, num + 2, sizeof(int)) == true); + assert(vms_par_queue_push(&q, num + 3, sizeof(int)) == true); + assert(vms_par_queue_push(&q, num + 4, sizeof(int)) == true); + assert(vms_par_queue_push(&q, num + 5, sizeof(int)) == false); int *d1, *d2; size_t s1, s2; - size_t n = shm_par_queue_peek(&q, 5, (void **)&d1, &s1, (void **)&d2, &s2); + size_t n = vms_par_queue_peek(&q, 5, (void **)&d1, &s1, (void **)&d2, &s2); assert(n == 3); assert(s1 + s2 == 3); size_t si = 0; @@ -138,25 +138,25 @@ int main(void) { ++si; } - assert(*(int *)shm_par_queue_peek_at(&q, 0) == 2); - assert(*(int *)shm_par_queue_peek_at(&q, 1) == 3); - assert(*(int *)shm_par_queue_peek_at(&q, 2) == 4); + assert(*(int *)vms_par_queue_peek_at(&q, 0) == 2); + assert(*(int *)vms_par_queue_peek_at(&q, 1) == 3); + assert(*(int *)vms_par_queue_peek_at(&q, 2) == 4); size_t k = 0; - assert(*(int *)shm_par_queue_peek_atmost_at(&q, &k) == 2 && k == 0); + assert(*(int *)vms_par_queue_peek_atmost_at(&q, &k) == 2 && k == 0); k = 1; - assert(*(int *)shm_par_queue_peek_atmost_at(&q, &k) == 3 && k == 1); + assert(*(int *)vms_par_queue_peek_atmost_at(&q, &k) == 3 && k == 1); k = 2; - assert(*(int *)shm_par_queue_peek_atmost_at(&q, &k) == 4 && k == 2); + assert(*(int *)vms_par_queue_peek_atmost_at(&q, &k) == 4 && k == 2); k = 3; - assert(*(int *)shm_par_queue_peek_atmost_at(&q, &k) == 4 && k == 2); + assert(*(int *)vms_par_queue_peek_atmost_at(&q, &k) == 4 && k == 2); k = 4; - assert(*(int *)shm_par_queue_peek_atmost_at(&q, &k) == 4 && k == 2); + assert(*(int *)vms_par_queue_peek_atmost_at(&q, &k) == 4 && k == 2); k = 4444; - assert(*(int *)shm_par_queue_peek_atmost_at(&q, &k) == 4 && k == 2); + assert(*(int *)vms_par_queue_peek_atmost_at(&q, &k) == 4 && k == 2); k = 111111; - assert(*(int *)shm_par_queue_peek_atmost_at(&q, &k) == 4 && k == 2); + assert(*(int *)vms_par_queue_peek_atmost_at(&q, &k) == 4 && k == 2); k = ~(0LL); - assert(*(int *)shm_par_queue_peek_atmost_at(&q, &k) == 4 && k == 2); + assert(*(int *)vms_par_queue_peek_atmost_at(&q, &k) == 4 && k == 2); - shm_par_queue_destroy(&q); + vms_par_queue_destroy(&q); } diff --git a/tests/queue-par-test.c b/tests/queue-par-test.c index 2dab2913..4ac2377b 100644 --- a/tests/queue-par-test.c +++ b/tests/queue-par-test.c @@ -6,10 +6,10 @@ int buffer[4]; int reader(void *data) { - shm_par_queue *q = (shm_par_queue *)data; + vms_par_queue *q = (vms_par_queue *)data; int n = 0; while (n < 3) { - if (shm_par_queue_pop(q, &buffer[n + 1])) { + if (vms_par_queue_pop(q, &buffer[n + 1])) { ++n; } } @@ -18,15 +18,15 @@ int reader(void *data) { int writer(void *data) { int num[] = {0, 1, 2, 3}; - shm_par_queue *q = (shm_par_queue *)data; - for (int i = 1; i < 4; ++i) shm_par_queue_push(q, &num[i], sizeof(int)); + vms_par_queue *q = (vms_par_queue *)data; + for (int i = 1; i < 4; ++i) vms_par_queue_push(q, &num[i], sizeof(int)); thrd_exit(0); } int main(void) { - shm_par_queue q; - shm_par_queue_init(&q, 3, sizeof(int)); + vms_par_queue q; + vms_par_queue_init(&q, 3, sizeof(int)); thrd_t r, w; thrd_create(&r, reader, &q); @@ -38,5 +38,5 @@ int main(void) { assert(buffer[i] == i); } - shm_par_queue_destroy(&q); + vms_par_queue_destroy(&q); } diff --git a/tests/spsc-ringbuf-1.c b/tests/spsc-ringbuf-1.c index 9e830d6f..1539d67f 100644 --- a/tests/spsc-ringbuf-1.c +++ b/tests/spsc-ringbuf-1.c @@ -6,143 +6,143 @@ #define CAPACITY 3 int main(void) { - shm_spsc_ringbuf r; + vms_spsc_ringbuf r; size_t n, off, len1, len2; - shm_spsc_ringbuf_init(&r, CAPACITY); - assert(shm_spsc_ringbuf_capacity(&r) == CAPACITY); - assert(shm_spsc_ringbuf_max_size(&r) == CAPACITY - 1); + vms_spsc_ringbuf_init(&r, CAPACITY); + assert(vms_spsc_ringbuf_capacity(&r) == CAPACITY); + assert(vms_spsc_ringbuf_max_size(&r) == CAPACITY - 1); /* ringbuf is empty */ - assert(shm_spsc_ringbuf_size(&r) == 0); - assert(shm_spsc_ringbuf_free_num(&r) == shm_spsc_ringbuf_max_size(&r)); - assert(!shm_spsc_ringbuf_full(&r)); - assert(shm_spsc_ringbuf_peek(&r, 0, &off, &len1, &len2) == 0); - assert(shm_spsc_ringbuf_peek(&r, 3, &off, &len1, &len2) == 0); + assert(vms_spsc_ringbuf_size(&r) == 0); + assert(vms_spsc_ringbuf_free_num(&r) == vms_spsc_ringbuf_max_size(&r)); + assert(!vms_spsc_ringbuf_full(&r)); + assert(vms_spsc_ringbuf_peek(&r, 0, &off, &len1, &len2) == 0); + assert(vms_spsc_ringbuf_peek(&r, 3, &off, &len1, &len2) == 0); - shm_spsc_ringbuf_read_off_nowrap(&r, &n); + vms_spsc_ringbuf_read_off_nowrap(&r, &n); assert(n == 0); - assert(shm_spsc_ringbuf_consume_upto(&r, CAPACITY) == 0); - assert(shm_spsc_ringbuf_consume_upto(&r, 1) == 0); - assert(shm_spsc_ringbuf_consume_upto(&r, 0) == 0); + assert(vms_spsc_ringbuf_consume_upto(&r, CAPACITY) == 0); + assert(vms_spsc_ringbuf_consume_upto(&r, 1) == 0); + assert(vms_spsc_ringbuf_consume_upto(&r, 0) == 0); /* write 1 elem */ - off = shm_spsc_ringbuf_write_off_nowrap(&r, &n); - assert(n == shm_spsc_ringbuf_max_size(&r)); + off = vms_spsc_ringbuf_write_off_nowrap(&r, &n); + assert(n == vms_spsc_ringbuf_max_size(&r)); assert(off == 0); - assert(shm_spsc_ringbuf_size(&r) == 0); - assert(shm_spsc_ringbuf_free_num(&r) == shm_spsc_ringbuf_max_size(&r)); - assert(!shm_spsc_ringbuf_full(&r)); - assert(shm_spsc_ringbuf_peek(&r, 0, &off, &len1, &len2) == 0); - assert(shm_spsc_ringbuf_peek(&r, 3, &off, &len1, &len2) == 0); + assert(vms_spsc_ringbuf_size(&r) == 0); + assert(vms_spsc_ringbuf_free_num(&r) == vms_spsc_ringbuf_max_size(&r)); + assert(!vms_spsc_ringbuf_full(&r)); + assert(vms_spsc_ringbuf_peek(&r, 0, &off, &len1, &len2) == 0); + assert(vms_spsc_ringbuf_peek(&r, 3, &off, &len1, &len2) == 0); - shm_spsc_ringbuf_write_finish(&r, 1); + vms_spsc_ringbuf_write_finish(&r, 1); /* ringbuf has 1 elem */ - assert(shm_spsc_ringbuf_size(&r) == 1); - assert(shm_spsc_ringbuf_free_num(&r) == shm_spsc_ringbuf_max_size(&r) - 1); - assert(!shm_spsc_ringbuf_full(&r)); - assert(shm_spsc_ringbuf_peek(&r, 0, &off, &len1, &len2) == 1); - assert(shm_spsc_ringbuf_peek(&r, 3, &off, &len1, &len2) == 1); + assert(vms_spsc_ringbuf_size(&r) == 1); + assert(vms_spsc_ringbuf_free_num(&r) == vms_spsc_ringbuf_max_size(&r) - 1); + assert(!vms_spsc_ringbuf_full(&r)); + assert(vms_spsc_ringbuf_peek(&r, 0, &off, &len1, &len2) == 1); + assert(vms_spsc_ringbuf_peek(&r, 3, &off, &len1, &len2) == 1); assert(len1 == 1); assert(len2 == 0); - off = shm_spsc_ringbuf_read_off_nowrap(&r, &n); + off = vms_spsc_ringbuf_read_off_nowrap(&r, &n); assert(n == 1); assert(off == 0); - shm_spsc_ringbuf_consume_upto(&r, 1); + vms_spsc_ringbuf_consume_upto(&r, 1); /* ringbuf is empty */ - assert(shm_spsc_ringbuf_size(&r) == 0); - assert(shm_spsc_ringbuf_free_num(&r) == shm_spsc_ringbuf_max_size(&r)); - assert(!shm_spsc_ringbuf_full(&r)); - assert(shm_spsc_ringbuf_peek(&r, 0, &off, &len1, &len2) == 0); - assert(shm_spsc_ringbuf_peek(&r, 3, &off, &len1, &len2) == 0); + assert(vms_spsc_ringbuf_size(&r) == 0); + assert(vms_spsc_ringbuf_free_num(&r) == vms_spsc_ringbuf_max_size(&r)); + assert(!vms_spsc_ringbuf_full(&r)); + assert(vms_spsc_ringbuf_peek(&r, 0, &off, &len1, &len2) == 0); + assert(vms_spsc_ringbuf_peek(&r, 3, &off, &len1, &len2) == 0); - shm_spsc_ringbuf_read_off_nowrap(&r, &n); + vms_spsc_ringbuf_read_off_nowrap(&r, &n); assert(n == 0); - assert(shm_spsc_ringbuf_consume_upto(&r, CAPACITY) == 0); - assert(shm_spsc_ringbuf_consume_upto(&r, 1) == 0); - assert(shm_spsc_ringbuf_consume_upto(&r, 0) == 0); + assert(vms_spsc_ringbuf_consume_upto(&r, CAPACITY) == 0); + assert(vms_spsc_ringbuf_consume_upto(&r, 1) == 0); + assert(vms_spsc_ringbuf_consume_upto(&r, 0) == 0); /* write 1 elem */ - off = shm_spsc_ringbuf_write_off_nowrap(&r, &n); - assert(n == shm_spsc_ringbuf_max_size(&r)); + off = vms_spsc_ringbuf_write_off_nowrap(&r, &n); + assert(n == vms_spsc_ringbuf_max_size(&r)); - assert(shm_spsc_ringbuf_size(&r) == 0); - assert(shm_spsc_ringbuf_free_num(&r) == shm_spsc_ringbuf_max_size(&r)); - assert(!shm_spsc_ringbuf_full(&r)); - assert(shm_spsc_ringbuf_peek(&r, 0, &off, &len1, &len2) == 0); - assert(shm_spsc_ringbuf_peek(&r, 3, &off, &len1, &len2) == 0); + assert(vms_spsc_ringbuf_size(&r) == 0); + assert(vms_spsc_ringbuf_free_num(&r) == vms_spsc_ringbuf_max_size(&r)); + assert(!vms_spsc_ringbuf_full(&r)); + assert(vms_spsc_ringbuf_peek(&r, 0, &off, &len1, &len2) == 0); + assert(vms_spsc_ringbuf_peek(&r, 3, &off, &len1, &len2) == 0); - shm_spsc_ringbuf_write_finish(&r, 1); + vms_spsc_ringbuf_write_finish(&r, 1); /* write 1 elem */ - off = shm_spsc_ringbuf_write_off_nowrap(&r, &n); - assert(n == shm_spsc_ringbuf_max_size(&r) - 1); + off = vms_spsc_ringbuf_write_off_nowrap(&r, &n); + assert(n == vms_spsc_ringbuf_max_size(&r) - 1); assert(off < CAPACITY); - assert(shm_spsc_ringbuf_size(&r) == 1); - assert(shm_spsc_ringbuf_free_num(&r) == shm_spsc_ringbuf_max_size(&r) - 1); - assert(!shm_spsc_ringbuf_full(&r)); - assert(shm_spsc_ringbuf_peek(&r, CAPACITY, &off, &len1, &len2) == 1); + assert(vms_spsc_ringbuf_size(&r) == 1); + assert(vms_spsc_ringbuf_free_num(&r) == vms_spsc_ringbuf_max_size(&r) - 1); + assert(!vms_spsc_ringbuf_full(&r)); + assert(vms_spsc_ringbuf_peek(&r, CAPACITY, &off, &len1, &len2) == 1); assert(len1 == 1); assert(len2 == 0); - assert(shm_spsc_ringbuf_peek(&r, CAPACITY + 2, &off, &len1, &len2) == 1); + assert(vms_spsc_ringbuf_peek(&r, CAPACITY + 2, &off, &len1, &len2) == 1); assert(len1 == 1); assert(len2 == 0); - assert(shm_spsc_ringbuf_peek(&r, 0, &off, &len1, &len2) == 1); + assert(vms_spsc_ringbuf_peek(&r, 0, &off, &len1, &len2) == 1); - shm_spsc_ringbuf_write_finish(&r, 1); + vms_spsc_ringbuf_write_finish(&r, 1); - assert(shm_spsc_ringbuf_size(&r) == 2); - assert(shm_spsc_ringbuf_free_num(&r) == shm_spsc_ringbuf_max_size(&r) - 2); - assert(shm_spsc_ringbuf_full(&r)); - assert(shm_spsc_ringbuf_peek(&r, CAPACITY, &off, &len1, &len2) == 2); + assert(vms_spsc_ringbuf_size(&r) == 2); + assert(vms_spsc_ringbuf_free_num(&r) == vms_spsc_ringbuf_max_size(&r) - 2); + assert(vms_spsc_ringbuf_full(&r)); + assert(vms_spsc_ringbuf_peek(&r, CAPACITY, &off, &len1, &len2) == 2); assert(len1 == 2); assert(len2 == 0); - assert(shm_spsc_ringbuf_peek(&r, CAPACITY + 2, &off, &len1, &len2) == 2); + assert(vms_spsc_ringbuf_peek(&r, CAPACITY + 2, &off, &len1, &len2) == 2); assert(len1 == 2); assert(len2 == 0); - assert(shm_spsc_ringbuf_peek(&r, 0, &off, &len1, &len2) == 2); + assert(vms_spsc_ringbuf_peek(&r, 0, &off, &len1, &len2) == 2); /* write 1 elem -- should fail */ - off = shm_spsc_ringbuf_write_off_nowrap(&r, &n); - assert(n == shm_spsc_ringbuf_max_size(&r) - 2); + off = vms_spsc_ringbuf_write_off_nowrap(&r, &n); + assert(n == vms_spsc_ringbuf_max_size(&r) - 2); assert(n == 0); /* consume 1 */ - shm_spsc_ringbuf_consume(&r, 1); + vms_spsc_ringbuf_consume(&r, 1); /* write 1 to wrap around */ - off = shm_spsc_ringbuf_write_off_nowrap(&r, &n); - assert(n == shm_spsc_ringbuf_max_size(&r) - 1); + off = vms_spsc_ringbuf_write_off_nowrap(&r, &n); + assert(n == vms_spsc_ringbuf_max_size(&r) - 1); assert(n == 1); assert(off < CAPACITY); - assert(shm_spsc_ringbuf_size(&r) == 1); - assert(shm_spsc_ringbuf_free_num(&r) == shm_spsc_ringbuf_max_size(&r) - 1); - assert(!shm_spsc_ringbuf_full(&r)); - assert(shm_spsc_ringbuf_peek(&r, CAPACITY, &off, &len1, &len2) == 1); + assert(vms_spsc_ringbuf_size(&r) == 1); + assert(vms_spsc_ringbuf_free_num(&r) == vms_spsc_ringbuf_max_size(&r) - 1); + assert(!vms_spsc_ringbuf_full(&r)); + assert(vms_spsc_ringbuf_peek(&r, CAPACITY, &off, &len1, &len2) == 1); assert(len1 == 1); assert(len2 == 0); - assert(shm_spsc_ringbuf_peek(&r, CAPACITY + 2, &off, &len1, &len2) == 1); + assert(vms_spsc_ringbuf_peek(&r, CAPACITY + 2, &off, &len1, &len2) == 1); assert(len1 == 1); assert(len2 == 0); - assert(shm_spsc_ringbuf_peek(&r, 0, &off, &len1, &len2) == 1); + assert(vms_spsc_ringbuf_peek(&r, 0, &off, &len1, &len2) == 1); - shm_spsc_ringbuf_write_finish(&r, 1); + vms_spsc_ringbuf_write_finish(&r, 1); - assert(shm_spsc_ringbuf_size(&r) == 2); - assert(shm_spsc_ringbuf_free_num(&r) == shm_spsc_ringbuf_max_size(&r) - 2); - assert(shm_spsc_ringbuf_full(&r)); - assert(shm_spsc_ringbuf_peek(&r, CAPACITY, &off, &len1, &len2) == 2); + assert(vms_spsc_ringbuf_size(&r) == 2); + assert(vms_spsc_ringbuf_free_num(&r) == vms_spsc_ringbuf_max_size(&r) - 2); + assert(vms_spsc_ringbuf_full(&r)); + assert(vms_spsc_ringbuf_peek(&r, CAPACITY, &off, &len1, &len2) == 2); assert(len1 == 1); assert(len2 == 1); - assert(shm_spsc_ringbuf_peek(&r, CAPACITY + 2, &off, &len1, &len2) == 2); + assert(vms_spsc_ringbuf_peek(&r, CAPACITY + 2, &off, &len1, &len2) == 2); assert(len1 == 1); assert(len2 == 1); - assert(shm_spsc_ringbuf_peek(&r, 0, &off, &len1, &len2) == 2); + assert(vms_spsc_ringbuf_peek(&r, 0, &off, &len1, &len2) == 2); } diff --git a/tests/spsc-ringbuf-cbmc-1.c b/tests/spsc-ringbuf-cbmc-1.c index 5e1e7f8a..21c874be 100644 --- a/tests/spsc-ringbuf-cbmc-1.c +++ b/tests/spsc-ringbuf-cbmc-1.c @@ -6,94 +6,94 @@ extern int nondet(void); #define CAPACITY 3 int main(void) { - shm_spsc_ringbuf r; + vms_spsc_ringbuf r; size_t n, off, len1, len2; - shm_spsc_ringbuf_init(&r, CAPACITY); - assert(shm_spsc_ringbuf_capacity(&r) == CAPACITY); - assert(shm_spsc_ringbuf_max_size(&r) == CAPACITY - 1); + vms_spsc_ringbuf_init(&r, CAPACITY); + assert(vms_spsc_ringbuf_capacity(&r) == CAPACITY); + assert(vms_spsc_ringbuf_max_size(&r) == CAPACITY - 1); /* ringbuf is empty */ - assert(shm_spsc_ringbuf_size(&r) == 0); - assert(shm_spsc_ringbuf_free_num(&r) == shm_spsc_ringbuf_max_size(&r)); - assert(!shm_spsc_ringbuf_full(&r)); - assert(shm_spsc_ringbuf_peek(&r, 0, &off, &len1, &len2) == 0); - assert(shm_spsc_ringbuf_peek(&r, 3, &off, &len1, &len2) == 0); + assert(vms_spsc_ringbuf_size(&r) == 0); + assert(vms_spsc_ringbuf_free_num(&r) == vms_spsc_ringbuf_max_size(&r)); + assert(!vms_spsc_ringbuf_full(&r)); + assert(vms_spsc_ringbuf_peek(&r, 0, &off, &len1, &len2) == 0); + assert(vms_spsc_ringbuf_peek(&r, 3, &off, &len1, &len2) == 0); - shm_spsc_ringbuf_read_off_nowrap(&r, &n); + vms_spsc_ringbuf_read_off_nowrap(&r, &n); assert(n == 0); - assert(shm_spsc_ringbuf_consume_upto(&r, CAPACITY) == 0); - assert(shm_spsc_ringbuf_consume_upto(&r, 1) == 0); - assert(shm_spsc_ringbuf_consume_upto(&r, 0) == 0); + assert(vms_spsc_ringbuf_consume_upto(&r, CAPACITY) == 0); + assert(vms_spsc_ringbuf_consume_upto(&r, 1) == 0); + assert(vms_spsc_ringbuf_consume_upto(&r, 0) == 0); size_t elem_num = 0; while (1) { - assert(shm_spsc_ringbuf_size(&r) == elem_num); + assert(vms_spsc_ringbuf_size(&r) == elem_num); if (nondet()) { /* CONSUME */ - assert(shm_spsc_ringbuf_free_num(&r) == - shm_spsc_ringbuf_max_size(&r) - elem_num); - assert(!shm_spsc_ringbuf_full(&r) || - shm_spsc_ringbuf_free_num(&r) == 0); - assert(shm_spsc_ringbuf_full(&r) || - shm_spsc_ringbuf_free_num(&r) > 0); + assert(vms_spsc_ringbuf_free_num(&r) == + vms_spsc_ringbuf_max_size(&r) - elem_num); + assert(!vms_spsc_ringbuf_full(&r) || + vms_spsc_ringbuf_free_num(&r) == 0); + assert(vms_spsc_ringbuf_full(&r) || + vms_spsc_ringbuf_free_num(&r) > 0); - assert(shm_spsc_ringbuf_peek(&r, CAPACITY, &off, &len1, &len2) == + assert(vms_spsc_ringbuf_peek(&r, CAPACITY, &off, &len1, &len2) == elem_num); - assert(shm_spsc_ringbuf_peek(&r, 0, &off, &len1, &len2) == + assert(vms_spsc_ringbuf_peek(&r, 0, &off, &len1, &len2) == elem_num); - off = shm_spsc_ringbuf_read_off_nowrap(&r, &n); + off = vms_spsc_ringbuf_read_off_nowrap(&r, &n); assert(elem_num == 0 || n > 0); - assert(shm_spsc_ringbuf_size(&r) == 0 || n > 0); - assert(n == 0 || shm_spsc_ringbuf_size(&r) > 0); + assert(vms_spsc_ringbuf_size(&r) == 0 || n > 0); + assert(n == 0 || vms_spsc_ringbuf_size(&r) > 0); if (n > 0) { assert(elem_num > 0); --elem_num; - assert(shm_spsc_ringbuf_size(&r) > 0); - assert(shm_spsc_ringbuf_size(&r) <= - shm_spsc_ringbuf_max_size(&r)); - shm_spsc_ringbuf_consume(&r, 1); - assert(shm_spsc_ringbuf_size(&r) < - shm_spsc_ringbuf_max_size(&r)); + assert(vms_spsc_ringbuf_size(&r) > 0); + assert(vms_spsc_ringbuf_size(&r) <= + vms_spsc_ringbuf_max_size(&r)); + vms_spsc_ringbuf_consume(&r, 1); + assert(vms_spsc_ringbuf_size(&r) < + vms_spsc_ringbuf_max_size(&r)); } else { assert(elem_num == 0); - assert(shm_spsc_ringbuf_size(&r) == 0); - assert(shm_spsc_ringbuf_free_num(&r) == - shm_spsc_ringbuf_max_size(&r)); + assert(vms_spsc_ringbuf_size(&r) == 0); + assert(vms_spsc_ringbuf_free_num(&r) == + vms_spsc_ringbuf_max_size(&r)); } } else { /* write 1 elem */ - off = shm_spsc_ringbuf_write_off_nowrap(&r, &n); - assert(shm_spsc_ringbuf_free_num(&r) <= - shm_spsc_ringbuf_max_size(&r)); + off = vms_spsc_ringbuf_write_off_nowrap(&r, &n); + assert(vms_spsc_ringbuf_free_num(&r) <= + vms_spsc_ringbuf_max_size(&r)); /* PUSH */ if (n > 0) { - assert(shm_spsc_ringbuf_size(&r) == elem_num); - assert(!shm_spsc_ringbuf_full(&r)); - assert(elem_num <= shm_spsc_ringbuf_max_size(&r)); - assert(shm_spsc_ringbuf_peek(&r, 3, &off, &len1, &len2) == + assert(vms_spsc_ringbuf_size(&r) == elem_num); + assert(!vms_spsc_ringbuf_full(&r)); + assert(elem_num <= vms_spsc_ringbuf_max_size(&r)); + assert(vms_spsc_ringbuf_peek(&r, 3, &off, &len1, &len2) == elem_num); - assert(shm_spsc_ringbuf_peek(&r, 0, &off, &len1, &len2) == + assert(vms_spsc_ringbuf_peek(&r, 0, &off, &len1, &len2) == elem_num); ++elem_num; - shm_spsc_ringbuf_write_finish(&r, 1); + vms_spsc_ringbuf_write_finish(&r, 1); - assert(elem_num <= shm_spsc_ringbuf_max_size(&r)); - assert(shm_spsc_ringbuf_peek(&r, 3, &off, &len1, &len2) == + assert(elem_num <= vms_spsc_ringbuf_max_size(&r)); + assert(vms_spsc_ringbuf_peek(&r, 3, &off, &len1, &len2) == elem_num); - assert(shm_spsc_ringbuf_peek(&r, 0, &off, &len1, &len2) == + assert(vms_spsc_ringbuf_peek(&r, 0, &off, &len1, &len2) == elem_num); } else { - assert(elem_num == shm_spsc_ringbuf_max_size(&r)); - assert(shm_spsc_ringbuf_full(&r)); - assert(shm_spsc_ringbuf_size(&r) == - shm_spsc_ringbuf_max_size(&r)); + assert(elem_num == vms_spsc_ringbuf_max_size(&r)); + assert(vms_spsc_ringbuf_full(&r)); + assert(vms_spsc_ringbuf_size(&r) == + vms_spsc_ringbuf_max_size(&r)); } - assert(shm_spsc_ringbuf_size(&r) > 0); + assert(vms_spsc_ringbuf_size(&r) > 0); } } } From f1ac1b4f0ae9d1d55fff95ba7e048465ca76c5a8 Mon Sep 17 00:00:00 2001 From: Marek Chalupa Date: Wed, 19 Apr 2023 17:01:26 +0200 Subject: [PATCH 02/34] Format the previous patch --- include/vamos-buffers/core/vector.h | 6 ------ src/core/arbiter.c | 2 -- src/streams/stream-drregex.c | 1 - src/streams/stream-funs.c | 1 - 4 files changed, 10 deletions(-) diff --git a/include/vamos-buffers/core/vector.h b/include/vamos-buffers/core/vector.h index 0d5e56de..e1c64e49 100644 --- a/include/vamos-buffers/core/vector.h +++ b/include/vamos-buffers/core/vector.h @@ -23,7 +23,6 @@ void vms_vector_destroy(vms_vector *vec); size_t vms_vector_size(vms_vector *vec); size_t vms_vector_elem_size(vms_vector *vec); - /** * Return the pointer to the element at index `idx`. * @@ -38,7 +37,6 @@ void *vms_vector_at(vms_vector *vec, size_t idx); */ void *vms_vector_at_checked(vms_vector *vec, size_t idx); - /** * Push an element pointed by `elem` into `vec`. * Memory is reallocated if needed. By default, the new memory can store @@ -46,7 +44,6 @@ void *vms_vector_at_checked(vms_vector *vec, size_t idx); */ size_t vms_vector_push(vms_vector *vec, void *elem); - /** * Return the pointer to the next free space in the vector and increase * the number of elements in the vector by one. @@ -66,20 +63,17 @@ void *vms_vector_extend(vms_vector *vec); */ size_t vms_vector_pop(vms_vector *vec); - /** * Return the pointer to the top element or NULL * if the vector is empty. */ void *vms_vector_top(vms_vector *vec); - /** * Swap the contents of two vectors. */ void vms_vector_swap(vms_vector *vec, vms_vector *with); - /** * Resize the vector to hold `size` elements. If the current size * of the vector is smaller than `size`, do nothing. diff --git a/src/core/arbiter.c b/src/core/arbiter.c index 02f6d188..95e35f15 100644 --- a/src/core/arbiter.c +++ b/src/core/arbiter.c @@ -637,7 +637,6 @@ void *stream_filter_fetch(vms_stream *stream, vms_arbiter_buffer *buffer, } } - bool vms_arbiter_buffer_is_done(vms_arbiter_buffer *buffer) { /* XXX: should we rather use a flag that we set to true when stream-fetch * knows that the stream is done? */ @@ -655,7 +654,6 @@ bool vms_arbiter_buffer_is_done(vms_arbiter_buffer *buffer) { vms_par_queue_size(&buffer->buffer) == 0); } - void vms_arbiter_buffer_notify_dropped(vms_arbiter_buffer *buffer, uint64_t begin_id, uint64_t end_id) { vms_stream_notify_dropped(buffer->stream, begin_id, end_id); diff --git a/src/streams/stream-drregex.c b/src/streams/stream-drregex.c index 5c1d95e9..ab12067e 100644 --- a/src/streams/stream-drregex.c +++ b/src/streams/stream-drregex.c @@ -7,7 +7,6 @@ #include "vamos-buffers/core/arbiter.h" #include "vamos-buffers/shmbuf/buffer.h" - bool drregex_is_ready(vms_stream *stream) { struct buffer *b = ((vms_stream_drregex *)stream)->shmbuffer; /* buffer must be ready or it may not be ready anymore, but it diff --git a/src/streams/stream-funs.c b/src/streams/stream-funs.c index b6253bf5..5cdc456a 100644 --- a/src/streams/stream-funs.c +++ b/src/streams/stream-funs.c @@ -7,7 +7,6 @@ #include "vamos-buffers/core/arbiter.h" #include "vamos-buffers/shmbuf/buffer.h" - bool funs_is_ready(vms_stream *stream) { struct buffer *b = ((vms_stream_funs *)stream)->shmbuffer; /* buffer must be ready or it may not be ready anymore, but it From 6a2d754739d1ed1a806ba2528bd2c37c198000a3 Mon Sep 17 00:00:00 2001 From: Marek Chalupa Date: Wed, 19 Apr 2023 17:02:01 +0200 Subject: [PATCH 03/34] Reformat the project --- include/vamos-buffers/core/vector-macro.h | 1 - include/vamos-buffers/core/vector.h | 6 +++--- include/vamos-buffers/streams/stream-generic.h | 1 - include/vamos-buffers/streams/stream-regex.h | 2 +- include/vamos-buffers/streams/stream-regexrw.h | 2 +- src/core/arbiter.c | 3 ++- src/core/event.c | 4 ++-- src/core/list-embedded.c | 4 ++-- src/core/list.c | 4 ++-- src/core/monitor.c | 3 ++- src/core/par_queue.c | 4 ++-- src/core/queue_spsc.c | 4 ++-- src/core/spsc_ringbuf.c | 16 ++++++++-------- src/core/stream.c | 2 +- src/core/vector-aligned.c | 3 ++- src/shmbuf/buffer-aux.c | 1 - src/shmbuf/buffer-local.c | 2 +- src/shmbuf/buffer-sub.c | 3 +-- src/shmbuf/client.c | 5 +++-- src/streams/stream-drregex.c | 3 ++- src/streams/stream-funs.c | 3 ++- src/streams/stream-generic.c | 1 - src/streams/stream-regexrw.c | 3 ++- src/streams/streams.c | 2 +- tests/arbiter-buffer.c | 2 +- tests/buffer-test.c | 3 ++- tests/fetch-test-2.c | 5 ++--- tests/fetch-test-3.c | 5 ++--- tests/fetch-test.c | 5 ++--- 29 files changed, 51 insertions(+), 51 deletions(-) diff --git a/include/vamos-buffers/core/vector-macro.h b/include/vamos-buffers/core/vector-macro.h index eec04449..3ccaa72a 100644 --- a/include/vamos-buffers/core/vector-macro.h +++ b/include/vamos-buffers/core/vector-macro.h @@ -121,7 +121,6 @@ VEC_SIZE(vec) += 1; \ } while (0) - #define VEC_EXTEND(vec, outptr) VEC_EXTEND_N(vec, outptr, 16) /** diff --git a/include/vamos-buffers/core/vector.h b/include/vamos-buffers/core/vector.h index e1c64e49..7d900120 100644 --- a/include/vamos-buffers/core/vector.h +++ b/include/vamos-buffers/core/vector.h @@ -83,9 +83,9 @@ void vms_vector_swap(vms_vector *vec, vms_vector *with); * * Note that the vector may have allocated memory for more than `size` * elements. In that case the memory remains non-0'ed. - * This should not be a problem as the uninitialized memory should not be touched - * as the user knows only that there is space for `size` elements - * but nothing more. + * This should not be a problem as the uninitialized memory should not be + * touched as the user knows only that there is space for `size` elements but + * nothing more. */ void vms_vector_resize(vms_vector *vec, size_t size); diff --git a/include/vamos-buffers/streams/stream-generic.h b/include/vamos-buffers/streams/stream-generic.h index d561614b..3db20962 100644 --- a/include/vamos-buffers/streams/stream-generic.h +++ b/include/vamos-buffers/streams/stream-generic.h @@ -7,7 +7,6 @@ #include "vamos-buffers/core/event.h" #include "vamos-buffers/core/stream.h" - struct buffer; /* A generic stream for events stored in shared memory buffer, diff --git a/include/vamos-buffers/streams/stream-regex.h b/include/vamos-buffers/streams/stream-regex.h index 761117db..62e5f2c6 100644 --- a/include/vamos-buffers/streams/stream-regex.h +++ b/include/vamos-buffers/streams/stream-regex.h @@ -2,8 +2,8 @@ #include #include "vamos-buffers/core/event.h" -#include "vamos-buffers/shmbuf/buffer.h" #include "vamos-buffers/core/stream.h" +#include "vamos-buffers/shmbuf/buffer.h" typedef struct _vms_event_regex { vms_event base; diff --git a/include/vamos-buffers/streams/stream-regexrw.h b/include/vamos-buffers/streams/stream-regexrw.h index f6aa3f2c..56f75f24 100644 --- a/include/vamos-buffers/streams/stream-regexrw.h +++ b/include/vamos-buffers/streams/stream-regexrw.h @@ -2,8 +2,8 @@ #include #include "vamos-buffers/core/event.h" -#include "vamos-buffers/shmbuf/buffer.h" #include "vamos-buffers/core/stream.h" +#include "vamos-buffers/shmbuf/buffer.h" typedef struct _vms_event_regexrw { vms_event base; diff --git a/src/core/arbiter.c b/src/core/arbiter.c index 95e35f15..1dd598c5 100644 --- a/src/core/arbiter.c +++ b/src/core/arbiter.c @@ -1,8 +1,9 @@ +#include "vamos-buffers/core/arbiter.h" + #include #include #include -#include "vamos-buffers/core/arbiter.h" #include "vamos-buffers/core/par_queue.h" #include "vamos-buffers/core/stream.h" #include "vamos-buffers/core/utils.h" diff --git a/src/core/event.c b/src/core/event.c index 69b7c953..2c528043 100644 --- a/src/core/event.c +++ b/src/core/event.c @@ -1,9 +1,9 @@ +#include "vamos-buffers/core/event.h" + #include #include #include -#include "vamos-buffers/core/event.h" - static const vms_kind hole_kind = 1; static const vms_kind last_special_kind = 1; diff --git a/src/core/list-embedded.c b/src/core/list-embedded.c index 2ba80072..fd6da4fc 100644 --- a/src/core/list-embedded.c +++ b/src/core/list-embedded.c @@ -1,9 +1,9 @@ +#include "vamos-buffers/core/list-embedded.h" + #include #include #include -#include "vamos-buffers/core/list-embedded.h" - void vms_list_embedded_init(vms_list_embedded *list) { list->prev = list; list->next = list; diff --git a/src/core/list.c b/src/core/list.c index 89c1c2f2..c45ef077 100644 --- a/src/core/list.c +++ b/src/core/list.c @@ -1,9 +1,9 @@ +#include "vamos-buffers/core/list.h" + #include #include #include -#include "vamos-buffers/core/list.h" - void vms_list_init(vms_list *list) { memset(list, 0, sizeof(vms_list)); } void vms_list_destroy(vms_list *list, vms_list_elem_destroy_fn destroy) { diff --git a/src/core/monitor.c b/src/core/monitor.c index 8696ba95..c7d932c5 100644 --- a/src/core/monitor.c +++ b/src/core/monitor.c @@ -1,10 +1,11 @@ +#include "vamos-buffers/core/monitor.h" + #include #include #include #include #include -#include "vamos-buffers/core/monitor.h" #include "vamos-buffers/core/par_queue.h" #include "vamos-buffers/core/utils.h" diff --git a/src/core/par_queue.c b/src/core/par_queue.c index 3d94caf0..8603e446 100644 --- a/src/core/par_queue.c +++ b/src/core/par_queue.c @@ -1,11 +1,11 @@ +#include "vamos-buffers/core/par_queue.h" + #include #include #include #include #include -#include "vamos-buffers/core/par_queue.h" - #define __predict_false(x) __builtin_expect((x) != 0, 0) #define __predict_true(x) __builtin_expect((x) != 0, 1) diff --git a/src/core/queue_spsc.c b/src/core/queue_spsc.c index 0c17bbe8..470be6d9 100644 --- a/src/core/queue_spsc.c +++ b/src/core/queue_spsc.c @@ -1,11 +1,11 @@ +#include "vamos-buffers/core/queue_spsc.h" + #include #include #include #include #include -#include "vamos-buffers/core/queue_spsc.h" - #define __predict_false(x) __builtin_expect((x) != 0, 0) #define __predict_true(x) __builtin_expect((x) != 0, 1) diff --git a/src/core/spsc_ringbuf.c b/src/core/spsc_ringbuf.c index 1c87909c..c1610c39 100644 --- a/src/core/spsc_ringbuf.c +++ b/src/core/spsc_ringbuf.c @@ -1,8 +1,8 @@ +#include "vamos-buffers/core/spsc_ringbuf.h" + #include #include -#include "vamos-buffers/core/spsc_ringbuf.h" - #define __predict_false(x) __builtin_expect((x) != 0, 0) #define __predict_true(x) __builtin_expect((x) != 0, 1) @@ -32,7 +32,7 @@ static inline size_t _get_written_num(size_t head, size_t tail, return head - tail; } else { if (__predict_false(tail == head)) { - return 0; + return 0; } else { return capacity - tail + head; } @@ -187,9 +187,9 @@ size_t vms_spsc_ringbuf_write_off(vms_spsc_ringbuf *b, size_t *n, size_t vms_spsc_ringbuf_write_off_nowrap(vms_spsc_ringbuf *b, size_t *n) { const size_t head = atomic_load_explicit(&b->head, memory_order_acquire); - /* Update the cache if seen_tail is 0 (which very likely means it has not been updated yet, - * but can be of course also after wrapping around) or if there is no space left considering - * the cached information. */ + /* Update the cache if seen_tail is 0 (which very likely means it has not + * been updated yet, but can be of course also after wrapping around) or if + * there is no space left considering the cached information. */ if (b->seen_tail == 0 || get_write_off(head, b->seen_tail, b->capacity, n, NULL) == 0) { b->seen_tail = atomic_load_explicit(&b->tail, memory_order_relaxed); @@ -340,8 +340,8 @@ size_t vms_spsc_ringbuf_peek(vms_spsc_ringbuf *b, size_t n, size_t *off, const size_t tail = atomic_load_explicit(&b->tail, memory_order_acquire); size_t head = b->seen_head; size_t cur_elem_num = get_written_num(head, tail, b->capacity); - /* update the information if needed or when n == 0 (which means we want to get an up-to-date the number - * of elements) */ + /* update the information if needed or when n == 0 (which means we want to + * get an up-to-date the number of elements) */ if (cur_elem_num < n || n == 0) { b->seen_head = head = atomic_load_explicit(&b->head, memory_order_acquire); diff --git a/src/core/stream.c b/src/core/stream.c index 2c4010c8..6c96ae37 100644 --- a/src/core/stream.c +++ b/src/core/stream.c @@ -5,9 +5,9 @@ #include #include +#include "vamos-buffers/core/utils.h" #include "vamos-buffers/core/vector-macro.h" #include "vamos-buffers/shmbuf/buffer.h" -#include "vamos-buffers/core/utils.h" /***** * STREAM diff --git a/src/core/vector-aligned.c b/src/core/vector-aligned.c index cd212e48..1ad26607 100644 --- a/src/core/vector-aligned.c +++ b/src/core/vector-aligned.c @@ -1,8 +1,9 @@ +#include "vamos-buffers/core/vector-aligned.h" + #include #include #include -#include "vamos-buffers/core/vector-aligned.h" #include "vamos-buffers/core/utils.h" void vms_vector_aligned_init(vms_vector_aligned *vec, size_t elem_size, diff --git a/src/shmbuf/buffer-aux.c b/src/shmbuf/buffer-aux.c index 1889c21d..50a6f77d 100644 --- a/src/shmbuf/buffer-aux.c +++ b/src/shmbuf/buffer-aux.c @@ -20,7 +20,6 @@ void drop_ranges_lock(struct buffer *buff) { } while (atomic_compare_exchange_weak(l, &unlocked, true)); } - HIDE_SYMBOL void aux_buffer_release(struct aux_buffer *buffer) { assert((buffer->size + sizeof(struct aux_buffer)) % sysconf(_SC_PAGESIZE) == diff --git a/src/shmbuf/buffer-local.c b/src/shmbuf/buffer-local.c index 7e86060b..d4f4b8b6 100644 --- a/src/shmbuf/buffer-local.c +++ b/src/shmbuf/buffer-local.c @@ -14,12 +14,12 @@ #include #include "buffer-private.h" -#include "vamos-buffers/shmbuf/buffer.h" #include "vamos-buffers/core/list.h" #include "vamos-buffers/core/source.h" #include "vamos-buffers/core/spsc_ringbuf.h" #include "vamos-buffers/core/utils.h" #include "vamos-buffers/core/vector-macro.h" +#include "vamos-buffers/shmbuf/buffer.h" /* FOR TESTING */ struct buffer *initialize_local_buffer(const char *key, size_t elem_size, diff --git a/src/shmbuf/buffer-sub.c b/src/shmbuf/buffer-sub.c index 1846c777..011351d7 100644 --- a/src/shmbuf/buffer-sub.c +++ b/src/shmbuf/buffer-sub.c @@ -3,10 +3,9 @@ #include "buffer-private.h" #include "shm.h" - -#include "vamos-buffers/shmbuf/buffer.h" #include "vamos-buffers/core/source.h" #include "vamos-buffers/core/vector-macro.h" +#include "vamos-buffers/shmbuf/buffer.h" char *get_sub_buffer_key(const char *key, size_t idx) { size_t tmpsize = strlen(key) + 16 /* space for index */; diff --git a/src/shmbuf/client.c b/src/shmbuf/client.c index 2daf139d..806f35f3 100644 --- a/src/shmbuf/client.c +++ b/src/shmbuf/client.c @@ -1,13 +1,14 @@ +#include "vamos-buffers/shmbuf/client.h" + #include #include #include #include #include -#include "vamos-buffers/shmbuf/buffer.h" #include "shm.h" -#include "vamos-buffers/shmbuf/client.h" #include "vamos-buffers/core/utils.h" +#include "vamos-buffers/shmbuf/buffer.h" #define SLEEP_TIME 20 diff --git a/src/streams/stream-drregex.c b/src/streams/stream-drregex.c index ab12067e..eecd71fa 100644 --- a/src/streams/stream-drregex.c +++ b/src/streams/stream-drregex.c @@ -1,9 +1,10 @@ +#include "vamos-buffers/streams/stream-drregex.h" + #include #include #include #include -#include "vamos-buffers/streams/stream-drregex.h" #include "vamos-buffers/core/arbiter.h" #include "vamos-buffers/shmbuf/buffer.h" diff --git a/src/streams/stream-funs.c b/src/streams/stream-funs.c index 5cdc456a..9477c5ba 100644 --- a/src/streams/stream-funs.c +++ b/src/streams/stream-funs.c @@ -1,9 +1,10 @@ +#include "vamos-buffers/streams/stream-funs.h" + #include #include #include #include -#include "vamos-buffers/streams/stream-funs.h" #include "vamos-buffers/core/arbiter.h" #include "vamos-buffers/shmbuf/buffer.h" diff --git a/src/streams/stream-generic.c b/src/streams/stream-generic.c index dc70fa34..e5fea9dd 100644 --- a/src/streams/stream-generic.c +++ b/src/streams/stream-generic.c @@ -8,7 +8,6 @@ #include "vamos-buffers/core/arbiter.h" #include "vamos-buffers/shmbuf/buffer.h" - /* A generic stream for events stored in shared memory buffer, no filter nor modification of events supported (unless done manually). */ diff --git a/src/streams/stream-regexrw.c b/src/streams/stream-regexrw.c index eb88a6d7..c6d91b75 100644 --- a/src/streams/stream-regexrw.c +++ b/src/streams/stream-regexrw.c @@ -1,10 +1,11 @@ +#include "vamos-buffers/streams/stream-regexrw.h" + #include #include #include #include -#include "vamos-buffers/streams/stream-regexrw.h" #include "vamos-buffers/core/arbiter.h" bool sregexrw_is_ready(vms_stream *stream) { diff --git a/src/streams/streams.c b/src/streams/streams.c index ed6c7836..24623d3d 100644 --- a/src/streams/streams.c +++ b/src/streams/streams.c @@ -3,12 +3,12 @@ #include #include +#include "vamos-buffers/core/stream.h" #include "vamos-buffers/streams/stream-drregex.h" #include "vamos-buffers/streams/stream-funs.h" #include "vamos-buffers/streams/stream-generic.h" #include "vamos-buffers/streams/stream-regex.h" #include "vamos-buffers/streams/stream-regexrw.h" -#include "vamos-buffers/core/stream.h" const char *find_next_part(const char *params) { params = strchr(params, ':'); diff --git a/tests/arbiter-buffer.c b/tests/arbiter-buffer.c index 31ef37ef..9459e932 100644 --- a/tests/arbiter-buffer.c +++ b/tests/arbiter-buffer.c @@ -8,8 +8,8 @@ #include #include "vamos-buffers/core/arbiter.h" -#include "vamos-buffers/shmbuf/buffer.h" #include "vamos-buffers/core/stream.h" +#include "vamos-buffers/shmbuf/buffer.h" static bool is_ready(vms_stream *s) { (void)s; diff --git a/tests/buffer-test.c b/tests/buffer-test.c index 0bf2f8f3..e418f360 100644 --- a/tests/buffer-test.c +++ b/tests/buffer-test.c @@ -1,9 +1,10 @@ #undef NDEBUG +#include "vamos-buffers/shmbuf/buffer.h" + #include #include -#include "vamos-buffers/shmbuf/buffer.h" #include "vamos-buffers/core/source.h" int main(void) { diff --git a/tests/fetch-test-2.c b/tests/fetch-test-2.c index 4d6449bb..655b78cd 100644 --- a/tests/fetch-test-2.c +++ b/tests/fetch-test-2.c @@ -7,11 +7,10 @@ #include #include +#include "shmbuf/buffer-private.h" #include "vamos-buffers/core/arbiter.h" -#include "vamos-buffers/shmbuf/buffer.h" #include "vamos-buffers/core/stream.h" - -#include "shmbuf/buffer-private.h" +#include "vamos-buffers/shmbuf/buffer.h" static int stream_ready = 1; static int main_finished = 0; diff --git a/tests/fetch-test-3.c b/tests/fetch-test-3.c index a8464963..10992fac 100644 --- a/tests/fetch-test-3.c +++ b/tests/fetch-test-3.c @@ -7,11 +7,10 @@ #include #include +#include "shmbuf/buffer-private.h" #include "vamos-buffers/core/arbiter.h" -#include "vamos-buffers/shmbuf/buffer.h" #include "vamos-buffers/core/stream.h" - -#include "shmbuf/buffer-private.h" +#include "vamos-buffers/shmbuf/buffer.h" static int stream_ready = 1; static int main_finished = 0; diff --git a/tests/fetch-test.c b/tests/fetch-test.c index ffa9759c..9f7eebba 100644 --- a/tests/fetch-test.c +++ b/tests/fetch-test.c @@ -7,11 +7,10 @@ #include #include +#include "shmbuf/buffer-private.h" #include "vamos-buffers/core/arbiter.h" -#include "vamos-buffers/shmbuf/buffer.h" #include "vamos-buffers/core/stream.h" - -#include "shmbuf/buffer-private.h" +#include "vamos-buffers/shmbuf/buffer.h" static int stream_ready = 1; static bool is_ready(vms_stream *s) { From ff18c6bb51f805170e7752fd2a6d549a4f80e330 Mon Sep 17 00:00:00 2001 From: Marek Chalupa Date: Wed, 19 Apr 2023 17:02:30 +0200 Subject: [PATCH 04/34] update .git-blame-ignore-revs --- .git-blame-ignore-revs | 1 + 1 file changed, 1 insertion(+) diff --git a/.git-blame-ignore-revs b/.git-blame-ignore-revs index d055e586..a8b7b496 100755 --- a/.git-blame-ignore-revs +++ b/.git-blame-ignore-revs @@ -10,3 +10,4 @@ ef787ba565f7c4e6bb1ba5f9259300254e95e266 141621deb9177fb2fcd74d7e14965da0e267beab 67843e4a329e0b49308ee13eedbad0683bdf7221 2f5e5f2fdc872e39a157bb7086c3bdde28fce239 +6a2d754739d1ed1a806ba2528bd2c37c198000a3 From ccc659c87ef0332b083534ef4ba889de11fc6de2 Mon Sep 17 00:00:00 2001 From: Marek Chalupa Date: Wed, 19 Apr 2023 17:57:15 +0200 Subject: [PATCH 05/34] cpp: add C++ bindings to vms_event --- include/vamos-buffers/core/event.h | 21 +++++++++++++++------ include/vamos-buffers/cpp/event.h | 17 +++++++++++++++++ src/core/event.c | 6 +++--- 3 files changed, 35 insertions(+), 9 deletions(-) create mode 100644 include/vamos-buffers/cpp/event.h diff --git a/include/vamos-buffers/core/event.h b/include/vamos-buffers/core/event.h index 2a9de8d2..16938b6a 100644 --- a/include/vamos-buffers/core/event.h +++ b/include/vamos-buffers/core/event.h @@ -1,5 +1,9 @@ -#ifndef SHAMON_EVENT_H_ -#define SHAMON_EVENT_H_ +#ifndef VMS_EVENT_H_ +#define VMS_EVENT_H_ + +#ifdef __cplusplus +extern "C" { +#endif #include #include @@ -28,12 +32,17 @@ void initialize_events(void) __attribute__((deprecated)); void deinitialize_events(void) __attribute__((deprecated)); // EVENTS -vms_eventid vms_event_id(vms_event *event); -vms_kind vms_event_kind(vms_event *event); +vms_eventid vms_event_id(const vms_event *event); +vms_kind vms_event_kind(const vms_event *event); // DROP EVENT -bool vms_event_is_hole(vms_event *); +bool vms_event_is_hole(const vms_event *); vms_kind vms_get_hole_kind(void); vms_kind vms_get_last_special_kind(void); -#endif // SHAMON_EVENT_H_ + +#ifdef __cplusplus +} +#endif + +#endif // VMS_EVENT_H diff --git a/include/vamos-buffers/cpp/event.h b/include/vamos-buffers/cpp/event.h new file mode 100644 index 00000000..3f5d629d --- /dev/null +++ b/include/vamos-buffers/cpp/event.h @@ -0,0 +1,17 @@ +#ifndef VAMOS_BUFFERS_EVENT_H +#define VAMOS_BUFFERS_EVENT_H + +#include + +namespace vamos { + +class Event : public vms_event { +public: + vms_eventid id() const { return vms_event_id(this); } + vms_kind kind() const { return vms_event_kind(this); }; + bool is_hole() const { return vms_event_is_hole(this); } +}; + +} + +#endif diff --git a/src/core/event.c b/src/core/event.c index 2c528043..64be72e2 100644 --- a/src/core/event.c +++ b/src/core/event.c @@ -14,7 +14,7 @@ void initialize_events(void) { void deinitialize_events(void) {} -bool vms_event_is_hole(vms_event *ev) { +bool vms_event_is_hole(const vms_event *ev) { assert(hole_kind > 0); return vms_event_kind(ev) == hole_kind; } @@ -30,6 +30,6 @@ vms_kind vms_get_last_special_kind(void) { return last_special_kind; } -vms_eventid vms_event_id(vms_event *event) { return event->id; } +vms_eventid vms_event_id(const vms_event *event) { return event->id; } -vms_kind vms_event_kind(vms_event *event) { return event->kind; } +vms_kind vms_event_kind(const vms_event *event) { return event->kind; } From a51875a2e1b9cb89e452fe88c874e687ac0ad311 Mon Sep 17 00:00:00 2001 From: Marek Chalupa Date: Wed, 19 Apr 2023 21:33:45 +0200 Subject: [PATCH 06/34] cpp: rewrite ringbuffer to C++ We use templates to propagate the capacity as constant. --- include/vamos-buffers/core/event.h | 6 + include/vamos-buffers/cpp/event.h | 6 +- include/vamos-buffers/cpp/ring_buffer.h | 392 ++++++++++++++++++++++++ 3 files changed, 402 insertions(+), 2 deletions(-) create mode 100644 include/vamos-buffers/cpp/ring_buffer.h diff --git a/include/vamos-buffers/core/event.h b/include/vamos-buffers/core/event.h index 16938b6a..a6193560 100644 --- a/include/vamos-buffers/core/event.h +++ b/include/vamos-buffers/core/event.h @@ -18,8 +18,14 @@ typedef struct _vms_stream vms_stream; typedef struct _vms_event { vms_kind kind; vms_eventid id; + +#ifdef __cplusplus + _vms_event() {} + _vms_event(vms_kind kind, vms_eventid id): kind(kind), id(id) {} +#endif } vms_event; + typedef struct _vms_event_default_hole { vms_event base; size_t n; /* number of dropped events */ diff --git a/include/vamos-buffers/cpp/event.h b/include/vamos-buffers/cpp/event.h index 3f5d629d..6d03a0d2 100644 --- a/include/vamos-buffers/cpp/event.h +++ b/include/vamos-buffers/cpp/event.h @@ -5,8 +5,10 @@ namespace vamos { -class Event : public vms_event { -public: +struct Event : public vms_event { + Event() {} + Event(vms_kind kind, vms_eventid id): _vms_event(kind, id) {} + vms_eventid id() const { return vms_event_id(this); } vms_kind kind() const { return vms_event_kind(this); }; bool is_hole() const { return vms_event_is_hole(this); } diff --git a/include/vamos-buffers/cpp/ring_buffer.h b/include/vamos-buffers/cpp/ring_buffer.h new file mode 100644 index 00000000..6f294d9f --- /dev/null +++ b/include/vamos-buffers/cpp/ring_buffer.h @@ -0,0 +1,392 @@ +#ifndef VAMOS_CPP_RING_BUFFER_H +#define VAMOS_CPP_RING_BUFFER_H + +#include +#include + +namespace vamos { + +#define __predict_false(x) __builtin_expect((x) != 0, 0) +#define __predict_true(x) __builtin_expect((x) != 0, 1) + +/* On x86 and ARM the cache line has 64 bytes, change if needed. */ +#define CACHELINE_SIZE 64 +#define CACHELINE_ALIGNED alignas(CACHELINE_SIZE) +#define ADDR_IS_CACHE_ALIGNED(addr) \ + (((unsigned long long)(addr)) % CACHELINE_SIZE == 0) + +/** + * Single-producer single-consumer (SPSC) lock-free concurrent ring-buffer + * of a fixed size; + * This implementation is rewritten `spsc_ringbuf.h`, but it is not kept + * in sync, so the two implementations might diverge. + */ + +namespace { +static inline size_t _is_empty(size_t head, size_t tail) { + return head == tail; +} + +template +size_t _is_full(size_t head, size_t tail) { + return head + 1 == tail || (head == capacity - 1 && tail == 0); +} + +template +size_t __written_num(size_t head, size_t tail) { + if (tail < head) { + return head - tail; + } else { + if (__predict_false(tail == head)) { + return 0; + } else { + return capacity - tail + head; + } + } +} + +template +size_t __free_num(size_t head, size_t tail) { + assert(head < capacity); + assert(tail < capacity); + + if (tail < head) { + return (capacity - head - 1) + tail; + } + + if (__predict_false(head == tail)) { + return capacity - 1; + } + + return tail - head - 1; +} + +#define CHECK_IF(c, t) assert(!(c) || (t)) +#define CHECK_IFF(c, t) \ + do { \ + CHECK_IF(c, t); \ + CHECK_IF(t, c); \ + } while (0) + +template +size_t _written_num(size_t head, size_t tail) { + /* pre */ + assert(head < capacity); + assert(tail < capacity); + + size_t ret = __written_num(head, tail); + + /* post */ + assert(ret < capacity); + CHECK_IFF(_is_empty(head, tail), ret == 0); + CHECK_IFF(_is_full(head, tail), ret == capacity - 1); + CHECK_IF(tail < head, ret == head - tail); + CHECK_IF(tail > head, ret == (head + (capacity - tail))); + assert(ret == capacity - 1 - __free_num(head, tail)); + + return ret; +} + +template +size_t _free_num(size_t head, size_t tail) { + assert(head < capacity); + assert(tail < capacity); + + size_t ret = __free_num(head, tail); + + assert(ret < capacity); + CHECK_IFF(_is_empty(head, tail), ret == capacity - 1); + CHECK_IFF(_is_full(head, tail), ret == 0); + assert(ret == capacity - 1 - __written_num(head, tail)); + + return ret; +} + +} // namespace + + +template +class RingBuffer { + CACHELINE_ALIGNED std::atomic _tail; + CACHELINE_ALIGNED std::atomic _head; + +public: + static constexpr uint64_t Capacity = CAPACITY; + + constexpr uint64_t capacity() const { return CAPACITY; } + + RingBuffer(): _tail(0), _head(0) { + static_assert(CAPACITY > 0); + } + + size_t load_head(std::memory_order mo) const { + return _head.load(mo); + } + + size_t load_tail(std::memory_order mo) const { + return _tail.load(mo); + } + + void set_head(size_t new_head, std::memory_order mo) { + _head.store(new_head, mo); + } + + void set_tail(size_t new_tail, std::memory_order mo) { + _head.store(new_tail, mo); + } + + bool is_full() const { + return __predict_false(_is_full(load_head(std::memory_order_relaxed), + load_tail(std::memory_order_relaxed))); + } +}; + +template +class RingBufferAccessor { + static constexpr uint64_t _capacity = RingBufferTy::Capacity; + +protected: + RingBufferTy &rb; + // the cache for the head/tail last seen by this thread + size_t seen; + +public: + RingBufferAccessor(RingBufferTy &rb) : rb(rb) {} //, _capacity(rb.capacity()) {} + + + constexpr size_t capacity() const { + return _capacity; + } + + size_t max_size() const { + /* we use one element as a separator */ + return capacity() - 1; + } + + size_t size() const { + return _written_num(rb.load_head(std::memory_order_relaxed), + rb.load_tail(std::memory_order_relaxed)); + } + + bool empty() const { + return _is_empty(rb.load_head(std::memory_order_relaxed), + rb.load_tail(std::memory_order_relaxed)); + } + + size_t free_num() const { + return _free_num(rb.load_head(std::memory_order_relaxed), + rb.load_tail(std::memory_order_relaxed)); + } +}; + +namespace { +/* Compute the write offset (with wrapping). Returns the num of free elements */ +template +size_t _write_off(size_t head, size_t tail, + size_t &n, size_t *wrap_n) { + if (tail < head) { + if (__predict_false(tail == 0)) { + n = capacity - head - 1; + if (wrap_n) { + *wrap_n = 0; + } + return capacity - head - 1; + } + + n = capacity - head; + if (wrap_n) { + *wrap_n = tail - 1; + } + return capacity - head + tail - 1; + } + + if (__predict_false(tail == head)) { + n = capacity - 1; + if (wrap_n) { + *wrap_n = 0; + } + return capacity - 1; + } + + n = tail - head - 1; + if (wrap_n) { + *wrap_n = 0; + } + return tail - head - 1; +} + +} + +template +class RingBufferWriter : public RingBufferAccessor { +public: + RingBufferWriter(RingBufferTy &rb) : RingBufferAccessor(rb) {} + + /* Return the offset for writing and set the number of writable elements to `n` + * and also to `wrap_n` if there is a wrap-around the end of the buffer */ + size_t write_off(size_t &n, size_t *wrap_n) { + const size_t head = this->rb.load_head(std::memory_order_acquire); + + if (_write_off(head, this->seen, n, wrap_n) == 0) { + this->seen = this->rb.load_tail(std::memory_order_relaxed); + _write_off(head, this->seen, n, wrap_n); + } + + return head; + } + + size_t write_off(size_t &n) { + const size_t head = this->rb.load_head(std::memory_order_acquire); + + /* Update the cache if seen_tail is 0 (which very likely means it has not + * been updated yet, but can be of course also after wrapping around) or if + * there is no space left considering the cached information. */ + if (this->seen == 0 || + _write_off(head, this->seen, n, NULL) == 0) { + this->seen = this->rb.load_tail(std::memory_order_relaxed); + _write_off(head, this->seen, n, NULL); + } + + return head; + } + + /* Ask for at least *n elements */ + size_t acquire(size_t &n, size_t *wrap_n) { + const size_t head = this->rb.load_head(std::memory_order_acquire); + + size_t req = n; + if (_write_off(head, this->seen, n, wrap_n) < req) { + this->seen = this->rb.load_tail(std::memory_order_relaxed); + _write_off(head, this->seen, n, wrap_n); + } + + return head; + } + + /* Ask for at least *n elements */ + size_t acquire(size_t &n) { + const size_t head = this->rb.load_head(std::memory_order_acquire); + + size_t req = n; + if (_write_off(head, this->seen, n, NULL) < req) { + this->seen = this->rb.load_tail(std::memory_order_relaxed); + _write_off(head, this->seen, n, NULL); + } + return head; + } + + void write_finish(size_t n) { + constexpr auto c = this->capacity(); + assert(n <= c); + assert((c < (~((size_t)0)) - n) && "Possible overflow"); + + size_t new_head = this->rb.load_head(std::memory_order_relaxed) + n; + + /* buffer full, rotate it */ + if (__predict_false(new_head >= c)) { + new_head -= c; + } + + assert(new_head != this->rb.load_tail(std::memory_order_relaxed) && + "Invalid head move"); + + this->rb.set_head(new_head, std::memory_order_release); + } + + +}; + +template +class RingBufferReader : public RingBufferAccessor { +public: + RingBufferReader(RingBufferTy &rb) : RingBufferAccessor(rb) {} + + size_t read_off(size_t &n) { + const size_t tail = this->rb.load_tail(std::memory_order_acquire); + size_t tmp = _written_num(this->seen, tail); + if (tmp == 0) { + this->seen = this->rb.load_head(std::memory_order_acquire); + tmp = _written_num(this->seen, tail); + } + + n = tmp; + + return tail; + } + + size_t acquire(size_t &n) { + const size_t tail = this->rb.load_tail(std::memory_order_acquire); + size_t tmp = _written_num(this->seen, tail); + if (tmp < n) { + this->seen = this->rb.load_head(std::memory_order_acquire); + tmp = _written_num(this->seen, tail); + } + + n = tmp; + + return tail; + } + + /* + * Consume n items from the ringbuffer. There must be at least n items. + */ + void consume(size_t n) { + assert(n > 0 && "Consume 0 elems"); + assert(n <= this->capacity()); + assert((this->capacity() < (~((size_t)0)) - n) && "Possible overflow"); + + size_t new_tail = this->rb.load_tail(std::memory_order_relaxed) + n; + if (__predict_false(new_tail >= this->capacity())) { + new_tail -= this->capacity(); + } + + this->rb.set_tail(new_tail, std::memory_order_release); + } + + + /* If return value is 0, values *off, *len1 and *len2 may not have been set */ + size_t peek(size_t n, size_t &off, size_t &len1, size_t &len2) { + const size_t tail = this->rb.load_tail(std::memory_order_acquire); + size_t head = this->seen; + size_t cur_elem_num = _written_num(head, tail); + /* update the information if needed or when n == 0 (which means we want to + * get an up-to-date the number of elements) */ + if (cur_elem_num < n || n == 0) { + this->seen = head = this->rb.load_head(std::memory_order_acquire); + cur_elem_num = _written_num(head, tail); + } + + if (n == 0) + return cur_elem_num; + + if (__predict_false(cur_elem_num == 0)) + return 0; + + if (cur_elem_num < n) { + n = cur_elem_num; + } + + assert(n <= cur_elem_num); + + if (tail < head) { + len1 = n; + len2 = 0; + } else { + assert(tail != head && "Ringbuf is empty"); + constexpr size_t c = this->capacity(); + const size_t l1 = c - tail > n ? n : c - tail; + len1 = l1; + len2 = head < (n - l1) ? head : n - l1; + } + + assert(len1 + len2 == n); + off = tail; + + return cur_elem_num; + } +}; + + +} // namespace vamos + +#endif From e9da89cb2457867b0ea21330a73197ecde2b8b9c Mon Sep 17 00:00:00 2001 From: Marek Chalupa Date: Fri, 21 Apr 2023 07:18:34 +0200 Subject: [PATCH 07/34] cpp/ring_buff: fix the implementation --- include/vamos-buffers/cpp/ring_buffer.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/vamos-buffers/cpp/ring_buffer.h b/include/vamos-buffers/cpp/ring_buffer.h index 6f294d9f..62954ba7 100644 --- a/include/vamos-buffers/cpp/ring_buffer.h +++ b/include/vamos-buffers/cpp/ring_buffer.h @@ -132,7 +132,7 @@ class RingBuffer { } void set_tail(size_t new_tail, std::memory_order mo) { - _head.store(new_tail, mo); + _tail.store(new_tail, mo); } bool is_full() const { @@ -148,7 +148,7 @@ class RingBufferAccessor { protected: RingBufferTy &rb; // the cache for the head/tail last seen by this thread - size_t seen; + size_t seen{0}; public: RingBufferAccessor(RingBufferTy &rb) : rb(rb) {} //, _capacity(rb.capacity()) {} @@ -276,7 +276,7 @@ class RingBufferWriter : public RingBufferAccessor { } void write_finish(size_t n) { - constexpr auto c = this->capacity(); + constexpr auto c = RingBufferTy::Capacity; assert(n <= c); assert((c < (~((size_t)0)) - n) && "Possible overflow"); From a5e68644cd43774d14d4296eb1031365a922ed01 Mon Sep 17 00:00:00 2001 From: Marek Chalupa Date: Fri, 21 Apr 2023 07:19:25 +0200 Subject: [PATCH 08/34] Fix format the new commits --- include/vamos-buffers/core/event.h | 4 +- include/vamos-buffers/cpp/event.h | 4 +- include/vamos-buffers/cpp/ring_buffer.h | 96 ++++++++++++------------- 3 files changed, 49 insertions(+), 55 deletions(-) diff --git a/include/vamos-buffers/core/event.h b/include/vamos-buffers/core/event.h index a6193560..120bf57e 100644 --- a/include/vamos-buffers/core/event.h +++ b/include/vamos-buffers/core/event.h @@ -21,11 +21,10 @@ typedef struct _vms_event { #ifdef __cplusplus _vms_event() {} - _vms_event(vms_kind kind, vms_eventid id): kind(kind), id(id) {} + _vms_event(vms_kind kind, vms_eventid id) : kind(kind), id(id) {} #endif } vms_event; - typedef struct _vms_event_default_hole { vms_event base; size_t n; /* number of dropped events */ @@ -46,7 +45,6 @@ bool vms_event_is_hole(const vms_event *); vms_kind vms_get_hole_kind(void); vms_kind vms_get_last_special_kind(void); - #ifdef __cplusplus } #endif diff --git a/include/vamos-buffers/cpp/event.h b/include/vamos-buffers/cpp/event.h index 6d03a0d2..82337020 100644 --- a/include/vamos-buffers/cpp/event.h +++ b/include/vamos-buffers/cpp/event.h @@ -7,13 +7,13 @@ namespace vamos { struct Event : public vms_event { Event() {} - Event(vms_kind kind, vms_eventid id): _vms_event(kind, id) {} + Event(vms_kind kind, vms_eventid id) : _vms_event(kind, id) {} vms_eventid id() const { return vms_event_id(this); } vms_kind kind() const { return vms_event_kind(this); }; bool is_hole() const { return vms_event_is_hole(this); } }; -} +} // namespace vamos #endif diff --git a/include/vamos-buffers/cpp/ring_buffer.h b/include/vamos-buffers/cpp/ring_buffer.h index 62954ba7..539645e7 100644 --- a/include/vamos-buffers/cpp/ring_buffer.h +++ b/include/vamos-buffers/cpp/ring_buffer.h @@ -102,30 +102,23 @@ size_t _free_num(size_t head, size_t tail) { return ret; } -} // namespace - +} // namespace template class RingBuffer { CACHELINE_ALIGNED std::atomic _tail; CACHELINE_ALIGNED std::atomic _head; -public: + public: static constexpr uint64_t Capacity = CAPACITY; constexpr uint64_t capacity() const { return CAPACITY; } - RingBuffer(): _tail(0), _head(0) { - static_assert(CAPACITY > 0); - } + RingBuffer() : _tail(0), _head(0) { static_assert(CAPACITY > 0); } - size_t load_head(std::memory_order mo) const { - return _head.load(mo); - } + size_t load_head(std::memory_order mo) const { return _head.load(mo); } - size_t load_tail(std::memory_order mo) const { - return _tail.load(mo); - } + size_t load_tail(std::memory_order mo) const { return _tail.load(mo); } void set_head(size_t new_head, std::memory_order mo) { _head.store(new_head, mo); @@ -136,8 +129,9 @@ class RingBuffer { } bool is_full() const { - return __predict_false(_is_full(load_head(std::memory_order_relaxed), - load_tail(std::memory_order_relaxed))); + return __predict_false( + _is_full(load_head(std::memory_order_relaxed), + load_tail(std::memory_order_relaxed))); } }; @@ -145,18 +139,16 @@ template class RingBufferAccessor { static constexpr uint64_t _capacity = RingBufferTy::Capacity; -protected: + protected: RingBufferTy &rb; // the cache for the head/tail last seen by this thread size_t seen{0}; -public: - RingBufferAccessor(RingBufferTy &rb) : rb(rb) {} //, _capacity(rb.capacity()) {} + public: + RingBufferAccessor(RingBufferTy &rb) + : rb(rb) {} //, _capacity(rb.capacity()) {} - - constexpr size_t capacity() const { - return _capacity; - } + constexpr size_t capacity() const { return _capacity; } size_t max_size() const { /* we use one element as a separator */ @@ -164,26 +156,28 @@ class RingBufferAccessor { } size_t size() const { - return _written_num(rb.load_head(std::memory_order_relaxed), - rb.load_tail(std::memory_order_relaxed)); + return _written_num( + rb.load_head(std::memory_order_relaxed), + rb.load_tail(std::memory_order_relaxed)); } bool empty() const { - return _is_empty(rb.load_head(std::memory_order_relaxed), - rb.load_tail(std::memory_order_relaxed)); + return _is_empty( + rb.load_head(std::memory_order_relaxed), + rb.load_tail(std::memory_order_relaxed)); } size_t free_num() const { - return _free_num(rb.load_head(std::memory_order_relaxed), - rb.load_tail(std::memory_order_relaxed)); + return _free_num( + rb.load_head(std::memory_order_relaxed), + rb.load_tail(std::memory_order_relaxed)); } }; namespace { /* Compute the write offset (with wrapping). Returns the num of free elements */ template -size_t _write_off(size_t head, size_t tail, - size_t &n, size_t *wrap_n) { +size_t _write_off(size_t head, size_t tail, size_t &n, size_t *wrap_n) { if (tail < head) { if (__predict_false(tail == 0)) { n = capacity - head - 1; @@ -215,19 +209,21 @@ size_t _write_off(size_t head, size_t tail, return tail - head - 1; } -} +} // namespace template class RingBufferWriter : public RingBufferAccessor { -public: + public: RingBufferWriter(RingBufferTy &rb) : RingBufferAccessor(rb) {} - /* Return the offset for writing and set the number of writable elements to `n` - * and also to `wrap_n` if there is a wrap-around the end of the buffer */ + /* Return the offset for writing and set the number of writable elements to + * `n` and also to `wrap_n` if there is a wrap-around the end of the buffer + */ size_t write_off(size_t &n, size_t *wrap_n) { const size_t head = this->rb.load_head(std::memory_order_acquire); - if (_write_off(head, this->seen, n, wrap_n) == 0) { + if (_write_off(head, this->seen, n, wrap_n) == + 0) { this->seen = this->rb.load_tail(std::memory_order_relaxed); _write_off(head, this->seen, n, wrap_n); } @@ -238,11 +234,12 @@ class RingBufferWriter : public RingBufferAccessor { size_t write_off(size_t &n) { const size_t head = this->rb.load_head(std::memory_order_acquire); - /* Update the cache if seen_tail is 0 (which very likely means it has not - * been updated yet, but can be of course also after wrapping around) or if - * there is no space left considering the cached information. */ - if (this->seen == 0 || - _write_off(head, this->seen, n, NULL) == 0) { + /* Update the cache if seen_tail is 0 (which very likely means it has + * not been updated yet, but can be of course also after wrapping + * around) or if there is no space left considering the cached + * information. */ + if (this->seen == 0 || _write_off( + head, this->seen, n, NULL) == 0) { this->seen = this->rb.load_tail(std::memory_order_relaxed); _write_off(head, this->seen, n, NULL); } @@ -255,7 +252,8 @@ class RingBufferWriter : public RingBufferAccessor { const size_t head = this->rb.load_head(std::memory_order_acquire); size_t req = n; - if (_write_off(head, this->seen, n, wrap_n) < req) { + if (_write_off(head, this->seen, n, wrap_n) < + req) { this->seen = this->rb.load_tail(std::memory_order_relaxed); _write_off(head, this->seen, n, wrap_n); } @@ -268,7 +266,8 @@ class RingBufferWriter : public RingBufferAccessor { const size_t head = this->rb.load_head(std::memory_order_acquire); size_t req = n; - if (_write_off(head, this->seen, n, NULL) < req) { + if (_write_off(head, this->seen, n, NULL) < + req) { this->seen = this->rb.load_tail(std::memory_order_relaxed); _write_off(head, this->seen, n, NULL); } @@ -292,13 +291,11 @@ class RingBufferWriter : public RingBufferAccessor { this->rb.set_head(new_head, std::memory_order_release); } - - }; template class RingBufferReader : public RingBufferAccessor { -public: + public: RingBufferReader(RingBufferTy &rb) : RingBufferAccessor(rb) {} size_t read_off(size_t &n) { @@ -343,14 +340,14 @@ class RingBufferReader : public RingBufferAccessor { this->rb.set_tail(new_tail, std::memory_order_release); } - - /* If return value is 0, values *off, *len1 and *len2 may not have been set */ + /* If return value is 0, values *off, *len1 and *len2 may not have been set + */ size_t peek(size_t n, size_t &off, size_t &len1, size_t &len2) { const size_t tail = this->rb.load_tail(std::memory_order_acquire); size_t head = this->seen; size_t cur_elem_num = _written_num(head, tail); - /* update the information if needed or when n == 0 (which means we want to - * get an up-to-date the number of elements) */ + /* update the information if needed or when n == 0 (which means we want + * to get an up-to-date the number of elements) */ if (cur_elem_num < n || n == 0) { this->seen = head = this->rb.load_head(std::memory_order_acquire); cur_elem_num = _written_num(head, tail); @@ -386,7 +383,6 @@ class RingBufferReader : public RingBufferAccessor { } }; - -} // namespace vamos +} // namespace vamos #endif From 2ca58d648af6274156e64608376e7c9ba013ede8 Mon Sep 17 00:00:00 2001 From: Marek Chalupa Date: Tue, 2 May 2023 15:09:37 +0200 Subject: [PATCH 09/34] cpp/ring_buffer: add Reader::available() method Return the number of elements that are available to read. --- include/vamos-buffers/cpp/ring_buffer.h | 5 +++++ src/core/CMakeLists.txt | 1 + 2 files changed, 6 insertions(+) diff --git a/include/vamos-buffers/cpp/ring_buffer.h b/include/vamos-buffers/cpp/ring_buffer.h index 539645e7..16906e4f 100644 --- a/include/vamos-buffers/cpp/ring_buffer.h +++ b/include/vamos-buffers/cpp/ring_buffer.h @@ -311,6 +311,11 @@ class RingBufferReader : public RingBufferAccessor { return tail; } + size_t available() const { + return _written_num(this->rb.load_head(std::memory_order_relaxed), + this->rb.load_tail(std::memory_order_relaxed)); + } + size_t acquire(size_t &n) { const size_t tail = this->rb.load_tail(std::memory_order_acquire); size_t tmp = _written_num(this->seen, tail); diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index d1c1c9ca..54b6a3a6 100755 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -16,6 +16,7 @@ add_library(vamos-buffers-monitor-buffer STATIC monitor.c) set_property(TARGET vamos-buffers-utils PROPERTY POSITION_INDEPENDENT_CODE 1) set_property(TARGET vamos-buffers-source PROPERTY POSITION_INDEPENDENT_CODE 1) set_property(TARGET vamos-buffers-arbiter PROPERTY POSITION_INDEPENDENT_CODE 1) +set_property(TARGET vamos-buffers-event PROPERTY POSITION_INDEPENDENT_CODE 1) set_property(TARGET vamos-buffers-signature PROPERTY POSITION_INDEPENDENT_CODE 1) set_property(TARGET vamos-buffers-stream PROPERTY POSITION_INDEPENDENT_CODE 1) set_property(TARGET vamos-buffers-shamon PROPERTY POSITION_INDEPENDENT_CODE 1) From 33903b3ef1f5503c2f282e39a0d637dba1eacd16 Mon Sep 17 00:00:00 2001 From: Marek Chalupa Date: Wed, 31 May 2023 21:33:28 +0200 Subject: [PATCH 10/34] event: add a new special kind and move special kids ids A new special kind of event `done` representing the end of stream was added. Special kinds max id was moved to 9 to have some space in the future. --- include/vamos-buffers/core/event.h | 1 + include/vamos-buffers/cpp/event.h | 1 + src/core/event.c | 16 +++++++++++++++- src/core/stream.c | 1 + src/shmbuf/buffer.c | 2 ++ 5 files changed, 20 insertions(+), 1 deletion(-) diff --git a/include/vamos-buffers/core/event.h b/include/vamos-buffers/core/event.h index 120bf57e..4ce099fa 100644 --- a/include/vamos-buffers/core/event.h +++ b/include/vamos-buffers/core/event.h @@ -42,6 +42,7 @@ vms_kind vms_event_kind(const vms_event *event); // DROP EVENT bool vms_event_is_hole(const vms_event *); +bool vms_event_is_done(const vms_event *); vms_kind vms_get_hole_kind(void); vms_kind vms_get_last_special_kind(void); diff --git a/include/vamos-buffers/cpp/event.h b/include/vamos-buffers/cpp/event.h index 82337020..14d79741 100644 --- a/include/vamos-buffers/cpp/event.h +++ b/include/vamos-buffers/cpp/event.h @@ -12,6 +12,7 @@ struct Event : public vms_event { vms_eventid id() const { return vms_event_id(this); } vms_kind kind() const { return vms_event_kind(this); }; bool is_hole() const { return vms_event_is_hole(this); } + bool is_done() const { return vms_event_is_done(this); } }; } // namespace vamos diff --git a/src/core/event.c b/src/core/event.c index 64be72e2..d3f3ec53 100644 --- a/src/core/event.c +++ b/src/core/event.c @@ -4,8 +4,11 @@ #include #include +/** The kind of `hole` event that represents a gap **/ static const vms_kind hole_kind = 1; -static const vms_kind last_special_kind = 1; +/** The kind of `done` event that represents the end of stream **/ +static const vms_kind done_kind = 2; +static const vms_kind last_special_kind = 9; void initialize_events(void) { assert(hole_kind == 1 && "We assume that the 'hole_kind' is 1 for now"); @@ -19,12 +22,23 @@ bool vms_event_is_hole(const vms_event *ev) { return vms_event_kind(ev) == hole_kind; } +bool vms_event_is_done(const vms_event *ev) { + assert(done_kind > 0); + return vms_event_kind(ev) == done_kind; +} + vms_kind vms_get_hole_kind(void) { assert(hole_kind > 0); assert(last_special_kind >= hole_kind); return hole_kind; } +vms_kind vms_get_done_kind(void) { + assert(done_kind > 0); + assert(last_special_kind >= done_kind); + return done_kind; +} + vms_kind vms_get_last_special_kind(void) { assert(last_special_kind > 0); return last_special_kind; diff --git a/src/core/stream.c b/src/core/stream.c index 6c96ae37..42148f54 100644 --- a/src/core/stream.c +++ b/src/core/stream.c @@ -295,6 +295,7 @@ size_t vms_stream_event_size(vms_stream *s) { return s->event_size; } int vms_stream_register_event(vms_stream *stream, const char *name, size_t kind) { + assert(kind > vms_get_last_special_kind() && "Invalid event kind, it overlaps special kinds"); return buffer_register_event(stream->incoming_events_buffer, name, kind); } diff --git a/src/shmbuf/buffer.c b/src/shmbuf/buffer.c index 5f21fab2..a3c71595 100644 --- a/src/shmbuf/buffer.c +++ b/src/shmbuf/buffer.c @@ -592,6 +592,7 @@ void buffer_notify_dropped(struct buffer *buff, uint64_t begin_id, } int buffer_register_event(struct buffer *b, const char *name, uint64_t kind) { + assert(kind > vms_get_last_special_kind() && "Invalid event kind, it overlaps special kinds"); struct event_record *rec = source_control_get_event(b->control, name); if (rec == NULL) { return -1; @@ -608,6 +609,7 @@ int buffer_register_events(struct buffer *b, size_t ev_nums, ...) { for (size_t i = 0; i < ev_nums; ++i) { const char *name = va_arg(ap, const char *); vms_kind kind = va_arg(ap, vms_kind); + assert(kind > vms_get_last_special_kind() && "Invalid event kind, it overlaps special kinds"); if (buffer_register_event(b, name, kind) < 0) { va_end(ap); return -1; From 32c26ec3780e89fea0c998e8506d76711ff64807 Mon Sep 17 00:00:00 2001 From: Marek Chalupa Date: Wed, 31 May 2023 22:25:56 +0200 Subject: [PATCH 11/34] event: fix naming of some functions --- include/vamos-buffers/core/event.h | 9 +++++++-- include/vamos-buffers/cpp/event.h | 20 ++++++++++++++++++++ src/core/event.c | 12 ++++++------ src/core/stream.c | 4 ++-- src/shmbuf/buffer.c | 6 +++--- tests/fetch-test-2.c | 2 +- tests/fetch-test-3.c | 2 +- tests/fetch-test.c | 2 +- 8 files changed, 41 insertions(+), 16 deletions(-) diff --git a/include/vamos-buffers/core/event.h b/include/vamos-buffers/core/event.h index 4ce099fa..4db9c553 100644 --- a/include/vamos-buffers/core/event.h +++ b/include/vamos-buffers/core/event.h @@ -37,14 +37,19 @@ void initialize_events(void) __attribute__((deprecated)); void deinitialize_events(void) __attribute__((deprecated)); // EVENTS +#define VMS_EVENT_HOLE_KIND 1 +#define VMS_EVENT_DONE_KIND 2 +#define VMS_EVENT_LAST_SPECIAL_KIND 9 + vms_eventid vms_event_id(const vms_event *event); vms_kind vms_event_kind(const vms_event *event); // DROP EVENT bool vms_event_is_hole(const vms_event *); bool vms_event_is_done(const vms_event *); -vms_kind vms_get_hole_kind(void); -vms_kind vms_get_last_special_kind(void); +vms_kind vms_event_get_hole_kind(void); +vms_kind vms_event_get_done_kind(void); +vms_kind vms_event_get_last_special_kind(void); #ifdef __cplusplus } diff --git a/include/vamos-buffers/cpp/event.h b/include/vamos-buffers/cpp/event.h index 14d79741..df1f53d5 100644 --- a/include/vamos-buffers/cpp/event.h +++ b/include/vamos-buffers/cpp/event.h @@ -13,6 +13,26 @@ struct Event : public vms_event { vms_kind kind() const { return vms_event_kind(this); }; bool is_hole() const { return vms_event_is_hole(this); } bool is_done() const { return vms_event_is_done(this); } + + static Event getDone(vms_eventid id) { + return Event(vms_event_get_done_kind(), id); + } + + static constexpr vms_kind holeKind() { + return VMS_EVENT_HOLE_KIND; + } + + static constexpr vms_kind doneKind() { + return VMS_EVENT_DONE_KIND; + } + + static constexpr vms_kind lastSpecialKind() { + return VMS_EVENT_LAST_SPECIAL_KIND; + } + + static constexpr vms_kind firstValidKind() { + return VMS_EVENT_LAST_SPECIAL_KIND + 1; + } }; } // namespace vamos diff --git a/src/core/event.c b/src/core/event.c index d3f3ec53..10c54cf7 100644 --- a/src/core/event.c +++ b/src/core/event.c @@ -5,10 +5,10 @@ #include /** The kind of `hole` event that represents a gap **/ -static const vms_kind hole_kind = 1; +static const vms_kind hole_kind = VMS_EVENT_HOLE_KIND; /** The kind of `done` event that represents the end of stream **/ -static const vms_kind done_kind = 2; -static const vms_kind last_special_kind = 9; +static const vms_kind done_kind = VMS_EVENT_DONE_KIND; +static const vms_kind last_special_kind = VMS_EVENT_LAST_SPECIAL_KIND; void initialize_events(void) { assert(hole_kind == 1 && "We assume that the 'hole_kind' is 1 for now"); @@ -27,19 +27,19 @@ bool vms_event_is_done(const vms_event *ev) { return vms_event_kind(ev) == done_kind; } -vms_kind vms_get_hole_kind(void) { +vms_kind vms_event_get_hole_kind(void) { assert(hole_kind > 0); assert(last_special_kind >= hole_kind); return hole_kind; } -vms_kind vms_get_done_kind(void) { +vms_kind vms_event_get_done_kind(void) { assert(done_kind > 0); assert(last_special_kind >= done_kind); return done_kind; } -vms_kind vms_get_last_special_kind(void) { +vms_kind vms_event_get_last_special_kind(void) { assert(last_special_kind > 0); return last_special_kind; } diff --git a/src/core/stream.c b/src/core/stream.c index 42148f54..2d22c660 100644 --- a/src/core/stream.c +++ b/src/core/stream.c @@ -16,7 +16,7 @@ /* FIXME: we duplicate the counter in stream here. Maybe rather set the funs to * NULL? */ static void default_hole_init(vms_event *ev) { - ev->kind = vms_get_hole_kind(); + ev->kind = vms_event_get_hole_kind(); ((vms_event_default_hole *)ev)->n = 0; } @@ -295,7 +295,7 @@ size_t vms_stream_event_size(vms_stream *s) { return s->event_size; } int vms_stream_register_event(vms_stream *stream, const char *name, size_t kind) { - assert(kind > vms_get_last_special_kind() && "Invalid event kind, it overlaps special kinds"); + assert(kind > vms_event_get_last_special_kind() && "Invalid event kind, it overlaps special kinds"); return buffer_register_event(stream->incoming_events_buffer, name, kind); } diff --git a/src/shmbuf/buffer.c b/src/shmbuf/buffer.c index a3c71595..c3f4c897 100644 --- a/src/shmbuf/buffer.c +++ b/src/shmbuf/buffer.c @@ -592,7 +592,7 @@ void buffer_notify_dropped(struct buffer *buff, uint64_t begin_id, } int buffer_register_event(struct buffer *b, const char *name, uint64_t kind) { - assert(kind > vms_get_last_special_kind() && "Invalid event kind, it overlaps special kinds"); + assert(kind > vms_event_get_last_special_kind() && "Invalid event kind, it overlaps special kinds"); struct event_record *rec = source_control_get_event(b->control, name); if (rec == NULL) { return -1; @@ -609,7 +609,7 @@ int buffer_register_events(struct buffer *b, size_t ev_nums, ...) { for (size_t i = 0; i < ev_nums; ++i) { const char *name = va_arg(ap, const char *); vms_kind kind = va_arg(ap, vms_kind); - assert(kind > vms_get_last_special_kind() && "Invalid event kind, it overlaps special kinds"); + assert(kind > vms_event_get_last_special_kind() && "Invalid event kind, it overlaps special kinds"); if (buffer_register_event(b, name, kind) < 0) { va_end(ap); return -1; @@ -626,7 +626,7 @@ int buffer_register_all_events(struct buffer *b) { const size_t ev_nums = source_control_get_records_num(b->control); for (size_t i = 0; i < ev_nums; ++i) { - recs[i].kind = 1 + i + vms_get_last_special_kind(); + recs[i].kind = 1 + i + vms_event_get_last_special_kind(); } return 0; diff --git a/tests/fetch-test-2.c b/tests/fetch-test-2.c index 655b78cd..df2c7ef8 100644 --- a/tests/fetch-test-2.c +++ b/tests/fetch-test-2.c @@ -28,7 +28,7 @@ static size_t failed_push = 0; void* filler_thread(void* data) { struct buffer* buffer = (struct buffer*)data; struct event ev; - ev.base.kind = vms_get_last_special_kind() + 1; + ev.base.kind = vms_event_get_last_special_kind() + 1; for (size_t i = 1; i <= 10000; ++i) { ev.base.id = i; ev.n = i; diff --git a/tests/fetch-test-3.c b/tests/fetch-test-3.c index 10992fac..72cb7c5a 100644 --- a/tests/fetch-test-3.c +++ b/tests/fetch-test-3.c @@ -28,7 +28,7 @@ static size_t failed_push = 0; void *filler_thread(void *data) { struct buffer *buffer = (struct buffer *)data; struct event ev; - ev.base.kind = vms_get_last_special_kind() + 1; + ev.base.kind = vms_event_get_last_special_kind() + 1; for (size_t i = 0; i < 10000; ++i) { ev.base.id = i + 1; ev.n = i; diff --git a/tests/fetch-test.c b/tests/fetch-test.c index 9f7eebba..10a97000 100644 --- a/tests/fetch-test.c +++ b/tests/fetch-test.c @@ -26,7 +26,7 @@ struct event { void *filler_thread(void *data) { struct buffer *buffer = (struct buffer *)data; struct event ev; - ev.base.kind = vms_get_last_special_kind() + 1; + ev.base.kind = vms_event_get_last_special_kind() + 1; for (int i = 0; i < 4; ++i) { ev.base.id = i + 1; ev.n = i; From c3b5d6e610738f13f1320103895f53378d3637b1 Mon Sep 17 00:00:00 2001 From: Marek Chalupa Date: Thu, 1 Jun 2023 12:56:43 +0200 Subject: [PATCH 12/34] cpp: add a draft of our vector --- include/vamos-buffers/cpp/vector.h | 168 +++++++++++++++++++++++++++++ 1 file changed, 168 insertions(+) create mode 100644 include/vamos-buffers/cpp/vector.h diff --git a/include/vamos-buffers/cpp/vector.h b/include/vamos-buffers/cpp/vector.h new file mode 100644 index 00000000..64f1b2a1 --- /dev/null +++ b/include/vamos-buffers/cpp/vector.h @@ -0,0 +1,168 @@ +#ifndef VAMOS_BUFFERS_CPP_VECTOR_H +#define VAMOS_BUFFERS_CPP_VECTOR_H + +#include + +#error "This class must be debugged and fixed, it does not work" + +namespace vamos { + +template +class vec { + ElemTy *_data{nullptr}; + size_t _size{0}; + size_t _alloc_size{0}; + + +public: + ~vec() { + free(_data); + } + + void swap(vec& rhs) { + auto tmp = rhs; + rhs = *this; + *this = rhs; + } + + size_t size() const { return _size; } + size_t empty() const { return _size == 0; } + size_t alloc_size() const { return _alloc_size; } + constexpr size_t elem_size() { return sizeof(ElemTy); } + + ElemTy *at(size_t idx) { + assert(idx < _size); + return _data + idx; + } + + ElemTy *get(size_t idx) { + if (idx < _size) + return _data + idx; + return nullptr; + } + + const ElemTy *get(size_t idx) const { + return const_cast(get(idx)); + } + + /** + * Return the pointer to the next free space in the vector and increase + * the number of elements in the vector by one. + * The returned pointer should be used to write the element into the vector, + * e.g.: + * + * int *ptr = VEC_EXTEND_N(vec, 4); + * *ptr = 1; + * + * If there is not enough space to write a single element, the memory is + * reallocated to hold `n` new elements. + */ + ElemTy *extend_n(size_t n) { + if (_size >= _alloc_size) { + _alloc_size += n; + _data = static_cast(realloc(_data, _alloc_size * elem_size())); + assert(_data != nullptr && "Memory re-allocation failed"); + } + assert((_size < _alloc_size) && "Vector too small"); + return _data[_size++]; + } + + ElemTy *extend() { + return extend_n(16); + } + + size_t push(const ElemTy &e) { + if (_size >= _alloc_size) { + _alloc_size += 10; + _data = static_cast(realloc(_data, _alloc_size * elem_size())); + assert(_data != nullptr && "Memory re-allocation failed"); + } + assert((_size < _alloc_size) && "Vector too small"); + _data[_size++] = e; + return _size; + } + + size_t push(const ElemTy *e) { + return push(*e); + } + + size_t push(ElemTy &&e) { + if (_size >= _alloc_size) { + _alloc_size += 10; + _data = static_cast(realloc(_data, _alloc_size * elem_size())); + assert(_data != nullptr && "Memory re-allocation failed"); + } + assert((_size < _alloc_size) && "Vector too small"); + _data[_size++] = std::move(e); + return _size; + } + + /** + * Remove the last element from the vector (decrease its size by 1) + */ + size_t pop() { return --_size; } + void clear() { _size = 0; } + + ElemTy *back() { return &_data[_size]; } + const ElemTy *back() const { return &_data[_size]; } + + template + class _iterator { + protected: + VecTy *v; + size_t pos; + static const size_t npos = ~static_cast(0); + + _iterator(VecTy& v0) : v(&v0), pos(v0._size == 0 ? npos : 0) {} + _iterator() : pos(npos) {} + _iterator(const _iterator&) = default; + + friend class vec; + + public: + _iterator &operator++() { + ++pos; + if (pos == v->_size) + pos = npos; + return *this; + } + + _iterator operator++(int) { + auto tmp = *this; + operator++(); + return tmp; + } + + bool operator==(const _iterator& rhs) const { + return pos == rhs.pos; + } + bool operator!=(const _iterator& rhs) const { return !operator==(rhs); } + }; + + struct iterator : public _iterator { + iterator(vec& v) : _iterator(v) {} + iterator() : _iterator() {} + iterator(const iterator&) = default; + + ElemTy& operator*() { return this->v->_data[this->pos]; } + ElemTy *operator->() { return &this->v->_data[this->pos]; } + }; + + struct const_iterator : public _iterator { + const_iterator(const vec& v) : _iterator(v) {} + const_iterator() : _iterator() {} + const_iterator(const const_iterator&) = default; + + const ElemTy& operator*() const { return this->v->_data[this->pos]; } + const ElemTy *operator->() const { return &this->v->_data[this->pos]; } + }; + + iterator begin() { return iterator(*this); } + iterator end() { return iterator(); } + const_iterator begin() const { return const_iterator(*this); } + const_iterator end() const { return const_iterator(); } +}; + +} // namespace vamos + +#endif From 9fc569489de1b086b1b866fa58faa9227281e118 Mon Sep 17 00:00:00 2001 From: Marek Chalupa Date: Wed, 21 Jun 2023 19:57:05 +0200 Subject: [PATCH 13/34] core/event: add a method to set ID of events If we generate events per partes, this method may be handy. --- include/vamos-buffers/core/event.h | 4 ++++ include/vamos-buffers/cpp/event.h | 1 + src/core/event.c | 4 ++++ 3 files changed, 9 insertions(+) diff --git a/include/vamos-buffers/core/event.h b/include/vamos-buffers/core/event.h index 4db9c553..639a2917 100644 --- a/include/vamos-buffers/core/event.h +++ b/include/vamos-buffers/core/event.h @@ -44,6 +44,10 @@ void deinitialize_events(void) __attribute__((deprecated)); vms_eventid vms_event_id(const vms_event *event); vms_kind vms_event_kind(const vms_event *event); +// Sometimes we want to generate the event step-by-step +// and then we may need this fun. +void vms_event_set_id(vms_event *event, vms_eventid id); + // DROP EVENT bool vms_event_is_hole(const vms_event *); bool vms_event_is_done(const vms_event *); diff --git a/include/vamos-buffers/cpp/event.h b/include/vamos-buffers/cpp/event.h index df1f53d5..d31b4d0e 100644 --- a/include/vamos-buffers/cpp/event.h +++ b/include/vamos-buffers/cpp/event.h @@ -13,6 +13,7 @@ struct Event : public vms_event { vms_kind kind() const { return vms_event_kind(this); }; bool is_hole() const { return vms_event_is_hole(this); } bool is_done() const { return vms_event_is_done(this); } + void set_id(vms_eventid id) { vms_event_set_id(this, id); } static Event getDone(vms_eventid id) { return Event(vms_event_get_done_kind(), id); diff --git a/src/core/event.c b/src/core/event.c index 10c54cf7..62ad9129 100644 --- a/src/core/event.c +++ b/src/core/event.c @@ -46,4 +46,8 @@ vms_kind vms_event_get_last_special_kind(void) { vms_eventid vms_event_id(const vms_event *event) { return event->id; } +void vms_event_set_id(vms_event *event, vms_eventid id) { + event->id = id; +} + vms_kind vms_event_kind(const vms_event *event) { return event->kind; } From 6df0ae292473430e55b6c6a84204706eece35922 Mon Sep 17 00:00:00 2001 From: Marek Chalupa Date: Tue, 4 Jul 2023 13:05:22 +0200 Subject: [PATCH 14/34] event: allow initialize partially It is useful for automatically generated code where creating an event can proceed in several steps part by part. --- include/vamos-buffers/core/event.h | 1 + include/vamos-buffers/cpp/event.h | 1 + 2 files changed, 2 insertions(+) diff --git a/include/vamos-buffers/core/event.h b/include/vamos-buffers/core/event.h index 639a2917..f46495e7 100644 --- a/include/vamos-buffers/core/event.h +++ b/include/vamos-buffers/core/event.h @@ -21,6 +21,7 @@ typedef struct _vms_event { #ifdef __cplusplus _vms_event() {} + _vms_event(vms_kind kind) : kind(kind) {} _vms_event(vms_kind kind, vms_eventid id) : kind(kind), id(id) {} #endif } vms_event; diff --git a/include/vamos-buffers/cpp/event.h b/include/vamos-buffers/cpp/event.h index d31b4d0e..abca6796 100644 --- a/include/vamos-buffers/cpp/event.h +++ b/include/vamos-buffers/cpp/event.h @@ -7,6 +7,7 @@ namespace vamos { struct Event : public vms_event { Event() {} + Event(vms_kind kind) : _vms_event(kind) {} Event(vms_kind kind, vms_eventid id) : _vms_event(kind, id) {} vms_eventid id() const { return vms_event_id(this); } From 538d7bc2b253cb4659584746b1e514a63ad9d97f Mon Sep 17 00:00:00 2001 From: Marek Chalupa Date: Thu, 27 Jul 2023 14:36:45 +0200 Subject: [PATCH 15/34] cpp/event: do not shadow the names of attributes with methods --- include/vamos-buffers/core/event.h | 7 +++++-- include/vamos-buffers/cpp/event.h | 2 -- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/include/vamos-buffers/core/event.h b/include/vamos-buffers/core/event.h index f46495e7..7f5b7c0a 100644 --- a/include/vamos-buffers/core/event.h +++ b/include/vamos-buffers/core/event.h @@ -23,6 +23,9 @@ typedef struct _vms_event { _vms_event() {} _vms_event(vms_kind kind) : kind(kind) {} _vms_event(vms_kind kind, vms_eventid id) : kind(kind), id(id) {} + + vms_kind get_kind() const { return kind; } + vms_eventid get_id() const { return id; } #endif } vms_event; @@ -42,8 +45,8 @@ void deinitialize_events(void) __attribute__((deprecated)); #define VMS_EVENT_DONE_KIND 2 #define VMS_EVENT_LAST_SPECIAL_KIND 9 -vms_eventid vms_event_id(const vms_event *event); -vms_kind vms_event_kind(const vms_event *event); +vms_eventid vms_event_id(const vms_event *event) __attribute__((always_inline)); +vms_kind vms_event_kind(const vms_event *event) __attribute__((always_inline)); // Sometimes we want to generate the event step-by-step // and then we may need this fun. diff --git a/include/vamos-buffers/cpp/event.h b/include/vamos-buffers/cpp/event.h index abca6796..b35b9f89 100644 --- a/include/vamos-buffers/cpp/event.h +++ b/include/vamos-buffers/cpp/event.h @@ -10,8 +10,6 @@ struct Event : public vms_event { Event(vms_kind kind) : _vms_event(kind) {} Event(vms_kind kind, vms_eventid id) : _vms_event(kind, id) {} - vms_eventid id() const { return vms_event_id(this); } - vms_kind kind() const { return vms_event_kind(this); }; bool is_hole() const { return vms_event_is_hole(this); } bool is_done() const { return vms_event_is_done(this); } void set_id(vms_eventid id) { vms_event_set_id(this, id); } From 32ad604bcc3654d27b9008564fe05307c3e8803d Mon Sep 17 00:00:00 2001 From: Marek Chalupa Date: Tue, 8 Aug 2023 16:14:53 +0200 Subject: [PATCH 16/34] Fix GCC build GCC is not able always inline this function and breaks the build. --- include/vamos-buffers/core/event.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/vamos-buffers/core/event.h b/include/vamos-buffers/core/event.h index 7f5b7c0a..035c8f6a 100644 --- a/include/vamos-buffers/core/event.h +++ b/include/vamos-buffers/core/event.h @@ -45,8 +45,8 @@ void deinitialize_events(void) __attribute__((deprecated)); #define VMS_EVENT_DONE_KIND 2 #define VMS_EVENT_LAST_SPECIAL_KIND 9 -vms_eventid vms_event_id(const vms_event *event) __attribute__((always_inline)); -vms_kind vms_event_kind(const vms_event *event) __attribute__((always_inline)); +vms_eventid vms_event_id(const vms_event *event); +vms_kind vms_event_kind(const vms_event *event); // Sometimes we want to generate the event step-by-step // and then we may need this fun. From 30ff4f2400d1f9ddde667c2407e2d23fbb02bbd7 Mon Sep 17 00:00:00 2001 From: Marek Chalupa Date: Thu, 12 Oct 2023 14:18:00 +0000 Subject: [PATCH 17/34] Fix vms_open -> vms_shm_open --- src/shmbuf/buffer-aux.c | 4 ++-- src/shmbuf/buffer-control.c | 4 ++-- src/shmbuf/buffer-dbg.c | 4 ++-- src/shmbuf/buffer.c | 4 ++-- src/shmbuf/shm.c | 2 +- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/shmbuf/buffer-aux.c b/src/shmbuf/buffer-aux.c index 50a6f77d..b343690e 100644 --- a/src/shmbuf/buffer-aux.c +++ b/src/shmbuf/buffer-aux.c @@ -66,7 +66,7 @@ static struct aux_buffer *new_aux_buffer(struct buffer *buff, size_t size) { int fd = vms_shm_open(key, O_RDWR | O_CREAT, buff->mode); if (fd < 0) { - perror("vms_open"); + perror("vms_shm_open"); abort(); } @@ -183,7 +183,7 @@ struct aux_buffer *reader_get_aux_buffer(struct buffer *buff, size_t idx) { int fd = vms_shm_open(key, O_RDWR, S_IRWXU); if (fd < 0) { - perror("vms_open"); + perror("vms_shm_open"); abort(); } diff --git a/src/shmbuf/buffer-control.c b/src/shmbuf/buffer-control.c index b18f60ce..d782dd07 100644 --- a/src/shmbuf/buffer-control.c +++ b/src/shmbuf/buffer-control.c @@ -14,7 +14,7 @@ struct source_control *get_shared_control_buffer(const char *buff_key) { /* fprintf(stderr, "getting control buffer '%s'\n", key); */ int fd = vms_shm_open(key, O_RDWR | O_CREAT, S_IRWXU); if (fd < 0) { - perror("vms_open"); + perror("vms_shm_open"); return NULL; } @@ -59,7 +59,7 @@ struct source_control *create_shared_control_buffer( int fd = vms_shm_open(tmpkey, O_RDWR | O_CREAT, mode); if (fd < 0) { - perror("vms_open"); + perror("vms_shm_open"); return NULL; } diff --git a/src/shmbuf/buffer-dbg.c b/src/shmbuf/buffer-dbg.c index 00288baa..d2ccb9ec 100644 --- a/src/shmbuf/buffer-dbg.c +++ b/src/shmbuf/buffer-dbg.c @@ -65,7 +65,7 @@ vms_vms_dbg_buffer *vms_vms_dbg_buffer_create(const char *key, size_t capacity, int fd = vms_shm_open(tmpkey, O_RDWR | O_CREAT, S_IRWXU); if (fd < 0) { - perror("vms_open"); + perror("vms_shm_open"); return NULL; } @@ -124,7 +124,7 @@ vms_vms_dbg_buffer *vms_vms_dbg_buffer_get(const char *key) { int fd = vms_shm_open(key, O_RDWR | O_CREAT, S_IRWXU); if (fd < 0) { - perror("vms_open"); + perror("vms_shm_open"); return NULL; } diff --git a/src/shmbuf/buffer.c b/src/shmbuf/buffer.c index c3f4c897..c8e55ac7 100644 --- a/src/shmbuf/buffer.c +++ b/src/shmbuf/buffer.c @@ -111,7 +111,7 @@ struct buffer *initialize_shared_buffer(const char *key, mode_t mode, int fd = vms_shm_open(tmpkey, O_RDWR | O_CREAT | O_TRUNC, mode); if (fd < 0) { - perror("vms_open"); + perror("vms_shm_open"); return NULL; } @@ -241,7 +241,7 @@ struct buffer *try_get_shared_buffer(const char *key, size_t retry) { } while (--retry > 0); if (fd == -1) { - perror("vms_open"); + perror("vms_shm_open"); fprintf(stderr, "Failed getting shared buffer '%s'\n", key); return NULL; } diff --git a/src/shmbuf/shm.c b/src/shmbuf/shm.c index f1308d93..30382bd1 100644 --- a/src/shmbuf/shm.c +++ b/src/shmbuf/shm.c @@ -14,7 +14,7 @@ const char *SHM_DIR = "/dev/shm/"; const size_t SHM_DIRlen = 9; -/* adapted function from musl project, src/mman/vms_open.c +/* adapted function from musl project, src/mman/shm_open.c * and from libc sysdeps/posix/shm-directory.h */ char *vms_shm_mapname(const char *name, char *buf) { assert(name[0] == '/'); From 4c5901fbaa6392b065241a3847a6ff320332a29a Mon Sep 17 00:00:00 2001 From: Marek Chalupa Date: Tue, 17 Oct 2023 19:31:15 +0200 Subject: [PATCH 18/34] Change SHAMON -> VAMOS An artifact after renaming. --- include/vamos-buffers/core/arbiter.h | 6 +++--- include/vamos-buffers/core/list-embedded.h | 6 +++--- include/vamos-buffers/core/list.h | 6 +++--- include/vamos-buffers/core/monitor.h | 6 +++--- include/vamos-buffers/core/par_queue.h | 6 +++--- include/vamos-buffers/core/queue_spsc.h | 6 +++--- include/vamos-buffers/core/shamon.h | 6 +++--- include/vamos-buffers/core/signatures.h | 4 ++-- include/vamos-buffers/core/source.h | 6 +++--- include/vamos-buffers/core/spsc_ringbuf.h | 6 +++--- include/vamos-buffers/core/stream.h | 6 +++--- include/vamos-buffers/core/utils.h | 6 +++--- include/vamos-buffers/core/vector-aligned.h | 6 +++--- include/vamos-buffers/core/vector-macro.h | 6 +++--- include/vamos-buffers/core/vector.h | 6 +++--- include/vamos-buffers/core/vms_string-macro.h | 6 +++--- include/vamos-buffers/core/vms_string.h | 6 +++--- include/vamos-buffers/shmbuf/buffer.h | 6 +++--- include/vamos-buffers/shmbuf/client.h | 4 ++-- include/vamos-buffers/streams/streams.h | 6 +++--- install.sh | 8 ++++---- src/core/legacy/queue.h | 6 +++--- src/shmbuf/buffer-private.h | 6 +++--- src/shmbuf/shm.h | 6 +++--- 24 files changed, 71 insertions(+), 71 deletions(-) diff --git a/include/vamos-buffers/core/arbiter.h b/include/vamos-buffers/core/arbiter.h index cfc2d054..8d0eba0d 100644 --- a/include/vamos-buffers/core/arbiter.h +++ b/include/vamos-buffers/core/arbiter.h @@ -8,8 +8,8 @@ * at the same time is not thread-safe. ************************************************/ -#ifndef SHAMON_ARBITER_H_ -#define SHAMON_ARBITER_H_ +#ifndef VAMOS_ARBITER_H_ +#define VAMOS_ARBITER_H_ #include #include @@ -77,4 +77,4 @@ void vms_arbiter_buffer_notify_dropped(vms_arbiter_buffer *buffer, uint64_t begin_id, uint64_t end_id); void vms_arbiter_buffer_dump_stats(vms_arbiter_buffer *buffer); -#endif /* SHAMON_ARBITER_H_ */ +#endif /* VAMOS_ARBITER_H_ */ diff --git a/include/vamos-buffers/core/list-embedded.h b/include/vamos-buffers/core/list-embedded.h index f8cd044a..ed7038fd 100644 --- a/include/vamos-buffers/core/list-embedded.h +++ b/include/vamos-buffers/core/list-embedded.h @@ -1,5 +1,5 @@ -#ifndef SHAMON_LIST_EMBEDDED_H_ -#define SHAMON_LIST_EMBEDDED_H_ +#ifndef VAMOS_LIST_EMBEDDED_H_ +#define VAMOS_LIST_EMBEDDED_H_ #include @@ -43,4 +43,4 @@ _Bool vms_list_embedded_empty(const vms_list_embedded *list); &pos->member != (head); pos = tmp, \ tmp = vms_list_embedded_object_of(pos->member.prev, tmp, member)) -#endif /* SHAMON_LIST_EMBEDDED_H_ */ +#endif /* VAMOS_LIST_EMBEDDED_H_ */ diff --git a/include/vamos-buffers/core/list.h b/include/vamos-buffers/core/list.h index dae5ad06..02e4a47f 100644 --- a/include/vamos-buffers/core/list.h +++ b/include/vamos-buffers/core/list.h @@ -1,5 +1,5 @@ -#ifndef SHAMON_LIST_H_ -#define SHAMON_LIST_H_ +#ifndef VAMOS_LIST_H_ +#define VAMOS_LIST_H_ #include @@ -33,4 +33,4 @@ size_t vms_list_size(vms_list *list); vms_list_elem *vms_list_first(vms_list *list); vms_list_elem *vms_list_last(vms_list *list); -#endif /* SHAMON_LIST_H_ */ +#endif /* VAMOS_LIST_H_ */ diff --git a/include/vamos-buffers/core/monitor.h b/include/vamos-buffers/core/monitor.h index 4a6bfdec..8657c4fb 100644 --- a/include/vamos-buffers/core/monitor.h +++ b/include/vamos-buffers/core/monitor.h @@ -2,8 +2,8 @@ * Implementation of the buffer for the monitor. ************************************************/ -#ifndef SHAMON_MONITOR_H_ -#define SHAMON_MONITOR_H_ +#ifndef VAMOS_MONITOR_H_ +#define VAMOS_MONITOR_H_ #include #include @@ -63,4 +63,4 @@ void vms_monitor_buffer_notify_dropped(vms_monitor_buffer *buffer, uint64_t begin_id, uint64_t end_id); */ -#endif /* SHAMON_MONITOR_H_ */ +#endif /* VAMOS_MONITOR_H_ */ diff --git a/include/vamos-buffers/core/par_queue.h b/include/vamos-buffers/core/par_queue.h index c71b34e5..4fac2c23 100644 --- a/include/vamos-buffers/core/par_queue.h +++ b/include/vamos-buffers/core/par_queue.h @@ -1,5 +1,5 @@ -#ifndef SHAMON_PARALLEL_QUEUE_H -#define SHAMON_PARALLEL_QUEUE_H +#ifndef VAMOS_PARALLEL_QUEUE_H +#define VAMOS_PARALLEL_QUEUE_H #include #include @@ -48,4 +48,4 @@ vms_event *vms_par_queue_peek_atmost_at(vms_par_queue *q, size_t *k); void *vms_par_queue_write_ptr(vms_par_queue *q); void vms_par_queue_write_finish(vms_par_queue *q); -#endif /* SHAMON_PARALLEL_QUEUE_H */ +#endif /* VAMOS_PARALLEL_QUEUE_H */ diff --git a/include/vamos-buffers/core/queue_spsc.h b/include/vamos-buffers/core/queue_spsc.h index 804e4f71..40f5fe5c 100644 --- a/include/vamos-buffers/core/queue_spsc.h +++ b/include/vamos-buffers/core/queue_spsc.h @@ -1,5 +1,5 @@ -#ifndef SHAMON_QUEUE_SPSC_H -#define SHAMON_QUEUE_SPSC_H +#ifndef VAMOS_QUEUE_SPSC_H +#define VAMOS_QUEUE_SPSC_H #include #include @@ -69,4 +69,4 @@ size_t vms_queue_spsc_peek_at(vms_queue_spsc *q, size_t k); size_t vms_queue_spsc_peek_atmost_at(vms_queue_spsc *q, size_t *k, size_t *offset); -#endif /* SHAMON_QUEUE_SPSC_H */ +#endif /* VAMOS_QUEUE_SPSC_H */ diff --git a/include/vamos-buffers/core/shamon.h b/include/vamos-buffers/core/shamon.h index 94e26b17..3d38d5e7 100644 --- a/include/vamos-buffers/core/shamon.h +++ b/include/vamos-buffers/core/shamon.h @@ -1,5 +1,5 @@ -#ifndef SHAMON_H_ -#define SHAMON_H_ +#ifndef VAMOS_H_ +#define VAMOS_H_ #include #include @@ -25,4 +25,4 @@ vms_event *shamon_get_next_ev(shamon *, vms_stream **); vms_vector *shamon_get_buffers(shamon *); vms_stream **shamon_get_streams(shamon *, size_t *); -#endif // SHAMON_H_ +#endif // VAMOS_H_ diff --git a/include/vamos-buffers/core/signatures.h b/include/vamos-buffers/core/signatures.h index 343e127e..b01e7b89 100644 --- a/include/vamos-buffers/core/signatures.h +++ b/include/vamos-buffers/core/signatures.h @@ -1,5 +1,5 @@ -#ifndef SHAMON_SIGNATURES_H_ -#define SHAMON_SIGNATURES_H_ +#ifndef VAMOS_SIGNATURES_H_ +#define VAMOS_SIGNATURES_H_ #include #include diff --git a/include/vamos-buffers/core/source.h b/include/vamos-buffers/core/source.h index 32af5544..fc65b98b 100644 --- a/include/vamos-buffers/core/source.h +++ b/include/vamos-buffers/core/source.h @@ -1,5 +1,5 @@ -#ifndef SHAMON_SOURCE_H_ -#define SHAMON_SOURCE_H_ +#ifndef VAMOS_SOURCE_H_ +#define VAMOS_SOURCE_H_ #include "event.h" @@ -39,4 +39,4 @@ _Bool source_control_define_pairwise_partially(struct source_control *control, _Bool source_control_define_partially(struct source_control *control, size_t from, size_t ev_nums, ...); -#endif /* SHAMON_SOURCE_H_ */ +#endif /* VAMOS_SOURCE_H_ */ diff --git a/include/vamos-buffers/core/spsc_ringbuf.h b/include/vamos-buffers/core/spsc_ringbuf.h index 402513e6..ed4f933e 100644 --- a/include/vamos-buffers/core/spsc_ringbuf.h +++ b/include/vamos-buffers/core/spsc_ringbuf.h @@ -1,5 +1,5 @@ -#ifndef SHAMON_SPSC_RINGBUF_H -#define SHAMON_SPSC_RINGBUF_H +#ifndef VAMOS_SPSC_RINGBUF_H +#define VAMOS_SPSC_RINGBUF_H #include #include @@ -61,4 +61,4 @@ size_t vms_spsc_ringbuf_read_acquire(vms_spsc_ringbuf *b, size_t *n); void *vms_spsc_ringbuf_top(vms_spsc_ringbuf *b); size_t vms_spsc_ringbuf_peek(vms_spsc_ringbuf *b, size_t n, size_t *off, size_t *len1, size_t *len2); -#endif /* SHAMON_SPSC_RINGBUF_H */ +#endif /* VAMOS_SPSC_RINGBUF_H */ diff --git a/include/vamos-buffers/core/stream.h b/include/vamos-buffers/core/stream.h index 8be81e34..21cc9b3b 100644 --- a/include/vamos-buffers/core/stream.h +++ b/include/vamos-buffers/core/stream.h @@ -1,5 +1,5 @@ -#ifndef SHAMON_STREAMS_H -#define SHAMON_STREAMS_H +#ifndef VAMOS_STREAMS_H +#define VAMOS_STREAMS_H #include "event.h" #include "source.h" @@ -121,4 +121,4 @@ inline size_t vms_stream_last_event_id(vms_stream *s) { #endif void vms_stream_dump_events(vms_stream *stream); -#endif // SHAMON_STREAMS_H +#endif // VAMOS_STREAMS_H diff --git a/include/vamos-buffers/core/utils.h b/include/vamos-buffers/core/utils.h index 8ee05927..5cc6e62c 100644 --- a/include/vamos-buffers/core/utils.h +++ b/include/vamos-buffers/core/utils.h @@ -1,5 +1,5 @@ -#ifndef SHAMON_UTILS_H_ -#define SHAMON_UTILS_H_ +#ifndef VAMOS_UTILS_H_ +#define VAMOS_UTILS_H_ #include /* size_t */ #include @@ -20,4 +20,4 @@ void *xalloc_aligned(size_t size, size_t alignment); char *xstrdup(const char *str); -#endif /* SHAMON_UTILS_H_ */ +#endif /* VAMOS_UTILS_H_ */ diff --git a/include/vamos-buffers/core/vector-aligned.h b/include/vamos-buffers/core/vector-aligned.h index f082518c..12720eb8 100644 --- a/include/vamos-buffers/core/vector-aligned.h +++ b/include/vamos-buffers/core/vector-aligned.h @@ -1,5 +1,5 @@ -#ifndef SHAMON_VECTOR_ALIGNED_H_ -#define SHAMON_VECTOR_ALIGNED_H_ +#ifndef VAMOS_VECTOR_ALIGNED_H_ +#define VAMOS_VECTOR_ALIGNED_H_ #include @@ -20,4 +20,4 @@ void vms_vector_aligned_resize(vms_vector *vec, size_t size); /* the rest of functions is inherited from vms_vector, use those */ -#endif /* SHAMON_VECTOR_ALIGNED_H_ */ +#endif /* VAMOS_VECTOR_ALIGNED_H_ */ diff --git a/include/vamos-buffers/core/vector-macro.h b/include/vamos-buffers/core/vector-macro.h index 3ccaa72a..ff6ee2f1 100644 --- a/include/vamos-buffers/core/vector-macro.h +++ b/include/vamos-buffers/core/vector-macro.h @@ -1,5 +1,5 @@ -#ifndef SHAMON_VECTOR_MACRO_H_ -#define SHAMON_VECTOR_MACRO_H_ +#ifndef VAMOS_VECTOR_MACRO_H_ +#define VAMOS_VECTOR_MACRO_H_ /** * Macro for creating inlined vector, that is, a piece of continuous memory @@ -175,4 +175,4 @@ */ #define VEC_POP_TOP(vec) (--VEC_SIZE(vec), *((vec) + VEC_SIZE(vec))) -#endif /* SHAMON_VECTOR_MACRO_H_ */ +#endif /* VAMOS_VECTOR_MACRO_H_ */ diff --git a/include/vamos-buffers/core/vector.h b/include/vamos-buffers/core/vector.h index 7d900120..426da4fe 100644 --- a/include/vamos-buffers/core/vector.h +++ b/include/vamos-buffers/core/vector.h @@ -1,5 +1,5 @@ -#ifndef SHAMON_VECTOR_H_ -#define SHAMON_VECTOR_H_ +#ifndef VAMOS_VECTOR_H_ +#define VAMOS_VECTOR_H_ /** * Implementation of a vector similar to std::vector in C++. @@ -89,4 +89,4 @@ void vms_vector_swap(vms_vector *vec, vms_vector *with); */ void vms_vector_resize(vms_vector *vec, size_t size); -#endif /* SHAMON_VECTOR_H_ */ +#endif /* VAMOS_VECTOR_H_ */ diff --git a/include/vamos-buffers/core/vms_string-macro.h b/include/vamos-buffers/core/vms_string-macro.h index 92becf9a..faab4cd2 100644 --- a/include/vamos-buffers/core/vms_string-macro.h +++ b/include/vamos-buffers/core/vms_string-macro.h @@ -1,5 +1,5 @@ -#ifndef SHAMON_STRING_MACRO_H_ -#define SHAMON_STRING_MACRO_H_ +#ifndef VAMOS_STRING_MACRO_H_ +#define VAMOS_STRING_MACRO_H_ #include "vector-macro.h" @@ -57,4 +57,4 @@ #define STRING_TOP_PTR(s) ((s) + STRING_SIZE(s) - 1) #define STRING_TOP(s) (*STRING_TOP_PTR(s)) -#endif /* SHAMON_STRING_MACRO_H_ */ +#endif /* VAMOS_STRING_MACRO_H_ */ diff --git a/include/vamos-buffers/core/vms_string.h b/include/vamos-buffers/core/vms_string.h index 6c7b495d..45e2775e 100644 --- a/include/vamos-buffers/core/vms_string.h +++ b/include/vamos-buffers/core/vms_string.h @@ -1,5 +1,5 @@ -#ifndef SHAMON_STRING_H_ -#define SHAMON_STRING_H_ +#ifndef VAMOS_STRING_H_ +#define VAMOS_STRING_H_ #include @@ -33,4 +33,4 @@ char vms_string_at(vms_string *s, ssize_t idx); void vms_string_swap(vms_string *s, vms_string *with); void vms_string_grow(vms_string *s, size_t size); -#endif /* SHAMON_VECTOR_H_ */ +#endif /* VAMOS_VECTOR_H_ */ diff --git a/include/vamos-buffers/shmbuf/buffer.h b/include/vamos-buffers/shmbuf/buffer.h index 1943a24d..60f63cb1 100644 --- a/include/vamos-buffers/shmbuf/buffer.h +++ b/include/vamos-buffers/shmbuf/buffer.h @@ -1,5 +1,5 @@ -#ifndef SHAMON_SHM_BUFFER_H -#define SHAMON_SHM_BUFFER_H +#ifndef VAMOS_SHM_BUFFER_H +#define VAMOS_SHM_BUFFER_H #include #include @@ -98,4 +98,4 @@ void vms_vms_dbg_buffer_inc_size(vms_vms_dbg_buffer *b, size_t size); size_t vms_vms_dbg_buffer_version(vms_vms_dbg_buffer *b); void vms_vms_dbg_buffer_bump_version(vms_vms_dbg_buffer *b); -#endif /* SHAMON_SHM_BUFFER_H */ +#endif /* VAMOS_SHM_BUFFER_H */ diff --git a/include/vamos-buffers/shmbuf/client.h b/include/vamos-buffers/shmbuf/client.h index 2a007237..75dbdee1 100644 --- a/include/vamos-buffers/shmbuf/client.h +++ b/include/vamos-buffers/shmbuf/client.h @@ -1,5 +1,5 @@ -#ifndef SHAMON_CLIENT_H -#define SHAMON_CLIENT_H +#ifndef VAMOS_CLIENT_H +#define VAMOS_CLIENT_H struct buffer; diff --git a/include/vamos-buffers/streams/streams.h b/include/vamos-buffers/streams/streams.h index 3a6de074..2c75a32e 100644 --- a/include/vamos-buffers/streams/streams.h +++ b/include/vamos-buffers/streams/streams.h @@ -1,5 +1,5 @@ -#ifndef SHAMON_STREAMS_H_ -#define SHAMON_STREAMS_H_ +#ifndef VAMOS_STREAMS_H_ +#define VAMOS_STREAMS_H_ #include "vamos-buffers/core/stream.h" @@ -9,4 +9,4 @@ vms_stream *vms_stream_create_from_argv( vms_stream *vms_stream_create(const char *stream_name, const char *spec, const vms_stream_hole_handling *hole_handling); -#endif /* SHAMON_STREAMS_H_ */ +#endif /* VAMOS_STREAMS_H_ */ diff --git a/install.sh b/install.sh index ef7e0e4e..b6e550f5 100755 --- a/install.sh +++ b/install.sh @@ -1,10 +1,10 @@ #!/usr/bin/env bash -SHAMONDIR=$(readlink -f $(dirname $0)) -echo $SHAMONDIR +VAMOSDIR=$(readlink -f $(dirname $0)) +echo $VAMOSDIR for FILE in bin/*; do - echo $SHAMONDIR/$FILE; - sed "s|\$BASEPATH/shamon|$SHAMONDIR|" $FILE > tmpfile + echo $VAMOSDIR/$FILE; + sed "s|\$BASEPATH/shamon|$VAMOSDIR|" $FILE > tmpfile sudo chmod +xxx tmpfile sudo mv tmpfile /usr/$FILE done diff --git a/src/core/legacy/queue.h b/src/core/legacy/queue.h index 2782a7ed..a89caa07 100644 --- a/src/core/legacy/queue.h +++ b/src/core/legacy/queue.h @@ -1,5 +1,5 @@ -#ifndef SHAMON_QUEUE_H -#define SHAMON_QUEUE_H +#ifndef VAMOS_QUEUE_H +#define VAMOS_QUEUE_H #include #include @@ -24,4 +24,4 @@ size_t vms_queue_max_size(vms_queue *q); // The returned memory can be filled manually. void *vms_queue_extend(vms_queue *q); -#endif // SHAMON_QUEUE_H +#endif // VAMOS_QUEUE_H diff --git a/src/shmbuf/buffer-private.h b/src/shmbuf/buffer-private.h index 4c667626..48f8afd0 100644 --- a/src/shmbuf/buffer-private.h +++ b/src/shmbuf/buffer-private.h @@ -1,5 +1,5 @@ -#ifndef SHAMON_SHM_BUFFER_PRIVATE_H -#define SHAMON_SHM_BUFFER_PRIVATE_H +#ifndef VAMOS_SHM_BUFFER_PRIVATE_H +#define VAMOS_SHM_BUFFER_PRIVATE_H #include #include @@ -121,4 +121,4 @@ struct aux_buffer *reader_get_aux_buffer(struct buffer *buff, size_t idx); void drop_ranges_lock(struct buffer *buff); void drop_ranges_unlock(struct buffer *buff); -#endif /* SHAMON_SHM_BUFFER_PRIVATE_H */ +#endif /* VAMOS_SHM_BUFFER_PRIVATE_H */ diff --git a/src/shmbuf/shm.h b/src/shmbuf/shm.h index b62fbfcf..5958df5c 100644 --- a/src/shmbuf/shm.h +++ b/src/shmbuf/shm.h @@ -1,5 +1,5 @@ -#ifndef SHAMON_SHM_H -#define SHAMON_SHM_H +#ifndef VAMOS_SHM_H +#define VAMOS_SHM_H #include #include @@ -17,4 +17,4 @@ char *vms_shm_mapname(const char *name, char *buf); int vms_shm_get_tmp_key(const char *key, char *buf, size_t bufsize); char *vms_shm_map_ctrl_key(const char *key, char name[SHM_NAME_MAXLEN]); -#endif /* SHAMON_SHM_H */ +#endif /* VAMOS_SHM_H */ From 55181d4dc93f6257a6bd50041b84aa9ee7623444 Mon Sep 17 00:00:00 2001 From: Marek Chalupa Date: Tue, 17 Oct 2023 19:36:48 +0200 Subject: [PATCH 19/34] rename source_control -> vms_source_control --- include/vamos-buffers/core/source.h | 32 +++++++++---------- include/vamos-buffers/shmbuf/buffer.h | 15 ++++----- src/core/source.c | 45 +++++++++++++-------------- src/shmbuf/buffer-control.c | 15 ++++----- src/shmbuf/buffer-local.c | 2 +- src/shmbuf/buffer-private.h | 19 +++++------ src/shmbuf/buffer-sub.c | 9 +++--- src/shmbuf/buffer.c | 24 +++++++------- tests/arbiter-buffer.c | 2 +- tests/bench/ringbufs.c | 6 ++-- tests/buffer-test.c | 2 +- 11 files changed, 86 insertions(+), 85 deletions(-) diff --git a/include/vamos-buffers/core/source.h b/include/vamos-buffers/core/source.h index fc65b98b..2f6659eb 100644 --- a/include/vamos-buffers/core/source.h +++ b/include/vamos-buffers/core/source.h @@ -10,33 +10,31 @@ struct event_record { unsigned char signature[32]; }; -struct source_control { +struct vms_source_control { size_t size; /* size of this structure (it's variable) */ struct event_record events[]; }; -size_t source_control_get_records_num(struct source_control *sc); +size_t vms_source_control_get_records_num(struct vms_source_control *sc); -struct source_control *source_control_define(size_t ev_nums, ...); -struct source_control *source_control_define_pairwise(size_t ev_nums, - const char *names[], - const char *signatures[]); +struct vms_source_control *vms_source_control_define(size_t ev_nums, ...); +struct vms_source_control *vms_source_control_define_pairwise( + size_t ev_nums, const char *names[], const char *signatures[]); /* string is in the format "event-name:signature,event-name:signature,..." */ -struct source_control *source_control_define_str(const char *str); +struct vms_source_control *vms_source_control_define_str(const char *str); -size_t source_control_max_event_size(struct source_control *control); +size_t vms_source_control_max_event_size(struct vms_source_control *control); -struct event_record *source_control_get_event(struct source_control *control, - const char *name); +struct event_record *vms_source_control_get_event( + struct vms_source_control *control, const char *name); -struct source_control *source_control_allocate(size_t ev_nums); +struct vms_source_control *vms_source_control_allocate(size_t ev_nums); -_Bool source_control_define_pairwise_partially(struct source_control *control, - size_t from, size_t ev_nums, - const char *names[], - const char *signatures[]); +_Bool vms_source_control_define_pairwise_partially( + struct vms_source_control *control, size_t from, size_t ev_nums, + const char *names[], const char *signatures[]); -_Bool source_control_define_partially(struct source_control *control, - size_t from, size_t ev_nums, ...); +_Bool vms_source_control_define_partially(struct vms_source_control *control, + size_t from, size_t ev_nums, ...); #endif /* VAMOS_SOURCE_H_ */ diff --git a/include/vamos-buffers/shmbuf/buffer.h b/include/vamos-buffers/shmbuf/buffer.h index 60f63cb1..d2a51614 100644 --- a/include/vamos-buffers/shmbuf/buffer.h +++ b/include/vamos-buffers/shmbuf/buffer.h @@ -6,17 +6,18 @@ #include #include -struct source_control; +struct vms_source_control; struct event_record; struct buffer; struct buffer *create_shared_buffer(const char *key, size_t capacity, - const struct source_control *control); -struct buffer *create_shared_buffer_adv(const char *key, mode_t mode, - size_t elem_size, size_t capacity, - const struct source_control *control); -struct buffer *create_shared_sub_buffer(struct buffer *buffer, size_t capacity, - const struct source_control *control); + const struct vms_source_control *control); +struct buffer *create_shared_buffer_adv( + const char *key, mode_t mode, size_t elem_size, size_t capacity, + const struct vms_source_control *control); +struct buffer *create_shared_sub_buffer( + struct buffer *buffer, size_t capacity, + const struct vms_source_control *control); size_t buffer_get_sub_buffers_no(struct buffer *buffer); struct buffer *try_get_shared_buffer(const char *key, size_t retry); diff --git a/src/core/source.c b/src/core/source.c index 59a36448..d6ce4ab5 100644 --- a/src/core/source.c +++ b/src/core/source.c @@ -7,13 +7,13 @@ #include "vamos-buffers/core/signatures.h" -size_t source_control_get_records_num(struct source_control *sc) { - return ((sc->size - sizeof(struct source_control)) / +size_t vms_source_control_get_records_num(struct vms_source_control *sc) { + return ((sc->size - sizeof(struct vms_source_control)) / sizeof(struct event_record)); } -size_t source_control_max_event_size(struct source_control *control) { - const size_t en = source_control_get_records_num(control); +size_t vms_source_control_max_event_size(struct vms_source_control *control) { + const size_t en = vms_source_control_get_records_num(control); size_t max_size = 0; for (size_t i = 0; i < en; ++i) { if (max_size < control->events[i].size) @@ -22,9 +22,9 @@ size_t source_control_max_event_size(struct source_control *control) { return max_size; } -struct event_record *source_control_get_event(struct source_control *control, - const char *name) { - const size_t en = source_control_get_records_num(control); +struct event_record *vms_source_control_get_event( + struct vms_source_control *control, const char *name) { + const size_t en = vms_source_control_get_records_num(control); for (size_t i = 0; i < en; ++i) { if (strncmp(control->events[i].name, name, sizeof(control->events[0].name)) == 0) { @@ -52,10 +52,10 @@ static inline void init_record(struct event_record *ev, const char *name, ev->kind = 0; } -struct source_control *source_control_allocate(size_t ev_nums) { +struct vms_source_control *vms_source_control_allocate(size_t ev_nums) { size_t control_size = sizeof(size_t) + ev_nums * sizeof(struct event_record); - struct source_control *control = malloc(control_size); + struct vms_source_control *control = malloc(control_size); if (!control) { assert(0 && "Allocation failed"); abort(); @@ -66,8 +66,8 @@ struct source_control *source_control_allocate(size_t ev_nums) { return control; } -struct source_control *source_control_define(size_t ev_nums, ...) { - struct source_control *control = source_control_allocate(ev_nums); +struct vms_source_control *vms_source_control_define(size_t ev_nums, ...) { + struct vms_source_control *control = vms_source_control_allocate(ev_nums); va_list ap; va_start(ap, ev_nums); @@ -82,9 +82,9 @@ struct source_control *source_control_define(size_t ev_nums, ...) { return control; } -struct source_control *source_control_define_pairwise( +struct vms_source_control *vms_source_control_define_pairwise( size_t ev_nums, const char *names[], const char *signatures[]) { - struct source_control *control = source_control_allocate(ev_nums); + struct vms_source_control *control = vms_source_control_allocate(ev_nums); for (size_t i = 0; i < ev_nums; ++i) { init_record(control->events + i, names[i], signatures[i]); } @@ -92,7 +92,7 @@ struct source_control *source_control_define_pairwise( return control; } -struct source_control *source_control_define_str(const char *str) { +struct vms_source_control *vms_source_control_define_str(const char *str) { size_t ev_nums = 0; size_t com_nums = 0; /* count the number of events */ @@ -109,7 +109,7 @@ struct source_control *source_control_define_str(const char *str) { size_t control_size = sizeof(size_t) + ev_nums * sizeof(struct event_record); - struct source_control *control = malloc(control_size); + struct vms_source_control *control = malloc(control_size); assert(control); control->size = control_size; @@ -178,8 +178,8 @@ struct source_control *source_control_define_str(const char *str) { return NULL; } -_Bool source_control_define_partially(struct source_control *control, - size_t from, size_t ev_nums, ...) { +_Bool vms_source_control_define_partially(struct vms_source_control *control, + size_t from, size_t ev_nums, ...) { va_list ap; va_start(ap, ev_nums); @@ -192,7 +192,7 @@ _Bool source_control_define_partially(struct source_control *control, const size_t end = from + ev_nums; for (size_t i = from; i < end; ++i) { - assert(i < source_control_get_records_num(control)); + assert(i < vms_source_control_get_records_num(control)); const char *name = va_arg(ap, const char *); const char *sig = va_arg(ap, const char *); init_record(control->events + i, name, sig); @@ -203,10 +203,9 @@ _Bool source_control_define_partially(struct source_control *control, return 1; } -_Bool source_control_define_pairwise_partially(struct source_control *control, - size_t from, size_t ev_nums, - const char *names[], - const char *signatures[]) { +_Bool vms_source_control_define_pairwise_partially( + struct vms_source_control *control, size_t from, size_t ev_nums, + const char *names[], const char *signatures[]) { #ifndef NDEBUG for (size_t i = 0; i < from; ++i) { assert(control->events[i].size > 0 && @@ -217,7 +216,7 @@ _Bool source_control_define_pairwise_partially(struct source_control *control, size_t n = 0; const size_t end = from + ev_nums; for (size_t i = from; i < end; ++i) { - assert(i < source_control_get_records_num(control)); + assert(i < vms_source_control_get_records_num(control)); init_record(control->events + i, names[n], signatures[n]); ++n; } diff --git a/src/shmbuf/buffer-control.c b/src/shmbuf/buffer-control.c index d782dd07..e248bf8d 100644 --- a/src/shmbuf/buffer-control.c +++ b/src/shmbuf/buffer-control.c @@ -7,7 +7,7 @@ #include "vamos-buffers/core/source.h" HIDE_SYMBOL -struct source_control *get_shared_control_buffer(const char *buff_key) { +struct vms_source_control *get_shared_control_buffer(const char *buff_key) { char key[SHM_NAME_MAXLEN]; vms_shm_map_ctrl_key(buff_key, key); @@ -38,12 +38,13 @@ struct source_control *get_shared_control_buffer(const char *buff_key) { } /* FIXME: we leak fd */ - return (struct source_control *)mem; + return (struct vms_source_control *)mem; } HIDE_SYMBOL -struct source_control *create_shared_control_buffer( - const char *buff_key, mode_t mode, const struct source_control *control) { +struct vms_source_control *create_shared_control_buffer( + const char *buff_key, mode_t mode, + const struct vms_source_control *control) { char key[SHM_NAME_MAXLEN]; vms_shm_map_ctrl_key(buff_key, key); size_t size = control->size; @@ -105,11 +106,11 @@ struct source_control *create_shared_control_buffer( return NULL; } - return (struct source_control *)mem; + return (struct vms_source_control *)mem; } HIDE_SYMBOL -void release_shared_control_buffer(struct source_control *buffer) { +void release_shared_control_buffer(struct vms_source_control *buffer) { if (munmap(buffer, buffer->size) != 0) { perror("munmap failure"); } @@ -119,7 +120,7 @@ void release_shared_control_buffer(struct source_control *buffer) { HIDE_SYMBOL void destroy_shared_control_buffer(const char *buffkey, - struct source_control *buffer) { + struct vms_source_control *buffer) { release_shared_control_buffer(buffer); char key[SHM_NAME_MAXLEN]; diff --git a/src/shmbuf/buffer-local.c b/src/shmbuf/buffer-local.c index d4f4b8b6..da411086 100644 --- a/src/shmbuf/buffer-local.c +++ b/src/shmbuf/buffer-local.c @@ -24,7 +24,7 @@ /* FOR TESTING */ struct buffer *initialize_local_buffer(const char *key, size_t elem_size, size_t capacity, - struct source_control *control) { + struct vms_source_control *control) { assert(elem_size > 0 && "Element size is 0"); printf("Initializing LOCAL buffer '%s' with elem size '%lu'\n", key, elem_size); diff --git a/src/shmbuf/buffer-private.h b/src/shmbuf/buffer-private.h index 48f8afd0..b79d00b0 100644 --- a/src/shmbuf/buffer-private.h +++ b/src/shmbuf/buffer-private.h @@ -11,7 +11,7 @@ #include "vamos-buffers/core/spsc_ringbuf.h" #include "vamos-buffers/core/vector-macro.h" -struct source_control; +struct vms_source_control; struct event_record; struct buffer; @@ -68,7 +68,7 @@ struct aux_buffer { the vms_spsc_ringbuf so that we can keep local cache */ struct buffer { struct shmbuffer *shmbuffer; - struct source_control *control; + struct vms_source_control *control; /* shared memory of auxiliary buffer */ struct aux_buffer *cur_aux_buff; /* the known aux_buffers that might still be needed */ @@ -89,7 +89,7 @@ struct buffer { struct buffer *initialize_shared_buffer(const char *key, mode_t mode, size_t elem_size, size_t capacity, - struct source_control *control); + struct vms_source_control *control); struct buffer *get_shared_buffer(const char *key); struct buffer *try_get_shared_buffer(const char *key, size_t retry); @@ -101,16 +101,17 @@ size_t compute_vms_buffer_size(size_t nondata_size, size_t elem_size, /*** LOCAL buffers ***/ struct buffer *initialize_local_buffer(const char *key, size_t elem_size, size_t capacity, - struct source_control *control); + struct vms_source_control *control); void release_local_buffer(struct buffer *buff); /*** CONTROL buffers ***/ -struct source_control *get_shared_control_buffer(const char *buff_key); -struct source_control *create_shared_control_buffer( - const char *buff_key, mode_t mode, const struct source_control *control); -void release_shared_control_buffer(struct source_control *buffer); +struct vms_source_control *get_shared_control_buffer(const char *buff_key); +struct vms_source_control *create_shared_control_buffer( + const char *buff_key, mode_t mode, + const struct vms_source_control *control); +void release_shared_control_buffer(struct vms_source_control *buffer); void destroy_shared_control_buffer(const char *buffkey, - struct source_control *buffer); + struct vms_source_control *buffer); /*** AUX buffers ***/ size_t aux_buffer_free_space(struct aux_buffer *buff); diff --git a/src/shmbuf/buffer-sub.c b/src/shmbuf/buffer-sub.c index 011351d7..d0ac9b74 100644 --- a/src/shmbuf/buffer-sub.c +++ b/src/shmbuf/buffer-sub.c @@ -18,17 +18,18 @@ char *get_sub_buffer_key(const char *key, size_t idx) { return tmp; } -struct buffer *create_shared_sub_buffer(struct buffer *buffer, size_t capacity, - const struct source_control *control) { +struct buffer *create_shared_sub_buffer( + struct buffer *buffer, size_t capacity, + const struct vms_source_control *control) { char *key = get_sub_buffer_key(buffer->key, ++buffer->last_subbufer_no); - struct source_control *ctrl = + struct vms_source_control *ctrl = create_shared_control_buffer(key, S_IRWXU, control); if (!ctrl) { fprintf(stderr, "Failed creating subbuffer\n"); return NULL; } - size_t elem_size = source_control_max_event_size(ctrl); + size_t elem_size = vms_source_control_max_event_size(ctrl); if (capacity == 0) capacity = buffer_capacity(buffer); struct buffer *sbuf = diff --git a/src/shmbuf/buffer.c b/src/shmbuf/buffer.c index c8e55ac7..4547f847 100644 --- a/src/shmbuf/buffer.c +++ b/src/shmbuf/buffer.c @@ -84,7 +84,7 @@ int buffer_get_ctrl_key_path(struct buffer *buff, char keypath[], HIDE_SYMBOL struct buffer *initialize_shared_buffer(const char *key, mode_t mode, size_t elem_size, size_t capacity, - struct source_control *control) { + struct vms_source_control *control) { assert(elem_size > 0 && "Element size is 0"); assert(capacity > 0 && "Capacity is 0"); /* the ringbuffer has one unusable dummy element, so increase the capacity @@ -194,22 +194,22 @@ struct buffer *initialize_shared_buffer(const char *key, mode_t mode, } struct buffer *create_shared_buffer(const char *key, size_t capacity, - const struct source_control *control) { - struct source_control *ctrl = + const struct vms_source_control *control) { + struct vms_source_control *ctrl = create_shared_control_buffer(key, S_IRWXU, control); if (!ctrl) { fprintf(stderr, "Failed creating control buffer\n"); return NULL; } - size_t elem_size = source_control_max_event_size(ctrl); + size_t elem_size = vms_source_control_max_event_size(ctrl); return initialize_shared_buffer(key, S_IRWXU, elem_size, capacity, ctrl); } -struct buffer *create_shared_buffer_adv(const char *key, mode_t mode, - size_t elem_size, size_t capacity, - const struct source_control *control) { - struct source_control *ctrl = +struct buffer *create_shared_buffer_adv( + const char *key, mode_t mode, size_t elem_size, size_t capacity, + const struct vms_source_control *control) { + struct vms_source_control *ctrl = create_shared_control_buffer(key, mode, control); if (!ctrl) { fprintf(stderr, "Failed creating control buffer\n"); @@ -217,7 +217,7 @@ struct buffer *create_shared_buffer_adv(const char *key, mode_t mode, } if (elem_size == 0) { - elem_size = source_control_max_event_size(ctrl); + elem_size = vms_source_control_max_event_size(ctrl); } if (mode == 0) { @@ -326,7 +326,7 @@ struct event_record *buffer_get_avail_events(struct buffer *buff, assert(evs_num); assert(buff->control); - *evs_num = source_control_get_records_num(buff->control); + *evs_num = vms_source_control_get_records_num(buff->control); return buff->control->events; } @@ -593,7 +593,7 @@ void buffer_notify_dropped(struct buffer *buff, uint64_t begin_id, int buffer_register_event(struct buffer *b, const char *name, uint64_t kind) { assert(kind > vms_event_get_last_special_kind() && "Invalid event kind, it overlaps special kinds"); - struct event_record *rec = source_control_get_event(b->control, name); + struct event_record *rec = vms_source_control_get_event(b->control, name); if (rec == NULL) { return -1; } @@ -623,7 +623,7 @@ int buffer_register_events(struct buffer *b, size_t ev_nums, ...) { int buffer_register_all_events(struct buffer *b) { struct event_record *recs = b->control->events; - const size_t ev_nums = source_control_get_records_num(b->control); + const size_t ev_nums = vms_source_control_get_records_num(b->control); for (size_t i = 0; i < ev_nums; ++i) { recs[i].kind = 1 + i + vms_event_get_last_special_kind(); diff --git a/tests/arbiter-buffer.c b/tests/arbiter-buffer.c index 9459e932..94b95642 100644 --- a/tests/arbiter-buffer.c +++ b/tests/arbiter-buffer.c @@ -23,7 +23,7 @@ struct event { struct buffer *initialize_local_buffer(const char *key, size_t elem_size, size_t capacity, - struct source_control *control); + struct vms_source_control *control); int main(void) { struct buffer *lbuffer = diff --git a/tests/bench/ringbufs.c b/tests/bench/ringbufs.c index c49a4cff..62b8f997 100644 --- a/tests/bench/ringbufs.c +++ b/tests/bench/ringbufs.c @@ -30,7 +30,7 @@ static double report_time(const char *msg, struct timespec *start, #if 0 static void run_shmbuf_push_pop_st() { - struct source_control *ctrl = source_control_define(1, "dummy", "i"); + struct vms_source_control *ctrl = vms_source_control_define(1, "dummy", "i"); struct buffer *buff = create_shared_buffer("/test", sizeof(int), ctrl); assert(buff); @@ -65,11 +65,11 @@ static void run_shmbuf_push_pop_st() { /* to see if there is any difference between malloce'd and shared memory */ struct buffer *initialize_local_buffer(const char *key, size_t elem_size, - struct source_control *control); + struct vms_source_control *control); void release_local_buffer(struct buffer *buff); static void run_local_shmbuf_push_pop_st() { - struct source_control *ctrl = source_control_define(1, "dummy", "i"); + struct vms_source_control *ctrl = vms_source_control_define(1, "dummy", "i"); struct buffer *buff = initialize_local_buffer("/test", sizeof(int), ctrl); assert(buff); diff --git a/tests/buffer-test.c b/tests/buffer-test.c index e418f360..e9016520 100644 --- a/tests/buffer-test.c +++ b/tests/buffer-test.c @@ -10,7 +10,7 @@ int main(void) { size_t i; const size_t ctrl_size = sizeof(size_t) + sizeof(struct event_record); - struct source_control *ctrl = malloc(ctrl_size); + struct vms_source_control *ctrl = malloc(ctrl_size); ctrl->size = ctrl_size; ctrl->events[0].size = sizeof(size_t); ctrl->events[0].kind = 2; From 52986ea9f46e0a8e8e8b7739639c8127ec7469ec Mon Sep 17 00:00:00 2001 From: Marek Chalupa Date: Tue, 17 Oct 2023 19:38:12 +0200 Subject: [PATCH 20/34] Rename event_record -> vms_event_record --- include/vamos-buffers/core/source.h | 6 +++--- include/vamos-buffers/core/stream.h | 8 ++++---- include/vamos-buffers/shmbuf/buffer.h | 4 ++-- include/vamos-buffers/streams/stream-funs.h | 4 ++-- src/core/source.c | 14 +++++++------- src/core/stream.c | 20 ++++++++++---------- src/shmbuf/buffer-private.h | 2 +- src/shmbuf/buffer.c | 6 +++--- src/streams/stream-funs.c | 2 +- tests/buffer-test.c | 2 +- 10 files changed, 34 insertions(+), 34 deletions(-) diff --git a/include/vamos-buffers/core/source.h b/include/vamos-buffers/core/source.h index 2f6659eb..c598cb87 100644 --- a/include/vamos-buffers/core/source.h +++ b/include/vamos-buffers/core/source.h @@ -3,7 +3,7 @@ #include "event.h" -struct event_record { +struct vms_event_record { char name[64]; vms_kind kind; size_t size; @@ -12,7 +12,7 @@ struct event_record { struct vms_source_control { size_t size; /* size of this structure (it's variable) */ - struct event_record events[]; + struct vms_event_record events[]; }; size_t vms_source_control_get_records_num(struct vms_source_control *sc); @@ -26,7 +26,7 @@ struct vms_source_control *vms_source_control_define_str(const char *str); size_t vms_source_control_max_event_size(struct vms_source_control *control); -struct event_record *vms_source_control_get_event( +struct vms_event_record *vms_source_control_get_event( struct vms_source_control *control, const char *name); struct vms_source_control *vms_source_control_allocate(size_t ev_nums); diff --git a/include/vamos-buffers/core/stream.h b/include/vamos-buffers/core/stream.h index 21cc9b3b..814b28ae 100644 --- a/include/vamos-buffers/core/stream.h +++ b/include/vamos-buffers/core/stream.h @@ -35,7 +35,7 @@ typedef struct _vms_stream { * shared memory buffer */ size_t substreams_no; /* cached info about events in 'incoming_events_buffer' */ - struct event_record *events_cache; + struct vms_event_record *events_cache; /* callbacks */ vms_stream_is_ready_fn is_ready; vms_stream_filter_fn filter; @@ -70,9 +70,9 @@ const char *vms_stream_get_type(vms_stream *); size_t vms_stream_event_size(vms_stream *); size_t vms_stream_id(vms_stream *); -struct event_record *vms_stream_get_avail_events(vms_stream *, size_t *); -struct event_record *vms_stream_get_event_record(vms_stream *, vms_kind); -struct event_record *vms_stream_get_event_record_no_cache(vms_stream *, +struct vms_event_record *vms_stream_get_avail_events(vms_stream *, size_t *); +struct vms_event_record *vms_stream_get_vms_event_record(vms_stream *, vms_kind); +struct vms_event_record *vms_stream_get_vms_event_record_no_cache(vms_stream *, vms_kind); /* register to receiving events particular events */ diff --git a/include/vamos-buffers/shmbuf/buffer.h b/include/vamos-buffers/shmbuf/buffer.h index d2a51614..95b9b641 100644 --- a/include/vamos-buffers/shmbuf/buffer.h +++ b/include/vamos-buffers/shmbuf/buffer.h @@ -7,7 +7,7 @@ #include struct vms_source_control; -struct event_record; +struct vms_event_record; struct buffer; struct buffer *create_shared_buffer(const char *key, size_t capacity, @@ -22,7 +22,7 @@ size_t buffer_get_sub_buffers_no(struct buffer *buffer); struct buffer *try_get_shared_buffer(const char *key, size_t retry); struct buffer *get_shared_buffer(const char *key); -struct event_record *buffer_get_avail_events(struct buffer *, size_t *); +struct vms_event_record *buffer_get_avail_events(struct buffer *, size_t *); int buffer_get_key_path(struct buffer *, char keypath[], size_t keypathsize); int buffer_get_ctrl_key_path(struct buffer *, char keypath[], diff --git a/include/vamos-buffers/streams/stream-funs.h b/include/vamos-buffers/streams/stream-funs.h index 802d4abd..f57510dc 100644 --- a/include/vamos-buffers/streams/stream-funs.h +++ b/include/vamos-buffers/streams/stream-funs.h @@ -14,7 +14,7 @@ typedef struct _vms_event_funcall { typedef struct _vms_stream_funs { vms_stream base; struct buffer *shmbuffer; - struct event_record *events; + struct vms_event_record *events; /* buffer for reading and publishing the event */ vms_event_funcall *ev; void *ev_buff; @@ -27,7 +27,7 @@ vms_event_funcall *funs_get_next_event(vms_stream *stream); vms_stream *vms_create_funs_stream(const char *key, const char *name); -struct event_record *vms_funs_stream_get_event_spec(vms_stream_funs *stream, +struct vms_event_record *vms_funs_stream_get_event_spec(vms_stream_funs *stream, vms_kind kind); void vms_event_funcall_release(vms_event_funcall *fev); diff --git a/src/core/source.c b/src/core/source.c index d6ce4ab5..9a4d783e 100644 --- a/src/core/source.c +++ b/src/core/source.c @@ -9,7 +9,7 @@ size_t vms_source_control_get_records_num(struct vms_source_control *sc) { return ((sc->size - sizeof(struct vms_source_control)) / - sizeof(struct event_record)); + sizeof(struct vms_event_record)); } size_t vms_source_control_max_event_size(struct vms_source_control *control) { @@ -22,7 +22,7 @@ size_t vms_source_control_max_event_size(struct vms_source_control *control) { return max_size; } -struct event_record *vms_source_control_get_event( +struct vms_event_record *vms_source_control_get_event( struct vms_source_control *control, const char *name) { const size_t en = vms_source_control_get_records_num(control); for (size_t i = 0; i < en; ++i) { @@ -34,7 +34,7 @@ struct event_record *vms_source_control_get_event( return NULL; } -static inline void init_record(struct event_record *ev, const char *name, +static inline void init_record(struct vms_event_record *ev, const char *name, const char *sig) { const size_t max_name_size = sizeof(ev->name) - 1; const size_t max_sig_size = sizeof(ev->signature) - 1; @@ -54,7 +54,7 @@ static inline void init_record(struct event_record *ev, const char *name, struct vms_source_control *vms_source_control_allocate(size_t ev_nums) { size_t control_size = - sizeof(size_t) + ev_nums * sizeof(struct event_record); + sizeof(size_t) + ev_nums * sizeof(struct vms_event_record); struct vms_source_control *control = malloc(control_size); if (!control) { assert(0 && "Allocation failed"); @@ -108,17 +108,17 @@ struct vms_source_control *vms_source_control_define_str(const char *str) { } size_t control_size = - sizeof(size_t) + ev_nums * sizeof(struct event_record); + sizeof(size_t) + ev_nums * sizeof(struct vms_event_record); struct vms_source_control *control = malloc(control_size); assert(control); control->size = control_size; const size_t max_name_size = - sizeof(((struct event_record *)NULL)->name) - 1; + sizeof(((struct vms_event_record *)NULL)->name) - 1; #ifndef NDEBUG const size_t max_sig_size = - sizeof(((struct event_record *)NULL)->signature) - 1; + sizeof(((struct vms_event_record *)NULL)->signature) - 1; #endif char name[max_name_size]; diff --git a/src/core/stream.c b/src/core/stream.c index 2d22c660..3e2a7469 100644 --- a/src/core/stream.c +++ b/src/core/stream.c @@ -180,13 +180,13 @@ vms_stream *vms_stream_create_substream( return substream; } -struct event_record *vms_stream_get_avail_events(vms_stream *s, size_t *sz) { +struct vms_event_record *vms_stream_get_avail_events(vms_stream *s, size_t *sz) { return buffer_get_avail_events(s->incoming_events_buffer, sz); } #define MAX_EVENTS_CACHE_SIZE 128 -static vms_kind get_max_kind(struct event_record *recs, size_t size) { +static vms_kind get_max_kind(struct vms_event_record *recs, size_t size) { vms_kind ret = 0; for (size_t i = 0; i < size; ++i) { if (recs[i].kind > ret) @@ -196,22 +196,22 @@ static vms_kind get_max_kind(struct event_record *recs, size_t size) { return ret; } -struct event_record *vms_stream_get_event_record(vms_stream *stream, +struct vms_event_record *vms_stream_get_vms_event_record(vms_stream *stream, vms_kind kind) { assert(kind > 0 && "Got invalid kind"); - struct event_record *rec = NULL; + struct vms_event_record *rec = NULL; if (stream->events_cache) { if (kind < MAX_EVENTS_CACHE_SIZE) { rec = &stream->events_cache[kind]; assert(rec->kind == kind); return rec; } else { - return vms_stream_get_event_record_no_cache(stream, kind); + return vms_stream_get_vms_event_record_no_cache(stream, kind); } } else { /* create cache */ size_t sz; - struct event_record *recs = + struct vms_event_record *recs = buffer_get_avail_events(stream->incoming_events_buffer, &sz); size_t max_kind = get_max_kind(recs, sz); size_t cache_sz = @@ -219,7 +219,7 @@ struct event_record *vms_stream_get_event_record(vms_stream *stream, : max_kind) + 1; assert(cache_sz > 0 && "Invalid cache size"); - stream->events_cache = malloc(cache_sz * sizeof(struct event_record)); + stream->events_cache = malloc(cache_sz * sizeof(struct vms_event_record)); /* cache elements that fit into the cache (cache is indexed by kinds) */ for (size_t i = 0; i < sz; ++i) { if (recs[i].kind < cache_sz) { @@ -232,10 +232,10 @@ struct event_record *vms_stream_get_event_record(vms_stream *stream, } } -struct event_record *vms_stream_get_event_record_no_cache(vms_stream *stream, +struct vms_event_record *vms_stream_get_vms_event_record_no_cache(vms_stream *stream, vms_kind kind) { size_t sz; - struct event_record *recs = + struct vms_event_record *recs = buffer_get_avail_events(stream->incoming_events_buffer, &sz); for (size_t i = 0; i < sz; ++i) { if (recs[i].kind == kind) @@ -331,7 +331,7 @@ void vms_stream_detach(vms_stream *stream) { void vms_stream_dump_events(vms_stream *stream) { size_t evs_num; - struct event_record *events = + struct vms_event_record *events = buffer_get_avail_events(stream->incoming_events_buffer, &evs_num); for (size_t i = 0; i < evs_num; ++i) { fprintf(stderr, diff --git a/src/shmbuf/buffer-private.h b/src/shmbuf/buffer-private.h index b79d00b0..cdbf188c 100644 --- a/src/shmbuf/buffer-private.h +++ b/src/shmbuf/buffer-private.h @@ -12,7 +12,7 @@ #include "vamos-buffers/core/vector-macro.h" struct vms_source_control; -struct event_record; +struct vms_event_record; struct buffer; #define MAX_AUX_BUF_KEY_SIZE 16 diff --git a/src/shmbuf/buffer.c b/src/shmbuf/buffer.c index 4547f847..704e25bf 100644 --- a/src/shmbuf/buffer.c +++ b/src/shmbuf/buffer.c @@ -320,7 +320,7 @@ struct buffer *get_shared_buffer(const char *key) { return try_get_shared_buffer(key, 10); } -struct event_record *buffer_get_avail_events(struct buffer *buff, +struct vms_event_record *buffer_get_avail_events(struct buffer *buff, size_t *evs_num) { assert(buff); assert(evs_num); @@ -593,7 +593,7 @@ void buffer_notify_dropped(struct buffer *buff, uint64_t begin_id, int buffer_register_event(struct buffer *b, const char *name, uint64_t kind) { assert(kind > vms_event_get_last_special_kind() && "Invalid event kind, it overlaps special kinds"); - struct event_record *rec = vms_source_control_get_event(b->control, name); + struct vms_event_record *rec = vms_source_control_get_event(b->control, name); if (rec == NULL) { return -1; } @@ -622,7 +622,7 @@ int buffer_register_events(struct buffer *b, size_t ev_nums, ...) { } int buffer_register_all_events(struct buffer *b) { - struct event_record *recs = b->control->events; + struct vms_event_record *recs = b->control->events; const size_t ev_nums = vms_source_control_get_records_num(b->control); for (size_t i = 0; i < ev_nums; ++i) { diff --git a/src/streams/stream-funs.c b/src/streams/stream-funs.c index 9477c5ba..339e0e01 100644 --- a/src/streams/stream-funs.c +++ b/src/streams/stream-funs.c @@ -41,7 +41,7 @@ const char *vms_stream_funs_get_str(vms_stream_funs *fstream, uint64_t elem) { return buffer_get_str(fstream->shmbuffer, elem); } -struct event_record *vms_funs_stream_get_event_spec(vms_stream_funs *stream, +struct vms_event_record *vms_funs_stream_get_event_spec(vms_stream_funs *stream, vms_kind kind) { for (size_t i = 0; i < stream->spec_count; ++i) if (stream->events[i].kind == kind) diff --git a/tests/buffer-test.c b/tests/buffer-test.c index e9016520..5567d3b7 100644 --- a/tests/buffer-test.c +++ b/tests/buffer-test.c @@ -9,7 +9,7 @@ int main(void) { size_t i; - const size_t ctrl_size = sizeof(size_t) + sizeof(struct event_record); + const size_t ctrl_size = sizeof(size_t) + sizeof(struct vms_event_record); struct vms_source_control *ctrl = malloc(ctrl_size); ctrl->size = ctrl_size; ctrl->events[0].size = sizeof(size_t); From 3a78b7d0fe0497c76521582b2d48a3ef51ceeebd Mon Sep 17 00:00:00 2001 From: Marek Chalupa Date: Tue, 17 Oct 2023 19:50:49 +0200 Subject: [PATCH 21/34] Rename buffer structures to use vms_ --- include/vamos-buffers/core/stream.h | 5 +- include/vamos-buffers/shmbuf/buffer.h | 103 +++++++----------- include/vamos-buffers/shmbuf/client.h | 4 +- include/vamos-buffers/shmbuf/dbg.h | 27 +++++ .../vamos-buffers/streams/stream-drregex.h | 2 +- include/vamos-buffers/streams/stream-funs.h | 2 +- .../vamos-buffers/streams/stream-generic.h | 4 +- include/vamos-buffers/streams/stream-regex.h | 2 +- .../vamos-buffers/streams/stream-regexrw.h | 2 +- src/core/stream.c | 4 +- src/shmbuf/buffer-aux.c | 14 +-- src/shmbuf/buffer-dbg.c | 38 +++---- src/shmbuf/buffer-local.c | 12 +- src/shmbuf/buffer-private.h | 38 +++---- src/shmbuf/buffer-sub.c | 14 +-- src/shmbuf/buffer.c | 100 ++++++++--------- src/shmbuf/client.c | 2 +- src/streams/stream-drregex.c | 4 +- src/streams/stream-funs.c | 6 +- src/streams/stream-generic.c | 4 +- src/streams/stream-regex.c | 4 +- src/streams/stream-regexrw.c | 4 +- tests/arbiter-buffer.c | 8 +- tests/bench/ringbufs.c | 8 +- tests/buffer-test.c | 2 +- tests/fetch-test-2.c | 4 +- tests/fetch-test-3.c | 4 +- tests/fetch-test.c | 4 +- 28 files changed, 215 insertions(+), 210 deletions(-) create mode 100644 include/vamos-buffers/shmbuf/dbg.h diff --git a/include/vamos-buffers/core/stream.h b/include/vamos-buffers/core/stream.h index 814b28ae..9ce55675 100644 --- a/include/vamos-buffers/core/stream.h +++ b/include/vamos-buffers/core/stream.h @@ -6,6 +6,7 @@ #include "vector-macro.h" typedef struct _vms_arbiter_buffer vms_arbiter_buffer; +typedef struct _vms_shm_buffer vms_shm_buffer; typedef size_t (*vms_stream_buffer_events_fn)(struct _vms_stream *, vms_arbiter_buffer *buffer); @@ -30,7 +31,7 @@ typedef struct _vms_stream { char *type; size_t event_size; /* shared-memory buffer */ - struct buffer *incoming_events_buffer; + vms_shm_buffer *incoming_events_buffer; /* the number of created substreams (sub-buffers) for the * shared memory buffer */ size_t substreams_no; @@ -58,7 +59,7 @@ typedef struct _vms_stream { #endif } vms_stream; -void vms_stream_init(vms_stream *stream, struct buffer *incoming_events_buffer, +void vms_stream_init(vms_stream *stream, vms_shm_buffer *incoming_events_buffer, size_t event_size, vms_stream_is_ready_fn is_ready, vms_stream_filter_fn filter, vms_stream_alter_fn alter, vms_stream_destroy_fn destroy, diff --git a/include/vamos-buffers/shmbuf/buffer.h b/include/vamos-buffers/shmbuf/buffer.h index 95b9b641..72232e61 100644 --- a/include/vamos-buffers/shmbuf/buffer.h +++ b/include/vamos-buffers/shmbuf/buffer.h @@ -8,95 +8,72 @@ struct vms_source_control; struct vms_event_record; -struct buffer; +typedef struct _vms_shm_buffer vms_shm_buffer; -struct buffer *create_shared_buffer(const char *key, size_t capacity, - const struct vms_source_control *control); -struct buffer *create_shared_buffer_adv( +vms_shm_buffer *create_shared_buffer(const char *key, size_t capacity, + const struct vms_source_control *control); +vms_shm_buffer *create_shared_buffer_adv( const char *key, mode_t mode, size_t elem_size, size_t capacity, const struct vms_source_control *control); -struct buffer *create_shared_sub_buffer( - struct buffer *buffer, size_t capacity, +vms_shm_buffer *create_shared_sub_buffer( + vms_shm_buffer *buffer, size_t capacity, const struct vms_source_control *control); -size_t buffer_get_sub_buffers_no(struct buffer *buffer); +size_t buffer_get_sub_buffers_no(vms_shm_buffer *buffer); -struct buffer *try_get_shared_buffer(const char *key, size_t retry); -struct buffer *get_shared_buffer(const char *key); -struct vms_event_record *buffer_get_avail_events(struct buffer *, size_t *); +vms_shm_buffer *try_get_shared_buffer(const char *key, size_t retry); +vms_shm_buffer *get_shared_buffer(const char *key); +struct vms_event_record *buffer_get_avail_events(vms_shm_buffer *, size_t *); -int buffer_get_key_path(struct buffer *, char keypath[], size_t keypathsize); -int buffer_get_ctrl_key_path(struct buffer *, char keypath[], +int buffer_get_key_path(vms_shm_buffer *, char keypath[], size_t keypathsize); +int buffer_get_ctrl_key_path(vms_shm_buffer *, char keypath[], size_t keypathsize); -const char *buffer_get_key(struct buffer *); +const char *buffer_get_key(vms_shm_buffer *); char *get_sub_buffer_key(const char *key, size_t idx); -void release_shared_buffer(struct buffer *); -void destroy_shared_buffer(struct buffer *); -void destroy_shared_sub_buffer(struct buffer *); -void release_shared_sub_buffer(struct buffer *); +void release_shared_buffer(vms_shm_buffer *); +void destroy_shared_buffer(vms_shm_buffer *); +void destroy_shared_sub_buffer(vms_shm_buffer *); +void release_shared_sub_buffer(vms_shm_buffer *); -bool buffer_is_ready(struct buffer *); -bool buffer_monitor_attached(struct buffer *); -void buffer_set_attached(struct buffer *, bool); -void buffer_set_destroyed(struct buffer *buff); +bool buffer_is_ready(vms_shm_buffer *); +bool buffer_monitor_attached(vms_shm_buffer *); +void buffer_set_attached(vms_shm_buffer *, bool); +void buffer_set_destroyed(vms_shm_buffer *buff); -bool buffer_pop(struct buffer *buff, void *dst); -bool buffer_push(struct buffer *buff, const void *elem, size_t size); -void *buffer_get_str(struct buffer *buff, uint64_t elem); +bool buffer_pop(vms_shm_buffer *buff, void *dst); +bool buffer_push(vms_shm_buffer *buff, const void *elem, size_t size); +void *buffer_get_str(vms_shm_buffer *buff, uint64_t elem); -void *buffer_read_pointer(struct buffer *buff, size_t *size); -bool buffer_drop_k(struct buffer *buff, size_t size); -size_t buffer_consume(struct buffer *buff, size_t k); +void *buffer_read_pointer(vms_shm_buffer *buff, size_t *size); +bool buffer_drop_k(vms_shm_buffer *buff, size_t size); +size_t buffer_consume(vms_shm_buffer *buff, size_t k); -size_t buffer_size(struct buffer *buff); -size_t buffer_capacity(struct buffer *buff); -size_t buffer_elem_size(struct buffer *buff); +size_t buffer_size(vms_shm_buffer *buff); +size_t buffer_capacity(vms_shm_buffer *buff); +size_t buffer_elem_size(vms_shm_buffer *buff); -void *buffer_start_push(struct buffer *buff); +void *buffer_start_push(vms_shm_buffer *buff); -void *buffer_partial_push(struct buffer *buff, void *prev_push, +void *buffer_partial_push(vms_shm_buffer *buff, void *prev_push, const void *elem, size_t size); /* evid may be ~(0LL), but then there's no garbage collection */ -void *buffer_partial_push_str(struct buffer *buff, void *prev_push, +void *buffer_partial_push_str(vms_shm_buffer *buff, void *prev_push, uint64_t evid, const char *str); -void *buffer_partial_push_str_n(struct buffer *buff, void *prev_push, +void *buffer_partial_push_str_n(vms_shm_buffer *buff, void *prev_push, uint64_t evid, const char *str, size_t len); -void buffer_finish_push(struct buffer *buff); +void buffer_finish_push(vms_shm_buffer *buff); struct aux_buff_ptr { uint32_t buffer_id; uint32_t offset; } __attribute__((packed, aligned(64))); -void buffer_set_last_processed_id(struct buffer *buff, uint64_t id); -void buffer_notify_dropped(struct buffer *buffer, uint64_t begin_id, +void buffer_set_last_processed_id(vms_shm_buffer *buff, uint64_t id); +void buffer_notify_dropped(vms_shm_buffer *buffer, uint64_t begin_id, uint64_t end_id); -int buffer_register_event(struct buffer *b, const char *name, uint64_t kind); -int buffer_register_events(struct buffer *b, size_t ev_nums, ...); -int buffer_register_all_events(struct buffer *b); - -/** - * dbg buffers - */ - -typedef struct _vms_vms_dbg_buffer vms_vms_dbg_buffer; -vms_vms_dbg_buffer *vms_vms_dbg_buffer_create(const char *key, size_t capacity, - uint16_t key_size, - uint16_t element_size); -vms_vms_dbg_buffer *vms_vms_dbg_buffer_get(const char *key); -void vms_vms_dbg_buffer_release(vms_vms_dbg_buffer *); -void vms_vms_dbg_buffer_destroy(vms_vms_dbg_buffer *); - -size_t vms_vms_dbg_buffer_size(vms_vms_dbg_buffer *b); -size_t vms_vms_dbg_buffer_capacity(vms_vms_dbg_buffer *b); -size_t vms_vms_dbg_buffer_key_size(vms_vms_dbg_buffer *b); -size_t vms_vms_dbg_buffer_value_size(vms_vms_dbg_buffer *b); -size_t vms_vms_dbg_buffer_rec_size(vms_vms_dbg_buffer *b); -unsigned char *vms_vms_dbg_buffer_data(vms_vms_dbg_buffer *b); - -void vms_vms_dbg_buffer_inc_size(vms_vms_dbg_buffer *b, size_t size); -size_t vms_vms_dbg_buffer_version(vms_vms_dbg_buffer *b); -void vms_vms_dbg_buffer_bump_version(vms_vms_dbg_buffer *b); +int buffer_register_event(vms_shm_buffer *b, const char *name, uint64_t kind); +int buffer_register_events(vms_shm_buffer *b, size_t ev_nums, ...); +int buffer_register_all_events(vms_shm_buffer *b); #endif /* VAMOS_SHM_BUFFER_H */ diff --git a/include/vamos-buffers/shmbuf/client.h b/include/vamos-buffers/shmbuf/client.h index 75dbdee1..a9771d61 100644 --- a/include/vamos-buffers/shmbuf/client.h +++ b/include/vamos-buffers/shmbuf/client.h @@ -1,8 +1,8 @@ #ifndef VAMOS_CLIENT_H #define VAMOS_CLIENT_H -struct buffer; +typedef struct _vms_shm_buffer vms_shm_buffer; -int buffer_wait_for_monitor(struct buffer *); +int buffer_wait_for_monitor(vms_shm_buffer *); #endif diff --git a/include/vamos-buffers/shmbuf/dbg.h b/include/vamos-buffers/shmbuf/dbg.h new file mode 100644 index 00000000..00b0afab --- /dev/null +++ b/include/vamos-buffers/shmbuf/dbg.h @@ -0,0 +1,27 @@ +#ifndef VAMOS_SHM_DBG_BUFFER_H +#define VAMOS_SHM_DBG_BUFFER_H + +/** + * dbg buffers + */ + +typedef struct _vms_shm_dbg_buffer vms_shm_dbg_buffer; +vms_vms_dbg_buffer *vms_vms_dbg_buffer_create(const char *key, size_t capacity, + uint16_t key_size, + uint16_t element_size); +vms_vms_dbg_buffer *vms_vms_dbg_buffer_get(const char *key); +void vms_vms_dbg_buffer_release(vms_vms_dbg_buffer *); +void vms_vms_dbg_buffer_destroy(vms_vms_dbg_buffer *); + +size_t vms_vms_dbg_buffer_size(vms_vms_dbg_buffer *b); +size_t vms_vms_dbg_buffer_capacity(vms_vms_dbg_buffer *b); +size_t vms_vms_dbg_buffer_key_size(vms_vms_dbg_buffer *b); +size_t vms_vms_dbg_buffer_value_size(vms_vms_dbg_buffer *b); +size_t vms_vms_dbg_buffer_rec_size(vms_vms_dbg_buffer *b); +unsigned char *vms_vms_dbg_buffer_data(vms_vms_dbg_buffer *b); + +void vms_vms_dbg_buffer_inc_size(vms_vms_dbg_buffer *b, size_t size); +size_t vms_vms_dbg_buffer_version(vms_vms_dbg_buffer *b); +void vms_vms_dbg_buffer_bump_version(vms_vms_dbg_buffer *b); + +#endif diff --git a/include/vamos-buffers/streams/stream-drregex.h b/include/vamos-buffers/streams/stream-drregex.h index b8696ef6..8c277686 100644 --- a/include/vamos-buffers/streams/stream-drregex.h +++ b/include/vamos-buffers/streams/stream-drregex.h @@ -19,7 +19,7 @@ typedef struct _vms_event_drregex { typedef struct _vms_stream_drregex { vms_stream base; - struct buffer *shmbuffer; + vms_shm_buffer *shmbuffer; } vms_stream_drregex; vms_stream *vms_create_drregex_stream(const char *key, const char *name); diff --git a/include/vamos-buffers/streams/stream-funs.h b/include/vamos-buffers/streams/stream-funs.h index f57510dc..d7bde3c3 100644 --- a/include/vamos-buffers/streams/stream-funs.h +++ b/include/vamos-buffers/streams/stream-funs.h @@ -13,7 +13,7 @@ typedef struct _vms_event_funcall { typedef struct _vms_stream_funs { vms_stream base; - struct buffer *shmbuffer; + vms_shm_buffer *shmbuffer; struct vms_event_record *events; /* buffer for reading and publishing the event */ vms_event_funcall *ev; diff --git a/include/vamos-buffers/streams/stream-generic.h b/include/vamos-buffers/streams/stream-generic.h index 3db20962..9cc100a8 100644 --- a/include/vamos-buffers/streams/stream-generic.h +++ b/include/vamos-buffers/streams/stream-generic.h @@ -7,7 +7,7 @@ #include "vamos-buffers/core/event.h" #include "vamos-buffers/core/stream.h" -struct buffer; +vms_shm_buffer; /* A generic stream for events stored in shared memory buffer, no filter nor modification of events supported (unless done manually). */ @@ -20,7 +20,7 @@ typedef struct _vms_event_generic { typedef struct _vms_stream_generic { vms_stream base; - struct buffer *shmbuffer; + vms_shm_buffer *shmbuffer; } vms_stream_generic; vms_stream *vms_create_generic_stream(const char *key, const char *name, diff --git a/include/vamos-buffers/streams/stream-regex.h b/include/vamos-buffers/streams/stream-regex.h index 62e5f2c6..8d6245f4 100644 --- a/include/vamos-buffers/streams/stream-regex.h +++ b/include/vamos-buffers/streams/stream-regex.h @@ -13,7 +13,7 @@ typedef struct _vms_event_regex { typedef struct _vms_stream_sregex { vms_stream base; - struct buffer *shmbuffer; + vms_shm_buffer *shmbuffer; } vms_stream_sregex; vms_stream *vms_create_sregex_stream( diff --git a/include/vamos-buffers/streams/stream-regexrw.h b/include/vamos-buffers/streams/stream-regexrw.h index 56f75f24..98caa6f8 100644 --- a/include/vamos-buffers/streams/stream-regexrw.h +++ b/include/vamos-buffers/streams/stream-regexrw.h @@ -13,7 +13,7 @@ typedef struct _vms_event_regexrw { typedef struct _vms_stream_sregexrw { vms_stream base; - struct buffer *shmbuffer; + vms_shm_buffer *shmbuffer; } vms_stream_sregexrw; vms_stream *vms_create_sregexrw_stream(const char *key, const char *name); diff --git a/src/core/stream.c b/src/core/stream.c index 3e2a7469..7fe57af8 100644 --- a/src/core/stream.c +++ b/src/core/stream.c @@ -32,7 +32,7 @@ static vms_stream_hole_handling default_hole_handling = { static uint64_t last_stream_id = 0; -void vms_stream_init(vms_stream *stream, struct buffer *incoming_events_buffer, +void vms_stream_init(vms_stream *stream, vms_shm_buffer *incoming_events_buffer, size_t event_size, vms_stream_is_ready_fn is_ready, vms_stream_filter_fn filter, vms_stream_alter_fn alter, vms_stream_destroy_fn destroy, @@ -155,7 +155,7 @@ vms_stream *vms_stream_create_substream( size_t substream_no = ++stream->substreams_no; char *key = get_sub_buffer_key( buffer_get_key(stream->incoming_events_buffer), substream_no); - struct buffer *shmbuffer = get_shared_buffer(key); + vms_shm_buffer *shmbuffer = get_shared_buffer(key); assert(shmbuffer && "Getting the shm buffer failed"); vms_stream *substream = xalloc(sizeof *substream); diff --git a/src/shmbuf/buffer-aux.c b/src/shmbuf/buffer-aux.c index b343690e..d596bb15 100644 --- a/src/shmbuf/buffer-aux.c +++ b/src/shmbuf/buffer-aux.c @@ -6,13 +6,13 @@ #include "shm.h" HIDE_SYMBOL -void drop_ranges_unlock(struct buffer *buff) { +void drop_ranges_unlock(vms_shm_buffer *buff) { /* FIXME: use explicit memory ordering, seq_cnt is not needed here */ buff->shmbuffer->info.dropped_ranges_lock = false; } HIDE_SYMBOL -void drop_ranges_lock(struct buffer *buff) { +void drop_ranges_lock(vms_shm_buffer *buff) { _Atomic bool *l = &buff->shmbuffer->info.dropped_ranges_lock; bool unlocked; do { @@ -53,7 +53,7 @@ void aux_buffer_destroy(struct aux_buffer *buffer) { } */ -static struct aux_buffer *new_aux_buffer(struct buffer *buff, size_t size) { +static struct aux_buffer *new_aux_buffer(vms_shm_buffer *buff, size_t size) { size_t idx = buff->aux_buf_idx++; const size_t pg_size = sysconf(_SC_PAGESIZE); size = (((size + sizeof(struct aux_buffer)) / pg_size) + 2) * pg_size; @@ -104,8 +104,8 @@ static struct aux_buffer *new_aux_buffer(struct buffer *buff, size_t size) { return ab; } -static inline bool ab_was_dropped(struct aux_buffer *ab, struct buffer *buff) { - struct buffer_info *info = &buff->shmbuffer->info; +static inline bool ab_was_dropped(struct aux_buffer *ab, vms_shm_buffer *buff) { + vms_shm_buffer_info *info = &buff->shmbuffer->info; drop_ranges_lock(buff); for (size_t i = 0; i < DROPPED_RANGES_NUM; ++i) { struct dropped_range *r = &info->dropped_ranges[i]; @@ -122,7 +122,7 @@ static inline bool ab_was_dropped(struct aux_buffer *ab, struct buffer *buff) { } HIDE_SYMBOL -struct aux_buffer *writer_get_aux_buffer(struct buffer *buff, size_t size) { +struct aux_buffer *writer_get_aux_buffer(vms_shm_buffer *buff, size_t size) { if (!buff->cur_aux_buff || aux_buffer_free_space(buff->cur_aux_buff) < size) { /* try to find a free buffer */ @@ -160,7 +160,7 @@ struct aux_buffer *writer_get_aux_buffer(struct buffer *buff, size_t size) { } HIDE_SYMBOL -struct aux_buffer *reader_get_aux_buffer(struct buffer *buff, size_t idx) { +struct aux_buffer *reader_get_aux_buffer(vms_shm_buffer *buff, size_t idx) { /* cache the last use */ if (buff->cur_aux_buff && buff->cur_aux_buff->idx == idx) return buff->cur_aux_buff; diff --git a/src/shmbuf/buffer-dbg.c b/src/shmbuf/buffer-dbg.c index d2ccb9ec..e61d401e 100644 --- a/src/shmbuf/buffer-dbg.c +++ b/src/shmbuf/buffer-dbg.c @@ -8,7 +8,7 @@ /** * @brief The __vms_dbg_buffer class * - * vms_vms_dbg_buffer is a versioned array of key+value records + * vms_shm_dbg_buffer is a versioned array of key+value records */ struct __vms_dbg_buffer { CACHELINE_ALIGNED struct { @@ -30,7 +30,7 @@ struct __vms_dbg_buffer { unsigned char data[]; }; -typedef struct _vms_vms_dbg_buffer { +typedef struct _vms_shm_dbg_buffer { /* shared-memory buffer */ struct __vms_dbg_buffer *buffer; @@ -39,7 +39,7 @@ typedef struct _vms_vms_dbg_buffer { char *key; /* data pointer */ unsigned char *data; /* = buffer->data */ -} vms_vms_dbg_buffer; +} vms_shm_dbg_buffer; static void dbg_buffer_init(struct __vms_dbg_buffer *b, size_t allocation_size, size_t capacity, uint16_t key_size, @@ -52,7 +52,7 @@ static void dbg_buffer_init(struct __vms_dbg_buffer *b, size_t allocation_size, b->info.version = 0; } -vms_vms_dbg_buffer *vms_vms_dbg_buffer_create(const char *key, size_t capacity, +vms_shm_dbg_buffer *vms_shm_dbg_buffer_create(const char *key, size_t capacity, uint16_t key_size, uint16_t value_size) { printf("Initializing dbg buffer '%s' of capacity '%lu'\n", key, capacity); @@ -91,7 +91,7 @@ vms_vms_dbg_buffer *vms_vms_dbg_buffer_create(const char *key, size_t capacity, return NULL; } - vms_vms_dbg_buffer *buff = (vms_vms_dbg_buffer *)xalloc(sizeof(*buff)); + vms_shm_dbg_buffer *buff = (vms_shm_dbg_buffer *)xalloc(sizeof(*buff)); buff->buffer = (struct __vms_dbg_buffer *)mem; dbg_buffer_init(buff->buffer, allocation_size, capacity, key_size, value_size); @@ -119,7 +119,7 @@ vms_vms_dbg_buffer *vms_vms_dbg_buffer_create(const char *key, size_t capacity, return buff; } -vms_vms_dbg_buffer *vms_vms_dbg_buffer_get(const char *key) { +vms_shm_dbg_buffer *vms_shm_dbg_buffer_get(const char *key) { printf("Getting dbg buffer '%s'\n", key); int fd = vms_shm_open(key, O_RDWR | O_CREAT, S_IRWXU); @@ -150,7 +150,7 @@ vms_vms_dbg_buffer *vms_vms_dbg_buffer_get(const char *key) { return NULL; } - vms_vms_dbg_buffer *buff = (vms_vms_dbg_buffer *)xalloc(sizeof(*buff)); + vms_shm_dbg_buffer *buff = (vms_shm_dbg_buffer *)xalloc(sizeof(*buff)); buff->buffer = (struct __vms_dbg_buffer *)mem; buff->data = buff->buffer->data; buff->key = xstrdup(key); @@ -159,7 +159,7 @@ vms_vms_dbg_buffer *vms_vms_dbg_buffer_get(const char *key) { return buff; } -void vms_vms_dbg_buffer_release(vms_vms_dbg_buffer *buff) { +void vms_shm_dbg_buffer_release(vms_shm_dbg_buffer *buff) { if (close(buff->fd) == -1) { perror("closing fd when releasing dbg buffer"); } @@ -171,41 +171,41 @@ void vms_vms_dbg_buffer_release(vms_vms_dbg_buffer *buff) { free(buff); } -void vms_vms_dbg_buffer_destroy(vms_vms_dbg_buffer *buff) { +void vms_shm_dbg_buffer_destroy(vms_shm_dbg_buffer *buff) { if (vms_shm_unlink(buff->key) != 0) { perror("vms_unlink when destroying a dbg buffer"); } - vms_vms_dbg_buffer_release(buff); + vms_shm_dbg_buffer_release(buff); } -size_t vms_vms_dbg_buffer_size(vms_vms_dbg_buffer *b) { +size_t vms_shm_dbg_buffer_size(vms_shm_dbg_buffer *b) { return b->buffer->info.size; } -size_t vms_vms_dbg_buffer_capacity(vms_vms_dbg_buffer *b) { +size_t vms_shm_dbg_buffer_capacity(vms_shm_dbg_buffer *b) { return b->buffer->info.capacity; } -size_t vms_vms_dbg_buffer_key_size(vms_vms_dbg_buffer *b) { +size_t vms_shm_dbg_buffer_key_size(vms_shm_dbg_buffer *b) { return b->buffer->info.key_size; } -size_t vms_vms_dbg_buffer_value_size(vms_vms_dbg_buffer *b) { +size_t vms_shm_dbg_buffer_value_size(vms_shm_dbg_buffer *b) { return b->buffer->info.value_size; } -size_t vms_vms_dbg_buffer_rec_size(vms_vms_dbg_buffer *b) { +size_t vms_shm_dbg_buffer_rec_size(vms_shm_dbg_buffer *b) { return b->buffer->info.key_size + b->buffer->info.value_size; } -unsigned char *vms_vms_dbg_buffer_data(vms_vms_dbg_buffer *b) { +unsigned char *vms_shm_dbg_buffer_data(vms_shm_dbg_buffer *b) { return b->data; } -void vms_vms_dbg_buffer_inc_size(vms_vms_dbg_buffer *b, size_t size) { +void vms_shm_dbg_buffer_inc_size(vms_shm_dbg_buffer *b, size_t size) { b->buffer->info.size += size; } -size_t vms_vms_dbg_buffer_version(vms_vms_dbg_buffer *b) { +size_t vms_shm_dbg_buffer_version(vms_shm_dbg_buffer *b) { return b->buffer->info.version; } -void vms_vms_dbg_buffer_bump_version(vms_vms_dbg_buffer *b) { +void vms_shm_dbg_buffer_bump_version(vms_shm_dbg_buffer *b) { ++b->buffer->info.version; } diff --git a/src/shmbuf/buffer-local.c b/src/shmbuf/buffer-local.c index da411086..041cb724 100644 --- a/src/shmbuf/buffer-local.c +++ b/src/shmbuf/buffer-local.c @@ -22,9 +22,9 @@ #include "vamos-buffers/shmbuf/buffer.h" /* FOR TESTING */ -struct buffer *initialize_local_buffer(const char *key, size_t elem_size, - size_t capacity, - struct vms_source_control *control) { +vms_shm_buffer *initialize_local_buffer(const char *key, size_t elem_size, + size_t capacity, + struct vms_source_control *control) { assert(elem_size > 0 && "Element size is 0"); printf("Initializing LOCAL buffer '%s' with elem size '%lu'\n", key, elem_size); @@ -36,14 +36,14 @@ struct buffer *initialize_local_buffer(const char *key, size_t elem_size, return NULL; } - struct buffer *buff = malloc(sizeof(struct buffer)); + vms_shm_buffer *buff = malloc(sizeof(vms_shm_buffer)); assert(buff && "Memory allocation failed"); buff->shmbuffer = (struct shmbuffer *)mem; assert(ADDR_IS_CACHE_ALIGNED(buff->shmbuffer->data)); assert(ADDR_IS_CACHE_ALIGNED(&buff->shmbuffer->info.ringbuf)); - memset(buff->shmbuffer, 0, sizeof(struct buffer_info)); + memset(buff->shmbuffer, 0, sizeof(vms_shm_buffer_info)); /* ringbuf has one dummy element */ buff->shmbuffer->info.capacity = capacity; @@ -69,7 +69,7 @@ struct buffer *initialize_local_buffer(const char *key, size_t elem_size, return buff; } -void release_local_buffer(struct buffer *buff) { +void release_local_buffer(vms_shm_buffer *buff) { free(buff->key); size_t vecsize = VEC_SIZE(buff->aux_buffers); diff --git a/src/shmbuf/buffer-private.h b/src/shmbuf/buffer-private.h index cdbf188c..7c81af41 100644 --- a/src/shmbuf/buffer-private.h +++ b/src/shmbuf/buffer-private.h @@ -13,7 +13,7 @@ struct vms_source_control; struct vms_event_record; -struct buffer; +typedef struct _vms_shm_buffer vms_shm_buffer; #define MAX_AUX_BUF_KEY_SIZE 16 #define DROPPED_RANGES_NUM 5 @@ -30,7 +30,7 @@ struct dropped_range { vms_eventid end; }; -struct buffer_info { +typedef struct _vms_shm_buffer_info { vms_spsc_ringbuf ringbuf; size_t allocated_size; @@ -45,10 +45,10 @@ struct buffer_info { /* the monitored program exited/destroyed the buffer */ volatile _Bool destroyed; volatile _Bool monitor_attached; -} __attribute__((aligned(CACHELINE_SIZE))); +} vms_shm_buffer_info __attribute__((aligned(CACHELINE_SIZE))); struct shmbuffer { - struct buffer_info info; + vms_shm_buffer_info info; /* pointer to the beginning of data */ unsigned char data[]; }; @@ -66,7 +66,7 @@ struct aux_buffer { /* TODO: cache the shared state in local state (e.g., elem_size, etc.). Maybe we could also inline the vms_spsc_ringbuf so that we can keep local cache */ -struct buffer { +typedef struct _vms_shm_buffer { struct shmbuffer *shmbuffer; struct vms_source_control *control; /* shared memory of auxiliary buffer */ @@ -83,26 +83,26 @@ struct buffer { mode_t mode; /* The number of the last subbufer */ _Atomic size_t last_subbufer_no; -}; +} vms_shm_buffer; #define _ringbuf(buff) (&buff->shmbuffer->info.ringbuf) -struct buffer *initialize_shared_buffer(const char *key, mode_t mode, - size_t elem_size, size_t capacity, - struct vms_source_control *control); +vms_shm_buffer *initialize_shared_buffer(const char *key, mode_t mode, + size_t elem_size, size_t capacity, + struct vms_source_control *control); -struct buffer *get_shared_buffer(const char *key); -struct buffer *try_get_shared_buffer(const char *key, size_t retry); +vms_shm_buffer *get_shared_buffer(const char *key); +vms_shm_buffer *try_get_shared_buffer(const char *key, size_t retry); size_t compute_vms_size(size_t elem_size, size_t capacity); size_t compute_vms_buffer_size(size_t nondata_size, size_t elem_size, size_t capacity); /*** LOCAL buffers ***/ -struct buffer *initialize_local_buffer(const char *key, size_t elem_size, - size_t capacity, - struct vms_source_control *control); -void release_local_buffer(struct buffer *buff); +vms_shm_buffer *initialize_local_buffer(const char *key, size_t elem_size, + size_t capacity, + struct vms_source_control *control); +void release_local_buffer(vms_shm_buffer *buff); /*** CONTROL buffers ***/ struct vms_source_control *get_shared_control_buffer(const char *buff_key); @@ -116,10 +116,10 @@ void destroy_shared_control_buffer(const char *buffkey, /*** AUX buffers ***/ size_t aux_buffer_free_space(struct aux_buffer *buff); void aux_buffer_release(struct aux_buffer *buffer); -struct aux_buffer *writer_get_aux_buffer(struct buffer *buff, size_t size); -struct aux_buffer *reader_get_aux_buffer(struct buffer *buff, size_t idx); +struct aux_buffer *writer_get_aux_buffer(vms_shm_buffer *buff, size_t size); +struct aux_buffer *reader_get_aux_buffer(vms_shm_buffer *buff, size_t idx); -void drop_ranges_lock(struct buffer *buff); -void drop_ranges_unlock(struct buffer *buff); +void drop_ranges_lock(vms_shm_buffer *buff); +void drop_ranges_unlock(vms_shm_buffer *buff); #endif /* VAMOS_SHM_BUFFER_PRIVATE_H */ diff --git a/src/shmbuf/buffer-sub.c b/src/shmbuf/buffer-sub.c index d0ac9b74..5f829138 100644 --- a/src/shmbuf/buffer-sub.c +++ b/src/shmbuf/buffer-sub.c @@ -18,8 +18,8 @@ char *get_sub_buffer_key(const char *key, size_t idx) { return tmp; } -struct buffer *create_shared_sub_buffer( - struct buffer *buffer, size_t capacity, +vms_shm_buffer *create_shared_sub_buffer( + vms_shm_buffer *buffer, size_t capacity, const struct vms_source_control *control) { char *key = get_sub_buffer_key(buffer->key, ++buffer->last_subbufer_no); struct vms_source_control *ctrl = @@ -32,7 +32,7 @@ struct buffer *create_shared_sub_buffer( size_t elem_size = vms_source_control_max_event_size(ctrl); if (capacity == 0) capacity = buffer_capacity(buffer); - struct buffer *sbuf = + vms_shm_buffer *sbuf = initialize_shared_buffer(key, S_IRWXU, elem_size, capacity, ctrl); /* XXX: we copy the key in 'initialize_shared_buffer' which is redundant as * we have created it in `get_sub_buffer_key` and can just move it */ @@ -43,7 +43,7 @@ struct buffer *create_shared_sub_buffer( return sbuf; } -size_t buffer_get_sub_buffers_no(struct buffer *buffer) { +size_t buffer_get_sub_buffers_no(vms_shm_buffer *buffer) { return buffer->shmbuffer->info.subbuffers_no; } @@ -56,12 +56,12 @@ size_t buffer_get_sub_buffers_no(struct buffer *buffer) { * therefore destroying the buffer entirely would lead to accessing * dangling data. */ -void buffer_set_destroyed(struct buffer *buff) { +void buffer_set_destroyed(vms_shm_buffer *buff) { buff->shmbuffer->info.destroyed = 1; } /* for writers */ -void destroy_shared_sub_buffer(struct buffer *buff) { +void destroy_shared_sub_buffer(vms_shm_buffer *buff) { buff->shmbuffer->info.destroyed = 1; size_t vecsize = VEC_SIZE(buff->aux_buffers); @@ -90,7 +90,7 @@ void destroy_shared_sub_buffer(struct buffer *buff) { } /* for readers */ -void release_shared_sub_buffer(struct buffer *buff) { +void release_shared_sub_buffer(vms_shm_buffer *buff) { if (munmap(buff->shmbuffer, buff->shmbuffer->info.allocated_size) != 0) { perror("release_shared_sub_buffer: munmap failure"); } diff --git a/src/shmbuf/buffer.c b/src/shmbuf/buffer.c index 704e25bf..99c52031 100644 --- a/src/shmbuf/buffer.c +++ b/src/shmbuf/buffer.c @@ -23,29 +23,29 @@ #include "vamos-buffers/core/utils.h" #include "vamos-buffers/core/vector-macro.h" -bool buffer_is_ready(struct buffer *buff) { +bool buffer_is_ready(vms_shm_buffer *buff) { return !buff->shmbuffer->info.destroyed; } -bool buffer_monitor_attached(struct buffer *buff) { +bool buffer_monitor_attached(vms_shm_buffer *buff) { return buff->shmbuffer->info.monitor_attached; } -size_t buffer_capacity(struct buffer *buff) { +size_t buffer_capacity(vms_shm_buffer *buff) { return buff->shmbuffer->info.capacity; } -size_t buffer_size(struct buffer *buff) { +size_t buffer_size(vms_shm_buffer *buff) { return vms_spsc_ringbuf_size(_ringbuf(buff)); } -size_t buffer_elem_size(struct buffer *buff) { +size_t buffer_elem_size(vms_shm_buffer *buff) { return buff->shmbuffer->info.elem_size; } -const char *buffer_get_key(struct buffer *buffer) { return buffer->key; } +const char *buffer_get_key(vms_shm_buffer *buffer) { return buffer->key; } -int buffer_get_key_path(struct buffer *buff, char keypath[], +int buffer_get_key_path(vms_shm_buffer *buff, char keypath[], size_t keypathsize) { if (SHM_NAME_MAXLEN <= keypathsize) return -1; @@ -57,7 +57,7 @@ int buffer_get_key_path(struct buffer *buff, char keypath[], return 0; } -int buffer_get_ctrl_key_path(struct buffer *buff, char keypath[], +int buffer_get_ctrl_key_path(vms_shm_buffer *buff, char keypath[], size_t keypathsize) { if (SHM_NAME_MAXLEN <= keypathsize) return -1; @@ -82,9 +82,9 @@ int buffer_get_ctrl_key_path(struct buffer *buff, char keypath[], ((unsigned char *)b->data + (b->info.elem_size * (b->info.capacity + 1))) HIDE_SYMBOL -struct buffer *initialize_shared_buffer(const char *key, mode_t mode, - size_t elem_size, size_t capacity, - struct vms_source_control *control) { +vms_shm_buffer *initialize_shared_buffer(const char *key, mode_t mode, + size_t elem_size, size_t capacity, + struct vms_source_control *control) { assert(elem_size > 0 && "Element size is 0"); assert(capacity > 0 && "Capacity is 0"); /* the ringbuffer has one unusable dummy element, so increase the capacity @@ -132,12 +132,12 @@ struct buffer *initialize_shared_buffer(const char *key, mode_t mode, return NULL; } - struct buffer *buff = xalloc(sizeof(struct buffer)); + vms_shm_buffer *buff = xalloc(sizeof(vms_shm_buffer)); buff->shmbuffer = (struct shmbuffer *)shmem; assert(ADDR_IS_CACHE_ALIGNED(buff->shmbuffer->data)); assert(ADDR_IS_CACHE_ALIGNED(&buff->shmbuffer->info.ringbuf)); - memset(buff->shmbuffer, 0, sizeof(struct buffer_info)); + memset(buff->shmbuffer, 0, sizeof(vms_shm_buffer_info)); buff->shmbuffer->info.allocated_size = memsize; buff->shmbuffer->info.capacity = capacity; @@ -193,8 +193,8 @@ struct buffer *initialize_shared_buffer(const char *key, mode_t mode, return buff; } -struct buffer *create_shared_buffer(const char *key, size_t capacity, - const struct vms_source_control *control) { +vms_shm_buffer *create_shared_buffer(const char *key, size_t capacity, + const struct vms_source_control *control) { struct vms_source_control *ctrl = create_shared_control_buffer(key, S_IRWXU, control); if (!ctrl) { @@ -206,7 +206,7 @@ struct buffer *create_shared_buffer(const char *key, size_t capacity, return initialize_shared_buffer(key, S_IRWXU, elem_size, capacity, ctrl); } -struct buffer *create_shared_buffer_adv( +vms_shm_buffer *create_shared_buffer_adv( const char *key, mode_t mode, size_t elem_size, size_t capacity, const struct vms_source_control *control) { struct vms_source_control *ctrl = @@ -227,7 +227,7 @@ struct buffer *create_shared_buffer_adv( return initialize_shared_buffer(key, mode, elem_size, capacity, ctrl); } -struct buffer *try_get_shared_buffer(const char *key, size_t retry) { +vms_shm_buffer *try_get_shared_buffer(const char *key, size_t retry) { // fprintf(stderr, "getting shared buffer '%s'\n", key); int fd = -1; @@ -246,7 +246,7 @@ struct buffer *try_get_shared_buffer(const char *key, size_t retry) { return NULL; } - struct buffer_info info; + vms_shm_buffer_info info; if (pread(fd, &info, sizeof(info), 0) == -1) { perror("reading info of shared buffer"); close(fd); @@ -268,7 +268,7 @@ struct buffer *try_get_shared_buffer(const char *key, size_t retry) { goto before_mmap_clean; } - struct buffer *buff = malloc(sizeof(*buff)); + vms_shm_buffer *buff = malloc(sizeof(*buff)); if (!buff) { fprintf(stderr, "%s:%d: memory allocation failed\n", __func__, __LINE__); @@ -316,12 +316,12 @@ struct buffer *try_get_shared_buffer(const char *key, size_t retry) { return NULL; } -struct buffer *get_shared_buffer(const char *key) { +vms_shm_buffer *get_shared_buffer(const char *key) { return try_get_shared_buffer(key, 10); } -struct vms_event_record *buffer_get_avail_events(struct buffer *buff, - size_t *evs_num) { +struct vms_event_record *buffer_get_avail_events(vms_shm_buffer *buff, + size_t *evs_num) { assert(buff); assert(evs_num); assert(buff->control); @@ -330,20 +330,20 @@ struct vms_event_record *buffer_get_avail_events(struct buffer *buff, return buff->control->events; } -void buffer_set_attached(struct buffer *buff, bool val) { +void buffer_set_attached(vms_shm_buffer *buff, bool val) { if (!buff->shmbuffer->info.destroyed) buff->shmbuffer->info.monitor_attached = val; } /* set the ID of the last processed event */ -void buffer_set_last_processed_id(struct buffer *buff, vms_eventid id) { +void buffer_set_last_processed_id(vms_shm_buffer *buff, vms_eventid id) { assert(buff->shmbuffer->info.last_processed_id <= id && "The IDs are not monotonic"); buff->shmbuffer->info.last_processed_id = id; } /* for readers */ -void release_shared_buffer(struct buffer *buff) { +void release_shared_buffer(vms_shm_buffer *buff) { if (munmap(buff->shmbuffer, buff->shmbuffer->info.allocated_size) != 0) { perror("release_shared_buffer: munmap failure"); } @@ -365,7 +365,7 @@ void release_shared_buffer(struct buffer *buff) { } /* for writers */ -void destroy_shared_buffer(struct buffer *buff) { +void destroy_shared_buffer(vms_shm_buffer *buff) { buff->shmbuffer->info.destroyed = 1; size_t vecsize = VEC_SIZE(buff->aux_buffers); @@ -396,8 +396,8 @@ void destroy_shared_buffer(struct buffer *buff) { free(buff); } -void *buffer_read_pointer(struct buffer *buff, size_t *size) { - struct buffer_info *info = &buff->shmbuffer->info; +void *buffer_read_pointer(vms_shm_buffer *buff, size_t *size) { + vms_shm_buffer_info *info = &buff->shmbuffer->info; size_t tail = vms_spsc_ringbuf_read_off_nowrap(&info->ringbuf, size); if (*size == 0) return NULL; @@ -406,11 +406,11 @@ void *buffer_read_pointer(struct buffer *buff, size_t *size) { return buff->shmbuffer->data + tail * info->elem_size; } -bool buffer_drop_k(struct buffer *buff, size_t k) { +bool buffer_drop_k(vms_shm_buffer *buff, size_t k) { return vms_spsc_ringbuf_consume_upto(_ringbuf(buff), k) == k; } -size_t buffer_consume(struct buffer *buff, size_t k) { +size_t buffer_consume(vms_shm_buffer *buff, size_t k) { return vms_spsc_ringbuf_consume_upto(_ringbuf(buff), k); } @@ -430,8 +430,8 @@ size_t buffer_consume(struct buffer *buff, size_t k) { * multiple steps. */ -void *buffer_start_push(struct buffer *buff) { - struct buffer_info *info = &buff->shmbuffer->info; +void *buffer_start_push(vms_shm_buffer *buff) { + vms_shm_buffer_info *info = &buff->shmbuffer->info; assert(!info->destroyed && "Writing to a destroyed buffer"); size_t n; @@ -450,7 +450,7 @@ void *buffer_start_push(struct buffer *buff) { return mem; } -void *buffer_partial_push(struct buffer *buff, void *prev_push, +void *buffer_partial_push(vms_shm_buffer *buff, void *prev_push, const void *elem, size_t size) { assert(buffer_is_ready(buff) && "Writing to a destroyed buffer"); assert(BUFF_START(buff->shmbuffer) <= (unsigned char *)prev_push); @@ -462,14 +462,14 @@ void *buffer_partial_push(struct buffer *buff, void *prev_push, return (unsigned char *)prev_push + size; } -static size_t _buffer_push_strn(struct buffer *buff, const void *data, +static size_t _buffer_push_strn(vms_shm_buffer *buff, const void *data, size_t size); -static uint64_t buffer_push_str(struct buffer *buff, uint64_t evid, +static uint64_t buffer_push_str(vms_shm_buffer *buff, uint64_t evid, const char *str); -static uint64_t buffer_push_strn(struct buffer *buff, uint64_t evid, +static uint64_t buffer_push_strn(vms_shm_buffer *buff, uint64_t evid, const char *str, size_t len); -void *buffer_partial_push_str(struct buffer *buff, void *prev_push, +void *buffer_partial_push_str(vms_shm_buffer *buff, void *prev_push, uint64_t evid, const char *str) { assert(!buff->shmbuffer->info.destroyed && "Writing to a destroyed buffer"); assert(BUFF_START(buff->shmbuffer) <= (unsigned char *)prev_push); @@ -480,7 +480,7 @@ void *buffer_partial_push_str(struct buffer *buff, void *prev_push, return (unsigned char *)prev_push + sizeof(uint64_t); } -void *buffer_partial_push_str_n(struct buffer *buff, void *prev_push, +void *buffer_partial_push_str_n(vms_shm_buffer *buff, void *prev_push, uint64_t evid, const char *str, size_t len) { assert(!buff->shmbuffer->info.destroyed && "Writing to a destroyed buffer"); assert(BUFF_START(buff->shmbuffer) <= (unsigned char *)prev_push); @@ -491,12 +491,12 @@ void *buffer_partial_push_str_n(struct buffer *buff, void *prev_push, return (unsigned char *)prev_push + sizeof(uint64_t); } -void buffer_finish_push(struct buffer *buff) { +void buffer_finish_push(vms_shm_buffer *buff) { assert(!buff->shmbuffer->info.destroyed && "Writing to a destroyed buffer"); vms_spsc_ringbuf_write_finish(_ringbuf(buff), 1); } -bool buffer_push(struct buffer *buff, const void *elem, size_t size) { +bool buffer_push(vms_shm_buffer *buff, const void *elem, size_t size) { assert(!buff->shmbuffer->info.destroyed && "Writing to a destroyed buffer"); assert(buff->shmbuffer->info.elem_size >= size && "Size does not fit the slot"); @@ -511,7 +511,7 @@ bool buffer_push(struct buffer *buff, const void *elem, size_t size) { return true; } -bool buffer_pop(struct buffer *buff, void *dst) { +bool buffer_pop(vms_shm_buffer *buff, void *dst) { assert(!buff->shmbuffer->info.destroyed && "Reading from a destroyed buffer"); @@ -526,7 +526,7 @@ bool buffer_pop(struct buffer *buff, void *dst) { return false; } -size_t _buffer_push_strn(struct buffer *buff, const void *data, size_t size) { +size_t _buffer_push_strn(vms_shm_buffer *buff, const void *data, size_t size) { struct aux_buffer *ab = writer_get_aux_buffer(buff, size); assert(ab); assert(ab == buff->cur_aux_buff); @@ -540,21 +540,21 @@ size_t _buffer_push_strn(struct buffer *buff, const void *data, size_t size) { return off; } -void *buffer_get_str(struct buffer *buff, uint64_t elem) { +void *buffer_get_str(vms_shm_buffer *buff, uint64_t elem) { size_t idx = elem >> 32; struct aux_buffer *ab = reader_get_aux_buffer(buff, idx); size_t off = elem & 0xffffffff; return ab->data + off; } -uint64_t buffer_push_str(struct buffer *buff, uint64_t evid, const char *str) { +uint64_t buffer_push_str(vms_shm_buffer *buff, uint64_t evid, const char *str) { size_t len = strlen(str) + 1; size_t off = buffer_push_strn(buff, evid, str, len); assert(buff->cur_aux_buff); return (off | (buff->cur_aux_buff->idx << 32)); } -uint64_t buffer_push_strn(struct buffer *buff, uint64_t evid, const char *str, +uint64_t buffer_push_strn(vms_shm_buffer *buff, uint64_t evid, const char *str, size_t len) { size_t off = _buffer_push_strn(buff, str, len); struct aux_buffer *ab = buff->cur_aux_buff; @@ -565,9 +565,9 @@ uint64_t buffer_push_strn(struct buffer *buff, uint64_t evid, const char *str, return (off | (buff->cur_aux_buff->idx << 32)); } -void buffer_notify_dropped(struct buffer *buff, uint64_t begin_id, +void buffer_notify_dropped(vms_shm_buffer *buff, uint64_t begin_id, uint64_t end_id) { - struct buffer_info *info = &buff->shmbuffer->info; + vms_shm_buffer_info *info = &buff->shmbuffer->info; size_t idx = info->dropped_ranges_next; struct dropped_range *r = &info->dropped_ranges[idx]; if (r->begin == begin_id || r->end == r->begin - 1) { @@ -591,7 +591,7 @@ void buffer_notify_dropped(struct buffer *buff, uint64_t begin_id, drop_ranges_unlock(buff); } -int buffer_register_event(struct buffer *b, const char *name, uint64_t kind) { +int buffer_register_event(vms_shm_buffer *b, const char *name, uint64_t kind) { assert(kind > vms_event_get_last_special_kind() && "Invalid event kind, it overlaps special kinds"); struct vms_event_record *rec = vms_source_control_get_event(b->control, name); if (rec == NULL) { @@ -602,7 +602,7 @@ int buffer_register_event(struct buffer *b, const char *name, uint64_t kind) { return 0; } -int buffer_register_events(struct buffer *b, size_t ev_nums, ...) { +int buffer_register_events(vms_shm_buffer *b, size_t ev_nums, ...) { va_list ap; va_start(ap, ev_nums); @@ -621,7 +621,7 @@ int buffer_register_events(struct buffer *b, size_t ev_nums, ...) { return 0; } -int buffer_register_all_events(struct buffer *b) { +int buffer_register_all_events(vms_shm_buffer *b) { struct vms_event_record *recs = b->control->events; const size_t ev_nums = vms_source_control_get_records_num(b->control); diff --git a/src/shmbuf/client.c b/src/shmbuf/client.c index 806f35f3..2835ac79 100644 --- a/src/shmbuf/client.c +++ b/src/shmbuf/client.c @@ -25,7 +25,7 @@ static inline void restore_sigfunc(void (*sigfunc)(int)) { } } -int buffer_wait_for_monitor(struct buffer *buff) { +int buffer_wait_for_monitor(vms_shm_buffer *buff) { void (*sigfunc)(int); sigfunc = signal(SIGINT, sig_int); int err = 0; diff --git a/src/streams/stream-drregex.c b/src/streams/stream-drregex.c index eecd71fa..a7ab02db 100644 --- a/src/streams/stream-drregex.c +++ b/src/streams/stream-drregex.c @@ -9,7 +9,7 @@ #include "vamos-buffers/shmbuf/buffer.h" bool drregex_is_ready(vms_stream *stream) { - struct buffer *b = ((vms_stream_drregex *)stream)->shmbuffer; + vms_shm_buffer *b = ((vms_stream_drregex *)stream)->shmbuffer; /* buffer must be ready or it may not be ready anymore, but it * still has some data that we haven't read */ return buffer_is_ready(b) || buffer_size(b) > 0; @@ -21,7 +21,7 @@ void drregex_alter(vms_stream *stream, vms_event *in, vms_event *out) { vms_stream *vms_create_drregex_stream(const char *key, const char *name) { vms_stream_drregex *ss = malloc(sizeof *ss); - struct buffer *shmbuffer = get_shared_buffer(key); + vms_shm_buffer *shmbuffer = get_shared_buffer(key); assert(shmbuffer && "Getting the shm buffer failed"); size_t elem_size = buffer_elem_size(shmbuffer); assert(elem_size > 0); diff --git a/src/streams/stream-funs.c b/src/streams/stream-funs.c index 339e0e01..47ab9552 100644 --- a/src/streams/stream-funs.c +++ b/src/streams/stream-funs.c @@ -9,7 +9,7 @@ #include "vamos-buffers/shmbuf/buffer.h" bool funs_is_ready(vms_stream *stream) { - struct buffer *b = ((vms_stream_funs *)stream)->shmbuffer; + vms_shm_buffer *b = ((vms_stream_funs *)stream)->shmbuffer; /* buffer must be ready or it may not be ready anymore, but it * still has some data that we haven't read */ return buffer_is_ready(b) || buffer_size(b) > 0; @@ -19,12 +19,12 @@ void funs_alter(vms_stream *stream, vms_event *in, vms_event *out) { memcpy(out, in, stream->event_size); } -size_t stream_mk_event_kinds(const char *stream_name, struct buffer *shmbuffer, +size_t stream_mk_event_kinds(const char *stream_name, vms_shm_buffer *shmbuffer, size_t *max_ev_size); vms_stream *vms_create_funs_stream(const char *key, const char *name) { vms_stream_funs *ss = malloc(sizeof *ss); - struct buffer *shmbuffer = get_shared_buffer(key); + vms_shm_buffer *shmbuffer = get_shared_buffer(key); assert(shmbuffer && "Getting the shm buffer failed"); size_t elem_size = buffer_elem_size(shmbuffer); assert(elem_size > 0); diff --git a/src/streams/stream-generic.c b/src/streams/stream-generic.c index e5fea9dd..136ea760 100644 --- a/src/streams/stream-generic.c +++ b/src/streams/stream-generic.c @@ -12,7 +12,7 @@ no filter nor modification of events supported (unless done manually). */ static bool generic_is_ready(vms_stream *stream) { - struct buffer *b = stream->incoming_events_buffer; + vms_shm_buffer *b = stream->incoming_events_buffer; /* buffer must be ready or it may not be ready anymore, but it * still has some data that we haven't read */ return buffer_is_ready(b) || buffer_size(b) > 0; @@ -25,7 +25,7 @@ static void generic_alter(vms_stream *stream, vms_event *in, vms_event *out) { vms_stream *vms_create_generic_stream(const char *key, const char *name, vms_stream_hole_handling *hole_handling) { vms_stream_generic *ss = malloc(sizeof *ss); - struct buffer *shmbuffer = get_shared_buffer(key); + vms_shm_buffer *shmbuffer = get_shared_buffer(key); assert(shmbuffer && "Getting the shm buffer failed"); size_t elem_size = buffer_elem_size(shmbuffer); assert(elem_size > 0); diff --git a/src/streams/stream-regex.c b/src/streams/stream-regex.c index fa868e2b..0d97a3e1 100644 --- a/src/streams/stream-regex.c +++ b/src/streams/stream-regex.c @@ -9,7 +9,7 @@ #include "vamos-buffers/shmbuf/buffer.h" bool sregex_is_ready(vms_stream *stream) { - struct buffer *b = ((vms_stream_sregex *)stream)->shmbuffer; + vms_shm_buffer *b = ((vms_stream_sregex *)stream)->shmbuffer; /* buffer must be ready or it may not be ready anymore, but it * still has some data that we haven't read */ return buffer_is_ready(b) || buffer_size(b) > 0; @@ -23,7 +23,7 @@ vms_stream *vms_create_sregex_stream( const char *key, const char *name, const vms_stream_hole_handling *hole_handling) { vms_stream_sregex *ss = malloc(sizeof *ss); - struct buffer *shmbuffer = get_shared_buffer(key); + vms_shm_buffer *shmbuffer = get_shared_buffer(key); assert(shmbuffer && "Getting the shm buffer failed"); size_t elem_size = buffer_elem_size(shmbuffer); assert(elem_size > 0); diff --git a/src/streams/stream-regexrw.c b/src/streams/stream-regexrw.c index c6d91b75..74350992 100644 --- a/src/streams/stream-regexrw.c +++ b/src/streams/stream-regexrw.c @@ -9,7 +9,7 @@ #include "vamos-buffers/core/arbiter.h" bool sregexrw_is_ready(vms_stream *stream) { - struct buffer *b = ((vms_stream_sregexrw *)stream)->shmbuffer; + vms_shm_buffer *b = ((vms_stream_sregexrw *)stream)->shmbuffer; /* buffer must be ready or it may not be ready anymore, but it * still has some data that we haven't read */ return buffer_is_ready(b) || buffer_size(b) > 0; @@ -21,7 +21,7 @@ void sregexrw_alter(vms_stream *stream, vms_event *in, vms_event *out) { vms_stream *vms_create_sregexrw_stream(const char *key, const char *name) { vms_stream_sregexrw *ss = malloc(sizeof *ss); - struct buffer *shmbuffer = get_shared_buffer(key); + vms_shm_buffer *shmbuffer = get_shared_buffer(key); assert(shmbuffer && "Getting the shm buffer failed"); size_t elem_size = buffer_elem_size(shmbuffer); assert(elem_size > 0); diff --git a/tests/arbiter-buffer.c b/tests/arbiter-buffer.c index 94b95642..9bd705bf 100644 --- a/tests/arbiter-buffer.c +++ b/tests/arbiter-buffer.c @@ -21,12 +21,12 @@ struct event { int i; }; -struct buffer *initialize_local_buffer(const char *key, size_t elem_size, - size_t capacity, - struct vms_source_control *control); +vms_shm_buffer *initialize_local_buffer(const char *key, size_t elem_size, + size_t capacity, + struct vms_source_control *control); int main(void) { - struct buffer *lbuffer = + vms_shm_buffer *lbuffer = initialize_local_buffer("/dummy", sizeof(struct event), 30, NULL); assert(lbuffer); diff --git a/tests/bench/ringbufs.c b/tests/bench/ringbufs.c index 62b8f997..a60b5935 100644 --- a/tests/bench/ringbufs.c +++ b/tests/bench/ringbufs.c @@ -31,7 +31,7 @@ static double report_time(const char *msg, struct timespec *start, #if 0 static void run_shmbuf_push_pop_st() { struct vms_source_control *ctrl = vms_source_control_define(1, "dummy", "i"); - struct buffer *buff = create_shared_buffer("/test", sizeof(int), ctrl); + vms_shm_buffer *buff = create_shared_buffer("/test", sizeof(int), ctrl); assert(buff); assert(buffer_capacity(buff) >= N); @@ -64,13 +64,13 @@ static void run_shmbuf_push_pop_st() { /* to see if there is any difference between malloce'd and shared memory */ -struct buffer *initialize_local_buffer(const char *key, size_t elem_size, +vms_shm_buffer *initialize_local_buffer(const char *key, size_t elem_size, struct vms_source_control *control); -void release_local_buffer(struct buffer *buff); +void release_local_buffer(vms_shm_buffer *buff); static void run_local_shmbuf_push_pop_st() { struct vms_source_control *ctrl = vms_source_control_define(1, "dummy", "i"); - struct buffer *buff = initialize_local_buffer("/test", sizeof(int), ctrl); + vms_shm_buffer *buff = initialize_local_buffer("/test", sizeof(int), ctrl); assert(buff); diff --git a/tests/buffer-test.c b/tests/buffer-test.c index 5567d3b7..1ece2ec2 100644 --- a/tests/buffer-test.c +++ b/tests/buffer-test.c @@ -17,7 +17,7 @@ int main(void) { ctrl->events[0].name[0] = '\0'; ctrl->events[0].signature[0] = '\0'; - struct buffer *b = create_shared_buffer("/testkey", 128, ctrl); + vms_shm_buffer *b = create_shared_buffer("/testkey", 128, ctrl); assert(b); assert(buffer_elem_size(b) >= sizeof(size_t)); free(ctrl); diff --git a/tests/fetch-test-2.c b/tests/fetch-test-2.c index df2c7ef8..4b93dfa3 100644 --- a/tests/fetch-test-2.c +++ b/tests/fetch-test-2.c @@ -26,7 +26,7 @@ struct event { static size_t failed_push = 0; void* filler_thread(void* data) { - struct buffer* buffer = (struct buffer*)data; + vms_shm_buffer* buffer = (vms_shm_buffer*)data; struct event ev; ev.base.kind = vms_event_get_last_special_kind() + 1; for (size_t i = 1; i <= 10000; ++i) { @@ -81,7 +81,7 @@ void* reader_thread(void* data) { } int main(void) { - struct buffer* buffer = + vms_shm_buffer* buffer = initialize_local_buffer("/dummy", sizeof(struct event), 30, NULL); assert(buffer); vms_stream dummy_stream; diff --git a/tests/fetch-test-3.c b/tests/fetch-test-3.c index 72cb7c5a..54ea1e3d 100644 --- a/tests/fetch-test-3.c +++ b/tests/fetch-test-3.c @@ -26,7 +26,7 @@ struct event { static size_t failed_push = 0; void *filler_thread(void *data) { - struct buffer *buffer = (struct buffer *)data; + vms_shm_buffer *buffer = (vms_shm_buffer *)data; struct event ev; ev.base.kind = vms_event_get_last_special_kind() + 1; for (size_t i = 0; i < 10000; ++i) { @@ -72,7 +72,7 @@ void *reader_thread(void *data) { static size_t waiting_for_arbbuf = 0; int main(void) { - struct buffer *buffer = + vms_shm_buffer *buffer = initialize_local_buffer("/dummy", sizeof(struct event), 30, NULL); assert(buffer); vms_stream dummy_stream; diff --git a/tests/fetch-test.c b/tests/fetch-test.c index 10a97000..304071bc 100644 --- a/tests/fetch-test.c +++ b/tests/fetch-test.c @@ -24,7 +24,7 @@ struct event { }; void *filler_thread(void *data) { - struct buffer *buffer = (struct buffer *)data; + vms_shm_buffer *buffer = (vms_shm_buffer *)data; struct event ev; ev.base.kind = vms_event_get_last_special_kind() + 1; for (int i = 0; i < 4; ++i) { @@ -39,7 +39,7 @@ void *filler_thread(void *data) { } int main(void) { - struct buffer *buffer = + vms_shm_buffer *buffer = initialize_local_buffer("/dummy", sizeof(struct event), 30, NULL); assert(buffer); vms_stream dummy_stream; From 7c271d2da69bd664d8703399e72af1739e525541 Mon Sep 17 00:00:00 2001 From: Marek Chalupa Date: Thu, 19 Oct 2023 09:41:37 +0200 Subject: [PATCH 22/34] Rename buffer-related functions Those did not use the vms_ prefix and uniform naming. --- include/vamos-buffers/shmbuf/buffer.h | 138 ++++++++++++++++---------- include/vamos-buffers/shmbuf/client.h | 8 +- src/core/stream.c | 58 +++++------ src/shmbuf/buffer-private.h | 17 ++-- src/shmbuf/buffer-sub.c | 23 +++-- src/shmbuf/buffer.c | 125 ++++++++++++----------- src/shmbuf/client.c | 4 +- src/streams/stream-drregex.c | 6 +- src/streams/stream-funs.c | 8 +- src/streams/stream-generic.c | 6 +- src/streams/stream-regex.c | 6 +- src/streams/stream-regexrw.c | 6 +- tests/bench/ringbufs.c | 4 +- tests/buffer-test.c | 44 ++++---- tests/fetch-test-2.c | 2 +- tests/fetch-test-3.c | 2 +- tests/fetch-test.c | 2 +- 17 files changed, 259 insertions(+), 200 deletions(-) diff --git a/include/vamos-buffers/shmbuf/buffer.h b/include/vamos-buffers/shmbuf/buffer.h index 72232e61..0d908e38 100644 --- a/include/vamos-buffers/shmbuf/buffer.h +++ b/include/vamos-buffers/shmbuf/buffer.h @@ -10,70 +10,104 @@ struct vms_source_control; struct vms_event_record; typedef struct _vms_shm_buffer vms_shm_buffer; -vms_shm_buffer *create_shared_buffer(const char *key, size_t capacity, - const struct vms_source_control *control); -vms_shm_buffer *create_shared_buffer_adv( +/* ---------------------------------------------------------------------- */ +/* WRITER API */ +/* ---------------------------------------------------------------------- */ +vms_shm_buffer *vms_shm_buffer_create(const char *key, size_t capacity, + const struct vms_source_control *control); +vms_shm_buffer *vms_shm_buffer_create_adv( const char *key, mode_t mode, size_t elem_size, size_t capacity, const struct vms_source_control *control); -vms_shm_buffer *create_shared_sub_buffer( +vms_shm_buffer *vms_shm_buffer_create_sub_buffer( vms_shm_buffer *buffer, size_t capacity, const struct vms_source_control *control); -size_t buffer_get_sub_buffers_no(vms_shm_buffer *buffer); +size_t vms_shm_buffer_get_sub_buffers_no(vms_shm_buffer *buffer); -vms_shm_buffer *try_get_shared_buffer(const char *key, size_t retry); -vms_shm_buffer *get_shared_buffer(const char *key); -struct vms_event_record *buffer_get_avail_events(vms_shm_buffer *, size_t *); +void vms_shm_buffer_destroy(vms_shm_buffer *); +void vms_shm_buffer_destroy_sub_buffer(vms_shm_buffer *); -int buffer_get_key_path(vms_shm_buffer *, char keypath[], size_t keypathsize); -int buffer_get_ctrl_key_path(vms_shm_buffer *, char keypath[], - size_t keypathsize); -const char *buffer_get_key(vms_shm_buffer *); -char *get_sub_buffer_key(const char *key, size_t idx); +bool vms_shm_buffer_push(vms_shm_buffer *buff, const void *elem, size_t size); +void *vms_shm_buffer_start_push(vms_shm_buffer *buff); -void release_shared_buffer(vms_shm_buffer *); -void destroy_shared_buffer(vms_shm_buffer *); -void destroy_shared_sub_buffer(vms_shm_buffer *); -void release_shared_sub_buffer(vms_shm_buffer *); - -bool buffer_is_ready(vms_shm_buffer *); -bool buffer_monitor_attached(vms_shm_buffer *); -void buffer_set_attached(vms_shm_buffer *, bool); -void buffer_set_destroyed(vms_shm_buffer *buff); - -bool buffer_pop(vms_shm_buffer *buff, void *dst); -bool buffer_push(vms_shm_buffer *buff, const void *elem, size_t size); -void *buffer_get_str(vms_shm_buffer *buff, uint64_t elem); - -void *buffer_read_pointer(vms_shm_buffer *buff, size_t *size); -bool buffer_drop_k(vms_shm_buffer *buff, size_t size); -size_t buffer_consume(vms_shm_buffer *buff, size_t k); - -size_t buffer_size(vms_shm_buffer *buff); -size_t buffer_capacity(vms_shm_buffer *buff); -size_t buffer_elem_size(vms_shm_buffer *buff); - -void *buffer_start_push(vms_shm_buffer *buff); - -void *buffer_partial_push(vms_shm_buffer *buff, void *prev_push, - const void *elem, size_t size); +void *vms_shm_buffer_partial_push(vms_shm_buffer *buff, void *prev_push, + const void *elem, size_t size); /* evid may be ~(0LL), but then there's no garbage collection */ -void *buffer_partial_push_str(vms_shm_buffer *buff, void *prev_push, - uint64_t evid, const char *str); -void *buffer_partial_push_str_n(vms_shm_buffer *buff, void *prev_push, - uint64_t evid, const char *str, size_t len); -void buffer_finish_push(vms_shm_buffer *buff); +void *vms_shm_buffer_partial_push_str(vms_shm_buffer *buff, void *prev_push, + uint64_t evid, const char *str); +void *vms_shm_buffer_partial_push_str_n(vms_shm_buffer *buff, void *prev_push, + uint64_t evid, const char *str, + size_t len); +void vms_shm_buffer_finish_push(vms_shm_buffer *buff); + +/* ---------------------------------------------------------------------- */ +/* READER API */ +/* ---------------------------------------------------------------------- */ +vms_shm_buffer *vms_shm_buffer_try_connect(const char *key, size_t retry); +vms_shm_buffer *vms_shm_buffer_connect(const char *key); +struct vms_event_record *vms_shm_buffer_get_avail_events(vms_shm_buffer *, + size_t *); + +void vms_shm_buffer_release(vms_shm_buffer *); +void vms_shm_buffer_release_sub_buffer(vms_shm_buffer *); + +bool vms_shm_buffer_pop(vms_shm_buffer *buff, void *dst); +void *vms_shm_buffer_read_str(vms_shm_buffer *buff, uint64_t elem); +void *vms_shm_buffer_read_pointer(vms_shm_buffer *buff, size_t *size); +bool vms_shm_buffer_drop_k(vms_shm_buffer *buff, size_t size); +size_t vms_shm_buffer_consume(vms_shm_buffer *buff, size_t k); + +int vms_shm_buffer_register_event(vms_shm_buffer *b, const char *name, + uint64_t kind); +int vms_shm_buffer_register_events(vms_shm_buffer *b, size_t ev_nums, ...); +int vms_shm_buffer_register_all_events(vms_shm_buffer *b); + +/* ---------------------------------------------------------------------- */ +/* GENERIC AND AUXILIARY API */ +/* ---------------------------------------------------------------------- */ + +size_t vms_shm_buffer_size(vms_shm_buffer *buff); +size_t vms_shm_buffer_capacity(vms_shm_buffer *buff); +size_t vms_shm_buffer_elem_size(vms_shm_buffer *buff); + +/* default flags */ +enum _vms_shm_buffer_flag { + READER_IS_READY = 1UL << 0, + READER_FINISHED = 1UL << 1, + LAST_FLAG = READER_FINISHED +}; + +void vms_shm_buffer_set_flags(vms_shm_buffer *, uint64_t); +void vms_shm_buffer_unset_flags(vms_shm_buffer *, uint64_t); + +/** + * @brief vms_shm_buffer_reader_is_ready checks if the reader has called + * vms_shm_buffer_set_reader_is_ready(); + * vms_shm_buffer_reader_is_ready and vms_shm_buffer_reader_is_ready _may_ be + * used to synchronize the writer and the reader before transmiting data. + */ +bool vms_shm_buffer_reader_is_ready(vms_shm_buffer *); +void vms_shm_buffer_set_reader_is_ready(vms_shm_buffer *); + +void vms_shm_buffer_set_destroyed(vms_shm_buffer *buff); + +const char *vms_shm_buffer_key(vms_shm_buffer *); + +int vms_shm_buffer_compute_key_path(vms_shm_buffer *, char keypath[], + size_t keypathsize); +int vms_shm_buffer_compute_ctrl_key_path(vms_shm_buffer *, char keypath[], + size_t keypathsize); +char *vms_shm_buffer_compute_sub_buffer_key(const char *key, size_t idx); + +/* ---------------------------------------------------------------------- */ +/* AUXILIARY BUFFERS */ +/* ---------------------------------------------------------------------- */ struct aux_buff_ptr { uint32_t buffer_id; uint32_t offset; } __attribute__((packed, aligned(64))); -void buffer_set_last_processed_id(vms_shm_buffer *buff, uint64_t id); -void buffer_notify_dropped(vms_shm_buffer *buffer, uint64_t begin_id, - uint64_t end_id); - -int buffer_register_event(vms_shm_buffer *b, const char *name, uint64_t kind); -int buffer_register_events(vms_shm_buffer *b, size_t ev_nums, ...); -int buffer_register_all_events(vms_shm_buffer *b); - +void vms_shm_buffer_set_last_processed_id(vms_shm_buffer *buff, uint64_t id); +void vms_shm_buffer_notify_dropped(vms_shm_buffer *buffer, uint64_t begin_id, + uint64_t end_id); #endif /* VAMOS_SHM_BUFFER_H */ diff --git a/include/vamos-buffers/shmbuf/client.h b/include/vamos-buffers/shmbuf/client.h index a9771d61..a01aafa1 100644 --- a/include/vamos-buffers/shmbuf/client.h +++ b/include/vamos-buffers/shmbuf/client.h @@ -3,6 +3,12 @@ typedef struct _vms_shm_buffer vms_shm_buffer; -int buffer_wait_for_monitor(vms_shm_buffer *); +/** + * @brief vms_shm_buffer_wait_for_reader waits until a reader connects to the + * vms_shm_buffer + * @param buff + * @return negative errno value on error, otherwise 0 + */ +int vms_shm_buffer_wait_for_reader(vms_shm_buffer *); #endif diff --git a/src/core/stream.c b/src/core/stream.c index 7fe57af8..68b51807 100644 --- a/src/core/stream.c +++ b/src/core/stream.c @@ -94,7 +94,7 @@ static void vms_substream_destroy(vms_stream *stream) { free(stream->name); free(stream->events_cache); - release_shared_sub_buffer(stream->incoming_events_buffer); + vms_shm_buffer_release_sub_buffer(stream->incoming_events_buffer); free(stream); } @@ -120,7 +120,7 @@ void vms_stream_destroy(vms_stream *stream) { free(stream->name); free(stream->events_cache); - release_shared_buffer(stream->incoming_events_buffer); + vms_shm_buffer_release(stream->incoming_events_buffer); free(stream); } @@ -137,7 +137,7 @@ const char *vms_stream_get_type(vms_stream *stream) { } _Bool vms_stream_has_new_substreams(vms_stream *stream) { - return buffer_get_sub_buffers_no(stream->incoming_events_buffer) > + return vms_shm_buffer_get_sub_buffers_no(stream->incoming_events_buffer) > stream->substreams_no; } @@ -153,18 +153,18 @@ vms_stream *vms_stream_create_substream( return NULL; } size_t substream_no = ++stream->substreams_no; - char *key = get_sub_buffer_key( - buffer_get_key(stream->incoming_events_buffer), substream_no); - vms_shm_buffer *shmbuffer = get_shared_buffer(key); + char *key = vms_shm_buffer_compute_sub_buffer_key( + vms_shm_buffer_key(stream->incoming_events_buffer), substream_no); + vms_shm_buffer *shmbuffer = vms_shm_buffer_connect(key); assert(shmbuffer && "Getting the shm buffer failed"); vms_stream *substream = xalloc(sizeof *substream); - size_t elem_size = buffer_elem_size(shmbuffer); + size_t elem_size = vms_shm_buffer_elem_size(shmbuffer); assert(elem_size > 0); - char *substream_name = - get_sub_buffer_key(vms_stream_get_name(stream), substream_no); + char *substream_name = vms_shm_buffer_compute_sub_buffer_key( + vms_stream_get_name(stream), substream_no); vms_stream_init( substream, shmbuffer, elem_size, is_ready ? is_ready : stream->is_ready, @@ -181,7 +181,7 @@ vms_stream *vms_stream_create_substream( } struct vms_event_record *vms_stream_get_avail_events(vms_stream *s, size_t *sz) { - return buffer_get_avail_events(s->incoming_events_buffer, sz); + return vms_shm_buffer_get_avail_events(s->incoming_events_buffer, sz); } #define MAX_EVENTS_CACHE_SIZE 128 @@ -211,8 +211,8 @@ struct vms_event_record *vms_stream_get_vms_event_record(vms_stream *stream, } else { /* create cache */ size_t sz; - struct vms_event_record *recs = - buffer_get_avail_events(stream->incoming_events_buffer, &sz); + struct vms_event_record *recs = vms_shm_buffer_get_avail_events( + stream->incoming_events_buffer, &sz); size_t max_kind = get_max_kind(recs, sz); size_t cache_sz = (max_kind > MAX_EVENTS_CACHE_SIZE ? MAX_EVENTS_CACHE_SIZE @@ -236,7 +236,7 @@ struct vms_event_record *vms_stream_get_vms_event_record_no_cache(vms_stream *st vms_kind kind) { size_t sz; struct vms_event_record *recs = - buffer_get_avail_events(stream->incoming_events_buffer, &sz); + vms_shm_buffer_get_avail_events(stream->incoming_events_buffer, &sz); for (size_t i = 0; i < sz; ++i) { if (recs[i].kind == kind) return &recs[i]; @@ -246,12 +246,12 @@ struct vms_event_record *vms_stream_get_vms_event_record_no_cache(vms_stream *st /* the number of elements in the (shared memory) buffer of the stream */ size_t vms_stream_buffer_size(vms_stream *s) { - return buffer_size(s->incoming_events_buffer); + return vms_shm_buffer_size(s->incoming_events_buffer); } /* the capacity the (shared memory) buffer of the stream */ size_t vms_stream_buffer_capacity(vms_stream *s) { - return buffer_capacity(s->incoming_events_buffer); + return vms_shm_buffer_capacity(s->incoming_events_buffer); } /* FIXME: no longer related to stream */ @@ -277,18 +277,18 @@ void *vms_stream_read_events(vms_stream *s, size_t *num) { /* the buffer may be already destroyed on the client's side, * but still may have some events to read */ /* assert(vms_stream_is_ready(s)); */ - return buffer_read_pointer(s->incoming_events_buffer, num); + return vms_shm_buffer_read_pointer(s->incoming_events_buffer, num); } bool vms_stream_consume(vms_stream *stream, size_t num) { #ifdef DUMP_STATS stream->consumed_events += num; #endif - return buffer_drop_k(stream->incoming_events_buffer, num); + return vms_shm_buffer_drop_k(stream->incoming_events_buffer, num); } const char *vms_stream_get_str(vms_stream *stream, uint64_t elem) { - return buffer_get_str(stream->incoming_events_buffer, elem); + return vms_shm_buffer_read_str(stream->incoming_events_buffer, elem); } size_t vms_stream_event_size(vms_stream *s) { return s->event_size; } @@ -296,43 +296,45 @@ size_t vms_stream_event_size(vms_stream *s) { return s->event_size; } int vms_stream_register_event(vms_stream *stream, const char *name, size_t kind) { assert(kind > vms_event_get_last_special_kind() && "Invalid event kind, it overlaps special kinds"); - return buffer_register_event(stream->incoming_events_buffer, name, kind); + return vms_shm_buffer_register_event(stream->incoming_events_buffer, name, + kind); } int vms_stream_register_events(vms_stream *stream, size_t ev_nums, ...) { va_list ap; va_start(ap, ev_nums); - int ret = - buffer_register_events(stream->incoming_events_buffer, ev_nums, ap); + int ret = vms_shm_buffer_register_events(stream->incoming_events_buffer, + ev_nums, ap); va_end(ap); return ret; } int vms_stream_register_all_events(vms_stream *stream) { - return buffer_register_all_events(stream->incoming_events_buffer); + return vms_shm_buffer_register_all_events(stream->incoming_events_buffer); } void vms_stream_notify_last_processed_id(vms_stream *stream, vms_eventid id) { - buffer_set_last_processed_id(stream->incoming_events_buffer, id); + vms_shm_buffer_set_last_processed_id(stream->incoming_events_buffer, id); } void vms_stream_notify_dropped(vms_stream *stream, uint64_t begin_id, uint64_t end_id) { - buffer_notify_dropped(stream->incoming_events_buffer, begin_id, end_id); + vms_shm_buffer_notify_dropped(stream->incoming_events_buffer, begin_id, + end_id); } void vms_stream_attach(vms_stream *stream) { - buffer_set_attached(stream->incoming_events_buffer, true); + vms_shm_buffer_set_reader_is_ready(stream->incoming_events_buffer); } void vms_stream_detach(vms_stream *stream) { - buffer_set_attached(stream->incoming_events_buffer, false); + vms_shm_buffer_unset_flags(stream->incoming_events_buffer, READER_IS_READY); } void vms_stream_dump_events(vms_stream *stream) { size_t evs_num; - struct vms_event_record *events = - buffer_get_avail_events(stream->incoming_events_buffer, &evs_num); + struct vms_event_record *events = vms_shm_buffer_get_avail_events( + stream->incoming_events_buffer, &evs_num); for (size_t i = 0; i < evs_num; ++i) { fprintf(stderr, "[%s:%s] event: %-20s, kind: %-3lu, size: %-3lu, sig: %s\n", diff --git a/src/shmbuf/buffer-private.h b/src/shmbuf/buffer-private.h index 7c81af41..642b4203 100644 --- a/src/shmbuf/buffer-private.h +++ b/src/shmbuf/buffer-private.h @@ -42,9 +42,12 @@ typedef struct _vms_shm_buffer_info { _Atomic _Bool dropped_ranges_lock; /* spin lock */ /* Number of sub-buffers. Sub-buffers are numbered from 1. */ volatile _Atomic size_t subbuffers_no; - /* the monitored program exited/destroyed the buffer */ + /* the writer program/thread exited/destroyed the buffer */ + /* TODO: turn this into a flag */ volatile _Bool destroyed; - volatile _Bool monitor_attached; + /* flags that are used to pass information (e.g., syncing) the writer and + * reader */ + volatile _Atomic uint64_t flags; } vms_shm_buffer_info __attribute__((aligned(CACHELINE_SIZE))); struct shmbuffer { @@ -87,12 +90,12 @@ typedef struct _vms_shm_buffer { #define _ringbuf(buff) (&buff->shmbuffer->info.ringbuf) -vms_shm_buffer *initialize_shared_buffer(const char *key, mode_t mode, - size_t elem_size, size_t capacity, - struct vms_source_control *control); +vms_shm_buffer *_vms_shm_buffer_initialize(const char *key, mode_t mode, + size_t elem_size, size_t capacity, + struct vms_source_control *control); -vms_shm_buffer *get_shared_buffer(const char *key); -vms_shm_buffer *try_get_shared_buffer(const char *key, size_t retry); +// vms_shm_buffer *get_shared_buffer(const char *key); +// vms_shm_buffer *try_get_shared_buffer(const char *key, size_t retry); size_t compute_vms_size(size_t elem_size, size_t capacity); size_t compute_vms_buffer_size(size_t nondata_size, size_t elem_size, diff --git a/src/shmbuf/buffer-sub.c b/src/shmbuf/buffer-sub.c index 5f829138..3ec5130c 100644 --- a/src/shmbuf/buffer-sub.c +++ b/src/shmbuf/buffer-sub.c @@ -7,7 +7,7 @@ #include "vamos-buffers/core/vector-macro.h" #include "vamos-buffers/shmbuf/buffer.h" -char *get_sub_buffer_key(const char *key, size_t idx) { +char *vms_shm_buffer_compute_sub_buffer_key(const char *key, size_t idx) { size_t tmpsize = strlen(key) + 16 /* space for index */; char *tmp = xalloc(tmpsize); int written = snprintf(tmp, tmpsize, "%s.sub.%lu", key, idx); @@ -18,10 +18,11 @@ char *get_sub_buffer_key(const char *key, size_t idx) { return tmp; } -vms_shm_buffer *create_shared_sub_buffer( +vms_shm_buffer *vms_shm_buffer_create_sub_buffer( vms_shm_buffer *buffer, size_t capacity, const struct vms_source_control *control) { - char *key = get_sub_buffer_key(buffer->key, ++buffer->last_subbufer_no); + char *key = vms_shm_buffer_compute_sub_buffer_key( + buffer->key, ++buffer->last_subbufer_no); struct vms_source_control *ctrl = create_shared_control_buffer(key, S_IRWXU, control); if (!ctrl) { @@ -31,9 +32,9 @@ vms_shm_buffer *create_shared_sub_buffer( size_t elem_size = vms_source_control_max_event_size(ctrl); if (capacity == 0) - capacity = buffer_capacity(buffer); + capacity = vms_shm_buffer_capacity(buffer); vms_shm_buffer *sbuf = - initialize_shared_buffer(key, S_IRWXU, elem_size, capacity, ctrl); + _vms_shm_buffer_initialize(key, S_IRWXU, elem_size, capacity, ctrl); /* XXX: we copy the key in 'initialize_shared_buffer' which is redundant as * we have created it in `get_sub_buffer_key` and can just move it */ free(key); @@ -43,7 +44,7 @@ vms_shm_buffer *create_shared_sub_buffer( return sbuf; } -size_t buffer_get_sub_buffers_no(vms_shm_buffer *buffer) { +size_t vms_shm_buffer_get_sub_buffers_no(vms_shm_buffer *buffer) { return buffer->shmbuffer->info.subbuffers_no; } @@ -56,12 +57,12 @@ size_t buffer_get_sub_buffers_no(vms_shm_buffer *buffer) { * therefore destroying the buffer entirely would lead to accessing * dangling data. */ -void buffer_set_destroyed(vms_shm_buffer *buff) { +void vms_shm_buffer_set_destroyed(vms_shm_buffer *buff) { buff->shmbuffer->info.destroyed = 1; } /* for writers */ -void destroy_shared_sub_buffer(vms_shm_buffer *buff) { +void vms_shm_buffer_destroy_sub_buffer(vms_shm_buffer *buff) { buff->shmbuffer->info.destroyed = 1; size_t vecsize = VEC_SIZE(buff->aux_buffers); @@ -77,10 +78,10 @@ void destroy_shared_sub_buffer(vms_shm_buffer *buff) { #endif if (munmap(buff->shmbuffer, buff->shmbuffer->info.allocated_size) != 0) { - perror("destroy_shared_buffer: munmap failure"); + perror("vms_shm_buffer_destroy: munmap failure"); } if (close(buff->fd) == -1) { - perror("destroy_shared_buffer: failed closing mmap fd"); + perror("vms_shm_buffer_destroy: failed closing mmap fd"); } release_shared_control_buffer(buff->control); @@ -90,7 +91,7 @@ void destroy_shared_sub_buffer(vms_shm_buffer *buff) { } /* for readers */ -void release_shared_sub_buffer(vms_shm_buffer *buff) { +void vms_shm_buffer_release_sub_buffer(vms_shm_buffer *buff) { if (munmap(buff->shmbuffer, buff->shmbuffer->info.allocated_size) != 0) { perror("release_shared_sub_buffer: munmap failure"); } diff --git a/src/shmbuf/buffer.c b/src/shmbuf/buffer.c index 99c52031..6cb22789 100644 --- a/src/shmbuf/buffer.c +++ b/src/shmbuf/buffer.c @@ -23,30 +23,30 @@ #include "vamos-buffers/core/utils.h" #include "vamos-buffers/core/vector-macro.h" -bool buffer_is_ready(vms_shm_buffer *buff) { +bool vms_shm_buffer_is_destroyed(vms_shm_buffer *buff) { return !buff->shmbuffer->info.destroyed; } -bool buffer_monitor_attached(vms_shm_buffer *buff) { - return buff->shmbuffer->info.monitor_attached; +bool vms_shm_buffer_reader_is_ready(vms_shm_buffer *buff) { + return (buff->shmbuffer->info.flags & READER_IS_READY); } -size_t buffer_capacity(vms_shm_buffer *buff) { +size_t vms_shm_buffer_capacity(vms_shm_buffer *buff) { return buff->shmbuffer->info.capacity; } -size_t buffer_size(vms_shm_buffer *buff) { +size_t vms_shm_buffer_size(vms_shm_buffer *buff) { return vms_spsc_ringbuf_size(_ringbuf(buff)); } -size_t buffer_elem_size(vms_shm_buffer *buff) { +size_t vms_shm_buffer_elem_size(vms_shm_buffer *buff) { return buff->shmbuffer->info.elem_size; } -const char *buffer_get_key(vms_shm_buffer *buffer) { return buffer->key; } +const char *vms_shm_buffer_key(vms_shm_buffer *buffer) { return buffer->key; } -int buffer_get_key_path(vms_shm_buffer *buff, char keypath[], - size_t keypathsize) { +int vms_shm_buffer_compute_key_path(vms_shm_buffer *buff, char keypath[], + size_t keypathsize) { if (SHM_NAME_MAXLEN <= keypathsize) return -1; @@ -57,8 +57,8 @@ int buffer_get_key_path(vms_shm_buffer *buff, char keypath[], return 0; } -int buffer_get_ctrl_key_path(vms_shm_buffer *buff, char keypath[], - size_t keypathsize) { +int vms_shm_buffer_compute_ctrl_key_path(vms_shm_buffer *buff, char keypath[], + size_t keypathsize) { if (SHM_NAME_MAXLEN <= keypathsize) return -1; @@ -82,9 +82,9 @@ int buffer_get_ctrl_key_path(vms_shm_buffer *buff, char keypath[], ((unsigned char *)b->data + (b->info.elem_size * (b->info.capacity + 1))) HIDE_SYMBOL -vms_shm_buffer *initialize_shared_buffer(const char *key, mode_t mode, - size_t elem_size, size_t capacity, - struct vms_source_control *control) { +vms_shm_buffer *_vms_shm_buffer_initialize(const char *key, mode_t mode, + size_t elem_size, size_t capacity, + struct vms_source_control *control) { assert(elem_size > 0 && "Element size is 0"); assert(capacity > 0 && "Capacity is 0"); /* the ringbuffer has one unusable dummy element, so increase the capacity @@ -193,8 +193,9 @@ vms_shm_buffer *initialize_shared_buffer(const char *key, mode_t mode, return buff; } -vms_shm_buffer *create_shared_buffer(const char *key, size_t capacity, - const struct vms_source_control *control) { +vms_shm_buffer *vms_shm_buffer_create( + const char *key, size_t capacity, + const struct vms_source_control *control) { struct vms_source_control *ctrl = create_shared_control_buffer(key, S_IRWXU, control); if (!ctrl) { @@ -203,10 +204,10 @@ vms_shm_buffer *create_shared_buffer(const char *key, size_t capacity, } size_t elem_size = vms_source_control_max_event_size(ctrl); - return initialize_shared_buffer(key, S_IRWXU, elem_size, capacity, ctrl); + return _vms_shm_buffer_initialize(key, S_IRWXU, elem_size, capacity, ctrl); } -vms_shm_buffer *create_shared_buffer_adv( +vms_shm_buffer *vms_shm_buffer_create_adv( const char *key, mode_t mode, size_t elem_size, size_t capacity, const struct vms_source_control *control) { struct vms_source_control *ctrl = @@ -224,10 +225,10 @@ vms_shm_buffer *create_shared_buffer_adv( mode = S_IRWXU; } - return initialize_shared_buffer(key, mode, elem_size, capacity, ctrl); + return _vms_shm_buffer_initialize(key, mode, elem_size, capacity, ctrl); } -vms_shm_buffer *try_get_shared_buffer(const char *key, size_t retry) { +vms_shm_buffer *vms_shm_buffer_try_connect(const char *key, size_t retry) { // fprintf(stderr, "getting shared buffer '%s'\n", key); int fd = -1; @@ -316,12 +317,12 @@ vms_shm_buffer *try_get_shared_buffer(const char *key, size_t retry) { return NULL; } -vms_shm_buffer *get_shared_buffer(const char *key) { - return try_get_shared_buffer(key, 10); +vms_shm_buffer *vms_shm_buffer_connect(const char *key) { + return vms_shm_buffer_try_connect(key, 10); } -struct vms_event_record *buffer_get_avail_events(vms_shm_buffer *buff, - size_t *evs_num) { +struct vms_event_record *vms_shm_buffer_get_avail_events(vms_shm_buffer *buff, + size_t *evs_num) { assert(buff); assert(evs_num); assert(buff->control); @@ -330,20 +331,29 @@ struct vms_event_record *buffer_get_avail_events(vms_shm_buffer *buff, return buff->control->events; } -void buffer_set_attached(vms_shm_buffer *buff, bool val) { +void vms_shm_buffer_set_flags(vms_shm_buffer *buff, uint64_t flags) { + buff->shmbuffer->info.flags |= flags; +} + +void vms_shm_buffer_unset_flags(vms_shm_buffer *buff, uint64_t flags) { + buff->shmbuffer->info.flags &= (~flags); +} + +void vms_shm_buffer_set_reader_is_ready(vms_shm_buffer *buff) { if (!buff->shmbuffer->info.destroyed) - buff->shmbuffer->info.monitor_attached = val; + buff->shmbuffer->info.flags |= READER_IS_READY; } /* set the ID of the last processed event */ -void buffer_set_last_processed_id(vms_shm_buffer *buff, vms_eventid id) { +void vms_shm_buffer_set_last_processed_id(vms_shm_buffer *buff, + vms_eventid id) { assert(buff->shmbuffer->info.last_processed_id <= id && "The IDs are not monotonic"); buff->shmbuffer->info.last_processed_id = id; } /* for readers */ -void release_shared_buffer(vms_shm_buffer *buff) { +void vms_shm_buffer_release(vms_shm_buffer *buff) { if (munmap(buff->shmbuffer, buff->shmbuffer->info.allocated_size) != 0) { perror("release_shared_buffer: munmap failure"); } @@ -365,7 +375,7 @@ void release_shared_buffer(vms_shm_buffer *buff) { } /* for writers */ -void destroy_shared_buffer(vms_shm_buffer *buff) { +void vms_shm_buffer_destroy(vms_shm_buffer *buff) { buff->shmbuffer->info.destroyed = 1; size_t vecsize = VEC_SIZE(buff->aux_buffers); @@ -381,13 +391,13 @@ void destroy_shared_buffer(vms_shm_buffer *buff) { #endif if (munmap(buff->shmbuffer, buff->shmbuffer->info.allocated_size) != 0) { - perror("destroy_shared_buffer: munmap failure"); + perror("vms_shm_buffer_destroy: munmap failure"); } if (close(buff->fd) == -1) { - perror("destroy_shared_buffer: failed closing mmap fd"); + perror("vms_shm_buffer_destroy: failed closing mmap fd"); } if (vms_shm_unlink(buff->key) != 0) { - perror("destroy_shared_buffer: vms_unlink failure"); + perror("vms_shm_buffer_destroy: vms_unlink failure"); } destroy_shared_control_buffer(buff->key, buff->control); @@ -396,7 +406,7 @@ void destroy_shared_buffer(vms_shm_buffer *buff) { free(buff); } -void *buffer_read_pointer(vms_shm_buffer *buff, size_t *size) { +void *vms_shm_buffer_read_pointer(vms_shm_buffer *buff, size_t *size) { vms_shm_buffer_info *info = &buff->shmbuffer->info; size_t tail = vms_spsc_ringbuf_read_off_nowrap(&info->ringbuf, size); if (*size == 0) @@ -406,11 +416,11 @@ void *buffer_read_pointer(vms_shm_buffer *buff, size_t *size) { return buff->shmbuffer->data + tail * info->elem_size; } -bool buffer_drop_k(vms_shm_buffer *buff, size_t k) { +bool vms_shm_buffer_drop_k(vms_shm_buffer *buff, size_t k) { return vms_spsc_ringbuf_consume_upto(_ringbuf(buff), k) == k; } -size_t buffer_consume(vms_shm_buffer *buff, size_t k) { +size_t vms_shm_buffer_consume(vms_shm_buffer *buff, size_t k) { return vms_spsc_ringbuf_consume_upto(_ringbuf(buff), k); } @@ -430,7 +440,7 @@ size_t buffer_consume(vms_shm_buffer *buff, size_t k) { * multiple steps. */ -void *buffer_start_push(vms_shm_buffer *buff) { +void *vms_shm_buffer_start_push(vms_shm_buffer *buff) { vms_shm_buffer_info *info = &buff->shmbuffer->info; assert(!info->destroyed && "Writing to a destroyed buffer"); @@ -450,9 +460,10 @@ void *buffer_start_push(vms_shm_buffer *buff) { return mem; } -void *buffer_partial_push(vms_shm_buffer *buff, void *prev_push, - const void *elem, size_t size) { - assert(buffer_is_ready(buff) && "Writing to a destroyed buffer"); +void *vms_shm_buffer_partial_push(vms_shm_buffer *buff, void *prev_push, + const void *elem, size_t size) { + assert(!vms_shm_buffer_is_destroyed(buff) && + "Writing to a destroyed buffer"); assert(BUFF_START(buff->shmbuffer) <= (unsigned char *)prev_push); assert((unsigned char *)prev_push < BUFF_END(buff->shmbuffer)); assert((unsigned char *)prev_push <= BUFF_END(buff->shmbuffer) - size); @@ -469,8 +480,8 @@ static uint64_t buffer_push_str(vms_shm_buffer *buff, uint64_t evid, static uint64_t buffer_push_strn(vms_shm_buffer *buff, uint64_t evid, const char *str, size_t len); -void *buffer_partial_push_str(vms_shm_buffer *buff, void *prev_push, - uint64_t evid, const char *str) { +void *vms_shm_buffer_partial_push_str(vms_shm_buffer *buff, void *prev_push, + uint64_t evid, const char *str) { assert(!buff->shmbuffer->info.destroyed && "Writing to a destroyed buffer"); assert(BUFF_START(buff->shmbuffer) <= (unsigned char *)prev_push); assert((unsigned char *)prev_push < BUFF_END(buff->shmbuffer)); @@ -480,8 +491,9 @@ void *buffer_partial_push_str(vms_shm_buffer *buff, void *prev_push, return (unsigned char *)prev_push + sizeof(uint64_t); } -void *buffer_partial_push_str_n(vms_shm_buffer *buff, void *prev_push, - uint64_t evid, const char *str, size_t len) { +void *vms_shm_buffer_partial_push_str_n(vms_shm_buffer *buff, void *prev_push, + uint64_t evid, const char *str, + size_t len) { assert(!buff->shmbuffer->info.destroyed && "Writing to a destroyed buffer"); assert(BUFF_START(buff->shmbuffer) <= (unsigned char *)prev_push); assert((unsigned char *)prev_push < BUFF_END(buff->shmbuffer)); @@ -491,32 +503,32 @@ void *buffer_partial_push_str_n(vms_shm_buffer *buff, void *prev_push, return (unsigned char *)prev_push + sizeof(uint64_t); } -void buffer_finish_push(vms_shm_buffer *buff) { +void vms_shm_buffer_finish_push(vms_shm_buffer *buff) { assert(!buff->shmbuffer->info.destroyed && "Writing to a destroyed buffer"); vms_spsc_ringbuf_write_finish(_ringbuf(buff), 1); } -bool buffer_push(vms_shm_buffer *buff, const void *elem, size_t size) { +bool vms_shm_buffer_push(vms_shm_buffer *buff, const void *elem, size_t size) { assert(!buff->shmbuffer->info.destroyed && "Writing to a destroyed buffer"); assert(buff->shmbuffer->info.elem_size >= size && "Size does not fit the slot"); - void *dst = buffer_start_push(buff); + void *dst = vms_shm_buffer_start_push(buff); if (dst == NULL) return false; memcpy(dst, elem, size); - buffer_finish_push(buff); + vms_shm_buffer_finish_push(buff); return true; } -bool buffer_pop(vms_shm_buffer *buff, void *dst) { +bool vms_shm_buffer_pop(vms_shm_buffer *buff, void *dst) { assert(!buff->shmbuffer->info.destroyed && "Reading from a destroyed buffer"); size_t size; - void *pos = buffer_read_pointer(buff, &size); + void *pos = vms_shm_buffer_read_pointer(buff, &size); if (size > 0) { memcpy(dst, pos, buff->shmbuffer->info.elem_size); vms_spsc_ringbuf_consume(_ringbuf(buff), 1); @@ -540,7 +552,7 @@ size_t _buffer_push_strn(vms_shm_buffer *buff, const void *data, size_t size) { return off; } -void *buffer_get_str(vms_shm_buffer *buff, uint64_t elem) { +void *vms_shm_buffer_read_str(vms_shm_buffer *buff, uint64_t elem) { size_t idx = elem >> 32; struct aux_buffer *ab = reader_get_aux_buffer(buff, idx); size_t off = elem & 0xffffffff; @@ -565,8 +577,8 @@ uint64_t buffer_push_strn(vms_shm_buffer *buff, uint64_t evid, const char *str, return (off | (buff->cur_aux_buff->idx << 32)); } -void buffer_notify_dropped(vms_shm_buffer *buff, uint64_t begin_id, - uint64_t end_id) { +void vms_shm_buffer_notify_dropped(vms_shm_buffer *buff, uint64_t begin_id, + uint64_t end_id) { vms_shm_buffer_info *info = &buff->shmbuffer->info; size_t idx = info->dropped_ranges_next; struct dropped_range *r = &info->dropped_ranges[idx]; @@ -591,7 +603,8 @@ void buffer_notify_dropped(vms_shm_buffer *buff, uint64_t begin_id, drop_ranges_unlock(buff); } -int buffer_register_event(vms_shm_buffer *b, const char *name, uint64_t kind) { +int vms_shm_buffer_register_event(vms_shm_buffer *b, const char *name, + uint64_t kind) { assert(kind > vms_event_get_last_special_kind() && "Invalid event kind, it overlaps special kinds"); struct vms_event_record *rec = vms_source_control_get_event(b->control, name); if (rec == NULL) { @@ -602,7 +615,7 @@ int buffer_register_event(vms_shm_buffer *b, const char *name, uint64_t kind) { return 0; } -int buffer_register_events(vms_shm_buffer *b, size_t ev_nums, ...) { +int vms_shm_buffer_register_events(vms_shm_buffer *b, size_t ev_nums, ...) { va_list ap; va_start(ap, ev_nums); @@ -610,7 +623,7 @@ int buffer_register_events(vms_shm_buffer *b, size_t ev_nums, ...) { const char *name = va_arg(ap, const char *); vms_kind kind = va_arg(ap, vms_kind); assert(kind > vms_event_get_last_special_kind() && "Invalid event kind, it overlaps special kinds"); - if (buffer_register_event(b, name, kind) < 0) { + if (vms_shm_buffer_register_event(b, name, kind) < 0) { va_end(ap); return -1; } @@ -621,7 +634,7 @@ int buffer_register_events(vms_shm_buffer *b, size_t ev_nums, ...) { return 0; } -int buffer_register_all_events(vms_shm_buffer *b) { +int vms_shm_buffer_register_all_events(vms_shm_buffer *b) { struct vms_event_record *recs = b->control->events; const size_t ev_nums = vms_source_control_get_records_num(b->control); diff --git a/src/shmbuf/client.c b/src/shmbuf/client.c index 2835ac79..17a5a7da 100644 --- a/src/shmbuf/client.c +++ b/src/shmbuf/client.c @@ -25,12 +25,12 @@ static inline void restore_sigfunc(void (*sigfunc)(int)) { } } -int buffer_wait_for_monitor(vms_shm_buffer *buff) { +int vms_shm_buffer_wait_for_reader(vms_shm_buffer *buff) { void (*sigfunc)(int); sigfunc = signal(SIGINT, sig_int); int err = 0; - while (!buffer_monitor_attached(buff)) { + while (!vms_shm_buffer_reader_is_ready(buff)) { if (sleep_ms(SLEEP_TIME) != 0) { err = -errno; break; diff --git a/src/streams/stream-drregex.c b/src/streams/stream-drregex.c index a7ab02db..ed498a71 100644 --- a/src/streams/stream-drregex.c +++ b/src/streams/stream-drregex.c @@ -12,7 +12,7 @@ bool drregex_is_ready(vms_stream *stream) { vms_shm_buffer *b = ((vms_stream_drregex *)stream)->shmbuffer; /* buffer must be ready or it may not be ready anymore, but it * still has some data that we haven't read */ - return buffer_is_ready(b) || buffer_size(b) > 0; + return vms_shm_buffer_reader_is_ready(b) || vms_shm_buffer_size(b) > 0; } void drregex_alter(vms_stream *stream, vms_event *in, vms_event *out) { @@ -21,9 +21,9 @@ void drregex_alter(vms_stream *stream, vms_event *in, vms_event *out) { vms_stream *vms_create_drregex_stream(const char *key, const char *name) { vms_stream_drregex *ss = malloc(sizeof *ss); - vms_shm_buffer *shmbuffer = get_shared_buffer(key); + vms_shm_buffer *shmbuffer = vms_shm_buffer_connect(key); assert(shmbuffer && "Getting the shm buffer failed"); - size_t elem_size = buffer_elem_size(shmbuffer); + size_t elem_size = vms_shm_buffer_elem_size(shmbuffer); assert(elem_size > 0); vms_stream_init((vms_stream *)ss, shmbuffer, elem_size, drregex_is_ready, NULL, drregex_alter, NULL, NULL, "drregex-stream", name); diff --git a/src/streams/stream-funs.c b/src/streams/stream-funs.c index 47ab9552..2e0a9cdc 100644 --- a/src/streams/stream-funs.c +++ b/src/streams/stream-funs.c @@ -12,7 +12,7 @@ bool funs_is_ready(vms_stream *stream) { vms_shm_buffer *b = ((vms_stream_funs *)stream)->shmbuffer; /* buffer must be ready or it may not be ready anymore, but it * still has some data that we haven't read */ - return buffer_is_ready(b) || buffer_size(b) > 0; + return vms_shm_buffer_reader_is_ready(b) || vms_shm_buffer_size(b) > 0; } void funs_alter(vms_stream *stream, vms_event *in, vms_event *out) { @@ -24,9 +24,9 @@ size_t stream_mk_event_kinds(const char *stream_name, vms_shm_buffer *shmbuffer, vms_stream *vms_create_funs_stream(const char *key, const char *name) { vms_stream_funs *ss = malloc(sizeof *ss); - vms_shm_buffer *shmbuffer = get_shared_buffer(key); + vms_shm_buffer *shmbuffer = vms_shm_buffer_connect(key); assert(shmbuffer && "Getting the shm buffer failed"); - size_t elem_size = buffer_elem_size(shmbuffer); + size_t elem_size = vms_shm_buffer_elem_size(shmbuffer); assert(elem_size > 0); vms_stream_init((vms_stream *)ss, shmbuffer, elem_size, funs_is_ready, NULL, funs_alter, NULL, NULL, "funs-stream", name); @@ -38,7 +38,7 @@ vms_stream *vms_create_funs_stream(const char *key, const char *name) { } const char *vms_stream_funs_get_str(vms_stream_funs *fstream, uint64_t elem) { - return buffer_get_str(fstream->shmbuffer, elem); + return vms_shm_buffer_read_str(fstream->shmbuffer, elem); } struct vms_event_record *vms_funs_stream_get_event_spec(vms_stream_funs *stream, diff --git a/src/streams/stream-generic.c b/src/streams/stream-generic.c index 136ea760..cfd903b2 100644 --- a/src/streams/stream-generic.c +++ b/src/streams/stream-generic.c @@ -15,7 +15,7 @@ static bool generic_is_ready(vms_stream *stream) { vms_shm_buffer *b = stream->incoming_events_buffer; /* buffer must be ready or it may not be ready anymore, but it * still has some data that we haven't read */ - return buffer_is_ready(b) || buffer_size(b) > 0; + return vms_shm_buffer_reader_is_ready(b) || vms_shm_buffer_size(b) > 0; } static void generic_alter(vms_stream *stream, vms_event *in, vms_event *out) { @@ -25,9 +25,9 @@ static void generic_alter(vms_stream *stream, vms_event *in, vms_event *out) { vms_stream *vms_create_generic_stream(const char *key, const char *name, vms_stream_hole_handling *hole_handling) { vms_stream_generic *ss = malloc(sizeof *ss); - vms_shm_buffer *shmbuffer = get_shared_buffer(key); + vms_shm_buffer *shmbuffer = vms_shm_buffer_connect(key); assert(shmbuffer && "Getting the shm buffer failed"); - size_t elem_size = buffer_elem_size(shmbuffer); + size_t elem_size = vms_shm_buffer_elem_size(shmbuffer); assert(elem_size > 0); vms_stream_init((vms_stream *)ss, shmbuffer, elem_size, generic_is_ready, NULL, generic_alter, NULL, hole_handling, "generic-stream", diff --git a/src/streams/stream-regex.c b/src/streams/stream-regex.c index 0d97a3e1..3f29bd85 100644 --- a/src/streams/stream-regex.c +++ b/src/streams/stream-regex.c @@ -12,7 +12,7 @@ bool sregex_is_ready(vms_stream *stream) { vms_shm_buffer *b = ((vms_stream_sregex *)stream)->shmbuffer; /* buffer must be ready or it may not be ready anymore, but it * still has some data that we haven't read */ - return buffer_is_ready(b) || buffer_size(b) > 0; + return vms_shm_buffer_reader_is_ready(b) || vms_shm_buffer_size(b) > 0; } void sregex_alter(vms_stream *stream, vms_event *in, vms_event *out) { @@ -23,9 +23,9 @@ vms_stream *vms_create_sregex_stream( const char *key, const char *name, const vms_stream_hole_handling *hole_handling) { vms_stream_sregex *ss = malloc(sizeof *ss); - vms_shm_buffer *shmbuffer = get_shared_buffer(key); + vms_shm_buffer *shmbuffer = vms_shm_buffer_connect(key); assert(shmbuffer && "Getting the shm buffer failed"); - size_t elem_size = buffer_elem_size(shmbuffer); + size_t elem_size = vms_shm_buffer_elem_size(shmbuffer); assert(elem_size > 0); vms_stream_init((vms_stream *)ss, shmbuffer, elem_size, sregex_is_ready, /* filter = */ NULL, sregex_alter, NULL, hole_handling, diff --git a/src/streams/stream-regexrw.c b/src/streams/stream-regexrw.c index 74350992..08e28d95 100644 --- a/src/streams/stream-regexrw.c +++ b/src/streams/stream-regexrw.c @@ -12,7 +12,7 @@ bool sregexrw_is_ready(vms_stream *stream) { vms_shm_buffer *b = ((vms_stream_sregexrw *)stream)->shmbuffer; /* buffer must be ready or it may not be ready anymore, but it * still has some data that we haven't read */ - return buffer_is_ready(b) || buffer_size(b) > 0; + return vms_shm_buffer_reader_is_ready(b) || vms_shm_buffer_size(b) > 0; } void sregexrw_alter(vms_stream *stream, vms_event *in, vms_event *out) { @@ -21,9 +21,9 @@ void sregexrw_alter(vms_stream *stream, vms_event *in, vms_event *out) { vms_stream *vms_create_sregexrw_stream(const char *key, const char *name) { vms_stream_sregexrw *ss = malloc(sizeof *ss); - vms_shm_buffer *shmbuffer = get_shared_buffer(key); + vms_shm_buffer *shmbuffer = vms_shm_buffer_connect(key); assert(shmbuffer && "Getting the shm buffer failed"); - size_t elem_size = buffer_elem_size(shmbuffer); + size_t elem_size = vms_shm_buffer_elem_size(shmbuffer); assert(elem_size > 0); vms_stream_init((vms_stream *)ss, shmbuffer, elem_size, sregexrw_is_ready, NULL, sregexrw_alter, NULL, NULL, "regexrw-stream", name); diff --git a/tests/bench/ringbufs.c b/tests/bench/ringbufs.c index a60b5935..c569c6a3 100644 --- a/tests/bench/ringbufs.c +++ b/tests/bench/ringbufs.c @@ -31,7 +31,7 @@ static double report_time(const char *msg, struct timespec *start, #if 0 static void run_shmbuf_push_pop_st() { struct vms_source_control *ctrl = vms_source_control_define(1, "dummy", "i"); - vms_shm_buffer *buff = create_shared_buffer("/test", sizeof(int), ctrl); + vms_shm_buffer *buff = vms_shm_buffer_create("/test", sizeof(int), ctrl); assert(buff); assert(buffer_capacity(buff) >= N); @@ -59,7 +59,7 @@ static void run_shmbuf_push_pop_st() { printf("\033[34m[shmbuf], single-threaded, push/pop: %lf seconds.\033[0m\n", elapsed); assert(buffer_size(buff) == 0); - destroy_shared_buffer(buff); + vms_shm_buffer_destroy(buff); } diff --git a/tests/buffer-test.c b/tests/buffer-test.c index 1ece2ec2..96362f3d 100644 --- a/tests/buffer-test.c +++ b/tests/buffer-test.c @@ -17,49 +17,49 @@ int main(void) { ctrl->events[0].name[0] = '\0'; ctrl->events[0].signature[0] = '\0'; - vms_shm_buffer *b = create_shared_buffer("/testkey", 128, ctrl); + vms_shm_buffer *b = vms_shm_buffer_create("/testkey", 128, ctrl); assert(b); - assert(buffer_elem_size(b) >= sizeof(size_t)); + assert(vms_shm_buffer_elem_size(b) >= sizeof(size_t)); free(ctrl); - assert(buffer_size(b) == 0); + assert(vms_shm_buffer_size(b) == 0); for (i = 1; i < 101; ++i) { - assert(buffer_push(b, &i, sizeof(size_t)) == true); + assert(vms_shm_buffer_push(b, &i, sizeof(size_t)) == true); } - assert(buffer_size(b) == 100); + assert(vms_shm_buffer_size(b) == 100); size_t j; for (i = 1; i < 101; ++i) { - assert(buffer_pop(b, &j) == true); + assert(vms_shm_buffer_pop(b, &j) == true); assert(j == i); } - assert(buffer_size(b) == 0); + assert(vms_shm_buffer_size(b) == 0); // pop k for (i = 1; i < 101; ++i) { - assert(buffer_push(b, &i, sizeof(size_t)) == true); + assert(vms_shm_buffer_push(b, &i, sizeof(size_t)) == true); } - assert(buffer_size(b) == 100); + assert(vms_shm_buffer_size(b) == 100); size_t num; for (i = 0; i < 100; ++i) { - assert(buffer_pop(b, &num) == true); + assert(vms_shm_buffer_pop(b, &num) == true); assert(num == i + 1); } // pop k with rotate - const size_t capacity = buffer_capacity(b); + const size_t capacity = vms_shm_buffer_capacity(b); for (i = 1; i < capacity + 1; ++i) { - assert(buffer_push(b, &i, sizeof(size_t)) == true); + assert(vms_shm_buffer_push(b, &i, sizeof(size_t)) == true); } - assert(buffer_push(b, &i, sizeof(size_t)) == false); - assert(buffer_pop(b, &i) == true && i == 1); - assert(buffer_pop(b, &i) == true && i == 2); - assert(buffer_pop(b, &i) == true && i == 3); - assert(buffer_pop(b, &i) == true && i == 4); + assert(vms_shm_buffer_push(b, &i, sizeof(size_t)) == false); + assert(vms_shm_buffer_pop(b, &i) == true && i == 1); + assert(vms_shm_buffer_pop(b, &i) == true && i == 2); + assert(vms_shm_buffer_pop(b, &i) == true && i == 3); + assert(vms_shm_buffer_pop(b, &i) == true && i == 4); i = capacity + 1; - assert(buffer_push(b, &i, sizeof(size_t)) == true); + assert(vms_shm_buffer_push(b, &i, sizeof(size_t)) == true); i = capacity + 2; - assert(buffer_push(b, &i, sizeof(size_t)) == true); - // buffer contains 5, ... capacity + 1 - assert(buffer_size(b) == capacity - 2); - destroy_shared_buffer(b); + assert(vms_shm_buffer_push(b, &i, sizeof(size_t)) == true); + // vms_shm_buffer contains 5, ... capacity + 1 + assert(vms_shm_buffer_size(b) == capacity - 2); + vms_shm_buffer_destroy(b); } diff --git a/tests/fetch-test-2.c b/tests/fetch-test-2.c index 4b93dfa3..6702a2cb 100644 --- a/tests/fetch-test-2.c +++ b/tests/fetch-test-2.c @@ -36,7 +36,7 @@ void* filler_thread(void* data) { printf("PUSH Event: {{%lu, %lu}, %lu}\n", ev.base.id, ev.base.kind, ev.n); */ - while (!buffer_push(buffer, &ev, sizeof(ev))) ++failed_push; + while (!vms_shm_buffer_push(buffer, &ev, sizeof(ev))) ++failed_push; } stream_ready = 0; pthread_exit(NULL); diff --git a/tests/fetch-test-3.c b/tests/fetch-test-3.c index 54ea1e3d..90a5a91c 100644 --- a/tests/fetch-test-3.c +++ b/tests/fetch-test-3.c @@ -34,7 +34,7 @@ void *filler_thread(void *data) { ev.n = i; // printf("PUSH Event: {{%lu, %lu}, %lu}\n", ev.base.id, ev.base.kind, // ev.n); - while (!buffer_push(buffer, &ev, sizeof(ev))) ++failed_push; + while (!vms_shm_buffer_push(buffer, &ev, sizeof(ev))) ++failed_push; } stream_ready = 0; pthread_exit(NULL); diff --git a/tests/fetch-test.c b/tests/fetch-test.c index 304071bc..06826fa0 100644 --- a/tests/fetch-test.c +++ b/tests/fetch-test.c @@ -32,7 +32,7 @@ void *filler_thread(void *data) { ev.n = i; // printf("PUSH Event: {{%lu, %lu}, %d}\n", ev.base.id, ev.base.kind, // ev.n); - assert(buffer_push(buffer, &ev, sizeof(ev)) == true); + assert(vms_shm_buffer_push(buffer, &ev, sizeof(ev)) == true); } stream_ready = 0; pthread_exit(NULL); From 19b1317bcd804d3bc735f62461381ef32d5b3430 Mon Sep 17 00:00:00 2001 From: Marek Chalupa Date: Thu, 19 Oct 2023 09:50:04 +0200 Subject: [PATCH 23/34] Rename stream_fetch -> vms_stream_fetch --- include/vamos-buffers/core/arbiter.h | 6 +++--- src/core/arbiter.c | 10 +++++----- src/core/shamon.c | 2 +- tests/fetch-test-2.c | 4 ++-- tests/fetch-test-3.c | 4 ++-- tests/fetch-test.c | 4 ++-- 6 files changed, 15 insertions(+), 15 deletions(-) diff --git a/include/vamos-buffers/core/arbiter.h b/include/vamos-buffers/core/arbiter.h index 8d0eba0d..edd5a9c3 100644 --- a/include/vamos-buffers/core/arbiter.h +++ b/include/vamos-buffers/core/arbiter.h @@ -62,10 +62,10 @@ size_t vms_arbiter_buffer_drop_older_than(vms_arbiter_buffer *buffer, vms_eventid id); bool vms_arbiter_buffer_pop(vms_arbiter_buffer *q, void *buff); -void *stream_fetch(vms_stream *stream, vms_arbiter_buffer *buffer); +void *vms_stream_fetch(vms_stream *stream, vms_arbiter_buffer *buffer); -void *stream_filter_fetch(vms_stream *stream, vms_arbiter_buffer *buffer, - vms_stream_filter_fn filter); +void *vms_stream_filter_fetch(vms_stream *stream, vms_arbiter_buffer *buffer, + vms_stream_filter_fn filter); bool vms_arbiter_buffer_is_done(vms_arbiter_buffer *buffer); diff --git a/src/core/arbiter.c b/src/core/arbiter.c index 1dd598c5..b64a6a04 100644 --- a/src/core/arbiter.c +++ b/src/core/arbiter.c @@ -567,7 +567,7 @@ static bool handle_dropping_event(vms_stream *stream, } /* wait for an event on the 'stream' */ -void *stream_fetch(vms_stream *stream, vms_arbiter_buffer *buffer) { +void *vms_stream_fetch(vms_stream *stream, vms_arbiter_buffer *buffer) { void *ev; size_t last_ev_id = 1; while (1) { @@ -599,8 +599,8 @@ void *stream_fetch(vms_stream *stream, vms_arbiter_buffer *buffer) { } /* FIXME: do not duplicate the code */ -void *stream_filter_fetch(vms_stream *stream, vms_arbiter_buffer *buffer, - vms_stream_filter_fn filter) { +void *vms_stream_filter_fetch(vms_stream *stream, vms_arbiter_buffer *buffer, + vms_stream_filter_fn filter) { void *ev; size_t last_ev_id = 1; while (1) { @@ -683,10 +683,10 @@ void vms_arbiter_buffer_dump_stats(vms_arbiter_buffer *buffer) { s->last_event_id); COLOR_RESET #endif - fprintf(stderr, " stream_fetch() fetched %lu events\n", + fprintf(stderr, " vms_stream_fetch() fetched %lu events\n", s->fetched_events); fprintf(stderr, - " stream_fetch() totally dropped %lu events in %lu holes\n", + " vms_stream_fetch() totally dropped %lu events in %lu holes\n", buffer->total_dropped_num, buffer->total_dropped_times); fprintf(stderr, " Last event was drop: %s\n", buffer->last_was_drop ? "true" : "false"); diff --git a/src/core/shamon.c b/src/core/shamon.c index af095323..f1ce004f 100644 --- a/src/core/shamon.c +++ b/src/core/shamon.c @@ -51,7 +51,7 @@ static int default_buffer_manager_thrd(void *data) { void *ev, *out; while (1) { - ev = stream_fetch(stream, buffer); + ev = vms_stream_fetch(stream, buffer); if (!ev) { break; } diff --git a/tests/fetch-test-2.c b/tests/fetch-test-2.c index 6702a2cb..e14e2e42 100644 --- a/tests/fetch-test-2.c +++ b/tests/fetch-test-2.c @@ -107,7 +107,7 @@ int main(void) { struct event* ev; for (size_t i = 1; i <= 10000; ++i) { - ev = stream_fetch(stream, arbiter_buffer); + ev = vms_stream_fetch(stream, arbiter_buffer); /* there should be no dropped event generated */ assert(vms_arbiter_buffer_size(arbiter_buffer) == 0); @@ -121,7 +121,7 @@ int main(void) { vms_stream_consume(stream, 1); } - ev = stream_fetch(stream, arbiter_buffer); + ev = vms_stream_fetch(stream, arbiter_buffer); assert(!ev); main_finished = 1; diff --git a/tests/fetch-test-3.c b/tests/fetch-test-3.c index 90a5a91c..e3637fb5 100644 --- a/tests/fetch-test-3.c +++ b/tests/fetch-test-3.c @@ -99,7 +99,7 @@ int main(void) { struct event *ev; struct event *out; for (size_t i = 0; i < 10000; ++i) { - ev = stream_fetch(stream, arbiter_buffer); + ev = vms_stream_fetch(stream, arbiter_buffer); /* there should be no dropped event generated * since we are outputing the events into another buffer */ assert(vms_arbiter_buffer_size(arbiter_buffer) == 0); @@ -121,7 +121,7 @@ int main(void) { vms_stream_consume(stream, 1); } - ev = stream_fetch(stream, arbiter_buffer); + ev = vms_stream_fetch(stream, arbiter_buffer); assert(!ev); main_finished = 1; diff --git a/tests/fetch-test.c b/tests/fetch-test.c index 06826fa0..0620ecf8 100644 --- a/tests/fetch-test.c +++ b/tests/fetch-test.c @@ -57,7 +57,7 @@ int main(void) { struct event *ev; for (int i = 0; i < 4; ++i) { - ev = stream_fetch(stream, arbiter_buffer); + ev = vms_stream_fetch(stream, arbiter_buffer); assert(ev); assert(ev->n == i); // printf("POP Event: {{%lu, %lu}, %d}\n", ev->base.id, ev->base.kind, @@ -65,7 +65,7 @@ int main(void) { vms_stream_consume(stream, 1); } - ev = stream_fetch(stream, arbiter_buffer); + ev = vms_stream_fetch(stream, arbiter_buffer); assert(!ev); pthread_join(tid, NULL); From 1bebd2bc4bb3dc8d3f8b806394777db7442a924f Mon Sep 17 00:00:00 2001 From: Marek Chalupa Date: Fri, 20 Oct 2023 13:20:03 +0200 Subject: [PATCH 24/34] Fix some names missed in previous refactoring commits --- include/vamos-buffers/core/stream.h | 5 ++--- include/vamos-buffers/streams/stream-generic.h | 2 +- src/core/stream.c | 6 +++--- tests/fetch-test-2.c | 2 +- tests/fetch-test-3.c | 2 +- 5 files changed, 8 insertions(+), 9 deletions(-) diff --git a/include/vamos-buffers/core/stream.h b/include/vamos-buffers/core/stream.h index 9ce55675..c2c0e698 100644 --- a/include/vamos-buffers/core/stream.h +++ b/include/vamos-buffers/core/stream.h @@ -72,9 +72,8 @@ size_t vms_stream_event_size(vms_stream *); size_t vms_stream_id(vms_stream *); struct vms_event_record *vms_stream_get_avail_events(vms_stream *, size_t *); -struct vms_event_record *vms_stream_get_vms_event_record(vms_stream *, vms_kind); -struct vms_event_record *vms_stream_get_vms_event_record_no_cache(vms_stream *, - vms_kind); +struct vms_event_record *vms_stream_get_event_record(vms_stream *, vms_kind); +struct vms_event_record *vms_stream_get_event_record_no_cache(vms_stream *, vms_kind); /* register to receiving events particular events */ int vms_stream_register_event(vms_stream *, const char *, vms_kind); diff --git a/include/vamos-buffers/streams/stream-generic.h b/include/vamos-buffers/streams/stream-generic.h index 9cc100a8..4394d84e 100644 --- a/include/vamos-buffers/streams/stream-generic.h +++ b/include/vamos-buffers/streams/stream-generic.h @@ -7,7 +7,7 @@ #include "vamos-buffers/core/event.h" #include "vamos-buffers/core/stream.h" -vms_shm_buffer; +typedef struct _vms_shm_buffer vms_shm_buffer; /* A generic stream for events stored in shared memory buffer, no filter nor modification of events supported (unless done manually). */ diff --git a/src/core/stream.c b/src/core/stream.c index 68b51807..704650a4 100644 --- a/src/core/stream.c +++ b/src/core/stream.c @@ -196,7 +196,7 @@ static vms_kind get_max_kind(struct vms_event_record *recs, size_t size) { return ret; } -struct vms_event_record *vms_stream_get_vms_event_record(vms_stream *stream, +struct vms_event_record *vms_stream_get_event_record(vms_stream *stream, vms_kind kind) { assert(kind > 0 && "Got invalid kind"); struct vms_event_record *rec = NULL; @@ -206,7 +206,7 @@ struct vms_event_record *vms_stream_get_vms_event_record(vms_stream *stream, assert(rec->kind == kind); return rec; } else { - return vms_stream_get_vms_event_record_no_cache(stream, kind); + return vms_stream_get_event_record_no_cache(stream, kind); } } else { /* create cache */ @@ -232,7 +232,7 @@ struct vms_event_record *vms_stream_get_vms_event_record(vms_stream *stream, } } -struct vms_event_record *vms_stream_get_vms_event_record_no_cache(vms_stream *stream, +struct vms_event_record *vms_stream_get_event_record_no_cache(vms_stream *stream, vms_kind kind) { size_t sz; struct vms_event_record *recs = diff --git a/tests/fetch-test-2.c b/tests/fetch-test-2.c index e14e2e42..27a38b16 100644 --- a/tests/fetch-test-2.c +++ b/tests/fetch-test-2.c @@ -90,7 +90,7 @@ int main(void) { vms_stream_init(stream, buffer, sizeof(struct event), is_ready, NULL, NULL, NULL, NULL, "dummy-stream", "dummy"); - /* into this buffer, stream_fetch() will push dropped() events if any */ + /* into this buffer, vms_stream_fetch() will push dropped() events if any */ vms_arbiter_buffer* arbiter_buffer = vms_arbiter_buffer_create(stream, sizeof(struct event), 10); diff --git a/tests/fetch-test-3.c b/tests/fetch-test-3.c index e3637fb5..ca245c5d 100644 --- a/tests/fetch-test-3.c +++ b/tests/fetch-test-3.c @@ -81,7 +81,7 @@ int main(void) { vms_stream_init(stream, buffer, sizeof(struct event), is_ready, NULL, NULL, NULL, NULL, "dummy-stream", "dummy"); - /* into this buffer, stream_fetch() will push dropped() events if any */ + /* into this buffer, vms_stream_fetch() will push dropped() events if any */ vms_arbiter_buffer *arbiter_buffer = vms_arbiter_buffer_create(stream, sizeof(struct event), 10); From b08afa9a650d7ccbe608678a47ae54838df4401b Mon Sep 17 00:00:00 2001 From: Marek Chalupa Date: Fri, 20 Oct 2023 21:29:02 +0200 Subject: [PATCH 25/34] Fix bugs introduced during refactoring vms_shm_buffer_is_ready() got broken. --- include/vamos-buffers/shmbuf/buffer.h | 5 +++++ src/shmbuf/buffer.c | 16 ++++++++++------ src/streams/stream-drregex.c | 2 +- src/streams/stream-generic.c | 2 +- src/streams/stream-regex.c | 2 +- src/streams/stream-regexrw.c | 2 +- 6 files changed, 19 insertions(+), 10 deletions(-) diff --git a/include/vamos-buffers/shmbuf/buffer.h b/include/vamos-buffers/shmbuf/buffer.h index 0d908e38..e06245e2 100644 --- a/include/vamos-buffers/shmbuf/buffer.h +++ b/include/vamos-buffers/shmbuf/buffer.h @@ -88,6 +88,11 @@ void vms_shm_buffer_unset_flags(vms_shm_buffer *, uint64_t); bool vms_shm_buffer_reader_is_ready(vms_shm_buffer *); void vms_shm_buffer_set_reader_is_ready(vms_shm_buffer *); +/* buffer is ready to transmit data (it can be written to if there is space) */ +bool vms_shm_buffer_is_ready(vms_shm_buffer *); +/* buffer was not destroyed on the writer's side */ +bool vms_shm_buffer_is_destroyed(vms_shm_buffer *); + void vms_shm_buffer_set_destroyed(vms_shm_buffer *buff); const char *vms_shm_buffer_key(vms_shm_buffer *); diff --git a/src/shmbuf/buffer.c b/src/shmbuf/buffer.c index 6cb22789..c9eb722f 100644 --- a/src/shmbuf/buffer.c +++ b/src/shmbuf/buffer.c @@ -24,7 +24,11 @@ #include "vamos-buffers/core/vector-macro.h" bool vms_shm_buffer_is_destroyed(vms_shm_buffer *buff) { - return !buff->shmbuffer->info.destroyed; + return buff->shmbuffer->info.destroyed; +} + +bool vms_shm_buffer_is_ready(vms_shm_buffer *buff) { + return !vms_shm_buffer_is_destroyed(buff); } bool vms_shm_buffer_reader_is_ready(vms_shm_buffer *buff) { @@ -426,12 +430,12 @@ size_t vms_shm_buffer_consume(vms_shm_buffer *buff, size_t k) { /* buffer_push broken down into several operations: * - * p = buffer_start_push(...) - * p = buffer_partial_push(..., p, ...) + * p = vms_shm_buffer_start_push(...) + * p = vms_shm_buffer_partial_push(..., p, ...) * ... - * p = buffer_partial_push(..., p, ...) - * buffer_partial_push(..., p, ...) - * buffer_finish_push(...) + * p = vms_shm_buffer_partial_push(..., p, ...) + * vms_shm_buffer_partial_push(..., p, ...) + * vms_shm_buffer_finish_push(...) * * All the partial push may push only a single element in sum, * i.e., what can be done with buffer_push. Partial pushes diff --git a/src/streams/stream-drregex.c b/src/streams/stream-drregex.c index ed498a71..3894ec0a 100644 --- a/src/streams/stream-drregex.c +++ b/src/streams/stream-drregex.c @@ -12,7 +12,7 @@ bool drregex_is_ready(vms_stream *stream) { vms_shm_buffer *b = ((vms_stream_drregex *)stream)->shmbuffer; /* buffer must be ready or it may not be ready anymore, but it * still has some data that we haven't read */ - return vms_shm_buffer_reader_is_ready(b) || vms_shm_buffer_size(b) > 0; + return vms_shm_buffer_is_ready(b) || vms_shm_buffer_size(b) > 0; } void drregex_alter(vms_stream *stream, vms_event *in, vms_event *out) { diff --git a/src/streams/stream-generic.c b/src/streams/stream-generic.c index cfd903b2..2bafb473 100644 --- a/src/streams/stream-generic.c +++ b/src/streams/stream-generic.c @@ -15,7 +15,7 @@ static bool generic_is_ready(vms_stream *stream) { vms_shm_buffer *b = stream->incoming_events_buffer; /* buffer must be ready or it may not be ready anymore, but it * still has some data that we haven't read */ - return vms_shm_buffer_reader_is_ready(b) || vms_shm_buffer_size(b) > 0; + return vms_shm_buffer_is_ready(b) || vms_shm_buffer_size(b) > 0; } static void generic_alter(vms_stream *stream, vms_event *in, vms_event *out) { diff --git a/src/streams/stream-regex.c b/src/streams/stream-regex.c index 3f29bd85..ffbee55f 100644 --- a/src/streams/stream-regex.c +++ b/src/streams/stream-regex.c @@ -12,7 +12,7 @@ bool sregex_is_ready(vms_stream *stream) { vms_shm_buffer *b = ((vms_stream_sregex *)stream)->shmbuffer; /* buffer must be ready or it may not be ready anymore, but it * still has some data that we haven't read */ - return vms_shm_buffer_reader_is_ready(b) || vms_shm_buffer_size(b) > 0; + return vms_shm_buffer_is_ready(b) || vms_shm_buffer_size(b) > 0; } void sregex_alter(vms_stream *stream, vms_event *in, vms_event *out) { diff --git a/src/streams/stream-regexrw.c b/src/streams/stream-regexrw.c index 08e28d95..30f8c7a1 100644 --- a/src/streams/stream-regexrw.c +++ b/src/streams/stream-regexrw.c @@ -12,7 +12,7 @@ bool sregexrw_is_ready(vms_stream *stream) { vms_shm_buffer *b = ((vms_stream_sregexrw *)stream)->shmbuffer; /* buffer must be ready or it may not be ready anymore, but it * still has some data that we haven't read */ - return vms_shm_buffer_reader_is_ready(b) || vms_shm_buffer_size(b) > 0; + return vms_shm_buffer_is_ready(b) || vms_shm_buffer_size(b) > 0; } void sregexrw_alter(vms_stream *stream, vms_event *in, vms_event *out) { From e82694fec3078f7ba5c3f9d86a9dbc2781467e66 Mon Sep 17 00:00:00 2001 From: Marek Chalupa Date: Thu, 26 Oct 2023 10:20:31 +0200 Subject: [PATCH 26/34] source_control: add a fence on getting events from shared memory We want to be sure that the writes performed by monitor are visible in the source. --- src/shmbuf/buffer.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/shmbuf/buffer.c b/src/shmbuf/buffer.c index c9eb722f..c711ac3e 100644 --- a/src/shmbuf/buffer.c +++ b/src/shmbuf/buffer.c @@ -15,6 +15,8 @@ #include #include +#include /* _mm_mfence */ + #include "buffer-private.h" #include "shm.h" #include "vamos-buffers/core/list.h" @@ -331,6 +333,8 @@ struct vms_event_record *vms_shm_buffer_get_avail_events(vms_shm_buffer *buff, assert(evs_num); assert(buff->control); + /* make sure that concurrent writes to the source control buffer are visible now */ + _mm_mfence(); *evs_num = vms_source_control_get_records_num(buff->control); return buff->control->events; } From ea83343d285ea2bc47be90c8ae506ee1ac24a41e Mon Sep 17 00:00:00 2001 From: Marek Chalupa Date: Thu, 26 Oct 2023 12:56:15 +0200 Subject: [PATCH 27/34] Check that events were registered before reader is ready --- include/vamos-buffers/shmbuf/buffer.h | 6 ++++++ src/core/arbiter.c | 2 +- src/shmbuf/buffer-private.h | 6 +++--- src/shmbuf/buffer.c | 22 +++++++++++++++++++++- src/shmbuf/client.c | 5 +++++ 5 files changed, 36 insertions(+), 5 deletions(-) diff --git a/include/vamos-buffers/shmbuf/buffer.h b/include/vamos-buffers/shmbuf/buffer.h index e06245e2..500d0274 100644 --- a/include/vamos-buffers/shmbuf/buffer.h +++ b/include/vamos-buffers/shmbuf/buffer.h @@ -56,6 +56,10 @@ void *vms_shm_buffer_read_pointer(vms_shm_buffer *buff, size_t *size); bool vms_shm_buffer_drop_k(vms_shm_buffer *buff, size_t size); size_t vms_shm_buffer_consume(vms_shm_buffer *buff, size_t k); +/* The reader is supposed to let the writer know what events it wants + * to receive. This is done via this set of "register_event" functions. + * These functions must be called before `set_reader_is_ready`. + */ int vms_shm_buffer_register_event(vms_shm_buffer *b, const char *name, uint64_t kind); int vms_shm_buffer_register_events(vms_shm_buffer *b, size_t ev_nums, ...); @@ -73,9 +77,11 @@ size_t vms_shm_buffer_elem_size(vms_shm_buffer *buff); enum _vms_shm_buffer_flag { READER_IS_READY = 1UL << 0, READER_FINISHED = 1UL << 1, + EVENTS_REGISTERED = 1UL << 2, LAST_FLAG = READER_FINISHED }; +uint64_t vms_shm_buffer_get_flags(vms_shm_buffer *); void vms_shm_buffer_set_flags(vms_shm_buffer *, uint64_t); void vms_shm_buffer_unset_flags(vms_shm_buffer *, uint64_t); diff --git a/src/core/arbiter.c b/src/core/arbiter.c index b64a6a04..ebf33005 100644 --- a/src/core/arbiter.c +++ b/src/core/arbiter.c @@ -221,7 +221,7 @@ void vms_arbiter_buffer_set_active(vms_arbiter_buffer *buffer, bool val) { if (val) { vms_stream_attach(buffer->stream); } else { - vms_stream_attach(buffer->stream); + vms_stream_detach(buffer->stream); } } diff --git a/src/shmbuf/buffer-private.h b/src/shmbuf/buffer-private.h index 642b4203..02676766 100644 --- a/src/shmbuf/buffer-private.h +++ b/src/shmbuf/buffer-private.h @@ -41,13 +41,13 @@ typedef struct _vms_shm_buffer_info { size_t dropped_ranges_next; _Atomic _Bool dropped_ranges_lock; /* spin lock */ /* Number of sub-buffers. Sub-buffers are numbered from 1. */ - volatile _Atomic size_t subbuffers_no; + _Atomic size_t subbuffers_no; /* the writer program/thread exited/destroyed the buffer */ /* TODO: turn this into a flag */ - volatile _Bool destroyed; + _Atomic _Bool destroyed; /* flags that are used to pass information (e.g., syncing) the writer and * reader */ - volatile _Atomic uint64_t flags; + _Atomic uint64_t flags; } vms_shm_buffer_info __attribute__((aligned(CACHELINE_SIZE))); struct shmbuffer { diff --git a/src/shmbuf/buffer.c b/src/shmbuf/buffer.c index c711ac3e..db7e72e9 100644 --- a/src/shmbuf/buffer.c +++ b/src/shmbuf/buffer.c @@ -333,12 +333,24 @@ struct vms_event_record *vms_shm_buffer_get_avail_events(vms_shm_buffer *buff, assert(evs_num); assert(buff->control); - /* make sure that concurrent writes to the source control buffer are visible now */ +#ifndef NDEBUG + /* NOTE: this assert does the fence */ + assert((vms_shm_buffer_get_flags(buff) & EVENTS_REGISTERED) && + "Events were not registered"); +#else + /* make sure that any concurrent changes to the source control buffer are + * visible now */ _mm_mfence(); +#endif + *evs_num = vms_source_control_get_records_num(buff->control); return buff->control->events; } +uint64_t vms_shm_buffer_get_flags(vms_shm_buffer *buff) { + return buff->shmbuffer->info.flags; +} + void vms_shm_buffer_set_flags(vms_shm_buffer *buff, uint64_t flags) { buff->shmbuffer->info.flags |= flags; } @@ -348,6 +360,9 @@ void vms_shm_buffer_unset_flags(vms_shm_buffer *buff, uint64_t flags) { } void vms_shm_buffer_set_reader_is_ready(vms_shm_buffer *buff) { + assert((vms_shm_buffer_get_flags(buff) & EVENTS_REGISTERED) && + "No events registered before going to the ready state"); + if (!buff->shmbuffer->info.destroyed) buff->shmbuffer->info.flags |= READER_IS_READY; } @@ -614,6 +629,9 @@ void vms_shm_buffer_notify_dropped(vms_shm_buffer *buff, uint64_t begin_id, int vms_shm_buffer_register_event(vms_shm_buffer *b, const char *name, uint64_t kind) { assert(kind > vms_event_get_last_special_kind() && "Invalid event kind, it overlaps special kinds"); + + vms_shm_buffer_set_flags(b, EVENTS_REGISTERED); + struct vms_event_record *rec = vms_source_control_get_event(b->control, name); if (rec == NULL) { return -1; @@ -646,6 +664,8 @@ int vms_shm_buffer_register_all_events(vms_shm_buffer *b) { struct vms_event_record *recs = b->control->events; const size_t ev_nums = vms_source_control_get_records_num(b->control); + vms_shm_buffer_set_flags(b, EVENTS_REGISTERED); + for (size_t i = 0; i < ev_nums; ++i) { recs[i].kind = 1 + i + vms_event_get_last_special_kind(); } diff --git a/src/shmbuf/client.c b/src/shmbuf/client.c index 17a5a7da..15893ede 100644 --- a/src/shmbuf/client.c +++ b/src/shmbuf/client.c @@ -39,6 +39,11 @@ int vms_shm_buffer_wait_for_reader(vms_shm_buffer *buff) { err = -EINTR; break; } + + if (vms_shm_buffer_is_destroyed(buff)) { + err = -EOWNERDEAD; + break; + } } restore_sigfunc(sigfunc); From 7bbdb53d9bfccac055185b539215a30d45343d3c Mon Sep 17 00:00:00 2001 From: Marek Chalupa Date: Thu, 26 Oct 2023 13:12:27 +0200 Subject: [PATCH 28/34] shmbuf: use API instead of a hard-coded setting. --- src/shmbuf/buffer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/shmbuf/buffer.c b/src/shmbuf/buffer.c index db7e72e9..34f3a7d5 100644 --- a/src/shmbuf/buffer.c +++ b/src/shmbuf/buffer.c @@ -364,7 +364,7 @@ void vms_shm_buffer_set_reader_is_ready(vms_shm_buffer *buff) { "No events registered before going to the ready state"); if (!buff->shmbuffer->info.destroyed) - buff->shmbuffer->info.flags |= READER_IS_READY; + vms_shm_buffer_set_flags(buff, READER_IS_READY); } /* set the ID of the last processed event */ From c67fce23449a897b5ecc57f0249a7fc1e960f7e7 Mon Sep 17 00:00:00 2001 From: Marek Chalupa Date: Thu, 26 Oct 2023 13:13:55 +0200 Subject: [PATCH 29/34] Reformat the code with clang-format There were some unformated changes. --- include/vamos-buffers/core/stream.h | 3 +- include/vamos-buffers/cpp/event.h | 8 +-- include/vamos-buffers/cpp/ring_buffer.h | 5 +- include/vamos-buffers/cpp/vector.h | 58 +++++++++------------ include/vamos-buffers/streams/stream-funs.h | 2 +- src/core/event.c | 4 +- src/core/stream.c | 15 +++--- src/shmbuf/buffer.c | 14 ++--- src/streams/stream-funs.c | 2 +- 9 files changed, 53 insertions(+), 58 deletions(-) diff --git a/include/vamos-buffers/core/stream.h b/include/vamos-buffers/core/stream.h index c2c0e698..28dd1221 100644 --- a/include/vamos-buffers/core/stream.h +++ b/include/vamos-buffers/core/stream.h @@ -73,7 +73,8 @@ size_t vms_stream_id(vms_stream *); struct vms_event_record *vms_stream_get_avail_events(vms_stream *, size_t *); struct vms_event_record *vms_stream_get_event_record(vms_stream *, vms_kind); -struct vms_event_record *vms_stream_get_event_record_no_cache(vms_stream *, vms_kind); +struct vms_event_record *vms_stream_get_event_record_no_cache(vms_stream *, + vms_kind); /* register to receiving events particular events */ int vms_stream_register_event(vms_stream *, const char *, vms_kind); diff --git a/include/vamos-buffers/cpp/event.h b/include/vamos-buffers/cpp/event.h index b35b9f89..c75d5106 100644 --- a/include/vamos-buffers/cpp/event.h +++ b/include/vamos-buffers/cpp/event.h @@ -18,13 +18,9 @@ struct Event : public vms_event { return Event(vms_event_get_done_kind(), id); } - static constexpr vms_kind holeKind() { - return VMS_EVENT_HOLE_KIND; - } + static constexpr vms_kind holeKind() { return VMS_EVENT_HOLE_KIND; } - static constexpr vms_kind doneKind() { - return VMS_EVENT_DONE_KIND; - } + static constexpr vms_kind doneKind() { return VMS_EVENT_DONE_KIND; } static constexpr vms_kind lastSpecialKind() { return VMS_EVENT_LAST_SPECIAL_KIND; diff --git a/include/vamos-buffers/cpp/ring_buffer.h b/include/vamos-buffers/cpp/ring_buffer.h index 16906e4f..d0bef452 100644 --- a/include/vamos-buffers/cpp/ring_buffer.h +++ b/include/vamos-buffers/cpp/ring_buffer.h @@ -312,8 +312,9 @@ class RingBufferReader : public RingBufferAccessor { } size_t available() const { - return _written_num(this->rb.load_head(std::memory_order_relaxed), - this->rb.load_tail(std::memory_order_relaxed)); + return _written_num( + this->rb.load_head(std::memory_order_relaxed), + this->rb.load_tail(std::memory_order_relaxed)); } size_t acquire(size_t &n) { diff --git a/include/vamos-buffers/cpp/vector.h b/include/vamos-buffers/cpp/vector.h index 64f1b2a1..a1ad9a74 100644 --- a/include/vamos-buffers/cpp/vector.h +++ b/include/vamos-buffers/cpp/vector.h @@ -13,13 +13,10 @@ class vec { size_t _size{0}; size_t _alloc_size{0}; + public: + ~vec() { free(_data); } -public: - ~vec() { - free(_data); - } - - void swap(vec& rhs) { + void swap(vec &rhs) { auto tmp = rhs; rhs = *this; *this = rhs; @@ -60,21 +57,21 @@ class vec { ElemTy *extend_n(size_t n) { if (_size >= _alloc_size) { _alloc_size += n; - _data = static_cast(realloc(_data, _alloc_size * elem_size())); + _data = static_cast( + realloc(_data, _alloc_size * elem_size())); assert(_data != nullptr && "Memory re-allocation failed"); } assert((_size < _alloc_size) && "Vector too small"); return _data[_size++]; } - ElemTy *extend() { - return extend_n(16); - } + ElemTy *extend() { return extend_n(16); } size_t push(const ElemTy &e) { if (_size >= _alloc_size) { _alloc_size += 10; - _data = static_cast(realloc(_data, _alloc_size * elem_size())); + _data = static_cast( + realloc(_data, _alloc_size * elem_size())); assert(_data != nullptr && "Memory re-allocation failed"); } assert((_size < _alloc_size) && "Vector too small"); @@ -82,14 +79,13 @@ class vec { return _size; } - size_t push(const ElemTy *e) { - return push(*e); - } + size_t push(const ElemTy *e) { return push(*e); } size_t push(ElemTy &&e) { if (_size >= _alloc_size) { _alloc_size += 10; - _data = static_cast(realloc(_data, _alloc_size * elem_size())); + _data = static_cast( + realloc(_data, _alloc_size * elem_size())); assert(_data != nullptr && "Memory re-allocation failed"); } assert((_size < _alloc_size) && "Vector too small"); @@ -98,8 +94,8 @@ class vec { } /** - * Remove the last element from the vector (decrease its size by 1) - */ + * Remove the last element from the vector (decrease its size by 1) + */ size_t pop() { return --_size; } void clear() { _size = 0; } @@ -108,18 +104,18 @@ class vec { template class _iterator { - protected: + protected: VecTy *v; size_t pos; static const size_t npos = ~static_cast(0); - _iterator(VecTy& v0) : v(&v0), pos(v0._size == 0 ? npos : 0) {} + _iterator(VecTy &v0) : v(&v0), pos(v0._size == 0 ? npos : 0) {} _iterator() : pos(npos) {} - _iterator(const _iterator&) = default; + _iterator(const _iterator &) = default; friend class vec; - public: + public: _iterator &operator++() { ++pos; if (pos == v->_size) @@ -133,27 +129,25 @@ class vec { return tmp; } - bool operator==(const _iterator& rhs) const { - return pos == rhs.pos; - } - bool operator!=(const _iterator& rhs) const { return !operator==(rhs); } + bool operator==(const _iterator &rhs) const { return pos == rhs.pos; } + bool operator!=(const _iterator &rhs) const { return !operator==(rhs); } }; struct iterator : public _iterator { - iterator(vec& v) : _iterator(v) {} + iterator(vec &v) : _iterator(v) {} iterator() : _iterator() {} - iterator(const iterator&) = default; + iterator(const iterator &) = default; - ElemTy& operator*() { return this->v->_data[this->pos]; } + ElemTy &operator*() { return this->v->_data[this->pos]; } ElemTy *operator->() { return &this->v->_data[this->pos]; } }; struct const_iterator : public _iterator { - const_iterator(const vec& v) : _iterator(v) {} + const_iterator(const vec &v) : _iterator(v) {} const_iterator() : _iterator() {} - const_iterator(const const_iterator&) = default; + const_iterator(const const_iterator &) = default; - const ElemTy& operator*() const { return this->v->_data[this->pos]; } + const ElemTy &operator*() const { return this->v->_data[this->pos]; } const ElemTy *operator->() const { return &this->v->_data[this->pos]; } }; @@ -163,6 +157,6 @@ class vec { const_iterator end() const { return const_iterator(); } }; -} // namespace vamos +} // namespace vamos #endif diff --git a/include/vamos-buffers/streams/stream-funs.h b/include/vamos-buffers/streams/stream-funs.h index d7bde3c3..26d960d2 100644 --- a/include/vamos-buffers/streams/stream-funs.h +++ b/include/vamos-buffers/streams/stream-funs.h @@ -28,7 +28,7 @@ vms_event_funcall *funs_get_next_event(vms_stream *stream); vms_stream *vms_create_funs_stream(const char *key, const char *name); struct vms_event_record *vms_funs_stream_get_event_spec(vms_stream_funs *stream, - vms_kind kind); + vms_kind kind); void vms_event_funcall_release(vms_event_funcall *fev); diff --git a/src/core/event.c b/src/core/event.c index 62ad9129..397aa39a 100644 --- a/src/core/event.c +++ b/src/core/event.c @@ -46,8 +46,6 @@ vms_kind vms_event_get_last_special_kind(void) { vms_eventid vms_event_id(const vms_event *event) { return event->id; } -void vms_event_set_id(vms_event *event, vms_eventid id) { - event->id = id; -} +void vms_event_set_id(vms_event *event, vms_eventid id) { event->id = id; } vms_kind vms_event_kind(const vms_event *event) { return event->kind; } diff --git a/src/core/stream.c b/src/core/stream.c index 704650a4..af0cd7c8 100644 --- a/src/core/stream.c +++ b/src/core/stream.c @@ -180,7 +180,8 @@ vms_stream *vms_stream_create_substream( return substream; } -struct vms_event_record *vms_stream_get_avail_events(vms_stream *s, size_t *sz) { +struct vms_event_record *vms_stream_get_avail_events(vms_stream *s, + size_t *sz) { return vms_shm_buffer_get_avail_events(s->incoming_events_buffer, sz); } @@ -197,7 +198,7 @@ static vms_kind get_max_kind(struct vms_event_record *recs, size_t size) { } struct vms_event_record *vms_stream_get_event_record(vms_stream *stream, - vms_kind kind) { + vms_kind kind) { assert(kind > 0 && "Got invalid kind"); struct vms_event_record *rec = NULL; if (stream->events_cache) { @@ -219,7 +220,8 @@ struct vms_event_record *vms_stream_get_event_record(vms_stream *stream, : max_kind) + 1; assert(cache_sz > 0 && "Invalid cache size"); - stream->events_cache = malloc(cache_sz * sizeof(struct vms_event_record)); + stream->events_cache = + malloc(cache_sz * sizeof(struct vms_event_record)); /* cache elements that fit into the cache (cache is indexed by kinds) */ for (size_t i = 0; i < sz; ++i) { if (recs[i].kind < cache_sz) { @@ -232,8 +234,8 @@ struct vms_event_record *vms_stream_get_event_record(vms_stream *stream, } } -struct vms_event_record *vms_stream_get_event_record_no_cache(vms_stream *stream, - vms_kind kind) { +struct vms_event_record *vms_stream_get_event_record_no_cache( + vms_stream *stream, vms_kind kind) { size_t sz; struct vms_event_record *recs = vms_shm_buffer_get_avail_events(stream->incoming_events_buffer, &sz); @@ -295,7 +297,8 @@ size_t vms_stream_event_size(vms_stream *s) { return s->event_size; } int vms_stream_register_event(vms_stream *stream, const char *name, size_t kind) { - assert(kind > vms_event_get_last_special_kind() && "Invalid event kind, it overlaps special kinds"); + assert(kind > vms_event_get_last_special_kind() && + "Invalid event kind, it overlaps special kinds"); return vms_shm_buffer_register_event(stream->incoming_events_buffer, name, kind); } diff --git a/src/shmbuf/buffer.c b/src/shmbuf/buffer.c index 34f3a7d5..006609d2 100644 --- a/src/shmbuf/buffer.c +++ b/src/shmbuf/buffer.c @@ -2,6 +2,7 @@ #include #include +#include /* _mm_mfence */ #include #include #include @@ -15,8 +16,6 @@ #include #include -#include /* _mm_mfence */ - #include "buffer-private.h" #include "shm.h" #include "vamos-buffers/core/list.h" @@ -364,7 +363,7 @@ void vms_shm_buffer_set_reader_is_ready(vms_shm_buffer *buff) { "No events registered before going to the ready state"); if (!buff->shmbuffer->info.destroyed) - vms_shm_buffer_set_flags(buff, READER_IS_READY); + vms_shm_buffer_set_flags(buff, READER_IS_READY); } /* set the ID of the last processed event */ @@ -628,11 +627,13 @@ void vms_shm_buffer_notify_dropped(vms_shm_buffer *buff, uint64_t begin_id, int vms_shm_buffer_register_event(vms_shm_buffer *b, const char *name, uint64_t kind) { - assert(kind > vms_event_get_last_special_kind() && "Invalid event kind, it overlaps special kinds"); + assert(kind > vms_event_get_last_special_kind() && + "Invalid event kind, it overlaps special kinds"); vms_shm_buffer_set_flags(b, EVENTS_REGISTERED); - struct vms_event_record *rec = vms_source_control_get_event(b->control, name); + struct vms_event_record *rec = + vms_source_control_get_event(b->control, name); if (rec == NULL) { return -1; } @@ -648,7 +649,8 @@ int vms_shm_buffer_register_events(vms_shm_buffer *b, size_t ev_nums, ...) { for (size_t i = 0; i < ev_nums; ++i) { const char *name = va_arg(ap, const char *); vms_kind kind = va_arg(ap, vms_kind); - assert(kind > vms_event_get_last_special_kind() && "Invalid event kind, it overlaps special kinds"); + assert(kind > vms_event_get_last_special_kind() && + "Invalid event kind, it overlaps special kinds"); if (vms_shm_buffer_register_event(b, name, kind) < 0) { va_end(ap); return -1; diff --git a/src/streams/stream-funs.c b/src/streams/stream-funs.c index 2e0a9cdc..9c09671f 100644 --- a/src/streams/stream-funs.c +++ b/src/streams/stream-funs.c @@ -42,7 +42,7 @@ const char *vms_stream_funs_get_str(vms_stream_funs *fstream, uint64_t elem) { } struct vms_event_record *vms_funs_stream_get_event_spec(vms_stream_funs *stream, - vms_kind kind) { + vms_kind kind) { for (size_t i = 0; i < stream->spec_count; ++i) if (stream->events[i].kind == kind) return stream->events + i; From 77fd61a9b25b3b9e292c64942e1c130735351e42 Mon Sep 17 00:00:00 2001 From: Marek Chalupa Date: Tue, 31 Oct 2023 13:08:36 +0100 Subject: [PATCH 30/34] stream-generic: add a `const` qualifier --- include/vamos-buffers/streams/stream-generic.h | 2 +- src/streams/stream-generic.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/vamos-buffers/streams/stream-generic.h b/include/vamos-buffers/streams/stream-generic.h index 4394d84e..0dfb8c37 100644 --- a/include/vamos-buffers/streams/stream-generic.h +++ b/include/vamos-buffers/streams/stream-generic.h @@ -24,6 +24,6 @@ typedef struct _vms_stream_generic { } vms_stream_generic; vms_stream *vms_create_generic_stream(const char *key, const char *name, - vms_stream_hole_handling *hole_handling); + const vms_stream_hole_handling *hole_handling); #endif /* SHMN_STREAM_GENERIC_H_ */ diff --git a/src/streams/stream-generic.c b/src/streams/stream-generic.c index 2bafb473..ecc716bd 100644 --- a/src/streams/stream-generic.c +++ b/src/streams/stream-generic.c @@ -23,7 +23,7 @@ static void generic_alter(vms_stream *stream, vms_event *in, vms_event *out) { } vms_stream *vms_create_generic_stream(const char *key, const char *name, - vms_stream_hole_handling *hole_handling) { + const vms_stream_hole_handling *hole_handling) { vms_stream_generic *ss = malloc(sizeof *ss); vms_shm_buffer *shmbuffer = vms_shm_buffer_connect(key); assert(shmbuffer && "Getting the shm buffer failed"); From c30af97e2e6bca5f958c1380e857b1cf5e5ee3a6 Mon Sep 17 00:00:00 2001 From: Marek Chalupa Date: Fri, 3 Nov 2023 23:39:59 +0100 Subject: [PATCH 31/34] Rename vms_stream_fetch Reflect if it is dropping or not. --- include/vamos-buffers/core/arbiter.h | 8 ++++-- include/vamos-buffers/core/vector-macro.h | 17 ++++++++++++ src/core/arbiter.c | 34 ++++++++++++++++++----- src/core/shamon.c | 2 +- tests/fetch-test-2.c | 4 +-- tests/fetch-test-3.c | 4 +-- tests/fetch-test.c | 4 +-- 7 files changed, 56 insertions(+), 17 deletions(-) diff --git a/include/vamos-buffers/core/arbiter.h b/include/vamos-buffers/core/arbiter.h index edd5a9c3..788b35de 100644 --- a/include/vamos-buffers/core/arbiter.h +++ b/include/vamos-buffers/core/arbiter.h @@ -62,10 +62,12 @@ size_t vms_arbiter_buffer_drop_older_than(vms_arbiter_buffer *buffer, vms_eventid id); bool vms_arbiter_buffer_pop(vms_arbiter_buffer *q, void *buff); -void *vms_stream_fetch(vms_stream *stream, vms_arbiter_buffer *buffer); +void *vms_stream_fetch(vms_stream *stream); +void *vms_stream_fetch_dropping(vms_stream *stream, vms_arbiter_buffer *buffer); -void *vms_stream_filter_fetch(vms_stream *stream, vms_arbiter_buffer *buffer, - vms_stream_filter_fn filter); +void *vms_stream_fetch_dropping_filter(vms_stream *stream, + vms_arbiter_buffer *buffer, + vms_stream_filter_fn filter); bool vms_arbiter_buffer_is_done(vms_arbiter_buffer *buffer); diff --git a/include/vamos-buffers/core/vector-macro.h b/include/vamos-buffers/core/vector-macro.h index ff6ee2f1..8630d94e 100644 --- a/include/vamos-buffers/core/vector-macro.h +++ b/include/vamos-buffers/core/vector-macro.h @@ -149,6 +149,23 @@ --VEC_SIZE(vec); \ } while (0) +/** + * Remove the first occurence of `elem` from `vec`. `elem` must be a simple + * type, since we use == with it. + * IMPORTANT: The removal does shuffle the elements, in particular, + * the last element will be copied to the place of the removed one. + */ +#define VEC_UNORD_REMOVE(vec, elem) \ + do { \ + for (int i = 0; i < VEC_SIZE(vec); ++i) { \ + if (VEC_AT(i) == elem) { \ + vec[i] = vec[VEC_SIZE(vec) - 1]; \ + --VEC_SIZE(vec); \ + break; \ + } \ + } \ + while (0) + /** * Return the pointer to the top element in the vector. * There is no check if there is any element. If there is none, diff --git a/src/core/arbiter.c b/src/core/arbiter.c index ebf33005..0fbc5c98 100644 --- a/src/core/arbiter.c +++ b/src/core/arbiter.c @@ -567,7 +567,26 @@ static bool handle_dropping_event(vms_stream *stream, } /* wait for an event on the 'stream' */ -void *vms_stream_fetch(vms_stream *stream, vms_arbiter_buffer *buffer) { +void *vms_stream_fetch(vms_stream *stream) { + void *ev; + if ((ev = get_event(stream))) { + assert(ev && "Dont have event"); + assert(!vms_event_is_hole((vms_event *)ev) && "Got dropped event"); + assert(vms_event_id(ev) == ++stream->last_event_id && + "IDs are inconsistent"); + /* + printf("FETCH: read event { kind = %lu, id = %lu}\n", + ((vms_event*)ev)->kind, + ((vms_event*)ev)->id); + */ + } + + return ev; +} + +/* Wait for an event on the 'stream'. If `buffer` is full, start dropping. */ +void *vms_stream_fetch_dropping(vms_stream *stream, + vms_arbiter_buffer *buffer) { void *ev; size_t last_ev_id = 1; while (1) { @@ -594,13 +613,14 @@ void *vms_stream_fetch(vms_stream *stream, vms_arbiter_buffer *buffer) { return ev; } - /* got to next iteration to try the next event */ + /* go to next iteration to try the next event */ } } /* FIXME: do not duplicate the code */ -void *vms_stream_filter_fetch(vms_stream *stream, vms_arbiter_buffer *buffer, - vms_stream_filter_fn filter) { +void *vms_stream_fetch_dropping_filter(vms_stream *stream, + vms_arbiter_buffer *buffer, + vms_stream_filter_fn filter) { void *ev; size_t last_ev_id = 1; while (1) { @@ -634,7 +654,7 @@ void *vms_stream_filter_fetch(vms_stream *stream, vms_arbiter_buffer *buffer, return ev; } - /* got to next iteration to try the next event */ + /* go to next iteration to try the next event */ } } @@ -683,10 +703,10 @@ void vms_arbiter_buffer_dump_stats(vms_arbiter_buffer *buffer) { s->last_event_id); COLOR_RESET #endif - fprintf(stderr, " vms_stream_fetch() fetched %lu events\n", + fprintf(stderr, " vms_stream_fetch*() fetched %lu events\n", s->fetched_events); fprintf(stderr, - " vms_stream_fetch() totally dropped %lu events in %lu holes\n", + " vms_stream_fetch*() totally dropped %lu events in %lu holes\n", buffer->total_dropped_num, buffer->total_dropped_times); fprintf(stderr, " Last event was drop: %s\n", buffer->last_was_drop ? "true" : "false"); diff --git a/src/core/shamon.c b/src/core/shamon.c index f1ce004f..f4d22c1d 100644 --- a/src/core/shamon.c +++ b/src/core/shamon.c @@ -51,7 +51,7 @@ static int default_buffer_manager_thrd(void *data) { void *ev, *out; while (1) { - ev = vms_stream_fetch(stream, buffer); + ev = vms_stream_fetch_dropping(stream, buffer); if (!ev) { break; } diff --git a/tests/fetch-test-2.c b/tests/fetch-test-2.c index 27a38b16..a46ee96b 100644 --- a/tests/fetch-test-2.c +++ b/tests/fetch-test-2.c @@ -107,7 +107,7 @@ int main(void) { struct event* ev; for (size_t i = 1; i <= 10000; ++i) { - ev = vms_stream_fetch(stream, arbiter_buffer); + ev = vms_stream_fetch_dropping(stream, arbiter_buffer); /* there should be no dropped event generated */ assert(vms_arbiter_buffer_size(arbiter_buffer) == 0); @@ -121,7 +121,7 @@ int main(void) { vms_stream_consume(stream, 1); } - ev = vms_stream_fetch(stream, arbiter_buffer); + ev = vms_stream_fetch_dropping(stream, arbiter_buffer); assert(!ev); main_finished = 1; diff --git a/tests/fetch-test-3.c b/tests/fetch-test-3.c index ca245c5d..ac50c794 100644 --- a/tests/fetch-test-3.c +++ b/tests/fetch-test-3.c @@ -99,7 +99,7 @@ int main(void) { struct event *ev; struct event *out; for (size_t i = 0; i < 10000; ++i) { - ev = vms_stream_fetch(stream, arbiter_buffer); + ev = vms_stream_fetch_dropping(stream, arbiter_buffer); /* there should be no dropped event generated * since we are outputing the events into another buffer */ assert(vms_arbiter_buffer_size(arbiter_buffer) == 0); @@ -121,7 +121,7 @@ int main(void) { vms_stream_consume(stream, 1); } - ev = vms_stream_fetch(stream, arbiter_buffer); + ev = vms_stream_fetch_dropping(stream, arbiter_buffer); assert(!ev); main_finished = 1; diff --git a/tests/fetch-test.c b/tests/fetch-test.c index 0620ecf8..bfb2cc47 100644 --- a/tests/fetch-test.c +++ b/tests/fetch-test.c @@ -57,7 +57,7 @@ int main(void) { struct event *ev; for (int i = 0; i < 4; ++i) { - ev = vms_stream_fetch(stream, arbiter_buffer); + ev = vms_stream_fetch_dropping(stream, arbiter_buffer); assert(ev); assert(ev->n == i); // printf("POP Event: {{%lu, %lu}, %d}\n", ev->base.id, ev->base.kind, @@ -65,7 +65,7 @@ int main(void) { vms_stream_consume(stream, 1); } - ev = vms_stream_fetch(stream, arbiter_buffer); + ev = vms_stream_fetch_dropping(stream, arbiter_buffer); assert(!ev); pthread_join(tid, NULL); From 7049abb4b6c883998d754fc82c16bc8c6d86c44a Mon Sep 17 00:00:00 2001 From: Marek Chalupa Date: Wed, 24 Apr 2024 08:46:11 +0200 Subject: [PATCH 32/34] signatures: fix a comment --- include/vamos-buffers/core/signatures.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/vamos-buffers/core/signatures.h b/include/vamos-buffers/core/signatures.h index b01e7b89..30a90f54 100644 --- a/include/vamos-buffers/core/signatures.h +++ b/include/vamos-buffers/core/signatures.h @@ -6,7 +6,7 @@ /* * c = (unsigned) char - * s = (unsigned) short + * h = (unsigned) short * i = (unsigned) int * l = (unsigned) long * f = float From b8d53d03edae0374cb7e832184cd875cb2f7db40 Mon Sep 17 00:00:00 2001 From: Marek Chalupa Date: Wed, 24 Apr 2024 08:46:22 +0200 Subject: [PATCH 33/34] stream: fix compilation --- src/core/stream.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/core/stream.c b/src/core/stream.c index af0cd7c8..08e40abe 100644 --- a/src/core/stream.c +++ b/src/core/stream.c @@ -1,8 +1,10 @@ #include "vamos-buffers/core/stream.h" +#include "vamos-buffers/core/event.h" #include #include #include +#include #include #include "vamos-buffers/core/utils.h" From d9529088b2306f5d4a1824ce9d1a335631611737 Mon Sep 17 00:00:00 2001 From: Marek Chalupa Date: Wed, 24 Apr 2024 08:47:37 +0200 Subject: [PATCH 34/34] A tiny reformating of streams --- include/vamos-buffers/streams/stream-generic.h | 5 +++-- src/core/stream.c | 4 ++-- src/streams/stream-generic.c | 5 +++-- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/include/vamos-buffers/streams/stream-generic.h b/include/vamos-buffers/streams/stream-generic.h index 0dfb8c37..04514052 100644 --- a/include/vamos-buffers/streams/stream-generic.h +++ b/include/vamos-buffers/streams/stream-generic.h @@ -23,7 +23,8 @@ typedef struct _vms_stream_generic { vms_shm_buffer *shmbuffer; } vms_stream_generic; -vms_stream *vms_create_generic_stream(const char *key, const char *name, - const vms_stream_hole_handling *hole_handling); +vms_stream *vms_create_generic_stream( + const char *key, const char *name, + const vms_stream_hole_handling *hole_handling); #endif /* SHMN_STREAM_GENERIC_H_ */ diff --git a/src/core/stream.c b/src/core/stream.c index 08e40abe..1e7a24b3 100644 --- a/src/core/stream.c +++ b/src/core/stream.c @@ -1,12 +1,12 @@ #include "vamos-buffers/core/stream.h" -#include "vamos-buffers/core/event.h" #include #include -#include #include +#include #include +#include "vamos-buffers/core/event.h" #include "vamos-buffers/core/utils.h" #include "vamos-buffers/core/vector-macro.h" #include "vamos-buffers/shmbuf/buffer.h" diff --git a/src/streams/stream-generic.c b/src/streams/stream-generic.c index ecc716bd..63a9beb9 100644 --- a/src/streams/stream-generic.c +++ b/src/streams/stream-generic.c @@ -22,8 +22,9 @@ static void generic_alter(vms_stream *stream, vms_event *in, vms_event *out) { memcpy(out, in, stream->event_size); } -vms_stream *vms_create_generic_stream(const char *key, const char *name, - const vms_stream_hole_handling *hole_handling) { +vms_stream *vms_create_generic_stream( + const char *key, const char *name, + const vms_stream_hole_handling *hole_handling) { vms_stream_generic *ss = malloc(sizeof *ss); vms_shm_buffer *shmbuffer = vms_shm_buffer_connect(key); assert(shmbuffer && "Getting the shm buffer failed");