aboutsummaryrefslogtreecommitdiff
path: root/sc4pd/headers/lang/PyrDeepCopier.h
blob: 3b462802ba76e3b676f6da5bb2cd3b11d8875cd4 (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
/*
	SuperCollider real time audio synthesis system
    Copyright (c) 2002 James McCartney. All rights reserved.
	http://www.audiosynth.com

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 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 General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
/*

An object archiving system for SuperCollider.

*/


#ifndef _PyrDeepCopier_
#define _PyrDeepCopier_

#include "PyrObject.h"
#include "SC_AllocPool.h"

#include "PyrKernel.h"
#include "PyrPrimitive.h"
#include "VMGlobals.h"
#include "GC.h"
#include "ReadWriteMacros.h"

const int32 kDeepCopierObjectArrayInitialCapacity = 32;

class PyrDeepCopier
{
public:
	PyrDeepCopier(VMGlobals *inG)
		: g(inG), objectArray(initialObjectArray), numObjects(0), 
			objectArrayCapacity( kDeepCopierObjectArrayInitialCapacity )
		{
		}
	
	~PyrDeepCopier()
		{
			if (objectArrayCapacity > kDeepCopierObjectArrayInitialCapacity) {
				g->allocPool->Free(objectArray);
			}
		}
			
	long doDeepCopy(PyrSlot *objectSlot)
		{
			long err = errNone;
				
			try {
				if (IsObj(objectSlot)) {
					constructObjectArray(objectSlot->uo);				
					for (int i=0; i<numObjects; ++i) {
						fixSlots(objectArray[i]);
					}
					fixObjSlot(objectSlot);
					for (int i=0; i<numObjects; ++i) {
						objectArray[i]->ClearMark();
					}

				}
			} catch (std::exception &ex) {
				error(ex.what());
				err = errFailed;
			}
			return err;
		}
	
private:
			
	void recurse(PyrObject *obj, int n)
		{	
			//post("->recurse %s %08X\n", obj->classptr->name.us->name, obj);
			PyrSlot *slot = obj->slots;
			for (int i=0; i<n; ++i, ++slot) {
				if (IsObj(slot)) constructObjectArray(slot->uo);
			}
			//post("<-recurse %s %08X\n", obj->classptr->name.us->name, obj);
		}

	void growObjectArray()
		{
			int32 newObjectArrayCapacity = objectArrayCapacity << 1;
			
			int32 newSize = newObjectArrayCapacity * sizeof(PyrObject*);
			PyrObject** newArray = (PyrObject**)g->allocPool->Alloc(newSize);
			memcpy(newArray, objectArray, numObjects * sizeof(PyrObject*));
			if (objectArrayCapacity > kDeepCopierObjectArrayInitialCapacity) {
				g->allocPool->Free(objectArray);
			}
			objectArrayCapacity = newObjectArrayCapacity;
			objectArray = newArray;
		}
		
	void putSelf(PyrObject *obj)
		{
			obj->SetMark();
			obj->scratch1 = numObjects;
			
			// expand array if needed
			if (numObjects >= objectArrayCapacity) growObjectArray();
			
			//post("putSelf %d %08X\n", numObjects, obj);
			// add to array
			objectArray[numObjects++] = obj;
		}

	void putCopy(PyrObject *obj)
		{
			obj->SetMark();
			obj->scratch1 = numObjects;
			
			// expand array if needed
			if (numObjects+2 >= objectArrayCapacity) growObjectArray();
			
			// add a shallow copy to object array
			PyrObject *copy = copyObject(g->gc, obj, false);
			copy->ClearMark();
			
			//post("putCopy %d %08X\n", numObjects, copy);
			
			// add to array
			objectArray[numObjects++] = copy;
			objectArray[numObjects++] = obj;
		}

	void constructObjectArray(PyrObject *obj)
		{
			//post("->constructObjectArray %s %08X\n", obj->classptr->name.us->name, obj);
			if (!obj->IsMarked()) {
				if (isKindOf(obj, class_class)) {
					putSelf(obj);
				} else if (isKindOf(obj, class_process)) {
					putSelf(obj);
				} else if (isKindOf(obj, s_interpreter->u.classobj)) {
					putSelf(obj);
				} else if (isKindOf(obj, class_rawarray)) {
					putCopy(obj);
				} else if (isKindOf(obj, class_array)) {
					putCopy(obj);
					recurse(obj, obj->size);
				} else if (isKindOf(obj, class_func)) {
					putSelf(obj);
				} else if (isKindOf(obj, class_method)) {
					putSelf(obj);
				} else if (isKindOf(obj, class_thread)) {
					putSelf(obj);
				} else {
					putCopy(obj);
					recurse(obj, obj->size);
				}
			}
			//post("<-constructObjectArray %s %08X\n", obj->classptr->name.us->name, obj);
		}

	void fixObjSlot(PyrSlot* slot) 
		{
			//post("fixObjSlot %s %08X %d %08X\n", slot->uo->classptr->name.us->name, slot->uo, slot->uo->scratch1, objectArray[slot->uo->scratch1]);
			slot->uo = objectArray[slot->uo->scratch1];
		}
	
	void fixSlots(PyrObject *obj)
		{
			//post("fixSlots %s %08X %d\n", obj->classptr->name.us->name, obj, obj->IsMarked());
			if (!obj->IsMarked() && obj->obj_format <= obj_slot) { // it is a copy
				PyrSlot *slot = obj->slots;
				for (int i=0; i<obj->size; ++i, ++slot) {
					if (IsObj(slot)) fixObjSlot(slot);
				}
			}
		}
		
	VMGlobals *g;
		
	PyrObject **objectArray;
	int32 numObjects;
	int32 objectArrayCapacity;
	
	PyrObject *initialObjectArray[kDeepCopierObjectArrayInitialCapacity];
};

/*
	SuperCollider real time audio synthesis system
    Copyright (c) 2002 James McCartney. All rights reserved.
	http://www.audiosynth.com

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 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 General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
/*

An object archiving system for SuperCollider.

*/



#endif