summaryrefslogtreecommitdiffstats
path: root/gl/ialloc.h
diff options
context:
space:
mode:
authorLorenz <12514511+RincewindsHat@users.noreply.github.com>2023-03-11 10:34:47 (GMT)
committerGitHub <noreply@github.com>2023-03-11 10:34:47 (GMT)
commit75342b53853a898bdd93c7d3da9374115bab6b6d (patch)
tree0c0af2b796bcdd8ddd42187beb5badb420db8dea /gl/ialloc.h
parent6bbe0b7b0f609ecab831dec9be7690842bf0a0fc (diff)
parent357787868b5201ec3e874e7a225b1c944cbbdb4d (diff)
downloadmonitoring-plugins-75342b53853a898bdd93c7d3da9374115bab6b6d.tar.gz
Merge branch 'master' into patch-1refs/pull/1842/head
Diffstat (limited to 'gl/ialloc.h')
-rw-r--r--gl/ialloc.h100
1 files changed, 100 insertions, 0 deletions
diff --git a/gl/ialloc.h b/gl/ialloc.h
new file mode 100644
index 0000000..1d43faf
--- /dev/null
+++ b/gl/ialloc.h
@@ -0,0 +1,100 @@
1/* ialloc.h -- malloc with idx_t rather than size_t
2
3 Copyright 2021-2023 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#ifndef IALLOC_H_
19#define IALLOC_H_
20
21#include "idx.h"
22
23#include <errno.h>
24#include <stdint.h>
25#include <stdlib.h>
26
27#ifndef _GL_INLINE_HEADER_BEGIN
28 #error "Please include config.h first."
29#endif
30_GL_INLINE_HEADER_BEGIN
31#ifndef IALLOC_INLINE
32# define IALLOC_INLINE _GL_INLINE
33#endif
34
35#ifdef __cplusplus
36extern "C" {
37#endif
38
39IALLOC_INLINE void * _GL_ATTRIBUTE_COLD
40_gl_alloc_nomem (void)
41{
42 errno = ENOMEM;
43 return NULL;
44}
45
46IALLOC_INLINE
47_GL_ATTRIBUTE_MALLOC /*_GL_ATTRIBUTE_DEALLOC_FREE*/
48void *
49imalloc (idx_t s)
50{
51 return s <= SIZE_MAX ? malloc (s) : _gl_alloc_nomem ();
52}
53
54IALLOC_INLINE
55/*_GL_ATTRIBUTE_DEALLOC_FREE*/
56void *
57irealloc (void *p, idx_t s)
58{
59 /* Work around GNU realloc glitch by treating a zero size as if it
60 were 1, so that returning NULL is equivalent to failing. */
61 return s <= SIZE_MAX ? realloc (p, s | !s) : _gl_alloc_nomem ();
62}
63
64IALLOC_INLINE
65_GL_ATTRIBUTE_MALLOC /*_GL_ATTRIBUTE_DEALLOC_FREE*/
66void *
67icalloc (idx_t n, idx_t s)
68{
69 if (SIZE_MAX < n)
70 {
71 if (s != 0)
72 return _gl_alloc_nomem ();
73 n = 0;
74 }
75 if (SIZE_MAX < s)
76 {
77 if (n != 0)
78 return _gl_alloc_nomem ();
79 s = 0;
80 }
81 return calloc (n, s);
82}
83
84IALLOC_INLINE void *
85ireallocarray (void *p, idx_t n, idx_t s)
86{
87 /* Work around GNU reallocarray glitch by treating a zero size as if
88 it were 1, so that returning NULL is equivalent to failing. */
89 if (n == 0 || s == 0)
90 n = s = 1;
91 return (n <= SIZE_MAX && s <= SIZE_MAX
92 ? reallocarray (p, n, s)
93 : _gl_alloc_nomem ());
94}
95
96#ifdef __cplusplus
97}
98#endif
99
100#endif