LCOV - code coverage report
Current view: top level - pulse - xmalloc.c (source / functions) Hit Total Coverage
Test: lcov.out Lines: 36 44 81.8 %
Date: 2012-07-17 Functions: 7 8 87.5 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 15 28 53.6 %

           Branch data     Line data    Source code
       1                 :            : /***
       2                 :            :   This file is part of PulseAudio.
       3                 :            : 
       4                 :            :   Copyright 2004-2006 Lennart Poettering
       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.1 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 <stdlib.h>
      27                 :            : #include <signal.h>
      28                 :            : #include <unistd.h>
      29                 :            : #include <string.h>
      30                 :            : #include <errno.h>
      31                 :            : 
      32                 :            : #include <pulse/gccmacro.h>
      33                 :            : #include <pulsecore/core-util.h>
      34                 :            : #include <pulsecore/macro.h>
      35                 :            : 
      36                 :            : #include "xmalloc.h"
      37                 :            : 
      38                 :            : /* Make sure not to allocate more than this much memory. */
      39                 :            : #define MAX_ALLOC_SIZE (1024*1024*96) /* 96MB */
      40                 :            : 
      41                 :            : /* #undef malloc */
      42                 :            : /* #undef free */
      43                 :            : /* #undef realloc */
      44                 :            : /* #undef strndup */
      45                 :            : /* #undef strdup */
      46                 :            : 
      47                 :            : static void oom(void) PA_GCC_NORETURN;
      48                 :            : 
      49                 :            : /* called in case of an OOM situation. Prints an error message and
      50                 :            :  * exits */
      51                 :          0 : static void oom(void) {
      52                 :            :     static const char e[] = "Not enough memory\n";
      53                 :          0 :     pa_loop_write(STDERR_FILENO, e, sizeof(e)-1, NULL);
      54                 :            : #ifdef SIGQUIT
      55                 :          0 :     raise(SIGQUIT);
      56                 :            : #endif
      57                 :          0 :     _exit(1);
      58                 :            : }
      59                 :            : 
      60                 :      51681 : void* pa_xmalloc(size_t size) {
      61                 :            :     void *p;
      62         [ -  + ]:      51681 :     pa_assert(size > 0);
      63         [ -  + ]:      51681 :     pa_assert(size < MAX_ALLOC_SIZE);
      64                 :            : 
      65         [ -  + ]:      51681 :     if (!(p = malloc(size)))
      66                 :          0 :         oom();
      67                 :            : 
      68                 :      51681 :     return p;
      69                 :            : }
      70                 :            : 
      71                 :      50448 : void* pa_xmalloc0(size_t size) {
      72                 :            :     void *p;
      73         [ -  + ]:      50448 :     pa_assert(size > 0);
      74         [ -  + ]:      50448 :     pa_assert(size < MAX_ALLOC_SIZE);
      75                 :            : 
      76         [ -  + ]:      50448 :     if (!(p = calloc(1, size)))
      77                 :          0 :         oom();
      78                 :            : 
      79                 :      50448 :     return p;
      80                 :            : }
      81                 :            : 
      82                 :      50058 : void *pa_xrealloc(void *ptr, size_t size) {
      83                 :            :     void *p;
      84         [ -  + ]:      50058 :     pa_assert(size > 0);
      85         [ -  + ]:      50058 :     pa_assert(size < MAX_ALLOC_SIZE);
      86                 :            : 
      87         [ -  + ]:      50058 :     if (!(p = realloc(ptr, size)))
      88                 :          0 :         oom();
      89                 :      50058 :     return p;
      90                 :            : }
      91                 :            : 
      92                 :      50122 : void* pa_xmemdup(const void *p, size_t l) {
      93         [ +  - ]:      50122 :     if (!p)
      94                 :            :         return NULL;
      95                 :            :     else {
      96                 :      50122 :         char *r = pa_xmalloc(l);
      97                 :      50122 :         memcpy(r, p, l);
      98                 :      50122 :         return r;
      99                 :            :     }
     100                 :            : }
     101                 :            : 
     102                 :      50107 : char *pa_xstrdup(const char *s) {
     103         [ +  - ]:      50107 :     if (!s)
     104                 :            :         return NULL;
     105                 :            : 
     106                 :      50107 :     return pa_xmemdup(s, strlen(s)+1);
     107                 :            : }
     108                 :            : 
     109                 :         80 : char *pa_xstrndup(const char *s, size_t l) {
     110                 :            :     char *e, *r;
     111                 :            : 
     112         [ +  - ]:         80 :     if (!s)
     113                 :            :         return NULL;
     114                 :            : 
     115         [ -  + ]:         80 :     if ((e = memchr(s, 0, l)))
     116                 :          0 :         return pa_xmemdup(s, (size_t) (e-s+1));
     117                 :            : 
     118                 :         80 :     r = pa_xmalloc(l+1);
     119                 :         80 :     memcpy(r, s, l);
     120                 :         80 :     r[l] = 0;
     121                 :         80 :     return r;
     122                 :            : }
     123                 :            : 
     124                 :     151629 : void pa_xfree(void *p) {
     125                 :            :     int saved_errno;
     126                 :            : 
     127         [ +  + ]:     151629 :     if (!p)
     128                 :     151625 :         return;
     129                 :            : 
     130                 :     151542 :     saved_errno = errno;
     131                 :     151541 :     free(p);
     132                 :     151541 :     errno = saved_errno;
     133                 :            : }

Generated by: LCOV version 1.9