annotate src/audacious/memorypool.c @ 2484:adc63272c9ff trunk

[svn] - add some debugging assertions to this code
author nenolod
date Wed, 07 Feb 2007 10:23:29 -0800
parents 3149d4b1a9a9
children d226b83fa329
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2313
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
1 /* Audacious
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
2 * Copyright (c) 2007 William Pitcock <nenolod -at- atheme.org>
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
3 *
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
4 * This program is free software; you can redistribute it and/or modify
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
5 * it under the terms of the GNU General Public License as published by
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
6 * the Free Software Foundation; under version 2 of the License.
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
7 *
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
8 * This program is distributed in the hope that it will be useful,
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
11 * GNU General Public License for more details.
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
12 *
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
13 * You should have received a copy of the GNU General Public License
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
14 * along with this program; if not, write to the Free Software
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
16 */
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
17
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
18 #include <glib.h>
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
19 #include <stdlib.h>
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
20 #include <string.h>
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
21
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
22 #include "util.h"
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
23 #include "memorypool.h"
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
24
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
25 /* visibility of this object is not available to the outside */
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
26 struct _MemoryPool {
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
27 GList *stack;
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
28 GDestroyNotify notify;
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
29 GMutex *mutex;
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
30 };
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
31
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
32 MemoryPool *
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
33 memory_pool_new(void)
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
34 {
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
35 MemoryPool *pool;
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
36
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
37 pool = g_new0(MemoryPool, 1);
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
38 pool->notify = g_free;
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
39 pool->mutex = g_mutex_new();
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
40
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
41 return pool;
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
42 }
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
43
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
44 MemoryPool *
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
45 memory_pool_with_custom_destructor(GDestroyNotify notify)
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
46 {
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
47 MemoryPool *pool;
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
48
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
49 pool = g_new0(MemoryPool, 1);
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
50 pool->notify = notify;
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
51 pool->mutex = g_mutex_new();
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
52
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
53 return pool;
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
54 }
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
55
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
56 gpointer
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
57 memory_pool_add(MemoryPool * pool, gpointer ptr)
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
58 {
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
59 g_mutex_lock(pool->mutex);
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
60 pool->stack = g_list_append(pool->stack, ptr);
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
61 g_mutex_unlock(pool->mutex);
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
62
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
63 return ptr;
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
64 }
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
65
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
66 gpointer
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
67 memory_pool_allocate(MemoryPool * pool, gsize sz)
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
68 {
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
69 gpointer addr;
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
70
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
71 g_mutex_lock(pool->mutex);
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
72 addr = g_malloc0(sz);
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
73 pool->stack = g_list_append(pool->stack, addr);
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
74 g_mutex_unlock(pool->mutex);
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
75
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
76 return addr;
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
77 }
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
78
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
79 void
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
80 memory_pool_release(MemoryPool * pool, gpointer addr)
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
81 {
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
82 g_mutex_lock(pool->mutex);
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
83
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
84 pool->stack = g_list_remove(pool->stack, addr);
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
85 pool->notify(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_unlock(pool->mutex);
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
88 }
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
89
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
90 static void
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
91 memory_pool_cleanup_nolock(MemoryPool * pool)
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
92 {
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
93 GList *iter;
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
94
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
95 for (iter = pool->stack; iter != NULL; iter = g_list_next(iter))
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
96 {
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
97 pool->stack = g_list_delete_link(pool->stack, iter);
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
98 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
99 pool->notify(iter->data);
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
100 }
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
101 }
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
102
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
103 void
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
104 memory_pool_cleanup(MemoryPool * pool)
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
105 {
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
106 g_mutex_lock(pool->mutex);
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
107 memory_pool_cleanup_nolock(pool);
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
108 g_mutex_unlock(pool->mutex);
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
109 }
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
110
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
111 void
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
112 memory_pool_destroy(MemoryPool * pool)
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
113 {
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
114 g_mutex_lock(pool->mutex);
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
115 memory_pool_cleanup_nolock(pool);
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
116 g_mutex_unlock(pool->mutex);
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
117
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
118 g_mutex_free(pool->mutex);
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
119 g_free(pool);
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
120 }
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
121
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
122 gchar *
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
123 memory_pool_strdup(MemoryPool * pool, gchar * src)
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
124 {
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
125 gchar *out;
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
126 gsize sz = strlen(src) + 1;
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
127
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
128 out = memory_pool_allocate(pool, sz);
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
129 g_strlcpy(out, src, sz);
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
130
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
131 return out;
3149d4b1a9a9 [svn] - objective-make autodepend fixes
nenolod
parents:
diff changeset
132 }