Mercurial > audlegacy
annotate audacious/memorypool.c @ 2224:4446a9e7bdee trunk
[svn] - memory_pool_alloc_object()
- memory_pool_strdup()
- controlsocket now uses a memory pool for packet data
author | nenolod |
---|---|
date | Fri, 29 Dec 2006 21:01:49 -0800 |
parents | e0d7335f56c3 |
children | 9fc150e3542e |
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 |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
57 memory_pool_allocate(MemoryPool * pool, gsize sz) |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
58 { |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
59 gpointer addr; |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
60 |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
61 g_mutex_lock(pool->mutex); |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
62 addr = g_malloc0(sz); |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
63 pool->stack = g_list_append(pool->stack, addr); |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
64 g_mutex_unlock(pool->mutex); |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
65 |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
66 return addr; |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
67 } |
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 void |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
70 memory_pool_release(MemoryPool * pool, gpointer addr) |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
71 { |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
72 g_mutex_lock(pool->mutex); |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
73 |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
74 pool->stack = g_list_remove(pool->stack, addr); |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
75 pool->notify(addr); |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
76 |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
77 g_mutex_unlock(pool->mutex); |
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 |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
80 static void |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
81 memory_pool_cleanup_nolock(MemoryPool * pool) |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
82 { |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
83 GList *iter; |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
84 |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
85 for (iter = pool->stack; iter != NULL; iter = g_list_next(iter)) |
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 pool->stack = g_list_delete_link(pool->stack, iter); |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
88 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
|
89 pool->notify(iter->data); |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
90 } |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
91 } |
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 void |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
94 memory_pool_cleanup(MemoryPool * pool) |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
95 { |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
96 g_mutex_lock(pool->mutex); |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
97 memory_pool_cleanup_nolock(pool); |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
98 g_mutex_unlock(pool->mutex); |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
99 } |
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 void |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
102 memory_pool_destroy(MemoryPool * pool) |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
103 { |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
104 g_mutex_lock(pool->mutex); |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
105 memory_pool_cleanup_nolock(pool); |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
106 g_mutex_unlock(pool->mutex); |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
107 |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
108 g_mutex_free(pool->mutex); |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
109 g_free(pool); |
e0d7335f56c3
[svn] - MemoryPool, a simple performance-oriented memory pool.
nenolod
parents:
diff
changeset
|
110 } |
2224 | 111 |
112 gchar * | |
113 memory_pool_strdup(MemoryPool * pool, gchar * src) | |
114 { | |
115 gchar *out; | |
116 gsize sz = strlen(src) + 1; | |
117 | |
118 out = memory_pool_allocate(pool, sz); | |
119 g_strlcpy(out, src, sz); | |
120 | |
121 return out; | |
122 } |