Sierra Toolkit  Version of the Day
fixed_pool_eastl.cpp
1 /*
2 Copyright (C) 2009-2010 Electronic Arts, Inc. All rights reserved.
3 
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
7 
8 1. Redistributions of source code must retain the above copyright
9  notice, this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11  notice, this list of conditions and the following disclaimer in the
12  documentation and/or other materials provided with the distribution.
13 3. Neither the name of Electronic Arts, Inc. ("EA") nor the names of
14  its contributors may be used to endorse or promote products derived
15  from this software without specific prior written permission.
16 
17 THIS SOFTWARE IS PROVIDED BY ELECTRONIC ARTS AND ITS CONTRIBUTORS "AS IS" AND ANY
18 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL ELECTRONIC ARTS OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28 
30 // EASTL/fixed_pool.cpp
31 //
32 // Copyright (c) 2005, Electronic Arts. All rights reserved.
33 // Written and maintained by Paul Pedriana.
35 
36 
37 
38 #include <stk_util/util/fixed_pool_eastl.h>
39 #include <stk_util/util/fixed_allocator_eastl.h>
40 
41 
42 
43 namespace eastl
44 {
45 
46 
47  void fixed_pool_base::init(void* pMemory, size_t memorySize, size_t nodeSize,
48  size_t alignment, size_t /*alignmentOffset*/)
49  {
50  // To do: Support alignmentOffset.
51 
52  #if EASTL_FIXED_SIZE_TRACKING_ENABLED
53  mnCurrentSize = 0;
54  mnPeakSize = 0;
55  #endif
56 
57  if(pMemory)
58  {
59  // Assert that alignment is a power of 2 value (e.g. 1, 2, 4, 8, 16, etc.)
60  EASTL_ASSERT((alignment & (alignment - 1)) == 0);
61 
62  // Make sure alignment is a valid value.
63  if(alignment < 1)
64  alignment = 1;
65 
66  mpNext = (Link*)(((uintptr_t)pMemory + (alignment - 1)) & ~(alignment - 1));
67  memorySize -= (uintptr_t)mpNext - (uintptr_t)pMemory;
68  pMemory = mpNext;
69 
70  // The node size must be at least as big as a Link, which itself is sizeof(void*).
71  if(nodeSize < sizeof(Link))
72  nodeSize = ((sizeof(Link) + (alignment - 1))) & ~(alignment - 1);
73 
74  // If the user passed in a memory size that wasn't a multiple of the node size,
75  // we need to chop down the memory size so that the last node is not a whole node.
76  memorySize = (memorySize / nodeSize) * nodeSize;
77 
78  mpCapacity = (Link*)((uintptr_t)pMemory + memorySize);
79  mpHead = NULL;
80  mnNodeSize = nodeSize;
81  }
82  }
83 
84 
85 } // namespace eastl
void init(void *pMemory, size_t memorySize, size_t nodeSize, size_t alignment, size_t alignmentOffset=0)
EA Standard Template Library.