ArrayMemory.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 <stdlib.h>
  30. #include <string.h>
  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. memset((void*)ptr, 0, bufferLength);
  43. }
  44. // By default there is alignment and tail
  45. ArrayMemory::ArrayMemory(char* ptr, size_t bufferLength)
  46. {
  47. this->m_ptr=ptr;
  48. this->m_currentPtr=ptr;
  49. // Align on 64 bits per default
  50. this->alignSize = 8;
  51. this->tail=true;
  52. this->m_bufferLength = bufferLength;
  53. this->m_generation=0;
  54. this->memError=false;
  55. memset((void*)ptr, 0, bufferLength);
  56. }
  57. bool ArrayMemory::HasMemError()
  58. {
  59. return(this->memError);
  60. }
  61. size_t ArrayMemory::getTailSize()
  62. {
  63. if (this->tail)
  64. {
  65. return(16);
  66. }
  67. else
  68. {
  69. return(0);
  70. }
  71. }
  72. char *ArrayMemory::NewBuffer(size_t length)
  73. {
  74. if (length == 0)
  75. {
  76. return(NULL);
  77. }
  78. size_t tailSize = 0;
  79. // Add a tail of 16 bytes corresponding to the max number of lanes.
  80. tailSize = this->getTailSize();
  81. // Compute some offset to align the new buffer to be allocated
  82. if (this->alignSize > 0)
  83. {
  84. unsigned long offset;
  85. unsigned long pad;
  86. offset=(unsigned long)(this->m_currentPtr - this->m_ptr);
  87. pad = this->alignSize*ceil(1.0*offset / (1.0*this->alignSize)) - offset;
  88. //printf("new = %ld, old = %ld\n",pad,offset);
  89. this->m_currentPtr += pad;
  90. }
  91. // Return NULL is no more enough memory in array
  92. if (this->m_currentPtr + length + tailSize < this->m_ptr + m_bufferLength)
  93. {
  94. char *result=this->m_currentPtr;
  95. this->m_currentPtr += length + tailSize;
  96. return(result);
  97. }
  98. else
  99. {
  100. this->memError=true;
  101. return(NULL);
  102. }
  103. }
  104. bool ArrayMemory::IsTailEmpty(char *ptr, size_t length)
  105. {
  106. if ((ptr == NULL) || (length == 0))
  107. {
  108. return(true);
  109. }
  110. else
  111. {
  112. char *p=ptr + length;
  113. bool isEmpty=true;
  114. for(int i=0; i < this->getTailSize() ; i++)
  115. {
  116. //printf("%d\n",p[i]);
  117. if (p[i] != 0)
  118. {
  119. isEmpty = false;
  120. }
  121. }
  122. return(isEmpty);
  123. }
  124. }
  125. /** Reset memory
  126. The full C buffer is set to 0
  127. Current pointer is moved to start of buffer
  128. Memory generation is incremented (which is
  129. indirectly unvalidating all patterns.
  130. If the patterns are not reloaded after this, they'll return NULL
  131. when trying to access their pointer.
  132. )
  133. */
  134. void ArrayMemory::FreeMemory()
  135. {
  136. memset(this->m_ptr, 0, this->m_bufferLength);
  137. this->m_currentPtr=this->m_ptr;
  138. this->m_generation++;
  139. this->memError=false;
  140. }
  141. }