heap_tlsf.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. ** Two Level Segregated Fit memory allocator, version 3.1.
  3. ** Written by Matthew Conte
  4. ** http://tlsf.baisoku.org
  5. **
  6. ** Based on the original documentation by Miguel Masmano:
  7. ** http://www.gii.upv.es/tlsf/main/docs
  8. **
  9. ** This implementation was written to the specification
  10. ** of the document, therefore no GPL restrictions apply.
  11. **
  12. ** Copyright (c) 2006-2016, Matthew Conte
  13. ** All rights reserved.
  14. **
  15. ** Redistribution and use in source and binary forms, with or without
  16. ** modification, are permitted provided that the following conditions are met:
  17. ** * Redistributions of source code must retain the above copyright
  18. ** notice, this list of conditions and the following disclaimer.
  19. ** * Redistributions in binary form must reproduce the above copyright
  20. ** notice, this list of conditions and the following disclaimer in the
  21. ** documentation and/or other materials provided with the distribution.
  22. ** * Neither the name of the copyright holder nor the
  23. ** names of its contributors may be used to endorse or promote products
  24. ** derived from this software without specific prior written permission.
  25. **
  26. ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  27. ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  28. ** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  29. ** DISCLAIMED. IN NO EVENT SHALL MATTHEW CONTE BE LIABLE FOR ANY
  30. ** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  31. ** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  32. ** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  33. ** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  34. ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35. ** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. #pragma once
  38. #include <assert.h>
  39. #include <limits.h>
  40. #include <stddef.h>
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <string.h>
  44. #include <stddef.h>
  45. #include "heap_tlsf_config.h"
  46. #if defined(__cplusplus)
  47. extern "C" {
  48. #endif
  49. /*
  50. ** Cast and min/max macros.
  51. */
  52. #define tlsf_cast(t, exp) ((t) (exp))
  53. #define tlsf_min(a, b) ((a) < (b) ? (a) : (b))
  54. #define tlsf_max(a, b) ((a) > (b) ? (a) : (b))
  55. /* A type used for casting when doing pointer arithmetic. */
  56. typedef ptrdiff_t tlsfptr_t;
  57. typedef struct block_header_t
  58. {
  59. /* Points to the previous physical block. */
  60. struct block_header_t* prev_phys_block;
  61. /* The size of this block, excluding the block header. */
  62. size_t size;
  63. /* Next and previous free blocks. */
  64. struct block_header_t* next_free;
  65. struct block_header_t* prev_free;
  66. } block_header_t;
  67. /* The TLSF control structure. */
  68. typedef struct control_t
  69. {
  70. /* Empty lists point at this block to indicate they are free. */
  71. block_header_t block_null;
  72. /* Bitmaps for free lists. */
  73. unsigned int fl_bitmap;
  74. unsigned int sl_bitmap[FL_INDEX_COUNT];
  75. /* Head of free lists. */
  76. block_header_t* blocks[FL_INDEX_COUNT][SL_INDEX_COUNT];
  77. } control_t;
  78. #include "heap_tlsf_block_functions.h"
  79. /* tlsf_t: a TLSF structure. Can contain 1 to N pools. */
  80. /* pool_t: a block of memory that TLSF can manage. */
  81. typedef void* tlsf_t;
  82. typedef void* pool_t;
  83. /* Create/destroy a memory pool. */
  84. tlsf_t tlsf_create(void* mem);
  85. tlsf_t tlsf_create_with_pool(void* mem, size_t bytes);
  86. pool_t tlsf_get_pool(tlsf_t tlsf);
  87. /* Add/remove memory pools. */
  88. pool_t tlsf_add_pool(tlsf_t tlsf, void* mem, size_t bytes);
  89. void tlsf_remove_pool(tlsf_t tlsf, pool_t pool);
  90. /* malloc/memalign/realloc/free replacements. */
  91. void* tlsf_malloc(tlsf_t tlsf, size_t size);
  92. void* tlsf_memalign(tlsf_t tlsf, size_t align, size_t size);
  93. void* tlsf_memalign_offs(tlsf_t tlsf, size_t align, size_t size, size_t offset);
  94. void* tlsf_realloc(tlsf_t tlsf, void* ptr, size_t size);
  95. void tlsf_free(tlsf_t tlsf, void* ptr);
  96. /* Returns internal block size, not original request size */
  97. size_t tlsf_block_size(void* ptr);
  98. /* Overheads/limits of internal structures. */
  99. size_t tlsf_size(void);
  100. size_t tlsf_align_size(void);
  101. size_t tlsf_block_size_min(void);
  102. size_t tlsf_block_size_max(void);
  103. size_t tlsf_pool_overhead(void);
  104. size_t tlsf_alloc_overhead(void);
  105. /* Debugging. */
  106. typedef void (*tlsf_walker)(void* ptr, size_t size, int used, void* user);
  107. void tlsf_walk_pool(pool_t pool, tlsf_walker walker, void* user);
  108. /* Returns nonzero if any internal consistency check fails. */
  109. int tlsf_check(tlsf_t tlsf);
  110. int tlsf_check_pool(pool_t pool);
  111. #if defined(__cplusplus)
  112. };
  113. #endif