xref: /aosp_15_r20/external/speex/ti/os_support_custom.h (revision 28e138c64d234588b5cd2a8a403b584bd3036e4e)
1*28e138c6SAndroid Build Coastguard Worker /* Copyright (C) 2007 Psi Systems, Inc.
2*28e138c6SAndroid Build Coastguard Worker    Author:  Jean-Marc Valin
3*28e138c6SAndroid Build Coastguard Worker    File: os_support_custom.h
4*28e138c6SAndroid Build Coastguard Worker    Memory Allocation overrides to allow user control rather than C alloc/free.
5*28e138c6SAndroid Build Coastguard Worker 
6*28e138c6SAndroid Build Coastguard Worker    Redistribution and use in source and binary forms, with or without
7*28e138c6SAndroid Build Coastguard Worker    modification, are permitted provided that the following conditions
8*28e138c6SAndroid Build Coastguard Worker    are met:
9*28e138c6SAndroid Build Coastguard Worker 
10*28e138c6SAndroid Build Coastguard Worker    - Redistributions of source code must retain the above copyright
11*28e138c6SAndroid Build Coastguard Worker    notice, this list of conditions and the following disclaimer.
12*28e138c6SAndroid Build Coastguard Worker 
13*28e138c6SAndroid Build Coastguard Worker    - Redistributions in binary form must reproduce the above copyright
14*28e138c6SAndroid Build Coastguard Worker    notice, this list of conditions and the following disclaimer in the
15*28e138c6SAndroid Build Coastguard Worker    documentation and/or other materials provided with the distribution.
16*28e138c6SAndroid Build Coastguard Worker 
17*28e138c6SAndroid Build Coastguard Worker    - Neither the name of the Xiph.org Foundation nor the names of its
18*28e138c6SAndroid Build Coastguard Worker    contributors may be used to endorse or promote products derived from
19*28e138c6SAndroid Build Coastguard Worker    this software without specific prior written permission.
20*28e138c6SAndroid Build Coastguard Worker 
21*28e138c6SAndroid Build Coastguard Worker    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22*28e138c6SAndroid Build Coastguard Worker    ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23*28e138c6SAndroid Build Coastguard Worker    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24*28e138c6SAndroid Build Coastguard Worker    A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
25*28e138c6SAndroid Build Coastguard Worker    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26*28e138c6SAndroid Build Coastguard Worker    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27*28e138c6SAndroid Build Coastguard Worker    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28*28e138c6SAndroid Build Coastguard Worker    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29*28e138c6SAndroid Build Coastguard Worker    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30*28e138c6SAndroid Build Coastguard Worker    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31*28e138c6SAndroid Build Coastguard Worker    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*28e138c6SAndroid Build Coastguard Worker */
33*28e138c6SAndroid Build Coastguard Worker 
34*28e138c6SAndroid Build Coastguard Worker #ifdef MANUAL_ALLOC
35*28e138c6SAndroid Build Coastguard Worker 
36*28e138c6SAndroid Build Coastguard Worker /* To avoid changing the Speex call model, this file relies on four static variables
37*28e138c6SAndroid Build Coastguard Worker    The user main creates two linear buffers, and initializes spxGlobalHeap/ScratchPtr
38*28e138c6SAndroid Build Coastguard Worker    to point to the start of the two buffers, and initializes spxGlobalHeap/ScratchEnd
39*28e138c6SAndroid Build Coastguard Worker    to point to the first address following the last byte of the two buffers.
40*28e138c6SAndroid Build Coastguard Worker 
41*28e138c6SAndroid Build Coastguard Worker    This mechanism allows, for example, data caching for multichannel applications,
42*28e138c6SAndroid Build Coastguard Worker    where the Speex state is swapped from a large slow memory to a small fast memory
43*28e138c6SAndroid Build Coastguard Worker    each time the codec runs.
44*28e138c6SAndroid Build Coastguard Worker 
45*28e138c6SAndroid Build Coastguard Worker    Persistent data is allocated in spxGlobalHeap (instead of calloc), while scratch
46*28e138c6SAndroid Build Coastguard Worker    data is allocated in spxGlobalScratch.
47*28e138c6SAndroid Build Coastguard Worker */
48*28e138c6SAndroid Build Coastguard Worker 
49*28e138c6SAndroid Build Coastguard Worker extern char *spxGlobalHeapPtr, *spxGlobalHeapEnd;
50*28e138c6SAndroid Build Coastguard Worker extern char *spxGlobalScratchPtr, *spxGlobalScratchEnd;
51*28e138c6SAndroid Build Coastguard Worker 
52*28e138c6SAndroid Build Coastguard Worker /* Make sure that all structures are aligned to largest type */
53*28e138c6SAndroid Build Coastguard Worker #define BLOCK_MASK      (sizeof(long double)-1)
54*28e138c6SAndroid Build Coastguard Worker extern inline void speex_warning(const char *str);
55*28e138c6SAndroid Build Coastguard Worker 
56*28e138c6SAndroid Build Coastguard Worker #define OVERRIDE_SPEEX_ALLOC
speex_alloc(int size)57*28e138c6SAndroid Build Coastguard Worker static inline void *speex_alloc (int size)
58*28e138c6SAndroid Build Coastguard Worker {
59*28e138c6SAndroid Build Coastguard Worker     void *ptr;
60*28e138c6SAndroid Build Coastguard Worker 
61*28e138c6SAndroid Build Coastguard Worker     ptr = (void *) (((int)spxGlobalHeapPtr + BLOCK_MASK) & ~BLOCK_MASK);  //Start on 8 boundary
62*28e138c6SAndroid Build Coastguard Worker 
63*28e138c6SAndroid Build Coastguard Worker     spxGlobalHeapPtr = (char *)((int)ptr + size);	// Update pointer to next free location
64*28e138c6SAndroid Build Coastguard Worker 
65*28e138c6SAndroid Build Coastguard Worker     if (spxGlobalHeapPtr > spxGlobalHeapEnd )
66*28e138c6SAndroid Build Coastguard Worker     {
67*28e138c6SAndroid Build Coastguard Worker #ifdef VERBOSE_ALLOC
68*28e138c6SAndroid Build Coastguard Worker 	    fprintf (stderr, "insufficient space for persistent alloc request %d bytes\n", size);
69*28e138c6SAndroid Build Coastguard Worker #endif
70*28e138c6SAndroid Build Coastguard Worker        return 0;
71*28e138c6SAndroid Build Coastguard Worker     }
72*28e138c6SAndroid Build Coastguard Worker 
73*28e138c6SAndroid Build Coastguard Worker #ifdef VERBOSE_ALLOC
74*28e138c6SAndroid Build Coastguard Worker     fprintf (stderr, "Persist Allocated %d chars at %x, %d remaining\n", size, ptr, ((int)spxGlobalHeapEnd - (int)spxGlobalHeapPtr));
75*28e138c6SAndroid Build Coastguard Worker #endif
76*28e138c6SAndroid Build Coastguard Worker     memset(ptr, 0, size);
77*28e138c6SAndroid Build Coastguard Worker     return ptr;
78*28e138c6SAndroid Build Coastguard Worker }
79*28e138c6SAndroid Build Coastguard Worker 
80*28e138c6SAndroid Build Coastguard Worker #define OVERRIDE_SPEEX_ALLOC_SCRATCH
speex_alloc_scratch(int size)81*28e138c6SAndroid Build Coastguard Worker static inline void *speex_alloc_scratch (int size)
82*28e138c6SAndroid Build Coastguard Worker {
83*28e138c6SAndroid Build Coastguard Worker     void *ptr;
84*28e138c6SAndroid Build Coastguard Worker 
85*28e138c6SAndroid Build Coastguard Worker     ptr = (void *) (((int)spxGlobalScratchPtr + BLOCK_MASK) & ~BLOCK_MASK);  //Start on 8 boundary
86*28e138c6SAndroid Build Coastguard Worker 
87*28e138c6SAndroid Build Coastguard Worker     spxGlobalScratchPtr = (char *)((int)ptr + size);	// Update pointer to next free location
88*28e138c6SAndroid Build Coastguard Worker 
89*28e138c6SAndroid Build Coastguard Worker     if (spxGlobalScratchPtr > spxGlobalScratchEnd )
90*28e138c6SAndroid Build Coastguard Worker     {
91*28e138c6SAndroid Build Coastguard Worker #ifdef VERBOSE_ALLOC
92*28e138c6SAndroid Build Coastguard Worker 	    fprintf (stderr, "insufficient space for scratch alloc request %d bytes\n", size);
93*28e138c6SAndroid Build Coastguard Worker #endif
94*28e138c6SAndroid Build Coastguard Worker        return 0;
95*28e138c6SAndroid Build Coastguard Worker     }
96*28e138c6SAndroid Build Coastguard Worker 
97*28e138c6SAndroid Build Coastguard Worker #ifdef VERBOSE_ALLOC
98*28e138c6SAndroid Build Coastguard Worker     fprintf (stderr, "Scratch Allocated %d chars at %x, %d remaining\n", size, ptr, ((int)spxGlobalScratchEnd - (int)spxGlobalScratchPtr));
99*28e138c6SAndroid Build Coastguard Worker #endif
100*28e138c6SAndroid Build Coastguard Worker     memset(ptr, 0, size);
101*28e138c6SAndroid Build Coastguard Worker     return ptr;
102*28e138c6SAndroid Build Coastguard Worker }
103*28e138c6SAndroid Build Coastguard Worker 
104*28e138c6SAndroid Build Coastguard Worker #define OVERRIDE_SPEEX_REALLOC
speex_realloc(void * ptr,int size)105*28e138c6SAndroid Build Coastguard Worker static inline void *speex_realloc (void *ptr, int size)
106*28e138c6SAndroid Build Coastguard Worker {
107*28e138c6SAndroid Build Coastguard Worker #ifdef VERBOSE_ALLOC
108*28e138c6SAndroid Build Coastguard Worker    speex_warning("realloc attempted, not allowed");
109*28e138c6SAndroid Build Coastguard Worker #endif
110*28e138c6SAndroid Build Coastguard Worker    return 0;
111*28e138c6SAndroid Build Coastguard Worker }
112*28e138c6SAndroid Build Coastguard Worker 
113*28e138c6SAndroid Build Coastguard Worker #define OVERRIDE_SPEEX_FREE
speex_free(void * ptr)114*28e138c6SAndroid Build Coastguard Worker static inline void speex_free (void *ptr)
115*28e138c6SAndroid Build Coastguard Worker {
116*28e138c6SAndroid Build Coastguard Worker #ifdef VERBOSE_ALLOC
117*28e138c6SAndroid Build Coastguard Worker    speex_warning("at speex_free");
118*28e138c6SAndroid Build Coastguard Worker #endif
119*28e138c6SAndroid Build Coastguard Worker }
120*28e138c6SAndroid Build Coastguard Worker #define OVERRIDE_SPEEX_FREE_SCRATCH
speex_free_scratch(void * ptr)121*28e138c6SAndroid Build Coastguard Worker static inline void speex_free_scratch (void *ptr)
122*28e138c6SAndroid Build Coastguard Worker {
123*28e138c6SAndroid Build Coastguard Worker #ifdef VERBOSE_ALLOC
124*28e138c6SAndroid Build Coastguard Worker    speex_warning("at speex_free_scratch");
125*28e138c6SAndroid Build Coastguard Worker #endif
126*28e138c6SAndroid Build Coastguard Worker }
127*28e138c6SAndroid Build Coastguard Worker 
128*28e138c6SAndroid Build Coastguard Worker #endif    /* !MANUAL_ALLOC */
129