summaryrefslogtreecommitdiff
path: root/libs/pbd/pbd/stack_allocator.h
blob: d719b5c5f3c38958e8d8fd6e5db663feb50cfd0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
 * Copyright (C) 2020 Robin Gareus <robin@gareus.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifndef PBD_STACK_ALLOCATOR_H
#define PBD_STACK_ALLOCATOR_H

#include <boost/type_traits/aligned_storage.hpp>
#include <limits>

#include "pbd/libpbd_visibility.h"

#if 0
# include <cstdio>
# define DEBUG_STACK_ALLOC(...) printf (__VA_ARGS__)
#else
# define DEBUG_STACK_ALLOC(...)
#endif

namespace PBD {

template <class T, std::size_t stack_capacity>
class /*LIBPBD_API*/ StackAllocator
{
public:
#if 0 /* may be needed for compatibility */
	typedef typename std::allocator<T>::value_type value_type;
	typedef typename std::allocator<T>::size_type size_type;
	typedef typename std::allocator<T>::difference_type difference_type;
	typedef typename std::allocator<T>::pointer pointer;
	typedef typename std::allocator<T>::const_pointer const_pointer;
	typedef typename std::allocator<T>::reference reference;
	typedef typename std::allocator<T>::const_reference const_reference;
#else
	typedef T                 value_type;
	typedef std::size_t       size_type;
	typedef std::ptrdiff_t    difference_type;
	typedef value_type*       pointer;
	typedef const value_type* const_pointer;
	typedef value_type&       reference;
	typedef const value_type& const_reference;
#endif

	template <class U>
	struct rebind {
		typedef StackAllocator<U, stack_capacity> other;
	};

	StackAllocator ()
		: _ptr ((pointer)&_buf)
	{ }

	StackAllocator (const StackAllocator&)
		: _ptr ((pointer)&_buf)
	{ }

	template <typename U, size_t other_capacity>
	StackAllocator (const StackAllocator<U, other_capacity>&)
		: _ptr ((pointer)&_buf)
	{ }

	/* inspired by http://howardhinnant.github.io/stack_alloc.h */
	pointer allocate (size_type n, void* hint = 0)
	{
		if ((pointer)&_buf + stack_capacity >= _ptr + n) {
			DEBUG_STACK_ALLOC ("Allocate %ld item(s) of size %zu on the stack\n", n, sizeof (T));
			pointer rv = _ptr;
			_ptr += n;
			return rv;
		} else {
			DEBUG_STACK_ALLOC ("Allocate using new (%ld * %zu)\n", n, sizeof (T));
			return static_cast<pointer> (::operator new (n * sizeof (T)));
		}
	}

	void deallocate (pointer p, size_type n)
	{
		if (pointer_in_buffer (p)) {
			if (p + n == _ptr) {
				DEBUG_STACK_ALLOC ("Deallocate: pop item from the top of the stack\n");
				_ptr = p;
			} else {
				DEBUG_STACK_ALLOC ("Deallocate: ignored. Item is not at the top of the stack \n");
			}
		} else {
			::operator delete (p);
		}
	}

	size_type max_size () const throw ()
	{
		return std::numeric_limits<size_type>::max () / sizeof (T);
	}

	bool operator== (StackAllocator const& a) const
	{
		return &_buf == &a._buf;
	}

	bool operator!= (StackAllocator const& a) const
	{
		return &_buf != &a._buf;
	}

	template <class U>
	void destroy (U* const p)
	{
		p->~U ();
	}

	template <class U>
	void construct (U* const p)
	{
		new (p) U ();
	}

	template <class U, class A>
	void construct (U* const p, A& a)
	{
		new (p) U (a);
	}

	template <class U, class A, class B>
	void construct (U* const p, A& a, B& b)
	{
		new (p) U (a, b);
	}

private:
	StackAllocator& operator= (const StackAllocator&);

	bool pointer_in_buffer (pointer const p)
	{
		return ((pointer const)&_buf <= p && p < (pointer const)&_buf + stack_capacity);
	}

	typedef typename boost::aligned_storage<sizeof (T) * stack_capacity, 16>::type align_t;

	align_t _buf;
	pointer _ptr;
};

} // namespace PBD

#endif