1*a58d3d2aSXin Li /* Copyright (C) 2007 Jean-Marc Valin
2*a58d3d2aSXin Li
3*a58d3d2aSXin Li File: os_support.h
4*a58d3d2aSXin Li This is the (tiny) OS abstraction layer. Aside from math.h, this is the
5*a58d3d2aSXin Li only place where system headers are allowed.
6*a58d3d2aSXin Li
7*a58d3d2aSXin Li Redistribution and use in source and binary forms, with or without
8*a58d3d2aSXin Li modification, are permitted provided that the following conditions are
9*a58d3d2aSXin Li met:
10*a58d3d2aSXin Li
11*a58d3d2aSXin Li 1. Redistributions of source code must retain the above copyright notice,
12*a58d3d2aSXin Li this list of conditions and the following disclaimer.
13*a58d3d2aSXin Li
14*a58d3d2aSXin Li 2. Redistributions in binary form must reproduce the above copyright
15*a58d3d2aSXin Li notice, this list of conditions and the following disclaimer in the
16*a58d3d2aSXin Li documentation and/or other materials provided with the distribution.
17*a58d3d2aSXin Li
18*a58d3d2aSXin Li THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19*a58d3d2aSXin Li IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20*a58d3d2aSXin Li OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21*a58d3d2aSXin Li DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
22*a58d3d2aSXin Li INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23*a58d3d2aSXin Li (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24*a58d3d2aSXin Li SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25*a58d3d2aSXin Li HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26*a58d3d2aSXin Li STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27*a58d3d2aSXin Li ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28*a58d3d2aSXin Li POSSIBILITY OF SUCH DAMAGE.
29*a58d3d2aSXin Li */
30*a58d3d2aSXin Li
31*a58d3d2aSXin Li #ifndef OS_SUPPORT_H
32*a58d3d2aSXin Li #define OS_SUPPORT_H
33*a58d3d2aSXin Li
34*a58d3d2aSXin Li #ifdef CUSTOM_SUPPORT
35*a58d3d2aSXin Li # include "custom_support.h"
36*a58d3d2aSXin Li #endif
37*a58d3d2aSXin Li
38*a58d3d2aSXin Li #include "opus_types.h"
39*a58d3d2aSXin Li #include "opus_defines.h"
40*a58d3d2aSXin Li
41*a58d3d2aSXin Li #include <string.h>
42*a58d3d2aSXin Li #include <stdlib.h>
43*a58d3d2aSXin Li
44*a58d3d2aSXin Li /** Opus wrapper for malloc(). To do your own dynamic allocation replace this function, opus_realloc, and opus_free */
45*a58d3d2aSXin Li #ifndef OVERRIDE_OPUS_ALLOC
opus_alloc(size_t size)46*a58d3d2aSXin Li static OPUS_INLINE void *opus_alloc (size_t size)
47*a58d3d2aSXin Li {
48*a58d3d2aSXin Li return malloc(size);
49*a58d3d2aSXin Li }
50*a58d3d2aSXin Li #endif
51*a58d3d2aSXin Li
52*a58d3d2aSXin Li #ifndef OVERRIDE_OPUS_REALLOC
opus_realloc(void * ptr,size_t size)53*a58d3d2aSXin Li static OPUS_INLINE void *opus_realloc (void *ptr, size_t size)
54*a58d3d2aSXin Li {
55*a58d3d2aSXin Li return realloc(ptr, size);
56*a58d3d2aSXin Li }
57*a58d3d2aSXin Li #endif
58*a58d3d2aSXin Li
59*a58d3d2aSXin Li /** Used only for non-threadsafe pseudostack.
60*a58d3d2aSXin Li If desired, this can always return the same area of memory rather than allocating a new one every time. */
61*a58d3d2aSXin Li #ifndef OVERRIDE_OPUS_ALLOC_SCRATCH
opus_alloc_scratch(size_t size)62*a58d3d2aSXin Li static OPUS_INLINE void *opus_alloc_scratch (size_t size)
63*a58d3d2aSXin Li {
64*a58d3d2aSXin Li /* Scratch space doesn't need to be cleared */
65*a58d3d2aSXin Li return opus_alloc(size);
66*a58d3d2aSXin Li }
67*a58d3d2aSXin Li #endif
68*a58d3d2aSXin Li
69*a58d3d2aSXin Li /** Opus wrapper for free(). To do your own dynamic allocation replace this function, opus_realloc, and opus_free */
70*a58d3d2aSXin Li #ifndef OVERRIDE_OPUS_FREE
opus_free(void * ptr)71*a58d3d2aSXin Li static OPUS_INLINE void opus_free (void *ptr)
72*a58d3d2aSXin Li {
73*a58d3d2aSXin Li free(ptr);
74*a58d3d2aSXin Li }
75*a58d3d2aSXin Li #endif
76*a58d3d2aSXin Li
77*a58d3d2aSXin Li /** Copy n elements from src to dst. The 0* term provides compile-time type checking */
78*a58d3d2aSXin Li #ifndef OVERRIDE_OPUS_COPY
79*a58d3d2aSXin Li #define OPUS_COPY(dst, src, n) (memcpy((dst), (src), (n)*sizeof(*(dst)) + 0*((dst)-(src)) ))
80*a58d3d2aSXin Li #endif
81*a58d3d2aSXin Li
82*a58d3d2aSXin Li /** Copy n elements from src to dst, allowing overlapping regions. The 0* term
83*a58d3d2aSXin Li provides compile-time type checking */
84*a58d3d2aSXin Li #ifndef OVERRIDE_OPUS_MOVE
85*a58d3d2aSXin Li #define OPUS_MOVE(dst, src, n) (memmove((dst), (src), (n)*sizeof(*(dst)) + 0*((dst)-(src)) ))
86*a58d3d2aSXin Li #endif
87*a58d3d2aSXin Li
88*a58d3d2aSXin Li /** Set n elements of dst to zero */
89*a58d3d2aSXin Li #ifndef OVERRIDE_OPUS_CLEAR
90*a58d3d2aSXin Li #define OPUS_CLEAR(dst, n) (memset((dst), 0, (n)*sizeof(*(dst))))
91*a58d3d2aSXin Li #endif
92*a58d3d2aSXin Li
93*a58d3d2aSXin Li /*#ifdef __GNUC__
94*a58d3d2aSXin Li #pragma GCC poison printf sprintf
95*a58d3d2aSXin Li #pragma GCC poison malloc free realloc calloc
96*a58d3d2aSXin Li #endif*/
97*a58d3d2aSXin Li
98*a58d3d2aSXin Li #endif /* OS_SUPPORT_H */
99*a58d3d2aSXin Li
100