summaryrefslogtreecommitdiff
path: root/indra/lscript/lscript_execute/lscript_heapruntime.cpp
blob: 749857753d437862286dcca14d2a41d5ba21f9eb (plain)
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/** 
 * @file lscript_heapruntime.cpp
 * @brief classes to manage script heap at runtime
 *
 * $LicenseInfo:firstyear=2002&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2010, Linden Research, Inc.
 * 
 * This library 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;
 * version 2.1 of the License only.
 * 
 * This library 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
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */

#if 0

#include "linden_common.h"

#include "lscript_heapruntime.h"
#include "lscript_execute.h"


/*
	String Heap Format
	Byte		Description
	0x0 - 0xnn	Single byte string including null terminator

	List Heap Format
	Byte		Description
	0x0			Next Entry Type
					0: End of list
					1: Integer
					2: Floating point
					3: String
					4: Vector
					5: Quaternion
					6: List
	0x1 - 0x4	Integer, Floating Point, String Address, List Address
	or	
	0x1 - 0xd	Vector
	or	
	0x1 - 0x11	Quaternion
	. . .	

	Heap Block Format
	Byte		Description
	0x0 - 0x3	Offset to Next Block
	0x4 - 0x7	Object Reference Count
	0x8 		Block Type
					0: Empty
					3: String
					6: List
	0x9 - 0xM	Object Data

	Heap Management

	Adding Data

	1)	Set last empty spot to zero.
	2)	Go to start of the heap (HR).
	3)	Get next 4 bytes of offset.
	4)	If zero, we've reached the end of used memory.  If empty spot is zero go to step 9.  Otherwise set base offset to 0 and go to step 9.
	5)	Skip 4 bytes.
	6)	Get next 1 byte of entry type.
	7)	If zero, this spot is empty.  If empty spot is zero, set empty spot to this address and go to step 9.  Otherwise, coalesce with last empty spot and then go to step 9.
	8)	Skip forward by offset and go to step 3.
	9)	If the spot is empty, check to see if the size needed == offset - 9.
	10)	 If it does, let's drop our data into this spot.  Set reference count to 1.  Set entry type appropriately and copy the data in.  
	11)	 If size needed < offset - 9 then we can stick in data and add in an empty block.
	12)	 Otherwise, we need to keep looking.  Go to step 3.

	Increasing reference counts
		
	Decreasing reference counts
	1)	Set entry type to 0.
	2)	If offset is non-zero and the next entry is empty, coalesce.  Go to step 2.

	What increases reference count:
		Initial creation sets reference count to 1.
		Storing the reference increases reference count by 1.
		Pushing the reference increases reference count by 1.
		Duplicating the reference increases reference count by 1.

	What decreases the reference count:
		Popping the reference decreases reference count by 1.
 */


LLScriptHeapRunTime::LLScriptHeapRunTime()
: mLastEmpty(0), mBuffer(NULL), mCurrentPosition(0), mStackPointer(0), mHeapRegister(0), mbPrint(FALSE)
{
}

LLScriptHeapRunTime::~LLScriptHeapRunTime()
{
}

S32 LLScriptHeapRunTime::addData(char *string)
{
	if (!mBuffer)
		return 0;

	S32 size = strlen(string) + 1;
	S32 block_offset = findOpenBlock(size + HEAP_BLOCK_HEADER_SIZE);

	if (mCurrentPosition)
	{
		S32 offset = mCurrentPosition;
		if (offset + block_offset + HEAP_BLOCK_HEADER_SIZE + LSCRIPTDataSize[LST_INTEGER] >= mStackPointer)
		{
			set_fault(mBuffer, LSRF_STACK_HEAP_COLLISION);
			return 0;
		}
		// cool, we've found a spot!
		// set offset
		integer2bytestream(mBuffer, offset, block_offset);
		// set reference count
		integer2bytestream(mBuffer, offset, 1);
		// set type
		*(mBuffer + offset++) = LSCRIPTTypeByte[LST_STRING];
		// plug in data
		char2bytestream(mBuffer, offset, string);
		if (mbPrint)
			printf("0x%X created ref count %d\n", mCurrentPosition - mHeapRegister, 1);

		// now, zero out next offset to prevent "trouble"
	//	offset = mCurrentPosition + size + HEAP_BLOCK_HEADER_SIZE;
	//	integer2bytestream(mBuffer, offset, 0);
	}
	return mCurrentPosition;
}

S32 LLScriptHeapRunTime::addData(U8 *list)
{
	if (!mBuffer)
		return 0;
	return 0;
}

S32 LLScriptHeapRunTime::catStrings(S32 address1, S32 address2)
{
	if (!mBuffer)
		return 0;

	S32 dataaddress1 = address1 + 2*LSCRIPTDataSize[LST_INTEGER] + 1;
	S32 dataaddress2 = address2 + 2*LSCRIPTDataSize[LST_INTEGER] + 1;

	S32 toffset1 = dataaddress1;
	safe_heap_bytestream_count_char(mBuffer, toffset1);

	S32 toffset2 = dataaddress2;
	safe_heap_bytestream_count_char(mBuffer, toffset2);

	// calculate new string size
	S32 size1 = toffset1 - dataaddress1;
	S32 size2 = toffset2 - dataaddress2;
	S32 newbuffer = size1 + size2 - 1;

	char *temp = new char[newbuffer];

	// get the strings
	bytestream2char(temp, mBuffer, dataaddress1);
	bytestream2char(temp + size1 - 1, mBuffer, dataaddress2);

	decreaseRefCount(address1);
	decreaseRefCount(address2);

	S32 retaddress = addData(temp);

	return retaddress;
}

S32 LLScriptHeapRunTime::cmpStrings(S32 address1, S32 address2)
{
	if (!mBuffer)
		return 0;

	S32 dataaddress1 = address1 + 2*LSCRIPTDataSize[LST_INTEGER] + 1;
	S32 dataaddress2 = address2 + 2*LSCRIPTDataSize[LST_INTEGER] + 1;

	S32 toffset1 = dataaddress1;
	safe_heap_bytestream_count_char(mBuffer, toffset1);

	S32 toffset2 = dataaddress2;
	safe_heap_bytestream_count_char(mBuffer, toffset2);

	// calculate new string size
	S32 size1 = toffset1 - dataaddress1;
	S32 size2 = toffset2 - dataaddress2;

	if (size1 != size2)
	{
		return llmin(size1, size2);
	}
	else
	{
		return strncmp((char *)(mBuffer + dataaddress1), (char *)(mBuffer + dataaddress2), size1);
	}
}

void LLScriptHeapRunTime::removeData(S32 address)
{
	if (!mBuffer)
		return;

	S32 toffset = address;
	// read past offset (relying on function side effect
	bytestream2integer(mBuffer, toffset);

	// make sure that reference count is 0
	integer2bytestream(mBuffer, toffset, 0);
	// show the block as empty
	*(mBuffer + toffset) = 0;

	// now, clean up the heap
	S32 clean = mHeapRegister;
	S32 tclean;
	S32 clean_offset;

	S32 nclean;
	S32 tnclean;
	S32 next_offset;

	U8  type;
	U8  ntype;

	for(;;)
	{
		tclean = clean;
		clean_offset = bytestream2integer(mBuffer, tclean);
		// is this block, empty?
		tclean += LSCRIPTDataSize[LST_INTEGER];
		type = *(mBuffer + tclean);
		
		if (!clean_offset)
		{
			if (!type)
			{
				// we're done! if our block is empty, we can pull in the HP and zero out our offset
				set_register(mBuffer, LREG_HP, clean);
			}
			return;
		}


		if (!type)
		{
			// if we're empty, try to coalesce with the next one
			nclean = clean + clean_offset;
			tnclean = nclean;
			next_offset = bytestream2integer(mBuffer, tnclean);
			tnclean += LSCRIPTDataSize[LST_INTEGER];
			ntype = *(mBuffer + tnclean);

			if (!next_offset)
			{
				// we're done! if our block is empty, we can pull in the HP and zero out our offset
				tclean = clean;
				integer2bytestream(mBuffer, tclean, 0);
				set_register(mBuffer, LREG_HP, clean);
				return;
			}

			if (!ntype)
			{
				// hooray!  we can coalesce
				tclean = clean;
				integer2bytestream(mBuffer, tclean, clean_offset + next_offset);
				// don't skip forward so that we can keep coalescing on next pass through the loop
			}
			else
			{
				clean += clean_offset;
			}
		}
		else
		{
			// if not, move on to the next block
			clean += clean_offset;
		}
	}
}	

void LLScriptHeapRunTime::coalesce(S32 address1, S32 address2)
{
	// we need to bump the base offset by the second block's
	S32 toffset = address1;
	S32 offset1 = bytestream2integer(mBuffer, toffset);
	offset1 += bytestream2integer(mBuffer, address2);

	integer2bytestream(mBuffer, address1, offset1);
}

void LLScriptHeapRunTime::split(S32 address1, S32 size)
{
	S32 toffset = address1;
	S32 oldoffset = bytestream2integer(mBuffer, toffset);

	// add new offset and zero out reference count and block used
	S32 newoffset = oldoffset - size;
	S32 newblockpos = address1 + size;

	// set new offset
	integer2bytestream(mBuffer, newblockpos, newoffset);
	// zero out reference count
	integer2bytestream(mBuffer, newblockpos, 0);
	// mark as empty
	*(mBuffer + newblockpos) = 0;

	// now, change the offset of the original block
	integer2bytestream(mBuffer, address1, size + HEAP_BLOCK_HEADER_SIZE);
}

/* 

	For reference count changes, strings are easy.  For lists, we'll need to go through the lists reducing
	the reference counts for any included strings and lists

 */

void LLScriptHeapRunTime::increaseRefCount(S32 address)
{
	if (!mBuffer)
		return;

	if (!address)
	{
		// unused temp string entry
		return;
	}

	// get current reference count
	S32 toffset = address + 4;
	S32 count = bytestream2integer(mBuffer, toffset);

	count++;

	if (mbPrint)
		printf("0x%X inc ref count %d\n", address - mHeapRegister, count);

	// see which type it is
	U8 type = *(mBuffer + toffset);

	if (type == LSCRIPTTypeByte[LST_STRING])
	{
		toffset = address + 4;
		integer2bytestream(mBuffer, toffset, count);
	}
	// TO DO: put list stuff here!
	else
	{
		set_fault(mBuffer, LSRF_HEAP_ERROR);
	}
}

void LLScriptHeapRunTime::decreaseRefCount(S32 address)
{
	if (!mBuffer)
		return;

	if (!address)
	{
		// unused temp string entry
		return;
	}

	// get offset
	S32 toffset = address;
	// read past offset (rely on function side effect)
	bytestream2integer(mBuffer, toffset);

	// get current reference count
	S32 count = bytestream2integer(mBuffer, toffset);

	// see which type it is
	U8 type = *(mBuffer + toffset);

	if (type == LSCRIPTTypeByte[LST_STRING])
	{
		count--;

		if (mbPrint)
			printf("0x%X dec ref count %d\n", address - mHeapRegister, count);

		toffset = address + 4;
		integer2bytestream(mBuffer, toffset, count);
		if (!count)
		{
			// we can blow this one away
			removeData(address);
		}
	}
	// TO DO: put list stuff here!
	else
	{
		set_fault(mBuffer, LSRF_HEAP_ERROR);
	}
}

// if we're going to assign a variable, we need to decrement the reference count of what we were pointing at (if anything)
void LLScriptHeapRunTime::releaseLocal(S32 address)
{
	S32 hr = get_register(mBuffer, LREG_HR);
	address = lscript_local_get(mBuffer, address);
	if (  (address >= hr)
		&&(address < hr + get_register(mBuffer, LREG_HP)))
	{
		decreaseRefCount(address);
	}
}

void LLScriptHeapRunTime::releaseGlobal(S32 address)
{
	// NOTA BENE: Global strings are referenced relative to the HR while local strings aren't
	S32 hr = get_register(mBuffer, LREG_HR);
	address = lscript_global_get(mBuffer, address) + hr;
	if (  (address >= hr)
		&&(address <  hr + get_register(mBuffer, LREG_HP)))
	{
		decreaseRefCount(address);
	}
}


// we know the following function has "unreachable code"
// don't remind us every friggin' time we compile. . . 

#if defined(_MSC_VER)
# pragma warning(disable: 4702) // unreachable code
#endif

S32 LLScriptHeapRunTime::findOpenBlock(S32 size)
{
	S32 offset;
	S32 toffset;
	U8 blocktype;

	while(1)
	{
		if (mCurrentPosition + size >= mStackPointer)
		{
			set_fault(mBuffer, LSRF_STACK_HEAP_COLLISION);
			mCurrentPosition = 0;
		}

		toffset = mCurrentPosition;
		offset = bytestream2integer(mBuffer, toffset);
		if (!offset)
		{
			// we've reached the end of Heap, return this location if we'll fit
			// do we need to coalesce with last empty space?
			if (mLastEmpty)
			{
				// ok, that everything from mLastEmpty to us is empty, so we don't need a block
				// zero out the last empty's offset and return it
				mCurrentPosition = mLastEmpty;
				integer2bytestream(mBuffer, mLastEmpty, 0);
				mLastEmpty = 0;
			}
			// now, zero out next offset to prevent "trouble"
			offset = mCurrentPosition + size;
			integer2bytestream(mBuffer, offset, 0);

			// set HP to appropriate value
			set_register(mBuffer, LREG_HP, mCurrentPosition + size);
			return size;
		}

		// ok, is this slot empty?
		toffset += LSCRIPTDataSize[LST_INTEGER];

		blocktype = *(mBuffer + toffset++);

		if (!blocktype)
		{
			// Empty block, do we need to coalesce?
			if (mLastEmpty)
			{
				coalesce(mLastEmpty, mCurrentPosition);
				mCurrentPosition = mLastEmpty;
				toffset = mCurrentPosition;
				offset = bytestream2integer(mBuffer, toffset);
			}

			// do we fit in this block?
			if (offset >= size)
			{
				// do we need to split the block? (only split if splitting will leave > HEAP_BLOCK_SPLIT_THRESHOLD bytes of free space)
				if (offset - HEAP_BLOCK_SPLIT_THRESHOLD >= size)
				{
					split(mCurrentPosition, size);
					return size;
				}
				else
					return offset;
			}
		}
		// nothing found, keep looking
		mCurrentPosition += offset;
	}
	// fake return to prevent warnings
	mCurrentPosition = 0;
	return 0;
}

LLScriptHeapRunTime	gRunTime;
#endif