Merge branch '138-toggle-free-look-with-hotkey' into 'main/atys-live'
[ryzomcore.git] / nel / src / misc / contiguous_block_allocator.cpp
blobb99c6b090365665c9ebe85395b934d25e402a7c6
1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "stdmisc.h"
19 #include "nel/misc/contiguous_block_allocator.h"
21 #ifdef DEBUG_NEW
22 #define new DEBUG_NEW
23 #endif
25 namespace NLMISC
28 // *********************************************************************************************************
29 CContiguousBlockAllocator::CContiguousBlockAllocator()
31 _BlockStart = NULL;
32 _NextAvailablePos = NULL;
33 _BlockEnd = 0;
34 _NumAllocatedBytes = 0;
35 #ifdef NL_DEBUG
36 _NumAlloc = 0;
37 _NumFree = 0;
38 #endif
41 // *********************************************************************************************************
42 CContiguousBlockAllocator::~CContiguousBlockAllocator()
44 init(0);
47 // *********************************************************************************************************
48 void CContiguousBlockAllocator::init(uint numBytes /*=0*/)
50 if (_BlockStart) _DefaultAlloc.deallocate(_BlockStart, _BlockEnd - _BlockStart);
51 _BlockEnd = NULL;
52 _BlockStart = NULL;
53 _NumAllocatedBytes = 0;
54 _NextAvailablePos = NULL;
55 if (numBytes != 0)
57 _BlockStart = _DefaultAlloc.allocate(numBytes);
58 _NextAvailablePos = _BlockStart;
59 _BlockEnd = _BlockStart + numBytes;
60 _NumAllocatedBytes = 0;
62 #ifdef NL_DEBUG
63 _NumAlloc = 0;
64 _NumFree = 0;
65 #endif
68 // *********************************************************************************************************
69 void *CContiguousBlockAllocator::alloc(uint numBytes)
71 if (numBytes == 0) return NULL;
72 _NumAllocatedBytes += numBytes;
73 if (_BlockStart)
75 if (_NextAvailablePos + numBytes <= _BlockEnd)
77 uint8 *block = _NextAvailablePos;
78 _NextAvailablePos += numBytes;
79 #ifdef NL_DEBUG
80 ++ _NumAlloc;
81 #endif
82 return block;
85 // just uses standard new
86 #ifdef NL_DEBUG
87 ++ _NumAlloc;
88 #endif
89 return _DefaultAlloc.allocate(numBytes);
92 // *********************************************************************************************************
93 void CContiguousBlockAllocator::freeBlock(void *block, uint numBytes)
95 if (!block) return;
96 #ifdef NL_DEBUG
97 ++ _NumFree;
98 #endif
99 // no-op if block not inside the big block (sub-block are never deallocated until init(0) is encountered)
100 if (block < _BlockStart || block >= _BlockEnd)
102 // the block was allocated with std allocator
103 _DefaultAlloc.deallocate((uint8 *) block, numBytes);
107 } // NLMISC