home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / modules / coreincl / coremem.h next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  5.4 KB  |  169 lines

  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. /*-----------------------------------------------------------------------------
  20.     coremem.h - used to be xp_mem.h
  21.     
  22.     Cross-Platform Memory API
  23. -----------------------------------------------------------------------------*/
  24.  
  25.  
  26. #ifndef _coremem_h_
  27. #define _coremem_h_
  28.  
  29. #include "platform.h"
  30.  
  31. #ifdef XP_WIN16
  32. #include <malloc.h>
  33. #endif
  34.  
  35. /* global free routine */
  36. #define XP_FREEIF(obj) do { if(obj) XP_FREE(obj); } while(0)
  37.  
  38. /*-----------------------------------------------------------------------------
  39. Allocating Structures
  40. -----------------------------------------------------------------------------*/
  41.  
  42. #define XP_NEW( x )           (x*)malloc( sizeof( x ) )
  43. #define XP_DELETE( p )      free( p )
  44.  
  45.  
  46.  
  47. #ifdef XP_WIN16
  48.  
  49. XP_BEGIN_PROTOS
  50. extern void * WIN16_realloc(void * ptr, unsigned long size);
  51. extern void * WIN16_malloc(unsigned long size);
  52. XP_END_PROTOS
  53.  
  54. #define XP_REALLOC(ptr, size)   WIN16_realloc(ptr, size)
  55. #define XP_ALLOC(size)          WIN16_malloc(size)
  56. #else
  57.  
  58. #if defined(DEBUG) && defined(MOZILLA_CLIENT)
  59. /* Check that we never allocate anything greater than 64K.  If we ever tried,
  60.    Win16 would choke, and we'd like to find out about it on some other platform
  61.    (like, one where we have a working debugger). */
  62. /* This code used to call abort. Unfortunately, on Windows, abort() doesn't
  63.  * go to the debugger. Instead, it silently quits the program.
  64.  * So use XP_ASSERT(FALSE) instead.
  65.  */
  66.  
  67. #define XP_CHECK_ALLOC_SIZE(size)    ((size) <= 0xFFFF ? size : (XP_ASSERT(FALSE), (size)))
  68. #else
  69. #define XP_CHECK_ALLOC_SIZE(size)    size
  70. #endif
  71.  
  72. #define XP_REALLOC(ptr, size)       realloc(ptr, XP_CHECK_ALLOC_SIZE(size))
  73. #define XP_ALLOC(size)              malloc(XP_CHECK_ALLOC_SIZE(size))
  74. #endif
  75.  
  76.  
  77. #ifdef DEBUG
  78. #define XP_CALLOC(num, sz)    (((num)*(sz))<64000 ? calloc((num),(sz)) : (XP_ASSERT(FALSE), calloc((num),(sz))))
  79. #else
  80. #define XP_CALLOC(num, sz)      calloc((num), (sz))
  81. #endif
  82.  
  83. #define XP_FREE(ptr)            free(ptr)
  84. #define XP_NEW_ZAP(TYPE)        ( (TYPE*) calloc (1, sizeof (TYPE) ) )
  85.  
  86.  
  87. /* --------------------------------------------------------------------------
  88.   16-bit windows requires space allocated bigger than 32K to be of
  89.   type huge.  For example:
  90.  
  91.   int HUGE * foo = halloc(100000);
  92. -----------------------------------------------------------------------------*/
  93.  
  94. /* There's no huge realloc because win16 doesn't have a hrealloc,
  95.  * and there's no API to discover the original buffer's size.
  96.  */
  97. #ifdef XP_WIN16
  98. #define XP_HUGE __huge
  99. #define XP_HUGE_ALLOC(SIZE) halloc(SIZE,1)
  100. #define XP_HUGE_FREE(SIZE) hfree(SIZE)
  101. #define XP_HUGE_MEMCPY(DEST, SOURCE, LEN) hmemcpy(DEST, SOURCE, LEN)
  102. #else
  103. #define XP_HUGE
  104. #define XP_HUGE_ALLOC(SIZE) malloc(SIZE)
  105. #define XP_HUGE_FREE(SIZE) free(SIZE)
  106. #define XP_HUGE_MEMCPY(DEST, SOURCE, LEN) memcpy(DEST, SOURCE, LEN)
  107. #endif
  108.  
  109. #define XP_HUGE_CHAR_PTR char XP_HUGE *
  110.  
  111. /*-----------------------------------------------------------------------------
  112. Allocating Large Buffers
  113. NOTE: this does not interchange with XP_ALLOC/XP_NEW/XP_FREE/XP_DELETE
  114. -----------------------------------------------------------------------------*/
  115.  
  116. #if defined(XP_UNIX) || defined(XP_WIN32) || defined(XP_MAC)
  117.  
  118. /* don't typedef this to void* unless you want obscure bugs... */
  119. typedef unsigned long *    XP_Block;
  120.  
  121. #define XP_ALLOC_BLOCK(SIZE)            malloc ((SIZE))
  122. #define XP_FREE_BLOCK(BLOCK)            free ((BLOCK))
  123. #ifdef XP_UNIXu
  124.   /* On SunOS, realloc(0,n) ==> 0 */
  125. # define XP_REALLOC_BLOCK(BLOCK,SIZE)    ((BLOCK) \
  126.                       ? realloc ((BLOCK), (SIZE)) \
  127.                       : malloc ((SIZE)))
  128. #else /* !XP_UNIX */
  129. # define XP_REALLOC_BLOCK(BLOCK,SIZE)    realloc ((BLOCK), (SIZE))
  130. #endif /* !XP_UNIX */
  131. #define XP_LOCK_BLOCK(PTR,TYPE,BLOCK)   PTR = ((TYPE) (BLOCK))
  132. #ifdef DEBUG
  133. #define XP_UNLOCK_BLOCK(BLOCK)          (void)BLOCK
  134. #else
  135. #define XP_UNLOCK_BLOCK(BLOCK)
  136. #endif
  137. #endif /* XP_UNIX || XP_WIN32 */
  138.  
  139. #ifdef XP_WIN16
  140.  
  141. typedef unsigned char * XP_Block;
  142. #define XP_ALLOC_BLOCK(SIZE)            WIN16_malloc((SIZE))
  143. #define XP_FREE_BLOCK(BLOCK)            free ((BLOCK))
  144. #define XP_REALLOC_BLOCK(BLOCK,SIZE)    ((BLOCK) \
  145.                       ? WIN16_realloc ((BLOCK), (SIZE)) \
  146.                       : WIN16_malloc ((SIZE)))
  147. #define XP_LOCK_BLOCK(PTR,TYPE,BLOCK)   PTR = ((TYPE) (BLOCK))
  148. #ifdef DEBUG
  149. #define XP_UNLOCK_BLOCK(BLOCK)          (void)BLOCK
  150. #else
  151. #define XP_UNLOCK_BLOCK(BLOCK)
  152. #endif
  153.  
  154.  
  155. #endif /* XP_WIN16 */
  156.  
  157.  
  158.  
  159. #define    PA_Block                    XP_Block
  160. #define    PA_ALLOC(S)                XP_ALLOC_BLOCK(S)
  161. #define     PA_FREE(B)                XP_FREE_BLOCK(B)
  162. #define     PA_REALLOC(B,S)            XP_REALLOC_BLOCK(B,S)
  163. #define     PA_LOCK(P,T,B)                XP_LOCK_BLOCK(P,T,B)
  164. #define     PA_UNLOCK(B)                XP_UNLOCK_BLOCK(B)
  165.  
  166. #endif /* _coremem_h_ */
  167.  
  168.  
  169.