home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / mag&info / msjv7_4.zip / NETBIOS2.ARJ / ALLOC.C next >
C/C++ Source or Header  |  1992-07-01  |  4KB  |  144 lines

  1. /****************************************************************************
  2.  
  3.     PROGRAM: ALLOC.C
  4.  
  5.     PURPOSE: Contains memory allocation/deallocation routines.
  6.  
  7.     NOTE:    All memory is allocated in global fixed memory.
  8.  
  9.     FUNCTIONS:
  10.             NcbAlloc    - Returns a pointer to an NCB which resides
  11.                           in global memory and is zero-initialized
  12.                           as well locked in memory.
  13.  
  14.             NcbAllocBuf - Returns a pointer to a buffer which resides
  15.                           in global memory and is zero-initialized
  16.                           as well locked in memory.
  17.             NcbFree     - Frees a specifies buffer from global (locked) memory
  18.  
  19.     History:
  20.                 January, 1992       Alok Sinha      Created
  21.  
  22.  
  23. ****************************************************************************/
  24.  
  25. // Include files
  26. #include "windows.h"
  27. #include "ncb.h"
  28. #include "alloc.h"
  29.  
  30. /****************************************************************************
  31.  
  32.     STRUCTURE: BLOCK
  33.  
  34.     PURPOSE:  Any allocated memory is allocated as a block which hides
  35.               the handle to itself within the structure. This can
  36.               therefore be used to unlock and free the block at
  37.               a later time.
  38.  
  39. ****************************************************************************/
  40.  
  41. typedef struct _Block
  42. {
  43.     HANDLE hMem;
  44.     BYTE   bMem;
  45. } BLOCK;
  46.  
  47. typedef BLOCK FAR   *PBLOCK;
  48. #define HANDLE_SIZE sizeof(HANDLE)
  49.  
  50. /****************************************************************************
  51.  
  52.     FUNCTION: NcbAlloc
  53.  
  54.     PURPOSE: Returns a pointer to an NCB which resides
  55.              in global memory. It is zero-initialized as well
  56.              as locked in memory. The number of NCB's to allocate
  57.              is specified in 'iNosNcb'
  58.  
  59. ****************************************************************************/
  60.  
  61.  
  62. PNCB   NcbAlloc   ( int iNosNcb )
  63. {
  64.     if (iNosNcb <= 0)
  65.         return (PNCB) NULL;
  66.  
  67.     return (PNCB) NcbAllocBuf ( (DWORD) sizeof (NCB) * iNosNcb );
  68. }
  69.  
  70. /****************************************************************************
  71.  
  72.     FUNCTION: NcbAllocBuf
  73.  
  74.     PURPOSE: Returns a pointer to a buffer which resides
  75.              in global memory. It is zero-initialized as well
  76.              The size of the buffer is specified in 'wDataLen'.
  77. ****************************************************************************/
  78.  
  79. LPVOID  NcbAllocBuf( DWORD wDataLen )
  80. {
  81.     HANDLE hMem;
  82.     LPSTR  lpMem;
  83.     PBLOCK pbMem;
  84.  
  85.     // Allocate global memory
  86.     hMem =  GlobalAlloc( GMEM_MOVEABLE | GMEM_ZEROINIT,
  87.                          (DWORD) (wDataLen + HANDLE_SIZE)
  88.                        );
  89.     // Return NULL on error
  90.     if (hMem == NULL)
  91.         return NULL;
  92.  
  93.     // Now, lock the memory. Return NULL on error
  94.     lpMem = GlobalLock(hMem);
  95.     if (lpMem == NULL)
  96.     {
  97.         GlobalFree ( hMem);
  98.         return NULL;
  99.     }
  100.     // Now, we are going to save the handle to the memory right in the
  101.     // memory block and return a pointer to first memory byte past handle
  102.  
  103.     pbMem = (PBLOCK) lpMem;
  104.     pbMem->hMem = hMem;
  105.     return (LPVOID) &(pbMem->bMem);
  106.  
  107. }
  108.  
  109. /****************************************************************************
  110.  
  111.     FUNCTION: NcbFree
  112.  
  113.     PURPOSE: Frees a piece of memory which was previously locked
  114.              and allocated in global memory. The memory to free
  115.              is specified in 'pvBuffer'.
  116.  
  117.     CAVEAT : No check is made to see if this memory was truely
  118.              allocated by NcbAlloc() or NcbAllocBuf().
  119.  
  120. ****************************************************************************/
  121.  
  122.  
  123. VOID   NcbFree(LPVOID pvBuffer)
  124. {
  125.     PBLOCK pbMem;
  126.  
  127.     // Check for NULL buffer.
  128.     if (pvBuffer==NULL)
  129.         return;
  130.  
  131.     // Get a block pointer from the passed buffer
  132.     pbMem = (PBLOCK)  ((LPSTR) pvBuffer - HANDLE_SIZE);
  133.     if (pbMem==NULL)
  134.         return;
  135.  
  136.     // Now unlock and free the memory
  137.     if ( GlobalUnlock( pbMem->hMem ) == TRUE)
  138.     {
  139.         GlobalFree( pbMem->hMem );
  140.     }
  141.  
  142.     return;
  143. }
  144.