The R Project SVN R

Rev

Rev 45017 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 45017 Rev 87509
Line 18... Line 18...
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
/* R modification: mheader union (instead of simply long size) to force
-
 
24
   alignment of allocated data. With C11, one could use max_align_t. 
-
 
25
 
-
 
26
   Copyright (C) 2025  The R Core Team
-
 
27
*/
-
 
28
 
23
#include <stdlib.h>
29
#include <stdlib.h>
24
#include "internal.h"
30
#include "internal.h"
25
 
31
 
26
#ifndef array
32
#ifndef array
27
 
33
 
Line 44... Line 50...
44
 
50
 
45
#endif /* array defintions */
51
#endif /* array defintions */
46
 
52
 
47
#define TRACEAR(a)
53
#define TRACEAR(a)
48
 
54
 
-
 
55
typedef union {
-
 
56
    long size;
-
 
57
 
-
 
58
    void *dummy_ptr;
-
 
59
    void (*dummy_funptr)(void);
-
 
60
    long long dummy_ll;
-
 
61
    double dummy_dbl;
-
 
62
} mheader;
-
 
63
 
49
char * memalloc(long size)
64
char * memalloc(long size)
50
{
65
{
51
    long *block;
66
    char *block;
52
    char *a;
67
    char *a;
53
    long i, datasize;
68
    long i, datasize;
54
    TRACEAR("alloc");
69
    TRACEAR("alloc");
55
    datasize = (((size + 4) >> 2) << 2);
70
    datasize = (((size + 4) >> 2) << 2);
56
#ifdef COMPILER
71
#ifdef COMPILER
57
#if (COMPILER <= 16)
72
#if (COMPILER <= 16)
58
    if ((sizeof(long)+datasize) >= (1<<16))
73
    if ((sizeof(mheader)+datasize) >= (1<<16))
59
	return NULL;
74
	return NULL;
60
#endif
75
#endif
61
#endif
76
#endif
62
    block = (long *) malloc(sizeof(long) + datasize);
77
    block = (char *) malloc(sizeof(mheader) + datasize);
63
    if (block == NULL)
78
    if (block == NULL)
64
	return NULL;
79
	return NULL;
65
    block[0] = size;
80
    ((mheader *)block)->size = size;
66
    a = (char *) & block[1];
81
    a = block + sizeof(mheader);
67
    for (i=0; i<datasize; i++)
82
    for (i=0; i<datasize; i++)
68
	a[i] = '\0';
83
	a[i] = '\0';
69
    return a;
84
    return a;
70
}
85
}
71
 
86
 
72
char * memrealloc(char *a, long new_size)
87
char * memrealloc(char *a, long new_size)
73
{
88
{
74
    long *block;
89
    char *block;
75
    long i, size, oldsize, newsize;
90
    long i, size, oldsize, newsize;
76
    TRACEAR("realloc");
91
    TRACEAR("realloc");
77
    if (new_size <= 0) {
92
    if (new_size <= 0) {
78
	memfree(a);
93
	memfree(a);
79
	return NULL;
94
	return NULL;
Line 82... Line 97...
82
    if (a == NULL) {
97
    if (a == NULL) {
83
	block = NULL;
98
	block = NULL;
84
	size = 0;
99
	size = 0;
85
    }
100
    }
86
    else {
101
    else {
87
	block = ((long*)a) - 1;
102
	block = a - sizeof(mheader);
88
	size = block[0];
103
	size = ((mheader *)block)->size;
89
    }
104
    }
90
 
105
 
91
    oldsize = size ? (((size + 4) >> 2) << 2) : 0;
106
    oldsize = size ? (((size + 4) >> 2) << 2) : 0;
92
    newsize = (((new_size + 4) >> 2) << 2);
107
    newsize = (((new_size + 4) >> 2) << 2);
93
 
108
 
94
    if ( newsize != oldsize ) {
109
    if ( newsize != oldsize ) {
95
#ifdef COMPILER
110
#ifdef COMPILER
96
#if (COMPILER <= 16)
111
#if (COMPILER <= 16)
97
	if ((sizeof(long)+newsize) >= (1<<16))
112
	if ((sizeof(mheader)+newsize) >= (1<<16))
98
	    return NULL;
113
	    return NULL;
99
#endif
114
#endif
100
#endif
115
#endif
101
	block = (long *) realloc(block, sizeof(long) + newsize);
116
	block = (char *) realloc(block, sizeof(mheader) + newsize);
102
	if (block == NULL)
117
	if (block == NULL)
103
	    return NULL;
118
	    return NULL;
104
	a = (char *) & block[1];
119
	a = block + sizeof(mheader);
105
	for (i=oldsize; i<newsize; i++)
120
	for (i=oldsize; i<newsize; i++)
106
	    a[i] = '\0';
121
	    a[i] = '\0';
107
    }
122
    }
108
 
123
 
109
    block[0] = new_size;
124
    ((mheader *)block)->size = new_size;
110
    return a;
125
    return a;
111
}
126
}
112
 
127
 
113
long memlength(char *a)
128
long memlength(char *a)
114
{
129
{
115
    return (a) ? ((long*)(a)-1)[0] : 0;
130
    return (a) ? ((mheader *)(a - sizeof(mheader)))->size : 0;
116
}
131
}
117
 
132
 
118
void memfree(char *a)
133
void memfree(char *a)
119
{
134
{
120
    if (a) free((long*)(a)-1);
135
    if (a) free(a - sizeof(mheader));
121
}
136
}
122
 
137
 
123
char * memexpand(char *a, long extra)
138
char * memexpand(char *a, long extra)
124
{
139
{
125
    long *block;
140
    char *block;
126
    long i, size, oldsize, newsize;
141
    long i, size, oldsize, newsize;
127
    TRACEAR("exp");
142
    TRACEAR("exp");
128
    if (extra == 0)
143
    if (extra == 0)
129
	return a;
144
	return a;
130
 
145
 
131
    if (a == NULL) {
146
    if (a == NULL) {
132
	block = NULL;
147
	block = NULL;
133
	size = 0;
148
	size = 0;
134
    }
149
    }
135
    else {
150
    else {
136
	block = ((long*)a) - 1;
151
	block = a - sizeof(mheader);
137
	size = block[0];
152
	size = ((mheader *)block)->size;
138
    }
153
    }
139
 
154
 
140
    oldsize = size ? (((size + 4) >> 2) << 2) : 0;
155
    oldsize = size ? (((size + 4) >> 2) << 2) : 0;
141
    newsize = (((size + extra + 4) >> 2) << 2);
156
    newsize = (((size + extra + 4) >> 2) << 2);
142
 
157
 
143
    if ( newsize != oldsize ) {
158
    if ( newsize != oldsize ) {
144
#ifdef COMPILER
159
#ifdef COMPILER
145
#if (COMPILER <= 16)
160
#if (COMPILER <= 16)
146
	if ((sizeof(long)+newsize) >= (1<<16))
161
	if ((sizeof(mheader)+newsize) >= (1<<16))
147
	    return NULL;
162
	    return NULL;
148
#endif
163
#endif
149
#endif
164
#endif
150
	block = (long *) realloc(block, sizeof(long) + newsize);
165
	block = (char *) realloc(block, sizeof(mheader) + newsize);
151
	if (block == NULL)
166
	if (block == NULL)
152
	    return NULL;
167
	    return NULL;
153
	a = (char *) & block[1];
168
	a = block + sizeof(mheader);
154
	for (i=oldsize; i<newsize; i++)
169
	for (i=oldsize; i<newsize; i++)
155
	    a[i] = '\0';
170
	    a[i] = '\0';
156
    }
171
    }
157
 
172
 
158
    block[0] = size + extra;
173
    ((mheader *)block)->size = size + extra;
159
    return a;
174
    return a;
160
}
175
}
161
 
176
 
162
char * memjoin(char *a, char *b)
177
char * memjoin(char *a, char *b)
163
{
178
{
Line 170... Line 185...
170
	for (i=0; i<extra; i++)
185
	for (i=0; i<extra; i++)
171
	    a[i+size] = b[i];
186
	    a[i+size] = b[i];
172
    }
187
    }
173
    return a;
188
    return a;
174
}
189
}
-
 
190