home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-09-17 | 12.1 KB | 415 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: SLMemMgr.cpp
- // Release Version: $ ODF 2 $
- //
- // Copyright: (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #include "FWFound.hpp"
-
- #ifndef SLMEMMGR_H
- #include "SLMemMgr.h"
- #endif
-
- #ifndef FWDEBUG_H
- #include "FWDebug.h"
- #endif
-
- #ifndef FWEXCDEF_H
- #include "FWExcDef.h"
- #endif
-
- #ifndef FWMEMHLP_H
- #include "FWMemHlp.h"
- #endif
-
- #ifndef FWNEW_H
- #include "FWNew.h"
- #endif
-
- #if defined(FW_BUILD_MAC) && !defined(__OSUTILS__)
- #include <OSUtils.h>
- #endif
-
- #if defined(FW_BUILD_MAC) && !defined(__MEMORY__)
- #include <Memory.h>
- #endif
-
- #if defined(FW_BUILD_WIN) && !defined(_INC_WINDOWS)
- #include <Windows.h>
- #endif
-
- #if defined(FW_BUILD_WIN) && !defined(_INC_WINDOWSX)
- #include <WindowsX.h>
- #endif
-
- #if !defined(FW_qUsePlatformAlloc) && !defined(FW_qUseCRuntimeAlloc)
-
- #ifdef FW_BUILD_WIN
- #include <MMStubs.h>
- #endif
-
- #ifdef FW_BUILD_MAC
- #include <MemMgr.h>
- #endif
-
- #endif
-
- #include <string.h>
-
- #if defined(__SC__) && defined(FW_BUILD_WIN)
-
- #if !defined(__LIMITS_H)
- #include <limits.h>
- #endif
-
- #if !defined(__HUGEPTR_H) && defined(FW_BUILD_WIN16)
- #include <hugeptr.h>
- #endif
-
- #endif
-
- #define PRIV_CLEAR_MEM_ON_ALLOC
-
- // It'd be cool to let this be set at program startup using InitializationFile setting
- #ifdef PRIV_CLEAR_MEM_ON_ALLOC
- const unsigned char kKnownRawByte = 0;
- #else
- const unsigned char kKnownRawByte = 0x5A;
- #endif
-
- #ifdef FW_BUILD_MAC
- #pragma segment FWMemory
- #endif
-
- //========================================================================================
- // Global procedures
- //========================================================================================
-
- FW_EXTERN_C_BEGIN
-
- //========================================================================================
- // CLASS FW_CMemoryManager
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_PrivMemoryManager_AddOffsetToPointer
- //----------------------------------------------------------------------------------------
-
- void *FW_PrivMemoryManager_AddOffsetToPointer(void *pointer, unsigned long offset)
- {
- // No try block necessary - Do not throw
-
- #if defined(FW_BUILD_MAC) || defined(FW_BUILD_WIN32)
- return (void *)((char *)pointer + offset);
- #elif defined(__SC__) && defined(FW_BUILD_WIN16)
- // If 'offset' is not less than the maximum signed LONG Value, consider it an fError.
- // We could program for these cases, but it is More than likely an fError.
- FW_ASSERT(offset < (unsigned long)LONG_MAX);
-
- // Symantec C++ note: hugeptr_add(p,o) is a macro that calls the function:
- // hugeptr_add(p, sizeof(*p) * o)
- // Thus, the pointer passed to hugeptr_add() must be 'char *' type.
- char * typedPointer = pointer;
- signed long signedLongOffset = offset;
-
- // The call to hugeptr_add() is not scoped with '::' because Symantec C++
- // first uses a macro and the macro fails to compile when scoped with '::'.
- return hugeptr_add(typedPointer, signedLongOffset);
- #else
- return (char __huge* pointer) + offset;
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PrivMemoryManager_CopyMemory
- //----------------------------------------------------------------------------------------
- void FW_PrivMemoryManager_CopyMemory(const void* const source,
- void* const destination,
- unsigned long bytesToMove)
- {
- // No try block necessary - Do not throw
-
- FW_ASSERT(source != 0);
- FW_ASSERT(destination != 0);
-
- #if defined(FW_BUILD_MAC)
- ::BlockMoveData(source, destination, bytesToMove);
- #elif defined(FW_BUILD_WIN)
- ::FW_PrimitiveCopyMemory(source, destination, bytesToMove);
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PrivMemoryManager_SetMemory
- //----------------------------------------------------------------------------------------
- void FW_PrivMemoryManager_SetMemory(void *aBlock,
- unsigned long bytesToSet,
- unsigned char byteValue)
- {
- // No try block necessary - Do not throw
-
- FW_ASSERT(aBlock != 0);
- FW_PrimitiveSetMemory(aBlock, bytesToSet, byteValue);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PrivMemoryManager_AllocateBlock - Allocate a non-relocatable block of fMemory.
- //----------------------------------------------------------------------------------------
- void* FW_PrivMemoryManager_AllocateBlock(unsigned long bytesRequested)
- {
- // No try block necessary - Do not throw
-
- void* aBlock = FW_PrimitiveAllocateBlock(bytesRequested);
-
- #ifdef FW_DEBUG
- if (aBlock != 0)
- FW_PrivMemoryManager_SetMemory(aBlock, bytesRequested, kKnownRawByte);
- #endif
-
- return aBlock;
- }
-
- #ifdef FW_BUILD_MAC
- #pragma segment FWMemMgr2
- #endif
-
- //----------------------------------------------------------------------------------------
- // FW_PrivMemoryManager_ResizeBlock - Resize a non-relocatable block of fMemory. The
- // block may be moved to a new location if necessary.
- //----------------------------------------------------------------------------------------
- void *FW_PrivMemoryManager_ResizeBlock(void *aBlock, unsigned long bytesRequested)
- {
- // No try block necessary - Do not throw
-
- void *newBlock;
-
- if (aBlock == 0)
- newBlock = FW_PrimitiveAllocateBlock(bytesRequested);
- else
- newBlock = FW_PrimitiveResizeBlock(aBlock, bytesRequested);
-
- return newBlock;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PrivMemoryManager_GetBlockSize - Return the size of a non-relocatable block.
- //----------------------------------------------------------------------------------------
- unsigned long FW_PrivMemoryManager_GetBlockSize(const void* aBlock)
- {
- // No try block necessary - Do not throw
-
- return FW_PrimitiveGetBlockSize((void*)aBlock);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PrivMemoryManager_FreeBlock - Return a non-relocatable block to the free store.
- //----------------------------------------------------------------------------------------
- void FW_PrivMemoryManager_FreeBlock(void *aBlock)
- {
- // No try block necessary - Do not throw
-
- FW_PrimitiveFreeBlock(aBlock);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PrivMemoryManager_AllocateSystemHandle -
- //----------------------------------------------------------------------------------------
- FW_PlatformHandle FW_PrivMemoryManager_AllocateSystemHandle(unsigned long bytesNeeded)
- {
- // No try block necessary - Do not throw
-
- FW_PlatformHandle aSystemHandle;
-
- #if !defined FW_qUsePlatformAlloc
- aSystemHandle = (FW_PlatformHandle) ::MMAllocateHandle(bytesNeeded);
- #elif defined FW_BUILD_MAC
- aSystemHandle = ::NewHandle(bytesNeeded);
- #elif defined FW_BUILD_WIN
- aSystemHandle = ::GlobalAlloc(GMEM_MOVEABLE, bytesNeeded);
- #endif
-
- return aSystemHandle;
- }
-
- #ifdef FW_BUILD_MAC
- #pragma segment FWMemMgr3
- #endif
-
- //----------------------------------------------------------------------------------------
- // FW_PrivMemoryManager_ResizeSystemHandle -
- //----------------------------------------------------------------------------------------
- FW_PlatformHandle FW_PrivMemoryManager_ResizeSystemHandle(FW_PlatformHandle aHandle,
- unsigned long bytesNeeded)
- {
- // No try block necessary - Do not throw
-
- FW_ASSERT(aHandle != 0);
-
- FW_PlatformHandle newHandle = aHandle;
-
- #if !defined FW_qUsePlatformAlloc
- ::MMSetHandleSize(aHandle, bytesNeeded);
- #elif defined FW_BUILD_MAC
- ::SetHandleSize(aHandle, bytesNeeded);
- #elif defined FW_BUILD_WIN
- newHandle = ::GlobalReAlloc(aHandle, bytesNeeded, GMEM_MOVEABLE);
- #endif
-
- if (newHandle != 0 && FW_PrivMemoryManager_GetSystemHandleSize(newHandle) < bytesNeeded)
- {
- // If a new handle was allocated, don't destroy the old handle. This
- // does make it possible aHandle to become a bad pointer under FW_BUILD_WIN
- // if the ::GlobalReAlloc() did realloc to a size less than "bytesNeeded".
- // The client would hold onto the now invalid aHandle and think that it was
- // copacetic when the resize failed. But I don't think ::GlobalReAlloc()
- // would return a handle smaller than the size requested.
- if (newHandle != aHandle)
- FW_PrivMemoryManager_FreeSystemHandle(newHandle);
- newHandle = 0;
- }
-
- return newHandle;
- }
-
- #ifdef FW_BUILD_MAC
- #pragma segment FWMemMgr2
- #endif
-
- //----------------------------------------------------------------------------------------
- // FW_PrivMemoryManager_FreeSystemHandle -
- //----------------------------------------------------------------------------------------
- void FW_PrivMemoryManager_FreeSystemHandle(FW_PlatformHandle aHandle)
- {
- // No try block necessary - Do not throw
-
- #if !defined FW_qUsePlatformAlloc
- if (aHandle)
- ::MMFreeHandle(aHandle);
- #elif defined FW_BUILD_MAC
- if (aHandle)
- ::DisposeHandle(aHandle);
- #elif defined FW_BUILD_WIN
- if (aHandle)
- ::GlobalFree(aHandle);
- #endif
- }
-
- #ifdef FW_BUILD_MAC
- #pragma segment FWMemMgr4
- #endif
-
- //----------------------------------------------------------------------------------------
- // FW_PrivMemoryManager_LockSystemHandle -
- //----------------------------------------------------------------------------------------
- void * FW_PrivMemoryManager_LockSystemHandle(FW_PlatformHandle handle)
- {
- // No try block necessary - Do not throw
-
- FW_ASSERT(handle != 0);
-
- void* memory;
-
- #if !defined FW_qUsePlatformAlloc
- memory = ::MMLockHandle(handle);
- #elif defined FW_BUILD_MAC
- HLock(handle);
- memory = *handle;
- #elif defined FW_BUILD_WIN
- memory = ::GlobalLock(handle);
- #endif
-
- return memory;
- }
-
- #ifdef FW_BUILD_MAC
- #pragma segment FWMemMgr5
- #endif
-
- //----------------------------------------------------------------------------------------
- // FW_PrivMemoryManager_UnlockSystemHandle -
- //----------------------------------------------------------------------------------------
- void FW_PrivMemoryManager_UnlockSystemHandle(FW_PlatformHandle aHandle)
- {
- // No try block necessary - Do not throw
-
- FW_ASSERT(aHandle != 0);
-
- #if !defined FW_qUsePlatformAlloc
- ::MMUnlockHandle(aHandle);
- #elif defined FW_BUILD_MAC
- ::HUnlock(aHandle);
- #elif defined FW_BUILD_WIN
- ::GlobalUnlock(aHandle);
- #endif
- }
-
-
- #ifdef FW_BUILD_MAC
- #pragma segment FWMemMgr6
- #endif
-
- //----------------------------------------------------------------------------------------
- // FW_PrivMemoryManager_GetSystemHandleSize -
- //----------------------------------------------------------------------------------------
- unsigned long FW_PrivMemoryManager_GetSystemHandleSize(FW_PlatformHandle aHandle)
- {
- // No try block necessary - Do not throw
-
- FW_ASSERT(aHandle != 0);
-
- #if !defined FW_qUsePlatformAlloc
- return ::MMGetHandleSize(aHandle);
- #elif defined FW_BUILD_MAC
- return ::GetHandleSize(aHandle);
- #elif defined FW_BUILD_WIN
- return ::GlobalSize(aHandle);
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PrivMemoryManager_CopySystemHandle -
- //----------------------------------------------------------------------------------------
- FW_PlatformHandle FW_PrivMemoryManager_CopySystemHandle(FW_PlatformHandle aHandle)
- {
- FW_PlatformHandle newHandle = NULL;
- FW_VOLATILE(newHandle);
-
- #if !defined FW_qUsePlatformAlloc
- newHandle = (FW_PlatformHandle) ::MMCopyHandle(aHandle);
- #else
- // We need this try block soley for emulated exceptions.
- // The code that follows won't fail with an exception,
- // but any shared library entry point that creates autodestruct objects
- // needs to be enclosed in a try block.
- FW_TRY
- {
- unsigned long size = GetSystemHandleSize(aHandle);
-
- FW_CAcquireLockedSystemHandle sourceLock(aHandle);
- void *sourcePointer = sourceLock.GetPointer();
-
- newHandle = FW_PrivMemoryManager_AllocateSystemHandle(size);
- if (newHandle != 0)
- {
- FW_CAcquireLockedSystemHandle destinationLock(newHandle);
- void *destinationPointer = destinationLock.GetPointer();
-
- FW_PrivMemoryManager_CopyMemory(sourcePointer, destinationPointer, size);
- }
- }
- FW_CATCH_BEGIN
- FW_CATCH_EVERYTHING()
- {
- return NULL;
- }
- FW_CATCH_END
- #endif
-
- return newHandle;
- }
-
- FW_EXTERN_C_END
-