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