diff options
| author | Philipp Stephani | 2020-12-24 16:48:40 +0100 |
|---|---|---|
| committer | Philipp Stephani | 2020-12-24 16:48:40 +0100 |
| commit | 29064d02c31b08ae41d41a93fd1439718373b196 (patch) | |
| tree | 6c976729d7c6f296b36965f235d2a58e8142393e /lib/malloc | |
| parent | 26b8b30ff42568ff3e3f8599a20328a1efe93d2a (diff) | |
| download | emacs-29064d02c31b08ae41d41a93fd1439718373b196.tar.gz emacs-29064d02c31b08ae41d41a93fd1439718373b196.zip | |
Update Gnulib.
All changes in this commit are autogenerated by running
admin/merge-gnulib.
Diffstat (limited to 'lib/malloc')
| -rw-r--r-- | lib/malloc/scratch_buffer.h | 135 | ||||
| -rw-r--r-- | lib/malloc/scratch_buffer_grow.c | 56 | ||||
| -rw-r--r-- | lib/malloc/scratch_buffer_grow_preserve.c | 67 | ||||
| -rw-r--r-- | lib/malloc/scratch_buffer_set_array_size.c | 64 |
4 files changed, 322 insertions, 0 deletions
diff --git a/lib/malloc/scratch_buffer.h b/lib/malloc/scratch_buffer.h new file mode 100644 index 00000000000..b40478637c8 --- /dev/null +++ b/lib/malloc/scratch_buffer.h | |||
| @@ -0,0 +1,135 @@ | |||
| 1 | /* Variable-sized buffer with on-stack default allocation. | ||
| 2 | Copyright (C) 2015-2020 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 _SCRATCH_BUFFER_H | ||
| 20 | #define _SCRATCH_BUFFER_H | ||
| 21 | |||
| 22 | /* Scratch buffers with a default stack allocation and fallback to | ||
| 23 | heap allocation. It is expected that this function is used in this | ||
| 24 | way: | ||
| 25 | |||
| 26 | struct scratch_buffer tmpbuf; | ||
| 27 | scratch_buffer_init (&tmpbuf); | ||
| 28 | |||
| 29 | while (!function_that_uses_buffer (tmpbuf.data, tmpbuf.length)) | ||
| 30 | if (!scratch_buffer_grow (&tmpbuf)) | ||
| 31 | return -1; | ||
| 32 | |||
| 33 | scratch_buffer_free (&tmpbuf); | ||
| 34 | return 0; | ||
| 35 | |||
| 36 | The allocation functions (scratch_buffer_grow, | ||
| 37 | scratch_buffer_grow_preserve, scratch_buffer_set_array_size) make | ||
| 38 | sure that the heap allocation, if any, is freed, so that the code | ||
| 39 | above does not have a memory leak. The buffer still remains in a | ||
| 40 | state that can be deallocated using scratch_buffer_free, so a loop | ||
| 41 | like this is valid as well: | ||
| 42 | |||
| 43 | struct scratch_buffer tmpbuf; | ||
| 44 | scratch_buffer_init (&tmpbuf); | ||
| 45 | |||
| 46 | while (!function_that_uses_buffer (tmpbuf.data, tmpbuf.length)) | ||
| 47 | if (!scratch_buffer_grow (&tmpbuf)) | ||
| 48 | break; | ||
| 49 | |||
| 50 | scratch_buffer_free (&tmpbuf); | ||
| 51 | |||
| 52 | scratch_buffer_grow and scratch_buffer_grow_preserve are guaranteed | ||
| 53 | to grow the buffer by at least 512 bytes. This means that when | ||
| 54 | using the scratch buffer as a backing store for a non-character | ||
| 55 | array whose element size, in bytes, is 512 or smaller, the scratch | ||
| 56 | buffer only has to grow once to make room for at least one more | ||
| 57 | element. | ||
| 58 | */ | ||
| 59 | |||
| 60 | #include <stdbool.h> | ||
| 61 | #include <stddef.h> | ||
| 62 | #include <stdlib.h> | ||
| 63 | |||
| 64 | /* Scratch buffer. Must be initialized with scratch_buffer_init | ||
| 65 | before its use. */ | ||
| 66 | struct scratch_buffer { | ||
| 67 | void *data; /* Pointer to the beginning of the scratch area. */ | ||
| 68 | size_t length; /* Allocated space at the data pointer, in bytes. */ | ||
| 69 | union { max_align_t __align; char __c[1024]; } __space; | ||
| 70 | }; | ||
| 71 | |||
| 72 | /* Initializes *BUFFER so that BUFFER->data points to BUFFER->__space | ||
| 73 | and BUFFER->length reflects the available space. */ | ||
| 74 | static inline void | ||
| 75 | scratch_buffer_init (struct scratch_buffer *buffer) | ||
| 76 | { | ||
| 77 | buffer->data = buffer->__space.__c; | ||
| 78 | buffer->length = sizeof (buffer->__space); | ||
| 79 | } | ||
| 80 | |||
| 81 | /* Deallocates *BUFFER (if it was heap-allocated). */ | ||
| 82 | static inline void | ||
| 83 | scratch_buffer_free (struct scratch_buffer *buffer) | ||
| 84 | { | ||
| 85 | if (buffer->data != buffer->__space.__c) | ||
| 86 | free (buffer->data); | ||
| 87 | } | ||
| 88 | |||
| 89 | /* Grow *BUFFER by some arbitrary amount. The buffer contents is NOT | ||
| 90 | preserved. Return true on success, false on allocation failure (in | ||
| 91 | which case the old buffer is freed). On success, the new buffer is | ||
| 92 | larger than the previous size. On failure, *BUFFER is deallocated, | ||
| 93 | but remains in a free-able state, and errno is set. */ | ||
| 94 | bool __libc_scratch_buffer_grow (struct scratch_buffer *buffer); | ||
| 95 | libc_hidden_proto (__libc_scratch_buffer_grow) | ||
| 96 | |||
| 97 | /* Alias for __libc_scratch_buffer_grow. */ | ||
| 98 | static __always_inline bool | ||
| 99 | scratch_buffer_grow (struct scratch_buffer *buffer) | ||
| 100 | { | ||
| 101 | return __glibc_likely (__libc_scratch_buffer_grow (buffer)); | ||
| 102 | } | ||
| 103 | |||
| 104 | /* Like __libc_scratch_buffer_grow, but preserve the old buffer | ||
| 105 | contents on success, as a prefix of the new buffer. */ | ||
| 106 | bool __libc_scratch_buffer_grow_preserve (struct scratch_buffer *buffer); | ||
| 107 | libc_hidden_proto (__libc_scratch_buffer_grow_preserve) | ||
| 108 | |||
| 109 | /* Alias for __libc_scratch_buffer_grow_preserve. */ | ||
| 110 | static __always_inline bool | ||
| 111 | scratch_buffer_grow_preserve (struct scratch_buffer *buffer) | ||
| 112 | { | ||
| 113 | return __glibc_likely (__libc_scratch_buffer_grow_preserve (buffer)); | ||
| 114 | } | ||
| 115 | |||
| 116 | /* Grow *BUFFER so that it can store at least NELEM elements of SIZE | ||
| 117 | bytes. The buffer contents are NOT preserved. Both NELEM and SIZE | ||
| 118 | can be zero. Return true on success, false on allocation failure | ||
| 119 | (in which case the old buffer is freed, but *BUFFER remains in a | ||
| 120 | free-able state, and errno is set). It is unspecified whether this | ||
| 121 | function can reduce the array size. */ | ||
| 122 | bool __libc_scratch_buffer_set_array_size (struct scratch_buffer *buffer, | ||
| 123 | size_t nelem, size_t size); | ||
| 124 | libc_hidden_proto (__libc_scratch_buffer_set_array_size) | ||
| 125 | |||
| 126 | /* Alias for __libc_scratch_set_array_size. */ | ||
| 127 | static __always_inline bool | ||
| 128 | scratch_buffer_set_array_size (struct scratch_buffer *buffer, | ||
| 129 | size_t nelem, size_t size) | ||
| 130 | { | ||
| 131 | return __glibc_likely (__libc_scratch_buffer_set_array_size | ||
| 132 | (buffer, nelem, size)); | ||
| 133 | } | ||
| 134 | |||
| 135 | #endif /* _SCRATCH_BUFFER_H */ | ||
diff --git a/lib/malloc/scratch_buffer_grow.c b/lib/malloc/scratch_buffer_grow.c new file mode 100644 index 00000000000..41befe3d65f --- /dev/null +++ b/lib/malloc/scratch_buffer_grow.c | |||
| @@ -0,0 +1,56 @@ | |||
| 1 | /* Variable-sized buffer with on-stack default allocation. | ||
| 2 | Copyright (C) 2015-2020 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 <errno.h> | ||
| 25 | |||
| 26 | bool | ||
| 27 | __libc_scratch_buffer_grow (struct scratch_buffer *buffer) | ||
| 28 | { | ||
| 29 | void *new_ptr; | ||
| 30 | size_t new_length = buffer->length * 2; | ||
| 31 | |||
| 32 | /* Discard old buffer. */ | ||
| 33 | scratch_buffer_free (buffer); | ||
| 34 | |||
| 35 | /* Check for overflow. */ | ||
| 36 | if (__glibc_likely (new_length >= buffer->length)) | ||
| 37 | new_ptr = malloc (new_length); | ||
| 38 | else | ||
| 39 | { | ||
| 40 | __set_errno (ENOMEM); | ||
| 41 | new_ptr = NULL; | ||
| 42 | } | ||
| 43 | |||
| 44 | if (__glibc_unlikely (new_ptr == NULL)) | ||
| 45 | { | ||
| 46 | /* Buffer must remain valid to free. */ | ||
| 47 | scratch_buffer_init (buffer); | ||
| 48 | return false; | ||
| 49 | } | ||
| 50 | |||
| 51 | /* Install new heap-based buffer. */ | ||
| 52 | buffer->data = new_ptr; | ||
| 53 | buffer->length = new_length; | ||
| 54 | return true; | ||
| 55 | } | ||
| 56 | libc_hidden_def (__libc_scratch_buffer_grow) | ||
diff --git a/lib/malloc/scratch_buffer_grow_preserve.c b/lib/malloc/scratch_buffer_grow_preserve.c new file mode 100644 index 00000000000..aef232938d5 --- /dev/null +++ b/lib/malloc/scratch_buffer_grow_preserve.c | |||
| @@ -0,0 +1,67 @@ | |||
| 1 | /* Variable-sized buffer with on-stack default allocation. | ||
| 2 | Copyright (C) 2015-2020 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 <errno.h> | ||
| 25 | #include <string.h> | ||
| 26 | |||
| 27 | bool | ||
| 28 | __libc_scratch_buffer_grow_preserve (struct scratch_buffer *buffer) | ||
| 29 | { | ||
| 30 | size_t new_length = 2 * buffer->length; | ||
| 31 | void *new_ptr; | ||
| 32 | |||
| 33 | if (buffer->data == buffer->__space.__c) | ||
| 34 | { | ||
| 35 | /* Move buffer to the heap. No overflow is possible because | ||
| 36 | buffer->length describes a small buffer on the stack. */ | ||
| 37 | new_ptr = malloc (new_length); | ||
| 38 | if (new_ptr == NULL) | ||
| 39 | return false; | ||
| 40 | memcpy (new_ptr, buffer->__space.__c, buffer->length); | ||
| 41 | } | ||
| 42 | else | ||
| 43 | { | ||
| 44 | /* Buffer was already on the heap. Check for overflow. */ | ||
| 45 | if (__glibc_likely (new_length >= buffer->length)) | ||
| 46 | new_ptr = realloc (buffer->data, new_length); | ||
| 47 | else | ||
| 48 | { | ||
| 49 | __set_errno (ENOMEM); | ||
| 50 | new_ptr = NULL; | ||
| 51 | } | ||
| 52 | |||
| 53 | if (__glibc_unlikely (new_ptr == NULL)) | ||
| 54 | { | ||
| 55 | /* Deallocate, but buffer must remain valid to free. */ | ||
| 56 | free (buffer->data); | ||
| 57 | scratch_buffer_init (buffer); | ||
| 58 | return false; | ||
| 59 | } | ||
| 60 | } | ||
| 61 | |||
| 62 | /* Install new heap-based buffer. */ | ||
| 63 | buffer->data = new_ptr; | ||
| 64 | buffer->length = new_length; | ||
| 65 | return true; | ||
| 66 | } | ||
| 67 | libc_hidden_def (__libc_scratch_buffer_grow_preserve) | ||
diff --git a/lib/malloc/scratch_buffer_set_array_size.c b/lib/malloc/scratch_buffer_set_array_size.c new file mode 100644 index 00000000000..5f5e4c24f5a --- /dev/null +++ b/lib/malloc/scratch_buffer_set_array_size.c | |||
| @@ -0,0 +1,64 @@ | |||
| 1 | /* Variable-sized buffer with on-stack default allocation. | ||
| 2 | Copyright (C) 2015-2020 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 <errno.h> | ||
| 25 | #include <limits.h> | ||
| 26 | |||
| 27 | bool | ||
| 28 | __libc_scratch_buffer_set_array_size (struct scratch_buffer *buffer, | ||
| 29 | size_t nelem, size_t size) | ||
| 30 | { | ||
| 31 | size_t new_length = nelem * size; | ||
| 32 | |||
| 33 | /* Avoid overflow check if both values are small. */ | ||
| 34 | if ((nelem | size) >> (sizeof (size_t) * CHAR_BIT / 2) != 0 | ||
| 35 | && nelem != 0 && size != new_length / nelem) | ||
| 36 | { | ||
| 37 | /* Overflow. Discard the old buffer, but it must remain valid | ||
| 38 | to free. */ | ||
| 39 | scratch_buffer_free (buffer); | ||
| 40 | scratch_buffer_init (buffer); | ||
| 41 | __set_errno (ENOMEM); | ||
| 42 | return false; | ||
| 43 | } | ||
| 44 | |||
| 45 | if (new_length <= buffer->length) | ||
| 46 | return true; | ||
| 47 | |||
| 48 | /* Discard old buffer. */ | ||
| 49 | scratch_buffer_free (buffer); | ||
| 50 | |||
| 51 | char *new_ptr = malloc (new_length); | ||
| 52 | if (new_ptr == NULL) | ||
| 53 | { | ||
| 54 | /* Buffer must remain valid to free. */ | ||
| 55 | scratch_buffer_init (buffer); | ||
| 56 | return false; | ||
| 57 | } | ||
| 58 | |||
| 59 | /* Install new heap-based buffer. */ | ||
| 60 | buffer->data = new_ptr; | ||
| 61 | buffer->length = new_length; | ||
| 62 | return true; | ||
| 63 | } | ||
| 64 | libc_hidden_def (__libc_scratch_buffer_set_array_size) | ||