source: lib/data-struct/sliding_window.c @ 0a474e3

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

And more..

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