summaryrefslogtreecommitdiffstats
path: root/include/rtl/alloc.h
blob: 74d4ea5c4339dee64273049e6f6a4b699ecc8232 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#ifndef INCLUDED_RTL_ALLOC_H
#define INCLUDED_RTL_ALLOC_H

#include "sal/config.h"

#include "sal/saldllapi.h"
#include "sal/types.h"

#ifdef __cplusplus
extern "C" {
#endif


/** Allocate memory.

    A call to this function will return NULL upon the requested
    memory size being either zero or larger than currently allocatable.

    @param[in] Bytes memory size.
    @return pointer to the allocated memory.
 */
SAL_DLLPUBLIC void * SAL_CALL rtl_allocateMemory (
    sal_Size Bytes
) SAL_THROW_EXTERN_C();


/** Reallocate memory.

    A call to this function with parameter 'Ptr' being NULL
    is equivalent to a rtl_allocateMemory() call.
    A call to this function with parameter 'Bytes' being 0
    is equivalent to a rtl_freeMemory() call.

    @see rtl_allocateMemory()
    @see rtl_freeMemory()

    @param[in] Ptr   pointer to the previously allocated memory.
    @param[in] Bytes new memory size.
    @return pointer to the reallocated memory. May differ from Ptr.
 */
SAL_DLLPUBLIC void * SAL_CALL rtl_reallocateMemory (
    void *   Ptr,
    sal_Size Bytes
) SAL_THROW_EXTERN_C();


/** Free memory.
    @param[in] Ptr pointer to the previously allocated memory.
    @return none. Memory is released. Ptr is invalid.
 */
SAL_DLLPUBLIC void SAL_CALL rtl_freeMemory (
    void * Ptr
) SAL_THROW_EXTERN_C();

/** Allocate and zero memory.

    A call to this function will return NULL upon the requested
    memory size being either zero or larger than currently allocatable.

    @param[in] Bytes memory size.
    @return pointer to the allocated and zero'ed memory.
 */
SAL_DLLPUBLIC void * SAL_CALL rtl_allocateZeroMemory (
    sal_Size Bytes
) SAL_THROW_EXTERN_C();

/** Zero memory

    Fills a block of memory with zeros in a way that is guaranteed to be secure

    @param[in] Ptr   pointer to the previously allocated memory.
    @param[in] Bytes memory size.

    @since LibreOffice 5.0
 */
SAL_DLLPUBLIC void SAL_CALL rtl_secureZeroMemory (
    void *   Ptr,
    sal_Size Bytes
) SAL_THROW_EXTERN_C();


/** Zero and free memory.
    @param[in] Ptr   pointer to the previously allocated memory.
    @param[in] Bytes memory size.
    @return none. Memory is zero'ed with rtl_secureZeroMemory() and released.
                  Ptr is invalid.
 */
SAL_DLLPUBLIC void SAL_CALL rtl_freeZeroMemory (
    void *   Ptr,
    sal_Size Bytes
) SAL_THROW_EXTERN_C();

/** Allocate aligned memory.

    A call to this function will return NULL upon the requested
    memory size being either zero or larger than currently allocatable.

    Memory obtained through this function must be freed with
    rtl_freeAlignedMemory().

    @param[in] Alignment alignment in bytes, must be a power of two multiple of
        sizeof(void*).
    @param[in] Bytes     memory size.
    @return pointer to the allocated memory.

    @since LibreOffice 4.3
 */
SAL_DLLPUBLIC void* SAL_CALL rtl_allocateAlignedMemory (
    sal_Size Alignment,
    sal_Size Bytes
) SAL_THROW_EXTERN_C();


/** Free memory allocated with rtl_allocateAlignedMemory().

    @param[in] Ptr pointer to the previously allocated memory.
    @return none. Memory is released. Ptr is invalid.

    @since LibreOffice 4.3
 */
SAL_DLLPUBLIC void SAL_CALL rtl_freeAlignedMemory (
    void *   Ptr
) SAL_THROW_EXTERN_C();


/** Opaque rtl_arena_type.
 */
typedef struct SAL_DLLPUBLIC_RTTI rtl_arena_st rtl_arena_type;

#define RTL_ARENA_NAME_LENGTH 31


/**
 *  @param[in] pName             descriptive name; for debugging purposes.
 *  @param[in] quantum           resource allocation unit / granularity; rounded up to next power of 2.
 *  @param[in] quantum_cache_max no longer used, should be 0.
 *  @param[in] source_arena      passed as argument to source_alloc, source_free; usually NULL.
 *  @param[in] source_alloc      function to allocate resources; usually rtl_arena_alloc.
 *  @param[in] source_free       function to free resources; usually rtl_arena_free.
 *  @param[in] nFlags            flags; usually 0.
 *
 *  @return pointer to rtl_arena_type, or NULL upon failure.
 *
 *  @see rtl_arena_destroy()
 */
SAL_DLLPUBLIC rtl_arena_type * SAL_CALL rtl_arena_create (
    const char *       pName,
    sal_Size           quantum,
    sal_Size           quantum_cache_max,
    rtl_arena_type *   source_arena,
    void * (SAL_CALL * source_alloc)(rtl_arena_type *, sal_Size *),
    void   (SAL_CALL * source_free) (rtl_arena_type *, void *, sal_Size),
    int                nFlags
) SAL_THROW_EXTERN_C();


/**
 *  @param[in] pArena the arena to destroy.
 *  @return None
 *
 *  @see rtl_arena_create()
 */
SAL_DLLPUBLIC void SAL_CALL rtl_arena_destroy (
    rtl_arena_type * pArena
) SAL_THROW_EXTERN_C();


/**
 *  @param[in]     pArena arena from which resource is allocated.
 *  @param[in,out] pBytes size of resource to allocate.
 *
 *  @return allocated resource, or NULL upon failure.
 *
 *  @see rtl_arena_free()
 */
SAL_DLLPUBLIC void * SAL_CALL rtl_arena_alloc (
    rtl_arena_type * pArena,
    sal_Size *       pBytes
) SAL_THROW_EXTERN_C();


/**
 *  @param[in] pArena arena from which resource was allocated.
 *  @param[in] pAddr  resource to free.
 *  @param[in] nBytes size of resource.
 *
 *  @return None.
 *
 *  @see rtl_arena_alloc()
 */
SAL_DLLPUBLIC void SAL_CALL rtl_arena_free (
    rtl_arena_type * pArena,
    void *           pAddr,
    sal_Size         nBytes
) SAL_THROW_EXTERN_C();


/** Opaque rtl_cache_type.
 */
typedef struct rtl_cache_st rtl_cache_type;

#define RTL_CACHE_NAME_LENGTH 31

#define RTL_CACHE_FLAG_BULKDESTROY 1 /* obsolete */

/**
 *  @param[in] pName       descriptive name; for debugging purposes.
 *  @param[in] nObjSize    object size.
 *  @param[in] nObjAlign   object alignment; usually 0 for suitable default.
 *  @param[in] constructor object constructor callback function; returning 1 for success or 0 for failure.
 *  @param[in] destructor  object destructor callback function.
 *  @param[in] reclaim     reclaim callback function.
 *  @param[in] pUserArg    opaque argument passed to callback functions.
 *  @param[in] pSource     unused argument (should be null).
 *  @param[in] nFlags      flags (unused).
 *
 *  @return pointer to rtl_cache_type, or NULL upon failure.
 *
 *  @see rtl_cache_destroy()
 */
SAL_DLLPUBLIC rtl_cache_type * SAL_CALL rtl_cache_create (
    const char *     pName,
    sal_Size         nObjSize,
    sal_Size         nObjAlign,
    int  (SAL_CALL * constructor)(void * pObj, void * pUserArg),
    void (SAL_CALL * destructor) (void * pObj, void * pUserArg),
    void (SAL_CALL * reclaim)    (void * pUserArg),
    void *           pUserArg,
    rtl_arena_type * pSource,
    int              nFlags
) SAL_THROW_EXTERN_C();


/**
 *  @param[in] pCache the cache to destroy.
 *
 *  @return None.
 *
 *  @see rtl_cache_create()
 */
SAL_DLLPUBLIC void SAL_CALL rtl_cache_destroy (
    rtl_cache_type * pCache
) SAL_THROW_EXTERN_C();


/**
 *  @param[in] pCache cache from which object is allocated.
 *
 *  @return pointer to the allocated object, or NULL upon failure.
 */
SAL_DLLPUBLIC void * SAL_CALL rtl_cache_alloc (
    rtl_cache_type * pCache
) SAL_THROW_EXTERN_C();


/**
 *  @param[in] pCache cache from which object was allocated.
 *  @param[in] pObj   object to free.
 *
 *  @return None.
 *
 *  @see rtl_cache_alloc()
 */
SAL_DLLPUBLIC void SAL_CALL rtl_cache_free (
    rtl_cache_type * pCache,
    void *           pObj
) SAL_THROW_EXTERN_C();


#ifdef LIBO_INTERNAL_ONLY

/** @cond INTERNAL */
/** rtl_alloc_preInit
 *
 * This function, is called at the beginning and again
 * at the end of LibreOfficeKit pre-initialization to enable
 * various optimizations.
 *
 * Its function is to annotate a section @start = true
 * to end (@start = false) via. two calls. Inside this
 * section string allocators are replaced with ones which cause the
 * strings to be staticized at the end of the section.
 *
 * This brings a number of constraints - in particular no
 * string allocated outside the section should be freed
 * inside it, practically this means starting the section
 * as early as possible. No string allocated inside the
 * section will be freed subsequently as they are
 * staticized.
 *
 * This method is not thread-safe, nor intended for use in
 * a threaded context, cf. previous constraints.
 *
 * It is almost certainly not the method that you want,
 * use with extraordinary care referring to the
 * implementation.
 *
 * @since LibreOffice 6.1
 */
SAL_DLLPUBLIC void SAL_CALL rtl_alloc_preInit (
    sal_Bool start
) SAL_THROW_EXTERN_C();
/** @endcond */

#endif

#ifdef __cplusplus
}
#endif

#endif // INCLUDED_RTL_ALLOC_H

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */