The R Project SVN R

Rev

Rev 9184 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 9184 Rev 10347
1
/*
1
/*
2
 * GraphApp - Cross-Platform Graphics Programming Library.
2
 * GraphApp - Cross-Platform Graphics Programming Library.
3
 *
3
 *
4
 * File: array.c -- memory allocation functions.
4
 * File: array.c -- memory allocation functions.
5
 * Platform: Neutral  Version: 2.35  Date: 1998/03/04
5
 * Platform: Neutral  Version: 2.35  Date: 1998/03/04
6
 *
6
 *
7
 * Version: 2.30  Changes: Original version by Lachlan Patrick.
7
 * Version: 2.30  Changes: Original version by Lachlan Patrick.
8
 * Version: 2.35  Changes: Join and append improved by Jim McDonald.
8
 * Version: 2.35  Changes: Join and append improved by Jim McDonald.
9
 */
9
 */
10
 
10
 
11
/* Copyright (C) 1993-1998 Lachlan Patrick
11
/* Copyright (C) 1993-1998 Lachlan Patrick
12
 
12
 
13
   This file is part of GraphApp, a cross-platform C graphics library.
13
   This file is part of GraphApp, a cross-platform C graphics library.
14
 
14
 
15
   GraphApp is free software; you can redistribute it and/or modify it
15
   GraphApp is free software; you can redistribute it and/or modify it
16
   under the terms of the GNU Library General Public License.
16
   under the terms of the GNU Library General Public License.
17
   GraphApp is distributed in the hope that it will be useful, but
17
   GraphApp is distributed in the hope that it will be useful, but
18
   WITHOUT ANY WARRANTY.
18
   WITHOUT ANY WARRANTY.
19
 
19
 
20
   See the file COPYLIB.TXT for details.
20
   See the file COPYLIB.TXT for details.
21
*/
21
*/
22
 
22
 
23
/* Changes for R: use winmalloc etc not malloc (which does not free) */
-
 
24
 
-
 
25
#include <stdlib.h>
23
#include <stdlib.h>
26
#include "internal.h"
24
#include "internal.h"
27
 
25
 
28
#ifndef array
26
#ifndef array
29
 
27
 
30
#define create(type)  ( (type*) memalloc(sizeof(type)) )
28
#define create(type)  ( (type*) memalloc(sizeof(type)) )
31
#define array(n,type) ( (type*) memalloc(n*sizeof(type)) )
29
#define array(n,type) ( (type*) memalloc(n*sizeof(type)) )
32
#define len(a)        ( memlength((char*)(a))/sizeof((a)[0]) )
30
#define len(a)        ( memlength((char*)(a))/sizeof((a)[0]) )
33
#define element(a,i)  ( (((i)<len(a)) && ((i)>=0)) ? (a)[i] : 0 )
31
#define element(a,i)  ( (((i)<len(a)) && ((i)>=0)) ? (a)[i] : 0 )
34
#define append(a,e)   ( *(char**)&(a)=memexpand((char*)(a),sizeof((a)[0])), \
32
#define append(a,e)   ( *(char**)&(a)=memexpand((char*)(a),sizeof((a)[0])), \
35
				(a)[len(a)-1]=(e) )
33
				(a)[len(a)-1]=(e) )
36
#define shrink(a,ns)  ( *(char**)&(a)=memrealloc((char*)(a),(ns)) )
34
#define shrink(a,ns)  ( *(char**)&(a)=memrealloc((char*)(a),(ns)) )
37
#define join(a,b)     ( *(char**)&(a)=memjoin((char*)(a),(char*)(b)) )
35
#define join(a,b)     ( *(char**)&(a)=memjoin((char*)(a),(char*)(b)) )
38
#define discard(a)    ( memfree((char*)(a)), (a)=0 )
36
#define discard(a)    ( memfree((char*)(a)), (a)=0 )
39
 
37
 
40
char *	memalloc(long size);
38
char *	memalloc(long size);
41
char *	memrealloc(char *a, long new_size);
39
char *	memrealloc(char *a, long new_size);
42
void	memfree(char *a);
40
void	memfree(char *a);
43
long	memlength(char *a);
41
long	memlength(char *a);
44
char *	memexpand(char *a, long extra);
42
char *	memexpand(char *a, long extra);
45
char *	memjoin(char *a, char *b);
43
char *	memjoin(char *a, char *b);
46
 
44
 
47
#endif /* array defintions */
45
#endif /* array defintions */
48
 
46
 
49
#define TRACEAR(a)
47
#define TRACEAR(a)
50
 
48
 
51
char * memalloc(long size)
49
char * memalloc(long size)
52
{
50
{
53
	long *block;
51
	long *block;
54
	char *a;
52
	char *a;
55
	long i, datasize;
53
	long i, datasize;
56
        TRACEAR("alloc");
54
        TRACEAR("alloc");
57
	datasize = (((size + 4) >> 2) << 2);
55
	datasize = (((size + 4) >> 2) << 2);
58
	#ifdef COMPILER
56
	#ifdef COMPILER
59
	  #if (COMPILER <= 16)
57
	  #if (COMPILER <= 16)
60
		if ((sizeof(long)+datasize) >= (1<<16))
58
		if ((sizeof(long)+datasize) >= (1<<16))
61
			return NULL;
59
			return NULL;
62
	  #endif
60
	  #endif
63
	#endif
61
	#endif
64
	block = (long *) winmalloc(sizeof(long) + datasize);
62
	block = (long *) malloc(sizeof(long) + datasize);
65
	if (block == NULL)
63
	if (block == NULL)
66
		return NULL;
64
		return NULL;
67
	block[0] = size;
65
	block[0] = size;
68
	a = (char *) & block[1];
66
	a = (char *) & block[1];
69
	for (i=0; i<datasize; i++)
67
	for (i=0; i<datasize; i++)
70
		a[i] = '\0';
68
		a[i] = '\0';
71
	return a;
69
	return a;
72
}
70
}
73
 
71
 
74
char * memrealloc(char *a, long new_size)
72
char * memrealloc(char *a, long new_size)
75
{
73
{
76
	long *block;
74
	long *block;
77
	long i, size, oldsize, newsize;
75
	long i, size, oldsize, newsize;
78
        TRACEAR("realloc");
76
        TRACEAR("realloc");
79
	if (new_size <= 0) {
77
	if (new_size <= 0) {
80
		memfree(a);
78
		memfree(a);
81
		return NULL;
79
		return NULL;
82
	}
80
	}
83
 
81
 
84
	if (a == NULL) {
82
	if (a == NULL) {
85
		block = NULL;
83
		block = NULL;
86
		size = 0;
84
		size = 0;
87
	}
85
	}
88
	else {
86
	else {
89
		block = ((long*)a) - 1;
87
		block = ((long*)a) - 1;
90
		size = block[0];
88
		size = block[0];
91
	}
89
	}
92
 
90
 
93
	oldsize = size ? (((size + 4) >> 2) << 2) : 0;
91
	oldsize = size ? (((size + 4) >> 2) << 2) : 0;
94
	newsize = (((new_size + 4) >> 2) << 2);
92
	newsize = (((new_size + 4) >> 2) << 2);
95
 
93
 
96
	if ( newsize != oldsize ) {
94
	if ( newsize != oldsize ) {
97
		#ifdef COMPILER
95
		#ifdef COMPILER
98
		  #if (COMPILER <= 16)
96
		  #if (COMPILER <= 16)
99
			if ((sizeof(long)+newsize) >= (1<<16))
97
			if ((sizeof(long)+newsize) >= (1<<16))
100
				return NULL;
98
				return NULL;
101
		  #endif
99
		  #endif
102
		#endif
100
		#endif
103
		block = (long *) winrealloc(block, sizeof(long) + newsize);
101
		block = (long *) realloc(block, sizeof(long) + newsize);
104
		if (block == NULL)
102
		if (block == NULL)
105
			return NULL;
103
			return NULL;
106
		a = (char *) & block[1];
104
		a = (char *) & block[1];
107
		for (i=oldsize; i<newsize; i++)
105
		for (i=oldsize; i<newsize; i++)
108
			a[i] = '\0';
106
			a[i] = '\0';
109
	}
107
	}
110
 
108
 
111
	block[0] = new_size;
109
	block[0] = new_size;
112
	return a;
110
	return a;
113
}
111
}
114
 
112
 
115
long memlength(char *a)
113
long memlength(char *a)
116
{
114
{
117
	return (a) ? ((long*)(a)-1)[0] : 0;
115
	return (a) ? ((long*)(a)-1)[0] : 0;
118
}
116
}
119
 
117
 
120
void memfree(char *a)
118
void memfree(char *a)
121
{
119
{
122
	if (a) winfree((long*)(a)-1);
120
	if (a) free((long*)(a)-1);
123
}
121
}
124
 
122
 
125
char * memexpand(char *a, long extra)
123
char * memexpand(char *a, long extra)
126
{
124
{
127
	long *block;
125
	long *block;
128
	long i, size, oldsize, newsize;
126
	long i, size, oldsize, newsize;
129
        TRACEAR("exp");
127
        TRACEAR("exp");
130
	if (extra == 0)
128
	if (extra == 0)
131
		return a;
129
		return a;
132
 
130
 
133
	if (a == NULL) {
131
	if (a == NULL) {
134
		block = NULL;
132
		block = NULL;
135
		size = 0;
133
		size = 0;
136
	}
134
	}
137
	else {
135
	else {
138
		block = ((long*)a) - 1;
136
		block = ((long*)a) - 1;
139
		size = block[0];
137
		size = block[0];
140
	}
138
	}
141
 
139
 
142
	oldsize = size ? (((size + 4) >> 2) << 2) : 0;
140
	oldsize = size ? (((size + 4) >> 2) << 2) : 0;
143
	newsize = (((size + extra + 4) >> 2) << 2);
141
	newsize = (((size + extra + 4) >> 2) << 2);
144
 
142
 
145
	if ( newsize != oldsize ) {
143
	if ( newsize != oldsize ) {
146
		#ifdef COMPILER
144
		#ifdef COMPILER
147
		  #if (COMPILER <= 16)
145
		  #if (COMPILER <= 16)
148
			if ((sizeof(long)+newsize) >= (1<<16))
146
			if ((sizeof(long)+newsize) >= (1<<16))
149
				return NULL;
147
				return NULL;
150
		  #endif
148
		  #endif
151
		#endif
149
		#endif
152
		block = (long *) winrealloc(block, sizeof(long) + newsize);
150
		block = (long *) realloc(block, sizeof(long) + newsize);
153
		if (block == NULL)
151
		if (block == NULL)
154
			return NULL;
152
			return NULL;
155
		a = (char *) & block[1];
153
		a = (char *) & block[1];
156
		for (i=oldsize; i<newsize; i++)
154
		for (i=oldsize; i<newsize; i++)
157
			a[i] = '\0';
155
			a[i] = '\0';
158
	}
156
	}
159
 
157
 
160
	block[0] = size + extra;
158
	block[0] = size + extra;
161
	return a;
159
	return a;
162
}
160
}
163
 
161
 
164
char * memjoin(char *a, char *b)
162
char * memjoin(char *a, char *b)
165
{
163
{
166
	long i, size, extra;
164
	long i, size, extra;
167
 
165
 
168
	size = memlength(a);
166
	size = memlength(a);
169
	extra = memlength(b);
167
	extra = memlength(b);
170
	a = memexpand(a, extra);
168
	a = memexpand(a, extra);
171
	if (a) {
169
	if (a) {
172
		for (i=0; i<extra; i++)
170
		for (i=0; i<extra; i++)
173
			a[i+size] = b[i];
171
			a[i+size] = b[i];
174
	}
172
	}
175
	return a;
173
	return a;
176
}
174
}
177
 
175
 
178
 
176
 
179
 
177