Swap code/variable declaration to be pre-C99 compliant.
[xiph/unicode.git] / speex / ti / user_misc.h
blob9e2a4f5f021b00fc5e55aaf9ce51a3a92f509414
1 /* Copyright (C) 2005 Psi Systems, Inc.
2 Author: Jean-Marc Valin
3 File: user_misc.h
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
8 are met:
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.
34 #ifdef MANUAL_ALLOC
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)
58 void *ptr;
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 )
66 #ifdef VERBOSE_ALLOC
67 fprintf (stderr, "insufficient space for persistent alloc request %d bytes\n", size);
68 #endif
69 return 0;
72 #ifdef VERBOSE_ALLOC
73 fprintf (stderr, "Persist Allocated %d chars at %x, %d remaining\n", size, ptr, ((int)spxGlobalHeapEnd - (int)spxGlobalHeapPtr));
74 #endif
75 memset(ptr, 0, size);
76 return ptr;
79 #define OVERRIDE_SPEEX_ALLOC_SCRATCH
80 void *speex_alloc_scratch (int size)
82 void *ptr;
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 )
90 #ifdef VERBOSE_ALLOC
91 fprintf (stderr, "insufficient space for scratch alloc request %d bytes\n", size);
92 #endif
93 return 0;
96 #ifdef VERBOSE_ALLOC
97 fprintf (stderr, "Scratch Allocated %d chars at %x, %d remaining\n", size, ptr, ((int)spxGlobalScratchEnd - (int)spxGlobalScratchPtr));
98 #endif
99 memset(ptr, 0, size);
100 return ptr;
103 #define OVERRIDE_SPEEX_REALLOC
104 void *speex_realloc (void *ptr, int size)
106 #ifdef VERBOSE_ALLOC
107 speex_warning("realloc attempted, not allowed");
108 #endif
109 return 0;
112 #define OVERRIDE_SPEEX_FREE
113 void speex_free (void *ptr)
115 #ifdef VERBOSE_ALLOC
116 speex_warning("at speex_free");
117 #endif
119 #define OVERRIDE_SPEEX_FREE_SCRATCH
120 void speex_free_scratch (void *ptr)
122 #ifdef VERBOSE_ALLOC
123 speex_warning("at speex_free_scratch");
124 #endif
127 #endif /* !MANUAL_ALLOC */