tVector.cpp

Engine/source/core/util/tVector.cpp

More...

Public Functions

bool
VectorResize(U32 * aSize, U32 * aCount, void ** arrayPtr, U32 newCount, U32 elemSize)

Detailed Description

Public Functions

VectorResize(U32 * aSize, U32 * aCount, void ** arrayPtr, U32 newCount, U32 elemSize)

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#include "platform/platform.h"
 25#include "core/util/tVector.h"
 26
 27#include "platform/profiler.h"
 28
 29
 30#ifdef TORQUE_DEBUG_GUARD
 31
 32bool VectorResize(U32 *aSize, U32 *aCount, void **arrayPtr, U32 newCount, U32 elemSize,
 33                  const char* fileName,
 34                  const U32   lineNum)
 35{
 36   PROFILE_SCOPE( VectorResize );
 37
 38   if (newCount > 0) 
 39   {
 40      U32 blocks = newCount / VectorBlockSize;
 41      if (newCount % VectorBlockSize)
 42         blocks++;
 43      S32 mem_size = blocks * VectorBlockSize * elemSize;
 44
 45      const char* pUseFileName = fileName != NULL ? fileName : __FILE__;
 46      U32 useLineNum           = fileName != NULL ? lineNum  : __LINE__;
 47
 48      if (*arrayPtr != NULL)
 49         *arrayPtr = dRealloc_r(*arrayPtr, mem_size, pUseFileName, useLineNum);
 50      else
 51         *arrayPtr = dMalloc_r(mem_size, pUseFileName, useLineNum);
 52
 53      AssertFatal( *arrayPtr, "VectorResize - Allocation failed." );
 54
 55      *aCount = newCount;
 56      *aSize = blocks * VectorBlockSize;
 57      return true;
 58   }
 59
 60   if (*arrayPtr) 
 61   {
 62      dFree(*arrayPtr);
 63      *arrayPtr = 0;
 64   }
 65
 66   *aSize  = 0;
 67   *aCount = 0;
 68   return true;
 69}
 70
 71#else
 72
 73bool VectorResize(U32 *aSize, U32 *aCount, void **arrayPtr, U32 newCount, U32 elemSize)
 74{
 75   PROFILE_SCOPE( VectorResize );
 76
 77   if (newCount > 0)
 78   {
 79      U32 blocks = newCount / VectorBlockSize;
 80      if (newCount % VectorBlockSize)
 81         blocks++;
 82      S32 mem_size = blocks * VectorBlockSize * elemSize;
 83      *arrayPtr = *arrayPtr ? dRealloc(*arrayPtr,mem_size) :
 84         dMalloc(mem_size);
 85
 86      *aCount = newCount;
 87      *aSize = blocks * VectorBlockSize;
 88      return true;
 89   }
 90
 91   if (*arrayPtr) 
 92   {
 93      dFree(*arrayPtr);
 94      *arrayPtr = 0;
 95   }
 96
 97   *aSize = 0;
 98   *aCount = 0;
 99   return true;
100}
101
102#endif
103