Ignore:
Timestamp:
Aug 25, 1999, 10:57:14 AM (26 years ago)
Author:
phaller
Message:

Add: HMDeviceMemMapClass fixed

File:
1 edited

Legend:

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

    r657 r673  
    1 /* $Id: os2native.cpp,v 1.6 1999-08-24 12:23:24 sandervl Exp $ */
     1/* $Id: os2native.cpp,v 1.7 1999-08-25 08:57:14 phaller Exp $ */
    22
    33/*
     
    2020#define INCL_DOSMEMMGR
    2121#define INCL_DOSPROCESS
    22 #include <os2wrap.h>    //Odin32 OS/2 api wrappers
     22#include <os2wrap.h>                     //Odin32 OS/2 api wrappers
    2323#include <stdlib.h>
    2424#include <stdio.h>
     
    8080  if (cbSize > 0x7fc00000)  /* 2Gb - 4Mb */
    8181  {
    82         dprintf(("VirtualAlloc: size too large"));
     82   dprintf(("VirtualAlloc: size too large"));
    8383//        SetLastError( ERROR_OUTOFMEMORY );
    8484        return NULL;
     
    8888       (fdwAllocationType & ~(MEM_COMMIT | MEM_RESERVE)))
    8989  {
    90         dprintf(("VirtualAlloc: Invalid parameter"));
     90   dprintf(("VirtualAlloc: Invalid parameter"));
    9191//        SetLastError( ERROR_INVALID_PARAMETER );
    9292        return NULL;
     
    103103  if(fdwProtect & PAGE_EXECUTE_READWRITE) flag |= (PAG_EXECUTE | PAG_WRITE | PAG_READ);
    104104  if(fdwProtect & PAGE_EXECUTE_READ) flag |= (PAG_EXECUTE | PAG_READ);
    105   if(fdwProtect & PAGE_EXECUTE)      flag |= PAG_EXECUTE;
     105  if(fdwProtect & PAGE_EXECUTE)               flag |= PAG_EXECUTE;
    106106
    107107  if(fdwProtect & PAGE_GUARD)        flag |= PAG_GUARD;
    108108
    109109  //just do this if other options are used
    110   if(!(flag & (PAG_READ | PAG_WRITE | PAG_EXECUTE)) || flag == 0) {
    111         dprintf(("VirtualAlloc: Unknown protection flags, default to read/write"));
    112         flag |= PAG_READ | PAG_WRITE;
    113   }
    114 
    115   if(fdwAllocationType & MEM_COMMIT && lpvAddress != NULL) {
    116         Address = lpvAddress;
    117 
    118         rc = DosSetMem(lpvAddress, cbSize, flag);
     110  if(!(flag & (PAG_READ | PAG_WRITE | PAG_EXECUTE)) || flag == 0)
     111  {
     112    dprintf(("VirtualAlloc: Unknown protection flags, default to read/write"));
     113      flag |= PAG_READ | PAG_WRITE;
     114  }
     115
     116  if(fdwAllocationType & MEM_COMMIT && lpvAddress != NULL)
     117  {
     118    Address = lpvAddress;
     119
     120    rc = DosSetMem(lpvAddress, cbSize, flag);
    119121        //might try to commit larger part with same base address
    120         if(rc == ERROR_ACCESS_DENIED && cbSize > 4096 ) {//knut: AND more than one page
    121                 char *newbase = (char *)lpvAddress + ((cbSize-1) & 0xFFFFF000); //knut: lets not start after the last page!
    122                 ULONG size, os2flags;
    123 
    124                 while(newbase >= (char *)lpvAddress) {  //knut: should check first page to!!
    125                         size     = 4096;
    126                         os2flags = 0;
    127                         rc = DosQueryMem(newbase, &size, &os2flags);
    128                         if(rc)  break;
    129                         if(os2flags & PAG_COMMIT) {
    130                                 newbase += 4096;
    131                                 break;
    132                         }
    133                         newbase -= 4096;
    134                 }
    135                 if(rc == 0) {
    136                         //In case it wants to commit bytes that fall into the last
    137                         //page of the previous commit command
    138                         if(cbSize > ((int)newbase - (int)lpvAddress)) {
    139                                 rc = DosSetMem(newbase, cbSize - ((int)newbase - (int)lpvAddress), flag);
    140                         }
    141                 }
    142                 else    return(NULL);
     122    if(rc == ERROR_ACCESS_DENIED && cbSize > 4096 )
     123    { //knut: AND more than one page
     124      char *newbase = (char *)lpvAddress + ((cbSize-1) & 0xFFFFF000); //knut: lets not start after the last page!
     125      ULONG size, os2flags;
     126
     127      while(newbase >= (char *)lpvAddress)
     128      {  //knut: should check first page to!!
     129        size     = 4096;
     130        os2flags = 0;
     131        rc = DosQueryMem(newbase, &size, &os2flags);
     132        if(rc)
     133          break;
     134
     135        if(os2flags & PAG_COMMIT)
     136        {
     137          newbase += 4096;
     138          break;
    143139        }
    144         else
    145         {
    146                 if(rc == ERROR_INVALID_ADDRESS)
    147                 {
    148                         rc = DosAllocMem(&Address, cbSize, flag | flAllocMem );
    149                 }
    150                 else   
    151                 if(rc)  dprintf(("Unexpected DosSetMem error %x", rc));
    152         }
    153   }
    154   else {
     140        newbase -= 4096;
     141      }
     142
     143      if(rc == 0)
     144      {
     145        //In case it wants to commit bytes that fall into the last
     146        //page of the previous commit command
     147        if(cbSize > ((int)newbase - (int)lpvAddress))
     148          rc = DosSetMem(newbase, cbSize - ((int)newbase - (int)lpvAddress), flag);
     149      }
     150      else
     151        return(NULL);
     152
     153    }
     154    else
     155    {
     156      if(rc == ERROR_INVALID_ADDRESS)
     157        rc = DosAllocMem(&Address, cbSize, flag | flAllocMem );
     158      else
     159        if(rc)
     160          dprintf(("Unexpected DosSetMem error %x", rc));
     161    }
     162  }
     163  else
     164  {
    155165    /*knut: flAllocMem */
    156166    rc = DosAllocMem(&Address, cbSize, flag | flAllocMem );
     
    158168
    159169  //TODO: Set last error in case rc != 0
    160   if(rc) {
    161         dprintf(("DosSetMem returned %d\n", rc));
     170  if(rc)
     171  {
     172    dprintf(("DosSetMem returned %d\n", rc));
    162173//        SetLastError( ERROR_OUTOFMEMORY );
    163         return(NULL);
     174    return(NULL);
    164175  }
    165176
     
    265276
    266277  if(lpvAddress == NULL || pmbiBuffer == NULL || cbLength == 0) {
    267         return 0;
     278   return 0;
    268279  }
    269280  cbRangeSize = cbLength;
    270281  rc = DosQueryMem(lpvAddress, &cbRangeSize, &dAttr);
    271282  if(rc) {
    272         dprintf(("VirtualQuery - DosQueryMem %x %x returned %d\n", lpvAddress, cbLength, rc));
    273         return 0;
     283   dprintf(("VirtualQuery - DosQueryMem %x %x returned %d\n", lpvAddress, cbLength, rc));
     284   return 0;
    274285  }
    275286  memset(pmbiBuffer, 0, sizeof(MEMORY_BASIC_INFORMATION));
     
    291302
    292303  if(dAttr & PAG_FREE)
    293         pmbiBuffer->State = MEM_FREE;
     304   pmbiBuffer->State = MEM_FREE;
    294305  else
    295306  if(dAttr & PAG_COMMIT)
    296         pmbiBuffer->State = MEM_COMMIT;
    297   else  pmbiBuffer->State = MEM_RESERVE;
    298  
     307   pmbiBuffer->State = MEM_COMMIT;
     308  else   pmbiBuffer->State = MEM_RESERVE;
     309
    299310  if(!(dAttr & PAG_SHARED))
    300         pmbiBuffer->Type = MEM_PRIVATE;
     311   pmbiBuffer->Type = MEM_PRIVATE;
    301312
    302313  //TODO: This is not correct: AllocationProtect should contain the protection
     
    304315  pmbiBuffer->AllocationProtect = pmbiBuffer->Protect;
    305316  if(dAttr & PAG_BASE) {
    306         pmbiBuffer->AllocationBase = lpvAddress;
     317   pmbiBuffer->AllocationBase = lpvAddress;
    307318  }
    308319  else {
    309         while(lpvAddress > 0) {
    310                 rc = DosQueryMem(lpvAddress, &cbRangeSize, &dAttr);
    311                 if(rc) {
    312                         dprintf(("VirtualQuery - DosQueryMem %x %x returned %d\n", lpvAddress, cbLength, rc));
    313                         break;
    314                 }
    315                 if(dAttr & PAG_BASE) {
    316                         pmbiBuffer->AllocationBase = lpvAddress;
    317                         break;
    318                 }
    319                 lpvAddress = (LPVOID)((ULONG)lpvAddress - PAGE_SIZE);
    320         }
     320   while(lpvAddress > 0) {
     321      rc = DosQueryMem(lpvAddress, &cbRangeSize, &dAttr);
     322      if(rc) {
     323         dprintf(("VirtualQuery - DosQueryMem %x %x returned %d\n", lpvAddress, cbLength, rc));
     324         break;
     325      }
     326      if(dAttr & PAG_BASE) {
     327         pmbiBuffer->AllocationBase = lpvAddress;
     328         break;
     329      }
     330      lpvAddress = (LPVOID)((ULONG)lpvAddress - PAGE_SIZE);
     331   }
    321332  }
    322333  return sizeof(MEMORY_BASIC_INFORMATION);
Note: See TracChangeset for help on using the changeset viewer.