1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 * This file is part of LibreOffice published API.
24 #ifndef INCLUDED_RTL_ALLOC_H
25 #define INCLUDED_RTL_ALLOC_H
27 #include "sal/config.h"
29 #include "sal/saldllapi.h"
30 #include "sal/types.h"
39 A call to this function will return NULL upon the requested
40 memory size being either zero or larger than currently allocatable.
42 @param[in] Bytes memory size.
43 @return pointer to the allocated memory.
45 SAL_DLLPUBLIC
void * SAL_CALL
rtl_allocateMemory (
47 ) SAL_THROW_EXTERN_C();
50 /** Reallocate memory.
52 A call to this function with parameter 'Ptr' being NULL
53 is equivalent to a rtl_allocateMemory() call.
54 A call to this function with parameter 'Bytes' being 0
55 is equivalent to a rtl_freeMemory() call.
57 @see rtl_allocateMemory()
60 @param[in] Ptr pointer to the previously allocated memory.
61 @param[in] Bytes new memory size.
62 @return pointer to the reallocated memory. May differ from Ptr.
64 SAL_DLLPUBLIC
void * SAL_CALL
rtl_reallocateMemory (
67 ) SAL_THROW_EXTERN_C();
72 Memory is released, and the pointer is invalidated.
74 @param[in] Ptr pointer to the previously allocated memory.
76 SAL_DLLPUBLIC
void SAL_CALL
rtl_freeMemory (
78 ) SAL_THROW_EXTERN_C();
80 /** Allocate and zero memory.
82 A call to this function will return NULL upon the requested
83 memory size being either zero or larger than currently allocatable.
85 @param[in] Bytes memory size.
86 @return pointer to the allocated and zero'ed memory.
88 SAL_DLLPUBLIC
void * SAL_CALL
rtl_allocateZeroMemory (
90 ) SAL_THROW_EXTERN_C();
94 Fills a block of memory with zeros in a way that is guaranteed to be secure
96 @param[in] Ptr pointer to the previously allocated memory.
97 @param[in] Bytes memory size.
99 @since LibreOffice 5.0
101 SAL_DLLPUBLIC
void SAL_CALL
rtl_secureZeroMemory (
104 ) SAL_THROW_EXTERN_C();
107 /** Zero and free memory.
109 Memory is zero'ed with rtl_secureZeroMemory() and released.
110 The original pointer is no longer valid.
112 @param[in] Ptr pointer to the previously allocated memory.
113 @param[in] Bytes memory size.
115 SAL_DLLPUBLIC
void SAL_CALL
rtl_freeZeroMemory (
118 ) SAL_THROW_EXTERN_C();
120 /** Allocate aligned memory.
122 A call to this function will return NULL upon the requested
123 memory size being either zero or larger than currently allocatable.
125 Memory obtained through this function must be freed with
126 rtl_freeAlignedMemory().
128 @param[in] Alignment alignment in bytes, must be a power of two multiple of
130 @param[in] Bytes memory size.
131 @return pointer to the allocated memory.
133 @since LibreOffice 4.3
135 SAL_DLLPUBLIC
void* SAL_CALL
rtl_allocateAlignedMemory (
138 ) SAL_THROW_EXTERN_C();
141 /** Free memory allocated with rtl_allocateAlignedMemory().
143 Memory is released, and the pointer invalidated.
145 @param[in] Ptr pointer to the previously allocated memory.
147 @since LibreOffice 4.3
149 SAL_DLLPUBLIC
void SAL_CALL
rtl_freeAlignedMemory (
151 ) SAL_THROW_EXTERN_C();
154 /** Opaque rtl_arena_type.
156 typedef struct SAL_DLLPUBLIC_RTTI rtl_arena_st rtl_arena_type
;
158 #define RTL_ARENA_NAME_LENGTH 31
162 * @param[in] pName descriptive name; for debugging purposes.
163 * @param[in] quantum resource allocation unit / granularity; rounded up to next power of 2.
164 * @param[in] quantum_cache_max no longer used, should be 0.
165 * @param[in] source_arena passed as argument to source_alloc, source_free; usually NULL.
166 * @param[in] source_alloc function to allocate resources; usually rtl_arena_alloc.
167 * @param[in] source_free function to free resources; usually rtl_arena_free.
168 * @param[in] nFlags flags; usually 0.
170 * @return pointer to rtl_arena_type, or NULL upon failure.
172 * @see rtl_arena_destroy()
174 SAL_DLLPUBLIC rtl_arena_type
* SAL_CALL
rtl_arena_create (
177 sal_Size quantum_cache_max
,
178 rtl_arena_type
* source_arena
,
179 void * (SAL_CALL
* source_alloc
)(rtl_arena_type
*, sal_Size
*),
180 void (SAL_CALL
* source_free
) (rtl_arena_type
*, void *, sal_Size
),
182 ) SAL_THROW_EXTERN_C();
186 * @param[in] pArena the arena to destroy.
188 * @see rtl_arena_create()
190 SAL_DLLPUBLIC
void SAL_CALL
rtl_arena_destroy (
191 rtl_arena_type
* pArena
192 ) SAL_THROW_EXTERN_C();
196 * @param[in] pArena arena from which resource is allocated.
197 * @param[in,out] pBytes size of resource to allocate.
199 * @return allocated resource, or NULL upon failure.
201 * @see rtl_arena_free()
203 SAL_DLLPUBLIC
void * SAL_CALL
rtl_arena_alloc (
204 rtl_arena_type
* pArena
,
206 ) SAL_THROW_EXTERN_C();
210 * @param[in] pArena arena from which resource was allocated.
211 * @param[in] pAddr resource to free.
212 * @param[in] nBytes size of resource.
214 * @see rtl_arena_alloc()
216 SAL_DLLPUBLIC
void SAL_CALL
rtl_arena_free (
217 rtl_arena_type
* pArena
,
220 ) SAL_THROW_EXTERN_C();
223 /** Opaque rtl_cache_type.
225 typedef struct rtl_cache_st rtl_cache_type
;
227 #define RTL_CACHE_NAME_LENGTH 31
229 #define RTL_CACHE_FLAG_BULKDESTROY 1 /* obsolete */
232 * @param[in] pName descriptive name; for debugging purposes.
233 * @param[in] nObjSize object size.
234 * @param[in] nObjAlign object alignment; usually 0 for suitable default.
235 * @param[in] constructor object constructor callback function; returning 1 for success or 0 for failure.
236 * @param[in] destructor object destructor callback function.
237 * @param[in] reclaim reclaim callback function.
238 * @param[in] pUserArg opaque argument passed to callback functions.
239 * @param[in] pSource unused argument (should be null).
240 * @param[in] nFlags flags (unused).
242 * @return pointer to rtl_cache_type, or NULL upon failure.
244 * @see rtl_cache_destroy()
246 SAL_DLLPUBLIC rtl_cache_type
* SAL_CALL
rtl_cache_create (
250 int (SAL_CALL
* constructor
)(void * pObj
, void * pUserArg
),
251 void (SAL_CALL
* destructor
) (void * pObj
, void * pUserArg
),
252 void (SAL_CALL
* reclaim
) (void * pUserArg
),
254 rtl_arena_type
* pSource
,
256 ) SAL_THROW_EXTERN_C();
260 * @param[in] pCache the cache to destroy.
262 * @see rtl_cache_create()
264 SAL_DLLPUBLIC
void SAL_CALL
rtl_cache_destroy (
265 rtl_cache_type
* pCache
266 ) SAL_THROW_EXTERN_C();
270 * @param[in] pCache cache from which object is allocated.
272 * @return pointer to the allocated object, or NULL upon failure.
274 SAL_DLLPUBLIC
void * SAL_CALL
rtl_cache_alloc (
275 rtl_cache_type
* pCache
276 ) SAL_THROW_EXTERN_C();
280 * @param[in] pCache cache from which object was allocated.
281 * @param[in] pObj object to free.
283 * @see rtl_cache_alloc()
285 SAL_DLLPUBLIC
void SAL_CALL
rtl_cache_free (
286 rtl_cache_type
* pCache
,
288 ) SAL_THROW_EXTERN_C();
291 #ifdef LIBO_INTERNAL_ONLY
293 /** @cond INTERNAL */
294 /** rtl_alloc_preInit
296 * This function, is called at the beginning and again
297 * at the end of LibreOfficeKit pre-initialization to enable
298 * various optimizations.
300 * Its function is to annotate a section @start = true
301 * to end (@start = false) via. two calls. Inside this
302 * section string allocators are replaced with ones which cause the
303 * strings to be staticized at the end of the section.
305 * This brings a number of constraints - in particular no
306 * string allocated outside the section should be freed
307 * inside it, practically this means starting the section
308 * as early as possible. No string allocated inside the
309 * section will be freed subsequently as they are
312 * This method is not thread-safe, nor intended for use in
313 * a threaded context, cf. previous constraints.
315 * It is almost certainly not the method that you want,
316 * use with extraordinary care referring to the
319 * @since LibreOffice 6.1
321 SAL_DLLPUBLIC
void SAL_CALL
rtl_alloc_preInit (
323 ) SAL_THROW_EXTERN_C();
332 #endif // INCLUDED_RTL_ALLOC_H
334 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */