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