cppcore
A small, portable C++ core library
Loading...
Searching...
No Matches
TDefaultAllocator.h
Go to the documentation of this file.
1/*-----------------------------------------------------------------------------------------------
2The MIT License (MIT)
3
4Copyright (c) 2014-2025 Kim Kulling
5
6Permission is hereby granted, free of charge, to any person obtaining a copy of
7this software and associated documentation files (the "Software"), to deal in
8the Software without restriction, including without limitation the rights to
9use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10the Software, and to permit persons to whom the Software is furnished to do so,
11subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in all
14copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22-----------------------------------------------------------------------------------------------*/
23#pragma once
24
25#include <string>
26
27namespace cppcore {
28
29//-------------------------------------------------------------------------------------------------
35//-------------------------------------------------------------------------------------------------
36template <class T>
38public:
40 TDefaultAllocator() = default;
41
43 ~TDefaultAllocator() = default;
44
48 T *alloc(size_t size);
49
53 void release(T *ptr);
54
57 void reserve(size_t size);
58
60 void clear();
61
64 size_t capacity() const;
65
68 size_t reservedMem() const;
69
72 size_t freeMem() const;
73
76 void dumpAllocations(std::string &allocs);
77
78 // To copying allowed
80};
81
82template <class T>
83inline T *TDefaultAllocator<T>::alloc(size_t size) {
84 return new T[size];
85}
86
87template <class T>
88inline void TDefaultAllocator<T>::release(T *ptr) {
89 delete [] ptr;
90}
91
92template <class T>
93inline void TDefaultAllocator<T>::reserve(size_t) {
94 // empty
95}
96
97template <class T>
99 // empty
100}
101
102template <class T>
103inline size_t TDefaultAllocator<T>::capacity() const {
104 return 0L;
105}
106
107template <class T>
109 return 0L;
110}
111
112template <class T>
113inline size_t TDefaultAllocator<T>::freeMem() const {
114 return 0L;
115}
116
117template <class T>
118inline void TDefaultAllocator<T>::dumpAllocations(std::string &allocs) {
119 // empty
120}
121
122} // namespace cppcore
#define CPPCORE_NONE_COPYING(name)
This macro marks a class a not copyable.
void dumpAllocations(std::string &allocs)
Will dump a statistic overview into the given string.
~TDefaultAllocator()=default
The class destructor.
TDefaultAllocator()=default
The default class constructor.
T * alloc(size_t size)
Will allocate the number of insances.
size_t freeMem() const
Returns the free memory in bytes.
void reserve(size_t size)
Will prereserve the number of instances.
size_t reservedMem() const
Returns the allocated memory in bytes.
void release(T *ptr)
Will release the last item from the stack.
size_t capacity() const
Returns the number of allocated instances on the stack.
void clear()
Will clear the whole memory, all used data will be marked as invalid.