pool_allocator.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*************************************************************************/
  2. /* pool_allocator.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef POOL_ALLOCATOR_H
  30. #define POOL_ALLOCATOR_H
  31. #include "typedefs.h"
  32. /**
  33. @author Juan Linietsky <reduzio@gmail.com>
  34. * Generic Pool Allocator.
  35. * This is a generic memory pool allocator, with locking, compacting and alignment. (@TODO alignment)
  36. * It used as a standard way to manage alloction in a specific region of memory, such as texture memory,
  37. * audio sample memory, or just any kind of memory overall.
  38. * (@TODO) abstraction should be greater, because in many platforms, you need to manage a nonreachable memory.
  39. */
  40. enum {
  41. POOL_ALLOCATOR_INVALID_ID=-1 ///< default invalid value. use INVALID_ID( id ) to test
  42. };
  43. class PoolAllocator {
  44. public:
  45. typedef int ID;
  46. private:
  47. enum {
  48. CHECK_BITS=8,
  49. CHECK_LEN=(1<<CHECK_BITS),
  50. CHECK_MASK=CHECK_LEN-1
  51. };
  52. struct Entry {
  53. unsigned int pos;
  54. unsigned int len;
  55. unsigned int lock;
  56. unsigned int check;
  57. inline void clear() { pos=0; len=0; lock=0; check=0; }
  58. Entry() { clear(); }
  59. };
  60. typedef int EntryArrayPos;
  61. typedef int EntryIndicesPos;
  62. Entry *entry_array;
  63. int *entry_indices;
  64. int entry_max;
  65. int entry_count;
  66. uint8_t *pool;
  67. void *mem_ptr;
  68. int pool_size;
  69. int free_mem;
  70. int free_mem_peak;
  71. unsigned int check_count;
  72. int align;
  73. bool needs_locking;
  74. inline int entry_end(const Entry& p_entry) const {
  75. return p_entry.pos+aligned(p_entry.len);
  76. }
  77. inline int aligned(int p_size) const {
  78. int rem=p_size%align;
  79. if (rem)
  80. p_size+=align-rem;
  81. return p_size;
  82. }
  83. void compact(int p_up_to=-1);
  84. void compact_up(int p_from=0);
  85. bool get_free_entry(EntryArrayPos* p_pos);
  86. bool find_hole(EntryArrayPos *p_pos, int p_for_size);
  87. bool find_entry_index(EntryIndicesPos *p_map_pos,Entry *p_entry);
  88. Entry* get_entry(ID p_mem);
  89. const Entry* get_entry(ID p_mem) const;
  90. void create_pool(void * p_mem,int p_size,int p_max_entries);
  91. protected:
  92. virtual void mt_lock() const; ///< Reimplement for custom mt locking
  93. virtual void mt_unlock() const; ///< Reimplement for custom mt locking
  94. public:
  95. enum {
  96. DEFAULT_MAX_ALLOCS=4096,
  97. };
  98. ID alloc(int p_size); ///< Alloc memory, get an ID on success, POOL_ALOCATOR_INVALID_ID on failure
  99. void free(ID p_mem); ///< Free allocated memory
  100. Error resize(ID p_mem,int p_new_size); ///< resize a memory chunk
  101. int get_size(ID p_mem) const;
  102. int get_free_mem(); ///< get free memory
  103. int get_used_mem() const;
  104. int get_free_peak(); ///< get free memory
  105. Error lock(ID p_mem); //@todo move this out
  106. void *get(ID p_mem);
  107. const void *get(ID p_mem) const;
  108. void unlock(ID p_mem);
  109. bool is_locked(ID p_mem) const;
  110. PoolAllocator(int p_size,bool p_needs_locking=false,int p_max_entries=DEFAULT_MAX_ALLOCS);
  111. PoolAllocator(void * p_mem,int p_size, int p_align = 1, bool p_needs_locking=false,int p_max_entries=DEFAULT_MAX_ALLOCS);
  112. PoolAllocator(int p_align,int p_size,bool p_needs_locking=false,int p_max_entries=DEFAULT_MAX_ALLOCS);
  113. virtual ~PoolAllocator();
  114. };
  115. #endif