1 | /* |
---|
2 | * |
---|
3 | * Copyright (c) 2007-2016 The University of Waikato, Hamilton, New Zealand. |
---|
4 | * All rights reserved. |
---|
5 | * |
---|
6 | * This file is part of libtrace. |
---|
7 | * |
---|
8 | * This code has been developed by the University of Waikato WAND |
---|
9 | * research group. For further information please see http://www.wand.net.nz/ |
---|
10 | * |
---|
11 | * libtrace is free software; you can redistribute it and/or modify |
---|
12 | * it under the terms of the GNU Lesser General Public License as published by |
---|
13 | * the Free Software Foundation; either version 3 of the License, or |
---|
14 | * (at your option) any later version. |
---|
15 | * |
---|
16 | * libtrace is distributed in the hope that it will be useful, |
---|
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
19 | * GNU Lesser General Public License for more details. |
---|
20 | * |
---|
21 | * You should have received a copy of the GNU Lesser General Public License |
---|
22 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
23 | * |
---|
24 | * |
---|
25 | */ |
---|
26 | #include "sliding_window.h" |
---|
27 | |
---|
28 | #include <stdlib.h> |
---|
29 | #include <assert.h> |
---|
30 | #include <string.h> |
---|
31 | |
---|
32 | /** |
---|
33 | * Implements a sliding window via a ring buffer, this is a fixed size. |
---|
34 | * |
---|
35 | * @param rb A pointer to a ringbuffer structure. |
---|
36 | * @param size The maximum size of the ring buffer, note 1 of these slots are unusable. |
---|
37 | * @param mode The mode allows selection to use semaphores to signal when data |
---|
38 | * becomes available. LIBTRACE_RINGBUFFER_BLOCKING or LIBTRACE_RINGBUFFER_POLLING. |
---|
39 | * NOTE: this mainly applies to the blocking functions |
---|
40 | */ |
---|
41 | void libtrace_slidingwindow_init(libtrace_slidingwindow_t *sw, size_t size, uint64_t start_number) { |
---|
42 | sw->size = size; // All of this size can be used |
---|
43 | sw->start = 0; |
---|
44 | sw->elements = calloc(sw->size, sizeof(void*)); |
---|
45 | assert(sw->elements); |
---|
46 | memset((void *) sw->elements, 0, sizeof(void*) * sw->size); |
---|
47 | sw->start_number = start_number; |
---|
48 | } |
---|
49 | |
---|
50 | /** |
---|
51 | * Destroys the ring buffer along with any memory allocated to it |
---|
52 | * @param rb The ringbuffer to destroy |
---|
53 | */ |
---|
54 | void libtrace_slidingwindow_destroy(libtrace_slidingwindow_t *sw) { |
---|
55 | sw->size = 0; |
---|
56 | sw->start = 0; |
---|
57 | sw->start_number = 0; |
---|
58 | free((void *)sw->elements); |
---|
59 | sw->elements = NULL; |
---|
60 | } |
---|
61 | |
---|
62 | |
---|
63 | /** |
---|
64 | * Performs a non-blocking write to the buffer, if their is no space |
---|
65 | * or the list is locked by another thread this will return immediately |
---|
66 | * without writing the value. Assumes that only one thread is writing. |
---|
67 | * Otherwise use libtrace_ringbuffer_try_swrite. |
---|
68 | * |
---|
69 | * @param rb a pointer to libtrace_ringbuffer structure |
---|
70 | * @param value the value to store |
---|
71 | * @return 1 if a object was written otherwise 0. |
---|
72 | */ |
---|
73 | int libtrace_slidingwindow_try_write(libtrace_slidingwindow_t *sw, uint64_t number, void* value) { |
---|
74 | uint64_t adjusted_number = number - sw->start_number; |
---|
75 | if (adjusted_number < sw->size) { |
---|
76 | // Add it |
---|
77 | sw->elements[(adjusted_number + sw->start) % sw->size] = value; |
---|
78 | return 1; |
---|
79 | } else { |
---|
80 | // Out of range don't add it |
---|
81 | return 0; |
---|
82 | } |
---|
83 | } |
---|
84 | |
---|
85 | /* |
---|
86 | static inline uint64_t libtrace_slidingwindow_get_min_number(libtrace_slidingwindow_t *sw) { |
---|
87 | return sw->start_number; |
---|
88 | } |
---|
89 | */ |
---|
90 | |
---|
91 | uint64_t libtrace_slidingwindow_read_ready(libtrace_slidingwindow_t *sw) { |
---|
92 | return sw->elements[sw->start] != NULL; |
---|
93 | } |
---|
94 | |
---|
95 | /** |
---|
96 | * Tries to read from the supplied buffer if it fails this and returns |
---|
97 | * 0 to indicate nothing was read. |
---|
98 | * |
---|
99 | * @param rb a pointer to libtrace_ringbuffer structure |
---|
100 | * @param out a pointer to a memory address where the returned item would be placed |
---|
101 | * @return 1 if a object was received otherwise 0, in this case out remains unchanged |
---|
102 | */ |
---|
103 | int libtrace_slidingwindow_try_read(libtrace_slidingwindow_t *sw, void ** value, uint64_t *number) { |
---|
104 | if (sw->elements[sw->start]) { |
---|
105 | *value = sw->elements[sw->start]; |
---|
106 | sw->elements[sw->start] = NULL; |
---|
107 | if (number) |
---|
108 | *number = sw->start_number; |
---|
109 | ++sw->start_number; |
---|
110 | sw->start = (sw->start + 1) % sw->size; |
---|
111 | return 1; |
---|
112 | } else { |
---|
113 | return 0; |
---|
114 | } |
---|
115 | } |
---|
116 | |
---|
117 | void libtrace_zero_slidingwindow(libtrace_slidingwindow_t * sw) |
---|
118 | { |
---|
119 | sw->start = 0; |
---|
120 | sw->start_number = 0; |
---|
121 | sw->size = 0; |
---|
122 | sw->elements = NULL; |
---|
123 | } |
---|