mempool.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include "ycpool.h"
  2. #include <stdlib.h>
  3. error_t MP_Init(mp_t mp,
  4. void *start,
  5. uint8_t size,
  6. uint16_t block_size)
  7. {
  8. uint8_t *block_ptr = NULL;
  9. uint8_t offset = 0;
  10. _ASSERT(mp!=NULL);
  11. /* here init obj*/
  12. mp->start_address = start;
  13. size = _ALIGN_UP(size, _ALIGN_SIZE);
  14. block_size = _ALIGN_UP(block_size, _ALIGN_SIZE);
  15. /* align to align size byte */
  16. mp->size = size;
  17. mp->block_size = block_size;
  18. /* align to align size byte */
  19. mp->block_total_count = mp->size / (mp->block_size + sizeof(uint8_t *));
  20. mp->block_free_count = mp->block_total_count;
  21. /* initialize free block list */
  22. block_ptr = (uint8_t *)mp->start_address;
  23. for (offset = 0; offset < mp->block_total_count; offset ++) {
  24. *(uint8_t **)(block_ptr + offset * (block_size + sizeof(uint8_t *))) =
  25. (uint8_t *)(block_ptr + (offset + 1) * (block_size + sizeof(uint8_t *)));
  26. }
  27. *(uint8_t **)(block_ptr + (offset - 1) * (block_size + sizeof(uint8_t *))) = NULL;
  28. mp->block_list = block_ptr;
  29. return 0;
  30. }
  31. error_t MP_Create(mp_t mp,
  32. uint8_t block_count, uint16_t block_size)
  33. {
  34. uint8_t *block_ptr = NULL;
  35. uint8_t offset = 0;
  36. _ASSERT(mp!=NULL);
  37. /* here init obj*/
  38. block_size = _ALIGN_UP(block_size, _ALIGN_SIZE);
  39. mp->size = block_size;
  40. mp->block_size = block_count * (block_size + sizeof(uint8_t *));
  41. mp->start_address = malloc(mp->block_size);
  42. _ASSERT(mp->start_address!=NULL);
  43. /* align to align size byte */
  44. mp->block_total_count = block_count;
  45. mp->block_free_count = block_count;
  46. /* initialize free block list */
  47. block_ptr = (uint8_t *)mp->start_address;
  48. for (offset = 0; offset < mp->block_total_count; offset ++) {
  49. *(uint8_t **)(block_ptr + offset * (block_size + sizeof(uint8_t *))) =
  50. (uint8_t *)(block_ptr + (offset + 1) * (block_size + sizeof(uint8_t *)));
  51. }
  52. *(uint8_t **)(block_ptr + (offset - 1) * (block_size + sizeof(uint8_t *))) = NULL;
  53. mp->block_list = block_ptr;
  54. return 0;
  55. }
  56. void *MP_Alloc(mp_t mp)
  57. {
  58. uint8_t *block_ptr;
  59. register uint32_t level;
  60. if (mp->block_free_count == 0){
  61. return NULL;
  62. }
  63. /* memory block is available. decrease the free block counter */
  64. mp->block_free_count--;
  65. /* get block from block list */
  66. block_ptr = mp->block_list;
  67. _ASSERT(block_ptr != NULL);
  68. /* Setup the next free node. */
  69. mp->block_list = *(uint8_t **)block_ptr;
  70. /* point to memory pool */
  71. *(uint8_t **)block_ptr = (uint8_t *)mp;
  72. return (uint8_t *)(block_ptr + sizeof(uint8_t *));
  73. }
  74. void MP_Free(void *block)
  75. {
  76. uint8_t **block_ptr;
  77. struct mempool *mp;
  78. /* get the control block of pool which the block belongs to */
  79. block_ptr = (uint8_t **)((uint8_t *)block - sizeof(uint8_t *));
  80. mp = (struct mempool *)*block_ptr;
  81. /* increase the free block count */
  82. mp->block_free_count ++;
  83. /* link the block into the block list */
  84. *block_ptr = mp->block_list;
  85. mp->block_list = (uint8_t *)block_ptr;
  86. }