summaryrefslogtreecommitdiffstats
path: root/Mac/mwerks/mwerksglue.c
blob: f7ba7aae94a55c04157e8d34b96bb8b043e8a099 (plain)
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
155
156
157
158
159
160
161
162
163
164
165
166
/*
** Glue code for MetroWerks CodeWarrior, which misses
** unix-like routines for file-access.
*/

#ifdef __MWERKS__
#include <Types.h>
#include <Files.h>
#include <Strings.h>

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

/* #define DBGMALLOC /**/

#ifdef DBGMALLOC
#define NMALLOC 50000
static long m_index;
static long m_addrs[NMALLOC];
static long m_sizes[NMALLOC];
static long m_lastaddr;

#define SSLOP 2
#define SLOP (SSLOP+0)

void
m_abort() {
	printf("ABORT\n");
	exit(1);
}

void *
checkrv(ptr, size)
	void *ptr;
	int size;
{
	long b = (long)ptr, e = (long)ptr + size+SSLOP;
	int i;
	
	if ( m_index >= NMALLOC ) {
		printf("too many mallocs\n");
		m_abort();
	}
	m_lastaddr = (long)ptr;
	for(i=0; i<m_index; i++) {
		if ( m_addrs[i] > 0 && b < m_addrs[i]+m_sizes[i] && e > m_addrs[i] ) {
			printf("overlapping block with %d\n", i);
			m_abort();
		}
	}
	m_sizes[m_index] = size;
	m_addrs[m_index++] = (long)ptr;
	*(short *)ptr = m_index-1;
	return (void *)((char *)ptr+SSLOP);
	
}

void *
checkfree(ptr)
	void *ptr;
{
	int i;
	
	if ( ptr == 0 ) {
		printf("free null\n");
		m_abort();
	}
	m_lastaddr = (long)ptr;
	for(i=0; i<m_index; i++) {
		if ( m_addrs[i] == (long)ptr-SSLOP ) {
			m_addrs[i] = -m_addrs[i];
			ptr = (void *)((char *)ptr-SSLOP);
			if ( *(short *)ptr != i ) {
				int saved_i = i, saved_ptr = *(short *)ptr;
				
				printf("Wrong starter value\n");
				m_abort();
			}
			return ptr;
		}
	}
	printf("free unknown\n");
	m_abort();
	return 0;
}

void *
m_malloc(size)
{
	void *ptr;
	
	ptr = malloc(size+SLOP);
	if ( !ptr ) {
		printf("malloc failure\n");
		m_abort();
	}
	return checkrv(ptr, size);
}

void *
m_realloc(optr, size)
	void *optr;
{
	void *ptr;
	
	ptr = checkfree(ptr);
	ptr = realloc(ptr, size+SLOP);
	if ( !ptr ) {
		printf("realloc failure\n");
		m_abort();
	}
	return checkrv(ptr, size);
}

void *
m_calloc(size, nelem)
{
	void *ptr;
	
	ptr = calloc(1, nelem*size+SLOP);
	return checkrv(ptr, nelem*size);
}

void
m_free(ptr)
	void *ptr;
{
	
	ptr = checkfree(ptr);
	free(ptr);
}
#endif /* DBGMALLOC */

#ifdef CW4
int
fileno(fp)
	FILE *fp;
{
	if (fp==stdin) return 0;
	else if (fp==stdout) return 1;
	else if (fp==stderr) return 2;
	else return 3;
}

int
isatty(fd)
	int fd;
{
	return (fd >= 0 && fd <= 2);
}

int
unlink(old)
	char *old;
{
	OSErr err;
	
	if ((err=FSDelete((ConstStr255Param)Pstring(old), 0)) == noErr)
		return 0;
	errno= err;
	return -1;
}
#endif /* CW4 */

#endif /* __MWERKS__ */