Mercurial > audlegacy
annotate audacious/memorypool.c @ 2290:9fc150e3542e trunk
[svn] - add memory_pool_add(): adds a pre-allocated element to a memory pool
author | nenolod |
---|---|
date | Sun, 07 Jan 2007 00:47:47 -0800 |
parents | 4446a9e7bdee |
children | 10e07bc9a8c9 |
rev | line source |
---|---|
2223
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
1 /* Audacious |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
2 * Copyright (c) 2007 William Pitcock <nenolod -at- atheme.org> |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
3 * |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
4 * This program is free software; you can redistribute it and/or modify |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
5 * it under the terms of the GNU General Public License as published by |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
6 * the Free Software Foundation; under version 2 of the License. |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
7 * |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
8 * This program is distributed in the hope that it will be useful, |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
11 * GNU General Public License for more details. |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
12 * |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
13 * You should have received a copy of the GNU General Public License |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
14 * along with this program; if not, write to the Free Software |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
16 */ |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
17 |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
18 #include <glib.h> |
2224 | 19 #include <stdlib.h> |
20 #include <string.h> | |
2223
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
21 |
2224 | 22 #include "util.h" |
2223
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
23 #include "memorypool.h" |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
24 |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
25 /* visibility of this object is not available to the outside */ |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
26 struct _MemoryPool { |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
27 GList *stack; |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
28 GDestroyNotify notify; |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
29 GMutex *mutex; |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
30 }; |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
31 |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
32 MemoryPool * |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
33 memory_pool_new(void) |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
34 { |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
35 MemoryPool *pool; |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
36 |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
37 pool = g_new0(MemoryPool, 1); |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
38 pool->notify = g_free; |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
39 pool->mutex = g_mutex_new(); |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
40 |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
41 return pool; |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
42 } |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
43 |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
44 MemoryPool * |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
45 memory_pool_with_custom_destructor(GDestroyNotify notify) |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
46 { |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
47 MemoryPool *pool; |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
48 |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
49 pool = g_new0(MemoryPool, 1); |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
50 pool->notify = notify; |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
51 pool->mutex = g_mutex_new(); |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
52 |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
53 return pool; |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
54 } |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
55 |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
56 gpointer |
2290
9fc150e3542e
[svn] - add memory_pool_add(): adds a pre-allocated element to a memory pool
nenolod
parents:
2224
diff
changeset
|
57 memory_pool_add(MemoryPool * pool, gpointer ptr) |
9fc150e3542e
[svn] - add memory_pool_add(): adds a pre-allocated element to a memory pool
nenolod
parents:
2224
diff
changeset
|
58 { |
9fc150e3542e
[svn] - add memory_pool_add(): adds a pre-allocated element to a memory pool
nenolod
parents:
2224
diff
changeset
|
59 g_mutex_lock(pool->mutex); |
9fc150e3542e
[svn] - add memory_pool_add(): adds a pre-allocated element to a memory pool
nenolod
parents:
2224
diff
changeset
|
60 pool->stack = g_list_append(pool->stack, addr); |
9fc150e3542e
[svn] - add memory_pool_add(): adds a pre-allocated element to a memory pool
nenolod
parents:
2224
diff
changeset
|
61 g_mutex_unlock(pool->mutex); |
9fc150e3542e
[svn] - add memory_pool_add(): adds a pre-allocated element to a memory pool
nenolod
parents:
2224
diff
changeset
|
62 |
9fc150e3542e
[svn] - add memory_pool_add(): adds a pre-allocated element to a memory pool
nenolod
parents:
2224
diff
changeset
|
63 return ptr; |
9fc150e3542e
[svn] - add memory_pool_add(): adds a pre-allocated element to a memory pool
nenolod
parents:
2224
diff
changeset
|
64 } |
9fc150e3542e
[svn] - add memory_pool_add(): adds a pre-allocated element to a memory pool
nenolod
parents:
2224
diff
changeset
|
65 |
9fc150e3542e
[svn] - add memory_pool_add(): adds a pre-allocated element to a memory pool
nenolod
parents:
2224
diff
changeset
|
66 gpointer |
2223
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
67 memory_pool_allocate(MemoryPool * pool, gsize sz) |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
68 { |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
69 gpointer addr; |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
70 |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
71 g_mutex_lock(pool->mutex); |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
72 addr = g_malloc0(sz); |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
73 pool->stack = g_list_append(pool->stack, addr); |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
74 g_mutex_unlock(pool->mutex); |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
75 |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
76 return addr; |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
77 } |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
78 |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
79 void |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
80 memory_pool_release(MemoryPool * pool, gpointer addr) |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
81 { |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
82 g_mutex_lock(pool->mutex); |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
83 |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
84 pool->stack = g_list_remove(pool->stack, addr); |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
85 pool->notify(addr); |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
86 |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
87 g_mutex_unlock(pool->mutex); |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
88 } |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
89 |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
90 static void |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
91 memory_pool_cleanup_nolock(MemoryPool * pool) |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
92 { |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
93 GList *iter; |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
94 |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
95 for (iter = pool->stack; iter != NULL; iter = g_list_next(iter)) |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
96 { |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
97 pool->stack = g_list_delete_link(pool->stack, iter); |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
98 g_warning("MemoryPool<%p> element at %p was not released until cleanup!", pool, iter->data); |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
99 pool->notify(iter->data); |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
100 } |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
101 } |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
102 |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
103 void |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
104 memory_pool_cleanup(MemoryPool * pool) |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
105 { |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
106 g_mutex_lock(pool->mutex); |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
107 memory_pool_cleanup_nolock(pool); |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
108 g_mutex_unlock(pool->mutex); |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
109 } |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
110 |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
111 void |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
112 memory_pool_destroy(MemoryPool * pool) |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
113 { |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
114 g_mutex_lock(pool->mutex); |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
115 memory_pool_cleanup_nolock(pool); |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
116 g_mutex_unlock(pool->mutex); |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
117 |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
118 g_mutex_free(pool->mutex); |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
119 g_free(pool); |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
120 } |
2224 | 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 } |