LCOV - code coverage report
Current view: top level - tests - memblock-test.c (source / functions) Hit Total Coverage
Test: lcov.out Lines: 74 78 94.9 %
Date: 2012-07-17 Functions: 3 4 75.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 14 28 50.0 %

           Branch data     Line data    Source code
       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 <pulse/xmalloc.h>
      28                 :            : 
      29                 :            : #include <pulsecore/log.h>
      30                 :            : #include <pulsecore/memblock.h>
      31                 :            : #include <pulsecore/macro.h>
      32                 :            : 
      33                 :          8 : static void release_cb(pa_memimport *i, uint32_t block_id, void *userdata) {
      34                 :          8 :     pa_log("%s: Imported block %u is released.", (char*) userdata, block_id);
      35                 :          8 : }
      36                 :            : 
      37                 :          0 : static void revoke_cb(pa_memexport *e, uint32_t block_id, void *userdata) {
      38                 :          0 :     pa_log("%s: Exported block %u is revoked.", (char*) userdata, block_id);
      39                 :          0 : }
      40                 :            : 
      41                 :         12 : static void print_stats(pa_mempool *p, const char *text) {
      42                 :         12 :     const pa_mempool_stat*s = pa_mempool_get_stat(p);
      43                 :            : 
      44                 :         12 :     pa_log_debug("%s = {\n"
      45                 :            :                  "\tn_allocated = %u\n"
      46                 :            :                  "\tn_accumulated = %u\n"
      47                 :            :                  "\tn_imported = %u\n"
      48                 :            :                  "\tn_exported = %u\n"
      49                 :            :                  "\tallocated_size = %u\n"
      50                 :            :                  "\taccumulated_size = %u\n"
      51                 :            :                  "\timported_size = %u\n"
      52                 :            :                  "\texported_size = %u\n"
      53                 :            :                  "\tn_too_large_for_pool = %u\n"
      54                 :            :                  "\tn_pool_full = %u\n"
      55                 :            :                  "}",
      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                 :         12 : }
      68                 :            : 
      69                 :          1 : 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                 :          1 :     const char txt[] = "This is a test!";
      82                 :            : 
      83         [ -  + ]:          1 :     if (!getenv("MAKE_CHECK"))
      84                 :          0 :         pa_log_set_level(PA_LOG_DEBUG);
      85                 :            : 
      86                 :          1 :     pool_a = pa_mempool_new(TRUE, 0);
      87                 :          1 :     pool_b = pa_mempool_new(TRUE, 0);
      88                 :          1 :     pool_c = pa_mempool_new(TRUE, 0);
      89                 :            : 
      90                 :          1 :     pa_mempool_get_shm_id(pool_a, &id_a);
      91                 :          1 :     pa_mempool_get_shm_id(pool_b, &id_b);
      92                 :          1 :     pa_mempool_get_shm_id(pool_c, &id_c);
      93                 :            : 
      94 [ +  - ][ -  + ]:          1 :     pa_assert(pool_a && pool_b && pool_c);
      95                 :            : 
      96                 :          1 :     blocks[0] = pa_memblock_new_fixed(pool_a, (void*) txt, sizeof(txt), 1);
      97                 :            : 
      98                 :          1 :     blocks[1] = pa_memblock_new(pool_a, sizeof(txt));
      99                 :          1 :     x = pa_memblock_acquire(blocks[1]);
     100                 :          1 :     snprintf(x, pa_memblock_get_length(blocks[1]), "%s", txt);
     101                 :          1 :     pa_memblock_release(blocks[1]);
     102                 :            : 
     103                 :          1 :     blocks[2] = pa_memblock_new_pool(pool_a, sizeof(txt));
     104                 :          1 :     x = pa_memblock_acquire(blocks[2]);
     105                 :          1 :     snprintf(x, pa_memblock_get_length(blocks[2]), "%s", txt);
     106                 :          1 :     pa_memblock_release(blocks[2]);
     107                 :            : 
     108                 :          1 :     blocks[3] = pa_memblock_new_malloced(pool_a, pa_xstrdup(txt), sizeof(txt));
     109                 :          1 :     blocks[4] = NULL;
     110                 :            : 
     111         [ +  + ]:          5 :     for (i = 0; blocks[i]; i++) {
     112                 :          4 :         pa_log("Memory block %u", i);
     113                 :            : 
     114                 :          4 :         mb_a = blocks[i];
     115         [ -  + ]:          4 :         pa_assert(mb_a);
     116                 :            : 
     117                 :          4 :         export_a = pa_memexport_new(pool_a, revoke_cb, (void*) "A");
     118                 :          4 :         export_b = pa_memexport_new(pool_b, revoke_cb, (void*) "B");
     119                 :            : 
     120         [ -  + ]:          4 :         pa_assert(export_a && export_b);
     121                 :            : 
     122                 :          4 :         import_b = pa_memimport_new(pool_b, release_cb, (void*) "B");
     123                 :          4 :         import_c = pa_memimport_new(pool_c, release_cb, (void*) "C");
     124                 :            : 
     125         [ -  + ]:          4 :         pa_assert(import_b && import_c);
     126                 :            : 
     127                 :          4 :         r = pa_memexport_put(export_a, mb_a, &id, &shm_id, &offset, &size);
     128         [ -  + ]:          4 :         pa_assert(r >= 0);
     129         [ -  + ]:          4 :         pa_assert(shm_id == id_a);
     130                 :            : 
     131                 :          4 :         pa_log("A: Memory block exported as %u", id);
     132                 :            : 
     133                 :          4 :         mb_b = pa_memimport_get(import_b, id, shm_id, offset, size);
     134         [ -  + ]:          4 :         pa_assert(mb_b);
     135                 :          4 :         r = pa_memexport_put(export_b, mb_b, &id, &shm_id, &offset, &size);
     136         [ -  + ]:          4 :         pa_assert(r >= 0);
     137 [ -  + ][ #  # ]:          4 :         pa_assert(shm_id == id_a || shm_id == id_b);
     138                 :          4 :         pa_memblock_unref(mb_b);
     139                 :            : 
     140                 :          4 :         pa_log("B: Memory block exported as %u", id);
     141                 :            : 
     142                 :          4 :         mb_c = pa_memimport_get(import_c, id, shm_id, offset, size);
     143         [ -  + ]:          4 :         pa_assert(mb_c);
     144                 :          4 :         x = pa_memblock_acquire(mb_c);
     145                 :          4 :         pa_log_debug("1 data=%s", x);
     146                 :          4 :         pa_memblock_release(mb_c);
     147                 :            : 
     148                 :          4 :         print_stats(pool_a, "A");
     149                 :          4 :         print_stats(pool_b, "B");
     150                 :          4 :         print_stats(pool_c, "C");
     151                 :            : 
     152                 :          4 :         pa_memexport_free(export_b);
     153                 :          4 :         x = pa_memblock_acquire(mb_c);
     154                 :          4 :         pa_log_debug("2 data=%s", x);
     155                 :          4 :         pa_memblock_release(mb_c);
     156                 :          4 :         pa_memblock_unref(mb_c);
     157                 :            : 
     158                 :          4 :         pa_memimport_free(import_b);
     159                 :            : 
     160                 :          4 :         pa_memblock_unref(mb_a);
     161                 :            : 
     162                 :          4 :         pa_memimport_free(import_c);
     163                 :          4 :         pa_memexport_free(export_a);
     164                 :            :     }
     165                 :            : 
     166                 :          1 :     pa_log("vacuuming...");
     167                 :            : 
     168                 :          1 :     pa_mempool_vacuum(pool_a);
     169                 :          1 :     pa_mempool_vacuum(pool_b);
     170                 :          1 :     pa_mempool_vacuum(pool_c);
     171                 :            : 
     172                 :          1 :     pa_log("vacuuming done...");
     173                 :            : 
     174                 :          1 :     pa_mempool_free(pool_a);
     175                 :          1 :     pa_mempool_free(pool_b);
     176                 :          1 :     pa_mempool_free(pool_c);
     177                 :            : 
     178                 :            :     return 0;
     179                 :            : }

Generated by: LCOV version 1.9