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 | |
05b75f8d |
24 | #include "common.h" |
8e3fe3d8 |
25 | |
26 | #include <pthread.h> |
8e3fe3d8 |
27 | |
28 | #include "mem.h" |
29 | #include "vector.h" |
30 | #include "heap.h" |
31 | #include "playrtp.h" |
32 | |
33 | /** @brief Linked list of free packets |
34 | * |
35 | * This is a linked list of formerly used packets. For preference we re-use |
36 | * packets that have already been used rather than unused ones, to limit the |
37 | * size of the program's working set. If there are no free packets in the list |
38 | * we try @ref next_free_packet instead. |
39 | * |
40 | * Must hold @ref lock when accessing this. |
41 | */ |
42 | static union free_packet *free_packets; |
43 | |
44 | /** @brief Array of new free packets |
45 | * |
46 | * There are @ref count_free_packets ready to use at this address. If there |
47 | * are none left we allocate more memory. |
48 | * |
49 | * Must hold @ref lock when accessing this. |
50 | */ |
51 | static union free_packet *next_free_packet; |
52 | |
53 | /** @brief Count of new free packets at @ref next_free_packet |
54 | * |
55 | * Must hold @ref lock when accessing this. |
56 | */ |
57 | static size_t count_free_packets; |
58 | |
59 | /** @brief Lock protecting packet allocator */ |
60 | static pthread_mutex_t mem_lock = PTHREAD_MUTEX_INITIALIZER; |
61 | |
62 | /** @brief Return a new packet */ |
c593cf7c |
63 | struct packet *playrtp_new_packet(void) { |
8e3fe3d8 |
64 | struct packet *p; |
65 | |
66 | pthread_mutex_lock(&mem_lock); |
67 | if(free_packets) { |
68 | p = &free_packets->p; |
69 | free_packets = free_packets->next; |
70 | } else { |
71 | if(!count_free_packets) { |
72 | next_free_packet = xcalloc(1024, sizeof (union free_packet)); |
73 | count_free_packets = 1024; |
74 | } |
75 | p = &(next_free_packet++)->p; |
76 | --count_free_packets; |
77 | } |
78 | pthread_mutex_unlock(&mem_lock); |
79 | return p; |
80 | } |
81 | |
82 | /** @brief Free a packet */ |
c593cf7c |
83 | void playrtp_free_packet(struct packet *p) { |
8e3fe3d8 |
84 | union free_packet *u = (union free_packet *)p; |
85 | pthread_mutex_lock(&mem_lock); |
86 | u->next = free_packets; |
87 | free_packets = u; |
88 | pthread_mutex_unlock(&mem_lock); |
89 | } |
90 | |
91 | |
92 | /* |
93 | Local Variables: |
94 | c-basic-offset:2 |
95 | comment-column:40 |
96 | fill-column:79 |
97 | indent-tabs-mode:nil |
98 | End: |
99 | */ |