annotate src/audacious/memorypool.c @ 2866:4f84eb45ec3a trunk

BSD relicensing (pass 7).
author William Pitcock <nenolod@atheme.org>
date Sat, 23 Jun 2007 22:52:33 -0500
parents d226b83fa329
children 4c758281fe8f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2865
d226b83fa329 BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents: 2313
diff changeset
1 /*
d226b83fa329 BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents: 2313
diff changeset
2 * audacious: Cross-platform multimedia player.
d226b83fa329 BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents: 2313
diff changeset
3 * memorypool.c: Memory pooling.
d226b83fa329 BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents: 2313
diff changeset
4 *
d226b83fa329 BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents: 2313
diff changeset
5 * Copyright (c) 2005-2007 Audacious development team.
2313
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
6 *
2865
d226b83fa329 BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents: 2313
diff changeset
7 * Redistribution and use in source and binary forms, with or without
d226b83fa329 BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents: 2313
diff changeset
8 * modification, are permitted provided that the following conditions are
d226b83fa329 BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents: 2313
diff changeset
9 * met:
d226b83fa329 BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents: 2313
diff changeset
10 *
d226b83fa329 BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents: 2313
diff changeset
11 * 1. Redistributions of source code must retain the above copyright notice,
d226b83fa329 BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents: 2313
diff changeset
12 * this list of conditions and the following disclaimer.
2313
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
13 *
2865
d226b83fa329 BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents: 2313
diff changeset
14 * 2. Redistributions in binary form must reproduce the above copyright
d226b83fa329 BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents: 2313
diff changeset
15 * notice, this list of conditions and the following disclaimer in the
d226b83fa329 BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents: 2313
diff changeset
16 * documentation and/or other materials provided with the distribution.
d226b83fa329 BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents: 2313
diff changeset
17 *
d226b83fa329 BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents: 2313
diff changeset
18 * 3. The name of the author may not be used to endorse or promote products
d226b83fa329 BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents: 2313
diff changeset
19 * derived from this software without specific prior written permission.
2313
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
20 *
2865
d226b83fa329 BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents: 2313
diff changeset
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
d226b83fa329 BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents: 2313
diff changeset
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
d226b83fa329 BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents: 2313
diff changeset
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
d226b83fa329 BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents: 2313
diff changeset
24 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
d226b83fa329 BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents: 2313
diff changeset
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
d226b83fa329 BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents: 2313
diff changeset
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
d226b83fa329 BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents: 2313
diff changeset
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
d226b83fa329 BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents: 2313
diff changeset
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
d226b83fa329 BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents: 2313
diff changeset
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
d226b83fa329 BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents: 2313
diff changeset
30 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
d226b83fa329 BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents: 2313
diff changeset
31 * POSSIBILITY OF SUCH DAMAGE.
2313
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
32 */
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
33
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
34 #include <glib.h>
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
35 #include <stdlib.h>
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
36 #include <string.h>
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
37
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
38 #include "util.h"
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
39 #include "memorypool.h"
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
40
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
41 /* visibility of this object is not available to the outside */
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
42 struct _MemoryPool {
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
43 GList *stack;
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
44 GDestroyNotify notify;
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
45 GMutex *mutex;
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
46 };
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
47
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
48 MemoryPool *
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
49 memory_pool_new(void)
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
50 {
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
51 MemoryPool *pool;
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
52
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
53 pool = g_new0(MemoryPool, 1);
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
54 pool->notify = g_free;
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
55 pool->mutex = g_mutex_new();
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
56
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
57 return pool;
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
58 }
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
59
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
60 MemoryPool *
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
61 memory_pool_with_custom_destructor(GDestroyNotify notify)
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
62 {
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
63 MemoryPool *pool;
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
64
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
65 pool = g_new0(MemoryPool, 1);
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
66 pool->notify = notify;
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
67 pool->mutex = g_mutex_new();
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
68
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
69 return pool;
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
70 }
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
71
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
72 gpointer
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
73 memory_pool_add(MemoryPool * pool, gpointer ptr)
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
74 {
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
75 g_mutex_lock(pool->mutex);
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
76 pool->stack = g_list_append(pool->stack, ptr);
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
77 g_mutex_unlock(pool->mutex);
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
78
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
79 return ptr;
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
80 }
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
81
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
82 gpointer
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
83 memory_pool_allocate(MemoryPool * pool, gsize sz)
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
84 {
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
85 gpointer addr;
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
86
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
87 g_mutex_lock(pool->mutex);
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
88 addr = g_malloc0(sz);
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
89 pool->stack = g_list_append(pool->stack, addr);
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
90 g_mutex_unlock(pool->mutex);
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
91
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
92 return addr;
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
93 }
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
94
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
95 void
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
96 memory_pool_release(MemoryPool * pool, gpointer addr)
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
97 {
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
98 g_mutex_lock(pool->mutex);
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
99
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
100 pool->stack = g_list_remove(pool->stack, addr);
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
101 pool->notify(addr);
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
102
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
103 g_mutex_unlock(pool->mutex);
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
104 }
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
105
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
106 static void
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
107 memory_pool_cleanup_nolock(MemoryPool * pool)
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
108 {
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
109 GList *iter;
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
110
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
111 for (iter = pool->stack; iter != NULL; iter = g_list_next(iter))
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
112 {
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
113 pool->stack = g_list_delete_link(pool->stack, iter);
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
114 g_warning("MemoryPool<%p> element at %p was not released until cleanup!", pool, iter->data);
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
115 pool->notify(iter->data);
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
116 }
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
117 }
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
118
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
119 void
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
120 memory_pool_cleanup(MemoryPool * pool)
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
121 {
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
122 g_mutex_lock(pool->mutex);
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
123 memory_pool_cleanup_nolock(pool);
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
124 g_mutex_unlock(pool->mutex);
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
125 }
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
126
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
127 void
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
128 memory_pool_destroy(MemoryPool * pool)
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
129 {
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
130 g_mutex_lock(pool->mutex);
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
131 memory_pool_cleanup_nolock(pool);
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
132 g_mutex_unlock(pool->mutex);
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
133
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
134 g_mutex_free(pool->mutex);
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
135 g_free(pool);
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
136 }
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
137
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
138 gchar *
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
139 memory_pool_strdup(MemoryPool * pool, gchar * src)
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
140 {
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
141 gchar *out;
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
142 gsize sz = strlen(src) + 1;
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
143
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
144 out = memory_pool_allocate(pool, sz);
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
145 g_strlcpy(out, src, sz);
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
146
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
147 return out;
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
148 }