chiark / gitweb /
ensure tests avoid user config!
[disorder] / clients / playrtp-mem.c
CommitLineData
8e3fe3d8 1/*
2 * This file is part of DisOrder.
3 * Copyright (C) 2007 Richard Kettlewell
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 * USA
19 */
20/** @file clients/playrtp-mem.c
21 * @brief RTP player memory management
22 */
23
24#include <config.h>
25#include "types.h"
26
27#include <pthread.h>
28#include <assert.h>
29#include <string.h>
30
31#include "mem.h"
32#include "vector.h"
33#include "heap.h"
34#include "playrtp.h"
35
36/** @brief Linked list of free packets
37 *
38 * This is a linked list of formerly used packets. For preference we re-use
39 * packets that have already been used rather than unused ones, to limit the
40 * size of the program's working set. If there are no free packets in the list
41 * we try @ref next_free_packet instead.
42 *
43 * Must hold @ref lock when accessing this.
44 */
45static union free_packet *free_packets;
46
47/** @brief Array of new free packets
48 *
49 * There are @ref count_free_packets ready to use at this address. If there
50 * are none left we allocate more memory.
51 *
52 * Must hold @ref lock when accessing this.
53 */
54static union free_packet *next_free_packet;
55
56/** @brief Count of new free packets at @ref next_free_packet
57 *
58 * Must hold @ref lock when accessing this.
59 */
60static size_t count_free_packets;
61
62/** @brief Lock protecting packet allocator */
63static pthread_mutex_t mem_lock = PTHREAD_MUTEX_INITIALIZER;
64
65/** @brief Return a new packet */
c593cf7c 66struct packet *playrtp_new_packet(void) {
8e3fe3d8 67 struct packet *p;
68
69 pthread_mutex_lock(&mem_lock);
70 if(free_packets) {
71 p = &free_packets->p;
72 free_packets = free_packets->next;
73 } else {
74 if(!count_free_packets) {
75 next_free_packet = xcalloc(1024, sizeof (union free_packet));
76 count_free_packets = 1024;
77 }
78 p = &(next_free_packet++)->p;
79 --count_free_packets;
80 }
81 pthread_mutex_unlock(&mem_lock);
82 return p;
83}
84
85/** @brief Free a packet */
c593cf7c 86void playrtp_free_packet(struct packet *p) {
8e3fe3d8 87 union free_packet *u = (union free_packet *)p;
88 pthread_mutex_lock(&mem_lock);
89 u->next = free_packets;
90 free_packets = u;
91 pthread_mutex_unlock(&mem_lock);
92}
93
94
95/*
96Local Variables:
97c-basic-offset:2
98comment-column:40
99fill-column:79
100indent-tabs-mode:nil
101End:
102*/