Home | History | Annotate | Download | only in libspeex
      1 /* Copyright (C) 2007 Jean-Marc Valin
      2 
      3    File: os_support.h
      4    This is the (tiny) OS abstraction layer. Aside from math.h, this is the
      5    only place where system headers are allowed.
      6 
      7    Redistribution and use in source and binary forms, with or without
      8    modification, are permitted provided that the following conditions are
      9    met:
     10 
     11    1. Redistributions of source code must retain the above copyright notice,
     12    this list of conditions and the following disclaimer.
     13 
     14    2. Redistributions in binary form must reproduce the above copyright
     15    notice, this list of conditions and the following disclaimer in the
     16    documentation and/or other materials provided with the distribution.
     17 
     18    3. The name of the author may not be used to endorse or promote products
     19    derived from this software without specific prior written permission.
     20 
     21    THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23    OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     24    DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     25    INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     26    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     27    SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28    HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     29    STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     30    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31    POSSIBILITY OF SUCH DAMAGE.
     32 */
     33 
     34 #ifndef OS_SUPPORT_H
     35 #define OS_SUPPORT_H
     36 
     37 #include <string.h>
     38 #include <stdio.h>
     39 #include <stdlib.h>
     40 
     41 #ifdef HAVE_CONFIG_H
     42 #include "config.h"
     43 #endif
     44 #ifdef OS_SUPPORT_CUSTOM
     45 #include "os_support_custom.h"
     46 #endif
     47 
     48 /** Speex wrapper for calloc. To do your own dynamic allocation, all you need to do is replace this function, speex_realloc and speex_free
     49     NOTE: speex_alloc needs to CLEAR THE MEMORY */
     50 #ifndef OVERRIDE_SPEEX_ALLOC
     51 static inline void *speex_alloc (int size)
     52 {
     53    /* WARNING: this is not equivalent to malloc(). If you want to use malloc()
     54       or your own allocator, YOU NEED TO CLEAR THE MEMORY ALLOCATED. Otherwise
     55       you will experience strange bugs */
     56    return calloc(size,1);
     57 }
     58 #endif
     59 
     60 /** Same as speex_alloc, except that the area is only needed inside a Speex call (might cause problem with wideband though) */
     61 #ifndef OVERRIDE_SPEEX_ALLOC_SCRATCH
     62 static inline void *speex_alloc_scratch (int size)
     63 {
     64    /* Scratch space doesn't need to be cleared */
     65    return calloc(size,1);
     66 }
     67 #endif
     68 
     69 /** Speex wrapper for realloc. To do your own dynamic allocation, all you need to do is replace this function, speex_alloc and speex_free */
     70 #ifndef OVERRIDE_SPEEX_REALLOC
     71 static inline void *speex_realloc (void *ptr, int size)
     72 {
     73    return realloc(ptr, size);
     74 }
     75 #endif
     76 
     77 /** Speex wrapper for calloc. To do your own dynamic allocation, all you need to do is replace this function, speex_realloc and speex_alloc */
     78 #ifndef OVERRIDE_SPEEX_FREE
     79 static inline void speex_free (void *ptr)
     80 {
     81    free(ptr);
     82 }
     83 #endif
     84 
     85 /** Same as speex_free, except that the area is only needed inside a Speex call (might cause problem with wideband though) */
     86 #ifndef OVERRIDE_SPEEX_FREE_SCRATCH
     87 static inline void speex_free_scratch (void *ptr)
     88 {
     89    free(ptr);
     90 }
     91 #endif
     92 
     93 /** Copy n bytes of memory from src to dst. The 0* term provides compile-time type checking  */
     94 #ifndef OVERRIDE_SPEEX_COPY
     95 #define SPEEX_COPY(dst, src, n) (memcpy((dst), (src), (n)*sizeof(*(dst)) + 0*((dst)-(src)) ))
     96 #endif
     97 
     98 /** Copy n bytes of memory from src to dst, allowing overlapping regions. The 0* term
     99     provides compile-time type checking */
    100 #ifndef OVERRIDE_SPEEX_MOVE
    101 #define SPEEX_MOVE(dst, src, n) (memmove((dst), (src), (n)*sizeof(*(dst)) + 0*((dst)-(src)) ))
    102 #endif
    103 
    104 /** Set n bytes of memory to value of c, starting at address s */
    105 #ifndef OVERRIDE_SPEEX_MEMSET
    106 #define SPEEX_MEMSET(dst, c, n) (memset((dst), (c), (n)*sizeof(*(dst))))
    107 #endif
    108 
    109 
    110 #ifndef OVERRIDE_SPEEX_FATAL
    111 static inline void _speex_fatal(const char *str, const char *file, int line)
    112 {
    113    fprintf (stderr, "Fatal (internal) error in %s, line %d: %s\n", file, line, str);
    114    exit(1);
    115 }
    116 #endif
    117 
    118 #ifndef OVERRIDE_SPEEX_WARNING
    119 static inline void speex_warning(const char *str)
    120 {
    121 #ifndef DISABLE_WARNINGS
    122    fprintf (stderr, "warning: %s\n", str);
    123 #endif
    124 }
    125 #endif
    126 
    127 #ifndef OVERRIDE_SPEEX_WARNING_INT
    128 static inline void speex_warning_int(const char *str, int val)
    129 {
    130 #ifndef DISABLE_WARNINGS
    131    fprintf (stderr, "warning: %s %d\n", str, val);
    132 #endif
    133 }
    134 #endif
    135 
    136 #ifndef OVERRIDE_SPEEX_NOTIFY
    137 static inline void speex_notify(const char *str)
    138 {
    139 #ifndef DISABLE_NOTIFICATIONS
    140    fprintf (stderr, "notification: %s\n", str);
    141 #endif
    142 }
    143 #endif
    144 
    145 #ifndef OVERRIDE_SPEEX_PUTC
    146 /** Speex wrapper for putc */
    147 static inline void _speex_putc(int ch, void *file)
    148 {
    149    FILE *f = (FILE *)file;
    150    fprintf(f, "%c", ch);
    151 }
    152 #endif
    153 
    154 #define speex_fatal(str) _speex_fatal(str, __FILE__, __LINE__);
    155 #define speex_assert(cond) {if (!(cond)) {speex_fatal("assertion failed: " #cond);}}
    156 
    157 #ifndef RELEASE
    158 static inline void print_vec(float *vec, int len, char *name)
    159 {
    160    int i;
    161    printf ("%s ", name);
    162    for (i=0;i<len;i++)
    163       printf (" %f", vec[i]);
    164    printf ("\n");
    165 }
    166 #endif
    167 
    168 #endif
    169 
    170