Annotation of os2sdk/demos/examples/suballoc/suballoc.c, revision 1.1.1.2

1.1.1.2 ! root        1: /* This example illustrates the use of the following MS OS/2 functions:
        !             2:  *     DosSubSet, DosSubAlloc, DosSubFree
1.1       root        3:  *
                      4:  * This example allocates a segment <A>, and initialises it for
                      5:  * suballocation. It suballocates a block <B> from <A>, and writes
                      6:  * some value into a word in the block <B>. It then grows segment
                      7:  * <A>, and notifies the suballocator of the new size. It checks
                      8:  * that the value written into block <B> is still there, frees block
                      9:  * <B>, frees segment <A>, and terminates.
                     10:  *
                     11:  * This example program is intended to illustrate the possible functions
                     12:  * rather than do anything useful. It can run as a Family API program.
                     13:  *
                     14:  * Compile as: cl -AL -G0 -Lp suballoc.c
                     15:  *
1.1.1.2 ! root       16:  * Created by Microsoft Corp. 1986
1.1       root       17:  */
                     18: 
1.1.1.2 ! root       19: #define INCL_DOSMEMMGR
1.1       root       20: 
1.1.1.2 ! root       21: #include       <os2def.h>     /* SELECTOROF and OFFSETOF macros  */
        !            22: #include       <bsedos.h>     /* MS OS/2 function declarations   */
        !            23: 
        !            24: /* definitions used in DosAllocSeg call */
1.1       root       25: #define        SIZEA   16000           /* bytes to allocate */
                     26: #define PRIVATE 0              /* segment will not be shared */
                     27: 
1.1.1.2 ! root       28: /* definitions used in DosSubSet call */
1.1       root       29: #define INIT    1              /* initialise size of subset in a segment*/
                     30: #define INCRSIZE 0             /* increase size of subset in a segment  */
                     31: 
                     32: #define        SUBSIZEA 10000          /* size to suballocate */
                     33: #define SIZER  40000           /* size to reallocate */
                     34: #define BLKSIZE  100           /* size of block within the subset       */
                     35: #define SUBSIZEB 20000         /* size to suballocate after realloc     */
                     36: #define VALUEA   5             /* value to write to first word in block */
                     37: 
                     38: main ()
                     39: {
1.1.1.2 ! root       40:        SEL             Selector;       /* selector for segment allocated    */
        !            41:        USHORT          BlockOffset,    /* offset to block in segment subset */
1.1       root       42:                        *addr;          /*32 bit address to a word in segment*/
                     43: 
1.1.1.2 ! root       44:        DosAllocSeg (SIZEA, &Selector, PRIVATE); /* allocate a segment */
1.1       root       45: 
                     46:        /* subset the segment to size SUBSIZEA ( must be =< SIZEA ) */
1.1.1.2 ! root       47:        DosSubSet (Selector, INIT, SUBSIZEA);
1.1       root       48: 
1.1.1.2 ! root       49:        DosSubAlloc(Selector, &BlockOffset, BLKSIZE); /* suballocate block B */
1.1       root       50: 
                     51:        /* write VALUEA in to first word in block B of the subset */
1.1.1.2 ! root       52:        SELECTOROF(addr) = Selector;
        !            53:        OFFSETOF(addr) = BlockOffset;
1.1       root       54:        *addr = VALUEA;
                     55:        
1.1.1.2 ! root       56:        DosReallocSeg (SIZER, Selector); /* grow the segment to size SIZER */
1.1       root       57: 
                     58:        /* subset the segment to size SUBSIZEB where,
                     59:          * (SUBSIZEB > SUBSIZEA) and (SUBSIZEB =< SIZER) */
1.1.1.2 ! root       60:        DosSubSet(Selector, INCRSIZE, SUBSIZEB);
1.1       root       61: 
                     62:        /* first word in block B should still contain VALUEA */
                     63:        if (*addr != VALUEA)
                     64:           printf ("*** error: unexpected value in suballocated block ***\n");
                     65: 
1.1.1.2 ! root       66:        DosSubFree (Selector, BlockOffset, BLKSIZE); /* free block B */
1.1       root       67: 
1.1.1.2 ! root       68:        DosFreeSeg (Selector);          /* free the segment */
1.1       root       69: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.