Mercurial > audlegacy
annotate src/audacious/memorypool.c @ 3121:3b6d316f8b09 trunk
GPL3 relicensing.
author | William Pitcock <nenolod@atheme-project.org> |
---|---|
date | Fri, 20 Jul 2007 08:59:47 -0500 |
parents | 4c758281fe8f |
children | f1c756f39e6c |
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>. |
2313 | 15 */ |
16 | |
17 #include <glib.h> | |
18 #include <stdlib.h> | |
19 #include <string.h> | |
20 | |
21 #include "util.h" | |
22 #include "memorypool.h" | |
23 | |
24 /* visibility of this object is not available to the outside */ | |
25 struct _MemoryPool { | |
26 GList *stack; | |
27 GDestroyNotify notify; | |
28 GMutex *mutex; | |
29 }; | |
30 | |
31 MemoryPool * | |
32 memory_pool_new(void) | |
33 { | |
34 MemoryPool *pool; | |
35 | |
36 pool = g_new0(MemoryPool, 1); | |
37 pool->notify = g_free; | |
38 pool->mutex = g_mutex_new(); | |
39 | |
40 return pool; | |
41 } | |
42 | |
43 MemoryPool * | |
44 memory_pool_with_custom_destructor(GDestroyNotify notify) | |
45 { | |
46 MemoryPool *pool; | |
47 | |
48 pool = g_new0(MemoryPool, 1); | |
49 pool->notify = notify; | |
50 pool->mutex = g_mutex_new(); | |
51 | |
52 return pool; | |
53 } | |
54 | |
55 gpointer | |
56 memory_pool_add(MemoryPool * pool, gpointer ptr) | |
57 { | |
58 g_mutex_lock(pool->mutex); | |
59 pool->stack = g_list_append(pool->stack, ptr); | |
60 g_mutex_unlock(pool->mutex); | |
61 | |
62 return ptr; | |
63 } | |
64 | |
65 gpointer | |
66 memory_pool_allocate(MemoryPool * pool, gsize sz) | |
67 { | |
68 gpointer addr; | |
69 | |
70 g_mutex_lock(pool->mutex); | |
71 addr = g_malloc0(sz); | |
72 pool->stack = g_list_append(pool->stack, addr); | |
73 g_mutex_unlock(pool->mutex); | |
74 | |
75 return addr; | |
76 } | |
77 | |
78 void | |
79 memory_pool_release(MemoryPool * pool, gpointer addr) | |
80 { | |
81 g_mutex_lock(pool->mutex); | |
82 | |
83 pool->stack = g_list_remove(pool->stack, addr); | |
84 pool->notify(addr); | |
85 | |
86 g_mutex_unlock(pool->mutex); | |
87 } | |
88 | |
89 static void | |
90 memory_pool_cleanup_nolock(MemoryPool * pool) | |
91 { | |
92 GList *iter; | |
93 | |
94 for (iter = pool->stack; iter != NULL; iter = g_list_next(iter)) | |
95 { | |
96 pool->stack = g_list_delete_link(pool->stack, iter); | |
97 g_warning("MemoryPool<%p> element at %p was not released until cleanup!", pool, iter->data); | |
98 pool->notify(iter->data); | |
99 } | |
100 } | |
101 | |
102 void | |
103 memory_pool_cleanup(MemoryPool * pool) | |
104 { | |
105 g_mutex_lock(pool->mutex); | |
106 memory_pool_cleanup_nolock(pool); | |
107 g_mutex_unlock(pool->mutex); | |
108 } | |
109 | |
110 void | |
111 memory_pool_destroy(MemoryPool * pool) | |
112 { | |
113 g_mutex_lock(pool->mutex); | |
114 memory_pool_cleanup_nolock(pool); | |
115 g_mutex_unlock(pool->mutex); | |
116 | |
117 g_mutex_free(pool->mutex); | |
118 g_free(pool); | |
119 } | |
120 | |
121 gchar * | |
122 memory_pool_strdup(MemoryPool * pool, gchar * src) | |
123 { | |
124 gchar *out; | |
125 gsize sz = strlen(src) + 1; | |
126 | |
127 out = memory_pool_allocate(pool, sz); | |
128 g_strlcpy(out, src, sz); | |
129 | |
130 return out; | |
131 } |