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