home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / drgwps.zip / heap.c < prev    next >
C/C++ Source or Header  |  1993-08-02  |  10KB  |  231 lines

  1. /*********************************************************************
  2.  *                                                                   *
  3.  * MODULE NAME :  heap.c                 AUTHOR:  Rick Fishman       *
  4.  * DATE WRITTEN:  08-01-93                                           *
  5.  *                                                                   *
  6.  * DESCRIPTION:                                                      *
  7.  *                                                                   *
  8.  *   This module is part of the DRGPMWPS sample program. It is used  *
  9.  *   by both DRGPMWPS.EXE and DRGAGENT.DLL. It allocates and controls*
  10.  *   a shared memory heap that is common to both modules.            *
  11.  *                                                                   *
  12.  * CALLABLE FUNCTIONS:                                               *
  13.  *                                                                   *
  14.  *   HeapCreate                                                      *
  15.  *   HeapAlloc                                                       *
  16.  *   HeapFree                                                        *
  17.  *   HeapDestroy                                                     *
  18.  *                                                                   *
  19.  * HISTORY:                                                          *
  20.  *                                                                   *
  21.  *  08-01-93 - Started coding.                                       *
  22.  *                                                                   *
  23.  *  Rick Fishman                                                     *
  24.  *  Code Blazers, Inc.                                               *
  25.  *  4113 Apricot                                                     *
  26.  *  Irvine, CA. 92720                                                *
  27.  *  CIS ID: 72251,750                                                *
  28.  *                                                                   *
  29.  *********************************************************************/
  30.  
  31. #pragma strings(readonly)
  32.  
  33. /*********************************************************************/
  34. /*------- Include relevant sections of the OS/2 header files --------*/
  35. /*********************************************************************/
  36.  
  37. #define  INCL_DOSERRORS
  38. #define  INCL_DOSPROCESS
  39. #define  INCL_DOSMEMMGR
  40.  
  41. /**********************************************************************/
  42. /*----------------------------- INCLUDES -----------------------------*/
  43. /**********************************************************************/
  44.  
  45. #include <os2.h>
  46. #include <stdarg.h>
  47. #include <stdio.h>
  48. #include <stdlib.h>
  49. #include <string.h>
  50. #include "common.h"
  51.  
  52. /*********************************************************************/
  53. /*------------------- APPLICATION DEFINITIONS -----------------------*/
  54. /*********************************************************************/
  55.  
  56. #define MEM_NAME  "\\SHAREMEM\\CBLAZERS\\DRGPMWPS\\HEAP.MEM"
  57.  
  58. #define HEAP_SIZE 0xC000
  59.  
  60. /**********************************************************************/
  61. /*---------------------------- STRUCTURES ----------------------------*/
  62. /**********************************************************************/
  63.  
  64.  
  65. /**********************************************************************/
  66. /*----------------------- FUNCTION PROTOTYPES ------------------------*/
  67. /**********************************************************************/
  68.  
  69.  
  70. /**********************************************************************/
  71. /*------------------------ GLOBAL VARIABLES --------------------------*/
  72. /**********************************************************************/
  73.  
  74. PBYTE pHeap;
  75.  
  76. /**********************************************************************/
  77. /*--------------------------- HeapCreate -----------------------------*/
  78. /*                                                                    */
  79. /*  CREATE THE SHARED HEAP USED BETWEEN THE PM AND WPS WINDOWS.       */
  80. /*                                                                    */
  81. /*  PARMS: nothing                                                    */
  82. /*                                                                    */
  83. /*  NOTES:                                                            */
  84. /*                                                                    */
  85. /*  RETURNS: nothing                                                  */
  86. /*                                                                    */
  87. /*--------------------------------------------------------------------*/
  88. /**********************************************************************/
  89. void HeapCreate()
  90. {
  91.     APIRET rc;
  92.     BOOL   fAlloced = TRUE;
  93.  
  94.     rc = DosGetNamedSharedMem( (PPVOID) &pHeap, MEM_NAME,
  95.                                PAG_READ | PAG_WRITE );
  96.     if( rc )
  97.     {
  98.         if( rc == ERROR_FILE_NOT_FOUND )
  99.         {
  100.             rc = DosAllocSharedMem( (PPVOID) &pHeap, MEM_NAME, HEAP_SIZE,
  101.                                     PAG_READ | PAG_WRITE );
  102.             if( rc )
  103.                 Msg( "HeapCreate DosAllocSharedMem failed! rc:%u", rc );
  104.         }
  105.         else
  106.             Msg( "HeapCreate DosGetNamedSharedMem failed! rc:%u", rc );
  107.     }
  108.     else
  109.         fAlloced = FALSE;
  110.  
  111.     if( !rc )
  112.     {
  113.         ULONG fl = DOSSUB_SPARSE_OBJ | DOSSUB_SERIALIZE;
  114.  
  115.         if( fAlloced )
  116.             fl |= DOSSUB_INIT;
  117.  
  118.         rc = DosSubSetMem( pHeap, fl, HEAP_SIZE );
  119.         if( rc )
  120.             Msg( "DosSubSetMem failed! rc:%u", rc );
  121.     }
  122. }
  123.  
  124. /**********************************************************************/
  125. /*---------------------------- HeapAlloc -----------------------------*/
  126. /*                                                                    */
  127. /*  ALLOCATE A BLOCK FROM THE SHARED HEAP.                            */
  128. /*                                                                    */
  129. /*  PARMS: size of block to allocate                                  */
  130. /*                                                                    */
  131. /*  NOTES: Since DosSubFreeMem needs the size of the block to be      */
  132. /*         freed, we add that info to the beginning of the allocated  */
  133. /*         block and return a pointer past that memory, effectively   */
  134. /*         hiding that data.                                          */
  135. /*                                                                    */
  136. /*  RETURNS: pointer to block or NULL if unsuccessful                 */
  137. /*                                                                    */
  138. /*--------------------------------------------------------------------*/
  139. /**********************************************************************/
  140. void *HeapAlloc( ULONG cb )
  141. {
  142.     APIRET rc = ERROR_NOACCESS;
  143.     ULONG  cbExtra = sizeof cb;
  144.     ULONG  cbBlock = cb + cbExtra;
  145.     PVOID  pBlock = NULL;
  146.  
  147.     if( pHeap )
  148.     {
  149.         rc = DosSubAllocMem( pHeap, &pBlock, cbBlock );
  150.         if( !rc )
  151.         {
  152.             (void) memset( pBlock, 0, (size_t) cbBlock );
  153.  
  154.             // Add info used by Free
  155.  
  156.             *((PULONG) pBlock) = cb;
  157.         }
  158.         else
  159.             Msg( "HeapAlloc DosSubAllocMem rc(%u)", rc );
  160.     }
  161.     else
  162.         Msg( "HeapAlloc NO HEAP!" );
  163.  
  164.     return( rc ? NULLHANDLE : ((PBYTE)pBlock + cbExtra) );
  165. }
  166.  
  167. /**********************************************************************/
  168. /*----------------------------- HeapFree -----------------------------*/
  169. /*                                                                    */
  170. /*  FREE A BLOCK FROM THE SHARED HEAP.                                */
  171. /*                                                                    */
  172. /*  PARMS: pointer to block to be freed                               */
  173. /*                                                                    */
  174. /*  NOTES: See the notes under HeapAlloc for an explanation about the */
  175. /*         'hidden' memory.                                           */
  176. /*                                                                    */
  177. /*  RETURNS: nothing                                                  */
  178. /*                                                                    */
  179. /*--------------------------------------------------------------------*/
  180. /**********************************************************************/
  181. void HeapFree( void *pBlock )
  182. {
  183.     ULONG  cbRegion = 1, flAlloc, cbBlock;
  184.     ULONG  cbExtra = sizeof cbBlock;
  185.     APIRET rc;
  186.  
  187.     pBlock = (PBYTE) pBlock - cbExtra;
  188.  
  189.     // Make sure it is valid before we start accessing it. We don't want no
  190.     // traps!
  191.  
  192.     rc = DosQueryMem( pBlock, &cbRegion, &flAlloc );
  193.     if( !rc )
  194.     {
  195.         cbBlock = *((PULONG) pBlock);
  196.         cbBlock += cbExtra;
  197.  
  198.         rc = DosSubFreeMem( pHeap, pBlock, cbBlock );
  199.         if( rc )
  200.             Msg( "HeapFree DosSubFreeMem rc(%u)", rc );
  201.     }
  202.     else
  203.         Msg( "HeapFree DosQueryMem rc(%u)", rc );
  204. }
  205.  
  206. /**********************************************************************/
  207. /*--------------------------- HeapDestroy ----------------------------*/
  208. /*                                                                    */
  209. /*  DESTROY THE SHARED HEAP.                                          */
  210. /*                                                                    */
  211. /*  PARMS: nothing                                                    */
  212. /*                                                                    */
  213. /*  NOTES:                                                            */
  214. /*                                                                    */
  215. /*  RETURNS: nothing                                                  */
  216. /*                                                                    */
  217. /*--------------------------------------------------------------------*/
  218. /**********************************************************************/
  219. void HeapDestroy()
  220. {
  221.     if( pHeap )
  222.     {
  223.         DosSubUnsetMem( pHeap );
  224.         DosFreeMem( pHeap );
  225.     }
  226. }
  227.  
  228. /*************************************************************************
  229.  *                     E N D     O F     S O U R C E                     *
  230.  *************************************************************************/
  231.