tlsf.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #ifndef INCLUDED_tlsf
  2. #define INCLUDED_tlsf
  3. /*
  4. ** Two Level Segregated Fit memory allocator, version 3.1.
  5. ** Written by Matthew Conte
  6. ** http://tlsf.baisoku.org
  7. **
  8. ** Based on the original documentation by Miguel Masmano:
  9. ** http://www.gii.upv.es/tlsf/main/docs
  10. **
  11. ** This implementation was written to the specification
  12. ** of the document, therefore no GPL restrictions apply.
  13. **
  14. ** Copyright (c) 2006-2016, Matthew Conte
  15. ** All rights reserved.
  16. **
  17. ** Redistribution and use in source and binary forms, with or without
  18. ** modification, are permitted provided that the following conditions are met:
  19. ** * Redistributions of source code must retain the above copyright
  20. ** notice, this list of conditions and the following disclaimer.
  21. ** * Redistributions in binary form must reproduce the above copyright
  22. ** notice, this list of conditions and the following disclaimer in the
  23. ** documentation and/or other materials provided with the distribution.
  24. ** * Neither the name of the copyright holder nor the
  25. ** names of its contributors may be used to endorse or promote products
  26. ** derived from this software without specific prior written permission.
  27. **
  28. ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  29. ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  30. ** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. ** DISCLAIMED. IN NO EVENT SHALL MATTHEW CONTE BE LIABLE FOR ANY
  32. ** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  33. ** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  34. ** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  35. ** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  36. ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  37. ** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. */
  39. #include <stddef.h>
  40. #if defined(__cplusplus)
  41. extern "C" {
  42. #endif
  43. /* tlsf_t: a TLSF structure. Can contain 1 to N pools. */
  44. /* pool_t: a block of memory that TLSF can manage. */
  45. typedef void *tlsf_t;
  46. typedef void *pool_t;
  47. /* Create/destroy a memory pool. */
  48. tlsf_t tlsf_create(void *mem);
  49. tlsf_t tlsf_create_with_pool(void *mem, size_t bytes);
  50. void tlsf_destroy(tlsf_t tlsf);
  51. pool_t tlsf_get_pool(tlsf_t tlsf);
  52. /* Add/remove memory pools. */
  53. pool_t tlsf_add_pool(tlsf_t tlsf, void *mem, size_t bytes);
  54. void tlsf_remove_pool(tlsf_t tlsf, pool_t pool);
  55. /* malloc/memalign/realloc/free replacements. */
  56. void *tlsf_malloc(tlsf_t tlsf, size_t bytes);
  57. void *tlsf_memalign(tlsf_t tlsf, size_t align, size_t bytes);
  58. void *tlsf_realloc(tlsf_t tlsf, void *ptr, size_t size);
  59. void tlsf_free(tlsf_t tlsf, void *ptr);
  60. /* Returns internal block size, not original request size */
  61. size_t tlsf_block_size(void *ptr);
  62. /* Overheads/limits of internal structures. */
  63. size_t tlsf_size(void);
  64. size_t tlsf_align_size(void);
  65. size_t tlsf_block_size_min(void);
  66. size_t tlsf_block_size_max(void);
  67. size_t tlsf_pool_overhead(void);
  68. size_t tlsf_alloc_overhead(void);
  69. /* Debugging. */
  70. typedef void (*tlsf_walker)(void *ptr, size_t size, int used, void *user);
  71. void tlsf_walk_pool(pool_t pool, tlsf_walker walker, void *user);
  72. /* Returns nonzero if any internal consistency check fails. */
  73. int tlsf_check(tlsf_t tlsf);
  74. int tlsf_check_pool(pool_t pool);
  75. #if defined(__cplusplus)
  76. };
  77. #endif
  78. #endif