VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/nsprpub/pr/include/prmem.h@ 101786

Last change on this file since 101786 was 101786, checked in by vboxsync, 17 months ago

libs/xpcom: Make PR_Malloc and friends inline helpers and get rid of prmem.c, bugref:10545

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.3 KB
Line 
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/*
39** File: prmem.h
40** Description: API to NSPR 2.0 memory management functions
41**
42*/
43#ifndef prmem_h___
44#define prmem_h___
45
46#include "prtypes.h"
47#include <stddef.h>
48#include <stdlib.h>
49#include <iprt/mem.h>
50
51#ifdef VBOX_WITH_XPCOM_NAMESPACE_CLEANUP
52#define PR_Malloc VBoxNsprPR_Malloc
53#define PR_Calloc VBoxNsprPR_Calloc
54#define PR_Realloc VBoxNsprPR_Realloc
55#define PR_Free VBoxNsprPR_Free
56#endif /* VBOX_WITH_XPCOM_NAMESPACE_CLEANUP */
57
58PR_BEGIN_EXTERN_C
59
60/*
61** Thread safe memory allocation.
62**
63** NOTE: pr wraps up malloc, free, calloc, realloc so they are already
64** thread safe (and are not declared here - look in stdlib.h).
65*/
66
67#ifdef VBOX
68DECL_FORCE_INLINE(void *) PR_Malloc(PRUint32 size)
69{
70 return RTMemAlloc(RT_MAX(size, 1));
71}
72
73DECL_FORCE_INLINE(void *) PR_Calloc(PRUint32 nelem, PRUint32 elsize)
74{
75 return RTMemAllocZ(RT_MAX(nelem * (size_t)elsize, 1));
76}
77
78DECL_FORCE_INLINE(void *) PR_Realloc(void *ptr, PRUint32 size)
79{
80 return RTMemRealloc(ptr, size);
81}
82
83DECL_FORCE_INLINE(void) PR_Free(void *ptr)
84{
85 RTMemFree(ptr);
86}
87#else
88# error "Not supported"
89#endif
90
91/*
92** The following are some convenience macros defined in terms of
93** PR_Malloc, PR_Calloc, PR_Realloc, and PR_Free.
94*/
95
96/***********************************************************************
97** FUNCTION: PR_MALLOC()
98** DESCRIPTION:
99** PR_NEW() allocates an untyped item of size _size from the heap.
100** INPUTS: _size: size in bytes of item to be allocated
101** OUTPUTS: untyped pointer to the node allocated
102** RETURN: pointer to node or error returned from malloc().
103***********************************************************************/
104#define PR_MALLOC(_bytes) (PR_Malloc((_bytes)))
105
106/***********************************************************************
107** FUNCTION: PR_NEW()
108** DESCRIPTION:
109** PR_NEW() allocates an item of type _struct from the heap.
110** INPUTS: _struct: a data type
111** OUTPUTS: pointer to _struct
112** RETURN: pointer to _struct or error returns from malloc().
113***********************************************************************/
114#define PR_NEW(_struct) ((_struct *) PR_MALLOC(sizeof(_struct)))
115
116/***********************************************************************
117** FUNCTION: PR_REALLOC()
118** DESCRIPTION:
119** PR_REALLOC() re-allocates _ptr bytes from the heap as a _size
120** untyped item.
121** INPUTS: _ptr: pointer to node to reallocate
122** _size: size of node to allocate
123** OUTPUTS: pointer to node allocated
124** RETURN: pointer to node allocated
125***********************************************************************/
126#define PR_REALLOC(_ptr, _size) (PR_Realloc((_ptr), (_size)))
127
128/***********************************************************************
129** FUNCTION: PR_CALLOC()
130** DESCRIPTION:
131** PR_CALLOC() allocates a _size bytes untyped item from the heap
132** and sets the allocated memory to all 0x00.
133** INPUTS: _size: size of node to allocate
134** OUTPUTS: pointer to node allocated
135** RETURN: pointer to node allocated
136***********************************************************************/
137#define PR_CALLOC(_size) (PR_Calloc(1, (_size)))
138
139/***********************************************************************
140** FUNCTION: PR_NEWZAP()
141** DESCRIPTION:
142** PR_NEWZAP() allocates an item of type _struct from the heap
143** and sets the allocated memory to all 0x00.
144** INPUTS: _struct: a data type
145** OUTPUTS: pointer to _struct
146** RETURN: pointer to _struct
147***********************************************************************/
148#define PR_NEWZAP(_struct) ((_struct*)PR_Calloc(1, sizeof(_struct)))
149
150/***********************************************************************
151** FUNCTION: PR_DELETE()
152** DESCRIPTION:
153** PR_DELETE() unallocates an object previosly allocated via PR_NEW()
154** or PR_NEWZAP() to the heap.
155** INPUTS: pointer to previously allocated object
156** OUTPUTS: the referenced object is returned to the heap
157** RETURN: void
158***********************************************************************/
159#define PR_DELETE(_ptr) { PR_Free(_ptr); (_ptr) = NULL; }
160
161/***********************************************************************
162** FUNCTION: PR_FREEIF()
163** DESCRIPTION:
164** PR_FREEIF() conditionally unallocates an object previously allocated
165** vial PR_NEW() or PR_NEWZAP(). If the pointer to the object is
166** equal to zero (0), the object is not released.
167** INPUTS: pointer to previously allocated object
168** OUTPUTS: the referenced object is conditionally returned to the heap
169** RETURN: void
170***********************************************************************/
171#define PR_FREEIF(_ptr) if (_ptr) PR_DELETE(_ptr)
172
173PR_END_EXTERN_C
174
175#endif /* prmem_h___ */
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette