Changeset 8877 for trunk/src


Ignore:
Timestamp:
Jul 15, 2002, 4:28:53 PM (23 years ago)
Author:
sandervl
Message:

Rewrote algorithm for 64kb alignment in VirtualAlloc'ed memory; allocation changes for heap (in 64kb chunks) & PE image (align at 64kb)

Location:
trunk/src/kernel32
Files:
1 added
11 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kernel32/heapcode.cpp

    r5075 r8877  
    1 /* $Id: heapcode.cpp,v 1.3 2001-02-09 18:31:05 sandervl Exp $ */
     1/* $Id: heapcode.cpp,v 1.4 2002-07-15 14:28:51 sandervl Exp $ */
    22/*
    33 * Code heap functions for OS/2
    44 *
    55 * Initially commit 4 kb, add more when required
     6 *
     7 * NOTE: If high memory is ever used for this heap, then you must use VirtualAlloc!
    68 *
    79 * TODO: Not process/thread safe (initializing/destroying heap)
     
    2224#include "dbglocal.h"
    2325
    24 Heap_t  codeHeap = 0;
    25 static PVOID   pCodeMem = NULL;
     26Heap_t          codeHeap = 0;
     27static PVOID    pCodeMem = NULL;
    2628
    2729void * _LNK_CONV getmoreCodeMem(Heap_t pHeap, size_t *size, int *clean);
    28 void _LNK_CONV releaseCodeMem(Heap_t pHeap, void *block, size_t size);
     30void _LNK_CONV   releaseCodeMem(Heap_t pHeap, void *block, size_t size);
    2931
    3032//******************************************************************************
     
    3234BOOL InitializeCodeHeap()
    3335{
    34  APIRET rc;
     36    APIRET rc;
    3537
    36    dprintf(("KERNEL32: InitializeCodeHeap"));
    37    rc = DosAllocMem(&pCodeMem, PAGE_SIZE, PAG_READ|PAG_WRITE|PAG_COMMIT|PAG_EXECUTE);
    38    if(rc != 0) {
    39         dprintf(("InitializeSharedHeap: DosAllocSharedMem failed with %d", rc));
    40         return FALSE;
    41    }
    42    codeHeap = _ucreate(pCodeMem, PAGE_SIZE, _BLOCK_CLEAN,
    43                        _HEAP_REGULAR, getmoreCodeMem, releaseCodeMem);
    44    if(codeHeap == NULL) {
    45         DosFreeMem(pCodeMem);
     38    dprintf(("KERNEL32: InitializeCodeHeap"));
     39   
     40    //NOTE: MUST use 64kb here or else we are at risk of running out of virtual
     41    //      memory space. (when allocating 4kb we actually get 4kb + 60k uncommited)
     42    rc = DosAllocMem(&pCodeMem, 64*1024, PAG_READ|PAG_WRITE|PAG_COMMIT|PAG_EXECUTE);
     43    if(rc != 0) {
     44        dprintf(("InitializeSharedHeap: DosAllocSharedMem failed with %d", rc));
     45        return FALSE;
     46    }
     47    codeHeap = _ucreate(pCodeMem, PAGE_SIZE, _BLOCK_CLEAN, _HEAP_REGULAR, getmoreCodeMem, releaseCodeMem);
     48    if(codeHeap == NULL) {
     49        DosFreeMem(pCodeMem);
    4650        pCodeMem = NULL;
    47         dprintf(("InitializeSharedHeap: _ucreate failed!"));
    48         return FALSE;
    49    }
    50    return TRUE;
     51            dprintf(("InitializeSharedHeap: _ucreate failed!"));
     52            return FALSE;
     53    }
     54    return TRUE;
    5155}
    5256//******************************************************************************
     
    5458void DestroyCodeHeap()
    5559{
    56   dprintf(("KERNEL32: DestroyCodeHeap"));
    57   if(codeHeap) {
    58         _uclose(codeHeap);
    59         _udestroy(codeHeap, _FORCE);
    60         codeHeap = NULL;
    61   }
    62   if(pCodeMem) {
    63         DosFreeMem(pCodeMem);
    64         pCodeMem = NULL;
    65   }
     60    dprintf(("KERNEL32: DestroyCodeHeap"));
     61    if(codeHeap) {
     62            _uclose(codeHeap);
     63            _udestroy(codeHeap, _FORCE);
     64            codeHeap = NULL;
     65    }
     66    if(pCodeMem) {
     67        DosFreeMem(pCodeMem);
     68        pCodeMem = NULL;
     69    }
    6670}
    6771//******************************************************************************
     
    6973void * _LNK_CONV getmoreCodeMem(Heap_t pHeap, size_t *size, int *clean)
    7074{
    71  APIRET rc;
    72  PVOID newblock;
     75    APIRET rc;
     76    PVOID newblock;
    7377
    74   dprintf(("KERNEL32: getmoreCodeMem(%08xh, %08xh, %08xh)\n",
    75           pHeap,
    76           *size,
    77           *clean));
     78    dprintf(("KERNEL32: getmoreCodeMem(%08xh, %08xh, %08xh)\n", pHeap, *size, *clean));
    7879
    79   /* round the size up to a multiple of 4K */
    80   *size = (*size / 4096) * 4096 + 4096;
     80    /* round the size up to a multiple of 64K */
     81    //NOTE: MUST use 64kb here or else we are at risk of running out of virtual
     82    //      memory space. (when allocating 4kb we actually get 4kb + 60k uncommited)
     83    *size = ( (*size / 65536) + 1) * 65536;
    8184
    82   rc = DosAllocMem(&newblock, *size, PAG_READ|PAG_WRITE|PAG_COMMIT|PAG_EXECUTE);
    83   if(rc != 0) {
    84         dprintf(("getmoreCodeMem: DosAllocMem failed with %d", rc));
    85         return FALSE;
    86   }
    87   *clean = _BLOCK_CLEAN;
    88   dprintf(("KERNEL32: getmoreCodeMem %x %d", newblock, *size));
    89   return newblock;
     85    rc = DosAllocMem(&newblock, *size, PAG_READ|PAG_WRITE|PAG_COMMIT|PAG_EXECUTE);
     86    if(rc != 0) {
     87        dprintf(("getmoreCodeMem: DosAllocMem failed with %d", rc));
     88        return FALSE;
     89    }
     90    *clean = _BLOCK_CLEAN;
     91    dprintf(("KERNEL32: getmoreCodeMem %x %d", newblock, *size));
     92    return newblock;
    9093}
    9194//******************************************************************************
     
    9396void _LNK_CONV releaseCodeMem(Heap_t pHeap, void *block, size_t size)
    9497{
    95   dprintf(("KERNEL32: releaseCodeMem %x %d", block, size));
    96   DosFreeMem(block);
     98    dprintf(("KERNEL32: releaseCodeMem %x %d", block, size));
     99    DosFreeMem(block);
    97100}
    98101//******************************************************************************
  • trunk/src/kernel32/heapshared.cpp

    r5564 r8877  
    1 /* $Id: heapshared.cpp,v 1.8 2001-04-22 09:00:19 sandervl Exp $ */
     1/* $Id: heapshared.cpp,v 1.9 2002-07-15 14:28:51 sandervl Exp $ */
    22/*
    33 * Shared heap functions for OS/2
     
    3939BOOL InitializeSharedHeap()
    4040{
    41  APIRET rc;
     41    APIRET rc;
    4242
    43   if(pSharedMem == NULL) {
    44         rc = DosAllocSharedMem(&pSharedMem, NULL, MAX_HEAPSIZE, PAG_READ|PAG_WRITE|OBJ_GETTABLE);
    45         if(rc != 0) {
    46                 dprintf(("InitializeSharedHeap: DosAllocSharedMem failed with %d", rc));
    47                 return FALSE;
    48         }
    49         rc = DosSetMem(pSharedMem, INCR_HEAPSIZE, PAG_READ|PAG_WRITE|PAG_COMMIT);
    50         if(rc != 0) {
    51                 DosFreeMem(pSharedMem);
    52                 dprintf(("InitializeSharedHeap: DosSetMem failed with %d", rc));
    53                 return FALSE;
    54         }
    55         sharedHeap = _ucreate(pSharedMem, INCR_HEAPSIZE, _BLOCK_CLEAN,
    56                               _HEAP_REGULAR|_HEAP_SHARED,
    57                               getmoreShared, releaseShared);
     43    if(pSharedMem == NULL) {
     44            rc = DosAllocSharedMem(&pSharedMem, NULL, MAX_HEAPSIZE, PAG_READ|PAG_WRITE|OBJ_GETTABLE);
     45            if(rc != 0) {
     46                dprintf(("InitializeSharedHeap: DosAllocSharedMem failed with %d", rc));
     47                    return FALSE;
     48            }
     49            rc = DosSetMem(pSharedMem, INCR_HEAPSIZE, PAG_READ|PAG_WRITE|PAG_COMMIT);
     50            if(rc != 0) {
     51                DosFreeMem(pSharedMem);
     52                    dprintf(("InitializeSharedHeap: DosSetMem failed with %d", rc));
     53                    return FALSE;
     54            }
     55            sharedHeap = _ucreate(pSharedMem, INCR_HEAPSIZE, _BLOCK_CLEAN, _HEAP_REGULAR|_HEAP_SHARED,
     56                                  getmoreShared, releaseShared);
    5857
    59         if(sharedHeap == NULL) {
    60                 DosFreeMem(pSharedMem);
    61                 dprintf(("InitializeSharedHeap: _ucreate failed!"));
    62                 return FALSE;
    63         }
    64         dprintf(("KERNEL32: First InitializeSharedHeap %x %x", pSharedMem, sharedHeap));
    65         for(int i=0;i<INCR_HEAPSIZE/PAGE_SIZE;i++) {
    66                 pageBitmap[i] = 1; //mark as committed
    67         }
    68   }
    69   else {
    70         if(DosGetSharedMem(pSharedMem, PAG_READ|PAG_WRITE) != 0) {
    71                 dprintf(("InitializeSharedHeap: DosGetSharedMem failed!"));
    72                 return FALSE;
    73         }
    74         dprintf(("KERNEL32: InitializeSharedHeap %x %x refcount %d", pSharedMem, sharedHeap, refCount));
    75         if(_uopen(sharedHeap) != 0) {
    76                 dprintf(("InitializeSharedHeap: unable to open shared heap!"));
    77                 return FALSE;
    78         }
    79   }
    80   refCount++;
    81   return TRUE;
     58            if(sharedHeap == NULL) {
     59                DosFreeMem(pSharedMem);
     60                    dprintf(("InitializeSharedHeap: _ucreate failed!"));
     61                    return FALSE;
     62            }
     63            dprintf(("KERNEL32: First InitializeSharedHeap %x %x", pSharedMem, sharedHeap));
     64            for(int i=0;i<INCR_HEAPSIZE/PAGE_SIZE;i++) {
     65                pageBitmap[i] = 1; //mark as committed
     66        }
     67    }
     68    else {
     69        if(DosGetSharedMem(pSharedMem, PAG_READ|PAG_WRITE) != 0) {
     70                    dprintf(("InitializeSharedHeap: DosGetSharedMem failed!"));
     71                    return FALSE;
     72            }
     73            dprintf(("KERNEL32: InitializeSharedHeap %x %x refcount %d", pSharedMem, sharedHeap, refCount));
     74            if(_uopen(sharedHeap) != 0) {
     75                dprintf(("InitializeSharedHeap: unable to open shared heap!"));
     76                    return FALSE;
     77            }
     78    }
     79    refCount++;
     80    return TRUE;
    8281}
    8382//******************************************************************************
     
    8584void DestroySharedHeap()
    8685{
    87   dprintf(("KERNEL32: DestroySharedHeap %d", refCount));
    88   if(--refCount == 0) {
    89         if(sharedHeap) {
    90                 _uclose(sharedHeap);
    91                 _udestroy(sharedHeap, _FORCE);
    92                 sharedHeap = NULL;
    93         }
    94         if(pSharedMem) {
    95                 DosFreeMem(pSharedMem);
    96                 pSharedMem = NULL;
    97         }
    98   }
    99   else {
    100         _uclose(sharedHeap);
    101   }
     86    dprintf(("KERNEL32: DestroySharedHeap %d", refCount));
     87    if(--refCount == 0) {
     88            if(sharedHeap) {
     89                _uclose(sharedHeap);
     90                    _udestroy(sharedHeap, _FORCE);
     91                    sharedHeap = NULL;
     92            }
     93            if(pSharedMem) {
     94                DosFreeMem(pSharedMem);
     95                    pSharedMem = NULL;
     96            }
     97    }
     98    else {
     99        _uclose(sharedHeap);
     100    }
    102101}
    103102//******************************************************************************
     
    105104ULONG GetPageRangeFree(ULONG pageoffset)
    106105{
    107   dprintf(("KERNEL32: GetPageRangeFree(%08xh)\n",
    108            pageoffset));
     106    dprintf(("KERNEL32: GetPageRangeFree(%08xh)", pageoffset));
    109107
    110   for(int i=pageoffset;i<MAX_HEAPPAGES;i++) {
    111         if(pageBitmap[i] == 1) {
    112                 break;
    113         }
    114   }
    115   return i-pageoffset;
     108    for(int i=pageoffset;i<MAX_HEAPPAGES;i++) {
     109        if(pageBitmap[i] == 1) {
     110                    break;
     111            }
     112    }
     113    return i-pageoffset;
    116114}
    117115//******************************************************************************
     
    119117void * _LNK_CONV getmoreShared(Heap_t pHeap, size_t *size, int *clean)
    120118{
    121  APIRET rc;
    122  ULONG newsize;
    123  PVOID newblock;
     119    APIRET rc;
     120    ULONG newsize;
     121    PVOID newblock;
    124122
    125  dprintf(("KERNEL32: getmoreShared(%08xh, %08xh, %08xh)\n",
    126           pHeap,
    127           *size,
    128           *clean));
     123    dprintf(("KERNEL32: getmoreShared(%08xh, %08xh, %08xh)\n", pHeap, *size, *clean));
    129124
    130   /* round the size up to a multiple of 4K */
    131   // *size = (*size / 4096) * 4096 + 4096;
    132   // @@@PH speed improvement
    133   *size = (*size + 4096) & 0xFFFFF000;
    134   *size = max(*size, INCR_HEAPSIZE);
     125    /* round the size up to a multiple of 4K */
     126    // *size = (*size / 4096) * 4096 + 4096;
     127    // @@@PH speed improvement
     128    *size = (*size + 4096) & 0xFFFFF000;
     129    *size = max(*size, INCR_HEAPSIZE);
    135130
    136   for(int i=0;i<MAX_HEAPPAGES;i++)
    137   {
    138         int nrpagesfree = GetPageRangeFree(i);
    139         if(nrpagesfree >= *size/PAGE_SIZE) {
    140                 newblock = (PVOID)((ULONG)pSharedMem + i*PAGE_SIZE);
    141                 rc = DosSetMem(newblock, *size, PAG_READ|PAG_WRITE|PAG_COMMIT);
    142                 if(rc != 0) {
    143                         dprintf(("getmoreShared: DosSetMem failed with %d", rc));
    144                         return NULL;
    145                 }
    146                 for(int j=0;j < *size/PAGE_SIZE; j++)
    147                 {
    148                         pageBitmap[i+j] = 1; //mark as committed
    149                 }
     131    for(int i=0;i<MAX_HEAPPAGES;i++)
     132    {
     133        int nrpagesfree = GetPageRangeFree(i);
     134        if(nrpagesfree >= *size/PAGE_SIZE) {
     135                    newblock = (PVOID)((ULONG)pSharedMem + i*PAGE_SIZE);
     136                    rc = DosSetMem(newblock, *size, PAG_READ|PAG_WRITE|PAG_COMMIT);
     137                    if(rc != 0) {
     138                        dprintf(("getmoreShared: DosSetMem failed with %d", rc));
     139                            return NULL;
     140                    }
     141                    for(int j=0;j < *size/PAGE_SIZE; j++)
     142            {
     143                            pageBitmap[i+j] = 1; //mark as committed
     144                    }
    150145
    151                 *clean = _BLOCK_CLEAN;
    152                 dprintf(("KERNEL32: getmoreShared %x %d", newblock, *size));
    153                 return newblock;
    154         }
    155         if(nrpagesfree)
    156                 i += nrpagesfree-1;
    157   }
    158   dprintf(("KERNEL32: getmoreShared NOTHING LEFT (%d)", *size));
    159   return NULL;
     146                    *clean = _BLOCK_CLEAN;
     147                    dprintf(("KERNEL32: getmoreShared %x %d", newblock, *size));
     148                    return newblock;
     149            }
     150            if(nrpagesfree)
     151                i += nrpagesfree-1;
     152    }
     153    dprintf(("KERNEL32: getmoreShared NOTHING LEFT (%d)", *size));
     154    return NULL;
    160155}
    161156//******************************************************************************
     
    163158void _LNK_CONV releaseShared(Heap_t pHeap, void *block, size_t size)
    164159{
    165  ULONG pagenr;
     160    ULONG pagenr;
    166161
    167   dprintf(("KERNEL32: releaseShared %x %d", block, size));
    168   DosSetMem(block, size, PAG_READ|PAG_WRITE|PAG_DECOMMIT);
     162    dprintf(("KERNEL32: releaseShared %x %d", block, size));
     163    DosSetMem(block, size, PAG_READ|PAG_WRITE|PAG_DECOMMIT);
    169164
    170   pagenr  = (ULONG)block - (ULONG)pSharedMem;
    171   pagenr /= PAGE_SIZE;
    172   for(int i=pagenr;i<pagenr+size/PAGE_SIZE;i++) {
    173         pageBitmap[i] = 0; //mark as decommitted
    174   }
     165    pagenr  = (ULONG)block - (ULONG)pSharedMem;
     166    pagenr /= PAGE_SIZE;
     167    for(int i=pagenr;i<pagenr+size/PAGE_SIZE;i++) {
     168            pageBitmap[i] = 0; //mark as decommitted
     169    }
    175170}
    176171//******************************************************************************
     
    178173DWORD  HeapGetSharedMemBase()
    179174{
    180   dprintf(("KERNEL32: HeapGetSharedMemBase()\n"));
    181   return (DWORD) pSharedMem;
     175    dprintf(("KERNEL32: HeapGetSharedMemBase()\n"));
     176    return (DWORD) pSharedMem;
    182177}
    183178//******************************************************************************
  • trunk/src/kernel32/hmthread.cpp

    r8648 r8877  
    1 /* $Id: hmthread.cpp,v 1.13 2002-06-11 16:36:53 sandervl Exp $ */
     1/* $Id: hmthread.cpp,v 1.14 2002-07-15 14:28:51 sandervl Exp $ */
    22
    33/*
     
    1313 *       WaitForSingle/MultipleObjects needs to be rewritten! (not using
    1414 *       Open32)
     15 *
     16 ************************************************************************************
     17 * NOTE: If we ever decide to allocate our own stack, then we MUST use VirtualAlloc!!!!
     18 *       (alignment reasons)
     19 ************************************************************************************
    1520 *
    1621 * Copyright 2000 Sander van Leeuwen (sandervl@xs4all.nl)
     
    7782        cbStack = 1048576; // per default 1MB stack per thread
    7883
     84    //************************************************************************************
     85    //NOTE: If we ever decide to allocate our own stack, then we MUST use VirtualAlloc!!!!
     86    //      (alignment reasons)
     87    //************************************************************************************
    7988    pHMHandleData->hHMHandle = O32_CreateThread(lpsa, cbStack, winthread->GetOS2Callback(),
    8089                                                (LPVOID)winthread, fdwCreate, lpIDThread);
  • trunk/src/kernel32/mmap.cpp

    r8659 r8877  
    1 /* $Id: mmap.cpp,v 1.57 2002-06-13 14:11:39 sandervl Exp $ */
     1/* $Id: mmap.cpp,v 1.58 2002-07-15 14:28:51 sandervl Exp $ */
    22
    33/*
     
    3333#include "mmap.h"
    3434#include "oslibdos.h"
     35#include "oslibmem.h"
    3536#include <winimagepeldr.h>
    3637#include <custombuild.h>
     
    610611 Win32MemMapView *tmpview  = mapviews;
    611612
    612     errorState = 0;
    613     mParentMap = map;
    614     mSize    = size;
    615     mOffset  = offset;
    616     mProcessId = GetCurrentProcessId();
    617     pShareViewAddr = NULL;
     613    errorState      = 0;
     614    mParentMap      = map;
     615    mSize           = size;
     616    mOffset         = offset;
     617    mProcessId      = GetCurrentProcessId();
     618    pShareViewAddr  = NULL;
    618619
    619620    switch(fdwAccess) {
    620621    case FILE_MAP_READ:
    621         accessAttr = PAG_READ;
    622         mfAccess   = MEMMAP_ACCESS_READ;
     622        accessAttr  = PAG_READ;
     623        mfAccess    = MEMMAP_ACCESS_READ;
    623624        break;
    624625    case FILE_MAP_ALL_ACCESS:
     
    626627    case FILE_MAP_WRITE|FILE_MAP_READ:
    627628    case FILE_MAP_COPY:
    628         accessAttr = (PAG_READ|PAG_WRITE);
    629         mfAccess   = MEMMAP_ACCESS_READ | MEMMAP_ACCESS_WRITE;
     629        accessAttr  = (PAG_READ|PAG_WRITE);
     630        mfAccess    = MEMMAP_ACCESS_READ | MEMMAP_ACCESS_WRITE;
    630631        break;
    631632    }
  • trunk/src/kernel32/os2heap.cpp

    r7728 r8877  
    1 /* $Id: os2heap.cpp,v 1.32 2002-01-06 16:48:47 sandervl Exp $ */
     1/* $Id: os2heap.cpp,v 1.33 2002-07-15 14:28:51 sandervl Exp $ */
    22
    33/*
     
    77 *
    88 *
     9 * NOTE: Do NOT use high memory here. Risky with 16 bits tcpip stack
     10 *       If this is ever changed, then you must use VirtualAlloc!
     11 *
    912 * NOTE: ReAlloc allocates memory using Alloc if memory pointer == NULL
    1013 *       WINE controls depend on this, even though it apparently
     
    6467OS2Heap::OS2Heap(DWORD flOptions, DWORD dwInitialSize, DWORD dwMaximumSize)
    6568{
    66   OS2Heap *curheap = OS2Heap::heap;
     69    OS2Heap *curheap = OS2Heap::heap;
    6770 
    6871#ifdef DEBUG
    69   totalAlloc   = 0;
    70 #endif
    71   fInitialized = 0;
    72   nrHeaps      = 0;
    73   heapelem     = NULL;
    74 
    75   dwInitialSize       = (dwInitialSize >= 0x4000) ? dwInitialSize : 0x4000;
    76 
    77   this->dwMaximumSize = dwMaximumSize;
    78   this->dwInitialSize = dwInitialSize;
    79   this->flOptions     = flOptions;
    80 
    81   heaplistmutex.enter();
    82   if(curheap != NULL) {
     72    totalAlloc   = 0;
     73#endif
     74    fInitialized = 0;
     75    nrHeaps      = 0;
     76    heapelem     = NULL;
     77
     78    /* round the size up to a multiple of 64K */
     79    //NOTE: MUST use 64kb here or else we are at risk of running out of virtual
     80    //      memory space. (when allocating 4kb we actually get 4kb + 60k uncommited)
     81    dwInitialSize       = ( (dwInitialSize / 65536) + 1) * 65536;
     82
     83    this->dwMaximumSize = dwMaximumSize;
     84    this->dwInitialSize = dwInitialSize;
     85    this->flOptions     = flOptions;
     86
     87    heaplistmutex.enter();
     88    if(curheap != NULL) {
    8389        while(curheap->next != NULL) {
    8490                curheap = curheap->next;
    8591        }
    8692        curheap->next = this;
    87   }
    88   else  heap = this;
    89   next = NULL;
    90 
    91   heaplistmutex.leave();
    92 
    93   APIRET rc;
    94 
    95   rc = DosAllocMem((PPVOID)&pInitialHeapMem, dwInitialSize, PAG_READ|PAG_WRITE|PAG_COMMIT);
    96   if(rc != 0) {
    97         dprintf(("OS2Heap::OS2Heap: DosAllocSharedMem failed with %d", rc));
     93    }
     94    else  heap = this;
     95    next = NULL;
     96
     97    heaplistmutex.leave();
     98
     99    APIRET rc;
     100
     101    rc = DosAllocMem((PPVOID)&pInitialHeapMem, dwInitialSize, PAG_READ|PAG_WRITE|PAG_COMMIT);
     102    if(rc != 0) {
     103            dprintf(("OS2Heap::OS2Heap: DosAllocSharedMem failed with %d", rc));
    98104        DebugInt3();
    99   }
    100   uheap = _ucreate(pInitialHeapMem, dwInitialSize, _BLOCK_CLEAN,
    101                    _HEAP_REGULAR, getmoreHeapMem, releaseHeapMem);
    102   if(uheap == NULL) {
    103         DosFreeMem(pInitialHeapMem);
     105    }
     106    uheap = _ucreate(pInitialHeapMem, dwInitialSize, _BLOCK_CLEAN,
     107                     _HEAP_REGULAR, getmoreHeapMem, releaseHeapMem);
     108    if(uheap == NULL) {
     109            DosFreeMem(pInitialHeapMem);
    104110        pInitialHeapMem = NULL;
    105         dprintf(("OS2Heap::OS2Heap: _ucreate failed!"));
     111            dprintf(("OS2Heap::OS2Heap: _ucreate failed!"));
    106112        DebugInt3();
    107   }
    108   hPrimaryHeap = (HANDLE)uheap;
    109   dprintf(("KERNEL32:  HeapCreate: initial size %d, max size %d (flags %X) returned %X\n", dwInitialSize, dwMaximumSize, flOptions, hPrimaryHeap));
     113    }
     114    hPrimaryHeap = (HANDLE)uheap;
     115    dprintf(("KERNEL32:  HeapCreate: initial size %d, max size %d (flags %X) returned %X\n", dwInitialSize, dwMaximumSize, flOptions, hPrimaryHeap));
    110116}
    111117//******************************************************************************
     
    113119OS2Heap::~OS2Heap()
    114120{
    115  OS2Heap *curheap = OS2Heap::heap;
    116  int i;
    117  
    118   // invalidate handle cache
    119   fhhm_lastHandle = 0;
    120   fhhm_lastHeap   = NULL;
    121  
    122  
    123   dprintf(("dtr OS2Heap, hPrimaryHeap = %X\n", hPrimaryHeap));
    124 
    125   heaplistmutex.enter();
    126   if(heap == this) {
     121    OS2Heap *curheap = OS2Heap::heap;
     122    int i;
     123 
     124    // invalidate handle cache
     125    fhhm_lastHandle = 0;
     126    fhhm_lastHeap   = NULL;
     127 
     128    dprintf(("dtr OS2Heap, hPrimaryHeap = %X\n", hPrimaryHeap));
     129
     130    heaplistmutex.enter();
     131    if(heap == this) {
    127132        heap = next;
    128   }
    129   else {
     133    }
     134    else {
    130135        while(curheap->next != NULL) {
    131136                if(curheap->next == this) {
     
    135140                curheap = curheap->next;
    136141        }
    137   }
    138   heaplistmutex.leave();
    139 
    140   if(uheap) {
    141         _uclose(uheap);
    142         _udestroy(uheap, _FORCE);
    143         uheap = NULL;
    144   }
    145   if(pInitialHeapMem) {
    146         DosFreeMem(pInitialHeapMem);
    147         pInitialHeapMem = NULL;
    148   }
    149 
    150   dprintf(("dtr OS2Heap, hPrimaryHeap = %X done\n", hPrimaryHeap));
     142    }
     143    heaplistmutex.leave();
     144
     145    if(uheap) {
     146        _uclose(uheap);
     147        _udestroy(uheap, _FORCE);
     148        uheap = NULL;
     149    }     
     150    if(pInitialHeapMem) {
     151        DosFreeMem(pInitialHeapMem);
     152        pInitialHeapMem = NULL;
     153    }
     154
     155    dprintf(("dtr OS2Heap, hPrimaryHeap = %X done\n", hPrimaryHeap));
    151156}
    152157//******************************************************************************
     
    154159LPVOID OS2Heap::Alloc(DWORD dwFlags, DWORD dwBytes)
    155160{
    156  HEAPELEM *lpHeapObj;
    157  LPVOID    lpMem;
    158  DWORD     dwAllocBytes;
     161    HEAPELEM *lpHeapObj;
     162    LPVOID    lpMem;
     163    DWORD     dwAllocBytes;
    159164
    160165//  dprintf(("OS2Heap::Alloc\n"));
    161166
    162   //size must be multiple of 8 bytes
    163   dwAllocBytes = HEAP_ALIGN(dwBytes);
    164 
    165   lpMem = _umalloc(uheap, dwAllocBytes + HEAP_OVERHEAD);
    166   if(lpMem == NULL) {
    167       dprintf(("OS2Heap::Alloc, lpMem == NULL"));
    168       return(NULL);
    169   }
    170   if(dwFlags & HEAP_ZERO_MEMORY) {
    171       memset(lpMem, 0, dwAllocBytes+HEAP_OVERHEAD);
    172   }
     167    //size must be multiple of 8 bytes
     168    dwAllocBytes = HEAP_ALIGN(dwBytes);
     169
     170    lpMem = _umalloc(uheap, dwAllocBytes + HEAP_OVERHEAD);
     171    if(lpMem == NULL) {
     172        dprintf(("OS2Heap::Alloc, lpMem == NULL"));
     173        return(NULL);
     174    }
     175    if(dwFlags & HEAP_ZERO_MEMORY) {
     176        memset(lpMem, 0, dwAllocBytes+HEAP_OVERHEAD);
     177    }
    173178 
    174179#ifdef DEBUG
    175   totalAlloc += dwAllocBytes;
    176 #endif
    177  
    178   //align at 8 byte boundary
    179   lpHeapObj          = (HEAPELEM *)HEAP_ALIGN(lpMem);
    180   lpHeapObj->lpMem   = lpMem;
    181   lpHeapObj->magic   = MAGIC_NR_HEAP;
    182   lpHeapObj->orgsize = dwBytes; //original size
    183   lpHeapObj->cursize = dwBytes; //original size
     180    totalAlloc += dwAllocBytes;
     181#endif
     182 
     183    //align at 8 byte boundary
     184    lpHeapObj          = (HEAPELEM *)HEAP_ALIGN(lpMem);
     185    lpHeapObj->lpMem   = lpMem;
     186    lpHeapObj->magic   = MAGIC_NR_HEAP;
     187    lpHeapObj->orgsize = dwBytes;       //original size
     188    lpHeapObj->cursize = dwBytes;       //original size
    184189 
    185   return(LPVOID)(lpHeapObj+1);
     190    return(LPVOID)(lpHeapObj+1);
    186191}
    187192//******************************************************************************
     
    189194DWORD OS2Heap::Size(DWORD dwFlags, PVOID lpMem)
    190195{
    191  HEAPELEM *helem = GET_HEAPOBJ(lpMem);
    192 
    193   if(lpMem == NULL) {
     196    HEAPELEM *helem = GET_HEAPOBJ(lpMem);
     197
     198    if(lpMem == NULL) {
    194199        dprintf(("OS2Heap::Size lpMem == NULL\n"));
    195200        return -1;
    196   }
    197   /* verify lpMem address */
    198   if (lpMem >= (LPVOID)ulMaxAddr || lpMem < (LPVOID)0x10000)
    199   {
     201    }
     202    /* verify lpMem address */
     203    if (lpMem >= (LPVOID)ulMaxAddr || lpMem < (LPVOID)0x10000)
     204    {
     205        dprintf(("OS2Heap::Size ERROR BAD HEAP POINTER:%X\n", lpMem));
     206        return -1;
     207    }
     208
     209    if(helem->magic != MAGIC_NR_HEAP)
     210    {
    200211        dprintf(("OS2Heap::Size ERROR BAD HEAP POINTER:%X\n", lpMem));
    201212        return -1;
    202   }
    203 
    204   if(helem->magic != MAGIC_NR_HEAP)
    205   {
    206         dprintf(("OS2Heap::Size ERROR BAD HEAP POINTER:%X\n", lpMem));
    207         return -1;
    208   }
    209   return helem->cursize;  //return current size of memory block
     213    }
     214    return helem->cursize;  //return current size of memory block
    210215}
    211216//******************************************************************************
     
    213218LPVOID OS2Heap::ReAlloc(DWORD dwFlags, LPVOID lpMem, DWORD dwBytes)
    214219{
    215   HEAPELEM *helem = GET_HEAPOBJ(lpMem);
    216   LPVOID lpNewMem;
    217   int    i, maxSize;
    218 
    219   if (dwBytes == 0) return NULL;         // intercept stupid parameters
    220 
    221   //NOTE: Allocate memory using Alloc -> WINE controls depend on this, even
    222   //      though it apparently doesn't work in Windows.
    223   if (lpMem == 0)   return Alloc(dwFlags, dwBytes);
     220    HEAPELEM *helem = GET_HEAPOBJ(lpMem);
     221    LPVOID lpNewMem;
     222    int    i, maxSize;
     223
     224    if (dwBytes == 0) return NULL;         // intercept stupid parameters
     225
     226    //NOTE: Allocate memory using Alloc -> WINE controls depend on this, even
     227    //      though it apparently doesn't work in Windows.
     228    if (lpMem == 0)   return Alloc(dwFlags, dwBytes);
    224229//  if (lpMem == 0)   return NULL;
    225230
    226   if (helem->magic != MAGIC_NR_HEAP)
    227   {
    228     dprintf(("OS2Heap::ReAlloc ERROR BAD HEAP POINTER:%X\n", lpMem));
    229     return lpMem;
    230   }
    231 
    232   maxSize = HEAP_ALIGN(helem->orgsize);
    233   if (dwBytes <= maxSize) {
    234        dprintf(("ReAlloc with smaller size than original (%d); return old pointer", maxSize));
    235        //update current size so HeapSize will return the right value
    236        helem->cursize = dwBytes; 
    237        return lpMem; // if reallocation with same size don't do anything
    238   }
    239   lpNewMem = Alloc(dwFlags, dwBytes);
    240   memcpy(lpNewMem, lpMem, dwBytes < maxSize ? dwBytes : maxSize);
    241   Free(0, lpMem);
    242 
    243   if(lpNewMem == NULL)
    244   {
    245      dprintf(("OS2Heap::ReAlloc, no more memory left\n"));
    246   }
    247 
    248   return(lpNewMem);
     231    if (helem->magic != MAGIC_NR_HEAP)
     232    {
     233        dprintf(("OS2Heap::ReAlloc ERROR BAD HEAP POINTER:%X\n", lpMem));
     234        return lpMem;
     235    }
     236
     237    maxSize = HEAP_ALIGN(helem->orgsize);
     238    if (dwBytes <= maxSize) {
     239        dprintf(("ReAlloc with smaller size than original (%d); return old pointer", maxSize));
     240        //update current size so HeapSize will return the right value
     241        helem->cursize = dwBytes; 
     242        return lpMem; // if reallocation with same size don't do anything
     243    }
     244    lpNewMem = Alloc(dwFlags, dwBytes);
     245    memcpy(lpNewMem, lpMem, dwBytes < maxSize ? dwBytes : maxSize);
     246    Free(0, lpMem);
     247
     248    if(lpNewMem == NULL)
     249    {
     250        dprintf(("OS2Heap::ReAlloc, no more memory left\n"));
     251    }
     252   
     253    return(lpNewMem);
    249254}
    250255//******************************************************************************
     
    252257BOOL OS2Heap::Free(DWORD dwFlags, LPVOID lpMem)
    253258{
    254   HEAPELEM *helem = GET_HEAPOBJ(lpMem);
    255 
    256   /* verify lpMem address */
    257   if (lpMem >= (LPVOID)ulMaxAddr || lpMem < (LPVOID)0x10000)
    258   {
     259    HEAPELEM *helem = GET_HEAPOBJ(lpMem);
     260
     261    /* verify lpMem address */
     262    if (lpMem >= (LPVOID)ulMaxAddr || lpMem < (LPVOID)0x10000)
     263    {
    259264        dprintf(("OS2Heap::Free ERROR BAD HEAP POINTER:%X\n", lpMem));
    260265        return FALSE;
    261   }
    262 
    263   if(helem->magic != MAGIC_NR_HEAP)
    264   {
     266    }
     267
     268    if(helem->magic != MAGIC_NR_HEAP)
     269    {
    265270        dprintf(("OS2Heap::Free ERROR BAD HEAP POINTER:%X\n", lpMem));
    266271        return FALSE;
    267   }
     272    }
    268273
    269274#ifdef DEBUG1
    270   int size = Size(0, lpMem);
    271   dprintf(("OS2Heap::Free lpMem = %X, size %d\n", lpMem, size));
     275    int size = Size(0, lpMem);
     276    dprintf(("OS2Heap::Free lpMem = %X, size %d\n", lpMem, size));
    272277#ifdef DEBUG
    273   totalAlloc -= size;
    274 #endif
    275 #endif
    276 
    277   free(helem->lpMem);
    278   return(TRUE);
     278    totalAlloc -= size;
     279#endif
     280#endif
     281
     282    free(helem->lpMem);
     283    return(TRUE);
    279284}
    280285//******************************************************************************
     
    282287DWORD OS2Heap::Compact(DWORD dwFlags)
    283288{
    284   dprintf(("OS2Heap::Compact, %X- stub\n", dwFlags));
    285   return(0);
     289    dprintf(("OS2Heap::Compact, %X- stub\n", dwFlags));
     290    return(0);
    286291}
    287292//******************************************************************************
     
    289294BOOL OS2Heap::Validate(DWORD dwFlags, LPCVOID lpMem)
    290295{
    291   HEAPELEM *helem = GET_HEAPOBJ(lpMem);
    292 
    293   dprintf(("OS2Heap::Validate, %X %X", dwFlags, lpMem));
    294 
    295   /* verify lpMem address */
    296   if (lpMem >= (LPVOID)ulMaxAddr || lpMem < (LPVOID)0x10000)
    297   {
     296    HEAPELEM *helem = GET_HEAPOBJ(lpMem);
     297
     298    dprintf(("OS2Heap::Validate, %X %X", dwFlags, lpMem));
     299
     300    /* verify lpMem address */
     301    if (lpMem >= (LPVOID)ulMaxAddr || lpMem < (LPVOID)0x10000)
     302    {
    298303        dprintf(("OS2Heap::Validate BAD HEAP POINTER:%X\n", lpMem));
    299304        return FALSE;
    300   }
    301 
    302   if(helem->magic != MAGIC_NR_HEAP)
    303   {
     305    }
     306
     307    if(helem->magic != MAGIC_NR_HEAP)
     308    {
    304309        dprintf(("OS2Heap::Validate BAD HEAP POINTER:%X\n", lpMem));
    305310        return FALSE;
    306   }
    307   return(TRUE);
     311    }
     312    return(TRUE);
    308313}
    309314//******************************************************************************
     
    311316BOOL OS2Heap::Walk(void *lpEntry)
    312317{
    313   dprintf(("OS2Heap::Walk, %X - stub? (TRUE)\n", lpEntry));
    314   return(TRUE);
     318    dprintf(("OS2Heap::Walk, %X - stub? (TRUE)\n", lpEntry));
     319    return(TRUE);
    315320}
    316321//******************************************************************************
     
    363368void * _LNK_CONV getmoreHeapMem(Heap_t pHeap, size_t *size, int *clean)
    364369{
    365  APIRET rc;
    366  PVOID newblock;
    367 
    368   dprintf(("KERNEL32: getmoreHeapMem(%08xh, %08xh, %08xh)", pHeap, *size, *clean));
    369 
    370   /* round the size up to a multiple of 64K */
    371   //NOTE: MUST use 64kb here or else we are at risk of running out of virtual
    372   //      memory space. (when allocating 4kb we actually get 4kb + 60k uncommited)
    373   *size = ( (*size / 65536) + 1) * 65536;
    374 
    375   rc = DosAllocMem(&newblock, *size, PAG_READ|PAG_WRITE|PAG_COMMIT|PAG_EXECUTE);
    376 ////  rc = DosAllocMem(&newblock, *size, flAllocMem|PAG_READ|PAG_WRITE|PAG_COMMIT|PAG_EXECUTE);
    377   if(rc != 0) {
    378         dprintf(("getmoreHeapMem: DosAllocMem failed with %d", rc));
    379         return FALSE;
    380   }
    381   *clean = _BLOCK_CLEAN;
    382   dprintf(("KERNEL32: getmoreHeapMem %x %d", newblock, *size));
    383   return newblock;
     370    APIRET rc;
     371    PVOID newblock;
     372
     373    dprintf(("KERNEL32: getmoreHeapMem(%08xh, %08xh, %08xh)", pHeap, *size, *clean));
     374
     375    /* round the size up to a multiple of 64K */
     376    //NOTE: MUST use 64kb here or else we are at risk of running out of virtual
     377    //      memory space. (when allocating 4kb we actually get 4kb + 60k uncommited)
     378    *size = ( (*size / 65536) + 1) * 65536;
     379
     380    rc = DosAllocMem(&newblock, *size, PAG_READ|PAG_WRITE|PAG_COMMIT|PAG_EXECUTE);
     381    if(rc != 0) {
     382        dprintf(("getmoreHeapMem: DosAllocMem failed with %d", rc));
     383        return FALSE;
     384    }
     385    *clean = _BLOCK_CLEAN;
     386    dprintf(("KERNEL32: getmoreHeapMem %x %d", newblock, *size));
     387    return newblock;
    384388}
    385389//******************************************************************************
     
    387391void _LNK_CONV releaseHeapMem(Heap_t pHeap, void *block, size_t size)
    388392{
    389   dprintf(("KERNEL32: releaseHeapMem %x %x %d", pHeap, block, size));
    390   DosFreeMem(block);
    391 }
    392 //******************************************************************************
    393 //******************************************************************************
     393    dprintf(("KERNEL32: releaseHeapMem %x %x %d", pHeap, block, size));
     394    DosFreeMem(block);
     395}
     396//******************************************************************************
     397//******************************************************************************
  • trunk/src/kernel32/oslibdos.h

    r8600 r8877  
    1 /* $Id: oslibdos.h,v 1.46 2002-06-08 11:40:15 sandervl Exp $ */
     1/* $Id: oslibdos.h,v 1.47 2002-07-15 14:28:52 sandervl Exp $ */
    22
    33/*
     
    2222void  OSLibInitWSeBFileIO();
    2323
    24 DWORD OSLibDosAliasMem(LPVOID pb, ULONG cb, LPVOID *ppbAlias, ULONG fl);
    25 DWORD OSLibDosAllocMem(LPVOID *lplpMemAddr, DWORD size, DWORD flags);
    26 DWORD OSLibDosFreeMem(LPVOID lpMemAddr);
    27 DWORD OSLibDosQueryMem(LPVOID lpMemAddr, DWORD *lpRangeSize, DWORD *lpAttr);
    28 DWORD OSLibDosSetMem(LPVOID lpMemAddr, DWORD size, DWORD flags);
    29 DWORD OSLibDosAllocSharedMem(LPVOID *lplpMemAddr, DWORD size, DWORD flags, LPSTR name);
    30 DWORD OSLibDosGetNamedSharedMem(LPVOID *lplpMemAddr, LPSTR name);
    3124DWORD OSLibDosChangeMaxFileHandles();
    3225
     
    4134#define OSLIB_ERROR_ACCESS_DENIED       2
    4235#define OSLIB_ERROR_INVALID_PARAMETER   3
    43 
    44 #ifndef __OS2_H__
    45 
    46 /* Access protection                                                          */
    47 #define PAG_READ          0x00000001U      /* read access                      */
    48 #define PAG_WRITE         0x00000002U      /* write access                     */
    49 #define PAG_EXECUTE       0x00000004U      /* execute access                   */
    50 #define PAG_GUARD         0x00000008U      /* guard protection                 */
    51 #define PAG_DEFAULT       0x00000400U      /* default (initial) access         */
    52 
    53 /* Commit                                                                     */
    54 #define PAG_COMMIT        0x00000010U      /* commit storage                   */
    55 #define PAG_DECOMMIT      0x00000020U      /* decommit storage                 */
    56 
    57 /* Allocation attributes                                                      */
    58 #define OBJ_TILE          0x00000040U      /* tile object                      */
    59 #define OBJ_PROTECTED     0x00000080U      /* protect object */
    60 #define OBJ_GETTABLE      0x00000100U      /* gettable by other processes      */
    61 #define OBJ_GIVEABLE      0x00000200U      /* giveable to other processes      */
    62 
    63 /* Allocation type (returned from DosQueryMem)                                */
    64 #define PAG_SHARED        0x00002000U     /* shared object                    */
    65 #define PAG_FREE          0x00004000U     /* pages are free                   */
    66 #define PAG_BASE          0x00010000U     /* first page in object             */
    67 
    68 #endif
    6936
    7037#define OSLIB_ACCESS_READONLY           1
     
    144111DWORD OSLibDosDupHandle(DWORD hFile, DWORD *hNew);
    145112DWORD OSLibDosSetFilePtr2(DWORD hFile, DWORD offset, DWORD method);
    146 
    147 #ifndef PAGE_SIZE
    148 #define PAGE_SIZE 4096
    149 #endif
    150113
    151114BOOL OSLibDosQueryProcTimes(DWORD procid, ULONG *kerneltime, ULONG *usertime);
  • trunk/src/kernel32/oslibmem.cpp

    r8866 r8877  
    1 /* $Id: oslibmem.cpp,v 1.2 2002-07-13 16:30:40 sandervl Exp $ */
     1/* $Id: oslibmem.cpp,v 1.3 2002-07-15 14:28:52 sandervl Exp $ */
    22/*
    33 * Wrappers for OS/2 Dos* API
     
    3535#include "initterm.h"
    3636#include "oslibdos.h"
     37#include "oslibmem.h"
    3738#include "dosqss.h"
    3839#include "win32k.h"
     
    5657    rc = DosQueryMem(pb, &size, &attr);
    5758    if(rc) {
    58         dprintf(("OSLibDosAliasMem: DosQueryMem %x %x return %d", pb, size, rc));
     59        dprintf(("!ERROR!: OSLibDosAliasMem: DosQueryMem %x %x return %d", pb, size, rc));
    5960        return rc;
    6061    }
     
    6263    size+= PAGE_SIZE;
    6364    if(size != cb) {
    64         dprintf(("ERROR: OSLibDosAliasMem: size != cb (%x!=%x)!!!!!!!!", size, cb));
     65        dprintf(("!WARNING!: OSLibDosAliasMem: size != cb (%x!=%x)!!!!!!!!", size, cb));
    6566        //ignore this and continue return 5;
    6667        attr = fl; //just use original protection flags (NOT CORRECT)
     
    7071        rc = DosSetMem(pb, size, fl);
    7172        if(rc) {
    72                 dprintf(("OSLibDosAliasMem: DosSetMem %x %x return %d", pb, size, rc));
     73                dprintf(("!ERROR!: OSLibDosAliasMem: DosSetMem %x %x return %d", pb, size, rc));
    7374                attr = fl;
    7475                //just continue for now
     
    7879    rc = DosAliasMem(pb, cb, ppbAlias, 2);
    7980    if(rc) {
    80         dprintf(("OSLibDosAliasMem: DosAliasMem %x %x returned %d", pb, cb, rc));
     81        dprintf(("!ERROR!: OSLibDosAliasMem: DosAliasMem %x %x returned %d", pb, cb, rc));
    8182        return rc;
    8283    }
     
    8485        rc = DosSetMem(pb, size, attr);
    8586        if(rc) {
    86             dprintf(("OSLibDosAliasMem: DosSetMem (2) %x %x return %d", pb, size, rc));
     87            dprintf(("!ERROR!: OSLibDosAliasMem: DosSetMem (2) %x %x return %d", pb, size, rc));
    8788            return rc;
    8889        }
     
    9192}
    9293//******************************************************************************
     94//Allocate memory aligned at 64kb boundary
    9395//******************************************************************************
    9496DWORD OSLibDosAllocMem(LPVOID *lplpMemAddr, DWORD cbSize, DWORD flFlags)
     
    114116    rc = DosAllocMem(&pvMemAddr, cbSize, flFlags | flAllocMem);
    115117    if(rc) {
    116         dprintf(("DosAllocMem failed with rc %d", rc));
     118        dprintf(("!ERROR!: DosAllocMem failed with rc %d", rc));
    117119        return rc;
    118120    }
    119     //TODO!!!!!!!!!!!!
    120 #if 0
    121121    // already 64k aligned ?
    122     if((DWORD) pvMemAddr & 0xFFFF)
     122    if((ULONG) pvMemAddr & 0xFFFF)
    123123    {
    124         DWORD addr64kb;
     124        ULONG addr64kb;
     125
     126        //free misaligned allocated memory
     127        DosFreeMem(pvMemAddr);
    125128
    126129        //Allocate 64kb more so we can round the address to a 64kb aligned value
    127         rc = DosAllocMem((PPVOID)&addr64kb, cbSize + 64*1024,  PAG_READ | flAllocMem);
     130        rc = DosAllocMem((PPVOID)&addr64kb, cbSize + 64*1024,  (flFlags & ~PAG_COMMIT) | flAllocMem);
    128131        if(rc) {
    129             dprintf(("DosAllocMem failed with rc %d", rc));
     132            dprintf(("!ERROR!: DosAllocMem failed with rc %d", rc));
    130133            return rc;
    131134        }
     135        dprintf(("Allocate aligned memory %x -> %x", addr64kb, (addr64kb + 0xFFFF) & ~0xFFFF));
     136
    132137        if(addr64kb & 0xFFFF) {
    133             addr64kb = (addr64kb + 0xFFFF) & 0xFFFF;
     138            addr64kb         = (addr64kb + 0xFFFF) & ~0xFFFF;
    134139        }
    135140        pvMemAddr = (PVOID)addr64kb;
    136141
    137142        //and set the correct page flags for the request range
    138         rc = DosSetMem(pvMemAddr, cbSize, flFlags | flAllocMem);
    139     }
    140 #endif
     143        if((flFlags & ~PAG_COMMIT) != flFlags) {
     144            rc = DosSetMem(pvMemAddr, cbSize, flFlags);
     145            if(rc) {
     146                dprintf(("!ERROR!: DosSetMem failed with rc %d", rc));
     147                return rc;
     148            }
     149        }
     150    }
     151
    141152    if(!rc)
    142153        *lplpMemAddr = pvMemAddr;
     
    145156}
    146157//******************************************************************************
     158//Locate the base page of a memory allocation (the page with the PAG_BASE attribute)
     159//******************************************************************************
     160PVOID OSLibDosFindMemBase(LPVOID lpMemAddr)
     161{
     162    ULONG  ulAttr, ulSize, ulAddr;
     163    APIRET rc;
     164   
     165    rc = DosQueryMem(lpMemAddr, &ulSize, &ulAttr);
     166    if(rc != NO_ERROR) {
     167        dprintf(("!ERROR!: OSLibDosFindMemBase: DosQueryMem %x failed with rc %d", lpMemAddr, rc));
     168        return lpMemAddr;
     169    }
     170    if(!(ulAttr & PAG_BASE)) {
     171        //Not the base of the initial allocation (can happen due to alignment) or app
     172        //passing address inside memory allocation range
     173        ulAddr  = (DWORD)lpMemAddr & ~0xFFF;
     174        ulAddr -= PAGE_SIZE;
     175
     176        while(ulAddr > 0)
     177        {
     178            rc = DosQueryMem((PVOID)ulAddr, &ulSize, &ulAttr);
     179            if(rc) {
     180                dprintf(("!ERROR!: OSLibDosFindMemBase: DosQueryMem %x failed with rc %d", lpMemAddr, rc));
     181                DebugInt3();
     182                return NULL;
     183            }
     184            if(ulAttr & PAG_BASE) {
     185                //Memory below the 512 MB boundary is always aligned at 64kb and VirtualAlloc only
     186                //returns high memory (if OS/2 version supports it)
     187                //If it is above the 512 MB boundary, then we must make sure the right base address
     188                //is returned. VirtualAlloc allocates extra memory to make sure it can return addresses
     189                //aligned at 64kb. If extra pages are needed, then the allocation base is inside
     190                //the filler region. In that case we must return the next 64kb address as base.
     191                if(ulAddr > MEM_TILED_CEILING) {
     192                    ulAddr = (ulAddr + 0xFFFF) & ~0xFFFF;
     193                }
     194                lpMemAddr = (PVOID)ulAddr;
     195                break;
     196            }
     197            ulAddr -= PAGE_SIZE;
     198        }
     199    }
     200    return lpMemAddr;
     201}
     202//******************************************************************************
    147203//******************************************************************************
    148204DWORD OSLibDosFreeMem(LPVOID lpMemAddr)
    149205{
     206    ULONG  ulAttr, ulSize, ulAddr;
     207    APIRET rc;
     208
     209    ulAddr  = (DWORD)lpMemAddr & ~0xFFF;
     210
     211    //Find base within previous 64kb (alignment can add filler pages)
     212    for(int i=0;i<16;i++) {
     213        rc = DosQueryMem((PVOID)ulAddr, &ulSize, &ulAttr);
     214        if(rc != NO_ERROR) {
     215            dprintf(("!ERROR!: OSLibDosFreeMem: DosQueryMem %x failed with rc %d", lpMemAddr, rc));
     216            i = 16; //fail
     217            break;
     218        }
     219        if(ulAttr & PAG_BASE) {
     220            break;
     221        }
     222        ulAddr -= PAGE_SIZE;
     223    }
     224    if(i == 16) {
     225        //oh, oh. didn't find base; shouldn't happen!!
     226        dprintf(("!ERROR!: OSLibDosFreeMem: Unable to find base of %x", lpMemAddr));
     227        DebugInt3();
     228    }
     229    else {
     230        lpMemAddr = (PVOID)ulAddr;
     231    }
    150232    return DosFreeMem(lpMemAddr);
    151233}
  • trunk/src/kernel32/virtual.cpp

    r8866 r8877  
    1 /* $Id: virtual.cpp,v 1.47 2002-07-13 16:30:40 sandervl Exp $ */
     1/* $Id: virtual.cpp,v 1.48 2002-07-15 14:28:52 sandervl Exp $ */
    22
    33/*
     
    2828#include "mmap.h"
    2929#include "oslibdos.h"
     30#include "oslibmem.h"
    3031
    3132#define DBG_LOCALLOG    DBG_virtual
     
    270271                             DWORD  fdwProtect)
    271272{
    272   PVOID Address = lpvAddress;
    273   ULONG flag = 0, base;
    274   DWORD rc;
    275 
    276   SetLastError(ERROR_SUCCESS);
    277 
    278   if (cbSize > 0x7fc00000)  /* 2Gb - 4Mb */
    279   {
    280     dprintf(("VirtualAlloc: size too large"));
     273    PVOID Address = lpvAddress;
     274    ULONG flag = 0, base;
     275    DWORD rc;
     276
     277    SetLastError(ERROR_SUCCESS);
     278
     279    if (cbSize > 0x7fc00000)  /* 2Gb - 4Mb */
     280    {
     281        dprintf(("VirtualAlloc: size too large"));
    281282        SetLastError( ERROR_OUTOFMEMORY );
    282283        return NULL;
    283   }
    284 
    285   if (!(fdwAllocationType & (MEM_COMMIT | MEM_RESERVE)) ||
     284    }
     285
     286    if (!(fdwAllocationType & (MEM_COMMIT | MEM_RESERVE)) ||
    286287       (fdwAllocationType & ~(MEM_COMMIT | MEM_RESERVE)))
    287   {
     288    {
    288289        dprintf(("VirtualAlloc: Invalid parameter"));
    289290        SetLastError( ERROR_INVALID_PARAMETER );
    290291        return NULL;
    291   }
    292 
    293   if(fdwAllocationType & MEM_COMMIT)
    294   {
     292    }
     293
     294    if(fdwAllocationType & MEM_COMMIT)
     295    {
    295296        dprintf(("VirtualAlloc: commit\n"));
    296297        flag = PAG_COMMIT;
    297   }
    298 
    299   if(fdwAllocationType & MEM_RESERVE) {
    300     //SvL: DosRead crashes if memory is initially reserved with write
     298    }
     299
     300    if(fdwAllocationType & MEM_RESERVE) {
     301        //SvL: DosRead crashes if memory is initially reserved with write
    301302        //     access disabled (OS/2 bug) even if the commit sets the page
    302303        //     flags to read/write:
    303     // DosSetMem does not alter the 16 bit selectors so if you change memory
    304     // attributes and then access the memory with a 16 bit API (such as DosRead),
    305     // it will have the old (alloc time) attributes
    306     flag |= PAG_READ|PAG_WRITE;
    307   }
    308   if(fdwProtect & PAGE_READONLY)     flag |= PAG_READ;
    309   if(fdwProtect & PAGE_NOACCESS)     flag |= PAG_READ; //can't do this in OS/2
    310   if(fdwProtect & PAGE_READWRITE)    flag |= (PAG_READ | PAG_WRITE);
    311   if(fdwProtect & PAGE_WRITECOPY)    flag |= (PAG_READ | PAG_WRITE);
    312 
    313   if(fdwProtect & PAGE_EXECUTE_READWRITE) flag |= (PAG_EXECUTE | PAG_WRITE | PAG_READ);
    314   if(fdwProtect & PAGE_EXECUTE_READ) flag |= (PAG_EXECUTE | PAG_READ);
    315   if(fdwProtect & PAGE_EXECUTE)      flag |= PAG_EXECUTE;
    316 
    317   if(fdwProtect & PAGE_GUARD) {
     304        // DosSetMem does not alter the 16 bit selectors so if you change memory
     305        // attributes and then access the memory with a 16 bit API (such as DosRead),
     306        // it will have the old (alloc time) attributes
     307        flag |= PAG_READ|PAG_WRITE;
     308    }
     309    if(fdwProtect & PAGE_READONLY)     flag |= PAG_READ;
     310    if(fdwProtect & PAGE_NOACCESS)     flag |= PAG_READ; //can't do this in OS/2
     311    if(fdwProtect & PAGE_READWRITE)    flag |= (PAG_READ | PAG_WRITE);
     312    if(fdwProtect & PAGE_WRITECOPY)    flag |= (PAG_READ | PAG_WRITE);
     313
     314    if(fdwProtect & PAGE_EXECUTE_READWRITE) flag |= (PAG_EXECUTE | PAG_WRITE | PAG_READ);
     315    if(fdwProtect & PAGE_EXECUTE_READ) flag |= (PAG_EXECUTE | PAG_READ);
     316    if(fdwProtect & PAGE_EXECUTE)      flag |= PAG_EXECUTE;
     317
     318    if(fdwProtect & PAGE_GUARD) {
    318319        dprintf(("ERROR: PAGE_GUARD bit set for VirtualAlloc -> we don't support this right now!"));
    319320        flag |= PAG_GUARD;
    320   }
    321 
    322   //just do this if other options are used
    323   if(!(flag & (PAG_READ | PAG_WRITE | PAG_EXECUTE)) || flag == 0)
    324   {
    325     dprintf(("VirtualAlloc: Unknown protection flags, default to read/write"));
    326     flag |= PAG_READ | PAG_WRITE;
    327   }
    328 
    329   if(lpvAddress)
    330   {
    331     Win32MemMap *map;
    332     ULONG offset, nrpages, accessflags = 0;
     321    }
    333322   
    334     nrpages = cbSize >> PAGE_SHIFT;
    335     if(cbSize & 0xFFF)
    336         nrpages++;
    337 
    338     if(flag & PAG_READ) {
    339         accessflags |= MEMMAP_ACCESS_READ;
    340     }
    341     if(flag & PAG_WRITE) {
    342         accessflags |= MEMMAP_ACCESS_WRITE;
    343     }
    344     if(flag & PAG_EXECUTE) {
    345         accessflags |= MEMMAP_ACCESS_EXECUTE;
    346     }
    347     map = Win32MemMapView::findMapByView((ULONG)lpvAddress, &offset, accessflags);
    348     if(map) {
    349         //TODO: We don't allow protection flag changes for mmaped files now
    350         map->commitPage(offset, FALSE, nrpages);
    351         return lpvAddress;
    352     }
    353   }
    354 
    355   // commit memory
    356   if(fdwAllocationType & MEM_COMMIT)
    357   {
    358     Address = lpvAddress;
    359 
    360     rc = OSLibDosSetMem(lpvAddress, cbSize, flag);
    361 
    362     //might try to commit larger part with same base address
    363     if(rc == OSLIB_ERROR_ACCESS_DENIED && cbSize > 4096 )
    364     { //knut: AND more than one page
    365         char *newbase = (char *)lpvAddress + ((cbSize-1) & 0xFFFFF000); //knut: lets not start after the last page!
    366         ULONG size, os2flags;
    367 
    368         while(newbase >= (char *)lpvAddress)
    369         {     //knut: should check first page to!!
    370             size     = 4096;
    371             os2flags = 0;
    372             rc = OSLibDosQueryMem(newbase, &size, &os2flags);
    373             if(rc)
    374                 break;
    375 
    376             if(os2flags & PAG_COMMIT)
     323    //just do this if other options are used
     324    if(!(flag & (PAG_READ | PAG_WRITE | PAG_EXECUTE)) || flag == 0)
     325    {
     326        dprintf(("VirtualAlloc: Unknown protection flags, default to read/write"));
     327        flag |= PAG_READ | PAG_WRITE;
     328    }
     329
     330    if(lpvAddress)
     331    {
     332        Win32MemMap *map;
     333        ULONG offset, nrpages, accessflags = 0;
     334   
     335        nrpages = cbSize >> PAGE_SHIFT;
     336        if(cbSize & 0xFFF)
     337            nrpages++;
     338
     339        if(flag & PAG_READ) {
     340            accessflags |= MEMMAP_ACCESS_READ;
     341        }
     342        if(flag & PAG_WRITE) {
     343            accessflags |= MEMMAP_ACCESS_WRITE;
     344        }
     345        if(flag & PAG_EXECUTE) {
     346            accessflags |= MEMMAP_ACCESS_EXECUTE;
     347        }
     348        map = Win32MemMapView::findMapByView((ULONG)lpvAddress, &offset, accessflags);
     349        if(map) {
     350            //TODO: We don't allow protection flag changes for mmaped files now
     351            map->commitPage(offset, FALSE, nrpages);
     352            return lpvAddress;
     353        }
     354    }
     355
     356    // commit memory
     357    if(fdwAllocationType & MEM_COMMIT)
     358    {
     359        Address = lpvAddress;
     360
     361        rc = OSLibDosSetMem(lpvAddress, cbSize, flag);
     362
     363        //might try to commit larger part with same base address
     364        if(rc == OSLIB_ERROR_ACCESS_DENIED && cbSize > 4096 )
     365        { //knut: AND more than one page
     366            char *newbase = (char *)lpvAddress + ((cbSize-1) & 0xFFFFF000); //knut: lets not start after the last page!
     367            ULONG size, os2flags;
     368   
     369            while(newbase >= (char *)lpvAddress)
     370            {     //knut: should check first page to!!
     371                size     = 4096;
     372                os2flags = 0;
     373                rc = OSLibDosQueryMem(newbase, &size, &os2flags);
     374                if(rc)
     375                    break;
     376
     377                if(os2flags & PAG_COMMIT)
     378                {
     379                    newbase += 4096;
     380                    break;
     381                }
     382                newbase -= 4096;
     383            }
     384
     385            if(rc == 0)
    377386            {
    378                 newbase += 4096;
    379                 break;
     387                //In case it wants to commit bytes that fall into the last
     388                //page of the previous commit command
     389                if(cbSize > ((int)newbase - (int)lpvAddress))
     390                    rc = OSLibDosSetMem(newbase, cbSize - ((int)newbase - (int)lpvAddress), flag);
    380391            }
    381             newbase -= 4096;
    382         }
    383 
    384         if(rc == 0)
     392            else  return(NULL);
     393        }
     394        else
    385395        {
    386             //In case it wants to commit bytes that fall into the last
    387             //page of the previous commit command
    388             if(cbSize > ((int)newbase - (int)lpvAddress))
    389                 rc = OSLibDosSetMem(newbase, cbSize - ((int)newbase - (int)lpvAddress), flag);
    390         }
    391         else  return(NULL);
    392 
    393     }
    394     else
    395     {
    396         if(rc == OSLIB_ERROR_INVALID_ADDRESS) {
    397             rc = OSLibDosAllocMem(&Address, cbSize, flag );
    398         }
    399         else {
    400             if(rc) {
    401                 //check if the app tries to commit an already commited part of memory or change the protection flags
    402                 ULONG size = cbSize, os2flags, newrc;
    403                 newrc = OSLibDosQueryMem(lpvAddress, &size, &os2flags);
    404                 if(newrc == 0) {
    405                     if(size >= cbSize && (os2flags & PAG_COMMIT)) {
    406                             dprintf(("VirtualAlloc: commit on committed memory"));
    407                             if((flag & (PAG_READ|PAG_WRITE|PAG_EXECUTE)) != (os2flags & (PAG_READ|PAG_WRITE|PAG_EXECUTE)))
    408                             {   //change protection flags
    409                                 DWORD tmp;
    410                                 if(VirtualProtect(lpvAddress, cbSize, fdwProtect, &tmp) == TRUE) {
    411                                     return lpvAddress;
     396            if(rc == OSLIB_ERROR_INVALID_ADDRESS) {
     397                rc = OSLibDosAllocMem(&Address, cbSize, flag );
     398            }           
     399            else {
     400                if(rc) {
     401                    //check if the app tries to commit an already commited part of memory or change the protection flags
     402                    ULONG size = cbSize, os2flags, newrc;
     403                    newrc = OSLibDosQueryMem(lpvAddress, &size, &os2flags);
     404                    if(newrc == 0) {
     405                        if(size >= cbSize && (os2flags & PAG_COMMIT)) {
     406                                dprintf(("VirtualAlloc: commit on committed memory"));
     407                                if((flag & (PAG_READ|PAG_WRITE|PAG_EXECUTE)) != (os2flags & (PAG_READ|PAG_WRITE|PAG_EXECUTE)))
     408                                {   //change protection flags
     409                                    DWORD tmp;
     410                                    if(VirtualProtect(lpvAddress, cbSize, fdwProtect, &tmp) == TRUE) {
     411                                        return lpvAddress;
     412                                    }
     413                                    dprintf(("ERROR: VirtualAlloc: commit on committed memory -> VirtualProtect failed!!"));
     414                                    return NULL;
    412415                                }
    413                                 dprintf(("ERROR: VirtualAlloc: commit on committed memory -> VirtualProtect failed!!"));
    414                                 return NULL;
    415                             }
    416                             //else everything ok
    417                             return lpvAddress;
     416                                //else everything ok
     417                                return lpvAddress;
     418                        }
     419                        else    dprintf(("Unexpected DosSetMem error %x", rc));
    418420                    }
    419                     else    dprintf(("Unexpected DosSetMem error %x", rc));
    420                 }
    421                 else {
    422                     dprintf(("Unexpected DosQueryMem error %x", newrc));
     421                    else {
     422                        dprintf(("Unexpected DosQueryMem error %x", newrc));
     423                    }
    423424                }
    424425            }
    425426        }
    426427    }
    427   }
    428   else
    429   {
    430     rc = OSLibDosAllocMem(&Address, cbSize, flag);
    431   }
    432 
    433   if(rc)
    434   {
    435     dprintf(("DosSetMem returned %d\n", rc));
    436     SetLastError( ERROR_OUTOFMEMORY );
    437     return(NULL);
    438   }
    439 
    440   dprintf(("VirtualAlloc returned %X\n", Address));
    441   return(Address);
     428    else
     429    {
     430        rc = OSLibDosAllocMem(&Address, cbSize, flag);
     431    }
     432
     433    if(rc)
     434    {
     435        dprintf(("DosSetMem returned %d\n", rc));
     436        SetLastError( ERROR_OUTOFMEMORY );
     437        return(NULL);
     438    }
     439
     440    dprintf(("VirtualAlloc returned %X\n", Address));
     441    return(Address);
    442442}
    443443//******************************************************************************
     
    665665    //      flags used in the initial call to VirtualAlloc
    666666    pmbiBuffer->AllocationProtect = pmbiBuffer->Protect;
    667     if(dAttr & PAG_BASE) {
    668         pmbiBuffer->AllocationBase = lpBase;
    669     }
    670     else
    671     {
    672         pmbiBuffer->AllocationBase = 0;
    673         while(lpBase > 0)
    674         {
    675             rc = OSLibDosQueryMem(lpBase, &cbRangeSize, &dAttr);
    676             if(rc) {
    677                 dprintf(("VirtualQuery - OSLibDosQueryMem %x %x returned %d\n",
    678                           lpBase, cbLength, rc));
    679                 break;
    680             }
    681             if(dAttr & PAG_BASE) {
    682                 pmbiBuffer->AllocationBase = lpBase;
    683                 break;
    684             }
    685             lpBase = (LPVOID)((ULONG)lpBase - PAGE_SIZE);
    686         }
    687     }
    688 #if 0
    689     //TODO!!!!!!!!!!!!
    690     //NOTE: !!!!!!!!!!!!!!!!!!!!!!!
    691     //Allocation base is always aligned at 64kb
    692     //the page with the PAG_BASE attribute might not be the real allocation base
    693     //(due to extra alloc + rounding (see oslibmem.cpp)
    694     //Only exception to this rule is the stack
    695     TEB *teb = GetThreadTEB();
    696     if(teb) {
    697         if(pmbiBuffer->AllocationBase >= teb->stack_low && pmbiBuffer->AllocationBase < teb->stack_top) {
    698              pmbiBuffer->AllocationBase = pmbiBuffer->AllocationBase;
    699         }
    700         else pmbiBuffer->AllocationBase = (LPVOID)(((DWORD)pmbiBuffer->AllocationBase + 0xFFFF) & 0xFFFF);
    701     }
    702     else pmbiBuffer->AllocationBase = (LPVOID)(((DWORD)pmbiBuffer->AllocationBase + 0xFFFF) & 0xFFFF);
    703     //END NOTE: !!!!!!!!!!!!!!!!!!!
    704 #endif
     667    pmbiBuffer->AllocationBase    = OSLibDosFindMemBase(lpBase);
    705668
    706669    dprintf(("Memory region alloc base          0x%08x", pmbiBuffer->AllocationBase));
  • trunk/src/kernel32/winexedummy.cpp

    r7797 r8877  
    1 /* $Id: winexedummy.cpp,v 1.1 2002-02-03 13:16:22 sandervl Exp $ */
     1/* $Id: winexedummy.cpp,v 1.2 2002-07-15 14:28:53 sandervl Exp $ */
    22
    33/*
     
    2525#include <win32api.h>
    2626#include <wprocess.h>
     27#include "initterm.h"
    2728
    2829//******************************************************************************
     
    3132BOOL WIN32API RegisterDummyExe(LPSTR pszExeName)
    3233{
    33   if(WinExe != NULL) //should never happen
     34    if(WinExe != NULL) //should never happen
    3435        delete(WinExe);
    3536
    36   Win32DummyExe *winexe;
     37    Win32DummyExe *winexe;
    3738
    38   winexe = new Win32DummyExe(pszExeName);
     39    winexe = new Win32DummyExe(pszExeName);
    3940
    40   if(winexe) {
     41    if(winexe) {
    4142        InitCommandLine(FALSE);
    42         winexe->start();
    43   }
    44   else {
     43            winexe->start();
     44    }
     45    else {
    4546        eprintf(("Win32DummyExe creation failed!\n"));
    4647        DebugInt3();
    47         return FALSE;
    48   }
    49   return TRUE;
     48       return FALSE;
     49    }
     50    return TRUE;
    5051}
    5152//******************************************************************************
     
    5556                   Win32ExeBase(-1), header(0)
    5657{
    57   dprintf(("Win32DummyExe ctor: %s", pszExeName));
    58   hinstance = (HINSTANCE)buildHeader(1, 0, IMAGE_SUBSYSTEM_WINDOWS_GUI);
    59   strcpy(szModule, pszExeName);
    60   strcpy(szFileName, pszExeName);
    61   setFullPath(pszExeName);
     58    dprintf(("Win32DummyExe ctor: %s", pszExeName));
     59    hinstance = (HINSTANCE)buildHeader(1, 0, IMAGE_SUBSYSTEM_WINDOWS_GUI);
     60    strcpy(szModule, pszExeName);
     61    strcpy(szFileName, pszExeName);
     62    setFullPath(pszExeName);
    6263}
    6364//******************************************************************************
     
    6566Win32DummyExe::~Win32DummyExe()
    6667{
    67   if(header) {
    68         DosFreeMem(header);
    69   }
     68    if(header) {
     69        DosFreeMem(header);
     70    }
    7071}
    7172//******************************************************************************
     
    9293                                  DWORD Subsystem)
    9394{
    94  APIRET rc;
    95  IMAGE_DOS_HEADER *pdosheader;
    96  PIMAGE_OPTIONAL_HEADER poh;
    97  PIMAGE_FILE_HEADER     pfh;
    98  DWORD *ntsig;
     95    APIRET rc;
     96    IMAGE_DOS_HEADER *pdosheader;
     97    PIMAGE_OPTIONAL_HEADER poh;
     98    PIMAGE_FILE_HEADER     pfh;
     99    DWORD *ntsig;
    99100
    100   rc = DosAllocMem(&header, 4096, PAG_READ | PAG_WRITE | PAG_COMMIT);
    101   if(rc) {
    102         dprintf(("ERROR: buildHeader DosAllocMem failed!! (rc=%x)", rc));
     101    rc = DosAllocMem(&header, 4096, PAG_READ | PAG_WRITE | PAG_COMMIT | flAllocMem);
     102    if(rc) {
     103            dprintf(("ERROR: buildHeader DosAllocMem failed!! (rc=%x)", rc));
    103104        DebugInt3();
    104         return NULL;
    105   }
    106   memcpy(header, dosHeader, sizeof(dosHeader));
    107   ntsig  = (DWORD *)((LPBYTE)header + sizeof(dosHeader));
    108   *ntsig = IMAGE_NT_SIGNATURE;
    109   pfh    = (PIMAGE_FILE_HEADER)(ntsig+1);
    110   pfh->Machine              = IMAGE_FILE_MACHINE_I386;
    111   pfh->NumberOfSections     = 0;
    112   pfh->TimeDateStamp        = 0x3794f60f;
    113   pfh->PointerToSymbolTable = 0;
    114   pfh->NumberOfSymbols      = 0;
    115   pfh->SizeOfOptionalHeader = sizeof(IMAGE_OPTIONAL_HEADER);
    116   pfh->Characteristics      = IMAGE_FILE_DLL | IMAGE_FILE_32BIT_MACHINE |
    117                               IMAGE_FILE_DEBUG_STRIPPED | IMAGE_FILE_EXECUTABLE_IMAGE |
    118                               IMAGE_FILE_RELOCS_STRIPPED;
    119   poh    = (PIMAGE_OPTIONAL_HEADER)(pfh+1);
    120   poh->Magic                       = IMAGE_NT_OPTIONAL_HDR_MAGIC;
    121   poh->MajorLinkerVersion          = 0x3;
    122   poh->MinorLinkerVersion          = 0xA;
    123   poh->SizeOfCode                  = 0;
    124   poh->SizeOfInitializedData       = 0;
    125   poh->SizeOfUninitializedData     = 0;
    126   poh->AddressOfEntryPoint         = 0;
    127   poh->BaseOfCode                  = 0;
    128   poh->BaseOfData                  = 0;
    129   poh->ImageBase                   = 0;
    130   poh->SectionAlignment            = 4096;
    131   poh->FileAlignment               = 512;
    132   poh->MajorOperatingSystemVersion = MajorImageVersion;
    133   poh->MinorOperatingSystemVersion = MinorImageVersion;
    134   poh->MajorImageVersion           = MajorImageVersion;
    135   poh->MinorImageVersion           = MinorImageVersion;
    136   poh->MajorSubsystemVersion       = ODINNT_MAJOR_VERSION;
    137   poh->MinorSubsystemVersion       = ODINNT_MINOR_VERSION;
    138   poh->Reserved1                   = 0;
    139   poh->SizeOfImage                 = 0;
    140   poh->SizeOfHeaders               = 1024;
    141   poh->CheckSum                    = 0;
    142   poh->Subsystem                   = Subsystem;
    143   poh->DllCharacteristics          = 0;
    144   poh->SizeOfStackReserve          = 1*1024*1024;
    145   poh->SizeOfStackCommit           = 4096;
    146   poh->SizeOfHeapReserve           = 1*1024*1024;
    147   poh->SizeOfHeapCommit            = 4096;
    148   poh->LoaderFlags                 = 0;
    149   poh->NumberOfRvaAndSizes         = 0;
     105            return NULL;
     106    }
     107    memcpy(header, dosHeader, sizeof(dosHeader));
     108    ntsig  = (DWORD *)((LPBYTE)header + sizeof(dosHeader));
     109    *ntsig = IMAGE_NT_SIGNATURE;
     110    pfh    = (PIMAGE_FILE_HEADER)(ntsig+1);
     111    pfh->Machine              = IMAGE_FILE_MACHINE_I386;
     112    pfh->NumberOfSections     = 0;
     113    pfh->TimeDateStamp        = 0x3794f60f;
     114    pfh->PointerToSymbolTable = 0;
     115    pfh->NumberOfSymbols      = 0;
     116    pfh->SizeOfOptionalHeader = sizeof(IMAGE_OPTIONAL_HEADER);
     117    pfh->Characteristics      = IMAGE_FILE_DLL | IMAGE_FILE_32BIT_MACHINE |
     118                                IMAGE_FILE_DEBUG_STRIPPED | IMAGE_FILE_EXECUTABLE_IMAGE |
     119                                IMAGE_FILE_RELOCS_STRIPPED;
     120    poh    = (PIMAGE_OPTIONAL_HEADER)(pfh+1);
     121    poh->Magic                       = IMAGE_NT_OPTIONAL_HDR_MAGIC;
     122    poh->MajorLinkerVersion          = 0x3;
     123    poh->MinorLinkerVersion          = 0xA;
     124    poh->SizeOfCode                  = 0;
     125    poh->SizeOfInitializedData       = 0;
     126    poh->SizeOfUninitializedData     = 0;
     127    poh->AddressOfEntryPoint         = 0;
     128    poh->BaseOfCode                  = 0;
     129    poh->BaseOfData                  = 0;
     130    poh->ImageBase                   = 0;
     131    poh->SectionAlignment            = 4096;
     132    poh->FileAlignment               = 512;
     133    poh->MajorOperatingSystemVersion = MajorImageVersion;
     134    poh->MinorOperatingSystemVersion = MinorImageVersion;
     135    poh->MajorImageVersion           = MajorImageVersion;
     136    poh->MinorImageVersion           = MinorImageVersion;
     137    poh->MajorSubsystemVersion       = ODINNT_MAJOR_VERSION;
     138    poh->MinorSubsystemVersion       = ODINNT_MINOR_VERSION;
     139    poh->Reserved1                   = 0;
     140    poh->SizeOfImage                 = 0;
     141    poh->SizeOfHeaders               = 1024;
     142    poh->CheckSum                    = 0;
     143    poh->Subsystem                   = Subsystem;
     144    poh->DllCharacteristics          = 0;
     145    poh->SizeOfStackReserve          = 1*1024*1024;
     146    poh->SizeOfStackCommit           = 4096;
     147    poh->SizeOfHeapReserve           = 1*1024*1024;
     148    poh->SizeOfHeapCommit            = 4096;
     149    poh->LoaderFlags                 = 0;
     150    poh->NumberOfRvaAndSizes         = 0;
    150151//  poh->DataDirectory[0]
    151152
    152   return header;
     153    return header;
    153154}
    154155//******************************************************************************
  • trunk/src/kernel32/winimagelx.cpp

    r7797 r8877  
    1 /* $Id: winimagelx.cpp,v 1.14 2002-02-03 13:16:22 sandervl Exp $ */
     1/* $Id: winimagelx.cpp,v 1.15 2002-07-15 14:28:53 sandervl Exp $ */
    22
    33/*
     
    6666               : Win32ImageBase(hInstance), header(0)
    6767{
    68  APIRET rc;
    69  char  *name;
     68    APIRET rc;
     69    char  *name;
    7070
    71   szFileName[0] = 0;
     71    szFileName[0] = 0;
    7272
    73   if(lpszCustomDllName) {
     73    if(lpszCustomDllName) {
    7474       name = lpszCustomDllName;
    7575       this->dwOrdinalBase = ::dwOrdinalBase;
    76   }
    77   else {
     76    }
     77    else {
    7878       name = OSLibGetDllName(hinstance);
    7979       this->dwOrdinalBase = 0;
    80   }
     80    }
    8181
    82   strcpy(szFileName, name);
    83   strupr(szFileName);
     82    strcpy(szFileName, name);
     83    strupr(szFileName);
    8484
    85   setFullPath(szFileName);
     85    setFullPath(szFileName);
    8686
    87   //Pointer to PE resource tree generates by wrc (or NULL for system dlls)
    88   pResRootDir = (PIMAGE_RESOURCE_DIRECTORY)pResData;
     87    //Pointer to PE resource tree generates by wrc (or NULL for system dlls)
     88    pResRootDir = (PIMAGE_RESOURCE_DIRECTORY)pResData;
    8989
    90   //ulRVAResourceSection contains the virtual address of the imagebase in the PE header
    91   //for the resource section (images loaded by the pe.exe)
    92   //For LX images, this is 0 as OffsetToData contains a relative offset
    93   ulRVAResourceSection = 0;
     90    //ulRVAResourceSection contains the virtual address of the imagebase in the PE header
     91    //for the resource section (images loaded by the pe.exe)
     92    //For LX images, this is 0 as OffsetToData contains a relative offset
     93    ulRVAResourceSection = 0;
    9494}
    9595//******************************************************************************
     
    9797Win32LxImage::~Win32LxImage()
    9898{
    99   if(header) {
    100         DosFreeMem(header);
    101   }
     99    if(header) {
     100        DosFreeMem(header);
     101    }
    102102}
    103103//******************************************************************************
     
    105105ULONG Win32LxImage::getApi(char *name)
    106106{
    107   APIRET      rc;
    108   ULONG       apiaddr;
     107    APIRET      rc;
     108    ULONG       apiaddr;
    109109
    110   rc = DosQueryProcAddr(hinstanceOS2, 0, name, (PFN *)&apiaddr);
    111   if(rc)
    112   {
    113         dprintf(("Win32LxImage::getApi %x %s -> rc = %d", hinstanceOS2, name, rc));
    114         return(0);
    115   }
    116   return(apiaddr);
     110    rc = DosQueryProcAddr(hinstanceOS2, 0, name, (PFN *)&apiaddr);
     111    if(rc)
     112    {
     113            dprintf(("Win32LxImage::getApi %x %s -> rc = %d", hinstanceOS2, name, rc));
     114            return(0);
     115    }
     116    return(apiaddr);
    117117}
    118118//******************************************************************************
     
    120120ULONG Win32LxImage::getApi(int ordinal)
    121121{
    122  APIRET      rc;
    123  ULONG       apiaddr;
     122    APIRET      rc;
     123    ULONG       apiaddr;
    124124
    125   rc = DosQueryProcAddr(hinstanceOS2, dwOrdinalBase+ordinal, NULL, (PFN *)&apiaddr);
    126   if(rc) {
    127         dprintf(("Win32LxImage::getApi %x %d -> rc = %d", hinstanceOS2, ordinal, rc));
    128         return(0);
    129   }
    130   return(apiaddr);
     125    rc = DosQueryProcAddr(hinstanceOS2, dwOrdinalBase+ordinal, NULL, (PFN *)&apiaddr);
     126    if(rc) {
     127        dprintf(("Win32LxImage::getApi %x %d -> rc = %d", hinstanceOS2, ordinal, rc));
     128        return(0);
     129    }
     130    return(apiaddr);
    131131}
    132132//******************************************************************************
     
    135135                                 DWORD Subsystem)
    136136{
    137  APIRET rc;
    138  IMAGE_DOS_HEADER *pdosheader;
    139  PIMAGE_OPTIONAL_HEADER poh;
    140  PIMAGE_FILE_HEADER     pfh;
    141  DWORD *ntsig;
     137    APIRET rc;
     138    IMAGE_DOS_HEADER *pdosheader;
     139    PIMAGE_OPTIONAL_HEADER poh;
     140    PIMAGE_FILE_HEADER     pfh;
     141    DWORD *ntsig;
    142142
    143   rc = DosAllocMem(&header, 4096, PAG_READ | PAG_WRITE | PAG_COMMIT);
    144   if(rc) {
    145         dprintf(("ERROR: buildHeader DosAllocMem failed!! (rc=%x)", rc));
     143    rc = DosAllocMem(&header, 4096, PAG_READ | PAG_WRITE | PAG_COMMIT | flAllocMem);
     144    if(rc) {
     145        dprintf(("ERROR: buildHeader DosAllocMem failed!! (rc=%x)", rc));
    146146        DebugInt3();
    147         return NULL;
    148   }
    149   memcpy(header, dosHeader, sizeof(dosHeader));
    150   ntsig  = (DWORD *)((LPBYTE)header + sizeof(dosHeader));
    151   *ntsig = IMAGE_NT_SIGNATURE;
    152   pfh    = (PIMAGE_FILE_HEADER)(ntsig+1);
    153   pfh->Machine              = IMAGE_FILE_MACHINE_I386;
    154   pfh->NumberOfSections     = 0;
    155   pfh->TimeDateStamp        = 0x3794f60f;
    156   pfh->PointerToSymbolTable = 0;
    157   pfh->NumberOfSymbols      = 0;
    158   pfh->SizeOfOptionalHeader = sizeof(IMAGE_OPTIONAL_HEADER);
    159   pfh->Characteristics      = IMAGE_FILE_DLL | IMAGE_FILE_32BIT_MACHINE |
    160                               IMAGE_FILE_DEBUG_STRIPPED | IMAGE_FILE_EXECUTABLE_IMAGE |
    161                               IMAGE_FILE_RELOCS_STRIPPED;
    162   poh    = (PIMAGE_OPTIONAL_HEADER)(pfh+1);
    163   poh->Magic                       = IMAGE_NT_OPTIONAL_HDR_MAGIC;
    164   poh->MajorLinkerVersion          = 0x3;
    165   poh->MinorLinkerVersion          = 0xA;
    166   poh->SizeOfCode                  = 0;
    167   poh->SizeOfInitializedData       = 0;
    168   poh->SizeOfUninitializedData     = 0;
    169   poh->AddressOfEntryPoint         = 0;
    170   poh->BaseOfCode                  = 0;
    171   poh->BaseOfData                  = 0;
    172   poh->ImageBase                   = 0;
    173   poh->SectionAlignment            = 4096;
    174   poh->FileAlignment               = 512;
    175   poh->MajorOperatingSystemVersion = MajorImageVersion;
    176   poh->MinorOperatingSystemVersion = MinorImageVersion;
    177   poh->MajorImageVersion           = MajorImageVersion;
    178   poh->MinorImageVersion           = MinorImageVersion;
    179   poh->MajorSubsystemVersion       = ODINNT_MAJOR_VERSION;
    180   poh->MinorSubsystemVersion       = ODINNT_MINOR_VERSION;
    181   poh->Reserved1                   = 0;
    182   poh->SizeOfImage                 = 0;
    183   poh->SizeOfHeaders               = 1024;
    184   poh->CheckSum                    = 0;
    185   poh->Subsystem                   = Subsystem;
    186   poh->DllCharacteristics          = 0;
    187   poh->SizeOfStackReserve          = 1*1024*1024;
    188   poh->SizeOfStackCommit           = 4096;
    189   poh->SizeOfHeapReserve           = 1*1024*1024;
    190   poh->SizeOfHeapCommit            = 4096;
    191   poh->LoaderFlags                 = 0;
    192   poh->NumberOfRvaAndSizes         = 0;
     147            return NULL;
     148    }
     149    memcpy(header, dosHeader, sizeof(dosHeader));
     150    ntsig  = (DWORD *)((LPBYTE)header + sizeof(dosHeader));
     151    *ntsig = IMAGE_NT_SIGNATURE;
     152    pfh    = (PIMAGE_FILE_HEADER)(ntsig+1);
     153    pfh->Machine              = IMAGE_FILE_MACHINE_I386;
     154    pfh->NumberOfSections     = 0;
     155    pfh->TimeDateStamp        = 0x3794f60f;
     156    pfh->PointerToSymbolTable = 0;
     157    pfh->NumberOfSymbols      = 0;
     158    pfh->SizeOfOptionalHeader = sizeof(IMAGE_OPTIONAL_HEADER);
     159    pfh->Characteristics      = IMAGE_FILE_DLL | IMAGE_FILE_32BIT_MACHINE |
     160                                IMAGE_FILE_DEBUG_STRIPPED | IMAGE_FILE_EXECUTABLE_IMAGE |
     161                                IMAGE_FILE_RELOCS_STRIPPED;
     162    poh    = (PIMAGE_OPTIONAL_HEADER)(pfh+1);
     163    poh->Magic                       = IMAGE_NT_OPTIONAL_HDR_MAGIC;
     164    poh->MajorLinkerVersion          = 0x3;
     165    poh->MinorLinkerVersion          = 0xA;
     166    poh->SizeOfCode                  = 0;
     167    poh->SizeOfInitializedData       = 0;
     168    poh->SizeOfUninitializedData     = 0;
     169    poh->AddressOfEntryPoint         = 0;
     170    poh->BaseOfCode                  = 0;
     171    poh->BaseOfData                  = 0;
     172    poh->ImageBase                   = 0;
     173    poh->SectionAlignment            = 4096;
     174    poh->FileAlignment               = 512;
     175    poh->MajorOperatingSystemVersion = MajorImageVersion;
     176    poh->MinorOperatingSystemVersion = MinorImageVersion;
     177    poh->MajorImageVersion           = MajorImageVersion;
     178    poh->MinorImageVersion           = MinorImageVersion;
     179    poh->MajorSubsystemVersion       = ODINNT_MAJOR_VERSION;
     180    poh->MinorSubsystemVersion       = ODINNT_MINOR_VERSION;
     181    poh->Reserved1                   = 0;
     182    poh->SizeOfImage                 = 0;
     183    poh->SizeOfHeaders               = 1024;
     184    poh->CheckSum                    = 0;
     185    poh->Subsystem                   = Subsystem;
     186    poh->DllCharacteristics          = 0;
     187    poh->SizeOfStackReserve          = 1*1024*1024;
     188    poh->SizeOfStackCommit           = 4096;
     189    poh->SizeOfHeapReserve           = 1*1024*1024;
     190    poh->SizeOfHeapCommit            = 4096;
     191    poh->LoaderFlags                 = 0;
     192    poh->NumberOfRvaAndSizes         = 0;
    193193//  poh->DataDirectory[0]
    194194
    195   return header;
     195    return header;
    196196}
    197197//******************************************************************************
  • trunk/src/kernel32/winimagepeldr.cpp

    r7811 r8877  
    1 /* $Id: winimagepeldr.cpp,v 1.95 2002-02-06 16:33:39 sandervl Exp $ */
     1/* $Id: winimagepeldr.cpp,v 1.96 2002-07-15 14:28:53 sandervl Exp $ */
    22
    33/*
     
    5353#include <win\virtual.h>
    5454#include "oslibdos.h"
     55#include "oslibmem.h"
    5556#include "mmap.h"
    5657#include <wprocess.h>
     
    159160
    160161    if(realBaseAddress)
    161         DosFreeMem((PVOID)realBaseAddress);
     162        OSLibDosFreeMem((PVOID)realBaseAddress);
    162163
    163164    if(nameexports)
     
    924925        return allocFixedMem(reservedMem);
    925926    }
    926     rc = DosAllocMem((PPVOID)&baseAddress, imageSize, PAG_READ | PAG_WRITE | flAllocMem);
     927    rc = OSLibDosAllocMem((PPVOID)&baseAddress, imageSize, PAG_READ | PAG_WRITE);
    927928    if(rc) {
    928929        dprintf((LOG, "Win32PeLdrImage::allocSections, DosAllocMem returned %d", rc));
     
    10111012    }
    10121013    while(TRUE) {
    1013         rc = DosAllocMem((PPVOID)&address, FALLOC_SIZE, PAG_READ | allocFlags);
     1014        rc = OSLibDosAllocMem((PPVOID)&address, FALLOC_SIZE, PAG_READ | allocFlags);
    10141015        if(rc) break;
    10151016
     
    10171018        if(address + FALLOC_SIZE >= oh.ImageBase) {
    10181019            if(address > oh.ImageBase) {//we've passed it!
    1019                 DosFreeMem((PVOID)address);
     1020                OSLibDosFreeMem((PVOID)address);
    10201021                break;
    10211022            }
    10221023            //found the right address
    1023             DosFreeMem((PVOID)address);
     1024            OSLibDosFreeMem((PVOID)address);
    10241025
    10251026            diff = oh.ImageBase - address;
    10261027            if(diff) {
    1027                 rc = DosAllocMem((PPVOID)&address, diff, PAG_READ | allocFlags);
     1028                rc = OSLibDosAllocMem((PPVOID)&address, diff, PAG_READ | allocFlags);
    10281029                if(rc) break;
    10291030            }
    1030             rc = DosAllocMem((PPVOID)&baseAddress, imageSize, PAG_READ | PAG_WRITE | allocFlags);
     1031            rc = OSLibDosAllocMem((PPVOID)&baseAddress, imageSize, PAG_READ | PAG_WRITE | allocFlags);
    10311032            if(rc) break;
    10321033
    1033             if(diff) DosFreeMem((PVOID)address);
     1034            if(diff) OSLibDosFreeMem((PVOID)address);
    10341035
    10351036            realBaseAddress = baseAddress;
     
    10391040    }
    10401041    for(i=0;i<alloccnt;i++) {
    1041         DosFreeMem((PVOID)memallocs[i]);
     1042        OSLibDosFreeMem((PVOID)memallocs[i]);
    10421043    }
    10431044    free(memallocs);
Note: See TracChangeset for help on using the changeset viewer.