| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 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 |
| 36 |
extern "C" { |
| 37 |
#endif |
| 38 |
|
| 39 |
IALLOC_INLINE void * _GL_ATTRIBUTE_COLD |
| 40 |
_gl_alloc_nomem (void) |
| 41 |
{ |
| 42 |
errno = ENOMEM; |
| 43 |
return NULL; |
| 44 |
} |
| 45 |
|
| 46 |
IALLOC_INLINE |
| 47 |
_GL_ATTRIBUTE_MALLOC |
| 48 |
void * |
| 49 |
imalloc (idx_t s) |
| 50 |
{ |
| 51 |
return s <= SIZE_MAX ? malloc (s) : _gl_alloc_nomem (); |
| 52 |
} |
| 53 |
|
| 54 |
IALLOC_INLINE |
| 55 |
|
| 56 |
void * |
| 57 |
irealloc (void *p, idx_t s) |
| 58 |
{ |
| 59 |
|
| 60 |
|
| 61 |
return s <= SIZE_MAX ? realloc (p, s | !s) : _gl_alloc_nomem (); |
| 62 |
} |
| 63 |
|
| 64 |
IALLOC_INLINE |
| 65 |
_GL_ATTRIBUTE_MALLOC |
| 66 |
void * |
| 67 |
icalloc (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 |
|
| 84 |
IALLOC_INLINE void * |
| 85 |
ireallocarray (void *p, idx_t n, idx_t s) |
| 86 |
{ |
| 87 |
|
| 88 |
|
| 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 |