forked from JoeDog/siege
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory.c
140 lines (122 loc) · 2.79 KB
/
memory.c
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
* Memory handling
*
* Copyright (C) 2000-2016 by
* Jeffrey Fulmer - <[email protected]>
* This file is distributed as part of Siege
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
* --
*
*/
#include <config.h>
#include <memory.h>
#include <notify.h>
#include <string.h>
#include <stdarg.h>
char *
xstrdup(const char *str)
{
register char *ret;
#ifndef HAVE_STRDUP
register size_t len;
#endif/*HAVE_STRDUP*/
if(!str){
NOTIFY(ERROR, "string has no value!");
return NULL;
}
#ifdef HAVE_STRDUP
ret = strdup(str);
if(ret==NULL) NOTIFY(FATAL, "xstrdup: unable to allocate additional memory");
#else
len = strlen(str)+1;
ret = malloc(len);
if (ret==NULL) {
NOTIFY(FATAL, "xstrdup: unable to allocate additional memory");
}
memcpy(ret, str, len);
#endif
return ret;
}
char *
xstrcat(const char *arg1, ...)
{
const char *argptr;
char *resptr, *result;
size_t len = 0;
va_list valist;
va_start(valist, arg1);
for(argptr = arg1; argptr != NULL; argptr = va_arg(valist, char *))
len += strlen(argptr);
va_end(valist);
result = xmalloc(len + 1);
resptr = result;
va_start(valist, arg1);
for(argptr = arg1; argptr != NULL; argptr = va_arg(valist, char *)) {
len = strlen(argptr);
memcpy(resptr, argptr, len);
resptr += len;
}
va_end(valist);
*resptr = '\0';
return result;
}
/**
* xrealloc: value added realloc
*/
void *
xrealloc(void *ptr, size_t size)
{
void *tmp;
if (ptr) {
tmp = realloc(ptr, size);
} else {
tmp = malloc(size);
}
if (tmp==NULL) NOTIFY(FATAL, "Memory exhausted; unable to continue.");
return tmp;
}
/**
* xmalloc: value-added malloc
*/
void *
xmalloc(size_t size)
{
void *tmp = malloc(size);
if(tmp==NULL) NOTIFY(FATAL, "Unable to allocate additional memory.");
return tmp;
}
/**
* xcalloc replaces calloc
*/
void *
xcalloc(size_t num, size_t size)
{
void *tmp = xmalloc(num * size);
memset(tmp, 0, (num * size));
return tmp;
}
/**
* free() wrapper:
* free it and NULL it to ensure we
* don't free it a second time...
*/
void
xfree(void *ptr)
{
if(ptr!=(void *)NULL){
free(ptr); ptr=(void *)NULL;
}
}