aboutsummaryrefslogtreecommitdiffstats
path: root/libparc/parc/algol/parc_Memory.h
blob: fe3c4b5ae02e591e4b2f748cdf34e72bc1cec1a7 (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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * Licensed 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
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @file parc_Memory.h
 * @ingroup memory
 * @brief A Facade to memory allocation features.
 *
 * PARC Memory provides an interface implementing many regularly available memory allocation functions.
 * This interface is a Facade that software implementors may use to substitute different kinds of underlying
 * Interfaces of these allocation fucntions.
 * Notable examples are PARC Safe Memory and PARC Stdlib Memory.
 *
 */
#ifndef libparc_parc_Memory_h
#define libparc_parc_Memory_h

#include <stdlib.h>
#include <stdint.h>

/**
 * @typedef PARCMemoryAllocate
 * @brief Function signature for memory allocator.
 *
 */
typedef void *(PARCMemoryAllocate)(size_t size);

typedef void *(PARCMemoryAllocateAndClear)(size_t size);

typedef int (PARCMemoryMemAlign)(void **pointer, size_t alignment, size_t size);

typedef void (PARCMemoryDeallocate)(void **pointer);

typedef void *(PARCMemoryReallocate)(void *pointer, size_t newSize);

typedef char *(PARCMemoryStringDuplicate)(const char *string, size_t length);

typedef uint32_t (PARCMemoryOutstanding)(void);

/**
 * @typedef PARCMemoryInterface
 * @brief A structure containing pointers to functions that implement a PARC Memory manager.
 *
 * A 'PARC Memory' manager is a collection of inter-dependant functions that perform memory allocation,
 * re-allocation, deallocation, and housekeeping.
 *
 * PARC Memory managers are cascadable, where one Interface may call other Interface in a chain.
 * This permits the design and Interface of PARC Memory managers that specialise in fixed length memory sizes,
 * reference counting, debugging and so forth.
 */
typedef struct parc_memory_interface {
    /**
     * A pointer to a function that allocates @p size bytes of memory
     * and returns the allocation in the return value.
     *
     * @param [in] size The number of bytes to allocate.
     *
     * @return A `void *` pointer indicating the address of the allocated memory.
     * @return NULL Memory allocation error.
     *
     * @see AllocateAndClear
     * @see Reallocate
     */
    uintptr_t Allocate;

    /**
     * Performs the same operation as `Allocate` and then sets each byte of the allocated memory to zero.
     *
     * @param [in] size The number of bytes to allocate.
     *
     * @return A `void *` pointer indicating the address of the allocated memory.
     * @return NULL Memory allocation error.
     *
     * @see Allocate
     * @see Reallocate
     */
    uintptr_t AllocateAndClear;

    /**
     * A pointer to a function that allocates @p size bytes of memory such that the allocation's
     * base address is an exact multiple of alignment,
     * and returns the allocation in the value pointed to by @p pointer.
     *
     * The requested alignment must be a power of 2 greater than or equal to `sizeof(void *)`.
     *
     * Memory that is allocated can be used as an argument in subsequent call `Reallocate`, however
     * `Reallocate` is not guaranteed to preserve the original alignment.
     *
     * @param [out] pointer A pointer to a `void *` pointer that will be set to the address of the allocated memory.
     * @param [in] alignment A power of 2 greater than or equal to `sizeof(void *)`
     * @param [in] size The number of bytes to allocate.
     *
     * @return 0 Successful
     * @return EINVAL The alignment parameter is not a power of 2 at least as large as sizeof(void *)
     * @return ENOMEM Memory allocation error.
     *
     * @see posix_memalign
     */
    uintptr_t MemAlign;

    /**
     * Deallocate memory previously allocated via `Allocate` or `AllocateAndClear`.
     *
     * @param [in,out] pointer A pointer to a `void *` pointer to the address of the allocated memory that will be set to zero.
     *
     * @see AllocateAndClear
     * @see Allocate
     * @see Reallocate
     */
    uintptr_t Deallocate;

    /**
     * Try to change the size of the allocation pointed to by @p pointer to @p newSize, and returns ptr.
     * If there is not enough room to enlarge the memory allocation pointed to by @p pointer,
     * create a new allocation,
     * copy as much of the old data pointed to by @p pointer as will fit to the new allocation,
     * deallocate the old allocation,
     * and return a pointer to the allocated memory.
     *
     * If @p pointer is `NULL`,
     * simply invoke the `Allocate` function to allocate memory aligned to the value of `sizeof(void *)` of @p newSize bytes.
     * If @p newSize is zero and @p pointer is not NULL,
     * a new, minimum sized object is allocated and the original object is freed.
     *
     * When extending a region previously allocated with `AllocateAndClear`,
     * the additional memory is not guaranteed to be zero-filled.
     *
     * @param [in] pointer A pointer to previously allocated memory, or NULL.
     * @param [in] newSize The size of the allocated memory.
     *
     * @return non-NULL A pointer to allocated memory.
     * @return NULL A an error occurred.
     *
     * @see Deallocate
     * @see AllocateAndClear
     * @see Allocate
     */
    uintptr_t Reallocate;

    /**
     * Allocate sufficient memory for a copy of the string @p string,
     * copy at most n characters from the string @p string into the allocated memory,
     * and return the pointer to allocated memory.
     *
     * The copied string is always null-terminated.
     *
     * @param [in] string A pointer to a null-terminated string.
     * @param [length] The maximum allows length of the resulting copy.
     *
     * @return non-NULL A pointer to allocated memory.
     * @return NULL A an error occurred.
     */
    uintptr_t StringDuplicate;

    /**
     * Return the number of allocations outstanding. That is, the numbe of allocations
     * that have been made, but not yet freed.
     *
     * @return The number of outstanding allocations known to this `PARCMemoryInterface`.
     */
    uintptr_t Outstanding;
} PARCMemoryInterface;

/**
 *
 */
extern PARCMemoryInterface PARCMemoryAsPARCMemory;

/**
 * Set the current memory allocation interface.
 *
 * The previous interface is returned.
 *
 * @param [in] memoryProvider A pointer to a {@link PARCMemoryInterface} instance.
 *
 * @return A pointer to the previous `PARCMemoryInterface` instance.
 *
 * Example:
 * @code
 * {
 *     PARCMemoryInterface *previousInterface = parcMemory_SetInterface(&PARCSafeMemoryAsPARCMemory);
 * }
 * @endcode
 *
 * @see PARCSafeMemoryAsPARCMemory
 * @see PARCMemoryAsPARCMemory
 * @see PARCStdlibMemoryAsPARCMemory
 */
const PARCMemoryInterface *parcMemory_SetInterface(const PARCMemoryInterface *memoryProvider);

/**
 * Allocate memory.
 *
 * Allocates @p size bytes of memory and returns the allocation in the return value.
 *
 * Memory that is allocated can be used as an argument in subsequent call `Reallocate`.
 *
 * @param [in] size The number of bytes to allocate.
 *
 * @return A `void *` pointer set to the address of the allocated memory.
 * @return NULL Memory allocation error.
 *
 * Example:
 * @code
 * {
 *     void *allocatedMemory;
 *
 *     allocateMemory = parcMemory_Allocate(100);
 *     if (allocatedMemory == NULL) {
 *         // allocation failed.
 *     }
 * }
 * @endcode
 */
void *parcMemory_Allocate(const size_t size);

/**
 * Performs the same operation as `Allocate` and then sets each byte of the allocated memory to zero.
 *
 * @param [in] size The number of bytes to allocate.
 *
 * @return A `void *` pointer set to the address of the allocated memory.
 * @return NULL Memory allocation error.
 *
 * Example:
 * @code
 * {
 *     void *allocatedMemory;
 *
 *     allocatedMemory = parcMemory_AllocateAndClear(100);
 *     if (allocatedMemory == NULL)
 *         // allocation failed
 *     }
 * }
 * @endcode
 *
 * @see parcMemory_Allocate
 */
void *parcMemory_AllocateAndClear(const size_t size);

/**
 * Allocate aligned memory.
 *
 * Allocates @p size bytes of memory such that the allocation's
 * base address is an exact multiple of alignment,
 * and returns the allocation in the value pointed to by @p pointer.
 *
 * The requested alignment must be a power of 2 greater than or equal to `sizeof(void *)`.
 *
 * Memory that is allocated can be used as an argument in subsequent call `Reallocate`, however
 * `Reallocate` is not guaranteed to preserve the original alignment.
 *
 * @param [out] pointer A pointer to a `void *` pointer that will be set to the address of the allocated memory.
 * @param [in] alignment A power of 2 greater than or equal to `sizeof(void *)`
 * @param [in] size The number of bytes to allocate.
 *
 * @return 0 Successful
 * @return EINVAL The alignment parameter is not a power of 2 at least as large as sizeof(void *)
 * @return ENOMEM Memory allocation error.
 *
 * Example:
 * @code
 * {
 *     void *allocatedMemory;
 *
 *     int failure = parcMemory_MemAlign(&allocatedMemory, sizeof(void *), 100);
 *     if (failure == 0) {
 *         parcMemory_Deallocate(&allocatedMemory);
 *         // allocatedMemory is now equal to zero.
 *     }
 * }
 * @endcode
 * @see `posix_memalign`
 */
int parcMemory_MemAlign(void **pointer, const size_t alignment, const size_t size);

/**
 * Deallocate memory previously allocated via `Allocate` or `AllocateAndClear`.
 *
 * @param [in,out] pointer A pointer to a `void *` pointer to the address of the allocated memory that will be set to zero.
 *
 * Example:
 * @code
 * {
 *     void *allocatedMemory;
 *
 *     allocatedMemory = parcMemory_Allocate(100);
 *     if (allocatedMemory == NULL) {
 *         // allocation failed
 *     }
 * }
 * @endcode
 *
 * @see parcMemory_Allocate
 * @see parcMemory_AllocateAndClear
 */
void parcMemory_DeallocateImpl(void **pointer);

#define parcMemory_Deallocate(_pointer_) parcMemory_DeallocateImpl((void **) _pointer_)

/**
 * Try to change the size of the allocation pointed to by @p pointer to @p newSize, and returns ptr.
 * If there is not enough room to enlarge the memory allocation pointed to by @p pointer,
 * create a new allocation,
 * copy as much of the old data pointed to by @p pointer as will fit to the new allocation,
 * deallocate the old allocation,
 * and return a pointer to the allocated memory.
 *
 * If @p pointer is `NULL`,
 * simply invoke the {@link parcMemory_Allocate} function to allocate memory aligned to the value of `sizeof(void *)` of @p newSize bytes.
 * If @p newSize is zero and @p pointer is not NULL,
 * a new, minimum sized object is allocated and the original object is freed.
 *
 * When extending a region previously allocated with `AllocateAndClear`,
 * the additional memory is not guaranteed to be zero-filled.
 *
 * @param [in] pointer A pointer to previously allocated memory, or NULL.
 * @param [in] newSize The size of the allocated memory.
 *
 * @return non-NULL A pointer to allocated memory.
 * @return NULL A an error occurred.
 *
 * Example:
 * @code
 * {
 *     void *allocatedMemory;
 *
 *     allocateMemory = parcMemory_Allocate(100);
 *
 *     allocatedMemory = parcMemory_Reallocate(allocatedMemory, sizeof(void *), 200);
 *
 *     parcMemory_Deallocate(&allocatedMemory);
 * }
 * @endcode
 *
 * @see parcMemory_Deallocate
 * @see parcMemory_Allocate
 */
void *parcMemory_Reallocate(void *pointer, size_t newSize);

/**
 * Allocate sufficient memory for a copy of the string @p string,
 * copy at most n characters from the string @p string into the allocated memory,
 * and return the pointer to allocated memory.
 *
 * The copied string is always null-terminated.
 *
 * @param [in] string A pointer to a null-terminated string.
 * @param [in] length  The maximum allowed length of the resulting copy.
 *
 * @return non-NULL A pointer to allocated memory.
 * @return NULL A an error occurred.
 *
 * Example:
 * @code
 * {
 *     char *string = "this is a string";
 *     char *copy = parcMemory_StringDuplicate(string, strlen(string));
 *
 *     if (copy != NULL) {
 *         . . .
 *         parcMemory_Deallocate(&copy);
 *     }
 * }
 * @endcode
 *
 * @see {@link parcMemory_Deallocate()}
 */
char *parcMemory_StringDuplicate(const char *string, const size_t length);

/**
 * Return the number of outstanding allocations managed by this allocator.
 *
 * When you allocate memory, this count goes up by one. When you deallocate, it goes down by one.
 * A well-behaved program will terminate with a call to parcMemory_Outstanding() returning 0.
 *
 * @return The number of memory allocations still outstanding (remaining to be deallocated).
 *
 * Example:
 * @code
 * {
 *     uint32_t numberOfAllocations = parcMemory_Outstanding();
 * }
 * @endcode
 */
uint32_t parcMemory_Outstanding(void);

/**
 * Round up a given number of bytes to be a multiple of the cache line size on the target computer.
 *
 * @param [in] size The number of bytes to round up.
 *
 * @return The number of bytes that are a multiple of the cache line size on the target computer.
 *
 * Example:
 * @code
 * {
 *     size_t size = parcMemory_RoundUpToCacheLine(14);
 * }
 * @endcode
 *
 * @see parcMemory_RoundUpToMultiple
 */
size_t parcMemory_RoundUpToCacheLine(const size_t size);

/**
 * Round up a given number of bytes to be an even multiple.
 *
 * @param [in] size The number of bytes to round up.
 * @param [in] multiple The number of bytes to round up.
 *
 * @return The number of bytes that are an even multiple of @p multiple.
 *
 * Example:
 * @code
 * {
 *     size_t size = parcMemory_RoundUp(14, 20);
 * }
 * @endcode
 *
 * @see parcMemory_RoundUpToCacheLine
 */
size_t parcMemory_RoundUpToMultiple(size_t size, size_t multiple);

/**
 * @def parcMemory_SafeFree
 *
 * Free only non-null pointers to memory
 *
 * @param memory A pointer to allocated memory
 *
 */
#define parcMemory_SafeFree(memory) do { if (memory != NULL) { parcMemory_Deallocate(& (memory)); } } while (0)

/**
 * Allocate a printf(3) style formatted string.
 * The result must be deallocated via `parcMemory_Deallocate`
 *
 * This function is equivalent to the `asprintf(3)` function in the standard library.
 *
 * @param [in] format A pointer to nul-terminated C string containing a printf style format specification.
 *
 * @return non-NULL A pointer to allocated memory containing the formatted string.
 * @return NULL An error occurred.
 *
 * Example:
 * @code
 * {
 *     char *string = parcMemory_Format("Hello %s", "World");
 *
 *     parcMemory_Deallocated(&string);
 * }
 * @endcode
 */
char *parcMemory_Format(const char *format, ...);
#endif // libparc_parc_Memory_h