allocator.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include "types.h"
  25. namespace crown
  26. {
  27. /// Base class for memory allocators.
  28. ///
  29. /// @ingroup Memory
  30. class Allocator
  31. {
  32. public:
  33. Allocator() {}
  34. virtual ~Allocator() {}
  35. /// Allocates @a size bytes of memory aligned to the specified
  36. /// @a align byte and returns a pointer to the first allocated byte.
  37. virtual void* allocate(size_t size, size_t align = DEFAULT_ALIGN) = 0;
  38. /// Deallocates a previously allocated block of memory pointed by @a data.
  39. virtual void deallocate(void* data) = 0;
  40. /// Returns the total number of bytes allocated.
  41. virtual size_t allocated_size() = 0;
  42. /// Default memory alignment in bytes.
  43. static const size_t DEFAULT_ALIGN = 4;
  44. private:
  45. // Disable copying
  46. Allocator(const Allocator&);
  47. Allocator& operator=(const Allocator&);
  48. };
  49. } // namespace crown