home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / WHEEL1.ZIP / ALLOCMEM.C < prev    next >
Text File  |  1990-02-12  |  4KB  |  151 lines

  1. // Includes
  2. // --------
  3.    #define  INCL_WIN
  4.    #define  INCL_DOS
  5.    #include <os2.h>
  6.  
  7.    #include "malloc.h"                 // Changed from <..> for Eikon
  8.    #include "memory.h"                 // Changed from <..> for Eikon
  9.  
  10.    #include "AllocMem.H"
  11.    #include "AllocMem.RH"
  12.  
  13.    #include "TellUser.H"
  14.  
  15.  
  16. // Constants
  17. // ---------
  18.    const PCHAR pszMemErr = "Insufficient memory to continue";
  19.  
  20.  
  21. // Function Declarations (see also AllocMem.H)
  22. // -------------------------------------------
  23.    VOID EXPENTRY AllocMemInitialize (VOID);
  24.  
  25.  
  26. // Function: AllocMemInitialize - Initialize AllocMem processing
  27. // -------------------------------------------------------------
  28.    VOID EXPENTRY AllocMemInitialize (VOID)
  29.  
  30.    // Define function data
  31.  
  32.      {static DOSFSRSEM dosfsrs;         // Semaphore to block registration
  33.       static BOOL fRegistered = FALSE;  // TRUE if already registered
  34.  
  35.    // Block the actions that follow such that only one process at a
  36.    // time executes them.
  37.  
  38.       DosFSRamSemRequest (&dosfsrs, SEM_INDEFINITE_WAIT);
  39.  
  40.    // Perform dummy initialization
  41.  
  42.       if (! fRegistered)
  43.         {
  44.          fRegistered = TRUE;
  45.         }
  46.  
  47.    // Release the block and return
  48.  
  49.       DosFSRamSemClear (&dosfsrs);
  50.      }
  51.  
  52.  
  53. // Function: AllocMemAlloc - Allocate memory
  54. // -----------------------------------------
  55.    PVOID EXPENTRY AllocMemAlloc (cbSize)
  56.  
  57.       LONG cbSize;                     // Size of memory to allocate
  58.  
  59.    // Define function data
  60.  
  61.      {PVOID pvData;                    // Pointer to new memory
  62.  
  63.    // Verify that request is for les than 65535 bytes (temporary restriction)
  64.  
  65.       if (cbSize > 65535L)
  66.         {TellUser (ERROR_TOO_LARGE, ALLOCMEM_MODNAME, MB_OK | MB_ICONHAND, cbSize);
  67.          DosExit (EXIT_PROCESS, 0);
  68.         }
  69.  
  70.    // Attempt to allocate new memory; issue an error message on failure
  71.  
  72.       while ((pvData = malloc ((size_t) cbSize)) == NULL)
  73.         {if (WinMessageBox (HWND_DESKTOP, HWND_DESKTOP, pszMemErr, NULL, 0,
  74.           MB_ABORTRETRYIGNORE | MB_ICONHAND) == MBID_ABORT)
  75.            DosExit (EXIT_PROCESS, 0);
  76.         }
  77.  
  78.    // Initialize new memory
  79.  
  80.       memset (pvData, 0, (size_t) cbSize);
  81.  
  82.    // Return new memory pointer
  83.  
  84.       return pvData;
  85.  
  86.      }
  87.  
  88.  
  89. // Function: AllocMemFree - Free memory
  90. // ------------------------------------
  91.    VOID EXPENTRY AllocMemFree (pvData)
  92.  
  93.       PVOID pvData;                    // -->memory to free
  94.  
  95.    // Free memory (assuming current restriction of 65535 maximum)
  96.  
  97.      {if (pvData) free (pvData);
  98.  
  99.      }
  100.  
  101.  
  102. // Function: AllocMemQuerySize - Query Size of Memory
  103. // --------------------------------------------------
  104.    ULONG EXPENTRY AllocMemQuerySize (pvData)
  105.  
  106.       PVOID pvData;                    // -->memory to query
  107.  
  108.    // Query memory size
  109.  
  110.      {return (pvData == NULL)? 0L : (ULONG) _msize (pvData);
  111.  
  112.      }
  113.  
  114.  
  115. // Function: AllocMemReAlloc - Reallocate memory
  116. // ---------------------------------------------
  117.    PVOID EXPENTRY AllocMemReAlloc (pvOld, cbSize)
  118.  
  119.       PVOID pvOld;                     // Pointer to old memory
  120.       LONG cbSize;                     // Size of memory to allocate
  121.  
  122.    // Define function data
  123.  
  124.      {PVOID pvNew;                     // Pointer to new memory
  125.  
  126.    // If no "old" memory supplied, simply allocate as normal
  127.  
  128.       if (pvOld == NULL)
  129.          return AllocMemAlloc (cbSize);
  130.  
  131.    // Verify that request is for les than 65535 bytes (temporary restriction)
  132.  
  133.       if (cbSize > 65535L)
  134.         {TellUser (ERROR_TOO_LARGE, ALLOCMEM_MODNAME, MB_OK | MB_ICONHAND, cbSize);
  135.          DosExit (EXIT_PROCESS, 0);
  136.         }
  137.  
  138.    // Attempt to allocate new memory; issue an error message on failure
  139.  
  140.       while ((pvNew = realloc (pvOld, (size_t) cbSize)) == NULL)
  141.         {if (WinMessageBox (HWND_DESKTOP, HWND_DESKTOP, pszMemErr, NULL, 0,
  142.           MB_ABORTRETRYIGNORE | MB_ICONHAND) == MBID_ABORT)
  143.            DosExit (EXIT_PROCESS, 0);
  144.         }
  145.  
  146.    // Return new memory pointer
  147.  
  148.       return pvNew;
  149.  
  150.      }
  151.