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

1.1       root        1: /*
1.1.1.2 ! root        2:  * Example of DosAllocSeg/DosFreeSeg usage.
1.1       root        3:  *
                      4:  * Shows how to allocate a segment, create a
                      5:  * pointer to it, access the segment, and free it.
                      6:  * Creates one 16K segment by default, or the number
                      7:  * specified on the command line. After all are allocated,
                      8:  * writes a 0 into the first byte of each. If you run this on
                      9:  * a system with swapping enabled you can allocate more segments
1.1.1.2 ! root       10:  * than fit in physcical memory, and exercise your disk.
1.1       root       11:  *
                     12:  * This program works Family API mode, although obviously
                     13:  * without the benefit of virtual memory & swapping.
                     14:  *
1.1.1.2 ! root       15:  * Compile as: cl -AL -G0 -Lp alloc.c
1.1       root       16:  *
1.1.1.2 ! root       17:  * Created by Microsoft Corp. 1986
1.1       root       18:  */
1.1.1.2 ! root       19: #define INCL_DOSMEMMGR
1.1       root       20: 
1.1.1.2 ! root       21: #include <os2def.h>
1.1       root       22: #include <malloc.h>
1.1.1.2 ! root       23: #include <bsedos.h>
1.1       root       24: 
                     25: #define SEGSIZE        (16 * 1024)     /* size of each segment allocated */
                     26: #define NOTSHARED 0
                     27: 
                     28: main(argc, argv)
                     29: int argc;
                     30: char *argv[];
                     31: {
1.1.1.2 ! root       32:        USHORT *seglist;          /* pointer to list of segments */
        !            33:        char *p;                 /* pointer used to access segments */
1.1       root       34:        register int i, j;
1.1.1.2 ! root       35:        int rc;                  /* system call return code */
1.1       root       36: 
                     37:        if(argc == 2)                   /* number of segments to allocate */
                     38:                i = atoi(argv[1]);
                     39:        else
                     40:                i = 1;
                     41: 
1.1.1.2 ! root       42:        seglist = (USHORT *)malloc(2 * i);    /* list of segment selectors */
1.1       root       43: 
                     44:        /* allocate segment(s) */
                     45:        for(j = 0; j < i; j++) {
1.1.1.2 ! root       46:                rc = DosAllocSeg(SEGSIZE, (PSEL)&seglist[j], NOTSHARED);
1.1       root       47:                if(rc == 0)
                     48:                        printf("allocated segment %d\n", j);
                     49:                else {
                     50:                        printf("allocation failed on segment %d\n", j);
                     51:                        i = j;
                     52:                }
                     53:        }
                     54: 
                     55:        /* write 0 into first byte of each segment */
                     56:        for(j = 0; j < i; j++) {
1.1.1.2 ! root       57:                SELECTOROF(p) = seglist[j];         /* make long pointer to seg */
        !            58:                OFFSETOF(p) = 0;
1.1       root       59:                printf("touching segment %d\n", j);
                     60:                p[0] = 0;                       /* put zero in first byte */
                     61:        }
                     62: 
                     63:        /* free each segment */
                     64:        for(j = 0; j < i; j++) {
                     65:                printf("freeing segment %d\n", j);
1.1.1.2 ! root       66:                DosFreeSeg(seglist[j]);
1.1       root       67:        }
                     68: }

unix.superglobalmegacorp.com

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