source: lib/data-struct/sliding_window.c @ 10553bf

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivelibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 10553bf was 10553bf, checked in by Shane Alcock <salcock@…>, 6 years ago

Fix various build errors and warnings on OS X

  • Property mode set to 100644
File size: 2.9 KB
Line 
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 */
16void libtrace_slidingwindow_init(libtrace_slidingwindow_t *sw, size_t 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((void *) 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 */
29void 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 */
48int 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/*
61static inline uint64_t libtrace_slidingwindow_get_min_number(libtrace_slidingwindow_t *sw) {
62        return sw->start_number;
63}
64*/
65
66uint64_t libtrace_slidingwindow_read_ready(libtrace_slidingwindow_t *sw) {
67        return sw->elements[sw->start] != NULL;
68}
69
70/**
71 * Tries to read from the supplied buffer if it fails this and returns
72 * 0 to indicate nothing was read.
73 *
74 * @param rb a pointer to libtrace_ringbuffer structure
75 * @param out a pointer to a memory address where the returned item would be placed
76 * @return 1 if a object was received otherwise 0, in this case out remains unchanged
77 */
78int libtrace_slidingwindow_try_read(libtrace_slidingwindow_t *sw, void ** value, uint64_t *number) {
79        if (sw->elements[sw->start]) {
80                *value = sw->elements[sw->start];
81                sw->elements[sw->start] = NULL;
82                if (number)
83                        *number = sw->start_number;
84                ++sw->start_number;
85                sw->start = (sw->start + 1) % sw->size;
86                return 1;
87        } else {
88                return 0;
89        }
90}
91
92void libtrace_zero_slidingwindow(libtrace_slidingwindow_t * sw)
93{
94        sw->start = 0;
95        sw->start_number = 0;
96        sw->size = 0;
97        sw->elements = NULL;
98}
Note: See TracBrowser for help on using the repository browser.