home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tcltk805.zip / tcl805s.zip / tcl8.0.5 / os2 / tclOS2Alloc.c < prev    next >
C/C++ Source or Header  |  2001-02-09  |  3KB  |  125 lines

  1. /* 
  2.  * tclOS2Alloc.c --
  3.  *
  4.  *    This file contains the definitions for the system memory allocation
  5.  *    routines TclSys* used in tclAlloc.c, which cannot be handled via
  6.  *    #define because DosAllocMem doesn't give the pointer as the return
  7.  *    value and there's no ReAlloc either.
  8.  *
  9.  * Copyright (c) 1999-2001 Illya Vaes
  10.  *
  11.  */
  12.  
  13. #include "tclOS2Int.h"
  14.  
  15.  
  16. /*
  17.  *----------------------------------------------------------------------
  18.  *
  19.  * TclpSysAlloc --
  20.  *
  21.  *    Allocate a new block of memory from the system.
  22.  *      There will always be a multiple of 4K actually allocated.
  23.  *
  24.  * Results:
  25.  *    Returns a pointer to a new block of memory.
  26.  *
  27.  * Side effects:
  28.  *    Obtains memory from system.
  29.  *
  30.  *----------------------------------------------------------------------
  31.  */
  32.  
  33. void *
  34. TclpSysAlloc(
  35.     long size,    /* Size of block to allocate. */
  36.     int isBin)        /* Is this a bin allocation? */
  37. {
  38.     PVOID memPtr;
  39.  
  40. #ifdef VERBOSE
  41.     printf("TclpSysAlloc %d (0x%x), isBin %d\n", size, size, isBin);
  42.         fflush(stdout);
  43. #endif
  44.     rc = DosAllocMem(&memPtr, (ULONG)size,
  45.                      PAG_COMMIT | PAG_READ | PAG_WRITE | PAG_EXECUTE);
  46.     if (rc != NO_ERROR) {
  47. #ifdef VERBOSE
  48.         printf("TclpSysAlloc: DosAllocMem %d ERROR %x\n", size, rc);
  49.         fflush(stdout);
  50. #endif
  51.         return NULL;
  52.     }
  53. #ifdef VERBOSE
  54.     printf("TclpSysAlloc: DosAllocMem %d OK: %x\n", size, memPtr);
  55.     fflush(stdout);
  56. #endif
  57.  
  58.     return memPtr;
  59. }
  60.  
  61. /*
  62.  *----------------------------------------------------------------------
  63.  *
  64.  * TclpSysFree --
  65.  *
  66.  *    Free memory that we allocated back to the system.
  67.  *
  68.  * Results:
  69.  *    None.
  70.  *
  71.  * Side effects:
  72.  *    Memory is freed.
  73.  *
  74.  *----------------------------------------------------------------------
  75.  */
  76.  
  77. void
  78. TclpSysFree(
  79.     void *ptr)        /* Pointer to system memory to free. */
  80. {   
  81.     /* Technically, this could be done with
  82.      * #define TclpSysFree(ptr) ((void)DosFreeMem((PVOID)ptr))
  83.      */
  84.     rc = DosFreeMem((PVOID)ptr);
  85. #ifdef VERBOSE
  86.     if (rc != NO_ERROR) {
  87.         printf("TclpSysFree: DosFreeMem %x ERROR %x\n", ptr, rc);
  88.     } else {
  89.         printf("TclpSysFree: DosFreeMem %x OK\n", ptr);
  90.     }
  91.     fflush(stdout);
  92. #endif
  93. }
  94.  
  95. /*
  96.  *----------------------------------------------------------------------
  97.  *
  98.  * TclpSysRealloc --
  99.  *
  100.  *    This function reallocates a chunk of system memory.
  101.  *
  102.  * Results:
  103.  *    Returns a pointer to the newly allocated block.
  104.  *
  105.  * Side effects:
  106.  *    May copy the contents of the original block to the new block
  107.  *    and deallocate the original block.
  108.  *
  109.  *----------------------------------------------------------------------
  110.  */
  111.  
  112. void *
  113. TclpSysRealloc(
  114.     void *oldPtr,        /* Original block. */
  115.     unsigned int size)    /* New size of block. */
  116. {   
  117. #ifdef VERBOSE
  118.     printf("TclpSysRealloc\n");
  119.     fflush(stdout);
  120. #endif
  121.     /* We don't know the size of the block, so free and re-allocate */
  122.     TclpSysFree(oldPtr);
  123.     return TclpSysAlloc(size, 1);
  124. }
  125.