-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcompat.c
More file actions
40 lines (33 loc) · 721 Bytes
/
compat.c
File metadata and controls
40 lines (33 loc) · 721 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef USE_COMPAT
#define SIZE_T_MAX ~(size_t)0
#define SQRT_MAX (1 << (sizeof(size_t)*8/4))
void *
reallocarray(void *p, size_t nelem, size_t size)
{
if ((nelem >= SQRT_MAX || size >= SQRT_MAX) &&
size > 0 && (SIZE_T_MAX/size) < nelem)
{
errno = ENOMEM;
return NULL;
}
return realloc(p, nelem*size);
}
void *
recallocarray(void *p, size_t oelem, size_t nelem, size_t size)
{
char *np;
size_t nlen, olen;
np = reallocarray(p, nelem, size);
if (np == NULL || nelem <= oelem)
return np;
nlen = size*nelem;
olen = size*oelem;
memset(np + olen, 0, nlen - olen);
return np;
}
#endif // USE_COMPAT