xref: /aoo42x/main/sal/inc/rtl/alloc.h (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #ifndef _RTL_ALLOC_H_
29*cdf0e10cSrcweir #define _RTL_ALLOC_H_
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #	include <sal/types.h>
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #ifdef __cplusplus
34*cdf0e10cSrcweir extern "C" {
35*cdf0e10cSrcweir #endif
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir /** Allocate memory.
39*cdf0e10cSrcweir     @descr A call to this function will return NULL upon the requested
40*cdf0e10cSrcweir 	memory size being either zero or larger than currently allocatable.
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir 	@param  Bytes [in] memory size.
43*cdf0e10cSrcweir 	@return pointer to allocated memory.
44*cdf0e10cSrcweir  */
45*cdf0e10cSrcweir void * SAL_CALL rtl_allocateMemory (
46*cdf0e10cSrcweir 	sal_Size Bytes
47*cdf0e10cSrcweir ) SAL_THROW_EXTERN_C();
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir /** Reallocate memory.
51*cdf0e10cSrcweir     @descr A call to this function with parameter 'Ptr' being NULL
52*cdf0e10cSrcweir 	is equivalent to a rtl_allocateMemory() call.
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir 	A call to this function with parameter 'Bytes' being 0
55*cdf0e10cSrcweir 	is equivalent to a rtl_freeMemory() call.
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir 	@see rtl_allocateMemory()
58*cdf0e10cSrcweir 	@see rtl_freeMemory()
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir 	@param  Ptr   [in] pointer to previously allocated memory.
61*cdf0e10cSrcweir 	@param  Bytes [in] new memory size.
62*cdf0e10cSrcweir 	@return pointer to reallocated memory. May differ from Ptr.
63*cdf0e10cSrcweir  */
64*cdf0e10cSrcweir void * SAL_CALL rtl_reallocateMemory (
65*cdf0e10cSrcweir 	void *   Ptr,
66*cdf0e10cSrcweir 	sal_Size Bytes
67*cdf0e10cSrcweir ) SAL_THROW_EXTERN_C();
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir /** Free memory.
71*cdf0e10cSrcweir 	@param  Ptr   [in] pointer to previously allocated memory.
72*cdf0e10cSrcweir 	@return none. Memory is released. Ptr is invalid.
73*cdf0e10cSrcweir  */
74*cdf0e10cSrcweir void SAL_CALL rtl_freeMemory (
75*cdf0e10cSrcweir 	void * Ptr
76*cdf0e10cSrcweir ) SAL_THROW_EXTERN_C();
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir /** Allocate and zero memory.
80*cdf0e10cSrcweir     @descr A call to this function will return NULL upon the requested
81*cdf0e10cSrcweir 	memory size being either zero or larger than currently allocatable.
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir 	@param  Bytes [in] memory size.
84*cdf0e10cSrcweir 	@return pointer to allocated and zero'ed memory.
85*cdf0e10cSrcweir  */
86*cdf0e10cSrcweir void * SAL_CALL rtl_allocateZeroMemory (
87*cdf0e10cSrcweir 	sal_Size Bytes
88*cdf0e10cSrcweir ) SAL_THROW_EXTERN_C();
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir /** Zero and free memory.
92*cdf0e10cSrcweir 	@param  Ptr   [in] pointer to previously allocated memory.
93*cdf0e10cSrcweir 	@param  Bytes [in] memory size.
94*cdf0e10cSrcweir 	@return none. Memory is zero'ed and released. Ptr is invalid.
95*cdf0e10cSrcweir  */
96*cdf0e10cSrcweir void SAL_CALL rtl_freeZeroMemory (
97*cdf0e10cSrcweir 	void *   Ptr,
98*cdf0e10cSrcweir 	sal_Size Bytes
99*cdf0e10cSrcweir ) SAL_THROW_EXTERN_C();
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir /** Opaque rtl_arena_type.
103*cdf0e10cSrcweir  */
104*cdf0e10cSrcweir typedef struct rtl_arena_st rtl_arena_type;
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir #define RTL_ARENA_NAME_LENGTH 31
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir /** rtl_arena_create()
110*cdf0e10cSrcweir  *
111*cdf0e10cSrcweir  *  @param  pName             [in] descriptive name; for debugging purposes.
112*cdf0e10cSrcweir  *  @param  quantum           [in] resource allocation unit / granularity; rounded up to next power of 2.
113*cdf0e10cSrcweir  *  @param  quantum_cache_max [in] max resources to cache; rounded up to next multiple of quantum; usually 0.
114*cdf0e10cSrcweir  *  @param  source_arena      [in] passed as argument to source_alloc, source_free; usually NULL.
115*cdf0e10cSrcweir  *  @param  source_alloc      [in] function to allocate resources; usually rtl_arena_alloc.
116*cdf0e10cSrcweir  *  @param  source_free       [in] function to free resources; usually rtl_arena_free.
117*cdf0e10cSrcweir  *  @param  nFlags            [in] flags; usually 0.
118*cdf0e10cSrcweir  *
119*cdf0e10cSrcweir  *  @return pointer to rtl_arena_type, or NULL upon failure.
120*cdf0e10cSrcweir  *
121*cdf0e10cSrcweir  *  @see rtl_arena_destroy()
122*cdf0e10cSrcweir  */
123*cdf0e10cSrcweir rtl_arena_type *
124*cdf0e10cSrcweir SAL_CALL rtl_arena_create (
125*cdf0e10cSrcweir     const char *       pName,
126*cdf0e10cSrcweir     sal_Size           quantum,
127*cdf0e10cSrcweir     sal_Size           quantum_cache_max,
128*cdf0e10cSrcweir     rtl_arena_type *   source_arena,
129*cdf0e10cSrcweir     void * (SAL_CALL * source_alloc)(rtl_arena_type *, sal_Size *),
130*cdf0e10cSrcweir     void   (SAL_CALL * source_free) (rtl_arena_type *, void *, sal_Size),
131*cdf0e10cSrcweir     int                nFlags
132*cdf0e10cSrcweir ) SAL_THROW_EXTERN_C();
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir /** rtl_arena_destroy()
136*cdf0e10cSrcweir  *
137*cdf0e10cSrcweir  *  @param  pArena [in] the arena to destroy.
138*cdf0e10cSrcweir  *  @return None
139*cdf0e10cSrcweir  *
140*cdf0e10cSrcweir  *  @see rtl_arena_create()
141*cdf0e10cSrcweir  */
142*cdf0e10cSrcweir void
143*cdf0e10cSrcweir SAL_CALL rtl_arena_destroy (
144*cdf0e10cSrcweir     rtl_arena_type * pArena
145*cdf0e10cSrcweir ) SAL_THROW_EXTERN_C();
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir /** rtl_arena_alloc()
149*cdf0e10cSrcweir  *
150*cdf0e10cSrcweir  *  @param  pArena [in]    arena from which resource is allocated.
151*cdf0e10cSrcweir  *  @param  pBytes [inout] size of resource to allocate.
152*cdf0e10cSrcweir  *
153*cdf0e10cSrcweir  *  @return allocated resource, or NULL upon failure.
154*cdf0e10cSrcweir  *
155*cdf0e10cSrcweir  *  @see rtl_arena_free()
156*cdf0e10cSrcweir  */
157*cdf0e10cSrcweir void *
158*cdf0e10cSrcweir SAL_CALL rtl_arena_alloc (
159*cdf0e10cSrcweir     rtl_arena_type * pArena,
160*cdf0e10cSrcweir     sal_Size *       pBytes
161*cdf0e10cSrcweir ) SAL_THROW_EXTERN_C();
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir /** rtl_arena_free()
165*cdf0e10cSrcweir  *
166*cdf0e10cSrcweir  *  @param  pArena [in] arena from which resource was allocated.
167*cdf0e10cSrcweir  *  @param  pAddr  [in] resource to free.
168*cdf0e10cSrcweir  *  @param  nBytes [in] size of resource.
169*cdf0e10cSrcweir  *
170*cdf0e10cSrcweir  *  @return None.
171*cdf0e10cSrcweir  *
172*cdf0e10cSrcweir  *  @see rtl_arena_alloc()
173*cdf0e10cSrcweir  */
174*cdf0e10cSrcweir void
175*cdf0e10cSrcweir SAL_CALL rtl_arena_free (
176*cdf0e10cSrcweir     rtl_arena_type * pArena,
177*cdf0e10cSrcweir     void *           pAddr,
178*cdf0e10cSrcweir     sal_Size         nBytes
179*cdf0e10cSrcweir ) SAL_THROW_EXTERN_C();
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir /** Opaque rtl_cache_type.
183*cdf0e10cSrcweir  */
184*cdf0e10cSrcweir typedef struct rtl_cache_st rtl_cache_type;
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir #define RTL_CACHE_NAME_LENGTH 31
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir #define RTL_CACHE_FLAG_BULKDESTROY 1
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir /** rtl_cache_create()
191*cdf0e10cSrcweir  *
192*cdf0e10cSrcweir  *  @param  pName       [in] descriptive name; for debugging purposes.
193*cdf0e10cSrcweir  *  @param  nObjSize    [in] object size.
194*cdf0e10cSrcweir  *  @param  nObjAlign   [in] object alignment; usually 0 for suitable default.
195*cdf0e10cSrcweir  *  @param  constructor [in] object constructor callback function; returning 1 for success or 0 for failure.
196*cdf0e10cSrcweir  *  @param  destructor  [in] object destructor callback function.
197*cdf0e10cSrcweir  *  @param  reclaim     [in] reclaim callback function.
198*cdf0e10cSrcweir  *  @param  pUserArg    [in] opaque argument passed to callback functions.
199*cdf0e10cSrcweir  *  @param  nFlags      [in] flags.
200*cdf0e10cSrcweir  *
201*cdf0e10cSrcweir  *  @return pointer to rtl_cache_type, or NULL upon failure.
202*cdf0e10cSrcweir  *
203*cdf0e10cSrcweir  *  @see rtl_cache_destroy()
204*cdf0e10cSrcweir  */
205*cdf0e10cSrcweir rtl_cache_type *
206*cdf0e10cSrcweir SAL_CALL rtl_cache_create (
207*cdf0e10cSrcweir     const char *     pName,
208*cdf0e10cSrcweir     sal_Size         nObjSize,
209*cdf0e10cSrcweir     sal_Size         nObjAlign,
210*cdf0e10cSrcweir     int  (SAL_CALL * constructor)(void * pObj, void * pUserArg),
211*cdf0e10cSrcweir     void (SAL_CALL * destructor) (void * pObj, void * pUserArg),
212*cdf0e10cSrcweir 	void (SAL_CALL * reclaim)    (void * pUserArg),
213*cdf0e10cSrcweir     void *           pUserArg,
214*cdf0e10cSrcweir     rtl_arena_type * pSource,
215*cdf0e10cSrcweir     int              nFlags
216*cdf0e10cSrcweir ) SAL_THROW_EXTERN_C();
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir 
219*cdf0e10cSrcweir /** rtl_cache_destroy()
220*cdf0e10cSrcweir  *
221*cdf0e10cSrcweir  *  @param  pCache [in] the cache to destroy.
222*cdf0e10cSrcweir  *
223*cdf0e10cSrcweir  *  @return None.
224*cdf0e10cSrcweir  *
225*cdf0e10cSrcweir  *  @see rtl_cache_create()
226*cdf0e10cSrcweir  */
227*cdf0e10cSrcweir void
228*cdf0e10cSrcweir SAL_CALL rtl_cache_destroy (
229*cdf0e10cSrcweir     rtl_cache_type * pCache
230*cdf0e10cSrcweir ) SAL_THROW_EXTERN_C();
231*cdf0e10cSrcweir 
232*cdf0e10cSrcweir 
233*cdf0e10cSrcweir /** rtl_cache_alloc()
234*cdf0e10cSrcweir  *
235*cdf0e10cSrcweir  *  @param  pCache [in] cache from which object is allocated.
236*cdf0e10cSrcweir  *
237*cdf0e10cSrcweir  *  @return pointer to allocated object, or NULL upon failure.
238*cdf0e10cSrcweir  */
239*cdf0e10cSrcweir void *
240*cdf0e10cSrcweir SAL_CALL rtl_cache_alloc (
241*cdf0e10cSrcweir     rtl_cache_type * pCache
242*cdf0e10cSrcweir ) SAL_THROW_EXTERN_C();
243*cdf0e10cSrcweir 
244*cdf0e10cSrcweir 
245*cdf0e10cSrcweir /** rtl_cache_free()
246*cdf0e10cSrcweir  *
247*cdf0e10cSrcweir  *  @param  pCache [in] cache from which object was allocated.
248*cdf0e10cSrcweir  *  @param  pObj   [in] object to free.
249*cdf0e10cSrcweir  *
250*cdf0e10cSrcweir  *  @return None.
251*cdf0e10cSrcweir  *
252*cdf0e10cSrcweir  *  @see rtl_cache_alloc()
253*cdf0e10cSrcweir  */
254*cdf0e10cSrcweir void
255*cdf0e10cSrcweir SAL_CALL rtl_cache_free (
256*cdf0e10cSrcweir     rtl_cache_type * pCache,
257*cdf0e10cSrcweir     void *           pObj
258*cdf0e10cSrcweir ) SAL_THROW_EXTERN_C();
259*cdf0e10cSrcweir 
260*cdf0e10cSrcweir 
261*cdf0e10cSrcweir #ifdef __cplusplus
262*cdf0e10cSrcweir }
263*cdf0e10cSrcweir #endif
264*cdf0e10cSrcweir 
265*cdf0e10cSrcweir #endif /*_RTL_ALLOC_H_ */
266*cdf0e10cSrcweir 
267