File: PinnedSet.cpp

package info (click to toggle)
storm-lang 0.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 52,004 kB
  • sloc: ansic: 261,462; cpp: 140,405; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (282 lines) | stat: -rw-r--r-- 7,063 bytes parent folder | download | duplicates (2)
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#include "stdafx.h"
#include "PinnedSet.h"
#include "Compiler/Engine.h"

namespace storm {


	PinnedSet::PinnedSet() : data(null), root(null) {}

	PinnedSet::PinnedSet(const PinnedSet &o) : data(null), root(null) {
		if (o.data) {
			reserve(o.data->count);

			memcpy(data, o.data, dataSize(o.data->count));
		}
	}

	PinnedSet::~PinnedSet() {
		clear();
	}

	void PinnedSet::deepCopy(CloneEnv *) {}

	void PinnedSet::clear() {
		if (root) {
			engine().gc.destroyRoot(root);
			root = null;
		}

		if (data) {
			free(data);
			data = null;
		}
	}

	void PinnedSet::toS(StrBuf *to) const {
		if (!data) {
			*to << S("{}");
			return;
		}

		sort();

		*to << S("{");
		bool first = true;
		for (size_t i = 0; i < data->count; i++) {
			if (!data->v[i])
				continue;

			if (!first)
				*to << S(", ");
			first = false;

			*to << S("0x") << hex(data->v[i]);
		}
		*to << S("}");
	}

	void PinnedSet::invalidate() {
		if (data) {
			data->sorted = false;
		}
	}

	struct PtrCompare {
		bool operator() (const void *a, const void *b) const {
			return size_t(a) < size_t(b);
		}
	};

	void PinnedSet::put(void *ptr) {
		if (!ptr)
			return;

		// Don't allow adding a pointer to the pinned set itself! That would create a cycle that is
		// never collected.
		if (size_t(ptr) >= size_t(this) && size_t(ptr) < size_t(this) + sizeof(*this))
			return;

		if (!data)
			reserve(16);

		// If we're full, try sorting the array first. There might be duplicates we can remove.
		if (data->filled >= data->count && !data->sorted)
			sort();

		// Final fallback, resize.
		if (data->filled >= data->count)
			reserve(data->count * 2);

		// If the array is sorted, we can check if the address is already present.
		if (data->sorted) {
			void **begin = data->v;
			void **end = data->v + data->count;
			void **found = std::lower_bound(begin, end, ptr, PtrCompare());

			// Found it, we can return now!
			if (found != end && *found == ptr)
				return;
		}


		// Note: We place non-null pointers at the end. Since 0 is the lowest number we have in the
		// array, it is natural that it appears in the beginning of the array as it is sorted.
		data->filled++;
		data->v[data->count - data->filled] = ptr;
		data->sorted = false;
	}

	static size_t objSize(const void *object) {
		const GcType *type = Gc::typeOf(object);
		size_t size = type->stride;
		if (type->kind == GcType::tArray) {
			size *= ((const GcArray<Byte> *)object)->count;
			// size += OFFSET_OF(GcArray<Byte>, v);
		} else if (type->kind == GcType::tWeakArray) {
			size *= ((const GcWeakArray<void *> *)object)->count();
			// size += OFFSET_OF(GcArray<Byte>, v);
		}
		return size;
	}

	static size_t arrayHeader(const void *object) {
		const GcType *type = Gc::typeOf(object);
		if (type->kind == GcType::tArray) {
			return OFFSET_OF(GcArray<Byte>, v);
		} else if (type->kind == GcType::tWeakArray) {
			return OFFSET_OF(GcArray<Byte>, v);
		} else {
			return 0;
		}
	}

	Bool PinnedSet::has(const void *query) {
		return has(query, 0, Nat(objSize(query)));
	}

	Bool PinnedSet::has(const void *query, Nat start) {
		return has(query, start, 1);
	}

	Bool PinnedSet::has(const void *query, Nat start, Nat size) {
		if (!query)
			return false;
		if (!data)
			return false;

		sort();

		query = (const char *)query + start + arrayHeader(query);

		void **begin = data->v;
		void **end = data->v + data->count;

		void **found = std::lower_bound(begin, end, query, PtrCompare());

		if (found == end)
			return false;

		// Out of range?
		if (size_t(*found) >= size_t(query) + size)
			return false;

		return true;
	}

	Array<Nat> *PinnedSet::offsets(const void *query) {
		if (!query || !data)
			return new (this) Array<Nat>();

		sort();

		void **begin = data->v;
		void **end = data->v + data->count;

		size_t header = arrayHeader(query);
		size_t size = objSize(query) + header;
		Array<Nat> *result = new (this) Array<Nat>();

		// Loop until we're out of range and add all offsets.
		for (void **found = std::lower_bound(begin, end, query, PtrCompare());
			 found != end && size_t(*found) < size_t(query) + size;
			 found++) {

			size_t offset = size_t(*found) - size_t(query);
			if (offset >= header)
				*result << Nat(offset - header);
		}

		return result;
	}

	void PinnedSet::reserve(size_t n) {
		Data *newData = (Data *)malloc(dataSize(n));
		Gc::Root *newRoot = engine().gc.createRoot(newData, dataSize(n) / sizeof(void *), true);

		memset(newData, 0, dataSize(n));
		newData->count = n;

		if (data) {
			newData->filled = data->filled;
			for (size_t i = 0; i < min(data->count, n); i++) {
				newData->v[newData->count - i - 1] = data->v[data->count - i - 1];
			}
		}

		std::swap(newData, data);
		std::swap(newRoot, root);

		if (newRoot)
			engine().gc.destroyRoot(newRoot);
		if (newData)
			free(newData);
	}

	void PinnedSet::sort() const {
		if (data->sorted)
			return;

		void **begin = data->v;
		void **end = data->v + data->count;

		std::sort(begin, end, PtrCompare());

		// Move duplicates to the beginning of the array, then we can zero them. We stop when we see a null.
		void **to = end - 1;
		for (void **at = end - 1; at != begin && *(at - 1) != null; at--) {
			if (*at != *(at - 1)) {
				*--to = *(at - 1);
			}
		}

		// Remember new (possibly smaller) size.
		data->filled = end - to;

		// Zero remaining
		while (to != begin)
			*--to = null;

		data->sorted = true;
	}

	size_t PinnedSet::dataSize(size_t n) {
		return sizeof(Data) + sizeof(void *) * (max(n, size_t(1)) - 1);
	}

	void addPinned(Type *rawPtr) {
		Engine &e = rawPtr->engine;
		Type *to = StormInfo<PinnedSet>::type(e);

		Value b(StormInfo<Bool>::type(e));

		Array<Value> *raw = new (e) Array<Value>(2, Value(to));
		raw->at(1) = Value(rawPtr);
		to->add(nativeFunction(e, b, S("has"), raw, address<Bool (CODECALL PinnedSet::*)(const void *)>(&PinnedSet::has)));
		to->add(nativeFunction(e, Value(StormInfo<Array<Nat>>::type(e)), S("offsets"), raw, address(&PinnedSet::offsets)));

		Array<Value> *rawNat = new (e) Array<Value>(3, Value(to));
		rawNat->at(1) = Value(rawPtr);
		rawNat->at(2) = Value(StormInfo<Nat>::type(e));
		to->add(nativeFunction(e, b, S("has"), rawNat, address<Bool (CODECALL PinnedSet::*)(const void *, Nat)>(&PinnedSet::has)));

		rawNat = new (e) Array<Value>(4, Value(to));
		rawNat->at(1) = Value(rawPtr);
		rawNat->at(2) = Value(StormInfo<Nat>::type(e));
		rawNat->at(3) = Value(StormInfo<Nat>::type(e));
		to->add(nativeFunction(e, b, S("has"), rawNat, address<Bool (CODECALL PinnedSet::*)(const void *, Nat, Nat)>(&PinnedSet::has)));


		Type *unknown = as<Type>(e.nameSet(parseSimpleName(e, S("core.lang.unknown.PTR_NOGC"))));
		if (!unknown)
			throw new (e) InternalError(S("Unable to find the type PTR_NOGC"));

		Array<Value> *un = new (e) Array<Value>(2, Value(to));
		un->at(1) = Value(unknown);
		to->add(nativeFunction(e, Value(), S("put"), un, address(&PinnedSet::put)));

		// This version is more restricted, but easier to use from Storm.
		to->add(nativeFunction(e, Value(), S("put"), raw, address(&PinnedSet::put)));
	}

}