home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-11-08 | 5.5 KB | 189 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: FWMemTas.cpp
- // Release Version: $ 1.0d11 $
- //
- // Copyright: (c) 1993, 1995 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #include "FWFound.hpp"
-
- #ifndef FWMEMTAS_H
- #include "FWMemTas.h"
- #endif
-
- // ----- Foundation Includes -----
-
- #ifndef FWEXCDEF_H
- #include "FWExcDef.h"
- #endif
-
- #ifndef FWEXCTAS_H
- #include "FWExcTas.h"
- #endif
-
- #ifndef FWPRIDEB_H
- #include "FWPriDeb.h"
- #endif
-
- #ifndef FWNEW_H
- #include "FWNew.h"
- #endif
-
- // ----- OpenDoc Includes -----
-
- #if !defined(FW_qUsePlatformAlloc) && !defined(FW_qUseCRuntimeAlloc)
-
- #ifdef FW_BUILD_WIN
- #include <MMStubs.h>
- #endif
-
- #ifdef FW_BUILD_MAC
- #include <MemMgr.h>
- #endif
-
- #endif
-
- // ----- Platform Includes -----
-
- #include <stdlib.h>
-
- #if FW_LIB_EXPORT_PRAGMAS
- #pragma lib_export on
- #endif
-
- #ifdef FW_BUILD_MAC
- #pragma segment FWMemory_TaskGlobals
- #endif
-
- //----------------------------------------------------------------------------------------
- // FW_CMemoryTaskGlobals::GetMemoryGlobals
- //----------------------------------------------------------------------------------------
-
- FW_SPrivMemoryGlobals& FW_CMemoryTaskGlobals::GetMemoryGlobals()
- {
- // FW_SPrivMemoryGlobals *globals = (FW_SPrivMemoryGlobals*)
- // FW_CPrivTaskGlobals::GetTaskGlobals(kMemoryGlobalsOffset);
- FW_SPrivMemoryGlobals *globals = &gGlobals;
- FW_PRIV_ASSERT(globals != 0);
- #ifdef FW_qUsePlatformAlloc
- if (globals->gInitialized == 0)
- Initialize(*globals);
- #else
- if (globals->gMemoryHeap == 0)
- Initialize(*globals);
- #endif
- return *globals;
- }
-
- //----------------------------------------------------------------------------------------
- // Memory allocation glue code
- //----------------------------------------------------------------------------------------
- static void* OperatorNewHandler(size_t size)
- {
- #if defined(FW_qUsePlatformAlloc) || defined(FW_qUseCRuntimeAlloc)
- return ::malloc(size);
- #else
- return ::MMAllocateIn(size, FW_CMemoryTaskGlobals::GetMemoryGlobals().gMemoryHeap);
- #endif
- }
-
- static void OperatorDeleteHandler(void* p)
- {
- #if defined(FW_qUsePlatformAlloc) || defined(FW_qUseCRuntimeAlloc)
- ::free(p);
- #else
- ::MMFree(p);
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CMemoryTaskGlobals::gGlobals
- //----------------------------------------------------------------------------------------
-
- FW_SPrivMemoryGlobals FW_CMemoryTaskGlobals::gGlobals;
-
- //----------------------------------------------------------------------------------------
- // FW_CMemoryTaskGlobals::Initialize()
- //----------------------------------------------------------------------------------------
-
- void FW_CMemoryTaskGlobals::Initialize(FW_SPrivMemoryGlobals& memGlobals)
- {
- memGlobals.gMemoryHeap = 0;
- InitializeMemoryHeap(memGlobals);
- memGlobals.gLastRequested = 0;
- memGlobals.gNewHandler = FW_CMemoryManager::DefaultNewHandler;
-
- ::set_new_handler(memGlobals.gNewHandler);
-
- FW_SPrivExceptionGlobals &excGlobals = FW_CExceptionTaskGlobals::GetExceptionGlobals();
-
- #if !defined(FW_qUsePlatformAlloc) && !defined(FW_qUseCRuntimeAlloc)
- if (excGlobals.gOperatorNewHandler == 0)
- {
- excGlobals.gOperatorNewHandler = &::operator new;
- excGlobals.gOperatorDeleteHandler = &::operator delete;
- }
-
- // Note: Local variables are used here because it illegal to use the address of an
- // overloaded function in an expression, as in the asserts below. See ARM pg. 327
- // for legal uses of the address of an overloaded function.
-
- __FW_OperatorNewHandler newHandler = &::operator new;
- __FW_OperatorDeleteHandler deleteHandler = &::operator delete;
-
- FW_ASSERT(excGlobals.gOperatorNewHandler == newHandler);
- FW_ASSERT(excGlobals.gOperatorDeleteHandler == deleteHandler);
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CMemoryTaskGlobals::Terminate()
- //----------------------------------------------------------------------------------------
-
- void FW_CMemoryTaskGlobals::Terminate()
- {
- TerminateMemoryHeap();
- }
-
-
- //----------------------------------------------------------------------------------------
- // FW_CMemoryTaskGlobals::SetMemoryHeap()
- //----------------------------------------------------------------------------------------
-
- void FW_CMemoryTaskGlobals::SetMemoryHeap(MemHeap *aMemoryHeap)
- {
- GetMemoryGlobals().gMemoryHeap = aMemoryHeap;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CMemoryTaskGlobals::InitializeMemoryHeap()
- //----------------------------------------------------------------------------------------
-
- void FW_CMemoryTaskGlobals::InitializeMemoryHeap(FW_SPrivMemoryGlobals& memGlobals)
- {
- memGlobals.gInitialized = 1;
-
- #if defined(FW_qUsePlatformAlloc) || defined(FW_qUseCRuntimeAlloc)
- memGlobals.gMemoryHeap = NULL;
- #else
- memGlobals.gMemoryHeap = ::MMGetDefaultHeap();
- #ifdef FW_DEBUG
- // Check with Jens about what makes sense here. [AMB]
- #endif
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CMemoryTaskGlobals::TerminateMemoryHeap()
- //----------------------------------------------------------------------------------------
- void FW_CMemoryTaskGlobals::TerminateMemoryHeap()
- {
- #if !defined(FW_qUsePlatformAlloc) && !defined(FW_qUseCRuntimeAlloc)
- FW_SPrivMemoryGlobals& memGlobals = GetMemoryGlobals();
- // OpenDoc will delete the default heap, so just set it NULL.
- memGlobals.gMemoryHeap = NULL;
- #endif
- }
-