heap_tlsf.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. #include "heap_tlsf_block_functions.h"
  68. /* tlsf_t: a TLSF structure. Can contain 1 to N pools. */
  69. /* pool_t: a block of memory that TLSF can manage. */
  70. typedef void* tlsf_t;
  71. typedef void* pool_t;
  72. /* Create/destroy a memory pool. */
  73. tlsf_t tlsf_create(void* mem, size_t max_bytes);
  74. tlsf_t tlsf_create_with_pool(void* mem, size_t pool_bytes, size_t max_bytes);
  75. pool_t tlsf_get_pool(tlsf_t tlsf);
  76. /* Add/remove memory pools. */
  77. pool_t tlsf_add_pool(tlsf_t tlsf, void* mem, size_t bytes);
  78. void tlsf_remove_pool(tlsf_t tlsf, pool_t pool);
  79. /* malloc/memalign/realloc/free replacements. */
  80. void* tlsf_malloc(tlsf_t tlsf, size_t size);
  81. void* tlsf_memalign(tlsf_t tlsf, size_t align, size_t size);
  82. void* tlsf_memalign_offs(tlsf_t tlsf, size_t align, size_t size, size_t offset);
  83. void* tlsf_realloc(tlsf_t tlsf, void* ptr, size_t size);
  84. void tlsf_free(tlsf_t tlsf, void* ptr);
  85. /* Returns internal block size, not original request size */
  86. size_t tlsf_block_size(void* ptr);
  87. /* Overheads/limits of internal structures. */
  88. size_t tlsf_size(tlsf_t tlsf);
  89. size_t tlsf_align_size(void);
  90. size_t tlsf_block_size_min(void);
  91. size_t tlsf_block_size_max(tlsf_t tlsf);
  92. size_t tlsf_pool_overhead(void);
  93. size_t tlsf_alloc_overhead(void);
  94. size_t tlsf_fit_size(tlsf_t tlsf, size_t size);
  95. /* Debugging. */
  96. typedef void (*tlsf_walker)(void* ptr, size_t size, int used, void* user);
  97. void tlsf_walk_pool(pool_t pool, tlsf_walker walker, void* user);
  98. /* Returns nonzero if any internal consistency check fails. */
  99. int tlsf_check(tlsf_t tlsf);
  100. int tlsf_check_pool(pool_t pool);
  101. #if defined(__cplusplus)
  102. };
  103. #endif