| ︙ | | | ︙ | |
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
* Portions created by AOL are Copyright (C) 1999 America Online, 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"
#if defined(TCL_THREADS) && defined(USE_THREAD_ALLOC)
/*
* If range checking is enabled, an additional byte will be allocated to store
* the magic number at the end of the requested memory.
*/
#ifndef RCHECK
|
|
|
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
* Portions created by AOL are Copyright (C) 1999 America Online, 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"
#if defined(USE_THREAD_ALLOC)
/*
* If range checking is enabled, an additional byte will be allocated to store
* the magic number at the end of the requested memory.
*/
#ifndef RCHECK
|
| ︙ | | | ︙ | |
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
|
*
* IMPORTANT NOTE: the buckets have to be kept at the start, as the macros for
* TclSmallAlloc in tclInt.h assume that the smallest block
*/
typedef struct Cache {
Bucket buckets[NBUCKETS]; /* The buckets for this thread */
struct Cache *nextPtr; /* Linked list of cache entries */
Tcl_ThreadId owner; /* Which thread's cache is this? */
int totalAssigned; /* Total space assigned to thread */
} Cache;
/*
* The following array specifies various per-bucket limits and locks. The
* values are statically initialized to avoid calculating them repeatedly.
*/
static struct {
size_t blockSize; /* Bucket blocksize. */
int maxBlocks; /* Max blocks before move to share. */
int numMove; /* Num blocks to move to share. */
Tcl_Mutex *lockPtr; /* Share bucket lock. */
} bucketInfo[NBUCKETS];
/*
* Static functions defined in this file.
*/
static Cache * GetCache(void);
static void LockBucket(Cache *cachePtr, int bucket);
static void UnlockBucket(Cache *cachePtr, int bucket);
static void PutBlocks(Cache *cachePtr, int bucket, int numMove);
static int GetBlocks(Cache *cachePtr, int bucket);
static inline Block * Ptr2Block(char *ptr);
static inline char * Block2Ptr(Block *blockPtr, int bucket, unsigned int reqSize);
/*
* Local variables defined in this file and initialized at startup.
*/
static Tcl_Mutex *listLockPtr;
static Tcl_Mutex *objLockPtr;
static Cache sharedCache;
static Cache *sharedPtr = &sharedCache;
static Cache *firstCachePtr = &sharedCache;
/*
*----------------------------------------------------------------------
*
* Block2Ptr, Ptr2Block --
*
* Convert between internal blocks and user pointers.
|
>
>
<
>
>
>
>
>
>
<
<
<
<
|
<
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
|
>
>
|
|
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
|
*
* IMPORTANT NOTE: the buckets have to be kept at the start, as the macros for
* TclSmallAlloc in tclInt.h assume that the smallest block
*/
typedef struct Cache {
Bucket buckets[NBUCKETS]; /* The buckets for this thread */
int totalAssigned; /* Total space assigned to thread */
#if defined(TCL_THREADS)
struct Cache *nextPtr; /* Linked list of cache entries */
Tcl_ThreadId owner; /* Which thread's cache is this? */
#endif
} Cache;
static Cache sharedCache;
static Cache *sharedPtr = NULL; /* set on init */
/*
* The following array specifies various per-bucket limits and locks. The
* values are statically initialized to avoid calculating them repeatedly.
*/
static struct {
size_t blockSize; /* Bucket blocksize. */
#if defined(TCL_THREADS)
int maxBlocks; /* Max blocks before move to share. */
int numMove; /* Num blocks to move to share. */
Tcl_Mutex *lockPtr; /* Share bucket lock. */
#endif
} bucketInfo[NBUCKETS];
/*
* Static functions defined in this file.
*/
static Cache * GetCache(void);
static int GetBlocks(Cache *cachePtr, int bucket);
static inline Block * Ptr2Block(char *ptr);
static inline char * Block2Ptr(Block *blockPtr, int bucket, unsigned int reqSize);
#if defined(TCL_THREADS)
static Cache *firstCachePtr = &sharedCache;
static Tcl_Mutex *listLockPtr;
static Tcl_Mutex *objLockPtr;
# define GETCACHE(cachePtr) \
do { \
(cachePtr) = TclpGetAllocCache(); \
if ((cachePtr) == NULL) { \
(cachePtr) = GetCache(); \
} \
} while (0)
static void LockBucket(Cache *cachePtr, int bucket);
static void UnlockBucket(Cache *cachePtr, int bucket);
static void PutBlocks(Cache *cachePtr, int bucket, int numMove);
#else /* NOT THREADS! */
#define TclpSetAllocCache()
#define PutBlocks(cachePtr, bucket, numMove)
#define firstCachePtr &sharedCache
# define GETCACHE(cachePtr) \
do { \
if (sharedPtr == NULL) { \
GetCache(); \
} \
(cachePtr) = &sharedCache; \
} while (0)
void *
TclpGetAllocCache(void)
{
static int init = 1;
if (init) {
init = 0;
GetCache();
}
return &sharedCache;
}
#endif
/*
*----------------------------------------------------------------------
*
* Block2Ptr, Ptr2Block --
*
* Convert between internal blocks and user pointers.
|
| ︙ | | | ︙ | |
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
|
*----------------------------------------------------------------------
*/
static Cache *
GetCache(void)
{
Cache *cachePtr;
/*
* Check for first-time initialization.
*/
if (listLockPtr == NULL) {
Tcl_Mutex *initLockPtr;
unsigned int i;
initLockPtr = Tcl_GetAllocMutex();
Tcl_MutexLock(initLockPtr);
if (listLockPtr == NULL) {
listLockPtr = TclpNewAllocMutex();
objLockPtr = TclpNewAllocMutex();
for (i = 0; i < NBUCKETS; ++i) {
bucketInfo[i].blockSize = MINALLOC << i;
bucketInfo[i].maxBlocks = 1 << (NBUCKETS - 1 - i);
bucketInfo[i].numMove = i < NBUCKETS - 1 ?
1 << (NBUCKETS - 2 - i) : 1;
bucketInfo[i].lockPtr = TclpNewAllocMutex();
}
}
Tcl_MutexUnlock(initLockPtr);
}
/*
* Get this thread's cache, allocating if necessary.
*/
cachePtr = TclpGetAllocCache();
if (cachePtr == NULL) {
cachePtr = calloc(1, sizeof(Cache));
if (cachePtr == NULL) {
Tcl_Panic("alloc: could not allocate new cache");
}
Tcl_MutexLock(listLockPtr);
cachePtr->nextPtr = firstCachePtr;
firstCachePtr = cachePtr;
Tcl_MutexUnlock(listLockPtr);
cachePtr->owner = Tcl_GetCurrentThread();
TclpSetAllocCache(cachePtr);
}
return cachePtr;
}
/*
*----------------------------------------------------------------------
*
* TclFreeAllocCache --
*
* Flush and delete a cache, removing from list of caches.
*
|
>
>
<
<
>
>
>
>
>
>
>
>
>
|
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
|
*----------------------------------------------------------------------
*/
static Cache *
GetCache(void)
{
Cache *cachePtr;
unsigned int i;
/*
* Check for first-time initialization.
*/
#if defined(TCL_THREADS)
if (listLockPtr == NULL) {
Tcl_Mutex *initLockPtr;
initLockPtr = Tcl_GetAllocMutex();
Tcl_MutexLock(initLockPtr);
if (listLockPtr == NULL) {
listLockPtr = TclpNewAllocMutex();
objLockPtr = TclpNewAllocMutex();
#endif
for (i = 0; i < NBUCKETS; ++i) {
bucketInfo[i].blockSize = MINALLOC << i;
#if defined(TCL_THREADS)
bucketInfo[i].maxBlocks = 1 << (NBUCKETS - 1 - i);
bucketInfo[i].numMove = i < NBUCKETS - 1 ?
1 << (NBUCKETS - 2 - i) : 1;
bucketInfo[i].lockPtr = TclpNewAllocMutex();
#endif
}
#if defined(TCL_THREADS)
}
Tcl_MutexUnlock(initLockPtr);
}
#endif
/*
* Get this thread's cache, allocating if necessary.
*/
cachePtr = TclpGetAllocCache();
if (cachePtr == NULL) {
cachePtr = calloc(1, sizeof(Cache));
if (cachePtr == NULL) {
Tcl_Panic("alloc: could not allocate new cache");
}
#if defined(TCL_THREADS)
Tcl_MutexLock(listLockPtr);
cachePtr->nextPtr = firstCachePtr;
firstCachePtr = cachePtr;
Tcl_MutexUnlock(listLockPtr);
cachePtr->owner = Tcl_GetCurrentThread();
TclpSetAllocCache(cachePtr);
#endif
}
sharedPtr = cachePtr;
return cachePtr;
}
#if defined(TCL_THREADS)
/*
*----------------------------------------------------------------------
*
* TclFreeAllocCache --
*
* Flush and delete a cache, removing from list of caches.
*
|
| ︙ | | | ︙ | |
312
313
314
315
316
317
318
319
320
321
322
323
324
325
|
nextPtrPtr = &(*nextPtrPtr)->nextPtr;
}
*nextPtrPtr = cachePtr->nextPtr;
cachePtr->nextPtr = NULL;
Tcl_MutexUnlock(listLockPtr);
free(cachePtr);
}
/*
*----------------------------------------------------------------------
*
* Tcl_AttemptAlloc --
*
* Allocate memory.
|
>
|
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
|
nextPtrPtr = &(*nextPtrPtr)->nextPtr;
}
*nextPtrPtr = cachePtr->nextPtr;
cachePtr->nextPtr = NULL;
Tcl_MutexUnlock(listLockPtr);
free(cachePtr);
}
#endif
/*
*----------------------------------------------------------------------
*
* Tcl_AttemptAlloc --
*
* Allocate memory.
|
| ︙ | | | ︙ | |
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
|
char *
Tcl_AttemptAlloc(
unsigned int reqSize)
{
Cache *cachePtr;
cachePtr = TclpGetAllocCache();
if (cachePtr == NULL) {
cachePtr = GetCache();
}
return TclAttemptAlloc(cachePtr, reqSize);
}
char *
TclAttemptAlloc(
void *vcachePtr,
unsigned int reqSize)
|
<
|
<
<
|
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
|
char *
Tcl_AttemptAlloc(
unsigned int reqSize)
{
Cache *cachePtr;
GETCACHE(cachePtr);
return TclAttemptAlloc(cachePtr, reqSize);
}
char *
TclAttemptAlloc(
void *vcachePtr,
unsigned int reqSize)
|
| ︙ | | | ︙ | |
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
|
void
Tcl_Free(
char *ptr)
{
Cache *cachePtr;
cachePtr = TclpGetAllocCache();
if (cachePtr == NULL) {
cachePtr = GetCache();
}
return TclFree(cachePtr, ptr);
}
void
TclFree(
void *vcachePtr,
char *ptr)
|
<
|
<
<
|
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
|
void
Tcl_Free(
char *ptr)
{
Cache *cachePtr;
GETCACHE(cachePtr);
return TclFree(cachePtr, ptr);
}
void
TclFree(
void *vcachePtr,
char *ptr)
|
| ︙ | | | ︙ | |
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
|
cachePtr->buckets[bucket].totalAssigned -= blockPtr->blockReqSize;
blockPtr->nextBlock = cachePtr->buckets[bucket].firstPtr;
cachePtr->buckets[bucket].firstPtr = blockPtr;
cachePtr->buckets[bucket].numFree++;
cachePtr->buckets[bucket].numInserts++;
if (cachePtr != sharedPtr &&
cachePtr->buckets[bucket].numFree > bucketInfo[bucket].maxBlocks) {
PutBlocks(cachePtr, bucket, bucketInfo[bucket].numMove);
}
}
/*
*----------------------------------------------------------------------
*
* Tcl_AttemptRealloc --
*
|
>
>
|
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
|
cachePtr->buckets[bucket].totalAssigned -= blockPtr->blockReqSize;
blockPtr->nextBlock = cachePtr->buckets[bucket].firstPtr;
cachePtr->buckets[bucket].firstPtr = blockPtr;
cachePtr->buckets[bucket].numFree++;
cachePtr->buckets[bucket].numInserts++;
#if defined(TCL_THREADS)
if (cachePtr != sharedPtr &&
cachePtr->buckets[bucket].numFree > bucketInfo[bucket].maxBlocks) {
PutBlocks(cachePtr, bucket, bucketInfo[bucket].numMove);
}
#endif
}
/*
*----------------------------------------------------------------------
*
* Tcl_AttemptRealloc --
*
|
| ︙ | | | ︙ | |
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
|
char *
Tcl_AttemptRealloc(
char *ptr,
unsigned int reqSize)
{
Cache *cachePtr;
cachePtr = TclpGetAllocCache();
if (cachePtr == NULL) {
cachePtr = GetCache();
}
return TclAttemptRealloc(cachePtr, ptr, reqSize);
}
char *
TclAttemptRealloc(
void *vcachePtr,
char *ptr,
|
<
|
<
<
|
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
|
char *
Tcl_AttemptRealloc(
char *ptr,
unsigned int reqSize)
{
Cache *cachePtr;
GETCACHE(cachePtr);
return TclAttemptRealloc(cachePtr, ptr, reqSize);
}
char *
TclAttemptRealloc(
void *vcachePtr,
char *ptr,
|
| ︙ | | | ︙ | |
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
|
Tcl_MutexLock(listLockPtr);
cachePtr = firstCachePtr;
while (cachePtr != NULL) {
Tcl_DStringStartSublist(dsPtr);
if (cachePtr == sharedPtr) {
Tcl_DStringAppendElement(dsPtr, "shared");
} else {
sprintf(buf, "thread%p", cachePtr->owner);
Tcl_DStringAppendElement(dsPtr, buf);
}
for (n = 0; n < NBUCKETS; ++n) {
sprintf(buf, "%lu %ld %ld %ld %ld %ld %ld",
(unsigned long) bucketInfo[n].blockSize,
cachePtr->buckets[n].numFree,
cachePtr->buckets[n].numRemoves,
cachePtr->buckets[n].numInserts,
cachePtr->buckets[n].totalAssigned,
cachePtr->buckets[n].numLocks,
cachePtr->buckets[n].numWaits);
Tcl_DStringAppendElement(dsPtr, buf);
}
Tcl_DStringEndSublist(dsPtr);
cachePtr = cachePtr->nextPtr;
}
Tcl_MutexUnlock(listLockPtr);
}
/*
*----------------------------------------------------------------------
*
* LockBucket, UnlockBucket --
*
* Set/unset the lock to access a bucket in the shared cache.
*
|
>
>
>
>
>
>
>
|
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
|
Tcl_MutexLock(listLockPtr);
cachePtr = firstCachePtr;
while (cachePtr != NULL) {
Tcl_DStringStartSublist(dsPtr);
if (cachePtr == sharedPtr) {
Tcl_DStringAppendElement(dsPtr, "shared");
#if defined(TCL_THREADS)
} else {
sprintf(buf, "thread%p", cachePtr->owner);
Tcl_DStringAppendElement(dsPtr, buf);
#endif
}
for (n = 0; n < NBUCKETS; ++n) {
sprintf(buf, "%lu %ld %ld %ld %ld %ld %ld",
(unsigned long) bucketInfo[n].blockSize,
cachePtr->buckets[n].numFree,
cachePtr->buckets[n].numRemoves,
cachePtr->buckets[n].numInserts,
cachePtr->buckets[n].totalAssigned,
cachePtr->buckets[n].numLocks,
cachePtr->buckets[n].numWaits);
Tcl_DStringAppendElement(dsPtr, buf);
}
Tcl_DStringEndSublist(dsPtr);
#if defined(TCL_THREADS)
cachePtr = cachePtr->nextPtr;
#else
cachePtr = NULL;
#endif
}
Tcl_MutexUnlock(listLockPtr);
}
#if defined(TCL_THREADS)
/*
*----------------------------------------------------------------------
*
* LockBucket, UnlockBucket --
*
* Set/unset the lock to access a bucket in the shared cache.
*
|
| ︙ | | | ︙ | |
772
773
774
775
776
777
778
779
780
781
782
783
784
785
|
LockBucket(cachePtr, bucket);
lastPtr->nextBlock = sharedPtr->buckets[bucket].firstPtr;
sharedPtr->buckets[bucket].firstPtr = firstPtr;
sharedPtr->buckets[bucket].numFree += numMove;
UnlockBucket(cachePtr, bucket);
}
/*
*----------------------------------------------------------------------
*
* GetBlocks --
*
* Get more blocks for a bucket.
|
>
|
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
|
LockBucket(cachePtr, bucket);
lastPtr->nextBlock = sharedPtr->buckets[bucket].firstPtr;
sharedPtr->buckets[bucket].firstPtr = firstPtr;
sharedPtr->buckets[bucket].numFree += numMove;
UnlockBucket(cachePtr, bucket);
}
#endif
/*
*----------------------------------------------------------------------
*
* GetBlocks --
*
* Get more blocks for a bucket.
|
| ︙ | | | ︙ | |
797
798
799
800
801
802
803
804
805
806
807
808
809
810
|
GetBlocks(
Cache *cachePtr,
int bucket)
{
register Block *blockPtr;
register int n;
/*
* First, atttempt to move blocks from the shared cache. Note the
* potentially dirty read of numFree before acquiring the lock which is a
* slight performance enhancement. The value is verified after the lock is
* actually acquired.
*/
|
>
|
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
|
GetBlocks(
Cache *cachePtr,
int bucket)
{
register Block *blockPtr;
register int n;
#if defined(TCL_THREADS)
/*
* First, atttempt to move blocks from the shared cache. Note the
* potentially dirty read of numFree before acquiring the lock which is a
* slight performance enhancement. The value is verified after the lock is
* actually acquired.
*/
|
| ︙ | | | ︙ | |
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
|
}
sharedPtr->buckets[bucket].firstPtr = blockPtr->nextBlock;
blockPtr->nextBlock = NULL;
}
}
UnlockBucket(cachePtr, bucket);
}
if (cachePtr->buckets[bucket].numFree == 0) {
register size_t size;
/*
* If no blocks could be moved from shared, first look for a larger
* block in this cache to split up.
*/
|
>
|
|
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
|
}
sharedPtr->buckets[bucket].firstPtr = blockPtr->nextBlock;
blockPtr->nextBlock = NULL;
}
}
UnlockBucket(cachePtr, bucket);
}
#endif
if (cachePtr->buckets[bucket].numFree == 0) {
register size_t size;
/*
* If no blocks could be moved from shared, first look for a larger
* block in this cache to split up.
*/
|
| ︙ | | | ︙ | |
886
887
888
889
890
891
892
893
894
895
896
897
898
899
|
blockPtr = blockPtr->nextBlock;
}
blockPtr->nextBlock = NULL;
}
return 1;
}
/*
*----------------------------------------------------------------------
*
* TclFinalizeThreadAlloc --
*
* This procedure is used to destroy all private resources used in this
* file.
|
>
|
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
|
blockPtr = blockPtr->nextBlock;
}
blockPtr->nextBlock = NULL;
}
return 1;
}
#if defined(TCL_THREADS)
/*
*----------------------------------------------------------------------
*
* TclFinalizeThreadAlloc --
*
* This procedure is used to destroy all private resources used in this
* file.
|
| ︙ | | | ︙ | |
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
|
objLockPtr = NULL;
TclpFreeAllocMutex(listLockPtr);
listLockPtr = NULL;
TclpFreeAllocCache(NULL);
}
#else /* !(TCL_THREADS && USE_THREAD_ALLOC) */
/*
*----------------------------------------------------------------------
*
* Tcl_GetMemoryInfo --
*
* Return a list-of-lists of memory stats.
*
* Results:
* None.
*
* Side effects:
* List appended to given dstring.
*
*----------------------------------------------------------------------
*/
void
Tcl_GetMemoryInfo(
Tcl_DString *dsPtr)
{
Tcl_Panic("Tcl_GetMemoryInfo called when threaded memory allocator not in use");
}
/*
*----------------------------------------------------------------------
*
* TclFinalizeThreadAlloc --
*
* This procedure is used to destroy all private resources used in this
* file.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
void
TclFinalizeThreadAlloc(void)
{
Tcl_Panic("TclFinalizeThreadAlloc called when threaded memory allocator not in use");
}
#endif /* TCL_THREADS && USE_THREAD_ALLOC */
/*
* Local Variables:
* mode: c
* c-basic-offset: 4
* fill-column: 78
* End:
*/
|
|
<
<
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
|
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
|
objLockPtr = NULL;
TclpFreeAllocMutex(listLockPtr);
listLockPtr = NULL;
TclpFreeAllocCache(NULL);
}
#endif
#endif /* USE_THREAD_ALLOC */
/*
* Local Variables:
* mode: c
* c-basic-offset: 4
* fill-column: 78
* End:
*/
|