aboutsummaryrefslogtreecommitdiffstats
path: root/cheney_c89.c
blob: e6aee3aa83ccfe86c50a81e51be618d53c4dbf84 (plain) (blame)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/* Copyright (C) 2024 Peter McGoron
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, 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 for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program. If not, see
 * <https://www.gnu.org/licenses/>.
*/

#include <stdlib.h>
#include <limits.h>
#include <assert.h>
#include <string.h>
#include "uns.h"
#include "internal/c89_relo.h"
#include "cheney_c89.h"

struct ctx {
	unsigned char *tospace;
	unsigned char *tospace_end;
	unsigned char *tospace_alloc;
};

const size_t uns_cheney_c89_ctx_size = sizeof(struct ctx);

static void zero_ctx(struct ctx *ctx)
{
	ctx->tospace = 0;
	ctx->tospace_end = 0;
	ctx->tospace_alloc = 0;
}

void uns_cheney_c89_deinit(struct uns_gc *gc)
{
	struct ctx *ctx = gc->ctx;

	free(ctx->tospace);
}

/* Allocate without doing bounds checking. This is common code for
 * collecting (when all values will fit into the tospace) and
 * allocation (where bounds checking is done beforehand).
 */
static void *raw_alloc(struct ctx *ctx, size_t len, int is_record)
{
	unsigned char *p;

	p = uns_c89_relo_init(ctx->tospace_alloc, len, is_record);
	ctx->tospace_alloc = p + len;

	return p;
}

/* Move an entire object pointed to by p from fromspace to tospace. */
static unsigned char *relocate(struct uns_gc *gc, unsigned char *p)
{
	size_t l;
	unsigned char *res;
	struct ctx *ctx = gc->ctx;

	if (!p)
		return NULL;
	res = uns_c89_relo_get_relo(p);
	if (res)
		return res;

	l = uns_c89_relo_get_len(p);
	res = raw_alloc(ctx, l, uns_c89_relo_is_record(p));
	memcpy(res, p, l);
	gc->after_collection += l + sizeof(struct uns_c89_relo_hdr);

	uns_c89_relo_set_relo(p, res);
	return res;
}

/* Scan each part of the record pointed to by "p" and relocate it. */
static void scan_record(struct uns_gc *gc, unsigned char *p)
{
	size_t l = uns_c89_relo_get_record_len(p);
	size_t i;
	void *newp;

	for (i = 0; i < l; i++) {
		newp = relocate(gc, uns_c89_relo_record_get(p, i));
		uns_c89_relo_record_set(p, i, newp);
	}
}

int uns_cheney_c89_collect(struct uns_gc *gc)
{
	/* Save fromspace */
	struct ctx *ctx = gc->ctx;
	unsigned char *fromspace = ctx->tospace;
	unsigned char *fromspace_lim = ctx->tospace_alloc;
	unsigned char *scanptr;

	size_t newlen = gc->next_alloc;
	struct uns_root_list *root;

	assert(gc->next_alloc >= fromspace_lim - fromspace);

	/* Bail out immediately if allocation fails. This preserves
	 * the objects as they were.
	 */
	ctx->tospace = malloc(newlen);
	if (!ctx->tospace) {
		ctx->tospace = fromspace;
		return 1;
	}

	/* Setup statistics */
	gc->before_collection = fromspace_lim - fromspace;
	gc->after_collection = 0;
	gc->collection_number += 1;

	ctx->tospace_end = ctx->tospace + newlen;
	ctx->tospace_alloc = ctx->tospace;

	/* Relocate roots */
	for (root = gc->roots; root; root = root->next)
		root->p = relocate(gc, root->p);

	scanptr = ctx->tospace;
	while (scanptr != ctx->tospace_alloc) {
		/* scanptr currently points to the header data that the mutator
		 * does not usually see.
		 */
		scanptr += sizeof(struct uns_c89_relo_hdr);
		if (uns_c89_relo_is_record(scanptr))
			scan_record(gc, scanptr);
		scanptr += uns_c89_relo_get_len(scanptr);
	}

	free(fromspace);
	if (gc->after_gc)
		gc->after_gc(gc);
	return 1;
}

static void *alloc(struct uns_gc *gc, size_t bytes, int is_record)
{
	struct ctx *ctx = gc->ctx;
	size_t total_bytes = bytes + sizeof(struct uns_c89_relo_hdr);

	/* Make sure to check for header space when allocating */
	if (ctx->tospace_end - ctx->tospace_alloc < total_bytes)
		uns_cheney_c89_collect(gc);
	if (ctx->tospace_end - ctx->tospace_alloc < total_bytes)
		gc->oom(gc);

	return raw_alloc(ctx, bytes, is_record);
}

void *uns_cheney_c89_alloc(struct uns_gc *gc, size_t bytes)
{
	return alloc(gc, bytes, 0);
}

void *uns_cheney_c89_alloc_record(struct uns_gc *gc, size_t records)
{
	size_t i;
	unsigned char *res = alloc(gc, records*sizeof(void *), 1);

	for (i = 0; i < records; i++)
		uns_c89_relo_record_set(res, i, NULL);

	return res;
}

int uns_cheney_c89_init(struct uns_gc *gc)
{
	struct ctx *ctx = gc->ctx;

	gc->deinit = uns_cheney_c89_deinit;
	gc->collect = uns_cheney_c89_collect;
	gc->alloc = uns_cheney_c89_alloc;
	gc->alloc_record = uns_cheney_c89_alloc_record;
	gc->len = uns_c89_relo_get_len;
	gc->record_set_ptr = uns_c89_relo_record_set;
	gc->record_get_ptr = uns_c89_relo_record_get;

	zero_ctx(ctx);
	ctx->tospace = malloc(gc->next_alloc);
	if (!ctx->tospace)
		return 0;

	ctx->tospace_alloc = ctx->tospace;
	ctx->tospace_end = ctx->tospace + gc->next_alloc;
	return 1;
}