home *** CD-ROM | disk | FTP | other *** search
/ Troubleshooting Netware Systems / CSTRIAL0196.BIN / attach / msj / v10n04 / wqa0495.exe / FREESEL2.C < prev    next >
Text File  |  1995-04-01  |  2KB  |  44 lines

  1. WORD __export _far _pascal GetFreeSelectorCount(void)
  2. {
  3.     DESCRIPTOR FAR *lpDescriptor;
  4.     unsigned i, ldtSelectorCount, freeSelectorCount;
  5.     long LDTSize;
  6.  
  7.     // get a pointer to the start of the LDT
  8.     lpDescriptor = MAKELP(GetLDTAlias(), 0);
  9.  
  10.     // if NULL pointer, then can't get count, return 0
  11.     if (!lpDescriptor)
  12.     {
  13.         MessageBox(0, "Unable to find LDT", 0, MB_OK);
  14.         return 0;
  15.     }
  16.  
  17.     // The alias selector that allows access to the LDT can have a limit
  18.     // of less than 64k to prevent poking around in the unused upper
  19.     // reaches.  Therefore, we need to figure out how many DESCRIPTOR
  20.     // entries can fit within the memory limit of the LDT alias.
  21.  
  22.     LDTSize = GetSelectorLimit(SELECTOROF(lpDescriptor))+ 1;
  23.     ldtSelectorCount = (WORD) (LDTSize / sizeof(DESCRIPTOR));
  24.  
  25.     // We'll count all the selectors above the LDT alias limit as being
  26.     // available, since Windows will increase the limit of the LDT alias
  27.     // selector as necessary
  28.     freeSelectorCount = 8192 - ldtSelectorCount;
  29.     
  30.     // Iterate through each descriptor of the LDT (or at least every selector
  31.     // that we're allowed to look at)
  32.     for ( i=0; i < ldtSelectorCount; i++ )
  33.     {
  34.         // Is it a free descriptor?  Check for a value of 0 in the 
  35.         // byte at offset 5 in the descriptor (the type field, and flags)
  36.         if ( lpDescriptor->type_and_flags == 0 )
  37.             freeSelectorCount++;
  38.         
  39.         lpDescriptor++;     // Advance to the next descriptor
  40.     }
  41.  
  42.     return freeSelectorCount;
  43. }
  44.