ASPiK SDK
malloc.h
1 // This file is part of VSTGUI. It is subject to the license terms
2 // in the LICENSE file found in the top-level directory of this
3 // distribution and at http://github.com/steinbergmedia/vstgui/LICENSE
4 
5 #pragma once
6 
7 #include "../lib/vstguibase.h"
8 #include <cstring>
9 
10 namespace VSTGUI {
11 
12 //-----------------------------------------------------------------------------
14 {
15  static void* allocate (size_t size) { return std::malloc (size); }
16  static void deallocate (void* ptr, size_t size) { std::free (ptr); }
17 };
18 
19 //-----------------------------------------------------------------------------
20 template <typename T, typename Allocator = MallocAllocator>
21 class Malloc
22 {
23 public:
24  Malloc () = default;
25  Malloc (size_t objectCount) : count (objectCount)
26  {
27  if (objectCount)
28  allocate (objectCount);
29  }
30  Malloc (Malloc&& other) { *this = std::move (other); }
31  Malloc& operator= (Malloc&& other)
32  {
33  buffer = other.buffer;
34  count = other.count;
35  other.buffer = nullptr;
36  other.count = 0;
37  return *this;
38  }
39  Malloc (const Malloc&) = delete;
40  Malloc& operator= (const Malloc&) = delete;
41  ~Malloc () { deallocate (); }
42 
43  T* get () { return buffer; }
44  const T* get () const { return buffer; }
45  size_t size () const { return count; }
46 
47  void allocate (size_t objectCount)
48  {
49  if (buffer)
50  deallocate ();
51  if (objectCount)
52  buffer = static_cast<T*> (Allocator::allocate (objectCount * sizeof (T)));
53  count = objectCount;
54  }
55 
56  void deallocate ()
57  {
58  if (buffer)
59  {
60  Allocator::deallocate (buffer, count * sizeof (T));
61  buffer = nullptr;
62  count = 0;
63  }
64  }
65 
66 private:
67  T* buffer {nullptr};
68  size_t count {0};
69 };
70 
71 } // VSTGUI
Definition: malloc.h:21
Definition: malloc.h:13
Definition: customcontrols.cpp:8