TKernel模块--杂项

TKernel模块–杂项

1.DEFINE_HARRAY1

#define DEFINE_HARRAY1(HClassName, _Array1Type_)                               \
class HClassName : public _Array1Type_, public Standard_Transient {            \public:                                                                       \DEFINE_STANDARD_ALLOC                                                       \DEFINE_NCOLLECTION_ALLOC                                                    \HClassName () : _Array1Type_ () {}                                          \HClassName (const Standard_Integer theLower,                                \const Standard_Integer theUpper) :                              \_Array1Type_ (theLower,theUpper)  {}                                      \HClassName (const Standard_Integer theLower,                                \const Standard_Integer theUpper,                                \const _Array1Type_::value_type& theValue) :                     \_Array1Type_ (theLower,theUpper)  { Init (theValue); }                    \explicit HClassName (const typename _Array1Type_::value_type& theBegin,     \const Standard_Integer theLower,                                \const Standard_Integer theUpper,                                \const bool) :                                                   \_Array1Type_ (theBegin,theLower,theUpper)  {}                             \HClassName  (const _Array1Type_& theOther) : _Array1Type_(theOther) {}      \const _Array1Type_& Array1 () const { return *this; }                       \_Array1Type_& ChangeArray1 ()       { return *this; }                       \DEFINE_STANDARD_RTTI_INLINE(HClassName,Standard_Transient)                  \
};                                                                             \
DEFINE_STANDARD_HANDLE (HClassName, Standard_Transient)#define IMPLEMENT_HARRAY1(HClassName)

看着是定义一个基于参数2的派生类,提供类似的功能。

2.DEFINE_HASHER

#define DEFINE_HASHER(HasherName, TheKeyType, HashFunctor, EqualFunctor)  \
struct HasherName : protected HashFunctor, EqualFunctor                   \
{                                                                         \size_t operator()(const TheKeyType& theKey) const noexcept              \{                                                                       \return HashFunctor::operator()(theKey);                               \}                                                                       \\bool operator() (const TheKeyType& theK1,                               \const TheKeyType& theK2) const noexcept                \{                                                                       \return EqualFunctor::operator()(theK1, theK2);                        \}                                                                       \
};

集成取哈希值,键比较功能

3.DEFINE_HSEQUENCE

#define DEFINE_HSEQUENCE(HClassName, _SequenceType_)                           \
class HClassName : public _SequenceType_, public Standard_Transient {                \public:                                                                       \DEFINE_STANDARD_ALLOC                                                       \DEFINE_NCOLLECTION_ALLOC                                                    \HClassName () {}                                                            \HClassName (const _SequenceType_& theOther) : _SequenceType_(theOther) {}   \const _SequenceType_& Sequence () const { return *this; }                   \void Append (const _SequenceType_::value_type& theItem) {                   \_SequenceType_::Append (theItem);                                         \}                                                                           \void Append (_SequenceType_& theSequence) {                                 \_SequenceType_::Append (theSequence);                                     \}                                                                           \_SequenceType_& ChangeSequence ()       { return *this; }                   \template <class T>                                                          \void Append (const Handle(T)& theOther,                                     \typename opencascade::std::enable_if<opencascade::std::is_base_of<HClassName, T>::value>::type * = 0) { \_SequenceType_::Append (theOther->ChangeSequence());                      \}                                                                           \DEFINE_STANDARD_RTTI_INLINE(HClassName,Standard_Transient)                             \
}; \
DEFINE_STANDARD_HANDLE (HClassName, Standard_Transient) #define IMPLEMENT_HSEQUENCE(HClassName)         

为参数2提供派生类,提供类似功能

4.NCollection_Handle

template <class T>
class NCollection_Handle : public opencascade::handle<Standard_Transient> {
private:class Ptr : public Standard_Transient {public:Ptr (T* theObj) : myPtr (theObj) {}~Ptr () { if ( myPtr ) delete myPtr; myPtr = 0; }protected:Ptr(const Ptr&);Ptr& operator=(const Ptr&);public:T* myPtr; };NCollection_Handle (Ptr* thePtr, int) : opencascade::handle<Standard_Transient> (thePtr) {}public:typedef T element_type;NCollection_Handle () {}NCollection_Handle (T* theObject) : opencascade::handle<Standard_Transient> (theObject ? new Ptr (theObject) : 0) {}T* get () { return ((Ptr*)opencascade::handle<Standard_Transient>::get())->myPtr; }const T* get () const { return ((Ptr*)opencascade::handle<Standard_Transient>::get())->myPtr; }T* operator -> () { return get(); }const T* operator -> () const { return get(); }T& operator * () { return *get(); }const T& operator * () const { return *get(); }static NCollection_Handle<T> DownCast (const opencascade::handle<Standard_Transient>& theOther) {return NCollection_Handle<T>(dynamic_cast<Ptr*>(theOther.get()), 0);}
};

做的还是为原始指针做包装。opencascade::handle<Standard_Transient>已经是一层包装,为原始指针加引用计数管理。这里本质还是在包装。

5.NCollection_Shared

template <class T, typename = typename opencascade::std::enable_if<! opencascade::std::is_base_of<Standard_Transient, T>::value>::type>
class NCollection_Shared : public Standard_Transient, public T {
public:DEFINE_STANDARD_ALLOCDEFINE_NCOLLECTION_ALLOCNCollection_Shared () {}template<typename T1> NCollection_Shared (const T1& arg1) : T(arg1) {}template<typename T1> NCollection_Shared (T1& arg1) : T(arg1) {}template<typename T1, typename T2> NCollection_Shared (const T1& arg1, const T2& arg2) : T(arg1, arg2) {}template<typename T1, typename T2> NCollection_Shared (T1& arg1, const T2& arg2) : T(arg1, arg2) {}template<typename T1, typename T2> NCollection_Shared (const T1& arg1, T2& arg2) : T(arg1, arg2) {}template<typename T1, typename T2> NCollection_Shared (T1& arg1, T2& arg2) : T(arg1, arg2) {}
};

NCollection_Shared 是 OpenCASCADE (OCCT) 中的一个模板类,它提供了一种将非继承自 Standard_Transient 的类包装成可共享对象的方式。

这个类的主要目的是让那些原本不继承 Standard_Transient 的类能够获得引用计数和自动内存管理的功能。这在需要共享对象所有权的情况下非常有用。

6.Standard_MMgrRoot

class Standard_MMgrRoot {
public:Standard_EXPORT virtual ~Standard_MMgrRoot(){}Standard_EXPORT virtual Standard_Address Allocate (const Standard_Size theSize)=0;Standard_EXPORT virtual Standard_Address Reallocate (Standard_Address thePtr, const Standard_Size theSize)=0;Standard_EXPORT virtual void Free(Standard_Address thePtr)=0;Standard_EXPORT virtual Standard_Integer Purge(Standard_Boolean isDestroyed=Standard_False){return 0;}
};

7.Standard_MMgrOpt

if defined(__linux__)#define MMAP_BASE_ADDRESS 0x20000000#define MMAP_FLAGS (MAP_PRIVATE)
end
// 将给定的尺寸向上对齐到页面尺寸倍数
#define PAGE_ALIGN(size,thePageSize)                            \(((size) + (thePageSize) - 1) &  ~((thePageSize) - 1))
// 将给定的尺寸向上对齐到16的倍数
#define ROUNDUP16(size)                (((size) + 0xf) & ~(Standard_Size)0xf)
// 将给定的尺寸向上对齐到8的倍数
#define ROUNDUP8(size)                 (((size) + 0x7) & ~(Standard_Size)0x7)
// 将给定的尺寸向上对齐到4的倍数
#define ROUNDUP4(size)                 (((size) + 0x3) & ~(Standard_Size)0x3)
// 将给定的尺寸向下对齐到8的倍数
#define ROUNDDOWN8(size)               ((size) & ~(Standard_Size)0x7)
// 将给定的尺寸向上对齐到8的倍数
#define ROUNDUP_CELL(size)             ROUNDUP8(size)
// 将给定的尺寸向下对齐到8的倍数
#define ROUNDDOWN_CELL(size)           ROUNDDOWN8(size)
// 给定尺寸,计算其被8所除的结果整数部分
#define INDEX_CELL(rsize)              ((rsize) >> 3)#define BLOCK_SHIFT 1
// 前进一个Standard_Size大小,从块地址得到可用内存地址
#define GET_USER(block)    (((Standard_Size*)(block)) + BLOCK_SHIFT)
// 后退一个Standard_Size大小,从可用内存地址得到块地址
#define GET_BLOCK(storage) (((Standard_Size*)(storage))-BLOCK_SHIFT)
class Standard_MMgrOpt : public Standard_MMgrRoot {protected:Standard_Boolean myClear;        Standard_Size    myFreeListMax; Standard_Size ** myFreeList;     Standard_Size    myCellSize;    Standard_Integer myNbPages;      Standard_Size    myPageSize;    Standard_Size *  myAllocList;   Standard_Size *  myNextAddr;     Standard_Size *  myEndBlock;    Standard_Integer myMMap;        Standard_Size    myThreshold;    Standard_Mutex   myMutex;        Standard_Mutex   myMutexPools;  
public:Standard_EXPORT Standard_MMgrOpt(const Standard_Boolean aClear = Standard_True,const Standard_Boolean aMMap = Standard_True, const Standard_Size aCellSize = 200,const Standard_Integer aNbPages = 10000, const Standard_Size aThreshold  = 40000){Standard_STATIC_ASSERT(sizeof(Standard_Size) == sizeof(Standard_Address));myFreeListMax = 0;myFreeList = NULL;myPageSize = 0;myAllocList = NULL;myNextAddr = NULL;myEndBlock = NULL;myClear = aClear;myMMap = (Standard_Integer)aMMap;myCellSize = aCellSize;myNbPages = aNbPages;myThreshold = aThreshold;Initialize();	}
protected:Standard_EXPORT void Initialize(){if ( myNbPages < 100 ) myNbPages = 1000;#ifndef _WIN32myPageSize = getpagesize();if ( ! myPageSize )myMMap = 0;#elseSYSTEM_INFO SystemInfo;GetSystemInfo (&SystemInfo);myPageSize = SystemInfo.dwPageSize;#endifif(myMMap) {myMMap = -1;}myFreeListMax = INDEX_CELL(ROUNDUP_CELL(myThreshold-BLOCK_SHIFT)); // all blocks less than myThreshold are to be recycled// 每个链表负责维护固定尺寸内存块分配和回收?// 链表负责的内存块尺寸以8为刻度递增myFreeList = (Standard_Size **) calloc (myFreeListMax+1, sizeof(Standard_Size *));myCellSize = ROUNDUP16(myCellSize);}
public:Standard_EXPORT virtual Standard_Integer Purge(Standard_Boolean isDestroyed){Standard_Mutex::Sentry aSentry (myMutex);Standard_Integer nbFreed = 0;// 快速定位到myCellSize尺寸最低可分配的链表索引Standard_Size i = INDEX_CELL(ROUNDUP_CELL(myCellSize+BLOCK_SHIFT));// 从该链表到后续各个链表内所有块依次释放for (; i <= myFreeListMax; i++ ) {// 获得链表首个块地址Standard_Size * aFree = myFreeList[i];      while(aFree) {// 保存当前块地址Standard_Size * anOther = aFree;// 移动到下一个块。能如此的原因:每个内存块首个位置存放下一个块的地址。aFree = * (Standard_Size **) aFree;// 释放块free(anOther); nbFreed++;}// 释放后链表置空myFreeList[i] = NULL;}Standard_Mutex::Sentry aSentry1 (myMutexPools);#ifndef _WIN32const Standard_Size PoolSize = myPageSize * myNbPages;#else// 内存池大小const Standard_Size PoolSize = PAGE_ALIGN(myPageSize * myNbPages + sizeof(HANDLE), myPageSize) - sizeof(HANDLE);#endif// 向下规整const Standard_Size RPoolSize = ROUNDDOWN_CELL(PoolSize);// 一个池可容纳的Standard_Size对象个数const Standard_Size PoolSizeN = RPoolSize / sizeof(Standard_Size);static const Standard_Integer NB_POOLS_WIN = 512;static Standard_Size* aPools[NB_POOLS_WIN];static Standard_Size aFreeSize[NB_POOLS_WIN];static Standard_Integer aFreePools[NB_POOLS_WIN];Standard_Size * aNextPool = myAllocList;Standard_Size * aPrevPool = NULL;// 给定尺寸得到此尺寸的索引const Standard_Size nCells = INDEX_CELL(myCellSize);Standard_Integer nPool = 0, nPoolFreed = 0;// 每个内存块作为一个Pool?while (aNextPool) {Standard_Integer iPool;for (iPool = 0; aNextPool && iPool < NB_POOLS_WIN; iPool++) {aPools[iPool] = aNextPool;aFreeSize[iPool] = 0;aNextPool = * (Standard_Size **) aNextPool; // get next pool}const Standard_Integer iLast = iPool - 1;(void )nPool; // unused but set for debugnPool += iPool;// 块的个数// 完成小块链表释放for (i = 0; i <= nCells; i++ ) {// 负责小块内存分配的块列表Standard_Size * aFree = myFreeList[i];Standard_Size aSize = BLOCK_SHIFT * sizeof(Standard_Size) + ROUNDUP_CELL(1) * i;while(aFree) {for (iPool = 0; iPool <= iLast; iPool++) {// 若是此个地址块落在内存池块内if (aFree >= aPools[iPool] && aFree < aPools[iPool] + PoolSizeN) {aFreeSize[iPool] += aSize;// 此内存池块内可分配内存增加break;}}aFree = * (Standard_Size **) aFree; // 继续前进到链表下一个小块}}Standard_Integer iLastFree = -1;for (iPool = 0; iPool <= iLast; iPool++) {// 可分配尺寸向上对齐aFreeSize[iPool] = ROUNDUP_CELL(aFreeSize[iPool]);if (aFreeSize[iPool] == RPoolSize)aFreePools[++iLastFree] = iPool;// 得到一个完全空闲的快池}if (iLastFree == -1) {// 无法得到一个完全空闲的块池aPrevPool = aPools[iLast];continue;}Standard_Integer j;for (i = 0; i <= nCells; i++ ) {Standard_Size * aFree = myFreeList[i];Standard_Size * aPrevFree = NULL;while(aFree) {// 若小块落在完全空闲块池for (j = 0; j <= iLastFree; j++) {iPool = aFreePools[j];if (aFree >= aPools[iPool] && aFree < aPools[iPool] + PoolSizeN)break;}if (j <= iLastFree) {// 表示小块落在完全空闲块池// 将此小块从链表移除aFree = * (Standard_Size **) aFree;if (aPrevFree)* (Standard_Size **) aPrevFree = aFree; elsemyFreeList[i] = aFree;nbFreed++;}else {// 表示小块未落在完全空闲块池aPrevFree = aFree;// 取得小块地址aFree = * (Standard_Size **) aFree;// 继续分析下一个小块}}}Standard_Size * aPrev = (aFreePools[0] == 0 ? aPrevPool : aPools[aFreePools[0] - 1]);for (j = 0; j <= iLastFree; j++) {iPool = aFreePools[j];if (j > 0) {if (iPool - aFreePools[j - 1] > 1)aPrev = aPools[iPool - 1];}if (j == iLastFree || aFreePools[j + 1] - iPool > 1) {Standard_Size * aNext = (j == iLastFree && aFreePools[j] == iLast) ? aNextPool : aPools[iPool + 1];if (aPrev)* (Standard_Size **) aPrev = aNext;elsemyAllocList = aNext;}FreeMemory(aPools[iPool], PoolSize);}aPrevPool = (aFreePools[iLastFree] == iLast ? aPrev : aPools[iLast]);(void )nPoolFreed; // unused but set for debugnPoolFreed += iLastFree + 1;}return nbFreed;}void FreeMemory (Standard_Address aPtr, const Standard_Size aSize){if ( myMMap ) {#ifndef _WIN32const Standard_Size AlignedSize = PAGE_ALIGN(aSize, myPageSize);munmap((char*)aBlock, AlignedSize);#elseconst HANDLE * aMBlock = (const HANDLE *)aBlock;HANDLE hMap = *(--aMBlock);UnmapViewOfFile((LPCVOID)aMBlock);CloseHandle (hMap);#endif}elsefree(aBlock);}void FreePools(){Standard_Mutex::Sentry aSentry (myMutexPools);Standard_Size * aFree = myAllocList;myAllocList = 0;while (aFree) {Standard_Size * aBlock = aFree;aFree = * (Standard_Size **) aFree;FreeMemory ( aBlock, myPageSize * myNbPages );}}Standard_EXPORT virtual ~Standard_MMgrOpt(){Purge(Standard_True);free(myFreeList);FreePools();}public:// 分析内存分配实现策略Standard_EXPORT virtual Standard_Address Allocate(const Standard_Size aSize){Standard_Size * aStorage = NULL;// 尺寸向上对齐到倍数volatile Standard_Size RoundSize = ROUNDUP_CELL(aSize);// 可以满足此尺寸的最小索引const Standard_Size Index = INDEX_CELL(RoundSize);// 这里弄了个myFreeListMax这个概念。多级链表,由不同尺寸内存块构成的链表。// myFreeListMax这个概念用于实现小块内存的回收复用。从而避免小块内存分配时执行系统调用malloc/free,提升性能// 系统调用涉及内核态用户态转换,上下文恢复较纯应用层调用耗时会更多if ( Index <= myFreeListMax ) {// 对齐后尺寸能容纳的Standard_Size对象个数const Standard_Size RoundSizeN = RoundSize / sizeof(Standard_Size);// 核心分配过程做了互斥保护myMutex.Lock();// 定位到的链表是否有元素if ( myFreeList[Index] ) {// 取得链表首个元素地址Standard_Size* aBlock = myFreeList[Index];// 将首个元素分配出去,更新链表首个元素地址myFreeList[Index] = *(Standard_Size**)aBlock;myMutex.Unlock();// 基类分配到的内存块尺寸aBlock[0] = RoundSize;// 从块地址得到可用内存地址aStorage = GET_USER(aBlock);if (myClear)memset (aStorage, 0, RoundSize);// 内存部分需要清理}// 对应的小块链表为孔,且对齐后尺寸不超过myCellSizeelse if ( RoundSize <= myCellSize ) {myMutex.Unlock();// 这里使用多个锁,实现细粒度的互斥保护,有利于提升并发性能。Standard_Mutex::Sentry aSentry (myMutexPools);// myNextAddr,myEndBlock机制// 在多级小块内存复用链表机制外,对于较小尺寸内存的分配额外引用直接分配机制。// 提供一个较大块,当较小块分配时,定位到其复用块链表为空时,尝试从此较大块直接分配出一个小块。Standard_Size *aBlock = myNextAddr;// 当大块剩余部分不足完成本次分配if ( &aBlock[ BLOCK_SHIFT+RoundSizeN] > myEndBlock ) {// 进入到这里是发现此较大块剩余部分不足完成本次分配。// 采取的策略是重新分配一个较大块Standard_Size Size = myPageSize * myNbPages;aBlock = AllocMemory(Size);// 若当前较大块剩余部分尚有内存空间可供分配if (myEndBlock > myNextAddr) {// 剩余部分中可用内存尺寸const Standard_Size aPSize = (myEndBlock - GET_USER(myNextAddr)) * sizeof(Standard_Size);// 内存尺寸向下对齐const Standard_Size aRPSize = ROUNDDOWN_CELL(aPSize);// 内存尺寸对应的多级小块链表索引const Standard_Size aPIndex = INDEX_CELL(aRPSize);// 若此索引属于合法索引范围if ( aPIndex > 0 && aPIndex <= myFreeListMax ) {myMutex.Lock();// 头插法加入到对应小块链表// 这里的一个灵活多变。// 大尺寸块可分分割为多个小尺寸块。// 大尺寸块的结构是:// 4字节下一个大尺寸块地址+多个小尺寸块// 小尺寸块结构是:// 头4字节+可用内存区域// 在小尺寸块位于回收链表时,其头4字节指向链表下一个小尺寸块的地址// 在小尺寸块被分配出去时,其头4字节存储了此块内已经分配内存尺寸。*(Standard_Size**)myNextAddr = myFreeList[aPIndex];myFreeList[aPIndex] = myNextAddr;myMutex.Unlock();}}// 计算新块的结束位置myEndBlock = aBlock + Size / sizeof(Standard_Size);// 将新的较大块加入myAllocList链表// myAllocList机制:由较大块构成的分配链表。*(Standard_Size**)aBlock = myAllocList;myAllocList = aBlock;// 原始块的布局:// 4字节为下一个块的地址+4字节为本块已经分配出去内存尺寸+可用内存区域aBlock+=BLOCK_SHIFT;}// 记录本块内已经分配内存尺寸aBlock[0] = RoundSize;// 从块得到可用内存区域地址aStorage = GET_USER(aBlock);// 下一个可供分配的地址myNextAddr = &aStorage[RoundSizeN];}// 对应的小块链表为孔,且对齐后尺寸超过myCellSize。// myCellSize属于中尺寸和大尺寸分界线。else {myMutex.Unlock();// 这里相当于处理大尺寸块的分配// 直接通过系统api完成分配Standard_Size *aBlock = (Standard_Size*) (myClear ? calloc( RoundSizeN+BLOCK_SHIFT,   sizeof(Standard_Size)) : malloc((RoundSizeN+BLOCK_SHIFT) * sizeof(Standard_Size)) );// 直到无法完成分配时,才执行清理,释放多余空闲内存。// 清理的时机有点晚了。if ( ! aBlock ) {if ( Purge (Standard_False) )aBlock = (Standard_Size*)calloc(RoundSizeN+BLOCK_SHIFT, sizeof(Standard_Size));if ( ! aBlock )throw Standard_OutOfMemory("Standard_MMgrOpt::Allocate(): malloc failed");}// 直接分配的大尺寸块的结构:// 4字节存储块中已经分配内存尺寸+可用内存区域aBlock[0] = RoundSize;// 可用内存区域指针aStorage = GET_USER(aBlock);}}// 索引直接超过了myFreeListMax,属于超大块了。// myFreeListMax构成了大块和超大块的分水岭。else {Standard_Size AllocSize = RoundSize + sizeof(Standard_Size);// 通过AllocMemory完成超大快的分配Standard_Size* aBlock = AllocMemory(AllocSize);aBlock[0] = RoundSize;aStorage = GET_USER(aBlock);}// 每次分配成功都触发一次回调。参数3是对齐后尺寸,参数4是原始尺寸。callBack(Standard_True, aStorage, RoundSize, aSize);return aStorage;}// 释放+重新分配Standard_EXPORT virtual Standard_Address Reallocate (Standard_Address theStorage, const Standard_Size theNewSize){if (!theStorage) {return Allocate(theNewSize);}// 得到块起始地址Standard_Size * aBlock = GET_BLOCK(theStorage);Standard_Address newStorage = NULL;// 块内已经使用内存尺寸Standard_Size OldSize = aBlock[0];if (theNewSize <= OldSize) {// 复用newStorage = theStorage;}else {newStorage = Allocate(theNewSize);memcpy (newStorage, theStorage, OldSize);Free( theStorage );if ( myClear )memset(((char*)newStorage) + OldSize, 0, theNewSize-OldSize);}return newStorage;}Standard_EXPORT virtual void Free (Standard_Address thePtr){if ( ! theStorage )return;Standard_Size* aBlock = GET_BLOCK(theStorage);Standard_Size RoundSize = aBlock[0];// 释放时也触发回调callBack(Standard_False, theStorage, RoundSize, 0);const Standard_Size Index = INDEX_CELL(RoundSize);if ( Index <= myFreeListMax ) {myMutex.Lock();// 头插法加入链表*(Standard_Size**)aBlock = myFreeList[Index];myFreeList[Index] = aBlock;myMutex.Unlock();}else FreeMemory (aBlock, RoundSize);// 直接释放}typedef void (*TPCallBackFunc)(const Standard_Boolean theIsAlloc, const Standard_Address theStorage, const Standard_Size theRoundSize, const Standard_Size theSize);Standard_EXPORT static void SetCallBackFunction(TPCallBackFunc pFunc);// 内存分配Standard_Size* AllocMemory (Standard_Size &aSize){retry:Standard_Size * aBlock = NULL;// 使用内存映射完成分配if (myMMap) {#ifndef _WIN32const Standard_Size AlignedSize = PAGE_ALIGN(Size, myPageSize);aBlock = (Standard_Size * )mmap((char*)MMAP_BASE_ADDRESS, AlignedSize, PROT_READ | PROT_WRITE, MMAP_FLAGS, myMMap, 0);if (aBlock == MAP_FAILED /* -1 */) {int errcode = errno;if ( Purge(Standard_False) )goto retry;throw Standard_OutOfMemory(strerror(errcode));}Size = AlignedSize;#else /* _WIN32 */const Standard_Size AlignedSize = PAGE_ALIGN(Size+sizeof(HANDLE), myPageSize);// 内存映射得到内存HANDLE hMap = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, DWORD(AlignedSize / 0x80000000), DWORD(AlignedSize % 0x80000000), NULL); // 获得句柄HANDLE * aMBlock = (hMap && GetLastError() != ERROR_ALREADY_EXISTS ? (HANDLE*)MapViewOfFile(hMap,FILE_MAP_WRITE,0,0,0) : NULL);if ( ! aMBlock )  {if ( hMap ) CloseHandle(hMap); hMap = 0;// 无法完成分配时先清理再次尝试if ( Purge(Standard_False) )goto retry;const int BUFSIZE=1024;wchar_t message[BUFSIZE];if ( FormatMessageW (FORMAT_MESSAGE_FROM_SYSTEM, 0, GetLastError(), 0, message, BUFSIZE-1, 0) <=0 )StringCchCopyW(message, _countof(message), L"Standard_MMgrOpt::AllocMemory() failed to mmap");char messageA[BUFSIZE];WideCharToMultiByte(CP_UTF8, 0, message, -1, messageA, sizeof(messageA), NULL, NULL);throw Standard_OutOfMemory(messageA);}// 一开始存储hMapaMBlock[0] = hMap;// 块有效区域aBlock = (Standard_Size*)(aMBlock+1);// 有效区域尺寸Size = AlignedSize - sizeof(HANDLE);#endif    }else {// 使用api完成分配aBlock = (Standard_Size *) (myClear ? calloc(Size,sizeof(char)) : malloc(Size));if ( ! aBlock )  {if ( Purge(Standard_False) )goto retry;throw Standard_OutOfMemory("Standard_MMgrOpt::Allocate(): malloc failed");}}if (myClear)memset (aBlock, 0, Size);return aBlock;}
};// 源文件
static Standard_MMgrOpt::TPCallBackFunc MyPCallBackFunc = NULL;
Standard_EXPORT void Standard_MMgrOpt::SetCallBackFunction(TPCallBackFunc pFunc) {MyPCallBackFunc = pFunc;
}
inline void callBack(const Standard_Boolean isAlloc,const Standard_Address aStorage, const Standard_Size aRoundSize, const Standard_Size aSize) {if (MyPCallBackFunc)(*MyPCallBackFunc)(isAlloc, aStorage, aRoundSize, aSize);
}

8.Standard_Persistent

class Standard_Persistent : public Standard_Transient {
public:DEFINE_STANDARD_ALLOCStandard_Persistent() : _typenum(0), _refnum(0) {}DEFINE_STANDARD_RTTIEXT(Standard_Persistent,Standard_Transient)Standard_Integer& TypeNum() { return _typenum; }
private:Standard_Integer _typenum;Standard_Integer _refnum;friend class Storage_Schema;
};

9.Standard_Type

class Standard_Type : public Standard_Transient {
public:Standard_CString SystemName() const { return myInfo.name(); }Standard_CString Name() const { return myName; }Standard_Size Size() const { return mySize; }const Handle(Standard_Type)& Parent () const { return myParent; }Standard_EXPORT Standard_Boolean SubType (const Handle(Standard_Type)& theOther) const{return ! theOther.IsNull() && (theOther == this || (! myParent.IsNull() && myParent->SubType (theOther)));}Standard_EXPORT Standard_Boolean SubType (const Standard_CString theOther) const{return theName != 0 && (IsEqual (myName, theName) || (! myParent.IsNull() && myParent->SubType (theName)));}Standard_EXPORT void Print (Standard_OStream& AStream) const{AStream << std::hex << (Standard_Address)this << " : " << std::dec << myName ;}template <class T>static const Handle(Standard_Type)& Instance() {return opencascade::type_instance<T>::get();}Standard_EXPORT static Standard_Type* Register (const std::type_info& theInfo, const char* theName, Standard_Size theSize, const Handle(Standard_Type)& theParent){static Standard_Mutex theMutex;Standard_Mutex::Sentry aSentry (theMutex);registry_type& aRegistry = GetRegistry();Standard_Type* aType = 0;auto anIter = aRegistry.find(theInfo);if (anIter != aRegistry.end())return anIter->second;aType = new Standard_Type (theInfo, theName, theSize, theParent);aRegistry.emplace(theInfo, aType);return aType;}Standard_EXPORT ~Standard_Type (){registry_type& aRegistry = GetRegistry();Standard_ASSERT(aRegistry.erase(myInfo) > 0, "Standard_Type::~Standard_Type() cannot find itself in registry",);}DEFINE_STANDARD_RTTIEXT(Standard_Type,Standard_Transient)
private:Standard_Type (const std::type_info& theInfo, const char* theName, Standard_Size theSize, const Handle(Standard_Type)& theParent);
private:std::type_index myInfo;         //!< Object to store system name of the classStandard_CString myName;        //!< Given name of the classStandard_Size mySize;           //!< Size of the class instance, in bytesHandle(Standard_Type) myParent; //!< Type descriptor of parent class
};

RTTI识别系统识别类型所需的信息。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.pswp.cn/bicheng/83135.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

c++ typeid运算符

typeid运算符能获取类型信息。获取到的是type_info对象。type_info类型如下&#xff1a; 可以看到&#xff0c;这个类删除了拷贝构造函数以及等号操作符。有一些成员函数&#xff1a;hash_code、before、name、raw_name, 还重载了和!运算符。 测试&#xff1a; void testTyp…

第304个Vulnhub靶场演练攻略:digital world.local:FALL

digital world.local&#xff1a;FALL Vulnhub 演练 FALL (digitalworld.local: FALL) 是 Donavan 为 Vulnhub 打造的一款中型机器。这款实验室非常适合经验丰富的 CTF 玩家&#xff0c;他们希望在这类环境中检验自己的技能。那么&#xff0c;让我们开始吧&#xff0c;看看如何…

【数据库】数据库恢复技术

数据库恢复技术 实现恢复的核心是使用冗余&#xff0c;也就是根据冗余数据重建不正确数据。 事务 事务是一个数据库操作序列&#xff0c;是一个不可分割的工作单位&#xff0c;是恢复和并发的基本单位。 在关系数据库中&#xff0c;一个事务是一条或多条SQL语句&#xff0c…

switch-case判断

switch-case判断 #include <stdio.h> int main() {int type;printf("请输入你的选择&#xff1a;\n");scanf("%d",&type);getchar();switch (type){case 1:printf("你好&#xff01;");break;case 2:printf("早上好&#xff01;…

从监控到告警:Prometheus+Grafana+Alertmanager+告警通知服务全链路落地实践

文章目录 一、引言1.1 监控告警的必要性1.2 监控告警的基本原理1.2.1 指标采集与存储1.2.2 告警规则与触发机制1.2.3 多渠道通知与闭环 二、技术选型与架构设计2.1 为什么选择 Prometheus 及其生态2.1.1 Prometheus 优势分析2.1.2 Grafana 可视化能力2.1.3 Alertmanager 灵活告…

STM32 UART通信实战指南:从原理到项目落地

STM32串口通信实战指南&#xff1a;从零开始手把手教你 前言&#xff1a;为什么串口这么重要&#xff1f; 在嵌入式开发中&#xff0c;串口就像设备的"嘴巴"和"耳朵"。无论是给单片机下达指令、读取传感器数据&#xff0c;还是让两个模块"对话"…

Jmeter requests

1.Jemter元件和组件 1.1 元件和组件的概念 元件&#xff1a;多个功能相似的的组件的容器&#xff0c;类似于一个工具箱。 组件&#xff1a;实现某个特定功能的实例&#xff0c;类似于工具箱中的螺丝刀&#xff0c;十字扳手... 1.2 作用域和执行顺序 1.2.1 作用域 例子&#…

计算机视觉---GT(ground truth)

在计算机视觉&#xff08;Computer Vision, CV&#xff09;领域&#xff0c;Ground Truth&#xff08;GT&#xff0c;中文常译为“真值”或“ ground truth”&#xff09; 是指关于数据的真实标签或客观事实&#xff0c;是模型训练、评估和验证的基准。它是连接算法与现实世界的…

1-Wire 一线式总线:从原理到实战,玩转 DS18B20 温度采集

引言 在嵌入式系统中&#xff0c;通信总线是连接 CPU 与外设的桥梁。从 I2C、SPI 到 UART&#xff0c;每种总线都有其独特的应用场景。而本文要介绍的1-Wire 一线式总线&#xff0c;以其极简的硬件设计和独特的通信协议&#xff0c;在温度采集、身份识别等领域大放异彩。本文将…

基于开源AI大模型AI智能名片S2B2C商城小程序源码的销售环节数字化实现路径研究

摘要&#xff1a;在数字化浪潮下&#xff0c;企业销售环节的转型升级已成为提升竞争力的核心命题。本文基于清华大学全球产业研究院《中国企业数字化转型研究报告&#xff08;2020&#xff09;》提出的“提升销售率与利润率、打通客户数据、强化营销协同、构建全景用户画像、助…

Linux浅谈

Linux浅谈 一、什么是 Linux&#xff1f;先抛开 “内核”&#xff0c;看整体 可以把 Linux 系统 想象成一台 “组装电脑”&#xff1a; 最核心的零件是 “主板”—— 这就是 Linux 内核&#xff08;Kernel&#xff09;&#xff0c;负责管理电脑里的所有硬件&#xff08;比如 …

PostgreSQL ERROR: out of shared memory处理

使用pg_dump命令导出一个库的时候&#xff0c;报 pg_dump: error: query failed: ERROR: out of shared memory HINT: You might need to increase "max_locks_per_transaction". 从错误字面上看是超出内存大小了&#xff0c;建议增加max_locks_per_transaction参…

IoT/基于NB28-A/BC28-CNV通信模组使用AT指令连接华为云IoTDA平台(HCIP-IoT实验2)

文章目录 概述检查通信环境通信模组固件信号强度CGATT指令参数 / 啥是PS域&#xff1f;PS附着状态&#xff1a;ATCGATTPLMN 选择&#xff1a;ATCOPSCEREG指令参数 / 啥是EPS与EPC?CEREG指令参数 / 啥是URC?网络注册状态&#xff1a;ATCEREG网络附着和网络注册 AT指令接入IoTD…

红外遥控(外部中断)

目录 1.红外遥控简介 通信方式&#xff1a; 红外LED波长&#xff1a; 通信协议标准&#xff1a; 2.硬件电路 发送部分1&#xff1a; 内部元件介绍&#xff1a; 工作原理&#xff1a; 为什么要以38KHZ亮灭&#xff1f; 电路图&#xff1a; 发送部分2&#xff1a; 电…

【C#】一个简单的http服务器项目开发过程详解

这跟安装NoteJs程序运行脚本文件搭建一个简单Http服务器一样&#xff0c;相比起来&#xff0c;它的优点是可以开发的应用是免安装&#xff0c;跨平台的&#xff0c;放在移动盘上便捷的&#xff0c;这里着重讲http服务器实现的过程&#xff0c;以便自主实现特定的功能和服务。 …

WPF【11_4】WPF实战-重构与美化(MVVM 架构)

11-9 【理论】MVVM 架构 在 WPF 项目中&#xff0c;我们主要采用的是一种类似 MVC 的架构&#xff0c;叫做 MVVM。 MVVM 继承了 MVC 的理念&#xff0c;是 Model-View-ViewModel 的缩写&#xff0c;中文意思是模型、视图、视图模型。这三个词分开看我们都能看懂&#xff0c;不…

使用PowerBI个人网关定时刷新数据

使用PowerBI个人网关定时刷新数据 PowerBI desktop连接mysql&#xff0c;可以设置定时刷新数据或在PowerBI服务中手动刷新数据,步骤如下&#xff1a; 第一步&#xff1a; 下载网关。以个人网关为例&#xff0c;如图 第二步&#xff1a; 双击网关&#xff0c;点击下一步&…

深度学习驱动的超高清图修复技术——综述

Deep Learning-Driven Ultra-High-Definition Image Restoration: A Survey Liyan Wang, Weixiang Zhou, Cong Wang, Kin-Man Lam, Zhixun Su, Jinshan Pan Abstract Ultra-high-definition (UHD) image restoration​​ aims to specifically solve the problem of ​​quali…

3 分钟学会使用 Puppeteer 将 HTML 转 PDF

需求背景 1、网页存档与文档管理 需要将网页内容长期保存或归档为PDF,确保内容不被篡改或丢失,适用于法律文档、合同、技术文档等场景。PDF格式便于存储和检索。 2、电子报告生成 动态生成的HTML内容(如数据分析报告、仪表盘)需导出为PDF供下载或打印。PDF保留排版和样…

电子邮箱设置SSL:构建邮件传输的加密护城河

在数字化通信高度依赖的今天&#xff0c;电子邮件作为企业协作与个人隐私的核心载体&#xff0c;其安全性直接关系到数据主权与商业利益。SSL&#xff08;Secure Sockets Layer&#xff09;作为网络通信加密的基石技术&#xff0c;通过为邮件传输建立加密隧道&#xff0c;有效抵…