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