source: lib/data-struct/sliding_window.c @ 2193905

develop
Last change on this file since 2193905 was 2193905, checked in by Jacob Van Walraven <jcv9@…>, 2 years ago

Apply changes required for pull request #81

  • Property mode set to 100644
File size: 4.0 KB
Line 
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 <string.h>
30
31#include <stdio.h>
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 */
41void 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        if (!sw->elements) {
46                fprintf(stderr, "Unable to allocate memory for sw->elements in libtrace_slidingwindow_init()\n");
47                return;
48        }
49        memset((void *) sw->elements, 0, sizeof(void*) * sw->size);
50        sw->start_number = start_number;
51}
52
53/**
54 * Destroys the ring buffer along with any memory allocated to it
55 * @param rb The ringbuffer to destroy
56 */
57void libtrace_slidingwindow_destroy(libtrace_slidingwindow_t *sw) {
58        sw->size = 0;
59        sw->start = 0;
60        sw->start_number = 0;
61        free((void *)sw->elements);
62        sw->elements = NULL;
63}
64
65
66/**
67 * Performs a non-blocking write to the buffer, if their is no space
68 * or the list is locked by another thread this will return immediately
69 * without writing the value. Assumes that only one thread is writing.
70 * Otherwise use libtrace_ringbuffer_try_swrite.
71 *
72 * @param rb a pointer to libtrace_ringbuffer structure
73 * @param value the value to store
74 * @return 1 if a object was written otherwise 0.
75 */
76int libtrace_slidingwindow_try_write(libtrace_slidingwindow_t *sw, uint64_t number, void* value) {
77        uint64_t adjusted_number = number - sw->start_number;
78        if (adjusted_number < sw->size) {
79                // Add it
80                sw->elements[(adjusted_number + sw->start) % sw->size] = value;
81                return 1;
82        } else {
83                // Out of range don't add it
84                return 0;
85        }
86}
87
88/*
89static inline uint64_t libtrace_slidingwindow_get_min_number(libtrace_slidingwindow_t *sw) {
90        return sw->start_number;
91}
92*/
93
94uint64_t libtrace_slidingwindow_read_ready(libtrace_slidingwindow_t *sw) {
95        return sw->elements[sw->start] != NULL;
96}
97
98/**
99 * Tries to read from the supplied buffer if it fails this and returns
100 * 0 to indicate nothing was read.
101 *
102 * @param rb a pointer to libtrace_ringbuffer structure
103 * @param out a pointer to a memory address where the returned item would be placed
104 * @return 1 if a object was received otherwise 0, in this case out remains unchanged
105 */
106int libtrace_slidingwindow_try_read(libtrace_slidingwindow_t *sw, void ** value, uint64_t *number) {
107        if (sw->elements[sw->start]) {
108                *value = sw->elements[sw->start];
109                sw->elements[sw->start] = NULL;
110                if (number)
111                        *number = sw->start_number;
112                ++sw->start_number;
113                sw->start = (sw->start + 1) % sw->size;
114                return 1;
115        } else {
116                return 0;
117        }
118}
119
120void libtrace_zero_slidingwindow(libtrace_slidingwindow_t * sw)
121{
122        sw->start = 0;
123        sw->start_number = 0;
124        sw->size = 0;
125        sw->elements = NULL;
126}
Note: See TracBrowser for help on using the repository browser.