ArrayMemory.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: ArrayMemory.cpp
  4. * Description: Array Memory Manager
  5. *
  6. * $Date: 20. June 2019
  7. * $Revision: V1.0.0
  8. *
  9. * Target Processor: Cortex-M cores
  10. * -------------------------------------------------------------------- */
  11. /*
  12. * Copyright (C) 2010-2019 ARM Limited or its affiliates. All rights reserved.
  13. *
  14. * SPDX-License-Identifier: Apache-2.0
  15. *
  16. * Licensed under the Apache License, Version 2.0 (the License); you may
  17. * not use this file except in compliance with the License.
  18. * You may obtain a copy of the License at
  19. *
  20. * www.apache.org/licenses/LICENSE-2.0
  21. *
  22. * Unless required by applicable law or agreed to in writing, software
  23. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  24. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  25. * See the License for the specific language governing permissions and
  26. * limitations under the License.
  27. */
  28. #include "ArrayMemory.h"
  29. #include <cstdlib>
  30. #include <cstring>
  31. #include <math.h>
  32. namespace Client {
  33. ArrayMemory::ArrayMemory(char* ptr, size_t bufferLength,int aligned, bool tail)
  34. {
  35. this->m_ptr=ptr;
  36. this->m_currentPtr=ptr;
  37. this->alignSize = aligned;
  38. this->tail=tail;
  39. this->m_bufferLength = bufferLength;
  40. this->m_generation=0;
  41. this->memError=false;
  42. #if !defined(BENCHMARK)
  43. memset((void*)ptr, 0, bufferLength);
  44. #endif
  45. }
  46. // By default there is alignment and tail
  47. ArrayMemory::ArrayMemory(char* ptr, size_t bufferLength)
  48. {
  49. this->m_ptr=ptr;
  50. this->m_currentPtr=ptr;
  51. // Align on 64 bits per default
  52. this->alignSize = 8;
  53. this->tail=true;
  54. this->m_bufferLength = bufferLength;
  55. this->m_generation=0;
  56. this->memError=false;
  57. #if !defined(BENCHMARK)
  58. memset((void*)ptr, 0, bufferLength);
  59. #endif
  60. }
  61. bool ArrayMemory::HasMemError()
  62. {
  63. return(this->memError);
  64. }
  65. size_t ArrayMemory::getTailSize()
  66. {
  67. if (this->tail)
  68. {
  69. return(16);
  70. }
  71. else
  72. {
  73. return(0);
  74. }
  75. }
  76. char *ArrayMemory::NewBuffer(size_t length)
  77. {
  78. if (length == 0)
  79. {
  80. return(NULL);
  81. }
  82. size_t tailSize = 0;
  83. // Add a tail of 16 bytes corresponding to the max number of lanes.
  84. tailSize = this->getTailSize();
  85. // Compute some offset to align the new buffer to be allocated
  86. if (this->alignSize > 0)
  87. {
  88. unsigned long offset;
  89. unsigned long pad;
  90. offset=(unsigned long)(this->m_currentPtr - this->m_ptr);
  91. pad = this->alignSize*ceil(1.0*offset / (1.0*this->alignSize)) - offset;
  92. //printf("new = %ld, old = %ld\n",pad,offset);
  93. this->m_currentPtr += pad;
  94. }
  95. // Return NULL is no more enough memory in array
  96. if (this->m_currentPtr + length + tailSize < this->m_ptr + m_bufferLength)
  97. {
  98. char *result=this->m_currentPtr;
  99. this->m_currentPtr += length + tailSize;
  100. return(result);
  101. }
  102. else
  103. {
  104. this->memError=true;
  105. return(NULL);
  106. }
  107. }
  108. bool ArrayMemory::IsTailEmpty(char *ptr, size_t length)
  109. {
  110. if ((ptr == NULL) || (length == 0))
  111. {
  112. return(true);
  113. }
  114. else
  115. {
  116. char *p=ptr + length;
  117. bool isEmpty=true;
  118. for(unsigned long i=0; i < this->getTailSize() ; i++)
  119. {
  120. //printf("%d\n",p[i]);
  121. if (p[i] != 0)
  122. {
  123. isEmpty = false;
  124. }
  125. }
  126. return(isEmpty);
  127. }
  128. }
  129. /** Reset memory
  130. The full C buffer is set to 0
  131. Current pointer is moved to start of buffer
  132. Memory generation is incremented (which is
  133. indirectly unvalidating all patterns.
  134. If the patterns are not reloaded after this, they'll return NULL
  135. when trying to access their pointer.
  136. )
  137. */
  138. void ArrayMemory::FreeMemory()
  139. {
  140. #if !defined(BENCHMARK)
  141. /*
  142. In benchmark mode, memory is not clearer between
  143. tests. It is faster when running on cycle model or RTL.
  144. In benchmark mode, we don't tests so having a memory not
  145. in a clean state is not a problem.
  146. */
  147. memset(this->m_ptr, 0, this->m_bufferLength);
  148. #endif
  149. this->m_currentPtr=this->m_ptr;
  150. this->m_generation++;
  151. this->memError=false;
  152. }
  153. }