-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathALLOCS.C
154 lines (129 loc) · 4.09 KB
/
ALLOCS.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
;
; This is the module coded by Antti Karttunen and used by many programs.
; Following text applies to this module and to all other modules in this
; package unless otherwise noted:
;
; Copyright (C) 1991 Antti J. Karttunen
;
; 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 1, 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 (in file GPL.TXT) 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "stdio.h"
#include "mydefs.h"
static char *_allerrmsg =
"\nERROR in %salloc: received NULL when tried to allocate %d items of size %d\n";
static char *_moremsg =
"Implication: Space exhausted, compile the program with the large data option.\n";
PFI _addr_of_prstat=NULLFP;
#define call_prstat (_addr_of_prstat ? ((*_addr_of_prstat)(stderr)) : 7)
/* Total count of chars (= bytes) allocated with strsave: */
ULI _strsavecnt = 0L;
/* allocate n characters, and return pointer to first of them */
char *challoc(n)
register int n;
{
char *calloc();
register char *z;
if(z = ((char *) calloc(n,sizeof(char))))
{ return(z); }
else
{
fprintf(stderr,_allerrmsg,"ch",n,sizeof(char));
fputs(_moremsg,stderr);
call_prstat;
myexit(1);
}
}
/* K/R page 103 (little modified) -- (added 27-Sep-1988) */
char *strsave(s)
char *s;
{
char *calloc();
register char *p;
register int n;
n = strlen(s);
n++;
p = ((char *) calloc(n,sizeof(char)));
_strsavecnt += n;
if(!p)
{
fprintf(stderr,"\nInternal ERROR in strsave:\n");
fprintf(stderr,
"Received NULL when tried to allocate space for string: %s\n",s);
fprintf(stderr,
"(which is %d characters long). _strsavecnt: %ld. / 0x%lx\n",
--n,_strsavecnt,_strsavecnt);
fprintf(stderr,"%s",_moremsg);
call_prstat;
myexit(1);
}
strcpy(p,s);
return(p);
}
/* allocate n integers, and return pointer to first of them */
int *ialloc(n)
register int n;
{
char *calloc();
register int *z;
if(z = ((int *) calloc(n,sizeof(int)))) { return(z); }
else
{
fprintf(stderr,_allerrmsg,"i",n,sizeof(int));
fputs(_moremsg,stderr);
call_prstat;
myexit(1);
}
}
/* allocate n general pointers, and return pointer to first of them */
/* example:
* void **palloc();
* int **p1;
* p1 = (int **) palloc(6);
*/
void **palloc(n)
register int n;
{
char *calloc();
register void *z;
if(z = ((void **) calloc(n,sizeof(void *)))) { return(z); }
else
{
fprintf(stderr,_allerrmsg,"p",n,sizeof(void *));
fputs(_moremsg,stderr);
call_prstat;
myexit(1);
}
}
/* allocate n general items of size of variable size,
* and return pointer to first of them.
* This is like calloc but with check.
*/
void *galloc(n,size)
register int n;
int size;
{
char *calloc();
register void *z;
if(z = ((void *) calloc(n,size)))
{ return(z); }
else
{
fprintf(stderr,_allerrmsg,"g",n,size);
fputs(_moremsg,stderr);
call_prstat;
myexit(1);
}
}