1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
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.
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/>.
19 #include "nel/misc/contiguous_block_allocator.h"
28 // *********************************************************************************************************
29 CContiguousBlockAllocator::CContiguousBlockAllocator()
32 _NextAvailablePos
= NULL
;
34 _NumAllocatedBytes
= 0;
41 // *********************************************************************************************************
42 CContiguousBlockAllocator::~CContiguousBlockAllocator()
47 // *********************************************************************************************************
48 void CContiguousBlockAllocator::init(uint numBytes
/*=0*/)
50 if (_BlockStart
) _DefaultAlloc
.deallocate(_BlockStart
, _BlockEnd
- _BlockStart
);
53 _NumAllocatedBytes
= 0;
54 _NextAvailablePos
= NULL
;
57 _BlockStart
= _DefaultAlloc
.allocate(numBytes
);
58 _NextAvailablePos
= _BlockStart
;
59 _BlockEnd
= _BlockStart
+ numBytes
;
60 _NumAllocatedBytes
= 0;
68 // *********************************************************************************************************
69 void *CContiguousBlockAllocator::alloc(uint numBytes
)
71 if (numBytes
== 0) return NULL
;
72 _NumAllocatedBytes
+= numBytes
;
75 if (_NextAvailablePos
+ numBytes
<= _BlockEnd
)
77 uint8
*block
= _NextAvailablePos
;
78 _NextAvailablePos
+= numBytes
;
85 // just uses standard new
89 return _DefaultAlloc
.allocate(numBytes
);
92 // *********************************************************************************************************
93 void CContiguousBlockAllocator::freeBlock(void *block
, uint numBytes
)
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
);