cmem_pool.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /****************************************************************************
  2. *
  3. * Copyright (c) 2017, Michael Becker (michael.f.becker@gmail.com)
  4. *
  5. * This file is part of the FreeRTOS Add-ons project.
  6. *
  7. * Source Code:
  8. * https://github.com/michaelbecker/freertos-addons
  9. *
  10. * Project Page:
  11. * http://michaelbecker.github.io/freertos-addons/
  12. *
  13. * On-line Documentation:
  14. * http://michaelbecker.github.io/freertos-addons/docs/html/index.html
  15. *
  16. * Permission is hereby granted, free of charge, to any person obtaining a
  17. * copy of this software and associated documentation files
  18. * (the "Software"), to deal in the Software without restriction, including
  19. * without limitation the rights to use, copy, modify, merge, publish,
  20. * distribute, sublicense, and/or sell copies of the Software, and to
  21. * permit persons to whom the Software is furnished to do so,subject to the
  22. * following conditions:
  23. *
  24. * + The above copyright notice and this permission notice shall be included
  25. * in all copies or substantial portions of the Software.
  26. * + Credit is appreciated, but not required, if you find this project
  27. * useful enough to include in your application, product, device, etc.
  28. *
  29. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  30. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  31. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  32. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  33. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  34. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  35. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  36. *
  37. ***************************************************************************/
  38. #include <stdlib.h>
  39. #include "mem_pool.hpp"
  40. using namespace cpp_freertos;
  41. void MemoryPool::CalculateValidAlignment()
  42. {
  43. /**
  44. * Guarantee that the alignment is the size of a pointer.
  45. */
  46. if (Alignment < (int)sizeof(unsigned char *)) {
  47. Alignment = (int)sizeof(unsigned char *);
  48. }
  49. int alignmentBit = 0x1;
  50. int i;
  51. for (i = 0; i < 31; i++) {
  52. if (Alignment == alignmentBit) {
  53. break;
  54. }
  55. alignmentBit <<= 1;
  56. }
  57. if (i >= 31) {
  58. #ifndef CPP_FREERTOS_NO_EXCEPTIONS
  59. throw MemoryPoolBadAlignmentException();
  60. #else
  61. configASSERT(!"MemoryPool Bad Alignment");
  62. #endif
  63. }
  64. }
  65. void MemoryPool::CalculateItemSize()
  66. {
  67. if (ItemSize <= Alignment) {
  68. ItemSize = Alignment;
  69. }
  70. else {
  71. int alignmentCount = ItemSize / Alignment;
  72. if (ItemSize % Alignment != 0) {
  73. alignmentCount++;
  74. }
  75. ItemSize = alignmentCount * Alignment;
  76. }
  77. }
  78. MemoryPool::MemoryPool( int itemSize,
  79. int itemCount,
  80. int alignment)
  81. : ItemSize(itemSize),
  82. Alignment(alignment)
  83. {
  84. CalculateValidAlignment();
  85. CalculateItemSize();
  86. unsigned char *address = (unsigned char *)malloc(ItemSize * itemCount);
  87. if (address == NULL) {
  88. #ifndef CPP_FREERTOS_NO_EXCEPTIONS
  89. throw MemoryPoolMallocException();
  90. #else
  91. configASSERT(!"MemoryPool malloc Failed");
  92. #endif
  93. }
  94. for (int i = 0; i < itemCount; i++) {
  95. FreeItems.push_back(address);
  96. address += ItemSize;
  97. }
  98. Lock = new MutexStandard();
  99. }
  100. MemoryPool::MemoryPool( int itemSize,
  101. void *preallocatedMemory,
  102. int preallocatedMemorySize,
  103. int alignment)
  104. : ItemSize(itemSize),
  105. Alignment(alignment)
  106. {
  107. CalculateValidAlignment();
  108. CalculateItemSize();
  109. unsigned char *address = (unsigned char *)preallocatedMemory;
  110. while (preallocatedMemorySize >= ItemSize) {
  111. FreeItems.push_back(address);
  112. address += ItemSize;
  113. preallocatedMemorySize -= ItemSize;
  114. }
  115. Lock = new MutexStandard();
  116. }
  117. void *MemoryPool::Allocate()
  118. {
  119. LockGuard guard(*Lock);
  120. if (FreeItems.empty())
  121. return NULL;
  122. void *item = FreeItems.front();
  123. FreeItems.pop_front();
  124. return item;
  125. }
  126. void MemoryPool::Free(void *item)
  127. {
  128. LockGuard guard(*Lock);
  129. FreeItems.push_back(item);
  130. }
  131. void MemoryPool::AddMemory(int itemCount)
  132. {
  133. unsigned char *address = (unsigned char *)malloc(ItemSize * itemCount);
  134. if (address == NULL) {
  135. #ifndef CPP_FREERTOS_NO_EXCEPTIONS
  136. throw MemoryPoolMallocException();
  137. #else
  138. configASSERT(!"MemoryPool AddMemory Failed");
  139. #endif
  140. }
  141. for (int i = 0; i < itemCount; i++) {
  142. LockGuard guard(*Lock);
  143. FreeItems.push_back(address);
  144. address += ItemSize;
  145. }
  146. }
  147. void MemoryPool::AddMemory( void *preallocatedMemory,
  148. int preallocatedMemorySize)
  149. {
  150. unsigned char *address = (unsigned char *)preallocatedMemory;
  151. while (preallocatedMemorySize >= ItemSize) {
  152. LockGuard guard(*Lock);
  153. FreeItems.push_back(address);
  154. address += ItemSize;
  155. preallocatedMemorySize -= ItemSize;
  156. }
  157. }