daemon: replace colons by dash in per-machine directory names for compat with weird...
[profile/ivi/pulseaudio-panda.git] / src / tests / memblock-test.c
1 /***
2   This file is part of PulseAudio.
3
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.
8
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.
13
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
17   USA.
18 ***/
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <stdio.h>
25 #include <unistd.h>
26
27 #include <pulsecore/memblock.h>
28 #include <pulsecore/macro.h>
29 #include <pulse/xmalloc.h>
30
31 static void release_cb(pa_memimport *i, uint32_t block_id, void *userdata) {
32     printf("%s: Imported block %u is released.\n", (char*) userdata, block_id);
33 }
34
35 static void revoke_cb(pa_memexport *e, uint32_t block_id, void *userdata) {
36     printf("%s: Exported block %u is revoked.\n", (char*) userdata, block_id);
37 }
38
39 static void print_stats(pa_mempool *p, const char *text) {
40     const pa_mempool_stat*s = pa_mempool_get_stat(p);
41
42     printf("%s = {\n"
43            "n_allocated = %u\n"
44            "n_accumulated = %u\n"
45            "n_imported = %u\n"
46            "n_exported = %u\n"
47            "allocated_size = %u\n"
48            "accumulated_size = %u\n"
49            "imported_size = %u\n"
50            "exported_size = %u\n"
51            "n_too_large_for_pool = %u\n"
52            "n_pool_full = %u\n"
53            "}\n",
54            text,
55            (unsigned) pa_atomic_load(&s->n_allocated),
56            (unsigned) pa_atomic_load(&s->n_accumulated),
57            (unsigned) pa_atomic_load(&s->n_imported),
58            (unsigned) pa_atomic_load(&s->n_exported),
59            (unsigned) pa_atomic_load(&s->allocated_size),
60            (unsigned) pa_atomic_load(&s->accumulated_size),
61            (unsigned) pa_atomic_load(&s->imported_size),
62            (unsigned) pa_atomic_load(&s->exported_size),
63            (unsigned) pa_atomic_load(&s->n_too_large_for_pool),
64            (unsigned) pa_atomic_load(&s->n_pool_full));
65 }
66
67 int main(int argc, char *argv[]) {
68     pa_mempool *pool_a, *pool_b, *pool_c;
69     unsigned id_a, id_b, id_c;
70     pa_memexport *export_a, *export_b;
71     pa_memimport *import_b, *import_c;
72     pa_memblock *mb_a, *mb_b, *mb_c;
73     int r, i;
74     pa_memblock* blocks[5];
75     uint32_t id, shm_id;
76     size_t offset, size;
77     char *x;
78
79     const char txt[] = "This is a test!";
80
81     pool_a = pa_mempool_new(TRUE, 0);
82     pool_b = pa_mempool_new(TRUE, 0);
83     pool_c = pa_mempool_new(TRUE, 0);
84
85     pa_mempool_get_shm_id(pool_a, &id_a);
86     pa_mempool_get_shm_id(pool_b, &id_b);
87     pa_mempool_get_shm_id(pool_c, &id_c);
88
89     pa_assert(pool_a && pool_b && pool_c);
90
91     blocks[0] = pa_memblock_new_fixed(pool_a, (void*) txt, sizeof(txt), 1);
92
93     blocks[1] = pa_memblock_new(pool_a, sizeof(txt));
94     x = pa_memblock_acquire(blocks[1]);
95     snprintf(x, pa_memblock_get_length(blocks[1]), "%s", txt);
96     pa_memblock_release(blocks[1]);
97
98     blocks[2] = pa_memblock_new_pool(pool_a, sizeof(txt));
99     x = pa_memblock_acquire(blocks[2]);
100     snprintf(x, pa_memblock_get_length(blocks[2]), "%s", txt);
101     pa_memblock_release(blocks[2]);
102
103     blocks[3] = pa_memblock_new_malloced(pool_a, pa_xstrdup(txt), sizeof(txt));
104     blocks[4] = NULL;
105
106     for (i = 0; blocks[i]; i++) {
107         printf("Memory block %u\n", i);
108
109         mb_a = blocks[i];
110         pa_assert(mb_a);
111
112         export_a = pa_memexport_new(pool_a, revoke_cb, (void*) "A");
113         export_b = pa_memexport_new(pool_b, revoke_cb, (void*) "B");
114
115         pa_assert(export_a && export_b);
116
117         import_b = pa_memimport_new(pool_b, release_cb, (void*) "B");
118         import_c = pa_memimport_new(pool_c, release_cb, (void*) "C");
119
120         pa_assert(import_b && import_c);
121
122         r = pa_memexport_put(export_a, mb_a, &id, &shm_id, &offset, &size);
123         pa_assert(r >= 0);
124         pa_assert(shm_id == id_a);
125
126         printf("A: Memory block exported as %u\n", id);
127
128         mb_b = pa_memimport_get(import_b, id, shm_id, offset, size);
129         pa_assert(mb_b);
130         r = pa_memexport_put(export_b, mb_b, &id, &shm_id, &offset, &size);
131         pa_assert(r >= 0);
132         pa_assert(shm_id == id_a || shm_id == id_b);
133         pa_memblock_unref(mb_b);
134
135         printf("B: Memory block exported as %u\n", id);
136
137         mb_c = pa_memimport_get(import_c, id, shm_id, offset, size);
138         pa_assert(mb_c);
139         x = pa_memblock_acquire(mb_c);
140         printf("1 data=%s\n", x);
141         pa_memblock_release(mb_c);
142
143         print_stats(pool_a, "A");
144         print_stats(pool_b, "B");
145         print_stats(pool_c, "C");
146
147         pa_memexport_free(export_b);
148         x = pa_memblock_acquire(mb_c);
149         printf("2 data=%s\n", x);
150         pa_memblock_release(mb_c);
151         pa_memblock_unref(mb_c);
152
153         pa_memimport_free(import_b);
154
155         pa_memblock_unref(mb_a);
156
157         pa_memimport_free(import_c);
158         pa_memexport_free(export_a);
159     }
160
161     printf("vaccuuming...\n");
162
163     pa_mempool_vacuum(pool_a);
164     pa_mempool_vacuum(pool_b);
165     pa_mempool_vacuum(pool_c);
166
167     printf("vaccuuming done...\n");
168
169     pa_mempool_free(pool_a);
170     pa_mempool_free(pool_b);
171     pa_mempool_free(pool_c);
172
173     return 0;
174 }