pool_allocator.h
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-2016 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 
32 #include "typedefs.h"
33 
43 enum {
44 
45  POOL_ALLOCATOR_INVALID_ID=-1
46 };
47 
49 public:
50  typedef int ID;
51 private:
52  enum {
53  CHECK_BITS=8,
54  CHECK_LEN=(1<<CHECK_BITS),
55  CHECK_MASK=CHECK_LEN-1
56 
57  };
58 
59 
60  struct Entry {
61 
62  unsigned int pos;
63  unsigned int len;
64  unsigned int lock;
65  unsigned int check;
66 
67  inline void clear() { pos=0; len=0; lock=0; check=0; }
68  Entry() { clear(); }
69  };
70 
71 
72  typedef int EntryArrayPos;
73  typedef int EntryIndicesPos;
74 
75  Entry *entry_array;
76  int *entry_indices;
77  int entry_max;
78  int entry_count;
79 
80  uint8_t *pool;
81  void *mem_ptr;
82  int pool_size;
83 
84  int free_mem;
85  int free_mem_peak;
86 
87  unsigned int check_count;
88  int align;
89 
90  bool needs_locking;
91 
92  inline int entry_end(const Entry& p_entry) const {
93  return p_entry.pos+aligned(p_entry.len);
94  }
95  inline int aligned(int p_size) const {
96 
97  int rem=p_size%align;
98  if (rem)
99  p_size+=align-rem;
100 
101  return p_size;
102  }
103 
104  void compact(int p_up_to=-1);
105  void compact_up(int p_from=0);
106  bool get_free_entry(EntryArrayPos* p_pos);
107  bool find_hole(EntryArrayPos *p_pos, int p_for_size);
108  bool find_entry_index(EntryIndicesPos *p_map_pos,Entry *p_entry);
109  Entry* get_entry(ID p_mem);
110  const Entry* get_entry(ID p_mem) const;
111 
112  void create_pool(void * p_mem,int p_size,int p_max_entries);
113 protected:
114  virtual void mt_lock() const;
115  virtual void mt_unlock() const;
116 
117 public:
118 
119  enum {
120  DEFAULT_MAX_ALLOCS=4096,
121  };
122 
123  ID alloc(int p_size);
124  void free(ID p_mem);
125  Error resize(ID p_mem,int p_new_size);
126  int get_size(ID p_mem) const;
127 
128  int get_free_mem();
129  int get_used_mem() const;
130  int get_free_peak();
131 
132  Error lock(ID p_mem); //@todo move this out
133  void *get(ID p_mem);
134  const void *get(ID p_mem) const;
135  void unlock(ID p_mem);
136  bool is_locked(ID p_mem) const;
137 
138  PoolAllocator(int p_size,bool p_needs_locking=false,int p_max_entries=DEFAULT_MAX_ALLOCS);
139  PoolAllocator(void * p_mem,int p_size, int p_align = 1, bool p_needs_locking=false,int p_max_entries=DEFAULT_MAX_ALLOCS);
140  PoolAllocator(int p_align,int p_size,bool p_needs_locking=false,int p_max_entries=DEFAULT_MAX_ALLOCS);
141 
142  virtual ~PoolAllocator();
143 
144 };
145 
146 #endif
virtual void mt_unlock() const
Reimplement for custom mt locking.
Definition: pool_allocator.cpp:48
ID alloc(int p_size)
Alloc memory, get an ID on success, POOL_ALOCATOR_INVALID_ID on failure.
Definition: pool_allocator.cpp:192
Error resize(ID p_mem, int p_new_size)
resize a memory chunk
Definition: pool_allocator.cpp:343
void free(ID p_mem)
Free allocated memory.
Definition: pool_allocator.cpp:286
int get_free_peak()
get free memory
Definition: pool_allocator.cpp:580
virtual void mt_lock() const
Reimplement for custom mt locking.
Definition: pool_allocator.cpp:44
int get_free_mem()
get free memory
Definition: pool_allocator.cpp:585
Definition: pool_allocator.h:48