summaryrefslogtreecommitdiff
path: root/cesar/lib/circular_buffer.h
blob: bb01c9a771fde31cf1b960f04191906d1439148e (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
#ifndef lib_circular_buffer_h
#define lib_circular_buffer_h
/* Cesar project {{{
 *
 * Copyright (C) 2008 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    lib/circular_buffer.h
 * \brief   Circular buffer list
 * \ingroup lib
 *
 * Provides a circular buffer list API.
 * Reserve the real size of the buffer just after the declaration of the
 * circular buffer to not have some memory problems.
 */

struct circular_buffer_t
{
    /** Number of slots of power of 2. */
    uint num_slots;
    /** head. */
    uint head;
    /** tail. */
    uint tail;
    /** buffer. */
    void **buffer;
    /** number of elements in the list. */
    uint nb_elements;
};
typedef struct circular_buffer_t circular_buffer_t;

/** 
 * Initialize the buffer address list
 * 
 * \param  list the buffer address list to initiliaze
 * \param  buffer  the circular buffer to use.
 * \param  number_slots the quantity of slots.
 */
void
circular_buffer_init (circular_buffer_t *list, void *buffer, uint number_slots);

/**
 * Add an address to the buffer address buffer list
 * 
 * \param  ctx the ctx containing the list.
 * \param  address the address to add to the list
 * \param  number_of_slots the quantity of address it can keep in the list.
 * 
 * \return  true if the buffer had been added, false otherwise
 */
bool
circular_buffer_add (circular_buffer_t *ctx, void *address);

/**
 * Peek the first element of the list without removing it.
 * 
 * \param  ctx the context.
 */
void*
circular_buffer_peek (circular_buffer_t *ctx);

/**
 * Get the current address and go to the next one.
 * 
 * \param  ctx the ctx containing the list.
 * \param  number_of_slots the quantity of address it can keep in the list.
 */
void*
circular_buffer_get (circular_buffer_t *ctx);


#endif /* lib_circular_buffer_h */