home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / include / xp_mem.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  9.1 KB  |  294 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.     XPMem.h
  21.     Cross-Platform Memory API
  22. -----------------------------------------------------------------------------*/
  23. #ifndef _XP_MEM_
  24. #define _XP_MEM_
  25.  
  26. #include "xp_core.h"
  27.  
  28. #ifdef XP_MAC
  29. #include "FlushAllocator.h"
  30. #endif
  31.  
  32. #ifdef XP_WIN16
  33. #include <malloc.h>
  34. #endif
  35.  
  36. /* global free routine */
  37. #define XP_FREEIF(obj) do { if(obj) { XP_FREE(obj); obj = 0; }} while(0)
  38.  
  39. /*-----------------------------------------------------------------------------
  40. Allocating Structures
  41. -----------------------------------------------------------------------------*/
  42.  
  43. #ifndef XP_MAC
  44.  
  45. #define XP_NEW( x )           (x*)malloc( sizeof( x ) )
  46. #define XP_DELETE( p )      free( p )
  47.  
  48. #else /* XP_MAC */
  49.  
  50. #define XP_NEW( s )             ((s*)Flush_Allocate( sizeof(s), FALSE ) )
  51. #define XP_DELETE( p )            Flush_Free( p )
  52.  
  53. #endif /* XP_MAC */
  54.  
  55. /*-----------------------------------------------------------------------------
  56. Mallocs
  57. NOTE: this uses the same malloc as the structure allocator so it is
  58. ok and safe to use XP_DELETE or XP_FREE interchangeably!
  59. -----------------------------------------------------------------------------*/
  60.  
  61. #ifdef XP_MAC
  62.  
  63. #define XP_ALLOC( s )            Flush_Allocate( s, FALSE )
  64. #define XP_FREE( p )            Flush_Free( p )
  65. #define XP_REALLOC( p , s )        Flush_Reallocate( p, s )
  66. #define XP_CALLOC( n, s )        Flush_Allocate( (n)*(s), TRUE )
  67. #define XP_NEW_ZAP( t )            ((t*)Flush_Allocate( sizeof(t), TRUE ) )
  68.  
  69. #else /* !XP_MAC */
  70. /* normal win and unix */
  71.  
  72. #ifdef XP_WIN16
  73.  
  74. XP_BEGIN_PROTOS
  75. extern void * WIN16_realloc(void * ptr, unsigned long size);
  76. extern void * WIN16_malloc(unsigned long size);
  77. XP_END_PROTOS
  78.  
  79. #define XP_REALLOC(ptr, size)   WIN16_realloc(ptr, size)
  80. #define XP_ALLOC(size)          WIN16_malloc(size)
  81. #else
  82.  
  83. #if defined(DEBUG) && defined(MOZILLA_CLIENT)
  84. /* Check that we never allocate anything greater than 64K.  If we ever tried,
  85.    Win16 would choke, and we'd like to find out about it on some other platform
  86.    (like, one where we have a working debugger). */
  87. /* This code used to call abort. Unfortunately, on Windows, abort() doesn't
  88.  * go to the debugger. Instead, it silently quits the program.
  89.  * So use XP_ASSERT(FALSE) instead.
  90.  */
  91.  
  92. #define XP_CHECK_ALLOC_SIZE(size)    ((size) <= 0xFFFF ? size : (XP_ASSERT(FALSE), (size)))
  93. #else
  94. #define XP_CHECK_ALLOC_SIZE(size)    size
  95. #endif
  96.  
  97. #define XP_REALLOC(ptr, size)   realloc(ptr, XP_CHECK_ALLOC_SIZE(size))
  98. #define XP_ALLOC(size)          malloc(XP_CHECK_ALLOC_SIZE(size))
  99. #endif
  100.  
  101.  
  102. #ifdef DEBUG
  103. #define XP_CALLOC(num, sz)    (((num)*(sz))<64000 ? calloc((num),(sz)) : (XP_ASSERT(FALSE), calloc((num),(sz))))
  104. #else
  105. #define XP_CALLOC(num, sz)      calloc((num), (sz))
  106. #endif
  107.  
  108. #define XP_FREE(ptr)            free(ptr)
  109. #define XP_NEW_ZAP(TYPE)        ( (TYPE*) calloc (1, sizeof (TYPE) ) )
  110. #endif /* !XP_MAC */
  111.  
  112.  
  113. /* --------------------------------------------------------------------------
  114.   16-bit windows requires space allocated bigger than 32K to be of
  115.   type huge.  For example:
  116.  
  117.   int HUGE * foo = halloc(100000);
  118. -----------------------------------------------------------------------------*/
  119.  
  120. /* There's no huge realloc because win16 doesn't have a hrealloc,
  121.  * and there's no API to discover the original buffer's size.
  122.  */
  123. #ifdef XP_WIN16
  124. #define XP_HUGE __huge
  125. #define XP_HUGE_ALLOC(SIZE) halloc(SIZE,1)
  126. #define XP_HUGE_FREE(SIZE) hfree(SIZE)
  127. #define XP_HUGE_MEMCPY(DEST, SOURCE, LEN) hmemcpy(DEST, SOURCE, LEN)
  128. #else
  129. #define XP_HUGE
  130. #define XP_HUGE_ALLOC(SIZE) malloc(SIZE)
  131. #define XP_HUGE_FREE(SIZE) free(SIZE)
  132. #define XP_HUGE_MEMCPY(DEST, SOURCE, LEN) memcpy(DEST, SOURCE, LEN)
  133. #endif
  134.  
  135. #define XP_HUGE_CHAR_PTR char XP_HUGE *
  136.  
  137. /*-----------------------------------------------------------------------------
  138. Allocating Large Buffers
  139. NOTE: this does not interchange with XP_ALLOC/XP_NEW/XP_FREE/XP_DELETE
  140. -----------------------------------------------------------------------------*/
  141.  
  142. #if defined(XP_UNIX) || defined(XP_WIN32) 
  143.  
  144. /* don't typedef this to void* unless you want obscure bugs... */
  145. typedef unsigned long *    XP_Block;
  146.  
  147. #define XP_ALLOC_BLOCK(SIZE)            malloc ((SIZE))
  148. #define XP_FREE_BLOCK(BLOCK)            free ((BLOCK))
  149. #ifdef XP_UNIXu
  150.   /* On SunOS, realloc(0,n) ==> 0 */
  151. # define XP_REALLOC_BLOCK(BLOCK,SIZE)    ((BLOCK) \
  152.                       ? realloc ((BLOCK), (SIZE)) \
  153.                       : malloc ((SIZE)))
  154. #else /* !XP_UNIX */
  155. # define XP_REALLOC_BLOCK(BLOCK,SIZE)    realloc ((BLOCK), (SIZE))
  156. #endif /* !XP_UNIX */
  157. #define XP_LOCK_BLOCK(PTR,TYPE,BLOCK)   PTR = ((TYPE) (BLOCK))
  158. #ifdef DEBUG
  159. #define XP_UNLOCK_BLOCK(BLOCK)          (void)BLOCK
  160. #else
  161. #define XP_UNLOCK_BLOCK(BLOCK)
  162. #endif
  163. #endif /* XP_UNIX || XP_WIN32 */
  164.  
  165. #if defined(XP_OS2)
  166.  
  167. /* don't typedef this to void* unless you want obscure bugs... */
  168. typedef unsigned long * XP_Block;
  169.  
  170. #define XP_ALLOC_BLOCK(SIZE)            malloc ((SIZE))
  171. #define XP_FREE_BLOCK(BLOCK)            free ((BLOCK))
  172. # define XP_REALLOC_BLOCK(BLOCK,SIZE)    realloc ((BLOCK), (SIZE))
  173. #define XP_LOCK_BLOCK(PTR,TYPE,BLOCK)   PTR = ((TYPE) (BLOCK))
  174. #ifdef DEBUG
  175. #define XP_UNLOCK_BLOCK(BLOCK)          (void)BLOCK
  176. #else
  177. #define XP_UNLOCK_BLOCK(BLOCK)
  178. #endif
  179.  
  180. #ifdef MCW_DEBUG
  181. #include <stdlib.h>
  182. #include <shmalloc.h>
  183. #endif
  184. #endif /* XP_OS2 */
  185.  
  186. #ifdef XP_WIN16
  187. typedef unsigned char * XP_Block;
  188. #define XP_ALLOC_BLOCK(SIZE)            WIN16_malloc((SIZE))
  189. #define XP_FREE_BLOCK(BLOCK)            free ((BLOCK))
  190. #define XP_REALLOC_BLOCK(BLOCK,SIZE)    ((BLOCK) \
  191.                       ? WIN16_realloc ((BLOCK), (SIZE)) \
  192.                       : WIN16_malloc ((SIZE)))
  193. #define XP_LOCK_BLOCK(PTR,TYPE,BLOCK)   PTR = ((TYPE) (BLOCK))
  194. #ifdef DEBUG
  195. #define XP_UNLOCK_BLOCK(BLOCK)          (void)BLOCK
  196. #else
  197. #define XP_UNLOCK_BLOCK(BLOCK)
  198. #endif
  199.  
  200. #endif /* XP_WIN16 */
  201.  
  202.  
  203. #ifdef XP_MAC
  204.  
  205. typedef float*                        XP_Block;
  206. #define XP_ALLOC_BLOCK( s )            ((XP_Block)Flush_Allocate( s, FALSE ) )
  207. #define XP_FREE_BLOCK( b )            Flush_Free( b )
  208. #define    XP_REALLOC_BLOCK( b, s )    ((XP_Block)Flush_Reallocate( b, s ) )
  209. #define XP_LOCK_BLOCK( p, t, b )    (p = ( t )( b ))
  210. #define    XP_UNLOCK_BLOCK( b )
  211.  
  212. #endif /* XP_MAC */
  213.  
  214. #define    PA_Block                    XP_Block
  215. #define    PA_ALLOC(S)                    XP_ALLOC_BLOCK(S)
  216. #define PA_FREE(B)                    XP_FREE_BLOCK(B)
  217. #define PA_REALLOC(B,S)                XP_REALLOC_BLOCK(B,S)
  218. #define PA_LOCK(P,T,B)                XP_LOCK_BLOCK(P,T,B)
  219. #define PA_UNLOCK(B)                XP_UNLOCK_BLOCK(B)
  220.  
  221. /*-----------------------------------------------------------------------------
  222. Allocating many small structures.
  223.  
  224. If allocating many small structures, it is often more efficient to allocate
  225. an array of a bunch of them, and maintain a free list of them.  These
  226. utilities do that for you.
  227.  
  228. You must provide a XP_AllocStructInfo structure which describes what
  229. it is you are trying to allocate it.  If statically defined, use the
  230. XP_INITIALIZE_ALLOCSTRUCTINFO macro to initialize it; if you prefer to
  231. initialize it at runtime, use the XP_InitAllocStructInfo() routine.
  232.  
  233. If you free everything you've ever allocated for a given
  234. XP_AllocStructInfo, all the memory used will be freed.  Or, if you're
  235. *really sure* you're done with everything you've allocated for a given 
  236. XP_AllocStructInfo, you can just call the scary XP_FreeAllStructs() routine.
  237.  
  238. Don't mix calls to XP_AllocStruct/XP_FreeStruct and XP_ALLOC/XP_FREE !!!
  239.  
  240. XP_AllocStructZero is the same as XP_AllocStruct, but it also zeros out
  241. the allocated memory.
  242.  
  243. An example:
  244.  
  245. struct foo {
  246.   int a;
  247.   int b;
  248. };
  249.  
  250. static XP_AllocStructInfo FooAlloc =
  251.   { XP_INITIALIZE_ALLOCSTRUCTINFO(sizeof(struct foo)) };
  252.  
  253.   .
  254.   .
  255.   .
  256.  
  257.  
  258.   struct foo* ptr = (struct foo*) XP_AllocStruct(&FooAlloc);
  259.       .
  260.       .
  261.       .
  262.   XP_FreeStruct(FooAlloc, ptr);
  263. -----------------------------------------------------------------------------*/
  264.  
  265. typedef struct XP_AllocStructInfo {
  266.   int size;
  267.   void* curchunk;
  268.   int leftinchunk;
  269.   void* firstfree;
  270.   void* firstchunk;
  271.   int numalloced;
  272. } XP_AllocStructInfo;
  273.  
  274.  
  275. #define XP_INITIALIZE_ALLOCSTRUCTINFO(size) ((size + sizeof(void*) - 1) / sizeof(void*)) * sizeof(void*)
  276.  
  277.  
  278. XP_BEGIN_PROTOS
  279.  
  280. void XP_InitAllocStructInfo(XP_AllocStructInfo* info, int size);
  281. void* XP_AllocStruct(XP_AllocStructInfo* info);
  282. void* XP_AllocStructZero(XP_AllocStructInfo* info);
  283. void XP_FreeStruct(XP_AllocStructInfo* info, void* ptr);
  284. void XP_FreeAllStructs(XP_AllocStructInfo* info); /* Danger!  Use with care! */
  285.  
  286. XP_END_PROTOS
  287.  
  288.  
  289.  
  290.  
  291. #endif /* _XP_MEM_ */
  292.  
  293.  
  294.