sl@0: // Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of the License "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // e32\include\memmodel\emul\win32\memmodel.h sl@0: // sl@0: // WARNING: This file contains some APIs which are internal and are subject sl@0: // to change without notice. Such APIs should therefore not be used sl@0: // outside the Kernel and Hardware Services package. sl@0: // sl@0: sl@0: /** sl@0: @file sl@0: @internalComponent sl@0: */ sl@0: sl@0: #ifndef __WIN32_MEM_H__ sl@0: #define __WIN32_MEM_H__ sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: const TInt KRamChunkSize=0x10000; sl@0: const TInt KRamChunkShift=16; sl@0: const TInt KRamPageSize=0x1000; sl@0: const TInt KRamPageShift=12; sl@0: sl@0: /******************************************** sl@0: * Thread Control Block sl@0: ********************************************/ sl@0: sl@0: class DWin32Thread : public DThread sl@0: { sl@0: public: sl@0: ~DWin32Thread(); sl@0: virtual TInt Context(TDes8& aDes); sl@0: virtual TInt SetupContext(SThreadCreateInfo& anInfo); sl@0: virtual void DoExit2(); sl@0: }; sl@0: sl@0: /******************************************** sl@0: * Process Control Block sl@0: ********************************************/ sl@0: class DWin32CodeSeg; sl@0: class DWin32Chunk; sl@0: struct SProcessDllDataBlock; sl@0: class DWin32Process : public DProcess sl@0: { sl@0: public: sl@0: DWin32Process(); sl@0: ~DWin32Process(); sl@0: public: sl@0: virtual TInt DoCreate(TBool aKernelProcess, TProcessCreateInfo& aInfo); sl@0: virtual TInt NewChunk(DChunk*& aChunk, SChunkCreateInfo& aInfo, TLinAddr& aRunAddr); sl@0: virtual TInt AddChunk(DChunk* aChunk,TBool isReadOnly); sl@0: virtual TInt NewShPool(DShPool*& aPool, TShPoolCreateInfo& aInfo); sl@0: virtual TInt CreateDataBssStackArea(TProcessCreateInfo& aInfo); sl@0: virtual TInt GetNewThread(DThread*& aThread, SThreadCreateInfo& anInfo); sl@0: virtual TInt AttachExistingCodeSeg(TProcessCreateInfo& aInfo); sl@0: virtual TInt MapCodeSeg(DCodeSeg* aCodeSeg); sl@0: virtual void UnmapCodeSeg(DCodeSeg* aCodeSeg); sl@0: virtual void RemoveDllData(); sl@0: virtual void FinalRelease(); sl@0: virtual void Release(); sl@0: void CallRuntimeHook(TWin32RuntimeReason aReason); sl@0: public: sl@0: TWin32RuntimeHook iWin32RuntimeHook; sl@0: RArray iDllData; sl@0: }; sl@0: sl@0: /****************************************************************** sl@0: * structure to keep static data in code segments within a process sl@0: ******************************************************************/ sl@0: struct SProcessDllDataBlock sl@0: { sl@0: DWin32CodeSeg* iCodeSeg; sl@0: TAny* iDataCopy; // copy of .data sl@0: TAny* iBssCopy; // copy of .bss sl@0: }; sl@0: sl@0: /******************************************** sl@0: * Chunk Control Block sl@0: ********************************************/ sl@0: class DWin32Chunk : public DChunk sl@0: { sl@0: public: sl@0: enum TMemModelChunkAttributes sl@0: { sl@0: EPrivate=0x10000000, sl@0: sl@0: EMMChunkAttributesMask = EPrivate, sl@0: }; sl@0: sl@0: public: sl@0: ~DWin32Chunk(); sl@0: public: sl@0: virtual TInt DoCreate(SChunkCreateInfo& aInfo); sl@0: virtual TInt Adjust(TInt aNewSize); sl@0: virtual TInt AdjustDoubleEnded(TInt aBottom, TInt aTop); sl@0: virtual TInt CheckAccess(); sl@0: virtual TInt Commit(TInt anOffset, TInt aSize, TCommitType aCommitType=DChunk::ECommitDiscontiguous, TUint32* aExtraArg=0); sl@0: virtual TInt Allocate(TInt aSize, TInt aGuard=0, TInt aAlign=0); sl@0: virtual TInt Decommit(TInt anOffset, TInt aSize); sl@0: virtual TInt Lock(TInt anOffset, TInt aSize); sl@0: virtual TInt Unlock(TInt anOffset, TInt aSize); sl@0: virtual TInt Address(TInt aOffset, TInt aSize, TLinAddr& aKernelAddress); sl@0: virtual TInt PhysicalAddress(TInt aOffset, TInt aSize, TLinAddr& aKernelAddress, TUint32& aPhysicalAddress, TUint32* aPhysicalPageList=NULL); sl@0: virtual void BTracePrime(TInt aCategory); sl@0: virtual void Substitute(TInt aOffset, TPhysAddr aOldAddr, TPhysAddr aNewAddr); sl@0: virtual TUint8* Base(DProcess* aProcess); sl@0: inline TUint8* Base() const { return DChunk::Base(); } sl@0: private: sl@0: TInt DoCommit(TInt aOffset, TInt aSize); sl@0: void DoDecommit(TInt aOffset, TInt aSize); sl@0: public: sl@0: TBitMapAllocator* iPageBitMap; sl@0: TBitMapAllocator* iUnlockedPageBitMap; sl@0: TBitMapAllocator* iPermanentPageBitMap; sl@0: }; sl@0: sl@0: /******************************************** sl@0: * Code segment sl@0: ********************************************/ sl@0: class DModuleList; sl@0: class DWin32CodeSeg: public DCodeSeg sl@0: { sl@0: public: sl@0: DWin32CodeSeg(); sl@0: virtual ~DWin32CodeSeg(); sl@0: TInt ProcessImports(DProcess* aProcess); sl@0: TInt CreateAlreadyLoaded(HMODULE aModule, TInt aDepCount); sl@0: TInt RegisterCodeSeg(HMODULE aModule); sl@0: TInt CopyDataBss(); sl@0: public: sl@0: virtual TLibraryFunction Lookup(TInt aOrdinal); sl@0: virtual TInt GetMemoryInfo(TModuleMemoryInfo& aInfo, DProcess* aProcess); sl@0: virtual TInt DoCreate(TCodeSegCreateInfo& aInfo, DProcess* aProcess); sl@0: virtual TInt Loaded(TCodeSegCreateInfo& aInfo); sl@0: virtual void InitData(); sl@0: virtual void ReadExportDir(TUint32* aDest); sl@0: virtual TBool FindCheck(DProcess* aProcess); sl@0: virtual TBool OpenCheck(DProcess* aProcess); sl@0: virtual void Info(TCodeSegCreateInfo& aInfo); sl@0: public: sl@0: HINSTANCE iWinInstance; sl@0: HMODULE iModuleHandle; sl@0: wchar_t* iModuleFile; sl@0: TBool iAlwaysLoaded; // TRUE for variant or extension sl@0: DModuleList* iModuleList; sl@0: TAny* iDataCopy; // copy of .data sl@0: TInt iRealDataSize; sl@0: TInt iRealBssSize; sl@0: TLinAddr iDataDest; // load address of .data sl@0: TLinAddr iBssDest; // load address of .bss sl@0: TInt iCodeSegId; // unique ID, incremented each time a code segment is loaded sl@0: DWin32Process* iLiveProcess;// process who's static data is currently loaded in the codeseg sl@0: }; sl@0: sl@0: struct SWin32Module sl@0: { sl@0: TLinAddr iWin32ModuleHandle; sl@0: DWin32CodeSeg* iCodeSeg; sl@0: }; sl@0: sl@0: sl@0: /******************************************** sl@0: * Functions/Data defined in memory model sl@0: ********************************************/ sl@0: sl@0: class MM sl@0: { sl@0: public: sl@0: enum TMemModelPanic sl@0: { sl@0: EKernelHeapReserveFailed = 0, sl@0: EKernelHeapCommitFailed = 1, sl@0: ERamAllocMutexCreateFailed = 2, sl@0: EInvalidChunkCreate = 3, sl@0: EInvalidSharedModule = 4, sl@0: ECompileDepLists=5, sl@0: EWin32RuntimeError=6, sl@0: ENotSupportedOnEmulator=7, sl@0: EWsdBadReserve=8, sl@0: EWsdDllNotInProcess=9, sl@0: }; sl@0: sl@0: static void Panic(TMemModelPanic aPanic); sl@0: public: sl@0: static void Init1(); sl@0: static void Wait(); sl@0: static void Signal(); sl@0: static TUint32 RoundToPageSize(TUint32 aSize); sl@0: static TUint32 RoundToChunkSize(TUint32 aSize); sl@0: static TInt RegisterModule(HMODULE aModule); sl@0: static TInt Commit(TLinAddr aBase, TInt aSize, TInt aClearByte, TBool aExecute); sl@0: static TInt Decommit(TLinAddr aBase, TInt aSize); sl@0: static void CheckMemoryCounters(); sl@0: static void DoProcessSwitch(TAny* aAddressSpace); sl@0: static TAny* CurrentAddress(DThread* aThread, const TAny* aPtr, TInt aSize, TBool aWrite, TBool& aLocked); sl@0: public: sl@0: static TAny* KernelHeapAddress; sl@0: static DMutex* RamAllocatorMutex; sl@0: static TInt RamChunkSize; sl@0: static TInt RamChunkShift; sl@0: static TInt RamPageSize; sl@0: static TInt RamPageShift; sl@0: static TInt FreeMemory; // number of bytes in the system free memory sl@0: static TInt CacheMemory; // number of bytes of memory being used for cache chunks (RChunk::Unlock) sl@0: static TInt ReclaimedCacheMemory; // number of bytes of memory removed from CacheMemory in order to satisfy memory allocation sl@0: static TInt InitialFreeMemory; sl@0: static TBool AllocFailed; sl@0: static RArray Win32Modules; sl@0: static TInt NextCodeSegId; sl@0: }; sl@0: sl@0: sl@0: /******************************************** sl@0: * Shared buffers and pools sl@0: ********************************************/ sl@0: sl@0: class DWin32ShBuf : public DShBuf sl@0: { sl@0: public: sl@0: DWin32ShBuf(DShPool* aPool, TLinAddr aRelAddr); sl@0: ~DWin32ShBuf(); sl@0: sl@0: TInt AddToProcess(DProcess* aProcess, TUint aAttr); sl@0: sl@0: TInt Close(TAny* aPtr); sl@0: sl@0: protected: sl@0: virtual TInt Map(TUint, DProcess*, TLinAddr&); sl@0: virtual TInt UnMap(DProcess*); sl@0: virtual TUint8* Base(DProcess* aProcess); sl@0: virtual TUint8* Base(); sl@0: sl@0: private: sl@0: TBool iMapped; sl@0: }; sl@0: sl@0: sl@0: class DWin32ShPool : public DShPool sl@0: { sl@0: public: sl@0: DWin32ShPool(); sl@0: virtual ~DWin32ShPool(); sl@0: sl@0: TInt Close(TAny* aPtr); sl@0: TInt AddToProcess(DProcess* aProcess, TUint aAttr); sl@0: TInt Alloc(DShBuf*&); sl@0: sl@0: protected: sl@0: TInt DoCreate(TShPoolCreateInfo& aInfo); sl@0: TInt AddToFreeList(TInt aOffset); sl@0: TInt CreateInitialBuffers(); sl@0: TInt DeleteInitialBuffers(); sl@0: TInt DestroyHandles(DProcess* aProcess); sl@0: void DestroyClientResources(DProcess* aProcess); sl@0: sl@0: void Free(DShBuf* aBuf); sl@0: TInt UpdateFreeList(); sl@0: sl@0: TUint8* Base(); sl@0: TUint8* Base(DProcess* aProcess); sl@0: TBool IsOpen(DProcess* aProcess); sl@0: sl@0: TBitMapAllocator* iBufMap; sl@0: DWin32ShBuf* iInitialBuffersArray; sl@0: TUint8* iWin32MemoryBase; sl@0: TInt iWin32MemorySize; sl@0: friend class DWin32ShBuf; sl@0: }; sl@0: sl@0: sl@0: class DWin32AlignedShPool : public DWin32ShPool sl@0: { sl@0: public: sl@0: DWin32AlignedShPool(); sl@0: virtual ~DWin32AlignedShPool(); sl@0: TInt SetBufferWindow(DProcess* aProcess, TInt aWindowSize); sl@0: sl@0: private: sl@0: TInt DoCreate(TShPoolCreateInfo& aInfo); sl@0: TInt GrowPool(); sl@0: TInt ShrinkPool(); sl@0: }; sl@0: sl@0: sl@0: class DWin32NonAlignedShPool : public DWin32ShPool sl@0: { sl@0: public: sl@0: DWin32NonAlignedShPool(); sl@0: virtual ~DWin32NonAlignedShPool(); sl@0: sl@0: private: sl@0: TInt DoCreate(TShPoolCreateInfo& aInfo); sl@0: void FreeBufferPages(TUint aOffset); sl@0: TInt GrowPool(); sl@0: TInt ShrinkPool(); sl@0: sl@0: TBitMapAllocator* iPagesMap; sl@0: }; sl@0: sl@0: #endif // __WIN32_MEM_H__