_MemAvail( ) API Library Routine Example

The following example builds a linked list of 1K memory handle allocations until available memory is used. It then frees allocations and returns the number of allocations made. _MemAvail( ) is used to terminate the first while loop.

Visual FoxPro Code

SET LIBRARY TO MEMAVAIL
? MEMAVAIL()  && displays approx. memory available in K

C Code

#include <pro_ext.h>

#define ALLOCSIZE 1024

FAR Example(ParamBlk FAR *parm)
{
   int nHandles = 0;
   MHANDLE head = 0, mh;

   while (_MemAvail(ALLOCSIZE)) 
   {
      mh = _AllocHand(ALLOCSIZE);
      *((MHANDLE *) _HandToPtr(mh)) = head;
      head = mh;
      nHandles++;
   }
   _RetInt(nHandles, 10);
   while (head != 0) 
   {
      mh = *((MHANDLE *) _HandToPtr(head));
      _FreeHand(head);
      head = mh;
   }
}

FoxInfo myFoxInfo[] = {
   {"MEMAVAIL", (FPFI) Example, 0, ""},
};
FoxTable _FoxTable = {
   (FoxTable FAR *) 0, sizeof(myFoxInfo)/sizeof(FoxInfo), myFoxInfo
};