Groups | Search | Server Info | Login | Register
Groups > comp.os.os2.programmer.porting > #112
| From | Dave Yeo <dave.r.yeo@gmail.com> |
|---|---|
| Newsgroups | comp.os.os2.programmer.porting |
| Subject | Re: VirtualAlloc to DosAllocMem |
| Date | 2011-07-08 19:39 -0700 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <iv8f05$ccn$1@speranza.aioe.org> (permalink) |
| References | <iv5rr7$dqt$1@speranza.aioe.org> <4e176267$0$6545$9b4e6d93@newsspool4.arcor-online.net> <4e176505$0$6554$9b4e6d93@newsspool4.arcor-online.net> |
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
Back to comp.os.os2.programmer.porting | Previous | Next — Previous in thread | Next in thread | Find similar
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