home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / allocme2.zip / ALLOCMEM.C next >
Text File  |  1989-07-14  |  2KB  |  58 lines

  1. /*------------------------------------------*/
  2. /*  Memory Overcommitment                   */
  3. /*------------------------------------------*/
  4.  
  5. #include <doscall.h>  /* OS/2 API dynamic link lib */
  6. #include <stdio.h>    /* C std I/O run time lib */
  7. #include <string.h>   /* C string lib */
  8. #include <stdlib.h>
  9.  
  10.   unsigned  ErrorCode = 0;   /* error code return from OS/2 fnct calls */
  11.   unsigned  far *SelectorTable[256];  /* array of far ptrs */
  12.   int SegmentCount = 0;
  13.   int TableIndex=0;
  14.   int MaxIndex=0;
  15.   unsigned long PointerBuilder;  /* used to build a far ptr */
  16.   unsigned FakeDataValue; /* used to touch a new segment */
  17.  
  18. /* DosAllocSeg vars */
  19.   unsigned Size=0; /* means size = 64K */
  20.   unsigned Selector;  /* selector returned */
  21.   unsigned AllocFlags = 0;  /* not sharable or discardable */
  22.   
  23. /* DosExit vars */
  24.   unsigned ActionCode = 1;  /* exit all threads in process */
  25.   unsigned ResultCode = 0;  /* result save for DosCWait */
  26.  
  27.   void main(argc,argv,envp)
  28.    int argc;             /* argc is count of arg strings */
  29.    char *argv[ ];        /* argv array contains arg strings */
  30.    char *envp[ ];        /* envp is env strings */
  31.  
  32.    {
  33.     SegmentCount=atoi(argv[1]);         /* get # of segs to alloc */
  34.     printf ("Number of 64K segments to allocate is %d\n",SegmentCount);
  35.     do {            /* loop to allocate segments */
  36.         ErrorCode=DOSALLOCSEG (Size,(unsigned far *)&Selector,
  37.                                AllocFlags);   
  38.         if (ErrorCode == 0)
  39.            printf ("Successfully allocated segment %d\n",TableIndex+1);
  40.         PointerBuilder = Selector;
  41.         SelectorTable[TableIndex] = PointerBuilder << 16; /* put selector in
  42.                                                              ptr table */
  43.         ++TableIndex;
  44.         --SegmentCount;
  45.        }  while (SegmentCount >0);
  46.     MaxIndex = TableIndex - 1;
  47.     TableIndex = 0;
  48.  
  49.     for (;;) {  /* endless loop to touch segments */
  50.       FakeDataValue = *SelectorTable[TableIndex];
  51.       printf ("Just accessed segment %d\n",TableIndex+1);
  52.       ++TableIndex;
  53.       if (TableIndex > MaxIndex)  TableIndex = 0;
  54.       }
  55.  
  56.    DOSEXIT(ActionCode,ResultCode);
  57.    }
  58.