parsav  Diff

Differences From Artifact [de24aef2de]:

To Artifact [7e2b478eaf]:


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
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







>









|
|
|
<





>
>
>
>
>
|
<



|
>
|
>
>



>



>






>
>
>
|





>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>







>
|
>
>
>





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
283
284
285
286
287
288
289
end)

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

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 oldblock = @self
		self:init(sz)
		@self.debris = oldblock

	end
	return self
end

terra m.pool:init(sz: intptr)
	var b = m.heapa_raw(sz + sizeof(m.pool))
	self.storage = [&uint8](b) + sizeof(m.pool)
	self.cursor = self.storage
	self.sz = sz
	self.debris = [&m.pool](b)
	self.debris.storage = nil

	return self
end

terra m.pool:free(): {}
lib.io.fmt('DRAINING POOL %p\n',self.storage)
	if self.storage == nil then return end
	if self.debris.storage ~= nil then self.debris:free() end
	m.heapf(self.debris) -- storage + debris field allocated in one block
	self.storage = nil
	self.cursor = nil
	self.sz = 0
	self.debris = nil
end

terra m.pool:clear()
	if self.debris.storage ~= nil then self.debris:free() end
	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))
lib.io.fmt('%p / %p @ allocating %llu bytes in %llu of space\n',self.storage,self.cursor,sz,space)
	if space < sz then
lib.dbg('reserving more space')
		self:cue(space + sz + 256) end
	var ptr = self.cursor
	self.cursor = [&opaque]([&uint8](self.cursor) + sz)
	return ptr
end

terra m.pool:realloc_bytes(oldptr: &opaque, oldsz: intptr, newsz: intptr): &opaque
	var space = self.sz - ([&uint8](self.cursor) - [&uint8](self.storage))
	var cur = [&uint8](self.cursor)
	if cur - [&uint8](oldptr) == oldsz and newsz - oldsz < space then
		lib.dbg('moving pool cursor')
		cur = cur + (newsz - oldsz)
		self.cursor = [&opaque](cur)
		return oldptr
	else
		lib.dbg('copying pool object')
		var new = self:alloc_bytes(newsz)
		m.cpy(new, oldptr, oldsz)
		return new
	end
end

m.pool.methods.alloc = macro(function(self,typ,sz)
 	local ty = typ:astype()
	return `[m.ptr(ty)] {
		ptr = [&ty](self:alloc_bytes(sizeof([ty]) * [sz]));
		ct = [sz];
	}
end)

m.pool.methods.realloc = macro(function(self,ptr,oldsz,newsz)
	local ty = self.tree.type.type
	return `[m.ptr(ty)] {
		ptr = [&ty](self:realloc_bytes(ptr,
			sizeof(ty) * oldsz,
			sizeof(ty) * newsz));
		ct = sz;
	}
end)

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

terra m.pool:reset(frame: &opaque)
	if frame >= self.storage and frame <= self.cursor then
		self.cursor = frame
	else -- trying to rewind into a previous block! not possible
		self.cursor = self.storage
	end
	return self
end


return m