aboutsummaryrefslogtreecommitdiff
path: root/drivers/video/arm/v5xx/resource/mve_rsrc_circular_buffer.h
blob: 48b5b9d95e86ae831aef80c813eb3e73f4dcafa0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
 * (C) COPYRIGHT ARM Limited. All rights reserved.
 *
 * This program is free software and is provided to you under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation, and any use by you of this program is subject to the terms
 * of such GNU licence.
 *
 * A copy of the licence is included with the program, and can also be obtained
 * from Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 *
 */

#ifndef MVE_RSRC_CIRCULAR_BUFFER
#define MVE_RSRC_CIRCULAR_BUFFER

#ifdef EMULATOR
#include "emulator_userspace.h"
#else
#include <linux/types.h>
#endif

/**
 * Structure used to represent a circular buffer.
 */
struct mver_circular_buffer
{
    uint32_t size;  /**< Number of 32-bit entries in the circular buffer */
    uint32_t r_pos; /**< Tail position (read position) */
    uint32_t w_pos; /**< Head position (write position) */

    void **data;    /**< Pointer to the actual circular buffer */
};

/**
 * Iterate over all elements in the circular buffer.
 */
#define mver_circular_buffer_for_each(index, ptr, type, cb) \
    for (index = 0; ptr = (type)cb->data[(cb->r_pos + index) % cb->size], index < mver_circular_buffer_get_num_entries(cb); ++index)

/**
 * Create a circular buffer of the given size.
 * @param size Number of 32-bit elements in the circular buffer.
 * @return Pointer to an initialized mver_circular_buffer, NULL on failure
 */
struct mver_circular_buffer *mver_circular_buffer_create(uint32_t size);

/**
 * Destroy a circular buffer. Frees all resources that were allocated during
 * the creation of the circular buffer. Note that the elements themselves are
 * not freed.
 * @param cb The circular buffer to destroy
 */
void mver_circular_buffer_destroy(struct mver_circular_buffer *cb);

/**
 * Add an element to the circular buffer.
 * @param cb   The circular buffer to add the element to
 * @param item The item to add to the circular buffer
 * @return True on success, false if the element couldn't be added
 */
bool mver_circular_buffer_add(struct mver_circular_buffer *cb, void *item);

/**
 * Remove the tail element in the circular buffer and return it.
 * @param cb   The circular buffer
 * @param data This pointer will point to the removed element
 * @return True if an element was removed. False if the circular buffer was empty
 */
bool mver_circular_buffer_remove(struct mver_circular_buffer *cb, void **data);

/**
 * Get the number of items in the circular buffer.
 * @param cb The circular buffer
 * @return The number of items in the circular buffer
 */
uint32_t mver_circular_buffer_get_num_entries(struct mver_circular_buffer *cb);

/**
 * Get the tail element without removing it from the circular buffer.
 * @param cb   The circular buffer
 * @param data This pointer will point to the tail element
 * @return True if the circular buffer contains atleast one element, false otherwise
 */
bool mver_circular_buffer_peek(struct mver_circular_buffer *cb, void **data);

/**
 * Remove all occurances of a certain item.
 * @param cb   The circular buffer
 * @param data The item to be removed
 */
void mver_circular_buffer_remove_all_occurences(struct mver_circular_buffer *cb, void *data);

#endif /* MVE_RSRC_CIRCULAR_BUFFER */