// Created on: 2005-03-15
// Created by: Peter KURNEV
// Copyright (c) 1998-1999 Matra Datavision
-// Copyright (c) 1999-2012 OPEN CASCADE SAS
+// Copyright (c) 1999-2014 OPEN CASCADE SAS
//
-// The content of this file is subject to the Open CASCADE Technology Public
-// License Version 6.5 (the "License"). You may not use the content of this file
-// except in compliance with the License. Please obtain a copy of the License
-// at http://www.opencascade.org and read it completely before using this file.
+// This file is part of Open CASCADE Technology software library.
//
-// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
-// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
+// This library is free software; you can redistribute it and/or modify it under
+// the terms of the GNU Lesser General Public License version 2.1 as published
+// by the Free Software Foundation, with special exception defined in the file
+// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
+// distribution for complete text of the license and disclaimer of any warranty.
//
-// The Original Code and all software distributed under the License is
-// distributed on an "AS IS" basis, without warranty of any kind, and the
-// Initial Developer hereby disclaims all such warranties, including without
-// limitation, any warranties of merchantability, fitness for a particular
-// purpose or non-infringement. Please see the License for the specific terms
-// and conditions governing the rights and limitations under the License.
-
-
+// Alternatively, this file may be used under the terms of Open CASCADE
+// commercial license or contractual agreement.
#include <Standard.ixx>
#include <locale.h>
#endif
+#if defined(_MSC_VER) || defined(__ANDROID__)
+ #include <malloc.h>
+#elif (defined(__GNUC__) && __GNUC__ >= 4 && __GNUC_MINOR__ >= 1)
+ #include <mm_malloc.h>
+#else
+ extern "C" int posix_memalign (void** thePtr, size_t theAlign, size_t theSize);
+#endif
+
#ifndef OCCT_MMGT_OPT_DEFAULT
#define OCCT_MMGT_OPT_DEFAULT 0
#endif
// used to construct appropriate memory manager according
// to environment settings, and to ensure destruction upon exit
//=======================================================================
+class Standard_MMgrFactory
+{
+public:
+ static Standard_MMgrRoot* GetMMgr();
+ ~Standard_MMgrFactory();
-class Standard_MMgrFactory {
- public:
+private:
Standard_MMgrFactory();
- ~Standard_MMgrFactory();
- public:
+ Standard_MMgrFactory (const Standard_MMgrFactory&);
+ Standard_MMgrFactory& operator= (const Standard_MMgrFactory&);
+
+private:
Standard_MMgrRoot* myFMMgr;
};
#endif*/
char* aVar;
- Standard_Integer anAllocId = (aVar = getenv ("MMGT_OPT" )) ? atoi (aVar) :
- (OCCT_MMGT_OPT_DEFAULT);
- Standard_Boolean toClear = (aVar = getenv ("MMGT_CLEAR" )) ? (atoi (aVar) != 0) : Standard_True;
+ aVar = getenv ("MMGT_OPT");
+ Standard_Integer anAllocId = (aVar ? atoi (aVar): OCCT_MMGT_OPT_DEFAULT);
+
+#if defined(_WIN32) && !defined(_WIN64)
+ static const DWORD _SSE2_FEATURE_BIT(0x04000000);
+ if ( anAllocId == 2 )
+ {
+ // CR25396: Check if SSE2 instructions are supported, if not then use MMgrRaw
+ // instead of MMgrTBBalloc. It is to avoid runtime crash when running on a
+ // CPU that supports SSE but does not support SSE2 (some modifications of
+ // AMD Sempron).
+ DWORD volatile dwFeature;
+ _asm
+ {
+ push eax
+ push ebx
+ push ecx
+ push edx
+
+ // get the CPU feature bits
+ mov eax, 1
+ cpuid
+ mov dwFeature, edx
+
+ pop edx
+ pop ecx
+ pop ebx
+ pop eax
+ }
+ if ((dwFeature & _SSE2_FEATURE_BIT) == 0)
+ anAllocId = 0;
+ }
+#endif
+
+ aVar = getenv ("MMGT_CLEAR");
+ Standard_Boolean toClear = (aVar ? (atoi (aVar) != 0) : Standard_True);
// on Windows (actual for XP and 2000) activate low fragmentation heap
// for CRT heap in order to get best performance.
{
case 1: // OCCT optimized memory allocator
{
- Standard_Boolean bMMap = (aVar = getenv ("MMGT_MMAP" )) ? (atoi (aVar) != 0) : Standard_True;
- Standard_Integer aCellSize = (aVar = getenv ("MMGT_CELLSIZE" )) ? atoi (aVar) : 200;
- Standard_Integer aNbPages = (aVar = getenv ("MMGT_NBPAGES" )) ? atoi (aVar) : 1000;
- Standard_Integer aThreshold = (aVar = getenv ("MMGT_THRESHOLD")) ? atoi (aVar) : 40000;
+ aVar = getenv ("MMGT_MMAP");
+ Standard_Boolean bMMap = (aVar ? (atoi (aVar) != 0) : Standard_True);
+ aVar = getenv ("MMGT_CELLSIZE");
+ Standard_Integer aCellSize = (aVar ? atoi (aVar) : 200);
+ aVar = getenv ("MMGT_NBPAGES");
+ Standard_Integer aNbPages = (aVar ? atoi (aVar) : 1000);
+ aVar = getenv ("MMGT_THRESHOLD");
+ Standard_Integer aThreshold = (aVar ? atoi (aVar) : 40000);
myFMMgr = new Standard_MMgrOpt (toClear, bMMap, aCellSize, aNbPages, aThreshold);
break;
}
Standard_MMgrFactory::~Standard_MMgrFactory()
{
- if ( myFMMgr ) {
+ if ( myFMMgr )
myFMMgr->Purge(Standard_True);
-// delete myFMMgr;
-// myFMMgr = 0;
- }
}
//=======================================================================
// be counting calls to Allocate() and Free()...
//
//=======================================================================
-
-static Standard_MMgrRoot* GetMMgr()
+Standard_MMgrRoot* Standard_MMgrFactory::GetMMgr()
{
static Standard_MMgrFactory aFactory;
return aFactory.myFMMgr;
Standard_Address Standard::Allocate(const Standard_Size size)
{
- return GetMMgr()->Allocate(size);
+ return Standard_MMgrFactory::GetMMgr()->Allocate(size);
}
//=======================================================================
//purpose :
//=======================================================================
-void Standard::Free(Standard_Address& aStorage)
+void Standard::Free (Standard_Address theStorage)
{
- GetMMgr()->Free(aStorage);
+ Standard_MMgrFactory::GetMMgr()->Free(theStorage);
}
//=======================================================================
//purpose :
//=======================================================================
-Standard_Address Standard::Reallocate(Standard_Address& aStorage,
- const Standard_Size newSize)
+Standard_Address Standard::Reallocate (Standard_Address theStorage,
+ const Standard_Size theSize)
{
- return GetMMgr()->Reallocate(aStorage, newSize);
+ return Standard_MMgrFactory::GetMMgr()->Reallocate (theStorage, theSize);
}
//=======================================================================
Standard_Integer Standard::Purge()
{
- return GetMMgr()->Purge();
+ return Standard_MMgrFactory::GetMMgr()->Purge();
+}
+
+//=======================================================================
+//function : AllocateAligned
+//purpose :
+//=======================================================================
+
+Standard_Address Standard::AllocateAligned (const Standard_Size theSize,
+ const Standard_Size theAlign)
+{
+#if defined(_MSC_VER)
+ return _aligned_malloc (theSize, theAlign);
+#elif defined(__ANDROID__)
+ return memalign (theAlign, theSize);
+#elif (defined(__GNUC__) && __GNUC__ >= 4 && __GNUC_MINOR__ >= 1)
+ return _mm_malloc (theSize, theAlign);
+#else
+ void* aPtr;
+ if (posix_memalign (&aPtr, theAlign, theSize))
+ {
+ return NULL;
+ }
+ return aPtr;
+#endif
+}
+
+//=======================================================================
+//function : FreeAligned
+//purpose :
+//=======================================================================
+
+void Standard::FreeAligned (Standard_Address thePtrAligned)
+{
+#if defined(_MSC_VER)
+ _aligned_free (thePtrAligned);
+#elif defined(__ANDROID__)
+ free (thePtrAligned);
+#elif (defined(__GNUC__) && __GNUC__ >= 4 && __GNUC_MINOR__ >= 1)
+ _mm_free (thePtrAligned);
+#else
+ free (thePtrAligned);
+#endif
}