home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / nsprpub / pr / include / prmem.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  9.8 KB  |  244 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. /***********************************************************************
  139. ** Typedef ENUM: PRSegmentAccess
  140. ** DESCRIPTION:
  141. **   Defines a number of segment accessor types for PR_Seg* functions
  142. **
  143. ***********************************************************************/
  144. typedef struct PRSegment PRSegment;
  145. typedef enum {
  146.     PR_SEGMENT_NONE,
  147.     PR_SEGMENT_RDONLY,
  148.     PR_SEGMENT_RDWR
  149. } PRSegmentAccess;
  150.  
  151. /***********************************************************************
  152. ** FUNCTION:    PR_NewSegment()
  153. ** DESCRIPTION:
  154. **   Allocate a memory segment. The "size" value is rounded up to the
  155. **   native system page size and a page aligned portion of memory is
  156. **   returned.  This memory is not part of the malloc heap. If "vaddr" is
  157. **   not NULL then PR tries to allocate the segment at the desired virtual
  158. **   address. Segments are mapped PR_SEGMENT_RDWR when created.
  159. ** INPUTS:    size:  size of the desired memory segment
  160. **          vaddr:  address at which the newly aquired segment is to be
  161. **                  mapped into memory.
  162. ** OUTPUTS:    a memory segment is allocated, a PRSegment is allocated
  163. ** RETURN:    pointer to PRSegment
  164. ***********************************************************************/
  165. PR_EXTERN(PRSegment*) PR_NewSegment(PRUint32 size, void *vaddr);
  166.  
  167. /***********************************************************************
  168. ** FUNCTION:    PR_DestroySegment()
  169. ** DESCRIPTION:
  170. **   The memory segment and the PRSegment are freed
  171. ** INPUTS:    seg:  pointer to PRSegment to be freed
  172. ** OUTPUTS:    the the PRSegment and its associated memory segment are freed
  173. ** RETURN:    void
  174. ***********************************************************************/
  175. PR_EXTERN(void) PR_DestroySegment(PRSegment *seg);
  176.  
  177. /***********************************************************************
  178. ** FUNCTION:    PR_GrowSegment()
  179. ** DESCRIPTION:
  180. **   Attempt to grow/shrink a memory segment. If deltaBytes is positive,
  181. **   the segment is grown. If deltaBytes is negative, the segment is
  182. **   shrunk. This returns the number of bytes added to the segment if
  183. **   successful, zero otherwise.
  184. ** INPUTS:    seg:  pointer to a PRSegment
  185. ** OUTPUTS:    
  186. ** RETURN:    PRUint32:   number of bytes added to the memory segment or zero
  187. ***********************************************************************/
  188. PR_EXTERN(PRUint32) PR_GrowSegment(PRSegment *seg, PRInt32 deltaBytes);
  189.  
  190. /***********************************************************************
  191. ** FUNCTION:    PR_GetSegmentVaddr()
  192. ** DESCRIPTION:
  193. **   PR_Segment member accessor function.
  194. **   Return the virtual address of the memory segment
  195. **
  196. ** INPUTS:    seg:  pointer to a PRSegment
  197. ** OUTPUTS:    none
  198. ** RETURN:    void*: Address where the memory segment is mapped.
  199. ***********************************************************************/
  200. PR_EXTERN(void*) PR_GetSegmentVaddr(PRSegment *seg);
  201.  
  202. /***********************************************************************
  203. ** FUNCTION:    PR_GetSegmentSize()
  204. ** DESCRIPTION:
  205. **   PR_Segment member accessor function.
  206. **   Return the size of the associated memory segment
  207. ** INPUTS:    seg:  pointer to a PRSegment
  208. ** OUTPUTS:    none
  209. ** RETURN:    size_t:  size of the associated memory segment
  210. ***********************************************************************/
  211. PR_EXTERN(size_t) PR_GetSegmentSize(PRSegment *seg);
  212.  
  213. /***********************************************************************
  214. ** FUNCTION:    PR_MapSegment()
  215. ** DESCRIPTION:
  216. **  Change the mapping on a segment.
  217. **       "how" == PR_SEGMENT_NONE: the segment becomes unmapped
  218. **       "how" == PR_SEGMENT_RDONLY: the segment becomes mapped and readable
  219. **       "how" == PR_SEGMENT_RDWR: the segment becomes mapped read/write
  220. **
  221. **   Note: If a segment can be read then it is also possible to execute 
  222. **     code in it.
  223. ** INPUTS:    seg:  pointer to a PRSegment
  224. **          how:  one of PRSegmentAccess enumerated values
  225. ** OUTPUTS:    the access for the associated memory segment is changed
  226. ** RETURN:    void
  227. ***********************************************************************/
  228. PR_EXTERN(void) PR_MapSegment(PRSegment *seg, PRSegmentAccess how);
  229.  
  230. /***********************************************************************
  231. ** FUNCTION:    PR_GetSegmentAccess()
  232. ** DESCRIPTION:
  233. **   PR_Segment member accessor function.
  234. **   Return a memory segment's current access rights
  235. ** INPUTS:    seg:  pointer to a PRSegment
  236. ** OUTPUTS:    
  237. ** RETURN:    PRSegmentAccess: current access rights
  238. ***********************************************************************/
  239. PR_EXTERN(PRSegmentAccess) PR_GetSegmentAccess(PRSegment *seg);
  240.  
  241. PR_END_EXTERN_C
  242.  
  243. #endif /* prmem_h___ */
  244.