Sierra Toolkit Version of the Day
fixed_pool_eastl.cpp
00001 /*
00002 Copyright (C) 2009-2010 Electronic Arts, Inc.  All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions
00006 are met:
00007 
00008 1.  Redistributions of source code must retain the above copyright
00009     notice, this list of conditions and the following disclaimer.
00010 2.  Redistributions in binary form must reproduce the above copyright
00011     notice, this list of conditions and the following disclaimer in the
00012     documentation and/or other materials provided with the distribution.
00013 3.  Neither the name of Electronic Arts, Inc. ("EA") nor the names of
00014     its contributors may be used to endorse or promote products derived
00015     from this software without specific prior written permission.
00016 
00017 THIS SOFTWARE IS PROVIDED BY ELECTRONIC ARTS AND ITS CONTRIBUTORS "AS IS" AND ANY
00018 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00019 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00020 DISCLAIMED. IN NO EVENT SHALL ELECTRONIC ARTS OR ITS CONTRIBUTORS BE LIABLE FOR ANY
00021 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00022 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00023 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00024 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027 */
00028 
00030 // EASTL/fixed_pool.cpp
00031 //
00032 // Copyright (c) 2005, Electronic Arts. All rights reserved.
00033 // Written and maintained by Paul Pedriana.
00035 
00036 
00037 
00038 #include <stk_util/util/fixed_pool_eastl.h>
00039 #include <stk_util/util/fixed_allocator_eastl.h>
00040 
00041 
00042 
00043 namespace eastl
00044 {
00045 
00046 
00047     void fixed_pool_base::init(void* pMemory, size_t memorySize, size_t nodeSize,
00048                                size_t alignment, size_t /*alignmentOffset*/)
00049     {
00050         // To do: Support alignmentOffset.
00051 
00052         #if EASTL_FIXED_SIZE_TRACKING_ENABLED
00053             mnCurrentSize = 0;
00054             mnPeakSize    = 0;
00055         #endif
00056 
00057         if(pMemory)
00058         {
00059             // Assert that alignment is a power of 2 value (e.g. 1, 2, 4, 8, 16, etc.)
00060             EASTL_ASSERT((alignment & (alignment - 1)) == 0);
00061 
00062             // Make sure alignment is a valid value.
00063             if(alignment < 1)
00064                 alignment = 1;
00065 
00066             mpNext      = (Link*)(((uintptr_t)pMemory + (alignment - 1)) & ~(alignment - 1));
00067             memorySize -= (uintptr_t)mpNext - (uintptr_t)pMemory;
00068             pMemory     = mpNext;
00069 
00070             // The node size must be at least as big as a Link, which itself is sizeof(void*).
00071             if(nodeSize < sizeof(Link))
00072                 nodeSize = ((sizeof(Link) + (alignment - 1))) & ~(alignment - 1);
00073 
00074             // If the user passed in a memory size that wasn't a multiple of the node size,
00075             // we need to chop down the memory size so that the last node is not a whole node.
00076             memorySize = (memorySize / nodeSize) * nodeSize;
00077 
00078             mpCapacity = (Link*)((uintptr_t)pMemory + memorySize);
00079             mpHead     = NULL;
00080             mnNodeSize = nodeSize;
00081         }
00082     }
00083 
00084 
00085 } // namespace eastl
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends