Skip to content

Commit 23aaf04

Browse files
committed
Create StackAlloc.h
1 parent 79db4ce commit 23aaf04

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

StackAlloc.h

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*-
2+
* Copyright (c) 2013 Cosku Acay, http://www.coskuacay.com
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a
5+
* copy of this software and associated documentation files (the "Software"),
6+
* to deal in the Software without restriction, including without limitation
7+
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
8+
* and/or sell copies of the Software, and to permit persons to whom the
9+
* Software is furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20+
* IN THE SOFTWARE.
21+
*/
22+
23+
24+
/*-
25+
* A template class that implements a simple stack structure.
26+
* This demostrates how to use alloctor_traits (specifically with MemoryPool)
27+
*/
28+
29+
#ifndef STACK_ALLOC_H
30+
#define STACK_ALLOC_H
31+
32+
#include <memory>
33+
34+
template <typename T>
35+
struct StackNode_
36+
{
37+
T data;
38+
StackNode_* prev;
39+
};
40+
41+
/** T is the object to store in the stack, Alloc is the allocator to use */
42+
template <class T, class Alloc = std::allocator<T> >
43+
class StackAlloc
44+
{
45+
public:
46+
typedef StackNode_<T> Node;
47+
typedef typename std::allocator_traits<Alloc>::template rebind_alloc<Node>
48+
allocator;
49+
50+
/** Default constructor */
51+
StackAlloc() {head_ = nullptr; }
52+
/** Default destructor */
53+
~StackAlloc() { clear(); }
54+
55+
/** Returns true if the stack is empty */
56+
bool empty() {return (head_ == nullptr);}
57+
58+
/** Deallocate all elements and empty the stack */
59+
void clear() {
60+
Node* curr = head_;
61+
while (curr != nullptr)
62+
{
63+
Node* tmp = curr->prev;
64+
allocator_.destroy(curr);
65+
allocator_.deallocate(curr, 1);
66+
curr = tmp;
67+
}
68+
head_ = nullptr;
69+
}
70+
71+
/** Put an element on the top of the stack */
72+
void push(T element) {
73+
Node* newNode = allocator_.allocate(1);
74+
allocator_.construct(newNode);
75+
newNode->data = element;
76+
newNode->prev = head_;
77+
head_ = newNode;
78+
}
79+
80+
/** Remove and return the topmost element on the stack */
81+
T pop() {
82+
T result = head_->data;
83+
Node* tmp = head_->prev;
84+
allocator_.destroy(head_);
85+
allocator_.deallocate(head_, 1);
86+
head_ = tmp;
87+
return result;
88+
}
89+
90+
/** Return the topmost element */
91+
T top() { return (head_->data); }
92+
93+
private:
94+
allocator allocator_;
95+
Node* head_;
96+
};
97+
98+
#endif // STACK_ALLOC_H

0 commit comments

Comments
 (0)