Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:04:59

0001 // Created on: 2005-03-15
0002 // Created by: Peter KURNEV
0003 // Copyright (c) 2005-2014 OPEN CASCADE SAS
0004 //
0005 // This file is part of Open CASCADE Technology software library.
0006 //
0007 // This library is free software; you can redistribute it and/or modify it under
0008 // the terms of the GNU Lesser General Public License version 2.1 as published
0009 // by the Free Software Foundation, with special exception defined in the file
0010 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0011 // distribution for complete text of the license and disclaimer of any warranty.
0012 //
0013 // Alternatively, this file may be used under the terms of Open CASCADE
0014 // commercial license or contractual agreement.
0015 
0016 #ifndef _Standard_MMgrOpt_HeaderFile
0017 #define _Standard_MMgrOpt_HeaderFile
0018 
0019 #include <Standard_MMgrRoot.hxx>
0020 #include <Standard_Mutex.hxx>
0021 
0022 /**
0023 * @brief Open CASCADE memory manager optimized for speed.
0024 *
0025 * The behaviour is different for memory blocks of different sizes,
0026 * according to specified options provided to constructor:
0027 *
0028 * - Small blocks with size less than or equal to aCellSize are allocated 
0029 *   in big pools of memory. The parameter aNbPages specifies size of 
0030 *   these pools in pages (operating system-dependent). 
0031 *   When freed, small block is not returned to the system but added
0032 *   into free blocks list and reused when block of the same size is 
0033 *   requested.
0034 * 
0035 * - Medium size blocks with size less than aThreshold are allocated 
0036 *   using malloc() or calloc() function but not returned to the system
0037 *   when method Free() is called; instead they are put into free list
0038 *   and reused when block of the same size is requested.
0039 *   Blocks of medium size stored in free lists can be released to the 
0040 *   system (by free()) by calling method Purge().
0041 *
0042 * - Large blocks with size greater than or equal to aThreshold are allocated 
0043 *   and freed directly: either using malloc()/calloc() and free(), or using 
0044 *   memory mapped files (if option aMMap is True)
0045 *   
0046 * Thus the optimization of memory allocation/deallocation is reached 
0047 * for small and medium size blocks using free lists method; 
0048 * note that space allocated for small blocks cannot be (currently) released
0049 * to the system while space for medium size blocks can be released by method Purge().
0050 *
0051 * Note that destructor of that class frees all free lists and memory pools 
0052 * allocated for small blocks.
0053 * 
0054 * Note that size of memory blocks allocated by this memory manager is always 
0055 * rounded up to 16 bytes. In addition, 8 bytes are added at the beginning 
0056 * of the memory block to hold auxiliary information (size of the block when
0057 * in use, or pointer to the next free block when in free list).
0058 * This the expense of speed optimization. At the same time, allocating small 
0059 * blocks is usually less costly than directly by malloc since allocation is made
0060 * once (when allocating a pool) and overheads induced by malloc are minimized.
0061 */
0062 class Standard_MMgrOpt : public Standard_MMgrRoot
0063 {
0064  public:
0065   
0066   //! Constructor. If aClear is True, the allocated emmory will be 
0067   //! nullified. For description of other parameters, see description 
0068   //! of the class above.
0069   Standard_EXPORT Standard_MMgrOpt
0070                         (const Standard_Boolean aClear      = Standard_True,
0071                          const Standard_Boolean aMMap       = Standard_True,
0072                          const Standard_Size    aCellSize   = 200,
0073                          const Standard_Integer aNbPages    = 10000,
0074                          const Standard_Size    aThreshold  = 40000);
0075 
0076   //! Frees all free lists and pools allocated for small blocks 
0077   Standard_EXPORT virtual ~Standard_MMgrOpt();
0078   
0079   //! Allocate aSize bytes; see class description above
0080   Standard_EXPORT virtual Standard_Address Allocate(const Standard_Size aSize);
0081   
0082   //! Reallocate previously allocated aPtr to a new size; new address is returned.
0083   //! In case that aPtr is null, the function behaves exactly as Allocate.
0084   Standard_EXPORT virtual Standard_Address Reallocate (Standard_Address thePtr, 
0085                                                        const Standard_Size theSize);
0086   
0087   //! Free previously allocated block.
0088   //! Note that block can not all blocks are released to the OS by this 
0089   //! method (see class description)
0090   Standard_EXPORT virtual void Free (Standard_Address thePtr);
0091   
0092   //! Release medium-sized blocks of memory in free lists to the system.
0093   //! Returns number of actually freed blocks
0094   Standard_EXPORT virtual Standard_Integer Purge(Standard_Boolean isDestroyed);
0095 
0096   //! Declaration of a type pointer to the callback function that should accept the following arguments:
0097   //! @param theIsAlloc   true if the data is allocated, false if it is freed
0098   //! @param theStorage   address of the allocated/freed block
0099   //! @param theRoundSize the real rounded size of the block
0100   //! @param theSize      the size of the block that was requested by application (this value is correct only if theIsAlloc is true)
0101   typedef void (*TPCallBackFunc)(const Standard_Boolean theIsAlloc,
0102                                  const Standard_Address theStorage,
0103                                  const Standard_Size theRoundSize,
0104                                  const Standard_Size theSize);
0105 
0106   //! Set the callback function. You may pass 0 there to turn off the callback.
0107   //! The callback function, if set, will be automatically called from within
0108   //! Allocate and Free methods.
0109   Standard_EXPORT static void SetCallBackFunction(TPCallBackFunc pFunc);
0110 
0111 protected:
0112  
0113   //! Internal - initialization of buffers
0114   Standard_EXPORT void Initialize();
0115 
0116   //! Internal - allocation of memory using either malloc or memory mapped files.
0117   //! The size of the actually allocated block may be greater than requested one
0118   //! when memory mapping is used, since it is aligned to page size 
0119   Standard_Size* AllocMemory (Standard_Size &aSize);
0120   
0121   //! Internal - deallocation of memory taken by AllocMemory
0122   void FreeMemory (Standard_Address aPtr, const Standard_Size aSize);
0123   
0124   //! Internal - free memory pools allocated for small size blocks
0125   void FreePools();
0126 
0127  protected:
0128   Standard_Boolean myClear;         //!< option to clear allocated memory
0129   
0130   Standard_Size    myFreeListMax;   //!< last allocated index in the free blocks list
0131   Standard_Size ** myFreeList;      //!< free blocks list 
0132 
0133   Standard_Size    myCellSize;      //!< small blocks size
0134   Standard_Integer myNbPages;       //!< size (pages) for small block memory pools
0135   Standard_Size    myPageSize;      //!< system-dependent memory page size
0136   Standard_Size *  myAllocList;     //!< list of memory pools for small blocks
0137   Standard_Size *  myNextAddr;      //!< next free address in the active memory pool
0138   Standard_Size *  myEndBlock;      //!< end of the active memory pool
0139   
0140   Standard_Integer myMMap;          //!< non-null if using memory mapped files for allocation of large blocks
0141   Standard_Size    myThreshold;     //!< large block size  
0142   
0143   Standard_Mutex   myMutex;         //!< Mutex to protect free lists data
0144   Standard_Mutex   myMutexPools;    //!< Mutex to protect small block pools data
0145 };
0146 
0147 #endif