File: buffer.c

package info (click to toggle)
libdvbpsi 1.3.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 3,632 kB
  • sloc: ansic: 19,087; makefile: 252; xml: 100; sh: 93
file content (200 lines) | stat: -rw-r--r-- 4,711 bytes parent folder | download | duplicates (4)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*****************************************************************************
 * buffer.c: buffer management
 *****************************************************************************
 * Copyright (C) 2011 M2X BV
 *
 * Authors: Jean-Paul Saman <jpsaman@videolan.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *****************************************************************************/

#include "config.h"

#include <stdlib.h>
#include <stdbool.h>
#include <pthread.h>

#if defined(HAVE_INTTYPES_H)
#   include <inttypes.h>
#elif defined(HAVE_STDINT_H)
#   include <stdint.h>
#endif

#include <sys/types.h>
#include <assert.h>

typedef int64_t mtime_t;

#include "buffer.h"

struct fifo_s
{
    pthread_mutex_t lock;
    pthread_cond_t  wait;
    bool       b_force_wake;
    ssize_t    i_count;
    size_t     i_size; /* fifo size in bytes */
    buffer_t  *p_first;
    buffer_t **pp_last;
};

/* */
buffer_t *buffer_new(size_t i_size)
{
    buffer_t *buffer = (buffer_t*)malloc(sizeof(buffer_t) + i_size);
    if (buffer == NULL) return NULL;
    buffer->i_size = i_size;
    buffer->i_date = 0;
    buffer->p_next = NULL;
    buffer->p_data = (uint8_t*)((uint8_t *)buffer + sizeof(buffer_t));
    return buffer;
}

void buffer_free(buffer_t *buffer)
{
    free(buffer);
    buffer = NULL;
}

/* Fifo */
fifo_t *fifo_new(void)
{
    fifo_t *fifo = (fifo_t *) malloc(sizeof(fifo_t));
    if (fifo == NULL) return NULL;

    fifo->i_count = 0;
    fifo->i_size = 0;
    fifo->b_force_wake = false;
    fifo->p_first = NULL;
    fifo->pp_last = &fifo->p_first;
    pthread_mutex_init(&fifo->lock, NULL);
    pthread_cond_init(&fifo->wait, NULL);
    return fifo;
}

void fifo_free(fifo_t *fifo)
{
    if (fifo == NULL)
        return;

    pthread_mutex_lock(&fifo->lock);
    buffer_t *p = fifo->p_first;
    if (p != NULL)
    {
        fifo->i_count = 0;
        fifo->p_first = NULL;
        fifo->pp_last = &fifo->p_first;
    }
    pthread_mutex_unlock(&fifo->lock);

    while (p != NULL)
    {
        buffer_t *buffer;

        buffer = p->p_next;
        buffer_free(p);
        p = buffer;
    }

    pthread_cond_destroy(&fifo->wait);
    pthread_mutex_destroy(&fifo->lock);

    free(fifo);
    fifo = NULL;
}

void fifo_wake(fifo_t *fifo)
{
    pthread_mutex_lock(&fifo->lock);
    if (fifo->p_first == NULL)
        fifo->b_force_wake = true;
    pthread_cond_broadcast(&fifo->wait);
    pthread_mutex_unlock(&fifo->lock);
}

ssize_t fifo_count(fifo_t *fifo)
{
    pthread_mutex_lock(&fifo->lock);
    ssize_t count = fifo->i_count;
    pthread_mutex_unlock(&fifo->lock);
    return count;
}

size_t fifo_size(fifo_t *fifo)
{
    pthread_mutex_lock(&fifo->lock);
    size_t size = fifo->i_size;
    pthread_mutex_unlock(&fifo->lock);
    return size;
}

void fifo_push(fifo_t *fifo, buffer_t *buffer)
{
    buffer_t *p_last;
    size_t i_depth = 0;

    if (buffer == NULL)
        return;

    pthread_mutex_lock(&fifo->lock);
    for (p_last = buffer; ; p_last = p_last->p_next)
    {
         i_depth ++;
         if (!p_last->p_next)
             break;
    }

    *fifo->pp_last = buffer;
    fifo->pp_last = &p_last->p_next;
    fifo->i_count += i_depth;
    fifo->i_size += buffer->i_size;

    assert(fifo->p_first != NULL);
    assert(fifo->pp_last  != NULL);

    pthread_cond_signal(&fifo->wait);
    pthread_mutex_unlock(&fifo->lock);
}

buffer_t *fifo_pop(fifo_t *fifo)
{
    buffer_t *buffer;

    pthread_mutex_lock(&fifo->lock);

    while ((fifo->p_first == NULL) && !fifo->b_force_wake)
        pthread_cond_wait(&fifo->wait, &fifo->lock);

    buffer = fifo->p_first;
    fifo->b_force_wake = false;
    if (buffer == NULL)
    {
        pthread_mutex_unlock(&fifo->lock);
        return NULL;
    }

    fifo->p_first = buffer->p_next;
    fifo->i_count--;
    fifo->i_size -= buffer->i_size;

    if (fifo->p_first == NULL)
    {
        fifo->pp_last = &fifo->p_first;
    }
    pthread_mutex_unlock(&fifo->lock);

    buffer->p_next = NULL;
    return buffer;
}