Mercurial > audlegacy
annotate src/audacious/memorypool.c @ 3471:95d8ceb5e1d7 trunk
Automated merge with ssh://hg.atheme.org//hg/audacious
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sun, 09 Sep 2007 22:59:36 +0300 |
parents | f1c756f39e6c |
children |
rev | line source |
---|---|
3112
4c758281fe8f
Backed out changeset d226b83fa3298fc92f25f9519befcd754f44b0ef
William Pitcock <nenolod@atheme-project.org>
parents:
2865
diff
changeset
|
1 /* Audacious |
4c758281fe8f
Backed out changeset d226b83fa3298fc92f25f9519befcd754f44b0ef
William Pitcock <nenolod@atheme-project.org>
parents:
2865
diff
changeset
|
2 * Copyright (c) 2007 William Pitcock <nenolod -at- atheme.org> |
2313 | 3 * |
3112
4c758281fe8f
Backed out changeset d226b83fa3298fc92f25f9519befcd754f44b0ef
William Pitcock <nenolod@atheme-project.org>
parents:
2865
diff
changeset
|
4 * This program is free software; you can redistribute it and/or modify |
4c758281fe8f
Backed out changeset d226b83fa3298fc92f25f9519befcd754f44b0ef
William Pitcock <nenolod@atheme-project.org>
parents:
2865
diff
changeset
|
5 * it under the terms of the GNU General Public License as published by |
3121
3b6d316f8b09
GPL3 relicensing.
William Pitcock <nenolod@atheme-project.org>
parents:
3112
diff
changeset
|
6 * the Free Software Foundation; under version 3 of the License. |
2313 | 7 * |
3112
4c758281fe8f
Backed out changeset d226b83fa3298fc92f25f9519befcd754f44b0ef
William Pitcock <nenolod@atheme-project.org>
parents:
2865
diff
changeset
|
8 * This program is distributed in the hope that it will be useful, |
4c758281fe8f
Backed out changeset d226b83fa3298fc92f25f9519befcd754f44b0ef
William Pitcock <nenolod@atheme-project.org>
parents:
2865
diff
changeset
|
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
4c758281fe8f
Backed out changeset d226b83fa3298fc92f25f9519befcd754f44b0ef
William Pitcock <nenolod@atheme-project.org>
parents:
2865
diff
changeset
|
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
4c758281fe8f
Backed out changeset d226b83fa3298fc92f25f9519befcd754f44b0ef
William Pitcock <nenolod@atheme-project.org>
parents:
2865
diff
changeset
|
11 * GNU General Public License for more details. |
2313 | 12 * |
3112
4c758281fe8f
Backed out changeset d226b83fa3298fc92f25f9519befcd754f44b0ef
William Pitcock <nenolod@atheme-project.org>
parents:
2865
diff
changeset
|
13 * You should have received a copy of the GNU General Public License |
3121
3b6d316f8b09
GPL3 relicensing.
William Pitcock <nenolod@atheme-project.org>
parents:
3112
diff
changeset
|
14 * along with this program. If not, see <http://www.gnu.org/licenses>. |
3123
f1c756f39e6c
Invoke "Plugins are not derived work" clause provided by GPL3.
William Pitcock <nenolod@atheme-project.org>
parents:
3121
diff
changeset
|
15 * |
f1c756f39e6c
Invoke "Plugins are not derived work" clause provided by GPL3.
William Pitcock <nenolod@atheme-project.org>
parents:
3121
diff
changeset
|
16 * The Audacious team does not consider modular code linking to |
f1c756f39e6c
Invoke "Plugins are not derived work" clause provided by GPL3.
William Pitcock <nenolod@atheme-project.org>
parents:
3121
diff
changeset
|
17 * Audacious or using our public API to be a derived work. |
2313 | 18 */ |
19 | |
20 #include <glib.h> | |
21 #include <stdlib.h> | |
22 #include <string.h> | |
23 | |
24 #include "util.h" | |
25 #include "memorypool.h" | |
26 | |
27 /* visibility of this object is not available to the outside */ | |
28 struct _MemoryPool { | |
29 GList *stack; | |
30 GDestroyNotify notify; | |
31 GMutex *mutex; | |
32 }; | |
33 | |
34 MemoryPool * | |
35 memory_pool_new(void) | |
36 { | |
37 MemoryPool *pool; | |
38 | |
39 pool = g_new0(MemoryPool, 1); | |
40 pool->notify = g_free; | |
41 pool->mutex = g_mutex_new(); | |
42 | |
43 return pool; | |
44 } | |
45 | |
46 MemoryPool * | |
47 memory_pool_with_custom_destructor(GDestroyNotify notify) | |
48 { | |
49 MemoryPool *pool; | |
50 | |
51 pool = g_new0(MemoryPool, 1); | |
52 pool->notify = notify; | |
53 pool->mutex = g_mutex_new(); | |
54 | |
55 return pool; | |
56 } | |
57 | |
58 gpointer | |
59 memory_pool_add(MemoryPool * pool, gpointer ptr) | |
60 { | |
61 g_mutex_lock(pool->mutex); | |
62 pool->stack = g_list_append(pool->stack, ptr); | |
63 g_mutex_unlock(pool->mutex); | |
64 | |
65 return ptr; | |
66 } | |
67 | |
68 gpointer | |
69 memory_pool_allocate(MemoryPool * pool, gsize sz) | |
70 { | |
71 gpointer addr; | |
72 | |
73 g_mutex_lock(pool->mutex); | |
74 addr = g_malloc0(sz); | |
75 pool->stack = g_list_append(pool->stack, addr); | |
76 g_mutex_unlock(pool->mutex); | |
77 | |
78 return addr; | |
79 } | |
80 | |
81 void | |
82 memory_pool_release(MemoryPool * pool, gpointer addr) | |
83 { | |
84 g_mutex_lock(pool->mutex); | |
85 | |
86 pool->stack = g_list_remove(pool->stack, addr); | |
87 pool->notify(addr); | |
88 | |
89 g_mutex_unlock(pool->mutex); | |
90 } | |
91 | |
92 static void | |
93 memory_pool_cleanup_nolock(MemoryPool * pool) | |
94 { | |
95 GList *iter; | |
96 | |
97 for (iter = pool->stack; iter != NULL; iter = g_list_next(iter)) | |
98 { | |
99 pool->stack = g_list_delete_link(pool->stack, iter); | |
100 g_warning("MemoryPool<%p> element at %p was not released until cleanup!", pool, iter->data); | |
101 pool->notify(iter->data); | |
102 } | |
103 } | |
104 | |
105 void | |
106 memory_pool_cleanup(MemoryPool * pool) | |
107 { | |
108 g_mutex_lock(pool->mutex); | |
109 memory_pool_cleanup_nolock(pool); | |
110 g_mutex_unlock(pool->mutex); | |
111 } | |
112 | |
113 void | |
114 memory_pool_destroy(MemoryPool * pool) | |
115 { | |
116 g_mutex_lock(pool->mutex); | |
117 memory_pool_cleanup_nolock(pool); | |
118 g_mutex_unlock(pool->mutex); | |
119 | |
120 g_mutex_free(pool->mutex); | |
121 g_free(pool); | |
122 } | |
123 | |
124 gchar * | |
125 memory_pool_strdup(MemoryPool * pool, gchar * src) | |
126 { | |
127 gchar *out; | |
128 gsize sz = strlen(src) + 1; | |
129 | |
130 out = memory_pool_allocate(pool, sz); | |
131 g_strlcpy(out, src, sz); | |
132 | |
133 return out; | |
134 } |