Establishing Access Protection

When an OS/2 application commits a memory object, it specifies the types of access permitted for the memory object. This can be done at the same time the memory object is allocated, with DosAllocMem, or at a later time, using DosSetMem.

Any combination of read, write, execute, or guard-page access can be set, but at least read or write access must be specified when the memory object is committed; it is not possible to commit an object with no access protection attributes.

The application can also use DosSetMem to change the access permission of pages within a previously committed memory object. An application can permit read access to one page of the memory object and write access to the rest.

When using DosSetMem, all the pages in the range being changed must be either committed or decommitted.

The following code fragment commits a region of two pages within a previously allocated memory object, and sets read-only access rights for the region.

    #define  INCL_DOSMEMMGR   /* Memory Manager values */
    #include <os2.h>
    #include <stdio.h>

    PVOID   pBaseAddress;       /* Pointer to the range of pages to be changed */
    ULONG   ulRegionSize;       /* Size, in bytes, of the region to be changed */
    ULONG   ulAttributeFlags;   /* Flag describing the page range              */
    APIRET  ulrc;               /* Return code                                 */

    ulRegionSize = 8192;        /* Specify a two-page region                   */

    /* Obtain the base address  */
    ulrc = DosAllocMem((PVOID *) &pBaseAddress,
                       ulRegionSize,
                       PAG_WRITE);

    ulAttributeFlags = PAG_COMMIT |
                       PAG_READ;

    ulrc = DosSetMem(pBaseAddress,
                     ulRegionSize,
                     ulAttributeFlags);

    if (ulrc != 0) {
        printf("DosSetMem error: return code = %ld", ulrc);
        return;
    }


[Back: Committing and Decommitting Page Ranges]
[Next: Querying Memory Object Information]