Qualia  0.2
StaticAllocator.h
Go to the documentation of this file.
1 /*
2  * StaticAllocator.h
3  *
4  * An allocator (see Allocator.h) that "allocates" memory based on a
5  * pre-allocated static memory pool/buffer. Useful to manage memory on
6  * architectures that don't support well dynamic allocation (such as
7  * AVR-based systems). On such systems, it is usually recommended NOT
8  * to use dynamic allocation to avoid problems.
9  *
10  * This file is part of Qualia https://github.com/sofian/qualia
11  *
12  * (c) 2011 Sofian Audry | info(@)sofianaudry(.)com
13  *
14  * This program is free software: you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation, either version 3 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program. If not, see <http://www.gnu.org/licenses/>.
26  */
27 
28 #ifndef STATICALLOCATOR_H_
29 #define STATICALLOCATOR_H_
30 
31 #include "Allocator.h"
32 
57 class StaticAllocator: public Allocator {
58 public:
59  unsigned char* buffer;
60  size_t bufferSize;
61  unsigned int bufferIdx;
62  unsigned char* lastPtr;
63 
64  // Keeps track of leaks, for debugging.
65  unsigned int nLeaks;
66  unsigned char* lastLeak;
67 
68 public:
69  StaticAllocator(unsigned char* buffer, size_t size);
70 
71 protected:
72  virtual void* malloc(size_t size);
73 
74  virtual void* calloc(size_t num, size_t size);
75 
76  // WARNING: You should not use that method for static allocators unless you know what you are doing.
77  virtual void* realloc(void* ptr, size_t size);
78 
79  // WARNING: Calling StaticAllocator::free() does NOT free the pointer at all. See note above.
80  virtual void free(void* ptr);
81 
82  // Frees all pointers.
83  virtual void freeAll();
84 };
85 
86 #endif /* STATICALLOCATOR_H_ */