rework memory block management to be thread-safe and mostly lock-free.
[profile/ivi/pulseaudio-panda.git] / src / tests / mcalign-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
8   published by the Free Software Foundation; either version 2.1 of the
9   License, 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   Lesser General Public License for more details.
15  
16   You should have received a copy of the GNU Lesser General Public
17   License 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 <assert.h>
27 #include <unistd.h>
28 #include <sys/types.h>
29 #include <errno.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include <time.h>
33
34 #include <pulsecore/core-util.h>
35 #include <pulsecore/mcalign.h>
36 #include <pulsecore/gccmacro.h>
37
38 /* A simple program for testing pa_mcalign */
39
40 int main(PA_GCC_UNUSED int argc, PA_GCC_UNUSED char *argv[]) {
41     pa_mempool *p;
42     pa_mcalign *a;
43     pa_memchunk c;
44
45     p = pa_mempool_new(0);
46
47     a = pa_mcalign_new(11);
48     
49     pa_memchunk_reset(&c);
50
51     srand(time(NULL));
52
53     for (;;) {
54         ssize_t r;
55         size_t l;
56
57         if (!c.memblock) {
58             c.memblock = pa_memblock_new(p, 2048);
59             c.index = c.length = 0;
60         }
61
62         assert(c.index < pa_memblock_get_length(c.memblock));
63
64         l = pa_memblock_get_length(c.memblock) - c.index;
65
66         l = l <= 1 ? l : rand() % (l-1) +1 ;
67
68         p = pa_memblock_acquire(c.memblock);
69         if ((r = read(STDIN_FILENO, (uint8_t*) p + c.index, l)) <= 0) {
70             pa_memblock_release(c.memblock);
71             fprintf(stderr, "read() failed: %s\n", r < 0 ? strerror(errno) : "EOF");
72             break;
73         }
74         pa_memblock_release(c.memblock);
75             
76         c.length = r;
77         pa_mcalign_push(a, &c);
78         fprintf(stderr, "Read %ld bytes\n", (long)r);
79
80         c.index += r;
81
82         if (c.index >= pa_memblock_get_length(c.memblock)) {
83             pa_memblock_unref(c.memblock);
84             pa_memchunk_reset(&c);
85         }
86
87         for (;;) {
88             pa_memchunk t;
89
90             if (pa_mcalign_pop(a, &t) < 0)
91                 break;
92
93             p = pa_memblock_acquire(t.memblock);
94             pa_loop_write(STDOUT_FILENO, (uint8_t*) p + t.index, t.length, NULL);
95             pa_memblock_release(t.memblock);
96             fprintf(stderr, "Wrote %lu bytes.\n", (unsigned long) t.length);
97
98             pa_memblock_unref(t.memblock);
99         }
100     }
101
102     pa_mcalign_free(a);
103
104     if (c.memblock)
105         pa_memblock_unref(c.memblock);
106
107     pa_mempool_free(p);
108
109     return 0;
110 }