153a5a1b3Sopenharmony_ci/* Copyright (C) 2007 Jean-Marc Valin
253a5a1b3Sopenharmony_ci
353a5a1b3Sopenharmony_ci   File: os_support.h
453a5a1b3Sopenharmony_ci   This is the (tiny) OS abstraction layer. Aside from math.h, this is the
553a5a1b3Sopenharmony_ci   only place where system headers are allowed.
653a5a1b3Sopenharmony_ci
753a5a1b3Sopenharmony_ci   Redistribution and use in source and binary forms, with or without
853a5a1b3Sopenharmony_ci   modification, are permitted provided that the following conditions are
953a5a1b3Sopenharmony_ci   met:
1053a5a1b3Sopenharmony_ci
1153a5a1b3Sopenharmony_ci   1. Redistributions of source code must retain the above copyright notice,
1253a5a1b3Sopenharmony_ci   this list of conditions and the following disclaimer.
1353a5a1b3Sopenharmony_ci
1453a5a1b3Sopenharmony_ci   2. Redistributions in binary form must reproduce the above copyright
1553a5a1b3Sopenharmony_ci   notice, this list of conditions and the following disclaimer in the
1653a5a1b3Sopenharmony_ci   documentation and/or other materials provided with the distribution.
1753a5a1b3Sopenharmony_ci
1853a5a1b3Sopenharmony_ci   3. The name of the author may not be used to endorse or promote products
1953a5a1b3Sopenharmony_ci   derived from this software without specific prior written permission.
2053a5a1b3Sopenharmony_ci
2153a5a1b3Sopenharmony_ci   THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2253a5a1b3Sopenharmony_ci   IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2353a5a1b3Sopenharmony_ci   OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
2453a5a1b3Sopenharmony_ci   DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
2553a5a1b3Sopenharmony_ci   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
2653a5a1b3Sopenharmony_ci   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
2753a5a1b3Sopenharmony_ci   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2853a5a1b3Sopenharmony_ci   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
2953a5a1b3Sopenharmony_ci   STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
3053a5a1b3Sopenharmony_ci   ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3153a5a1b3Sopenharmony_ci   POSSIBILITY OF SUCH DAMAGE.
3253a5a1b3Sopenharmony_ci*/
3353a5a1b3Sopenharmony_ci
3453a5a1b3Sopenharmony_ci#ifndef OS_SUPPORT_H
3553a5a1b3Sopenharmony_ci#define OS_SUPPORT_H
3653a5a1b3Sopenharmony_ci
3753a5a1b3Sopenharmony_ci#include <string.h>
3853a5a1b3Sopenharmony_ci#include <stdio.h>
3953a5a1b3Sopenharmony_ci#include <stdlib.h>
4053a5a1b3Sopenharmony_ci
4153a5a1b3Sopenharmony_ci#ifdef HAVE_CONFIG_H
4253a5a1b3Sopenharmony_ci#include "config.h"
4353a5a1b3Sopenharmony_ci#endif
4453a5a1b3Sopenharmony_ci#ifdef OS_SUPPORT_CUSTOM
4553a5a1b3Sopenharmony_ci#include "os_support_custom.h"
4653a5a1b3Sopenharmony_ci#endif
4753a5a1b3Sopenharmony_ci
4853a5a1b3Sopenharmony_ci/** Speex wrapper for calloc. To do your own dynamic allocation, all you need to do is replace this function, speex_realloc and speex_free
4953a5a1b3Sopenharmony_ci    NOTE: speex_alloc needs to CLEAR THE MEMORY */
5053a5a1b3Sopenharmony_ci#ifndef OVERRIDE_SPEEX_ALLOC
5153a5a1b3Sopenharmony_cistatic inline void *speex_alloc (int size)
5253a5a1b3Sopenharmony_ci{
5353a5a1b3Sopenharmony_ci   /* WARNING: this is not equivalent to malloc(). If you want to use malloc()
5453a5a1b3Sopenharmony_ci      or your own allocator, YOU NEED TO CLEAR THE MEMORY ALLOCATED. Otherwise
5553a5a1b3Sopenharmony_ci      you will experience strange bugs */
5653a5a1b3Sopenharmony_ci   return calloc(size,1);
5753a5a1b3Sopenharmony_ci}
5853a5a1b3Sopenharmony_ci#endif
5953a5a1b3Sopenharmony_ci
6053a5a1b3Sopenharmony_ci/** Same as speex_alloc, except that the area is only needed inside a Speex call (might cause problem with wideband though) */
6153a5a1b3Sopenharmony_ci#ifndef OVERRIDE_SPEEX_ALLOC_SCRATCH
6253a5a1b3Sopenharmony_cistatic inline void *speex_alloc_scratch (int size)
6353a5a1b3Sopenharmony_ci{
6453a5a1b3Sopenharmony_ci   /* Scratch space doesn't need to be cleared */
6553a5a1b3Sopenharmony_ci   return calloc(size,1);
6653a5a1b3Sopenharmony_ci}
6753a5a1b3Sopenharmony_ci#endif
6853a5a1b3Sopenharmony_ci
6953a5a1b3Sopenharmony_ci/** Speex wrapper for realloc. To do your own dynamic allocation, all you need to do is replace this function, speex_alloc and speex_free */
7053a5a1b3Sopenharmony_ci#ifndef OVERRIDE_SPEEX_REALLOC
7153a5a1b3Sopenharmony_cistatic inline void *speex_realloc (void *ptr, int size)
7253a5a1b3Sopenharmony_ci{
7353a5a1b3Sopenharmony_ci   return realloc(ptr, size);
7453a5a1b3Sopenharmony_ci}
7553a5a1b3Sopenharmony_ci#endif
7653a5a1b3Sopenharmony_ci
7753a5a1b3Sopenharmony_ci/** Speex wrapper for calloc. To do your own dynamic allocation, all you need to do is replace this function, speex_realloc and speex_alloc */
7853a5a1b3Sopenharmony_ci#ifndef OVERRIDE_SPEEX_FREE
7953a5a1b3Sopenharmony_cistatic inline void speex_free (void *ptr)
8053a5a1b3Sopenharmony_ci{
8153a5a1b3Sopenharmony_ci   free(ptr);
8253a5a1b3Sopenharmony_ci}
8353a5a1b3Sopenharmony_ci#endif
8453a5a1b3Sopenharmony_ci
8553a5a1b3Sopenharmony_ci/** Same as speex_free, except that the area is only needed inside a Speex call (might cause problem with wideband though) */
8653a5a1b3Sopenharmony_ci#ifndef OVERRIDE_SPEEX_FREE_SCRATCH
8753a5a1b3Sopenharmony_cistatic inline void speex_free_scratch (void *ptr)
8853a5a1b3Sopenharmony_ci{
8953a5a1b3Sopenharmony_ci   free(ptr);
9053a5a1b3Sopenharmony_ci}
9153a5a1b3Sopenharmony_ci#endif
9253a5a1b3Sopenharmony_ci
9353a5a1b3Sopenharmony_ci/** Copy n elements from src to dst. The 0* term provides compile-time type checking  */
9453a5a1b3Sopenharmony_ci#ifndef OVERRIDE_SPEEX_COPY
9553a5a1b3Sopenharmony_ci#define SPEEX_COPY(dst, src, n) (memcpy((dst), (src), (n)*sizeof(*(dst)) + 0*((dst)-(src)) ))
9653a5a1b3Sopenharmony_ci#endif
9753a5a1b3Sopenharmony_ci
9853a5a1b3Sopenharmony_ci/** Copy n elements from src to dst, allowing overlapping regions. The 0* term
9953a5a1b3Sopenharmony_ci    provides compile-time type checking */
10053a5a1b3Sopenharmony_ci#ifndef OVERRIDE_SPEEX_MOVE
10153a5a1b3Sopenharmony_ci#define SPEEX_MOVE(dst, src, n) (memmove((dst), (src), (n)*sizeof(*(dst)) + 0*((dst)-(src)) ))
10253a5a1b3Sopenharmony_ci#endif
10353a5a1b3Sopenharmony_ci
10453a5a1b3Sopenharmony_ci/** For n elements worth of memory, set every byte to the value of c, starting at address dst */
10553a5a1b3Sopenharmony_ci#ifndef OVERRIDE_SPEEX_MEMSET
10653a5a1b3Sopenharmony_ci#define SPEEX_MEMSET(dst, c, n) (memset((dst), (c), (n)*sizeof(*(dst))))
10753a5a1b3Sopenharmony_ci#endif
10853a5a1b3Sopenharmony_ci
10953a5a1b3Sopenharmony_ci
11053a5a1b3Sopenharmony_ci#ifndef OVERRIDE_SPEEX_FATAL
11153a5a1b3Sopenharmony_cistatic inline void _speex_fatal(const char *str, const char *file, int line)
11253a5a1b3Sopenharmony_ci{
11353a5a1b3Sopenharmony_ci   fprintf (stderr, "Fatal (internal) error in %s, line %d: %s\n", file, line, str);
11453a5a1b3Sopenharmony_ci   exit(1);
11553a5a1b3Sopenharmony_ci}
11653a5a1b3Sopenharmony_ci#endif
11753a5a1b3Sopenharmony_ci
11853a5a1b3Sopenharmony_ci#ifndef OVERRIDE_SPEEX_WARNING
11953a5a1b3Sopenharmony_cistatic inline void speex_warning(const char *str)
12053a5a1b3Sopenharmony_ci{
12153a5a1b3Sopenharmony_ci#ifndef DISABLE_WARNINGS
12253a5a1b3Sopenharmony_ci   fprintf (stderr, "warning: %s\n", str);
12353a5a1b3Sopenharmony_ci#endif
12453a5a1b3Sopenharmony_ci}
12553a5a1b3Sopenharmony_ci#endif
12653a5a1b3Sopenharmony_ci
12753a5a1b3Sopenharmony_ci#ifndef OVERRIDE_SPEEX_WARNING_INT
12853a5a1b3Sopenharmony_cistatic inline void speex_warning_int(const char *str, int val)
12953a5a1b3Sopenharmony_ci{
13053a5a1b3Sopenharmony_ci#ifndef DISABLE_WARNINGS
13153a5a1b3Sopenharmony_ci   fprintf (stderr, "warning: %s %d\n", str, val);
13253a5a1b3Sopenharmony_ci#endif
13353a5a1b3Sopenharmony_ci}
13453a5a1b3Sopenharmony_ci#endif
13553a5a1b3Sopenharmony_ci
13653a5a1b3Sopenharmony_ci#ifndef OVERRIDE_SPEEX_NOTIFY
13753a5a1b3Sopenharmony_cistatic inline void speex_notify(const char *str)
13853a5a1b3Sopenharmony_ci{
13953a5a1b3Sopenharmony_ci#ifndef DISABLE_NOTIFICATIONS
14053a5a1b3Sopenharmony_ci   fprintf (stderr, "notification: %s\n", str);
14153a5a1b3Sopenharmony_ci#endif
14253a5a1b3Sopenharmony_ci}
14353a5a1b3Sopenharmony_ci#endif
14453a5a1b3Sopenharmony_ci
14553a5a1b3Sopenharmony_ci#ifndef OVERRIDE_SPEEX_PUTC
14653a5a1b3Sopenharmony_ci/** Speex wrapper for putc */
14753a5a1b3Sopenharmony_cistatic inline void _speex_putc(int ch, void *file)
14853a5a1b3Sopenharmony_ci{
14953a5a1b3Sopenharmony_ci   FILE *f = (FILE *)file;
15053a5a1b3Sopenharmony_ci   fprintf(f, "%c", ch);
15153a5a1b3Sopenharmony_ci}
15253a5a1b3Sopenharmony_ci#endif
15353a5a1b3Sopenharmony_ci
15453a5a1b3Sopenharmony_ci#define speex_fatal(str) _speex_fatal(str, __FILE__, __LINE__);
15553a5a1b3Sopenharmony_ci#define speex_assert(cond) {if (!(cond)) {speex_fatal("assertion failed: " #cond);}}
15653a5a1b3Sopenharmony_ci
15753a5a1b3Sopenharmony_ci#ifndef RELEASE
15853a5a1b3Sopenharmony_cistatic inline void print_vec(float *vec, int len, char *name)
15953a5a1b3Sopenharmony_ci{
16053a5a1b3Sopenharmony_ci   int i;
16153a5a1b3Sopenharmony_ci   printf ("%s ", name);
16253a5a1b3Sopenharmony_ci   for (i=0;i<len;i++)
16353a5a1b3Sopenharmony_ci      printf (" %f", vec[i]);
16453a5a1b3Sopenharmony_ci   printf ("\n");
16553a5a1b3Sopenharmony_ci}
16653a5a1b3Sopenharmony_ci#endif
16753a5a1b3Sopenharmony_ci
16853a5a1b3Sopenharmony_ci#endif
16953a5a1b3Sopenharmony_ci
170