0025215: Porting to Android - fix minor issues
[occt.git] / src / Standard / Standard_MMgrOpt.cxx
old mode 100755 (executable)
new mode 100644 (file)
index bf34b1f..49652da
@@ -1,57 +1,30 @@
 // Created on: 2005-03-15
 // Created by: Peter KURNEV
-// Copyright (c) 2005-2012 OPEN CASCADE SAS
+// Copyright (c) 2005-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_MMgrOpt.hxx>
 #include <Standard_OutOfMemory.hxx>
-
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
+#include <Standard_Assert.hxx>
 
 #include <stdio.h>
 
-#ifdef HAVE_STRING_H
-# include <string.h>
-#endif
-
-#ifndef WNT
-# include <stdlib.h>
-# include <errno.h>
-#endif
-
-#ifdef WNT
-#include <windows.h>
+#ifdef _WIN32
+# include <windows.h>
 #else
-# ifdef HAVE_UNISTD_H
-#  include <unistd.h>
-# endif
-# ifdef HAVE_SYS_MMAN_H
-#  include <sys/mman.h>    /* mmap() */
-# endif
+# include <sys/mman.h>    /* mmap() */
 #endif
-#ifdef HAVE_MALLOC_H
-# include <malloc.h>
-#endif
-#include <stdlib.h>
-#include <sys/types.h>
-#include <sys/stat.h>
+
 #include <fcntl.h>
 //
 #if defined (__sun) || defined(SOLARIS)
@@ -139,24 +112,6 @@ extern "C" int getpagesize() ;
 #define ROUNDDOWN_CELL(size)           ROUNDDOWN8(size)
 #define INDEX_CELL(rsize)              ((rsize) >> 3)
 
-// Minimal granularity: 4 bytes (32-bit systems only)
-#ifndef _OCC64
-//#define ROUNDUP_CELL(size)             ROUNDUP4(size)
-//#define INDEX_CELL(rsize)              ((rsize) >> 2)
-#endif
-
-// Adaptive granularity, less for little blocks and greater for bigger ones: 
-/*
-#if _OCC64
-#define ROUNDUP_CELL(size) ((size)  <= 0x40 ? ROUNDUP8(size) : ROUNDUP16(size))
-#define INDEX_CELL(rsize)  ((rsize) <= 0x40 ? ((rsize) >> 3) : (4 + ((rsize) >> 4)))
-#else
-#define ROUNDUP_CELL(size) ((size)  <= 0x40 ? ROUNDUP4(size) : ROUNDUP8(size))
-#define INDEX_CELL(rsize)  ((rsize) <= 0x40 ? ((rsize) >> 2) : (8 + ((rsize) >> 3)))
-#endif
-*/
-
-
 /* In the allocated block, first bytes are used for storing of memory manager's data.
    (size of block). The minimal size of these data is sizeof(int).
    The memory allocated in system usually alligned by 16 bytes.Tthe aligment of the 
@@ -192,11 +147,7 @@ Standard_MMgrOpt::Standard_MMgrOpt(const Standard_Boolean aClear,
                                    const Standard_Size aThreshold)
 {
   // check basic assumption
-  if ( sizeof(Standard_Size) != sizeof(Standard_Address) )
-  {
-    cerr << "Fatal error: Open CASCADE Optimized Memory manager: this platform is not supported!" << endl;
-    exit(1);
-  }
+  Standard_STATIC_ASSERT(sizeof(Standard_Size) == sizeof(Standard_Address));
 
   // clear buffer fields
   myFreeListMax = 0;
@@ -296,7 +247,7 @@ void Standard_MMgrOpt::Initialize()
       perror("ERR_MEMRY_FAIL");
 #endif
     
-#if defined(IRIX) || defined(__sgi) || defined(SOLARIS) || defined(__sun) || defined(LIN) || defined(linux) || defined(__FreeBSD__)
+#if defined(IRIX) || defined(__sgi) || defined(SOLARIS) || defined(__sun) || defined(LIN) || defined(linux) || defined(__FreeBSD__) || defined(__ANDROID__)
     if ((myMMap = open ("/dev/zero", O_RDWR)) < 0) {
       if ((myMMap = open ("/dev/null", O_RDWR)) < 0){
         myMMap = 0;
@@ -484,7 +435,7 @@ Standard_Address Standard_MMgrOpt::Allocate(const Standard_Size aSize)
 //purpose  : 
 //=======================================================================
 
-void Standard_MMgrOpt::Free(Standard_Address& theStorage)
+void Standard_MMgrOpt::Free(Standard_Address theStorage)
 {
   // safely return if attempt to free null pointer
   if ( ! theStorage )
@@ -519,8 +470,6 @@ void Standard_MMgrOpt::Free(Standard_Address& theStorage)
   // otherwise, we have block of big size which shall be simply released
   else 
     FreeMemory (aBlock, RoundSize);
-
-  theStorage = NULL;
 }
 
 //=======================================================================
@@ -713,7 +662,7 @@ void Standard_MMgrOpt::FreePools()
 //purpose  : 
 //=======================================================================
 
-Standard_Address Standard_MMgrOpt::Reallocate(Standard_Address&   theStorage,
+Standard_Address Standard_MMgrOpt::Reallocate(Standard_Address theStorage,
                                               const Standard_Size theNewSize)
 {
   // if theStorage == NULL, just allocate new memory block
@@ -741,7 +690,6 @@ Standard_Address Standard_MMgrOpt::Reallocate(Standard_Address&   theStorage,
     if ( myClear )
       memset(((char*)newStorage) + OldSize, 0, theNewSize-OldSize);
   }
-  theStorage = NULL;
   return newStorage;
 }
 
@@ -793,10 +741,10 @@ retry:
                                     PAGE_READWRITE,
                                     DWORD(AlignedSize / 0x80000000),
                                     DWORD(AlignedSize % 0x80000000), NULL); 
-    HANDLE * aMBlock = NULL;
+    HANDLE * aMBlock = (hMap && GetLastError() != ERROR_ALREADY_EXISTS ? 
+                        (HANDLE*)MapViewOfFile(hMap,FILE_MAP_WRITE,0,0,0) : NULL);
     // check for error and try allocating address space
-    if ( ! hMap || GetLastError() == ERROR_ALREADY_EXISTS ||
-         ! ((aMBlock = (HANDLE*)MapViewOfFile(hMap,FILE_MAP_WRITE,0,0,0))) ) 
+    if ( ! aMBlock ) 
     {
       // close handle if allocated
       if ( hMap )