prmem.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /*
  6. ** File: prmem.h
  7. ** Description: API to NSPR memory management functions
  8. **
  9. */
  10. #ifndef prmem_h___
  11. #define prmem_h___
  12. #include "prtypes.h"
  13. #include <stdlib.h>
  14. PR_BEGIN_EXTERN_C
  15. /*
  16. ** Thread safe memory allocation.
  17. **
  18. ** NOTE: pr wraps up malloc, free, calloc, realloc so they are already
  19. ** thread safe (and are not declared here - look in stdlib.h).
  20. */
  21. /*
  22. ** PR_Malloc, PR_Calloc, PR_Realloc, and PR_Free have the same signatures
  23. ** as their libc equivalent malloc, calloc, realloc, and free, and have
  24. ** the same semantics. (Note that the argument type size_t is replaced
  25. ** by PRUint32.) Memory allocated by PR_Malloc, PR_Calloc, or PR_Realloc
  26. ** must be freed by PR_Free.
  27. */
  28. NSPR_API(void *) PR_Malloc(PRUint32 size);
  29. NSPR_API(void *) PR_Calloc(PRUint32 nelem, PRUint32 elsize);
  30. NSPR_API(void *) PR_Realloc(void *ptr, PRUint32 size);
  31. NSPR_API(void) PR_Free(void *ptr);
  32. /*
  33. ** The following are some convenience macros defined in terms of
  34. ** PR_Malloc, PR_Calloc, PR_Realloc, and PR_Free.
  35. */
  36. /***********************************************************************
  37. ** FUNCTION: PR_MALLOC()
  38. ** DESCRIPTION:
  39. ** PR_NEW() allocates an untyped item of size _size from the heap.
  40. ** INPUTS: _size: size in bytes of item to be allocated
  41. ** OUTPUTS: untyped pointer to the node allocated
  42. ** RETURN: pointer to node or error returned from malloc().
  43. ***********************************************************************/
  44. #define PR_MALLOC(_bytes) (PR_Malloc((_bytes)))
  45. /***********************************************************************
  46. ** FUNCTION: PR_NEW()
  47. ** DESCRIPTION:
  48. ** PR_NEW() allocates an item of type _struct from the heap.
  49. ** INPUTS: _struct: a data type
  50. ** OUTPUTS: pointer to _struct
  51. ** RETURN: pointer to _struct or error returns from malloc().
  52. ***********************************************************************/
  53. #define PR_NEW(_struct) ((_struct *) PR_MALLOC(sizeof(_struct)))
  54. /***********************************************************************
  55. ** FUNCTION: PR_REALLOC()
  56. ** DESCRIPTION:
  57. ** PR_REALLOC() re-allocates _ptr bytes from the heap as a _size
  58. ** untyped item.
  59. ** INPUTS: _ptr: pointer to node to reallocate
  60. ** _size: size of node to allocate
  61. ** OUTPUTS: pointer to node allocated
  62. ** RETURN: pointer to node allocated
  63. ***********************************************************************/
  64. #define PR_REALLOC(_ptr, _size) (PR_Realloc((_ptr), (_size)))
  65. /***********************************************************************
  66. ** FUNCTION: PR_CALLOC()
  67. ** DESCRIPTION:
  68. ** PR_CALLOC() allocates a _size bytes untyped item from the heap
  69. ** and sets the allocated memory to all 0x00.
  70. ** INPUTS: _size: size of node to allocate
  71. ** OUTPUTS: pointer to node allocated
  72. ** RETURN: pointer to node allocated
  73. ***********************************************************************/
  74. #define PR_CALLOC(_size) (PR_Calloc(1, (_size)))
  75. /***********************************************************************
  76. ** FUNCTION: PR_NEWZAP()
  77. ** DESCRIPTION:
  78. ** PR_NEWZAP() allocates an item of type _struct from the heap
  79. ** and sets the allocated memory to all 0x00.
  80. ** INPUTS: _struct: a data type
  81. ** OUTPUTS: pointer to _struct
  82. ** RETURN: pointer to _struct
  83. ***********************************************************************/
  84. #define PR_NEWZAP(_struct) ((_struct*)PR_Calloc(1, sizeof(_struct)))
  85. /***********************************************************************
  86. ** FUNCTION: PR_DELETE()
  87. ** DESCRIPTION:
  88. ** PR_DELETE() unallocates an object previosly allocated via PR_NEW()
  89. ** or PR_NEWZAP() to the heap.
  90. ** INPUTS: pointer to previously allocated object
  91. ** OUTPUTS: the referenced object is returned to the heap
  92. ** RETURN: void
  93. ***********************************************************************/
  94. #define PR_DELETE(_ptr) { PR_Free(_ptr); (_ptr) = NULL; }
  95. /***********************************************************************
  96. ** FUNCTION: PR_FREEIF()
  97. ** DESCRIPTION:
  98. ** PR_FREEIF() conditionally unallocates an object previously allocated
  99. ** vial PR_NEW() or PR_NEWZAP(). If the pointer to the object is
  100. ** equal to zero (0), the object is not released.
  101. ** INPUTS: pointer to previously allocated object
  102. ** OUTPUTS: the referenced object is conditionally returned to the heap
  103. ** RETURN: void
  104. ***********************************************************************/
  105. #define PR_FREEIF(_ptr) if (_ptr) PR_DELETE(_ptr)
  106. PR_END_EXTERN_C
  107. #endif /* prmem_h___ */