os_support.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. #ifdef HAVE_CONFIG_H
  33. #include "config.h"
  34. #endif
  35. #ifdef OS_SUPPORT_CUSTOM
  36. #include "os_support_custom.h"
  37. #endif
  38. /** Speex wrapper for calloc. To do your own dynamic allocation, all you need to do is replace this function, speex_realloc and speex_free
  39. NOTE: speex_alloc needs to CLEAR THE MEMORY */
  40. #ifndef OVERRIDE_SPEEX_ALLOC
  41. static inline void *speex_alloc (int size)
  42. {
  43. /* WARNING: this is not equivalent to malloc(). If you want to use malloc()
  44. or your own allocator, YOU NEED TO CLEAR THE MEMORY ALLOCATED. Otherwise
  45. you will experience strange bugs */
  46. return calloc(size,1);
  47. }
  48. #endif
  49. /** Same as speex_alloc, except that the area is only needed inside a Speex call (might cause problem with wideband though) */
  50. #ifndef OVERRIDE_SPEEX_ALLOC_SCRATCH
  51. static inline void *speex_alloc_scratch (int size)
  52. {
  53. /* Scratch space doesn't need to be cleared */
  54. return calloc(size,1);
  55. }
  56. #endif
  57. /** Speex wrapper for realloc. To do your own dynamic allocation, all you need to do is replace this function, speex_alloc and speex_free */
  58. #ifndef OVERRIDE_SPEEX_REALLOC
  59. static inline void *speex_realloc (void *ptr, int size)
  60. {
  61. return realloc(ptr, size);
  62. }
  63. #endif
  64. /** Speex wrapper for calloc. To do your own dynamic allocation, all you need to do is replace this function, speex_realloc and speex_alloc */
  65. #ifndef OVERRIDE_SPEEX_FREE
  66. static inline void speex_free (void *ptr)
  67. {
  68. free(ptr);
  69. }
  70. #endif
  71. /** Same as speex_free, except that the area is only needed inside a Speex call (might cause problem with wideband though) */
  72. #ifndef OVERRIDE_SPEEX_FREE_SCRATCH
  73. static inline void speex_free_scratch (void *ptr)
  74. {
  75. free(ptr);
  76. }
  77. #endif
  78. /** Copy n elements from src to dst. The 0* term provides compile-time type checking */
  79. #ifndef OVERRIDE_SPEEX_COPY
  80. #define SPEEX_COPY(dst, src, n) (memcpy((dst), (src), (n)*sizeof(*(dst)) + 0*((dst)-(src)) ))
  81. #endif
  82. /** Copy n elements from src to dst, allowing overlapping regions. The 0* term
  83. provides compile-time type checking */
  84. #ifndef OVERRIDE_SPEEX_MOVE
  85. #define SPEEX_MOVE(dst, src, n) (memmove((dst), (src), (n)*sizeof(*(dst)) + 0*((dst)-(src)) ))
  86. #endif
  87. /** For n elements worth of memory, set every byte to the value of c, starting at address dst */
  88. #ifndef OVERRIDE_SPEEX_MEMSET
  89. #define SPEEX_MEMSET(dst, c, n) (memset((dst), (c), (n)*sizeof(*(dst))))
  90. #endif
  91. #ifndef OVERRIDE_SPEEX_FATAL
  92. static inline void _speex_fatal(const char *str, const char *file, int line)
  93. {
  94. fprintf (stderr, "Fatal (internal) error in %s, line %d: %s\n", file, line, str);
  95. exit(1);
  96. }
  97. #endif
  98. #ifndef OVERRIDE_SPEEX_WARNING
  99. static inline void speex_warning(const char *str)
  100. {
  101. #ifndef DISABLE_WARNINGS
  102. fprintf (stderr, "warning: %s\n", str);
  103. #endif
  104. }
  105. #endif
  106. #ifndef OVERRIDE_SPEEX_WARNING_INT
  107. static inline void speex_warning_int(const char *str, int val)
  108. {
  109. #ifndef DISABLE_WARNINGS
  110. fprintf (stderr, "warning: %s %d\n", str, val);
  111. #endif
  112. }
  113. #endif
  114. #ifndef OVERRIDE_SPEEX_NOTIFY
  115. static inline void speex_notify(const char *str)
  116. {
  117. #ifndef DISABLE_NOTIFICATIONS
  118. fprintf (stderr, "notification: %s\n", str);
  119. #endif
  120. }
  121. #endif
  122. #ifndef OVERRIDE_SPEEX_PUTC
  123. /** Speex wrapper for putc */
  124. static inline void _speex_putc(int ch, void *file)
  125. {
  126. FILE *f = (FILE *)file;
  127. fprintf(f, "%c", ch);
  128. }
  129. #endif
  130. #define speex_fatal(str) _speex_fatal(str, __FILE__, __LINE__);
  131. #define speex_assert(cond) {if (!(cond)) {speex_fatal("assertion failed: " #cond);}}
  132. #ifndef RELEASE
  133. static inline void print_vec(float *vec, int len, char *name)
  134. {
  135. int i;
  136. printf ("%s ", name);
  137. for (i=0;i<len;i++)
  138. printf (" %f", vec[i]);
  139. printf ("\n");
  140. }
  141. #endif
  142. #endif