parsav  Diff

Differences From Artifact [a0c3213659]:

To Artifact [de24aef2de]:


173
174
175
176
177
178
179
180






























































181
	v.metamethods.__apply = terra(self: &v, idx: intptr): &ty -- no index??
		if self.sz > idx then
			return self.storage.ptr + idx
		else lib.bail('vector overrun!') end
	end
	return v 
end)































































return m








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

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
	v.metamethods.__apply = terra(self: &v, idx: intptr): &ty -- no index??
		if self.sz > idx then
			return self.storage.ptr + idx
		else lib.bail('vector overrun!') end
	end
	return v 
end)

struct m.pool {
 -- implements growable memory pools. EVERY THREAD MUST HAVE ITS OWN
	storage: &opaque
	cursor: &opaque
	sz: intptr
}

terra m.pool:cue(sz: intptr)
	if self.storage == nil then
		self.storage = m.heapa_raw(sz)
		self.cursor = self.storage
		self.sz = sz
	else
		if self.sz >= sz then return self end
		var ofs = [&uint8](self.cursor) - [&uint8](self.storage)
		self.storage = m.heapr_raw(self.storage, sz)
		self.cursor = [&opaque]([&uint8](self.storage) + ofs)
		self.sz = sz
	end
	return self
end

terra m.pool:init(sz: intptr)
	self.storage = nil
	self:cue(sz)
	return self
end

terra m.pool:free()
	m.heapf(self.storage)
	self.storage = nil
	self.cursor = nil
	self.sz = 0
end

terra m.pool:clear()
	self.cursor = self.storage
	return self
end

terra m.pool:alloc_bytes(sz: intptr): &opaque
	var space = self.sz - ([&uint8](self.cursor) - [&uint8](self.storage))
	if space < sz then self:cue(space + sz + 256) end
	var ptr = self.cursor
	self.cursor = [&opaque]([&uint8](self.cursor) + sz)
	return ptr
end

m.pool.methods.alloc = macro(function(self,ty,sz)
	return `[ty](self:alloc_bytes(sizeof(ty) * sz))
end)

terra m.pool:frame() -- stack-style linear mgmt
	return self.cursor
end

terra m.pool:reset(frame: &opaque)
	self.cursor = frame
	return self
end


return m