os_support.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* Copyright (C) 2007 Jean-Marc Valin
  2. File: os_support.h
  3. This is the (tiny) OS abstraction layer. Aside from math.h, this is the
  4. only place where system headers are allowed.
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions are
  7. met:
  8. 1. Redistributions of source code must retain the above copyright notice,
  9. this list of conditions and the following disclaimer.
  10. 2. 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. 3. The name of the author may not be used to endorse or promote products
  14. derived from this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  16. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  19. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  23. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  24. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef OS_SUPPORT_H
  28. #define OS_SUPPORT_H
  29. #include <string.h>
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include "config.h"
  33. #ifdef OS_SUPPORT_CUSTOM
  34. #include "os_support_custom.h"
  35. #endif
  36. #include "speex_bind.h"
  37. /** Speex wrapper for calloc. To do your own dynamic allocation, all you need to do is replace this function, speex_realloc and speex_free
  38. NOTE: speex_alloc needs to CLEAR THE MEMORY */
  39. #ifndef OVERRIDE_SPEEX_ALLOC
  40. static SPEEX_INLINE void *speex_alloc (int size)
  41. {
  42. /* WARNING: this is not equivalent to malloc(). If you want to use malloc()
  43. or your own allocator, YOU NEED TO CLEAR THE MEMORY ALLOCATED. Otherwise
  44. you will experience strange bugs */
  45. return calloc(size,1);
  46. }
  47. #endif
  48. /** Same as speex_alloc, except that the area is only needed inside a Speex call (might cause problem with wideband though) */
  49. #ifndef OVERRIDE_SPEEX_ALLOC_SCRATCH
  50. static SPEEX_INLINE void *speex_alloc_scratch (int size)
  51. {
  52. /* Scratch space doesn't need to be cleared */
  53. return calloc(size,1);
  54. }
  55. #endif
  56. /** Speex wrapper for realloc. To do your own dynamic allocation, all you need to do is replace this function, speex_alloc and speex_free */
  57. #ifndef OVERRIDE_SPEEX_REALLOC
  58. static SPEEX_INLINE void *speex_realloc (void *ptr, int size)
  59. {
  60. return realloc(ptr, size);
  61. }
  62. #endif
  63. /** Speex wrapper for calloc. To do your own dynamic allocation, all you need to do is replace this function, speex_realloc and speex_alloc */
  64. #ifndef OVERRIDE_SPEEX_FREE
  65. static SPEEX_INLINE void speex_free (void *ptr)
  66. {
  67. free(ptr);
  68. }
  69. #endif
  70. /** Same as speex_free, except that the area is only needed inside a Speex call (might cause problem with wideband though) */
  71. #ifndef OVERRIDE_SPEEX_FREE_SCRATCH
  72. static SPEEX_INLINE void speex_free_scratch (void *ptr)
  73. {
  74. free(ptr);
  75. }
  76. #endif
  77. /** Copy n bytes of memory from src to dst. The 0* term provides compile-time type checking */
  78. #ifndef OVERRIDE_SPEEX_COPY
  79. #define SPEEX_COPY(dst, src, n) (memcpy((dst), (src), (n)*sizeof(*(dst)) + 0*((dst)-(src)) ))
  80. #endif
  81. /** Copy n bytes of memory from src to dst, allowing overlapping regions. The 0* term
  82. provides compile-time type checking */
  83. #ifndef OVERRIDE_SPEEX_MOVE
  84. #define SPEEX_MOVE(dst, src, n) (memmove((dst), (src), (n)*sizeof(*(dst)) + 0*((dst)-(src)) ))
  85. #endif
  86. /** Set n bytes of memory to value of c, starting at address s */
  87. #ifndef OVERRIDE_SPEEX_MEMSET
  88. #define SPEEX_MEMSET(dst, c, n) (memset((dst), (c), (n)*sizeof(*(dst))))
  89. #endif
  90. #ifndef OVERRIDE_SPEEX_FATAL
  91. static SPEEX_INLINE void _speex_fatal(const char *str, const char *file, int line)
  92. {
  93. fprintf (stderr, "Fatal (internal) error in %s, line %d: %s\n", file, line, str);
  94. exit(1);
  95. }
  96. #endif
  97. #ifndef OVERRIDE_SPEEX_WARNING
  98. static SPEEX_INLINE void speex_warning(const char *str)
  99. {
  100. #ifndef DISABLE_WARNINGS
  101. fprintf (stderr, "warning: %s\n", str);
  102. #endif
  103. }
  104. #endif
  105. #ifndef OVERRIDE_SPEEX_WARNING_INT
  106. static SPEEX_INLINE void speex_warning_int(const char *str, int val)
  107. {
  108. #ifndef DISABLE_WARNINGS
  109. fprintf (stderr, "warning: %s %d\n", str, val);
  110. #endif
  111. }
  112. #endif
  113. #ifndef OVERRIDE_SPEEX_NOTIFY
  114. static SPEEX_INLINE void speex_notify(const char *str)
  115. {
  116. #ifndef DISABLE_NOTIFICATIONS
  117. fprintf (stderr, "notification: %s\n", str);
  118. #endif
  119. }
  120. #endif
  121. #ifndef OVERRIDE_SPEEX_PUTC
  122. /** Speex wrapper for putc */
  123. static SPEEX_INLINE void _speex_putc(int ch, void *file)
  124. {
  125. FILE *f = (FILE *)file;
  126. fprintf(f, "%c", ch);
  127. }
  128. #endif
  129. #define speex_fatal(str) _speex_fatal(str, __FILE__, __LINE__);
  130. #define speex_assert(cond) {if (!(cond)) {speex_fatal("assertion failed: " #cond);}}
  131. #ifndef RELEASE
  132. static SPEEX_INLINE void print_vec(float *vec, int len, char *name)
  133. {
  134. int i;
  135. printf ("%s ", name);
  136. for (i=0;i<len;i++)
  137. printf (" %f", vec[i]);
  138. printf ("\n");
  139. }
  140. #endif
  141. #endif