Merge from gnulib.
[bpt/emacs.git] / lib / allocator.h
CommitLineData
d1fdcab7
PE
1/* Memory allocators such as malloc+free.
2
3 Copyright (C) 2011 Free Software Foundation, Inc.
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
9
10 This program 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 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17
18/* Written by Paul Eggert. */
19
20#ifndef _GL_ALLOCATOR_H
e5b28a7a 21#define _GL_ALLOCATOR_H
d1fdcab7
PE
22
23#include <stddef.h>
24
9e0e57c7
PE
25/* An object describing a memory allocator family. */
26
d1fdcab7
PE
27struct allocator
28{
9e0e57c7
PE
29 /* Do not use GCC attributes such as __attribute__ ((malloc)) with
30 the function types pointed at by these members, because these
31 attributes do not work with pointers to functions. See
32 <http://lists.gnu.org/archive/html/bug-gnulib/2011-04/msg00007.html>. */
33
7ec98caf 34 /* Call ALLOCATE to allocate memory, like 'malloc'. On failure ALLOCATE
d1fdcab7
PE
35 should return NULL, though not necessarily set errno. When given
36 a zero size it may return NULL even if successful. */
7ec98caf 37 void *(*allocate) (size_t);
d1fdcab7 38
7ec98caf
PE
39 /* If nonnull, call REALLOCATE to reallocate memory, like 'realloc'.
40 On failure REALLOCATE should return NULL, though not necessarily set
d1fdcab7
PE
41 errno. When given a zero size it may return NULL even if
42 successful. */
7ec98caf 43 void *(*reallocate) (void *, size_t);
d1fdcab7
PE
44
45 /* Call FREE to free memory, like 'free'. */
46 void (*free) (void *);
47
9e0e57c7
PE
48 /* If nonnull, call DIE if MALLOC or REALLOC fails. DIE should not
49 return. DIE can be used by code that detects memory overflow
50 while calculating sizes to be passed to MALLOC or REALLOC. */
d1fdcab7
PE
51 void (*die) (void);
52};
53
7ec98caf
PE
54/* An allocator using the stdlib functions and a null DIE function. */
55extern struct allocator const stdlib_allocator;
56
e5b28a7a 57#endif /* _GL_ALLOCATOR_H */