diff options
Diffstat (limited to 'xmalloc.c')
-rw-r--r-- | xmalloc.c | 48 |
1 files changed, 45 insertions, 3 deletions
@@ -1,3 +1,4 @@ | |||
1 | /* $OpenBSD: xmalloc.c,v 1.27 2006/08/03 03:34:42 deraadt Exp $ */ | ||
1 | /* | 2 | /* |
2 | * Author: Tatu Ylonen <ylo@cs.hut.fi> | 3 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
3 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland | 4 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
@@ -13,7 +14,12 @@ | |||
13 | */ | 14 | */ |
14 | 15 | ||
15 | #include "includes.h" | 16 | #include "includes.h" |
16 | RCSID("$OpenBSD: xmalloc.c,v 1.16 2001/07/23 18:21:46 stevesk Exp $"); | 17 | |
18 | #include <sys/param.h> | ||
19 | #include <stdarg.h> | ||
20 | #include <stdio.h> | ||
21 | #include <stdlib.h> | ||
22 | #include <string.h> | ||
17 | 23 | ||
18 | #include "xmalloc.h" | 24 | #include "xmalloc.h" |
19 | #include "log.h" | 25 | #include "log.h" |
@@ -32,18 +38,38 @@ xmalloc(size_t size) | |||
32 | } | 38 | } |
33 | 39 | ||
34 | void * | 40 | void * |
35 | xrealloc(void *ptr, size_t new_size) | 41 | xcalloc(size_t nmemb, size_t size) |
42 | { | ||
43 | void *ptr; | ||
44 | |||
45 | if (size == 0 || nmemb == 0) | ||
46 | fatal("xcalloc: zero size"); | ||
47 | if (SIZE_T_MAX / nmemb < size) | ||
48 | fatal("xcalloc: nmemb * size > SIZE_T_MAX"); | ||
49 | ptr = calloc(nmemb, size); | ||
50 | if (ptr == NULL) | ||
51 | fatal("xcalloc: out of memory (allocating %lu bytes)", | ||
52 | (u_long)(size * nmemb)); | ||
53 | return ptr; | ||
54 | } | ||
55 | |||
56 | void * | ||
57 | xrealloc(void *ptr, size_t nmemb, size_t size) | ||
36 | { | 58 | { |
37 | void *new_ptr; | 59 | void *new_ptr; |
60 | size_t new_size = nmemb * size; | ||
38 | 61 | ||
39 | if (new_size == 0) | 62 | if (new_size == 0) |
40 | fatal("xrealloc: zero size"); | 63 | fatal("xrealloc: zero size"); |
64 | if (SIZE_T_MAX / nmemb < size) | ||
65 | fatal("xrealloc: nmemb * size > SIZE_T_MAX"); | ||
41 | if (ptr == NULL) | 66 | if (ptr == NULL) |
42 | new_ptr = malloc(new_size); | 67 | new_ptr = malloc(new_size); |
43 | else | 68 | else |
44 | new_ptr = realloc(ptr, new_size); | 69 | new_ptr = realloc(ptr, new_size); |
45 | if (new_ptr == NULL) | 70 | if (new_ptr == NULL) |
46 | fatal("xrealloc: out of memory (new_size %lu bytes)", (u_long) new_size); | 71 | fatal("xrealloc: out of memory (new_size %lu bytes)", |
72 | (u_long) new_size); | ||
47 | return new_ptr; | 73 | return new_ptr; |
48 | } | 74 | } |
49 | 75 | ||
@@ -66,3 +92,19 @@ xstrdup(const char *str) | |||
66 | strlcpy(cp, str, len); | 92 | strlcpy(cp, str, len); |
67 | return cp; | 93 | return cp; |
68 | } | 94 | } |
95 | |||
96 | int | ||
97 | xasprintf(char **ret, const char *fmt, ...) | ||
98 | { | ||
99 | va_list ap; | ||
100 | int i; | ||
101 | |||
102 | va_start(ap, fmt); | ||
103 | i = vasprintf(ret, fmt, ap); | ||
104 | va_end(ap); | ||
105 | |||
106 | if (i < 0 || *ret == NULL) | ||
107 | fatal("xasprintf: could not allocate memory"); | ||
108 | |||
109 | return (i); | ||
110 | } | ||