home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / nspr30-v.zip / nspr30-v / include / prmem.h < prev    next >
C/C++ Source or Header  |  1998-11-02  |  5KB  |  141 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. ** File: prmem.h
  21. ** Description: API to NSPR 2.0 memory management functions
  22. **
  23. */
  24. #ifndef prmem_h___
  25. #define prmem_h___
  26.  
  27. #include "prtypes.h"
  28. #include <stddef.h>
  29. #include <stdlib.h>
  30.  
  31. PR_BEGIN_EXTERN_C
  32.  
  33. /*
  34. ** Thread safe memory allocation.
  35. **
  36. ** NOTE: pr wraps up malloc, free, calloc, realloc so they are already
  37. ** thread safe (and are not declared here - look in stdlib.h).
  38. */
  39.  
  40. /*
  41. ** PR_Malloc, PR_Calloc, PR_Realloc, and PR_Free have the same signatures
  42. ** as their libc equivalent malloc, calloc, realloc, and free, and have
  43. ** the same semantics.  (Note that the argument type size_t is replaced
  44. ** by PRUint32.)  Memory allocated by PR_Malloc, PR_Calloc, or PR_Realloc
  45. ** must be freed by PR_Free.
  46. */
  47.  
  48. PR_EXTERN(void *) PR_Malloc(PRUint32 size);
  49.  
  50. PR_EXTERN(void *) PR_Calloc(PRUint32 nelem, PRUint32 elsize);
  51.  
  52. PR_EXTERN(void *) PR_Realloc(void *ptr, PRUint32 size);
  53.  
  54. PR_EXTERN(void) PR_Free(void *ptr);
  55.  
  56. /*
  57. ** The following are some convenience macros defined in terms of
  58. ** PR_Malloc, PR_Calloc, PR_Realloc, and PR_Free.
  59. */
  60.  
  61. /***********************************************************************
  62. ** FUNCTION:    PR_MALLOC()
  63. ** DESCRIPTION:
  64. **   PR_NEW() allocates an untyped item of size _size from the heap.
  65. ** INPUTS:  _size: size in bytes of item to be allocated
  66. ** OUTPUTS:    untyped pointer to the node allocated
  67. ** RETURN:    pointer to node or error returned from malloc().
  68. ***********************************************************************/
  69. #define PR_MALLOC(_bytes) (PR_Malloc((_bytes)))
  70.  
  71. /***********************************************************************
  72. ** FUNCTION:    PR_NEW()
  73. ** DESCRIPTION:
  74. **   PR_NEW() allocates an item of type _struct from the heap.
  75. ** INPUTS:  _struct: a data type
  76. ** OUTPUTS:    pointer to _struct
  77. ** RETURN:    pointer to _struct or error returns from malloc().
  78. ***********************************************************************/
  79. #define PR_NEW(_struct) ((_struct *) PR_MALLOC(sizeof(_struct)))
  80.  
  81. /***********************************************************************
  82. ** FUNCTION:    PR_REALLOC()
  83. ** DESCRIPTION:
  84. **   PR_REALLOC() re-allocates _ptr bytes from the heap as a _size
  85. **   untyped item.
  86. ** INPUTS:    _ptr: pointer to node to reallocate
  87. **          _size: size of node to allocate
  88. ** OUTPUTS:    pointer to node allocated
  89. ** RETURN:    pointer to node allocated
  90. ***********************************************************************/
  91. #define PR_REALLOC(_ptr, _size) (PR_Realloc((_ptr), (_size)))
  92.  
  93. /***********************************************************************
  94. ** FUNCTION:    PR_CALLOC()
  95. ** DESCRIPTION:
  96. **   PR_CALLOC() allocates a _size bytes untyped item from the heap
  97. **   and sets the allocated memory to all 0x00.
  98. ** INPUTS:    _size: size of node to allocate
  99. ** OUTPUTS:    pointer to node allocated
  100. ** RETURN:    pointer to node allocated
  101. ***********************************************************************/
  102. #define PR_CALLOC(_size) (PR_Calloc(1, (_size)))
  103.  
  104. /***********************************************************************
  105. ** FUNCTION:    PR_NEWZAP()
  106. ** DESCRIPTION:
  107. **   PR_NEWZAP() allocates an item of type _struct from the heap
  108. **   and sets the allocated memory to all 0x00.
  109. ** INPUTS:    _struct: a data type
  110. ** OUTPUTS:    pointer to _struct
  111. ** RETURN:    pointer to _struct
  112. ***********************************************************************/
  113. #define PR_NEWZAP(_struct) ((_struct*)PR_Calloc(1, sizeof(_struct)))
  114.  
  115. /***********************************************************************
  116. ** FUNCTION:    PR_DELETE()
  117. ** DESCRIPTION:
  118. **   PR_DELETE() unallocates an object previosly allocated via PR_NEW()
  119. **   or PR_NEWZAP() to the heap.
  120. ** INPUTS:    pointer to previously allocated object
  121. ** OUTPUTS:    the referenced object is returned to the heap
  122. ** RETURN:    void
  123. ***********************************************************************/
  124. #define PR_DELETE(_ptr) { PR_Free(_ptr); (_ptr) = NULL; }
  125.  
  126. /***********************************************************************
  127. ** FUNCTION:    PR_FREEIF()
  128. ** DESCRIPTION:
  129. **   PR_FREEIF() conditionally unallocates an object previously allocated
  130. **   vial PR_NEW() or PR_NEWZAP(). If the pointer to the object is
  131. **   equal to zero (0), the object is not released.
  132. ** INPUTS:    pointer to previously allocated object
  133. ** OUTPUTS:    the referenced object is conditionally returned to the heap
  134. ** RETURN:    void
  135. ***********************************************************************/
  136. #define PR_FREEIF(_ptr)    if (_ptr) PR_DELETE(_ptr)
  137.  
  138. PR_END_EXTERN_C
  139.  
  140. #endif /* prmem_h___ */
  141.