aboutsummaryrefslogtreecommitdiffstats
path: root/lib/malloc
diff options
context:
space:
mode:
authorAndrea Corallo2021-01-02 10:11:15 +0100
committerAndrea Corallo2021-01-02 10:11:15 +0100
commit5db5064395c251a822e429e19ddecb74a974b6ef (patch)
tree2e04729a03d5fc68d0caef3a16e00349b6d413fc /lib/malloc
parent9420ea6e0840bffcb140d3677dfdabb9251c1f63 (diff)
parent0f561ee55348ff451600cc6027db5940ee14606f (diff)
downloademacs-5db5064395c251a822e429e19ddecb74a974b6ef.tar.gz
emacs-5db5064395c251a822e429e19ddecb74a974b6ef.zip
Merge remote-tracking branch 'savannah/master' into HEAD
Diffstat (limited to 'lib/malloc')
-rw-r--r--lib/malloc/scratch_buffer.h18
-rw-r--r--lib/malloc/scratch_buffer_dupfree.c41
2 files changed, 58 insertions, 1 deletions
diff --git a/lib/malloc/scratch_buffer.h b/lib/malloc/scratch_buffer.h
index b40478637c8..26e306212d1 100644
--- a/lib/malloc/scratch_buffer.h
+++ b/lib/malloc/scratch_buffer.h
@@ -1,5 +1,5 @@
1/* Variable-sized buffer with on-stack default allocation. 1/* Variable-sized buffer with on-stack default allocation.
2 Copyright (C) 2015-2020 Free Software Foundation, Inc. 2 Copyright (C) 2015-2021 Free Software Foundation, Inc.
3 This file is part of the GNU C Library. 3 This file is part of the GNU C Library.
4 4
5 The GNU C Library is free software; you can redistribute it and/or 5 The GNU C Library is free software; you can redistribute it and/or
@@ -132,4 +132,20 @@ scratch_buffer_set_array_size (struct scratch_buffer *buffer,
132 (buffer, nelem, size)); 132 (buffer, nelem, size));
133} 133}
134 134
135/* Return a copy of *BUFFER's first SIZE bytes as a heap-allocated block,
136 deallocating *BUFFER if it was heap-allocated. SIZE must be at
137 most *BUFFER's size. Return NULL (setting errno) on memory
138 exhaustion. */
139void *__libc_scratch_buffer_dupfree (struct scratch_buffer *buffer,
140 size_t size);
141libc_hidden_proto (__libc_scratch_buffer_dupfree)
142
143/* Alias for __libc_scratch_dupfree. */
144static __always_inline void *
145scratch_buffer_dupfree (struct scratch_buffer *buffer, size_t size)
146{
147 void *r = __libc_scratch_buffer_dupfree (buffer, size);
148 return __glibc_likely (r != NULL) ? r : NULL;
149}
150
135#endif /* _SCRATCH_BUFFER_H */ 151#endif /* _SCRATCH_BUFFER_H */
diff --git a/lib/malloc/scratch_buffer_dupfree.c b/lib/malloc/scratch_buffer_dupfree.c
new file mode 100644
index 00000000000..775bff5609f
--- /dev/null
+++ b/lib/malloc/scratch_buffer_dupfree.c
@@ -0,0 +1,41 @@
1/* Variable-sized buffer with on-stack default allocation.
2 Copyright (C) 2020-2021 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public
7 License as published by the Free Software Foundation; either
8 version 3 of the License, or (at your option) any later version.
9
10 The GNU C Library 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 GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19#ifndef _LIBC
20# include <libc-config.h>
21#endif
22
23#include <scratch_buffer.h>
24#include <string.h>
25
26void *
27__libc_scratch_buffer_dupfree (struct scratch_buffer *buffer, size_t size)
28{
29 void *data = buffer->data;
30 if (data == buffer->__space.__c)
31 {
32 void *copy = malloc (size);
33 return copy != NULL ? memcpy (copy, data, size) : NULL;
34 }
35 else
36 {
37 void *copy = realloc (data, size);
38 return copy != NULL ? copy : data;
39 }
40}
41libc_hidden_def (__libc_scratch_buffer_dupfree)