1 /* Copyright (C) 2005 Psi Systems, Inc.
2 Author: Jean-Marc Valin
4 Memory Allocation overrides to allow user control rather than C alloc/free.
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
10 - Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
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.
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.
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.
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.
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.
45 Persistent data is allocated in spxGlobalHeap (instead of calloc), while scratch
46 data is allocated in spxGlobalScratch.
49 extern char *spxGlobalHeapPtr
, *spxGlobalHeapEnd
;
50 extern char *spxGlobalScratchPtr
, *spxGlobalScratchEnd
;
52 /* Make sure that all structures are aligned to largest type */
53 #define BLOCK_MASK (sizeof(long double)-1)
55 #define OVERRIDE_SPEEX_ALLOC
56 void *speex_alloc (int size
)
60 ptr
= (void *) (((int)spxGlobalHeapPtr
+ BLOCK_MASK
) & ~BLOCK_MASK
); //Start on 8 boundary
62 spxGlobalHeapPtr
= (char *)((int)ptr
+ size
); // Update pointer to next free location
64 if (spxGlobalHeapPtr
> spxGlobalHeapEnd
)
67 fprintf (stderr
, "insufficient space for persistent alloc request %d bytes\n", size
);
73 fprintf (stderr
, "Persist Allocated %d chars at %x, %d remaining\n", size
, ptr
, ((int)spxGlobalHeapEnd
- (int)spxGlobalHeapPtr
));
79 #define OVERRIDE_SPEEX_ALLOC_SCRATCH
80 void *speex_alloc_scratch (int size
)
84 ptr
= (void *) (((int)spxGlobalScratchPtr
+ BLOCK_MASK
) & ~BLOCK_MASK
); //Start on 8 boundary
86 spxGlobalScratchPtr
= (char *)((int)ptr
+ size
); // Update pointer to next free location
88 if (spxGlobalScratchPtr
> spxGlobalScratchEnd
)
91 fprintf (stderr
, "insufficient space for scratch alloc request %d bytes\n", size
);
97 fprintf (stderr
, "Scratch Allocated %d chars at %x, %d remaining\n", size
, ptr
, ((int)spxGlobalScratchEnd
- (int)spxGlobalScratchPtr
));
103 #define OVERRIDE_SPEEX_REALLOC
104 void *speex_realloc (void *ptr
, int size
)
107 speex_warning("realloc attempted, not allowed");
112 #define OVERRIDE_SPEEX_FREE
113 void speex_free (void *ptr
)
116 speex_warning("at speex_free");
119 #define OVERRIDE_SPEEX_FREE_SCRATCH
120 void speex_free_scratch (void *ptr
)
123 speex_warning("at speex_free_scratch");
127 #endif /* !MANUAL_ALLOC */