1 | #include "sliding_window.h" |
---|
2 | |
---|
3 | #include <stdlib.h> |
---|
4 | #include <assert.h> |
---|
5 | #include <string.h> |
---|
6 | |
---|
7 | /** |
---|
8 | * Implements a sliding window via a ring buffer, this is a fixed size. |
---|
9 | * |
---|
10 | * @param rb A pointer to a ringbuffer structure. |
---|
11 | * @param size The maximum size of the ring buffer, note 1 of these slots are unusable. |
---|
12 | * @param mode The mode allows selection to use semaphores to signal when data |
---|
13 | * becomes available. LIBTRACE_RINGBUFFER_BLOCKING or LIBTRACE_RINGBUFFER_POLLING. |
---|
14 | * NOTE: this mainly applies to the blocking functions |
---|
15 | */ |
---|
16 | inline void libtrace_slidingwindow_init(libtrace_slidingwindow_t *sw, int size, uint64_t start_number) { |
---|
17 | sw->size = size; // All of this size can be used |
---|
18 | sw->start = 0; |
---|
19 | sw->elements = calloc(sw->size, sizeof(void*)); |
---|
20 | assert(sw->elements); |
---|
21 | memset(sw->elements, 0, sizeof(void*) * sw->size); |
---|
22 | sw->start_number = start_number; |
---|
23 | } |
---|
24 | |
---|
25 | /** |
---|
26 | * Destroys the ring buffer along with any memory allocated to it |
---|
27 | * @param rb The ringbuffer to destroy |
---|
28 | */ |
---|
29 | inline void libtrace_slidingwindow_destroy(libtrace_slidingwindow_t *sw) { |
---|
30 | sw->size = 0; |
---|
31 | sw->start = 0; |
---|
32 | sw->start_number = 0; |
---|
33 | free((void *)sw->elements); |
---|
34 | sw->elements = NULL; |
---|
35 | } |
---|
36 | |
---|
37 | |
---|
38 | /** |
---|
39 | * Performs a non-blocking write to the buffer, if their is no space |
---|
40 | * or the list is locked by another thread this will return immediately |
---|
41 | * without writing the value. Assumes that only one thread is writing. |
---|
42 | * Otherwise use libtrace_ringbuffer_try_swrite. |
---|
43 | * |
---|
44 | * @param rb a pointer to libtrace_ringbuffer structure |
---|
45 | * @param value the value to store |
---|
46 | * @return 1 if a object was written otherwise 0. |
---|
47 | */ |
---|
48 | inline int libtrace_slidingwindow_try_write(libtrace_slidingwindow_t *sw, uint64_t number, void* value) { |
---|
49 | uint64_t adjusted_number = number - sw->start_number; |
---|
50 | if (adjusted_number < sw->size) { |
---|
51 | // Add it |
---|
52 | sw->elements[(adjusted_number + sw->start) % sw->size] = value; |
---|
53 | return 1; |
---|
54 | } else { |
---|
55 | // Out of range don't add it |
---|
56 | return 0; |
---|
57 | } |
---|
58 | } |
---|
59 | |
---|
60 | static inline uint64_t libtrace_slidingwindow_get_min_number(libtrace_slidingwindow_t *sw) { |
---|
61 | return sw->start_number; |
---|
62 | } |
---|
63 | |
---|
64 | inline uint64_t libtrace_slidingwindow_read_ready(libtrace_slidingwindow_t *sw) { |
---|
65 | return sw->elements[sw->start] != NULL; |
---|
66 | } |
---|
67 | |
---|
68 | /** |
---|
69 | * Tries to read from the supplied buffer if it fails this and returns |
---|
70 | * 0 to indicate nothing was read. |
---|
71 | * |
---|
72 | * @param rb a pointer to libtrace_ringbuffer structure |
---|
73 | * @param out a pointer to a memory address where the returned item would be placed |
---|
74 | * @return 1 if a object was received otherwise 0, in this case out remains unchanged |
---|
75 | */ |
---|
76 | inline int libtrace_slidingwindow_try_read(libtrace_slidingwindow_t *sw, void ** value, uint64_t *number) { |
---|
77 | if (sw->elements[sw->start]) { |
---|
78 | *value = sw->elements[sw->start]; |
---|
79 | sw->elements[sw->start] = NULL; |
---|
80 | if (number) |
---|
81 | *number = sw->start_number; |
---|
82 | ++sw->start_number; |
---|
83 | sw->start = (sw->start + 1) % sw->size; |
---|
84 | return 1; |
---|
85 | } else { |
---|
86 | return 0; |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | inline void libtrace_zero_slidingwindow(libtrace_slidingwindow_t * sw) |
---|
91 | { |
---|
92 | sw->start = 0; |
---|
93 | sw->start_number = 0; |
---|
94 | sw->size = 0; |
---|
95 | sw->elements = NULL; |
---|
96 | } |
---|