]>
code.delx.au - pulseaudio/blob - src/tests/memblock-test.c
2 This file is part of PulseAudio.
4 PulseAudio is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published
6 by the Free Software Foundation; either version 2.1 of the License,
7 or (at your option) any later version.
9 PulseAudio is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with PulseAudio; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
29 #include <pulse/xmalloc.h>
31 #include <pulsecore/log.h>
32 #include <pulsecore/memblock.h>
33 #include <pulsecore/macro.h>
35 static void release_cb(pa_memimport
*i
, uint32_t block_id
, void *userdata
) {
36 pa_log("%s: Imported block %u is released.", (char*) userdata
, block_id
);
39 static void revoke_cb(pa_memexport
*e
, uint32_t block_id
, void *userdata
) {
40 pa_log("%s: Exported block %u is revoked.", (char*) userdata
, block_id
);
43 static void print_stats(pa_mempool
*p
, const char *text
) {
44 const pa_mempool_stat
*s
= pa_mempool_get_stat(p
);
46 pa_log_debug("%s = {\n"
47 "\tn_allocated = %u\n"
48 "\tn_accumulated = %u\n"
51 "\tallocated_size = %u\n"
52 "\taccumulated_size = %u\n"
53 "\timported_size = %u\n"
54 "\texported_size = %u\n"
55 "\tn_too_large_for_pool = %u\n"
56 "\tn_pool_full = %u\n"
59 (unsigned) pa_atomic_load(&s
->n_allocated
),
60 (unsigned) pa_atomic_load(&s
->n_accumulated
),
61 (unsigned) pa_atomic_load(&s
->n_imported
),
62 (unsigned) pa_atomic_load(&s
->n_exported
),
63 (unsigned) pa_atomic_load(&s
->allocated_size
),
64 (unsigned) pa_atomic_load(&s
->accumulated_size
),
65 (unsigned) pa_atomic_load(&s
->imported_size
),
66 (unsigned) pa_atomic_load(&s
->exported_size
),
67 (unsigned) pa_atomic_load(&s
->n_too_large_for_pool
),
68 (unsigned) pa_atomic_load(&s
->n_pool_full
));
71 START_TEST (memblock_test
) {
72 pa_mempool
*pool_a
, *pool_b
, *pool_c
;
73 unsigned id_a
, id_b
, id_c
;
74 pa_memexport
*export_a
, *export_b
;
75 pa_memimport
*import_b
, *import_c
;
76 pa_memblock
*mb_a
, *mb_b
, *mb_c
;
78 pa_memblock
* blocks
[5];
83 const char txt
[] = "This is a test!";
85 pool_a
= pa_mempool_new(true, 0);
86 fail_unless(pool_a
!= NULL
);
87 pool_b
= pa_mempool_new(true, 0);
88 fail_unless(pool_b
!= NULL
);
89 pool_c
= pa_mempool_new(true, 0);
90 fail_unless(pool_c
!= NULL
);
92 pa_mempool_get_shm_id(pool_a
, &id_a
);
93 pa_mempool_get_shm_id(pool_b
, &id_b
);
94 pa_mempool_get_shm_id(pool_c
, &id_c
);
96 blocks
[0] = pa_memblock_new_fixed(pool_a
, (void*) txt
, sizeof(txt
), 1);
98 blocks
[1] = pa_memblock_new(pool_a
, sizeof(txt
));
99 x
= pa_memblock_acquire(blocks
[1]);
100 snprintf(x
, pa_memblock_get_length(blocks
[1]), "%s", txt
);
101 pa_memblock_release(blocks
[1]);
103 blocks
[2] = pa_memblock_new_pool(pool_a
, sizeof(txt
));
104 x
= pa_memblock_acquire(blocks
[2]);
105 snprintf(x
, pa_memblock_get_length(blocks
[2]), "%s", txt
);
106 pa_memblock_release(blocks
[2]);
108 blocks
[3] = pa_memblock_new_malloced(pool_a
, pa_xstrdup(txt
), sizeof(txt
));
111 for (i
= 0; blocks
[i
]; i
++) {
112 pa_log("Memory block %u", i
);
115 fail_unless(mb_a
!= NULL
);
117 export_a
= pa_memexport_new(pool_a
, revoke_cb
, (void*) "A");
118 fail_unless(export_a
!= NULL
);
119 export_b
= pa_memexport_new(pool_b
, revoke_cb
, (void*) "B");
120 fail_unless(export_b
!= NULL
);
122 import_b
= pa_memimport_new(pool_b
, release_cb
, (void*) "B");
123 fail_unless(import_b
!= NULL
);
124 import_c
= pa_memimport_new(pool_c
, release_cb
, (void*) "C");
125 fail_unless(import_b
!= NULL
);
127 r
= pa_memexport_put(export_a
, mb_a
, &id
, &shm_id
, &offset
, &size
);
129 fail_unless(shm_id
== id_a
);
131 pa_log("A: Memory block exported as %u", id
);
133 mb_b
= pa_memimport_get(import_b
, id
, shm_id
, offset
, size
);
134 fail_unless(mb_b
!= NULL
);
135 r
= pa_memexport_put(export_b
, mb_b
, &id
, &shm_id
, &offset
, &size
);
137 fail_unless(shm_id
== id_a
|| shm_id
== id_b
);
138 pa_memblock_unref(mb_b
);
140 pa_log("B: Memory block exported as %u", id
);
142 mb_c
= pa_memimport_get(import_c
, id
, shm_id
, offset
, size
);
143 fail_unless(mb_c
!= NULL
);
144 x
= pa_memblock_acquire(mb_c
);
145 pa_log_debug("1 data=%s", x
);
146 pa_memblock_release(mb_c
);
148 print_stats(pool_a
, "A");
149 print_stats(pool_b
, "B");
150 print_stats(pool_c
, "C");
152 pa_memexport_free(export_b
);
153 x
= pa_memblock_acquire(mb_c
);
154 pa_log_debug("2 data=%s", x
);
155 pa_memblock_release(mb_c
);
156 pa_memblock_unref(mb_c
);
158 pa_memimport_free(import_b
);
160 pa_memblock_unref(mb_a
);
162 pa_memimport_free(import_c
);
163 pa_memexport_free(export_a
);
166 pa_log("vacuuming...");
168 pa_mempool_vacuum(pool_a
);
169 pa_mempool_vacuum(pool_b
);
170 pa_mempool_vacuum(pool_c
);
172 pa_log("vacuuming done...");
174 pa_mempool_free(pool_a
);
175 pa_mempool_free(pool_b
);
176 pa_mempool_free(pool_c
);
180 int main(int argc
, char *argv
[]) {
186 if (!getenv("MAKE_CHECK"))
187 pa_log_set_level(PA_LOG_DEBUG
);
189 s
= suite_create("Memblock");
190 tc
= tcase_create("memblock");
191 tcase_add_test(tc
, memblock_test
);
192 suite_add_tcase(s
, tc
);
194 sr
= srunner_create(s
);
195 srunner_run_all(sr
, CK_NORMAL
);
196 failed
= srunner_ntests_failed(sr
);
199 return (failed
== 0) ? EXIT_SUCCESS
: EXIT_FAILURE
;