File: malloc.c

package info (click to toggle)
fdclone 3.01b-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 4,772 kB
  • ctags: 8,674
  • sloc: ansic: 100,552; makefile: 4,497; sh: 1,480; sed: 224
file content (150 lines) | stat: -rw-r--r-- 2,154 bytes parent folder | download | duplicates (2)
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
140
141
142
143
144
145
146
147
148
149
150
/*
 *	malloc.c
 *
 *	alternative memoly allocation functions
 */

#include "headers.h"
#include "printf.h"
#include "malloc.h"
#include "sysemu.h"

#ifdef	FD
extern VOID error __P_((CONST char *));
#else
static VOID NEAR error __P_((CONST char *));
#endif


#ifndef	FD
static VOID NEAR error(s)
CONST char *s;
{
	VOID_C Xfprintf(Xstderr, "%s: memory allocation error", s);
	VOID_C fputnl(Xstderr);

	exit(2);
}
#endif	/* !FD */

char *Xmalloc(size)
ALLOC_T size;
{
	char *tmp;

	if (!size || !(tmp = (char *)malloc(size))) {
		error("malloc()");
#ifdef	FAKEUNINIT
		tmp = NULL;	/* fake for -Wuninitialized */
#endif
	}

	return(tmp);
}

char *Xrealloc(ptr, size)
VOID_P ptr;
ALLOC_T size;
{
	char *tmp;

	if (!size
	|| !(tmp = (ptr) ? (char *)realloc(ptr, size) : (char *)malloc(size)))
	{
		error("realloc()");
#ifdef	FAKEUNINIT
		tmp = NULL;	/* fake for -Wuninitialized */
#endif
	}

	return(tmp);
}

VOID Xfree(ptr)
VOID_P ptr;
{
	int duperrno;

	if (!ptr) return;
	duperrno = errno;
	free(ptr);
	errno = duperrno;
}

char *c_realloc(ptr, n, sizep)
char *ptr;
ALLOC_T n, *sizep;
{
	if (!ptr) {
		*sizep = BUFUNIT;
		return(Xmalloc(*sizep));
	}
	while (n + 1 >= *sizep) *sizep *= 2;

	return(Xrealloc(ptr, *sizep));
}

char *Xstrdup(s)
CONST char *s;
{
	char *tmp;
	int n;

	if (!s) return(NULL);
	n = strlen(s);
	if (!(tmp = (char *)malloc((ALLOC_T)n + 1))) error("malloc()");
	memcpy(tmp, s, n + 1);

	return(tmp);
}

char *Xstrndup(s, n)
CONST char *s;
int n;
{
	char *tmp;
	int i;

	if (!s) return(NULL);
	for (i = 0; i < n; i++) if (!s[i]) break;
	if (!(tmp = (char *)malloc((ALLOC_T)i + 1))) error("malloc()");
	memcpy(tmp, s, i);
	tmp[i] = '\0';

	return(tmp);
}

#ifndef	MINIMUMSHELL
int vasprintf2(sp, fmt, args)
char **sp;
CONST char *fmt;
va_list args;
{
	int n;

	n = Xvasprintf(sp, fmt, args);
	if (n < 0) error("malloc()");

	return(n);
}

# ifdef	USESTDARGH
/*VARARGS1*/
char *asprintf2(CONST char *fmt, ...)
# else
/*VARARGS1*/
char *asprintf2(fmt, va_alist)
CONST char *fmt;
va_dcl
# endif
{
	va_list args;
	char *cp;

	VA_START(args, fmt);
	VOID_C vasprintf2(&cp, fmt, args);
	va_end(args);

	return(cp);
}
#endif	/* !MINIMUMSHELL */