Groups | Search | Server Info | Login | Register


Groups > comp.os.os2.programmer.porting > #108

Re: VirtualAlloc to DosAllocMem

Date 2011-07-08 22:03 +0200
From Heiko Nitzsche <hn-expires-27nov11@arcor.de>
Newsgroups comp.os.os2.programmer.porting
Subject Re: VirtualAlloc to DosAllocMem
References <iv5rr7$dqt$1@speranza.aioe.org>
Message-ID <4e176267$0$6545$9b4e6d93@newsspool4.arcor-online.net> (permalink)
Organization Arcor

Show all headers | View raw


Well, I'd propose the following:

static inline DWORD protection(bool writable, bool executable)
{
   return executable ?
          (writable ? (PAG_EXECUTE | PAG_READ | PAG_WRITE) : (PAG_EXECUTE | PAG_READ) :
          (writable ? (              PAG_READ | PAG_WRITE) :                PAG_READ);
}

You could also write it this way:

static inline DWORD protection(bool writable, bool executable)
{
   DWORD permissions = PAG_READ; // always readable
   if (writable)
   {
     permissions |= PAG_WRITE; // allow writing
   }
   if (executable)
   {
     permissions |= PAG_EXECUTE; // content may be executed
   }
   return permissions;
}

And finally

void* OSAllocator::reserveUncommitted(size_t bytes, Usage, bool writable, bool executable)
{
   void* result = NULL;
   if (DosAllocMem(&result, bytes, OBJ_ANY | protection(writable, executable)) &&
       DosAllocMem(&result, bytes,           protection(writable, executable)))
   {
     CRASH();
   }
   return result;
}


> At that I don't even know what the ? operator does. Any advice?

? operator is a ternary operator which means simply

value = condition ? value_if_true : value_if_false;

It is a shorter variant of

if (condition)
{
   value = value_if_true;
}
else
{
   value = value_if_false;
}

Because of the structure of the operator you can only have single
operation statements for true and false case.

Hope it helps.

Back to comp.os.os2.programmer.porting | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

VirtualAlloc to DosAllocMem Dave Yeo <dave.r.yeo@gmail.com> - 2011-07-07 19:59 -0700
  Re: VirtualAlloc to DosAllocMem Heiko Nitzsche <hn-expires-27nov11@arcor.de> - 2011-07-08 22:03 +0200
    Re: VirtualAlloc to DosAllocMem Heiko Nitzsche <hn-expires-27nov11@arcor.de> - 2011-07-08 22:14 +0200
      Re: VirtualAlloc to DosAllocMem Dave Yeo <dave.r.yeo@gmail.com> - 2011-07-08 19:39 -0700
        Re: VirtualAlloc to DosAllocMem Dave Yeo <dave.r.yeo@gmail.com> - 2011-07-08 21:57 -0700
    Re: VirtualAlloc to DosAllocMem Paul Ratcliffe <abuse@orac12.clara34.co56.uk78> - 2011-07-08 23:02 +0000
      Re: VirtualAlloc to DosAllocMem "Lars Erdmann" <lars.erdmann@arcor.de> - 2011-07-09 03:46 +0200
        Re: VirtualAlloc to DosAllocMem Paul Ratcliffe <abuse@orac12.clara34.co56.uk78> - 2011-07-09 11:32 +0000
          Re: VirtualAlloc to DosAllocMem Paul Smedley <paulDESPAM@DESPAMMsmedley.id.au> - 2011-07-10 11:39 +0930

csiph-web