Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.os.os2.programmer.porting > #107 > unrolled thread

VirtualAlloc to DosAllocMem

Started byDave Yeo <dave.r.yeo@gmail.com>
First post2011-07-07 19:59 -0700
Last post2011-07-10 11:39 +0930
Articles 9 — 5 participants

Back to article view | Back to comp.os.os2.programmer.porting


Contents

  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

#107 — VirtualAlloc to DosAllocMem

FromDave Yeo <dave.r.yeo@gmail.com>
Date2011-07-07 19:59 -0700
SubjectVirtualAlloc to DosAllocMem
Message-ID<iv5rr7$dqt$1@speranza.aioe.org>
For Yarr we need an implementation of OSAllocator* OSAllocatorWin.cpp has

namespace WTF {

static inline DWORD protection(bool writable, bool executable)
{
     return executable ?
         (writable ? PAGE_EXECUTE_READWRITE : PAGE_EXECUTE_READ) :
         (writable ? PAGE_READWRITE : PAGE_READONLY);
}

void* OSAllocator::reserveUncommitted(size_t bytes, Usage, bool 
writable, bool executable)
{
     void* result = VirtualAlloc(0, bytes, MEM_RESERVE, 
protection(writable, executable));
     if (!result)
         CRASH();
     return result;
}
...

So far I have in OSAllocatorOS2.cpp (borrowed from some of Rich's code),

namespace WTF {

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

This works but I'm unsure how to implement the protection() function. At 
that I don't even know what the ? operator does. Any advice?
Dave

[toc] | [next] | [standalone]


#108

FromHeiko Nitzsche <hn-expires-27nov11@arcor.de>
Date2011-07-08 22:03 +0200
Message-ID<4e176267$0$6545$9b4e6d93@newsspool4.arcor-online.net>
In reply to#107
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.

[toc] | [prev] | [next] | [standalone]


#109

FromHeiko Nitzsche <hn-expires-27nov11@arcor.de>
Date2011-07-08 22:14 +0200
Message-ID<4e176505$0$6554$9b4e6d93@newsspool4.arcor-online.net>
In reply to#108
According to the OS/2 TK docs you can even get rid of PAG_READ
when PAG_EXECUTE is set as then PAG_READ is implied. But of
course it doesn't hurt and improves readability if it there ;)
Beware that when querying the memory protection flags, for
executable memory the PAG_READ is not returned since OS/2 Warp
as it implies PAG_READ.

Before accessing the memory it has to be committed with
DosSetMem(..., PAG_DEFAULT | PAG_COMMIT). Potentially
there is another function in the allocator for this.

> 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.

[toc] | [prev] | [next] | [standalone]


#112

FromDave Yeo <dave.r.yeo@gmail.com>
Date2011-07-08 19:39 -0700
Message-ID<iv8f05$ccn$1@speranza.aioe.org>
In reply to#109
Heiko Nitzsche wrote:
> According to the OS/2 TK docs you can even get rid of PAG_READ
> when PAG_EXECUTE is set as then PAG_READ is implied. But of
> course it doesn't hurt and improves readability if it there ;)
> Beware that when querying the memory protection flags, for
> executable memory the PAG_READ is not returned since OS/2 Warp
> as it implies PAG_READ.
>
> Before accessing the memory it has to be committed with
> DosSetMem(..., PAG_DEFAULT | PAG_COMMIT). Potentially
> there is another function in the allocator for this.

Yes there are also reserveAndCommit(), commit(), decommit(), and 
releaseDecommitted. I used DosSetMem() for commit and decommit and 
DosFreeMem() for releaseDecommitted.

>
>> 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;
>> }
>>

I'll try the first example as it looks closest to the Windows file and 
it will be reviewed by someone who doesn't know OS/2.

>>
>>> 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.

Simple explanation, wonder why none of my C books seem to mention it.
Thanks for the help
Dave
ps Is DWORD signed or unsigned? IIRC the GCC headers are missing DWORD

[toc] | [prev] | [next] | [standalone]


#113

FromDave Yeo <dave.r.yeo@gmail.com>
Date2011-07-08 21:57 -0700
Message-ID<iv8n3o$r3j$1@speranza.aioe.org>
In reply to#112
Dave Yeo wrote:
> ps Is DWORD signed or unsigned? IIRC the GCC headers are missing DWORD

Had to change DWORD to ULONG to get around a protection not declared in 
this scope error and I thought you had made a mistake with brackets. I 
was wrong, your example compiles and hopefully will run after a couple 
of more hours of compiling.
Thanks
Dave

[toc] | [prev] | [next] | [standalone]


#110

FromPaul Ratcliffe <abuse@orac12.clara34.co56.uk78>
Date2011-07-08 23:02 +0000
Message-ID<slrnj1f34j.3nn.abuse@news.pr.network>
In reply to#108
On Fri, 08 Jul 2011 22:03:12 +0200, Heiko Nitzsche
<hn-expires-27nov11@arcor.de> wrote:

> 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;
> }

Strewth, you like typing don't you...

return PAG_READ | (writable ? PAG_WRITE : 0) | (executable ? PAG_EXECUTE : 0);

... but why use 1 line when you can use 10, eh?


i = i + 1; // add one to i

FFS.

[toc] | [prev] | [next] | [standalone]


#111

From"Lars Erdmann" <lars.erdmann@arcor.de>
Date2011-07-09 03:46 +0200
Message-ID<4e17b2e3$0$6555$9b4e6d93@newsspool4.arcor-online.net>
In reply to#110
"Paul Ratcliffe" <abuse@orac12.clara34.co56.uk78> schrieb im Newsbeitrag 
news:slrnj1f34j.3nn.abuse@news.pr.network...
> On Fri, 08 Jul 2011 22:03:12 +0200, Heiko Nitzsche
> <hn-expires-27nov11@arcor.de> wrote:
>
>> 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;
>> }
>
> Strewth, you like typing don't you...
>
> return PAG_READ | (writable ? PAG_WRITE : 0) | (executable ? PAG_EXECUTE : 
> 0);
>
> ... but why use 1 line when you can use 10, eh?

It's all a matter of style. In the end, most compilers will generate the 
same binary code from that.
At least if the optimizer is turned on. It depends on what you consider to 
be better readable.

By the way: In a DO178B project people might rip your balls off if they see 
"condensed" code like you have been writing.

Lars

[toc] | [prev] | [next] | [standalone]


#116

FromPaul Ratcliffe <abuse@orac12.clara34.co56.uk78>
Date2011-07-09 11:32 +0000
Message-ID<slrnj1gf22.522.abuse@news.pr.network>
In reply to#111
On Sat, 9 Jul 2011 03:46:10 +0200, Lars Erdmann <lars.erdmann@arcor.de> wrote:

>> Strewth, you like typing don't you...
>>
>> return PAG_READ | (writable ? PAG_WRITE : 0) | (executable ? PAG_EXECUTE : 
>> 0);
>>
>> ... but why use 1 line when you can use 10, eh?
> 
> It's all a matter of style.

Sometimes, sometimes not.

> In the end, most compilers will generate the same binary code from that.

Possibly, possibly not.

> At least if the optimizer is turned on. It depends on what you consider to 
> be better readable.

Yes, a simple one liner like the above is perfectly readable and much more
so than either of the examples quoted previously.

> By the way: In a DO178B project people might rip your balls off if they see 
> "condensed" code like you have been writing.

Fuck 'em.
That isn't "condensed" code. There is absolutely nothing wrong with it.

I see you didn't pick up on my little jibe about the pointless comments.

[toc] | [prev] | [next] | [standalone]


#117

FromPaul Smedley <paulDESPAM@DESPAMMsmedley.id.au>
Date2011-07-10 11:39 +0930
Message-ID<4e1909e9$0$29986$c3e8da3$5496439d@news.astraweb.com>
In reply to#116
Hi Paul,

On 07/09/2011 09:02 PM, Paul Ratcliffe wrote:
> Fuck 'em.
> That isn't "condensed" code. There is absolutely nothing wrong with it.
>
> I see you didn't pick up on my little jibe about the pointless comments.

You'll find non-Brits don't pick up on a LOT of british humour :)

Cheers,

Paul

PS I'm a brit (came to Australia when I was 2 )

[toc] | [prev] | [standalone]


Back to top | Article view | comp.os.os2.programmer.porting


csiph-web