PageRenderTime 65ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/gecko_api/include/prmem.h

http://firefox-mac-pdf.googlecode.com/
C++ Header | 158 lines | 18 code | 19 blank | 121 comment | 1 complexity | 09ffd9bb0a7ee8acc65adf49b41c3701 MD5 | raw file
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. * http://www.mozilla.org/MPL/
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. *
  15. * The Original Code is the Netscape Portable Runtime (NSPR).
  16. *
  17. * The Initial Developer of the Original Code is
  18. * Netscape Communications Corporation.
  19. * Portions created by the Initial Developer are Copyright (C) 1998-2000
  20. * the Initial Developer. All Rights Reserved.
  21. *
  22. * Contributor(s):
  23. *
  24. * Alternatively, the contents of this file may be used under the terms of
  25. * either the GNU General Public License Version 2 or later (the "GPL"), or
  26. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27. * in which case the provisions of the GPL or the LGPL are applicable instead
  28. * of those above. If you wish to allow use of your version of this file only
  29. * under the terms of either the GPL or the LGPL, and not to allow others to
  30. * use your version of this file under the terms of the MPL, indicate your
  31. * decision by deleting the provisions above and replace them with the notice
  32. * and other provisions required by the GPL or the LGPL. If you do not delete
  33. * the provisions above, a recipient may use your version of this file under
  34. * the terms of any one of the MPL, the GPL or the LGPL.
  35. *
  36. * ***** END LICENSE BLOCK ***** */
  37. /*
  38. ** File: prmem.h
  39. ** Description: API to NSPR memory management functions
  40. **
  41. */
  42. #ifndef prmem_h___
  43. #define prmem_h___
  44. #include "prtypes.h"
  45. #include <stdlib.h>
  46. PR_BEGIN_EXTERN_C
  47. /*
  48. ** Thread safe memory allocation.
  49. **
  50. ** NOTE: pr wraps up malloc, free, calloc, realloc so they are already
  51. ** thread safe (and are not declared here - look in stdlib.h).
  52. */
  53. /*
  54. ** PR_Malloc, PR_Calloc, PR_Realloc, and PR_Free have the same signatures
  55. ** as their libc equivalent malloc, calloc, realloc, and free, and have
  56. ** the same semantics. (Note that the argument type size_t is replaced
  57. ** by PRUint32.) Memory allocated by PR_Malloc, PR_Calloc, or PR_Realloc
  58. ** must be freed by PR_Free.
  59. */
  60. NSPR_API(void *) PR_Malloc(PRUint32 size);
  61. NSPR_API(void *) PR_Calloc(PRUint32 nelem, PRUint32 elsize);
  62. NSPR_API(void *) PR_Realloc(void *ptr, PRUint32 size);
  63. NSPR_API(void) PR_Free(void *ptr);
  64. /*
  65. ** The following are some convenience macros defined in terms of
  66. ** PR_Malloc, PR_Calloc, PR_Realloc, and PR_Free.
  67. */
  68. /***********************************************************************
  69. ** FUNCTION: PR_MALLOC()
  70. ** DESCRIPTION:
  71. ** PR_NEW() allocates an untyped item of size _size from the heap.
  72. ** INPUTS: _size: size in bytes of item to be allocated
  73. ** OUTPUTS: untyped pointer to the node allocated
  74. ** RETURN: pointer to node or error returned from malloc().
  75. ***********************************************************************/
  76. #define PR_MALLOC(_bytes) (PR_Malloc((_bytes)))
  77. /***********************************************************************
  78. ** FUNCTION: PR_NEW()
  79. ** DESCRIPTION:
  80. ** PR_NEW() allocates an item of type _struct from the heap.
  81. ** INPUTS: _struct: a data type
  82. ** OUTPUTS: pointer to _struct
  83. ** RETURN: pointer to _struct or error returns from malloc().
  84. ***********************************************************************/
  85. #define PR_NEW(_struct) ((_struct *) PR_MALLOC(sizeof(_struct)))
  86. /***********************************************************************
  87. ** FUNCTION: PR_REALLOC()
  88. ** DESCRIPTION:
  89. ** PR_REALLOC() re-allocates _ptr bytes from the heap as a _size
  90. ** untyped item.
  91. ** INPUTS: _ptr: pointer to node to reallocate
  92. ** _size: size of node to allocate
  93. ** OUTPUTS: pointer to node allocated
  94. ** RETURN: pointer to node allocated
  95. ***********************************************************************/
  96. #define PR_REALLOC(_ptr, _size) (PR_Realloc((_ptr), (_size)))
  97. /***********************************************************************
  98. ** FUNCTION: PR_CALLOC()
  99. ** DESCRIPTION:
  100. ** PR_CALLOC() allocates a _size bytes untyped item from the heap
  101. ** and sets the allocated memory to all 0x00.
  102. ** INPUTS: _size: size of node to allocate
  103. ** OUTPUTS: pointer to node allocated
  104. ** RETURN: pointer to node allocated
  105. ***********************************************************************/
  106. #define PR_CALLOC(_size) (PR_Calloc(1, (_size)))
  107. /***********************************************************************
  108. ** FUNCTION: PR_NEWZAP()
  109. ** DESCRIPTION:
  110. ** PR_NEWZAP() allocates an item of type _struct from the heap
  111. ** and sets the allocated memory to all 0x00.
  112. ** INPUTS: _struct: a data type
  113. ** OUTPUTS: pointer to _struct
  114. ** RETURN: pointer to _struct
  115. ***********************************************************************/
  116. #define PR_NEWZAP(_struct) ((_struct*)PR_Calloc(1, sizeof(_struct)))
  117. /***********************************************************************
  118. ** FUNCTION: PR_DELETE()
  119. ** DESCRIPTION:
  120. ** PR_DELETE() unallocates an object previosly allocated via PR_NEW()
  121. ** or PR_NEWZAP() to the heap.
  122. ** INPUTS: pointer to previously allocated object
  123. ** OUTPUTS: the referenced object is returned to the heap
  124. ** RETURN: void
  125. ***********************************************************************/
  126. #define PR_DELETE(_ptr) { PR_Free(_ptr); (_ptr) = NULL; }
  127. /***********************************************************************
  128. ** FUNCTION: PR_FREEIF()
  129. ** DESCRIPTION:
  130. ** PR_FREEIF() conditionally unallocates an object previously allocated
  131. ** vial PR_NEW() or PR_NEWZAP(). If the pointer to the object is
  132. ** equal to zero (0), the object is not released.
  133. ** INPUTS: pointer to previously allocated object
  134. ** OUTPUTS: the referenced object is conditionally returned to the heap
  135. ** RETURN: void
  136. ***********************************************************************/
  137. #define PR_FREEIF(_ptr) if (_ptr) PR_DELETE(_ptr)
  138. PR_END_EXTERN_C
  139. #endif /* prmem_h___ */