diff options
Diffstat (limited to 'gl/ialloc.h')
| -rw-r--r-- | gl/ialloc.h | 68 |
1 files changed, 54 insertions, 14 deletions
diff --git a/gl/ialloc.h b/gl/ialloc.h index 1d43faf3..2aa94ae7 100644 --- a/gl/ialloc.h +++ b/gl/ialloc.h | |||
| @@ -1,6 +1,6 @@ | |||
| 1 | /* ialloc.h -- malloc with idx_t rather than size_t | 1 | /* ialloc.h -- malloc with idx_t rather than size_t |
| 2 | 2 | ||
| 3 | Copyright 2021-2023 Free Software Foundation, Inc. | 3 | Copyright 2021-2024 Free Software Foundation, Inc. |
| 4 | 4 | ||
| 5 | This file is free software: you can redistribute it and/or modify | 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 | 6 | it under the terms of the GNU Lesser General Public License as |
| @@ -18,15 +18,21 @@ | |||
| 18 | #ifndef IALLOC_H_ | 18 | #ifndef IALLOC_H_ |
| 19 | #define IALLOC_H_ | 19 | #define IALLOC_H_ |
| 20 | 20 | ||
| 21 | /* This file uses _GL_INLINE_HEADER_BEGIN, _GL_INLINE, _GL_ATTRIBUTE_COLD, | ||
| 22 | _GL_ATTRIBUTE_MALLOC. */ | ||
| 23 | #if !_GL_CONFIG_H_INCLUDED | ||
| 24 | #error "Please include config.h first." | ||
| 25 | #endif | ||
| 26 | |||
| 21 | #include "idx.h" | 27 | #include "idx.h" |
| 22 | 28 | ||
| 23 | #include <errno.h> | 29 | #include <errno.h> |
| 24 | #include <stdint.h> | 30 | #include <stdint.h> |
| 25 | #include <stdlib.h> | 31 | #include <stdlib.h> |
| 26 | 32 | #if defined __CHERI_PURE_CAPABILITY__ | |
| 27 | #ifndef _GL_INLINE_HEADER_BEGIN | 33 | # include <cheri.h> |
| 28 | #error "Please include config.h first." | ||
| 29 | #endif | 34 | #endif |
| 35 | |||
| 30 | _GL_INLINE_HEADER_BEGIN | 36 | _GL_INLINE_HEADER_BEGIN |
| 31 | #ifndef IALLOC_INLINE | 37 | #ifndef IALLOC_INLINE |
| 32 | # define IALLOC_INLINE _GL_INLINE | 38 | # define IALLOC_INLINE _GL_INLINE |
| @@ -43,6 +49,9 @@ _gl_alloc_nomem (void) | |||
| 43 | return NULL; | 49 | return NULL; |
| 44 | } | 50 | } |
| 45 | 51 | ||
| 52 | /* imalloc (size) is like malloc (size). | ||
| 53 | It returns a non-NULL pointer to size bytes of memory. | ||
| 54 | Upon failure, it returns NULL with errno set. */ | ||
| 46 | IALLOC_INLINE | 55 | IALLOC_INLINE |
| 47 | _GL_ATTRIBUTE_MALLOC /*_GL_ATTRIBUTE_DEALLOC_FREE*/ | 56 | _GL_ATTRIBUTE_MALLOC /*_GL_ATTRIBUTE_DEALLOC_FREE*/ |
| 48 | void * | 57 | void * |
| @@ -51,16 +60,32 @@ imalloc (idx_t s) | |||
| 51 | return s <= SIZE_MAX ? malloc (s) : _gl_alloc_nomem (); | 60 | return s <= SIZE_MAX ? malloc (s) : _gl_alloc_nomem (); |
| 52 | } | 61 | } |
| 53 | 62 | ||
| 63 | /* irealloc (ptr, size) is like realloc (ptr, size). | ||
| 64 | It returns a non-NULL pointer to size bytes of memory. | ||
| 65 | Upon failure, it returns NULL with errno set. */ | ||
| 54 | IALLOC_INLINE | 66 | IALLOC_INLINE |
| 55 | /*_GL_ATTRIBUTE_DEALLOC_FREE*/ | 67 | /*_GL_ATTRIBUTE_DEALLOC_FREE*/ |
| 56 | void * | 68 | void * |
| 57 | irealloc (void *p, idx_t s) | 69 | irealloc (void *p, idx_t s) |
| 58 | { | 70 | { |
| 59 | /* Work around GNU realloc glitch by treating a zero size as if it | 71 | if (s <= SIZE_MAX) |
| 60 | were 1, so that returning NULL is equivalent to failing. */ | 72 | { |
| 61 | return s <= SIZE_MAX ? realloc (p, s | !s) : _gl_alloc_nomem (); | 73 | /* Work around GNU realloc glitch by treating a zero size as if it |
| 74 | were 1, so that returning NULL is equivalent to failing. */ | ||
| 75 | p = realloc (p, s | !s); | ||
| 76 | #if defined __CHERI_PURE_CAPABILITY__ | ||
| 77 | if (p != NULL) | ||
| 78 | p = cheri_bounds_set (p, s); | ||
| 79 | #endif | ||
| 80 | return p; | ||
| 81 | } | ||
| 82 | else | ||
| 83 | return _gl_alloc_nomem (); | ||
| 62 | } | 84 | } |
| 63 | 85 | ||
| 86 | /* icalloc (num, size) is like calloc (num, size). | ||
| 87 | It returns a non-NULL pointer to num * size bytes of memory. | ||
| 88 | Upon failure, it returns NULL with errno set. */ | ||
| 64 | IALLOC_INLINE | 89 | IALLOC_INLINE |
| 65 | _GL_ATTRIBUTE_MALLOC /*_GL_ATTRIBUTE_DEALLOC_FREE*/ | 90 | _GL_ATTRIBUTE_MALLOC /*_GL_ATTRIBUTE_DEALLOC_FREE*/ |
| 66 | void * | 91 | void * |
| @@ -81,20 +106,35 @@ icalloc (idx_t n, idx_t s) | |||
| 81 | return calloc (n, s); | 106 | return calloc (n, s); |
| 82 | } | 107 | } |
| 83 | 108 | ||
| 109 | /* ireallocarray (ptr, num, size) is like reallocarray (ptr, num, size). | ||
| 110 | It returns a non-NULL pointer to num * size bytes of memory. | ||
| 111 | Upon failure, it returns NULL with errno set. */ | ||
| 84 | IALLOC_INLINE void * | 112 | IALLOC_INLINE void * |
| 85 | ireallocarray (void *p, idx_t n, idx_t s) | 113 | ireallocarray (void *p, idx_t n, idx_t s) |
| 86 | { | 114 | { |
| 87 | /* Work around GNU reallocarray glitch by treating a zero size as if | 115 | if (n <= SIZE_MAX && s <= SIZE_MAX) |
| 88 | it were 1, so that returning NULL is equivalent to failing. */ | 116 | { |
| 89 | if (n == 0 || s == 0) | 117 | /* Work around GNU reallocarray glitch by treating a zero size as if |
| 90 | n = s = 1; | 118 | it were 1, so that returning NULL is equivalent to failing. */ |
| 91 | return (n <= SIZE_MAX && s <= SIZE_MAX | 119 | size_t nx = n; |
| 92 | ? reallocarray (p, n, s) | 120 | size_t sx = s; |
| 93 | : _gl_alloc_nomem ()); | 121 | if (n == 0 || s == 0) |
| 122 | nx = sx = 1; | ||
| 123 | p = reallocarray (p, nx, sx); | ||
| 124 | #if defined __CHERI_PURE_CAPABILITY__ | ||
| 125 | if (p != NULL && (n == 0 || s == 0)) | ||
| 126 | p = cheri_bounds_set (p, 0); | ||
| 127 | #endif | ||
| 128 | return p; | ||
| 129 | } | ||
| 130 | else | ||
| 131 | return _gl_alloc_nomem (); | ||
| 94 | } | 132 | } |
| 95 | 133 | ||
| 96 | #ifdef __cplusplus | 134 | #ifdef __cplusplus |
| 97 | } | 135 | } |
| 98 | #endif | 136 | #endif |
| 99 | 137 | ||
| 138 | _GL_INLINE_HEADER_END | ||
| 139 | |||
| 100 | #endif | 140 | #endif |
