2 * This file is part of DisOrder.
3 * Copyright (C) 2007 Richard Kettlewell
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.
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.
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
20 /** @file clients/playrtp-mem.c
21 * @brief RTP player memory management
36 /** @brief Linked list of free packets
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.
43 * Must hold @ref lock when accessing this.
45 static union free_packet *free_packets;
47 /** @brief Array of new free packets
49 * There are @ref count_free_packets ready to use at this address. If there
50 * are none left we allocate more memory.
52 * Must hold @ref lock when accessing this.
54 static union free_packet *next_free_packet;
56 /** @brief Count of new free packets at @ref next_free_packet
58 * Must hold @ref lock when accessing this.
60 static size_t count_free_packets;
62 /** @brief Lock protecting packet allocator */
63 static pthread_mutex_t mem_lock = PTHREAD_MUTEX_INITIALIZER;
65 /** @brief Return a new packet */
66 struct packet *playrtp_new_packet(void) {
69 pthread_mutex_lock(&mem_lock);
72 free_packets = free_packets->next;
74 if(!count_free_packets) {
75 next_free_packet = xcalloc(1024, sizeof (union free_packet));
76 count_free_packets = 1024;
78 p = &(next_free_packet++)->p;
81 pthread_mutex_unlock(&mem_lock);
85 /** @brief Free a packet */
86 void playrtp_free_packet(struct packet *p) {
87 union free_packet *u = (union free_packet *)p;
88 pthread_mutex_lock(&mem_lock);
89 u->next = free_packets;
91 pthread_mutex_unlock(&mem_lock);