]> code.delx.au - pulseaudio/blob - src/tests/memblock-test.c
merge 'lennart' branch back into trunk.
[pulseaudio] / src / tests / memblock-test.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
10
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <unistd.h>
28
29 #include <pulsecore/memblock.h>
30 #include <pulsecore/macro.h>
31 #include <pulse/xmalloc.h>
32
33 static void release_cb(pa_memimport *i, uint32_t block_id, void *userdata) {
34 printf("%s: Imported block %u is released.\n", (char*) userdata, block_id);
35 }
36
37 static void revoke_cb(pa_memexport *e, uint32_t block_id, void *userdata) {
38 printf("%s: Exported block %u is revoked.\n", (char*) userdata, block_id);
39 }
40
41 static void print_stats(pa_mempool *p, const char *text) {
42 const pa_mempool_stat*s = pa_mempool_get_stat(p);
43
44 printf("%s = {\n"
45 "n_allocated = %u\n"
46 "n_accumulated = %u\n"
47 "n_imported = %u\n"
48 "n_exported = %u\n"
49 "allocated_size = %u\n"
50 "accumulated_size = %u\n"
51 "imported_size = %u\n"
52 "exported_size = %u\n"
53 "n_too_large_for_pool = %u\n"
54 "n_pool_full = %u\n"
55 "}\n",
56 text,
57 (unsigned) pa_atomic_load(&s->n_allocated),
58 (unsigned) pa_atomic_load(&s->n_accumulated),
59 (unsigned) pa_atomic_load(&s->n_imported),
60 (unsigned) pa_atomic_load(&s->n_exported),
61 (unsigned) pa_atomic_load(&s->allocated_size),
62 (unsigned) pa_atomic_load(&s->accumulated_size),
63 (unsigned) pa_atomic_load(&s->imported_size),
64 (unsigned) pa_atomic_load(&s->exported_size),
65 (unsigned) pa_atomic_load(&s->n_too_large_for_pool),
66 (unsigned) pa_atomic_load(&s->n_pool_full));
67 }
68
69 int main(int argc, char *argv[]) {
70 pa_mempool *pool_a, *pool_b, *pool_c;
71 unsigned id_a, id_b, id_c;
72 pa_memexport *export_a, *export_b;
73 pa_memimport *import_b, *import_c;
74 pa_memblock *mb_a, *mb_b, *mb_c;
75 int r, i;
76 pa_memblock* blocks[5];
77 uint32_t id, shm_id;
78 size_t offset, size;
79 char *x;
80
81 const char txt[] = "This is a test!";
82
83 pool_a = pa_mempool_new(1);
84 pool_b = pa_mempool_new(1);
85 pool_c = pa_mempool_new(1);
86
87 pa_mempool_get_shm_id(pool_a, &id_a);
88 pa_mempool_get_shm_id(pool_b, &id_b);
89 pa_mempool_get_shm_id(pool_c, &id_c);
90
91 pa_assert(pool_a && pool_b && pool_c);
92
93 blocks[0] = pa_memblock_new_fixed(pool_a, (void*) txt, sizeof(txt), 1);
94
95 blocks[1] = pa_memblock_new(pool_a, sizeof(txt));
96 x = pa_memblock_acquire(blocks[1]);
97 snprintf(x, pa_memblock_get_length(blocks[1]), "%s", txt);
98 pa_memblock_release(blocks[1]);
99
100 blocks[2] = pa_memblock_new_pool(pool_a, sizeof(txt));
101 x = pa_memblock_acquire(blocks[2]);
102 snprintf(x, pa_memblock_get_length(blocks[2]), "%s", txt);
103 pa_memblock_release(blocks[2]);
104
105 blocks[3] = pa_memblock_new_malloced(pool_a, pa_xstrdup(txt), sizeof(txt));
106 blocks[4] = NULL;
107
108 for (i = 0; blocks[i]; i++) {
109 printf("Memory block %u\n", i);
110
111 mb_a = blocks[i];
112 pa_assert(mb_a);
113
114 export_a = pa_memexport_new(pool_a, revoke_cb, (void*) "A");
115 export_b = pa_memexport_new(pool_b, revoke_cb, (void*) "B");
116
117 pa_assert(export_a && export_b);
118
119 import_b = pa_memimport_new(pool_b, release_cb, (void*) "B");
120 import_c = pa_memimport_new(pool_c, release_cb, (void*) "C");
121
122 pa_assert(import_b && import_c);
123
124 r = pa_memexport_put(export_a, mb_a, &id, &shm_id, &offset, &size);
125 pa_assert(r >= 0);
126 pa_assert(shm_id == id_a);
127
128 printf("A: Memory block exported as %u\n", id);
129
130 mb_b = pa_memimport_get(import_b, id, shm_id, offset, size);
131 pa_assert(mb_b);
132 r = pa_memexport_put(export_b, mb_b, &id, &shm_id, &offset, &size);
133 pa_assert(r >= 0);
134 pa_assert(shm_id == id_a || shm_id == id_b);
135 pa_memblock_unref(mb_b);
136
137 printf("B: Memory block exported as %u\n", id);
138
139 mb_c = pa_memimport_get(import_c, id, shm_id, offset, size);
140 pa_assert(mb_c);
141 x = pa_memblock_acquire(mb_c);
142 printf("1 data=%s\n", x);
143 pa_memblock_release(mb_c);
144
145 print_stats(pool_a, "A");
146 print_stats(pool_b, "B");
147 print_stats(pool_c, "C");
148
149 pa_memexport_free(export_b);
150 x = pa_memblock_acquire(mb_c);
151 printf("2 data=%s\n", x);
152 pa_memblock_release(mb_c);
153 pa_memblock_unref(mb_c);
154
155 pa_memimport_free(import_b);
156
157 pa_memblock_unref(mb_a);
158
159 pa_memimport_free(import_c);
160 pa_memexport_free(export_a);
161 }
162
163 printf("vaccuuming...\n");
164
165 pa_mempool_vacuum(pool_a);
166 pa_mempool_vacuum(pool_b);
167 pa_mempool_vacuum(pool_c);
168
169 printf("vaccuuming done...\n");
170
171 pa_mempool_free(pool_a);
172 pa_mempool_free(pool_b);
173 pa_mempool_free(pool_c);
174
175 return 0;
176 }