You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
4.5KB

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