Debian Codegraph @

generic/tclThreadAlloc.c

raw
1 /*
2 * tclThreadAlloc.c --
3 *
4 * This is a very fast storage allocator for used with threads (designed
5 * avoid lock contention). The basic strategy is to allocate memory in
6 * fixed size blocks from block caches.
7 *
8 * The Initial Developer of the Original Code is America Online, Inc.
9 * Portions created by AOL are Copyright (C) 1999 America Online, Inc.
10 *
11 * See the file "license.terms" for information on usage and redistribution of
12 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
13 */
14
15 #include "tclInt.h"
16 #if defined(TCL_THREADS) && defined(USE_THREAD_ALLOC)
17
18 /*
19 * If range checking is enabled, an additional byte will be allocated to store
20 * the magic number at the end of the requested memory.
21 */
22
23 #ifndef RCHECK
24 #ifdef NDEBUG
25 #define RCHECK 0
26 #else
27 #define RCHECK 1
28 #endif
29 #endif
30
31 /*
32 * The following define the number of Tcl_Obj's to allocate/move at a time and
33 * the high water mark to prune a per-thread cache. On a 32 bit system,
34 * sizeof(Tcl_Obj) = 24 so 800 * 24 = ~16k.
35 */
36
37 #define NOBJALLOC 800
38
39 /* Actual definition moved to tclInt.h */
40 #define NOBJHIGH ALLOC_NOBJHIGH
41
42 /*
43 * The following union stores accounting information for each block including
44 * two small magic numbers and a bucket number when in use or a next pointer
45 * when free. The original requested size (not including the Block overhead)
46 * is also maintained.
47 */
48
49 typedef union Block {
50 struct {
51 union {
52 union Block *next; /* Next in free list. */
53 struct {
54 unsigned char magic1; /* First magic number. */
55 unsigned char bucket; /* Bucket block allocated from. */
56 unsigned char unused; /* Padding. */
57 unsigned char magic2; /* Second magic number. */
58 } s;
59 } u;
60 size_t reqSize; /* Requested allocation size. */
61 } b;
62 unsigned char padding[TCL_ALLOCALIGN];
63 } Block;
64 #define nextBlock b.u.next
65 #define sourceBucket b.u.s.bucket
66 #define magicNum1 b.u.s.magic1
67 #define magicNum2 b.u.s.magic2
68 #define MAGIC 0xEF
69 #define blockReqSize b.reqSize
70
71 /*
72 * The following defines the minimum and and maximum block sizes and the number
73 * of buckets in the bucket cache.
74 */
75
76 #define MINALLOC ((sizeof(Block) + 8 + (TCL_ALLOCALIGN-1)) & ~(TCL_ALLOCALIGN-1))
77 #define NBUCKETS (11 - (MINALLOC >> 5))
78 #define MAXALLOC (MINALLOC << (NBUCKETS - 1))
79
80 /*
81 * The following structure defines a bucket of blocks with various accounting
82 * and statistics information.
83 */
84
85 typedef struct Bucket {
86 Block *firstPtr; /* First block available */
87 Block *lastPtr; /* End of block list */
88 long numFree; /* Number of blocks available */
89
90 /* All fields below for accounting only */
91
92 long numRemoves; /* Number of removes from bucket */
93 long numInserts; /* Number of inserts into bucket */
94 long numWaits; /* Number of waits to acquire a lock */
95 long numLocks; /* Number of locks acquired */
96 long totalAssigned; /* Total space assigned to bucket */
97 } Bucket;
98
99 /*
100 * The following structure defines a cache of buckets and objs, of which there
101 * will be (at most) one per thread. Any changes need to be reflected in the
102 * struct AllocCache defined in tclInt.h, possibly also in the initialisation
103 * code in Tcl_CreateInterp().
104 */
105
106 typedef struct Cache {
107 struct Cache *nextPtr; /* Linked list of cache entries */
108 Tcl_ThreadId owner; /* Which thread's cache is this? */
109 Tcl_Obj *firstObjPtr; /* List of free objects for thread */
110 int numObjects; /* Number of objects for thread */
111 Tcl_Obj *lastPtr; /* Last object in this cache */
112 int totalAssigned; /* Total space assigned to thread */
113 Bucket buckets[NBUCKETS]; /* The buckets for this thread */
114 } Cache;
115
116 /*
117 * The following array specifies various per-bucket limits and locks. The
118 * values are statically initialized to avoid calculating them repeatedly.
119 */
120
121 static struct {
122 size_t blockSize; /* Bucket blocksize. */
123 int maxBlocks; /* Max blocks before move to share. */
124 int numMove; /* Num blocks to move to share. */
125 Tcl_Mutex *lockPtr; /* Share bucket lock. */
126 } bucketInfo[NBUCKETS];
127
128 /*
129 * Static functions defined in this file.
130 */
131
132 static Cache * GetCache(void);
133 static void LockBucket(Cache *cachePtr, int bucket);
134 static void UnlockBucket(Cache *cachePtr, int bucket);
135 static void PutBlocks(Cache *cachePtr, int bucket, int numMove);
136 static int GetBlocks(Cache *cachePtr, int bucket);
137 static Block * Ptr2Block(char *ptr);
138 static char * Block2Ptr(Block *blockPtr, int bucket, unsigned int reqSize);
139 static void MoveObjs(Cache *fromPtr, Cache *toPtr, int numMove);
140 static void PutObjs(Cache *fromPtr, int numMove);
141
142 /*
143 * Local variables defined in this file and initialized at startup.
144 */
145
146 static Tcl_Mutex *listLockPtr;
147 static Tcl_Mutex *objLockPtr;
148 static Cache sharedCache;
149 static Cache *sharedPtr = &sharedCache;
150 static Cache *firstCachePtr = &sharedCache;
151
152 #if defined(HAVE_FAST_TSD)
153 static __thread Cache *tcachePtr;
154
155 # define GETCACHE(cachePtr) \
156 do { \
157 if (!tcachePtr) { \
158 tcachePtr = GetCache(); \
159 } \
160 (cachePtr) = tcachePtr; \
161 } while (0)
162 #else
163 # define GETCACHE(cachePtr) \
164 do { \
165 (cachePtr) = TclpGetAllocCache(); \
166 if ((cachePtr) == NULL) { \
167 (cachePtr) = GetCache(); \
168 } \
169 } while (0)
170 #endif
171
172 /*
173 *----------------------------------------------------------------------
174 *
175 * GetCache ---
176 *
177 * Gets per-thread memory cache, allocating it if necessary.
178 *
179 * Results:
180 * Pointer to cache.
181 *
182 * Side effects:
183 * None.
184 *
185 *----------------------------------------------------------------------
186 */
187
188 static Cache *
189 GetCache(void)
190 {
191 Cache *cachePtr;
192
193 /*
194 * Check for first-time initialization.
195 */
196
197 if (listLockPtr == NULL) {
198 Tcl_Mutex *initLockPtr;
199 unsigned int i;
200
201 initLockPtr = Tcl_GetAllocMutex();
202 Tcl_MutexLock(initLockPtr);
203 if (listLockPtr == NULL) {
204 listLockPtr = TclpNewAllocMutex();
205 objLockPtr = TclpNewAllocMutex();
206 for (i = 0; i < NBUCKETS; ++i) {
207 bucketInfo[i].blockSize = MINALLOC << i;
208 bucketInfo[i].maxBlocks = 1 << (NBUCKETS - 1 - i);
209 bucketInfo[i].numMove = i < NBUCKETS - 1 ?
210 1 << (NBUCKETS - 2 - i) : 1;
211 bucketInfo[i].lockPtr = TclpNewAllocMutex();
212 }
213 }
214 Tcl_MutexUnlock(initLockPtr);
215 }
216
217 /*
218 * Get this thread's cache, allocating if necessary.
219 */
220
221 cachePtr = TclpGetAllocCache();
222 if (cachePtr == NULL) {
223 cachePtr = TclpSysAlloc(sizeof(Cache), 0);
224 if (cachePtr == NULL) {
225 Tcl_Panic("alloc: could not allocate new cache");
226 }
227 memset(cachePtr, 0, sizeof(Cache));
228 Tcl_MutexLock(listLockPtr);
229 cachePtr->nextPtr = firstCachePtr;
230 firstCachePtr = cachePtr;
231 Tcl_MutexUnlock(listLockPtr);
232 cachePtr->owner = Tcl_GetCurrentThread();
233 TclpSetAllocCache(cachePtr);
234 }
235 return cachePtr;
236 }
237
238 /*
239 *----------------------------------------------------------------------
240 *
241 * TclFreeAllocCache --
242 *
243 * Flush and delete a cache, removing from list of caches.
244 *
245 * Results:
246 * None.
247 *
248 * Side effects:
249 * None.
250 *
251 *----------------------------------------------------------------------
252 */
253
254 void
255 TclFreeAllocCache(
256 void *arg)
257 {
258 Cache *cachePtr = arg;
259 Cache **nextPtrPtr;
260 unsigned int bucket;
261
262 /*
263 * Flush blocks.
264 */
265
266 for (bucket = 0; bucket < NBUCKETS; ++bucket) {
267 if (cachePtr->buckets[bucket].numFree > 0) {
268 PutBlocks(cachePtr, bucket, cachePtr->buckets[bucket].numFree);
269 }
270 }
271
272 /*
273 * Flush objs.
274 */
275
276 if (cachePtr->numObjects > 0) {
277 PutObjs(cachePtr, cachePtr->numObjects);
278 }
279
280 /*
281 * Remove from pool list.
282 */
283
284 Tcl_MutexLock(listLockPtr);
285 nextPtrPtr = &firstCachePtr;
286 while (*nextPtrPtr != cachePtr) {
287 nextPtrPtr = &(*nextPtrPtr)->nextPtr;
288 }
289 *nextPtrPtr = cachePtr->nextPtr;
290 cachePtr->nextPtr = NULL;
291 Tcl_MutexUnlock(listLockPtr);
292 TclpSysFree(cachePtr);
293 }
294
295 /*
296 *----------------------------------------------------------------------
297 *
298 * TclpAlloc --
299 *
300 * Allocate memory.
301 *
302 * Results:
303 * Pointer to memory just beyond Block pointer.
304 *
305 * Side effects:
306 * May allocate more blocks for a bucket.
307 *
308 *----------------------------------------------------------------------
309 */
310
311 char *
312 TclpAlloc(
313 unsigned int reqSize)
314 {
315 Cache *cachePtr;
316 Block *blockPtr;
317 int bucket;
318 size_t size;
319
320 #ifndef __LP64__
321 if (sizeof(int) >= sizeof(size_t)) {
322 /* An unsigned int overflow can also be a size_t overflow */
323 const size_t zero = 0;
324 const size_t max = ~zero;
325
326 if (((size_t) reqSize) > max - sizeof(Block) - RCHECK) {
327 /* Requested allocation exceeds memory */
328 return NULL;
329 }
330 }
331 #endif
332
333 GETCACHE(cachePtr);
334
335 /*
336 * Increment the requested size to include room for the Block structure.
337 * Call TclpSysAlloc() directly if the required amount is greater than the
338 * largest block, otherwise pop the smallest block large enough,
339 * allocating more blocks if necessary.
340 */
341
342 blockPtr = NULL;
343 size = reqSize + sizeof(Block);
344 #if RCHECK
345 size++;
346 #endif
347 if (size > MAXALLOC) {
348 bucket = NBUCKETS;
349 blockPtr = TclpSysAlloc(size, 0);
350 if (blockPtr != NULL) {
351 cachePtr->totalAssigned += reqSize;
352 }
353 } else {
354 bucket = 0;
355 while (bucketInfo[bucket].blockSize < size) {
356 bucket++;
357 }
358 if (cachePtr->buckets[bucket].numFree || GetBlocks(cachePtr, bucket)) {
359 blockPtr = cachePtr->buckets[bucket].firstPtr;
360 cachePtr->buckets[bucket].firstPtr = blockPtr->nextBlock;
361 cachePtr->buckets[bucket].numFree--;
362 cachePtr->buckets[bucket].numRemoves++;
363 cachePtr->buckets[bucket].totalAssigned += reqSize;
364 }
365 }
366 if (blockPtr == NULL) {
367 return NULL;
368 }
369 return Block2Ptr(blockPtr, bucket, reqSize);
370 }
371
372 /*
373 *----------------------------------------------------------------------
374 *
375 * TclpFree --
376 *
377 * Return blocks to the thread block cache.
378 *
379 * Results:
380 * None.
381 *
382 * Side effects:
383 * May move blocks to shared cache.
384 *
385 *----------------------------------------------------------------------
386 */
387
388 void
389 TclpFree(
390 char *ptr)
391 {
392 Cache *cachePtr;
393 Block *blockPtr;
394 int bucket;
395
396 if (ptr == NULL) {
397 return;
398 }
399
400 GETCACHE(cachePtr);
401
402 /*
403 * Get the block back from the user pointer and call system free directly
404 * for large blocks. Otherwise, push the block back on the bucket and move
405 * blocks to the shared cache if there are now too many free.
406 */
407
408 blockPtr = Ptr2Block(ptr);
409 bucket = blockPtr->sourceBucket;
410 if (bucket == NBUCKETS) {
411 cachePtr->totalAssigned -= blockPtr->blockReqSize;
412 TclpSysFree(blockPtr);
413 return;
414 }
415
416 cachePtr->buckets[bucket].totalAssigned -= blockPtr->blockReqSize;
417 blockPtr->nextBlock = cachePtr->buckets[bucket].firstPtr;
418 cachePtr->buckets[bucket].firstPtr = blockPtr;
419 if (cachePtr->buckets[bucket].numFree == 0) {
420 cachePtr->buckets[bucket].lastPtr = blockPtr;
421 }
422 cachePtr->buckets[bucket].numFree++;
423 cachePtr->buckets[bucket].numInserts++;
424
425 if (cachePtr != sharedPtr &&
426 cachePtr->buckets[bucket].numFree > bucketInfo[bucket].maxBlocks) {
427 PutBlocks(cachePtr, bucket, bucketInfo[bucket].numMove);
428 }
429 }
430
431 /*
432 *----------------------------------------------------------------------
433 *
434 * TclpRealloc --
435 *
436 * Re-allocate memory to a larger or smaller size.
437 *
438 * Results:
439 * Pointer to memory just beyond Block pointer.
440 *
441 * Side effects:
442 * Previous memory, if any, may be freed.
443 *
444 *----------------------------------------------------------------------
445 */
446
447 char *
448 TclpRealloc(
449 char *ptr,
450 unsigned int reqSize)
451 {
452 Cache *cachePtr;
453 Block *blockPtr;
454 void *newPtr;
455 size_t size, min;
456 int bucket;
457
458 if (ptr == NULL) {
459 return TclpAlloc(reqSize);
460 }
461
462 #ifndef __LP64__
463 if (sizeof(int) >= sizeof(size_t)) {
464 /* An unsigned int overflow can also be a size_t overflow */
465 const size_t zero = 0;
466 const size_t max = ~zero;
467
468 if (((size_t) reqSize) > max - sizeof(Block) - RCHECK) {
469 /* Requested allocation exceeds memory */
470 return NULL;
471 }
472 }
473 #endif
474
475 GETCACHE(cachePtr);
476
477 /*
478 * If the block is not a system block and fits in place, simply return the
479 * existing pointer. Otherwise, if the block is a system block and the new
480 * size would also require a system block, call TclpSysRealloc() directly.
481 */
482
483 blockPtr = Ptr2Block(ptr);
484 size = reqSize + sizeof(Block);
485 #if RCHECK
486 size++;
487 #endif
488 bucket = blockPtr->sourceBucket;
489 if (bucket != NBUCKETS) {
490 if (bucket > 0) {
491 min = bucketInfo[bucket-1].blockSize;
492 } else {
493 min = 0;
494 }
495 if (size > min && size <= bucketInfo[bucket].blockSize) {
496 cachePtr->buckets[bucket].totalAssigned -= blockPtr->blockReqSize;
497 cachePtr->buckets[bucket].totalAssigned += reqSize;
498 return Block2Ptr(blockPtr, bucket, reqSize);
499 }
500 } else if (size > MAXALLOC) {
501 cachePtr->totalAssigned -= blockPtr->blockReqSize;
502 cachePtr->totalAssigned += reqSize;
503 blockPtr = TclpSysRealloc(blockPtr, size);
504 if (blockPtr == NULL) {
505 return NULL;
506 }
507 return Block2Ptr(blockPtr, NBUCKETS, reqSize);
508 }
509
510 /*
511 * Finally, perform an expensive malloc/copy/free.
512 */
513
514 newPtr = TclpAlloc(reqSize);
515 if (newPtr != NULL) {
516 if (reqSize > blockPtr->blockReqSize) {
517 reqSize = blockPtr->blockReqSize;
518 }
519 memcpy(newPtr, ptr, reqSize);
520 TclpFree(ptr);
521 }
522 return newPtr;
523 }
524
525 /*
526 *----------------------------------------------------------------------
527 *
528 * TclThreadAllocObj --
529 *
530 * Allocate a Tcl_Obj from the per-thread cache.
531 *
532 * Results:
533 * Pointer to uninitialized Tcl_Obj.
534 *
535 * Side effects:
536 * May move Tcl_Obj's from shared cached or allocate new Tcl_Obj's if
537 * list is empty.
538 *
539 * Note:
540 * If this code is updated, the changes need to be reflected in the macro
541 * TclAllocObjStorageEx() defined in tclInt.h
542 *
543 *----------------------------------------------------------------------
544 */
545
546 Tcl_Obj *
547 TclThreadAllocObj(void)
548 {
549 Cache *cachePtr;
550 Tcl_Obj *objPtr;
551
552 GETCACHE(cachePtr);
553
554 /*
555 * Get this thread's obj list structure and move or allocate new objs if
556 * necessary.
557 */
558
559 if (cachePtr->numObjects == 0) {
560 int numMove;
561
562 Tcl_MutexLock(objLockPtr);
563 numMove = sharedPtr->numObjects;
564 if (numMove > 0) {
565 if (numMove > NOBJALLOC) {
566 numMove = NOBJALLOC;
567 }
568 MoveObjs(sharedPtr, cachePtr, numMove);
569 }
570 Tcl_MutexUnlock(objLockPtr);
571 if (cachePtr->numObjects == 0) {
572 Tcl_Obj *newObjsPtr;
573
574 cachePtr->numObjects = numMove = NOBJALLOC;
575 newObjsPtr = TclpSysAlloc(sizeof(Tcl_Obj) * numMove, 0);
576 if (newObjsPtr == NULL) {
577 Tcl_Panic("alloc: could not allocate %d new objects", numMove);
578 }
579 cachePtr->lastPtr = newObjsPtr + numMove - 1;
580 objPtr = cachePtr->firstObjPtr; /* NULL */
581 while (--numMove >= 0) {
582 newObjsPtr[numMove].internalRep.twoPtrValue.ptr1 = objPtr;
583 objPtr = newObjsPtr + numMove;
584 }
585 cachePtr->firstObjPtr = newObjsPtr;
586 }
587 }
588
589 /*
590 * Pop the first object.
591 */
592
593 objPtr = cachePtr->firstObjPtr;
594 cachePtr->firstObjPtr = objPtr->internalRep.twoPtrValue.ptr1;
595 cachePtr->numObjects--;
596 return objPtr;
597 }
598
599 /*
600 *----------------------------------------------------------------------
601 *
602 * TclThreadFreeObj --
603 *
604 * Return a free Tcl_Obj to the per-thread cache.
605 *
606 * Results:
607 * None.
608 *
609 * Side effects:
610 * May move free Tcl_Obj's to shared list upon hitting high water mark.
611 *
612 * Note:
613 * If this code is updated, the changes need to be reflected in the macro
614 * TclAllocObjStorageEx() defined in tclInt.h
615 *
616 *----------------------------------------------------------------------
617 */
618
619 void
620 TclThreadFreeObj(
621 Tcl_Obj *objPtr)
622 {
623 Cache *cachePtr;
624
625 GETCACHE(cachePtr);
626
627 /*
628 * Get this thread's list and push on the free Tcl_Obj.
629 */
630
631 objPtr->internalRep.twoPtrValue.ptr1 = cachePtr->firstObjPtr;
632 cachePtr->firstObjPtr = objPtr;
633 if (cachePtr->numObjects == 0) {
634 cachePtr->lastPtr = objPtr;
635 }
636 cachePtr->numObjects++;
637
638 /*
639 * If the number of free objects has exceeded the high water mark, move
640 * some blocks to the shared list.
641 */
642
643 if (cachePtr->numObjects > NOBJHIGH) {
644 PutObjs(cachePtr, NOBJALLOC);
645 }
646 }
647
648 /*
649 *----------------------------------------------------------------------
650 *
651 * Tcl_GetMemoryInfo --
652 *
653 * Return a list-of-lists of memory stats.
654 *
655 * Results:
656 * None.
657 *
658 * Side effects:
659 * List appended to given dstring.
660 *
661 *----------------------------------------------------------------------
662 */
663
664 void
665 Tcl_GetMemoryInfo(
666 Tcl_DString *dsPtr)
667 {
668 Cache *cachePtr;
669 char buf[200];
670 unsigned int n;
671
672 Tcl_MutexLock(listLockPtr);
673 cachePtr = firstCachePtr;
674 while (cachePtr != NULL) {
675 Tcl_DStringStartSublist(dsPtr);
676 if (cachePtr == sharedPtr) {
677 Tcl_DStringAppendElement(dsPtr, "shared");
678 } else {
679 snprintf(buf, sizeof(buf), "thread%p", cachePtr->owner);
680 Tcl_DStringAppendElement(dsPtr, buf);
681 }
682 for (n = 0; n < NBUCKETS; ++n) {
683 snprintf(buf, sizeof(buf), "%lu %ld %ld %ld %ld %ld %ld",
684 (unsigned long) bucketInfo[n].blockSize,
685 cachePtr->buckets[n].numFree,
686 cachePtr->buckets[n].numRemoves,
687 cachePtr->buckets[n].numInserts,
688 cachePtr->buckets[n].totalAssigned,
689 cachePtr->buckets[n].numLocks,
690 cachePtr->buckets[n].numWaits);
691 Tcl_DStringAppendElement(dsPtr, buf);
692 }
693 Tcl_DStringEndSublist(dsPtr);
694 cachePtr = cachePtr->nextPtr;
695 }
696 Tcl_MutexUnlock(listLockPtr);
697 }
698
699 /*
700 *----------------------------------------------------------------------
701 *
702 * MoveObjs --
703 *
704 * Move Tcl_Obj's between caches.
705 *
706 * Results:
707 * None.
708 *
709 * Side effects:
710 * None.
711 *
712 *----------------------------------------------------------------------
713 */
714
715 static void
716 MoveObjs(
717 Cache *fromPtr,
718 Cache *toPtr,
719 int numMove)
720 {
721 Tcl_Obj *objPtr = fromPtr->firstObjPtr;
722 Tcl_Obj *fromFirstObjPtr = objPtr;
723
724 toPtr->numObjects += numMove;
725 fromPtr->numObjects -= numMove;
726
727 /*
728 * Find the last object to be moved; set the next one (the first one not
729 * to be moved) as the first object in the 'from' cache.
730 */
731
732 while (--numMove) {
733 objPtr = objPtr->internalRep.twoPtrValue.ptr1;
734 }
735 fromPtr->firstObjPtr = objPtr->internalRep.twoPtrValue.ptr1;
736
737 /*
738 * Move all objects as a block - they are already linked to each other, we
739 * just have to update the first and last.
740 */
741
742 toPtr->lastPtr = objPtr;
743 objPtr->internalRep.twoPtrValue.ptr1 = toPtr->firstObjPtr; /* NULL */
744 toPtr->firstObjPtr = fromFirstObjPtr;
745 }
746
747 /*
748 *----------------------------------------------------------------------
749 *
750 * PutObjs --
751 *
752 * Move Tcl_Obj's from thread cache to shared cache.
753 *
754 * Results:
755 * None.
756 *
757 * Side effects:
758 * None.
759 *
760 *----------------------------------------------------------------------
761 */
762
763 static void
764 PutObjs(
765 Cache *fromPtr,
766 int numMove)
767 {
768 int keep = fromPtr->numObjects - numMove;
769 Tcl_Obj *firstPtr, *lastPtr = NULL;
770
771 fromPtr->numObjects = keep;
772 firstPtr = fromPtr->firstObjPtr;
773 if (keep == 0) {
774 fromPtr->firstObjPtr = NULL;
775 } else {
776 do {
777 lastPtr = firstPtr;
778 firstPtr = firstPtr->internalRep.twoPtrValue.ptr1;
779 } while (--keep > 0);
780 lastPtr->internalRep.twoPtrValue.ptr1 = NULL;
781 }
782
783 /*
784 * Move all objects as a block - they are already linked to each other, we
785 * just have to update the first and last.
786 */
787
788 Tcl_MutexLock(objLockPtr);
789 fromPtr->lastPtr->internalRep.twoPtrValue.ptr1 = sharedPtr->firstObjPtr;
790 sharedPtr->firstObjPtr = firstPtr;
791 if (sharedPtr->numObjects == 0) {
792 sharedPtr->lastPtr = fromPtr->lastPtr;
793 }
794 sharedPtr->numObjects += numMove;
795 Tcl_MutexUnlock(objLockPtr);
796
797 fromPtr->lastPtr = lastPtr;
798 }
799
800 /*
801 *----------------------------------------------------------------------
802 *
803 * Block2Ptr, Ptr2Block --
804 *
805 * Convert between internal blocks and user pointers.
806 *
807 * Results:
808 * User pointer or internal block.
809 *
810 * Side effects:
811 * Invalid blocks will abort the server.
812 *
813 *----------------------------------------------------------------------
814 */
815
816 static char *
817 Block2Ptr(
818 Block *blockPtr,
819 int bucket,
820 unsigned int reqSize)
821 {
822 void *ptr;
823
824 blockPtr->magicNum1 = blockPtr->magicNum2 = MAGIC;
825 blockPtr->sourceBucket = bucket;
826 blockPtr->blockReqSize = reqSize;
827 ptr = ((void *) (blockPtr + 1));
828 #if RCHECK
829 ((unsigned char *)(ptr))[reqSize] = MAGIC;
830 #endif
831 return (char *) ptr;
832 }
833
834 static Block *
835 Ptr2Block(
836 char *ptr)
837 {
838 Block *blockPtr;
839
840 blockPtr = (((Block *) ptr) - 1);
841 if (blockPtr->magicNum1 != MAGIC || blockPtr->magicNum2 != MAGIC) {
842 Tcl_Panic("alloc: invalid block: %p: %x %x",
843 blockPtr, blockPtr->magicNum1, blockPtr->magicNum2);
844 }
845 #if RCHECK
846 if (((unsigned char *) ptr)[blockPtr->blockReqSize] != MAGIC) {
847 Tcl_Panic("alloc: invalid block: %p: %x %x %x",
848 blockPtr, blockPtr->magicNum1, blockPtr->magicNum2,
849 ((unsigned char *) ptr)[blockPtr->blockReqSize]);
850 }
851 #endif
852 return blockPtr;
853 }
854
855 /*
856 *----------------------------------------------------------------------
857 *
858 * LockBucket, UnlockBucket --
859 *
860 * Set/unset the lock to access a bucket in the shared cache.
861 *
862 * Results:
863 * None.
864 *
865 * Side effects:
866 * Lock activity and contention are monitored globally and on a per-cache
867 * basis.
868 *
869 *----------------------------------------------------------------------
870 */
871
872 static void
873 LockBucket(
874 Cache *cachePtr,
875 int bucket)
876 {
877 Tcl_MutexLock(bucketInfo[bucket].lockPtr);
878 cachePtr->buckets[bucket].numLocks++;
879 sharedPtr->buckets[bucket].numLocks++;
880 }
881
882 static void
883 UnlockBucket(
884 Cache *cachePtr,
885 int bucket)
886 {
887 Tcl_MutexUnlock(bucketInfo[bucket].lockPtr);
888 }
889
890 /*
891 *----------------------------------------------------------------------
892 *
893 * PutBlocks --
894 *
895 * Return unused blocks to the shared cache.
896 *
897 * Results:
898 * None.
899 *
900 * Side effects:
901 * None.
902 *
903 *----------------------------------------------------------------------
904 */
905
906 static void
907 PutBlocks(
908 Cache *cachePtr,
909 int bucket,
910 int numMove)
911 {
912 /*
913 * We have numFree. Want to shed numMove. So compute how many
914 * Blocks to keep.
915 */
916
917 int keep = cachePtr->buckets[bucket].numFree - numMove;
918 Block *lastPtr = NULL, *firstPtr;
919
920 cachePtr->buckets[bucket].numFree = keep;
921 firstPtr = cachePtr->buckets[bucket].firstPtr;
922 if (keep == 0) {
923 cachePtr->buckets[bucket].firstPtr = NULL;
924 } else {
925 do {
926 lastPtr = firstPtr;
927 firstPtr = firstPtr->nextBlock;
928 } while (--keep > 0);
929 lastPtr->nextBlock = NULL;
930 }
931
932 /*
933 * Aquire the lock and place the list of blocks at the front of the shared
934 * cache bucket.
935 */
936
937 LockBucket(cachePtr, bucket);
938 cachePtr->buckets[bucket].lastPtr->nextBlock
939 = sharedPtr->buckets[bucket].firstPtr;
940 sharedPtr->buckets[bucket].firstPtr = firstPtr;
941 if (sharedPtr->buckets[bucket].numFree == 0) {
942 sharedPtr->buckets[bucket].lastPtr
943 = cachePtr->buckets[bucket].lastPtr;
944 }
945 sharedPtr->buckets[bucket].numFree += numMove;
946 UnlockBucket(cachePtr, bucket);
947
948 cachePtr->buckets[bucket].lastPtr = lastPtr;
949 }
950
951 /*
952 *----------------------------------------------------------------------
953 *
954 * GetBlocks --
955 *
956 * Get more blocks for a bucket.
957 *
958 * Results:
959 * 1 if blocks where allocated, 0 otherwise.
960 *
961 * Side effects:
962 * Cache may be filled with available blocks.
963 *
964 *----------------------------------------------------------------------
965 */
966
967 static int
968 GetBlocks(
969 Cache *cachePtr,
970 int bucket)
971 {
972 Block *blockPtr;
973 int n;
974
975 /*
976 * First, attempt to move blocks from the shared cache. Note the
977 * potentially dirty read of numFree before acquiring the lock which is a
978 * slight performance enhancement. The value is verified after the lock is
979 * actually acquired.
980 */
981
982 if (cachePtr != sharedPtr && sharedPtr->buckets[bucket].numFree > 0) {
983 LockBucket(cachePtr, bucket);
984 if (sharedPtr->buckets[bucket].numFree > 0) {
985
986 /*
987 * Either move the entire list or walk the list to find the last
988 * block to move.
989 */
990
991 n = bucketInfo[bucket].numMove;
992 if (n >= sharedPtr->buckets[bucket].numFree) {
993 cachePtr->buckets[bucket].firstPtr =
994 sharedPtr->buckets[bucket].firstPtr;
995 cachePtr->buckets[bucket].lastPtr =
996 sharedPtr->buckets[bucket].lastPtr;
997 cachePtr->buckets[bucket].numFree =
998 sharedPtr->buckets[bucket].numFree;
999 sharedPtr->buckets[bucket].firstPtr = NULL;
1000 sharedPtr->buckets[bucket].numFree = 0;
1001 } else {
1002 blockPtr = sharedPtr->buckets[bucket].firstPtr;
1003 cachePtr->buckets[bucket].firstPtr = blockPtr;
1004 sharedPtr->buckets[bucket].numFree -= n;
1005 cachePtr->buckets[bucket].numFree = n;
1006 while (--n > 0) {
1007 blockPtr = blockPtr->nextBlock;
1008 }
1009 sharedPtr->buckets[bucket].firstPtr = blockPtr->nextBlock;
1010 cachePtr->buckets[bucket].lastPtr = blockPtr;
1011 blockPtr->nextBlock = NULL;
1012 }
1013 }
1014 UnlockBucket(cachePtr, bucket);
1015 }
1016
1017 if (cachePtr->buckets[bucket].numFree == 0) {
1018 size_t size;
1019
1020 /*
1021 * If no blocks could be moved from shared, first look for a larger
1022 * block in this cache to split up.
1023 */
1024
1025 blockPtr = NULL;
1026 n = NBUCKETS;
1027 size = 0; /* lint */
1028 while (--n > bucket) {
1029 if (cachePtr->buckets[n].numFree > 0) {
1030 size = bucketInfo[n].blockSize;
1031 blockPtr = cachePtr->buckets[n].firstPtr;
1032 cachePtr->buckets[n].firstPtr = blockPtr->nextBlock;
1033 cachePtr->buckets[n].numFree--;
1034 break;
1035 }
1036 }
1037
1038 /*
1039 * Otherwise, allocate a big new block directly.
1040 */
1041
1042 if (blockPtr == NULL) {
1043 size = MAXALLOC;
1044 blockPtr = TclpSysAlloc(size, 0);
1045 if (blockPtr == NULL) {
1046 return 0;
1047 }
1048 }
1049
1050 /*
1051 * Split the larger block into smaller blocks for this bucket.
1052 */
1053
1054 n = size / bucketInfo[bucket].blockSize;
1055 cachePtr->buckets[bucket].numFree = n;
1056 cachePtr->buckets[bucket].firstPtr = blockPtr;
1057 while (--n > 0) {
1058 blockPtr->nextBlock = (Block *)
1059 ((char *) blockPtr + bucketInfo[bucket].blockSize);
1060 blockPtr = blockPtr->nextBlock;
1061 }
1062 cachePtr->buckets[bucket].lastPtr = blockPtr;
1063 blockPtr->nextBlock = NULL;
1064 }
1065 return 1;
1066 }
1067
1068 /*
1069 *----------------------------------------------------------------------
1070 *
1071 * TclFinalizeThreadAlloc --
1072 *
1073 * This procedure is used to destroy all private resources used in this
1074 * file.
1075 *
1076 * Results:
1077 * None.
1078 *
1079 * Side effects:
1080 * None.
1081 *
1082 *----------------------------------------------------------------------
1083 */
1084
1085 void
1086 TclFinalizeThreadAlloc(void)
1087 {
1088 unsigned int i;
1089
1090 for (i = 0; i < NBUCKETS; ++i) {
1091 TclpFreeAllocMutex(bucketInfo[i].lockPtr);
1092 bucketInfo[i].lockPtr = NULL;
1093 }
1094
1095 TclpFreeAllocMutex(objLockPtr);
1096 objLockPtr = NULL;
1097
1098 TclpFreeAllocMutex(listLockPtr);
1099 listLockPtr = NULL;
1100
1101 TclpFreeAllocCache(NULL);
1102 }
1103
1104 /*
1105 *----------------------------------------------------------------------
1106 *
1107 * TclFinalizeThreadAllocThread --
1108 *
1109 * This procedure is used to destroy single thread private resources
1110 * defined in this file. Called either during Tcl_FinalizeThread() or
1111 * Tcl_Finalize().
1112 *
1113 * Results:
1114 * None.
1115 *
1116 * Side effects:
1117 * None.
1118 *
1119 *----------------------------------------------------------------------
1120 */
1121
1122 void
1123 TclFinalizeThreadAllocThread(void)
1124 {
1125 Cache *cachePtr = TclpGetAllocCache();
1126 if (cachePtr != NULL) {
1127 TclpFreeAllocCache(cachePtr);
1128 }
1129 }
1130
1131 #else /* !(TCL_THREADS && USE_THREAD_ALLOC) */
1132 /*
1133 *----------------------------------------------------------------------
1134 *
1135 * Tcl_GetMemoryInfo --
1136 *
1137 * Return a list-of-lists of memory stats.
1138 *
1139 * Results:
1140 * None.
1141 *
1142 * Side effects:
1143 * List appended to given dstring.
1144 *
1145 *----------------------------------------------------------------------
1146 */
1147
1148 void
1149 Tcl_GetMemoryInfo(
1150 Tcl_DString *dsPtr)
1151 {
1152 Tcl_Panic("Tcl_GetMemoryInfo called when threaded memory allocator not in use");
1153 }
1154
1155 /*
1156 *----------------------------------------------------------------------
1157 *
1158 * TclFinalizeThreadAlloc --
1159 *
1160 * This procedure is used to destroy all private resources used in this
1161 * file.
1162 *
1163 * Results:
1164 * None.
1165 *
1166 * Side effects:
1167 * None.
1168 *
1169 *----------------------------------------------------------------------
1170 */
1171
1172 void
1173 TclFinalizeThreadAlloc(void)
1174 {
1175 Tcl_Panic("TclFinalizeThreadAlloc called when threaded memory allocator not in use");
1176 }
1177 #endif /* TCL_THREADS && USE_THREAD_ALLOC */
1178
1179 /*
1180 * Local Variables:
1181 * mode: c
1182 * c-basic-offset: 4
1183 * fill-column: 78
1184 * End:
1185 */