MemoryManager.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * MemoryManager.h
  3. *
  4. * Copyright (c) 2017 Lukas W <lukaswhl/at/gmail.com>
  5. * Copyright (c) 2014 Vesa Kivimäki
  6. * Copyright (c) 2007-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
  7. *
  8. * This file is part of LMMS - https://lmms.io
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2 of the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public
  21. * License along with this program (see COPYING); if not, write to the
  22. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  23. * Boston, MA 02110-1301 USA.
  24. *
  25. */
  26. #ifndef MEMORY_MANAGER_H
  27. #define MEMORY_MANAGER_H
  28. #include <cstddef>
  29. #include <vector>
  30. #include "lmms_export.h"
  31. class LMMS_EXPORT MemoryManager
  32. {
  33. public:
  34. struct ThreadGuard
  35. {
  36. ThreadGuard();
  37. ~ThreadGuard();
  38. };
  39. static void * alloc( size_t size );
  40. static void free( void * ptr );
  41. };
  42. template<typename T>
  43. struct MmAllocator
  44. {
  45. typedef T value_type;
  46. template<class U> struct rebind { typedef MmAllocator<U> other; };
  47. T* allocate( std::size_t n )
  48. {
  49. return reinterpret_cast<T*>( MemoryManager::alloc( sizeof(T) * n ) );
  50. }
  51. void deallocate( T* p, std::size_t )
  52. {
  53. MemoryManager::free( p );
  54. }
  55. typedef std::vector<T, MmAllocator<T> > vector;
  56. };
  57. #define MM_OPERATORS \
  58. public: \
  59. static void * operator new ( size_t size ) \
  60. { \
  61. return MemoryManager::alloc( size ); \
  62. } \
  63. static void * operator new[] ( size_t size ) \
  64. { \
  65. return MemoryManager::alloc( size ); \
  66. } \
  67. static void operator delete ( void * ptr ) \
  68. { \
  69. MemoryManager::free( ptr ); \
  70. } \
  71. static void operator delete[] ( void * ptr ) \
  72. { \
  73. MemoryManager::free( ptr ); \
  74. }
  75. // for use in cases where overriding new/delete isn't a possibility
  76. template<typename T>
  77. T* MM_ALLOC(size_t count)
  78. {
  79. return reinterpret_cast<T*>(
  80. MemoryManager::alloc(sizeof(T) * count));
  81. }
  82. // and just for symmetry...
  83. template<typename T>
  84. void MM_FREE(T* ptr)
  85. {
  86. MemoryManager::free(ptr);
  87. }
  88. #endif