Tcl Source Code

Changes On Branch novem-ak-preserve-experiments
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Changes In Branch novem-ak-preserve-experiments Excluding Merge-Ins

This is equivalent to a diff from 1dfcdabf11 to 22f8fd36d9

2012-12-04
13:40
merge trunk check-in: ab905bcfdc user: jan.nijtmans tags: novem
00:01
Cleaning up some of the internals of TIP #280.

Replaced the hashtables keyed by pointers to Proc an... Leaf check-in: d2cb52feda user: andreask tags: novem-ak-iframe-direct

2012-12-03
20:47
This branch explores the performance implications of relacing the dynamic array data structure unde... Leaf check-in: 22f8fd36d9 user: andreask tags: novem-ak-preserve-experiments
13:02
Remove obsolete functions Tcl_GetDefaultEncodingDir and Tcl_SetDefaultEncodingDir check-in: 1dfcdabf11 user: jan.nijtmans tags: novem
10:01
Initialize legacyFreeProc with invalid value: This will result in a crash immediately, when an exten... check-in: f72e9defa2 user: jan.nijtmans tags: novem

Changes to generic/tclPreserve.c.

1
2
3
4
5
6
7
8
9
10
11
12












13
14
15
16
17
18
19
/*
 * tclPreserve.c --
 *
 *	This file contains a collection of functions that are used to make
 *	sure that widget records and other data structures aren't reallocated
 *	when there are nested functions that depend on their existence.
 *
 * Copyright (c) 1991-1994 The Regents of the University of California.
 * Copyright (c) 1994-1998 Sun Microsystems, Inc.
 *
 * See the file "license.terms" for information on usage and redistribution of
 * this file, and for a DISCLAIMER OF ALL WARRANTIES.












 */

#include "tclInt.h"

/*
 * The following data structure is used to keep track of all the Tcl_Preserve
 * calls that are still in effect. It grows as needed to accommodate any












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







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
/*
 * tclPreserve.c --
 *
 *	This file contains a collection of functions that are used to make
 *	sure that widget records and other data structures aren't reallocated
 *	when there are nested functions that depend on their existence.
 *
 * Copyright (c) 1991-1994 The Regents of the University of California.
 * Copyright (c) 1994-1998 Sun Microsystems, Inc.
 *
 * See the file "license.terms" for information on usage and redistribution of
 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
 *
 *
 * === NOVEM EXPERIMENT ===
 *   Replace the dynamic var-sized array of References with a
 *   hashtable. Faster lookup, no linear search.
 *
 *   Current disadvantage - Possible higher memory churn as individual
 *   Reference structures get allocated and freed.
 *
 *   Could be fixable by using a list-based stack of free/reusable Reference
 *   structures.
 * === NOVEM EXPERIMENT ===
 */

#include "tclInt.h"

/*
 * The following data structure is used to keep track of all the Tcl_Preserve
 * calls that are still in effect. It grows as needed to accommodate any
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
} Reference;

/*
 * Global data structures used to hold the list of preserved data references.
 * These variables are protected by "preserveMutex".
 */

static Reference *refArray = NULL;	/* First in array of references. */
static int spaceAvl = 0;	/* Total number of structures available at
				 * *firstRefPtr. */
static int inUse = 0;		/* Count of structures currently in use in
				 * refArray. */
TCL_DECLARE_MUTEX(preserveMutex)/* To protect the above statics */

#define INITIAL_SIZE	2	/* Initial number of reference slots to make */

/*
 * The following data structure is used to keep track of whether an arbitrary
 * block of memory has been deleted. This is used by the TclHandle code to
 * avoid the more time-expensive algorithm of Tcl_Preserve(). This mechanism
 * is mainly used when we have lots of references to a few big, expensive
 * objects that we don't want to live any longer than necessary.







|
|
<
<
<

<
<







44
45
46
47
48
49
50
51
52



53


54
55
56
57
58
59
60
} Reference;

/*
 * Global data structures used to hold the list of preserved data references.
 * These variables are protected by "preserveMutex".
 */

static Tcl_HashTable* refTable;




TCL_DECLARE_MUTEX(preserveMutex)/* To protect the above statics */



/*
 * The following data structure is used to keep track of whether an arbitrary
 * block of memory has been deleted. This is used by the TclHandle code to
 * avoid the more time-expensive algorithm of Tcl_Preserve(). This mechanism
 * is mainly used when we have lots of references to a few big, expensive
 * objects that we don't want to live any longer than necessary.
84
85
86
87
88
89
90
91








92

93
94
95
96
97
98
99
100
101
102
 */

	/* ARGSUSED */
void
TclFinalizePreserve(void)
{
    Tcl_MutexLock(&preserveMutex);
    if (spaceAvl != 0) {








	ckfree(refArray);

	refArray = NULL;
	inUse = 0;
	spaceAvl = 0;
    }
    Tcl_MutexUnlock(&preserveMutex);
}

/*
 *----------------------------------------------------------------------
 *







|
>
>
>
>
>
>
>
>
|
>
|
|
<







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

	/* ARGSUSED */
void
TclFinalizePreserve(void)
{
    Tcl_MutexLock(&preserveMutex);
    if (refTable) {
	for (;;) {
	    Reference* refPtr;
	    Tcl_HashSearch s;
	    Tcl_HashEntry* hEntry;
	    hEntry = Tcl_FirstHashEntry (refTable, &s);
	    if (!hEntry) break;
	    refPtr = Tcl_GetHashValue (hEntry);
	    Tcl_DeleteHashEntry (hEntry);
	    ckfree (refPtr);
	}
	Tcl_DeleteHashTable (refTable);
	refTable = 0;

    }
    Tcl_MutexUnlock(&preserveMutex);
}

/*
 *----------------------------------------------------------------------
 *
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
 */

void
Tcl_Preserve(
    ClientData clientData)	/* Pointer to malloc'ed block of memory. */
{
    Reference *refPtr;
    int i;


    /*
     * See if there is already a reference for this pointer. If so, just
     * increment its reference count.
     */

    Tcl_MutexLock(&preserveMutex);
    for (i=0, refPtr=refArray ; i<inUse ; i++, refPtr++) {
	if (refPtr->clientData == clientData) {
	    refPtr->refCount++;
	    Tcl_MutexUnlock(&preserveMutex);
	    return;
	}



    }

    /*
     * Make a reference array if it doesn't already exist, or make it bigger

     * if it is full.
     */



    if (inUse == spaceAvl) {
	spaceAvl = spaceAvl ? 2*spaceAvl : INITIAL_SIZE;
	refArray = ckrealloc(refArray, spaceAvl * sizeof(Reference));


    }

    /*
     * Make a new entry for the new reference.
     */

    refPtr = &refArray[inUse];
    refPtr->clientData = clientData;
    refPtr->refCount = 1;
    refPtr->mustFree = 0;
    refPtr->freeProc = TCL_STATIC;
    inUse += 1;



    Tcl_MutexUnlock(&preserveMutex);
}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_Release --







|
>







<
<
<
<
<
|
>
>
>


<
<
>
|
<
>
>

<
<
<
>
>






|




|
>
>
>







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

void
Tcl_Preserve(
    ClientData clientData)	/* Pointer to malloc'ed block of memory. */
{
    Reference *refPtr;
    int isnew;
    Tcl_HashEntry* hEntry;

    /*
     * See if there is already a reference for this pointer. If so, just
     * increment its reference count.
     */

    Tcl_MutexLock(&preserveMutex);






    if (!refTable) {
	refTable = ckalloc (sizeof (Tcl_HashTable));
	Tcl_InitHashTable (refTable, TCL_ONE_WORD_KEYS);
    }



    hEntry = Tcl_FindHashEntry (refTable, clientData);
    if (hEntry) {

	refPtr = Tcl_GetHashValue (hEntry);
	refPtr->refCount++;




	Tcl_MutexUnlock(&preserveMutex);
	return;
    }

    /*
     * Make a new entry for the new reference.
     */

    refPtr = ckalloc (sizeof (Reference));
    refPtr->clientData = clientData;
    refPtr->refCount = 1;
    refPtr->mustFree = 0;
    refPtr->freeProc = TCL_STATIC;

    hEntry = Tcl_CreateHashEntry (refTable, clientData, &isnew);
    Tcl_SetHashValue (hEntry, refPtr);

    Tcl_MutexUnlock(&preserveMutex);
}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_Release --
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
 */

void
Tcl_Release(
    ClientData clientData)	/* Pointer to malloc'ed block of memory. */
{
    Reference *refPtr;

    int i;


    Tcl_MutexLock(&preserveMutex);
    for (i=0, refPtr=refArray ; i<inUse ; i++, refPtr++) {
	int mustFree;
	Tcl_FreeProc *freeProc;


	if (refPtr->clientData != clientData) {



	    continue;



	}





	if (--refPtr->refCount != 0) {
	    Tcl_MutexUnlock(&preserveMutex);
	    return;
	}

	/*
	 * Must remove information from the slot before calling freeProc to
	 * avoid reentrancy problems if the freeProc calls Tcl_Preserve on the
	 * same clientData. Copy down the last reference in the array to
	 * overwrite the current slot.
	 */

	freeProc = refPtr->freeProc;
	mustFree = refPtr->mustFree;
	inUse--;
	if (i < inUse) {
	    refArray[i] = refArray[inUse];
	}



	/*
	 * Now committed to disposing the data. But first, we've patched up
	 * all the global data structures so we should release the mutex now.
	 * Only then should we dabble around with potentially-slow memory
	 * managers...
	 */

	Tcl_MutexUnlock(&preserveMutex);

	if (mustFree) {
	    if (freeProc == TCL_DYNAMIC) {
		ckfree(clientData);
	    } else {
		freeProc(clientData);
	    }
	}
	return;
    }
    Tcl_MutexUnlock(&preserveMutex);

    /*
     * Reference not found. This is a bug in the caller.
     */

    Tcl_Panic("Tcl_Release couldn't find reference for %p", clientData);
}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_EventuallyFree --
 *







>
|
>


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

|
|
|
|
<
|

|
|
<
<
<
|
>
>

|
|
|
|
|
|

|
>
|
|
|
|
|
|
|
|
<
<
<
<
<
<
<
<







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

void
Tcl_Release(
    ClientData clientData)	/* Pointer to malloc'ed block of memory. */
{
    Reference *refPtr;
    Tcl_HashEntry* hEntry;
    int mustFree;
    Tcl_FreeProc *freeProc;

    Tcl_MutexLock(&preserveMutex);

    if (!refTable) {
	refTable = ckalloc (sizeof (Tcl_HashTable));
	Tcl_InitHashTable (refTable, TCL_ONE_WORD_KEYS);
    }

    hEntry = Tcl_FindHashEntry (refTable, clientData);
    if (!hEntry) {
	Tcl_MutexUnlock(&preserveMutex);

	/*
	 * Reference not found. This is a bug in the caller.
	 */

	Tcl_Panic("Tcl_Release couldn't find reference for %p", clientData);
    }

    refPtr = Tcl_GetHashValue (hEntry);

    if (--refPtr->refCount != 0) {
	Tcl_MutexUnlock(&preserveMutex);
	return;
    }

    /*
     * Must remove information from the slot before calling freeProc to
     * avoid reentrancy problems if the freeProc calls Tcl_Preserve on the
     * same clientData.

     */

    freeProc = refPtr->freeProc;
    mustFree = refPtr->mustFree;




    Tcl_DeleteHashEntry (hEntry);
    ckfree (refPtr);

    /*
     * Now committed to disposing the data. But first, we've patched up
     * all the global data structures so we should release the mutex now.
     * Only then should we dabble around with potentially-slow memory
     * managers...
     */

    Tcl_MutexUnlock(&preserveMutex);

    if (mustFree) {
	if (freeProc == TCL_DYNAMIC) {
	    ckfree(clientData);
	} else {
	    freeProc(clientData);
	}
    }
    return;








}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_EventuallyFree --
 *
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

void
Tcl_EventuallyFree(
    ClientData clientData,	/* Pointer to malloc'ed block of memory. */
    Tcl_FreeProc *freeProc)	/* Function to actually do free. */
{
    Reference *refPtr;
    int i;

    /*
     * See if there is a reference for this pointer. If so, set its "mustFree"
     * flag (the flag had better not be set already!).
     */

    Tcl_MutexLock(&preserveMutex);
    for (i = 0, refPtr = refArray; i < inUse; i++, refPtr++) {
	if (refPtr->clientData != clientData) {

	    continue;



	}
	if (refPtr->mustFree) {
	    Tcl_Panic("Tcl_EventuallyFree called twice for %p", clientData);
	}
	refPtr->mustFree = 1;
	refPtr->freeProc = freeProc;

	Tcl_MutexUnlock(&preserveMutex);
	return;
    }


    Tcl_MutexUnlock(&preserveMutex);

    /*
     * No reference for this block.  Free it now.
     */

    if (freeProc == TCL_DYNAMIC) {







<







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







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

void
Tcl_EventuallyFree(
    ClientData clientData,	/* Pointer to malloc'ed block of memory. */
    Tcl_FreeProc *freeProc)	/* Function to actually do free. */
{
    Reference *refPtr;


    /*
     * See if there is a reference for this pointer. If so, set its "mustFree"
     * flag (the flag had better not be set already!).
     */

    Tcl_MutexLock(&preserveMutex);

    if (refTable) {
	Tcl_HashEntry* hEntry;

	hEntry = Tcl_FindHashEntry (refTable, clientData);
	if (hEntry) {
	    refPtr = Tcl_GetHashValue (hEntry);

	    if (refPtr->mustFree) {
		Tcl_Panic("Tcl_EventuallyFree called twice for %p", clientData);
	    }
	    refPtr->mustFree = 1;
	    refPtr->freeProc = freeProc;

	    Tcl_MutexUnlock(&preserveMutex);
	    return;
	}
    }

    Tcl_MutexUnlock(&preserveMutex);

    /*
     * No reference for this block.  Free it now.
     */

    if (freeProc == TCL_DYNAMIC) {