| 1 |
/* Memory allocators such as malloc+free. |
| 2 |
|
| 3 |
Copyright (C) 2011-2025 Free Software Foundation, Inc. |
| 4 |
|
| 5 |
This file is free software: you can redistribute it and/or modify |
| 6 |
it under the terms of the GNU Lesser General Public License as |
| 7 |
published by the Free Software Foundation; either version 2.1 of the |
| 8 |
License, or (at your option) any later version. |
| 9 |
|
| 10 |
This file is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
GNU Lesser General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU Lesser General Public License |
| 16 |
along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
| 17 |
|
| 18 |
/* Written by Paul Eggert. */ |
| 19 |
|
| 20 |
#ifndef _GL_ALLOCATOR_H |
| 21 |
#define _GL_ALLOCATOR_H |
| 22 |
|
| 23 |
#include <stddef.h> |
| 24 |
|
| 25 |
#ifdef __cplusplus |
| 26 |
extern "C" { |
| 27 |
#endif |
| 28 |
|
| 29 |
|
| 30 |
/* An object describing a memory allocator family. */ |
| 31 |
|
| 32 |
struct allocator |
| 33 |
{ |
| 34 |
/* Do not use GCC attributes such as __attribute__ ((malloc)) with |
| 35 |
the function types pointed at by these members, because these |
| 36 |
attributes do not work with pointers to functions. See |
| 37 |
<https://lists.gnu.org/r/bug-gnulib/2011-04/msg00007.html>. */ |
| 38 |
|
| 39 |
/* Call ALLOCATE to allocate memory, like 'malloc'. On failure ALLOCATE |
| 40 |
should return NULL, though not necessarily set errno. When given |
| 41 |
a zero size it may return NULL even if successful. */ |
| 42 |
void *(*allocate) (size_t); |
| 43 |
|
| 44 |
/* If nonnull, call REALLOCATE to reallocate memory, like 'realloc'. |
| 45 |
On failure REALLOCATE should return NULL, though not necessarily set |
| 46 |
errno. When given a zero size it may return NULL even if |
| 47 |
successful. */ |
| 48 |
void *(*reallocate) (void *, size_t); |
| 49 |
|
| 50 |
/* Call FREE to free memory, like 'free'. */ |
| 51 |
void (*free) (void *); |
| 52 |
|
| 53 |
/* If nonnull, call DIE (SIZE) if MALLOC (SIZE) or REALLOC (..., |
| 54 |
SIZE) fails. DIE should not return. SIZE should equal SIZE_MAX |
| 55 |
if size_t overflow was detected while calculating sizes to be |
| 56 |
passed to MALLOC or REALLOC. */ |
| 57 |
void (*die) (size_t); |
| 58 |
}; |
| 59 |
|
| 60 |
/* An allocator using the stdlib functions and a null DIE function. */ |
| 61 |
extern struct allocator const stdlib_allocator; |
| 62 |
|
| 63 |
|
| 64 |
#ifdef __cplusplus |
| 65 |
} |
| 66 |
#endif |
| 67 |
|
| 68 |
#endif /* _GL_ALLOCATOR_H */ |