membag.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /**
  2. * \file
  3. *
  4. * \brief Memory Bag allocator for 8-bit AVR, 32-bit AVR, SAM
  5. *
  6. * Copyright (c) 2014-2015 Atmel Corporation. All rights reserved.
  7. *
  8. * \asf_license_start
  9. *
  10. * \page License
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. *
  18. * 2. Redistributions in binary form must reproduce the above copyright notice,
  19. * this list of conditions and the following disclaimer in the documentation
  20. * and/or other materials provided with the distribution.
  21. *
  22. * 3. The name of Atmel may not be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * 4. This software may only be redistributed and used in connection with an
  26. * Atmel microcontroller product.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  29. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  30. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  31. * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
  32. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  34. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  36. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  37. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. * \asf_license_stop
  41. *
  42. */
  43. /*
  44. * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
  45. */
  46. #ifndef UTILS_MEMBAG_H
  47. #define UTILS_MEMBAG_H
  48. #include <stddef.h>
  49. /**
  50. * \defgroup membag_group Memory Bag Allocator
  51. *
  52. * The Membag allocator is a optimized, fragmentationless general purpose
  53. * memory allocator utility module designed to replace the standard C library
  54. * \c malloc() and \c free() functions in resource constrained environments.
  55. *
  56. *
  57. * Internally, the Membag allocator uses several user defined "bags" of one or
  58. * more fixed-size blocks to form a memory pool for use in a user application.
  59. * When an allocation is requested, the Membag module will search all available
  60. * bags and find the smallest unallocated block of sufficient size, and return
  61. * this block to the calling function. The size of each bag and number of blocks
  62. * in each bag is user configurable, via the \ref conf_membag.h header file
  63. * added to the user project.
  64. *
  65. * The allocator also has basic statistics functionality to obtain the size of
  66. * the entire memory pool, amount of free memory, and the size of the smallest
  67. * and largest free memory block.
  68. *
  69. * The memory bag allocator always allocates memory as a block from a fixed size
  70. * bag/pool. This helps reduce memory fragmentation compared to an generic
  71. * allocator that gives exactly the bytes requested. While this gives a
  72. * trade-off of the maximum number of concurrent allocations and the size of
  73. * allocations that are allowable, the allocator prevents memory fragmentation
  74. * from occuring in an embedded application.
  75. *
  76. * The allocation and deallocation of memory with the Membag module is
  77. * non-deterministic, however the module functions all have a maximum run time
  78. * that is dependent on the number of bags that have been configured.
  79. *
  80. * @{
  81. */
  82. /**
  83. * Macro used to create memory bags in conf_membag.h
  84. *
  85. * \note Multiple bags of the same size are allowed, if more than 32 bags of a
  86. * given size are required in an application.
  87. *
  88. * \param objsize Size of each block in the bag
  89. * \param nr_objs Number of blocks in the bag, a value less than 32
  90. */
  91. #define MEMBAG(objsize, nr_objs)\
  92. { .block_size = objsize, .num_blocks = nr_objs }
  93. /**
  94. * Macro used to store the size of the membags in conf_membag.h
  95. *
  96. * \note Multiple bags of the same size are allowed, if more than 32 bags of a
  97. * given size are required in an application.
  98. *
  99. * \param objsize Size of each block in the bag
  100. * \param nr_objs Number of blocks in the bag, a value less than 32
  101. */
  102. #define MEMBAG_SIZE(objsize, nr_objs)\
  103. (objsize * nr_objs)
  104. void membag_init(void);
  105. size_t membag_get_total(void);
  106. size_t membag_get_total_free(void);
  107. size_t membag_get_smallest_free_block_size(void);
  108. size_t membag_get_largest_free_block_size(void);
  109. void *membag_alloc(const size_t size);
  110. void membag_free(const void *ptr);
  111. /** @} */
  112. #endif /* UTILS_MEMBAG_H */