memory.c
上传用户:wstnjxml
上传日期:2014-04-03
资源大小:7248k
文件大小:6k
源码类别:

Windows CE

开发平台:

C/C++

  1. /* libFLAC - Free Lossless Audio Codec library
  2.  * Copyright (C) 2001,2002,2003,2004,2005  Josh Coalson
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  *
  8.  * - Redistributions of source code must retain the above copyright
  9.  * notice, this list of conditions and the following disclaimer.
  10.  *
  11.  * - Redistributions in binary form must reproduce the above copyright
  12.  * notice, this list of conditions and the following disclaimer in the
  13.  * documentation and/or other materials provided with the distribution.
  14.  *
  15.  * - Neither the name of the Xiph.org Foundation nor the names of its
  16.  * contributors may be used to endorse or promote products derived from
  17.  * this software without specific prior written permission.
  18.  *
  19.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20.  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22.  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
  23.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26.  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27.  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28.  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30.  */
  31. #include "private/memory.h"
  32. #include "FLAC/assert.h"
  33. #ifdef HAVE_CONFIG_H
  34. #include <config.h>
  35. #endif
  36. void *FLAC__memory_alloc_aligned(size_t bytes, void **aligned_address)
  37. {
  38. void *x;
  39. FLAC__ASSERT(0 != aligned_address);
  40. #ifdef FLAC__ALIGN_MALLOC_DATA
  41. /* align on 32-byte (256-bit) boundary */
  42. x = malloc(bytes+31);
  43. *aligned_address = (void*)(((unsigned)x + 31) & -32);
  44. #else
  45. x = malloc(bytes);
  46. *aligned_address = x;
  47. #endif
  48. return x;
  49. }
  50. FLAC__bool FLAC__memory_alloc_aligned_int32_array(unsigned elements, FLAC__int32 **unaligned_pointer, FLAC__int32 **aligned_pointer)
  51. {
  52. FLAC__int32 *pu; /* unaligned pointer */
  53. union { /* union needed to comply with C99 pointer aliasing rules */
  54. FLAC__int32 *pa; /* aligned pointer */
  55. void        *pv; /* aligned pointer alias */
  56. } u;
  57. FLAC__ASSERT(elements > 0);
  58. FLAC__ASSERT(0 != unaligned_pointer);
  59. FLAC__ASSERT(0 != aligned_pointer);
  60. FLAC__ASSERT(unaligned_pointer != aligned_pointer);
  61. pu = (FLAC__int32*)FLAC__memory_alloc_aligned(sizeof(FLAC__int32) * elements, &u.pv);
  62. if(0 == pu) {
  63. return false;
  64. }
  65. else {
  66. if(*unaligned_pointer != 0)
  67. free(*unaligned_pointer);
  68. *unaligned_pointer = pu;
  69. *aligned_pointer = u.pa;
  70. return true;
  71. }
  72. }
  73. FLAC__bool FLAC__memory_alloc_aligned_uint32_array(unsigned elements, FLAC__uint32 **unaligned_pointer, FLAC__uint32 **aligned_pointer)
  74. {
  75. FLAC__uint32 *pu; /* unaligned pointer */
  76. union { /* union needed to comply with C99 pointer aliasing rules */
  77. FLAC__uint32 *pa; /* aligned pointer */
  78. void         *pv; /* aligned pointer alias */
  79. } u;
  80. FLAC__ASSERT(elements > 0);
  81. FLAC__ASSERT(0 != unaligned_pointer);
  82. FLAC__ASSERT(0 != aligned_pointer);
  83. FLAC__ASSERT(unaligned_pointer != aligned_pointer);
  84. pu = (FLAC__uint32*)FLAC__memory_alloc_aligned(sizeof(FLAC__uint32) * elements, &u.pv);
  85. if(0 == pu) {
  86. return false;
  87. }
  88. else {
  89. if(*unaligned_pointer != 0)
  90. free(*unaligned_pointer);
  91. *unaligned_pointer = pu;
  92. *aligned_pointer = u.pa;
  93. return true;
  94. }
  95. }
  96. FLAC__bool FLAC__memory_alloc_aligned_uint64_array(unsigned elements, FLAC__uint64 **unaligned_pointer, FLAC__uint64 **aligned_pointer)
  97. {
  98. FLAC__uint64 *pu; /* unaligned pointer */
  99. union { /* union needed to comply with C99 pointer aliasing rules */
  100. FLAC__uint64 *pa; /* aligned pointer */
  101. void         *pv; /* aligned pointer alias */
  102. } u;
  103. FLAC__ASSERT(elements > 0);
  104. FLAC__ASSERT(0 != unaligned_pointer);
  105. FLAC__ASSERT(0 != aligned_pointer);
  106. FLAC__ASSERT(unaligned_pointer != aligned_pointer);
  107. pu = (FLAC__uint64*)FLAC__memory_alloc_aligned(sizeof(FLAC__uint64) * elements, &u.pv);
  108. if(0 == pu) {
  109. return false;
  110. }
  111. else {
  112. if(*unaligned_pointer != 0)
  113. free(*unaligned_pointer);
  114. *unaligned_pointer = pu;
  115. *aligned_pointer = u.pa;
  116. return true;
  117. }
  118. }
  119. FLAC__bool FLAC__memory_alloc_aligned_unsigned_array(unsigned elements, unsigned **unaligned_pointer, unsigned **aligned_pointer)
  120. {
  121. unsigned *pu; /* unaligned pointer */
  122. union { /* union needed to comply with C99 pointer aliasing rules */
  123. unsigned *pa; /* aligned pointer */
  124. void     *pv; /* aligned pointer alias */
  125. } u;
  126. FLAC__ASSERT(elements > 0);
  127. FLAC__ASSERT(0 != unaligned_pointer);
  128. FLAC__ASSERT(0 != aligned_pointer);
  129. FLAC__ASSERT(unaligned_pointer != aligned_pointer);
  130. pu = (unsigned*)FLAC__memory_alloc_aligned(sizeof(unsigned) * elements, &u.pv);
  131. if(0 == pu) {
  132. return false;
  133. }
  134. else {
  135. if(*unaligned_pointer != 0)
  136. free(*unaligned_pointer);
  137. *unaligned_pointer = pu;
  138. *aligned_pointer = u.pa;
  139. return true;
  140. }
  141. }
  142. #ifndef FLAC__INTEGER_ONLY_LIBRARY
  143. FLAC__bool FLAC__memory_alloc_aligned_real_array(unsigned elements, FLAC__real **unaligned_pointer, FLAC__real **aligned_pointer)
  144. {
  145. FLAC__real *pu; /* unaligned pointer */
  146. union { /* union needed to comply with C99 pointer aliasing rules */
  147. FLAC__real *pa; /* aligned pointer */
  148. void       *pv; /* aligned pointer alias */
  149. } u;
  150. FLAC__ASSERT(elements > 0);
  151. FLAC__ASSERT(0 != unaligned_pointer);
  152. FLAC__ASSERT(0 != aligned_pointer);
  153. FLAC__ASSERT(unaligned_pointer != aligned_pointer);
  154. pu = (FLAC__real*)FLAC__memory_alloc_aligned(sizeof(FLAC__real) * elements, &u.pv);
  155. if(0 == pu) {
  156. return false;
  157. }
  158. else {
  159. if(*unaligned_pointer != 0)
  160. free(*unaligned_pointer);
  161. *unaligned_pointer = pu;
  162. *aligned_pointer = u.pa;
  163. return true;
  164. }
  165. }
  166. #endif