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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
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
695
696
697
698
699
700
701
702
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
745
746
747
748
749
750
751
752
753
754
755
756
757
758
|
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
|
-
+
-
-
-
+
-
-
+
-
-
-
+
+
+
-
-
+
+
+
-
-
+
+
-
-
-
+
-
-
-
+
+
+
+
+
-
-
-
-
-
-
+
+
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
-
+
-
+
+
+
+
+
+
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
-
-
-
+
+
+
-
-
-
-
-
+
+
+
+
+
+
+
+
-
+
-
-
+
+
+
+
+
-
-
-
-
-
-
+
+
+
+
+
+
+
-
-
-
-
+
+
+
-
+
-
-
-
+
-
-
+
+
+
-
-
-
+
+
-
-
+
+
+
+
+
-
-
-
-
+
+
+
-
-
-
-
-
+
+
+
+
-
-
-
+
+
+
-
-
+
-
-
-
+
+
+
+
+
-
-
-
+
+
+
+
-
-
-
-
-
-
-
-
-
+
+
+
+
+
-
-
-
-
+
+
+
+
+
-
+
+
+
+
+
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
-
+
-
-
-
-
+
+
-
-
-
+
-
-
-
+
-
+
-
-
-
+
-
-
-
-
-
+
-
-
+
-
-
-
-
-
+
+
-
-
-
+
-
-
-
-
-
-
-
-
-
+
-
+
-
+
-
-
+
+
+
-
+
-
-
-
-
-
+
+
-
-
+
-
-
-
+
+
+
-
+
+
+
-
-
+
-
-
-
+
-
-
-
+
+
-
-
-
+
-
-
+
+
+
+
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
-
-
-
+
-
-
-
+
+
+
+
-
-
-
-
-
-
+
+
+
-
-
+
-
-
-
-
+
+
-
-
+
+
-
-
+
-
-
+
+
+
-
+
+
-
+
+
+
+
+
+
+
+
-
-
-
+
+
+
+
+
-
-
+
+
-
-
-
-
+
+
+
+
+
+
-
-
-
-
-
+
+
+
+
+
-
-
-
-
-
+
+
-
-
-
+
-
+
-
-
-
+
-
+
-
-
-
+
+
+
-
+
-
-
-
-
-
+
+
-
-
-
-
-
+
+
+
+
-
-
-
+
-
+
-
-
-
-
-
+
-
-
-
-
-
+
+
+
+
+
+
-
-
+
+
-
+
+
-
-
+
+
+
-
-
-
+
-
+
-
+
-
-
-
-
-
-
-
-
+
-
-
-
-
+
-
-
-
+
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
+
-
-
-
-
-
+
-
-
-
-
-
-
-
+
-
-
-
-
+
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
+
-
-
-
-
+
+
-
-
-
+
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
+
-
-
+
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|
/*
* tclAlloc.c --
*
* This is a very fast storage allocator. It allocates blocks of a small
* This is the generic part of the Tcl allocator. It handles the
* number of different sizes, and keeps free lists of each size. Blocks
* that don't exactly fit are passed up to the next larger size. Blocks
* over a certain size are directly allocated from the system.
* freeObjLists and defines which main allocator will be used.
*
* Copyright (c) 1983 Regents of the University of California.
* Copyright (c) 1996-1997 Sun Microsystems, Inc.
* Copyright (c) 2013 by Miguel Sofer. All rights reserved.
* Copyright (c) 1998-1999 by Scriptics Corporation.
*
* Portions contributed by Chris Kingsley, Jack Jansen and Ray Johnson.
*
* See the file "license.terms" for information on usage and redistribution of
* this file, and for a DISCLAIMER OF ALL WARRANTIES.
*/
#include "tclInt.h"
#include "tclAlloc.h"
/*
* Windows and Unix use an alternative allocator when building with threads
* that has significantly reduced lock contention.
* Parameters for the per-thread Tcl_Obj cache:
* - if >NOBJHIGH free objects, move some to the shared cache
* - if no objects are available, create NOBJALLOC of them
*/
#include "tclInt.h"
#if !defined(TCL_THREADS) || !defined(USE_THREAD_ALLOC)
#define NOBJHIGH 1200
#define NOBJALLOC ((NOBJHIGH*2)/3)
#if USE_TCLALLOC
/*
* We should really make use of AC_CHECK_TYPE(caddr_t) here, but it can wait
* until Tcl uses config.h properly.
* The Tcl_Obj per-thread cache.
*/
#if defined(_MSC_VER) || defined(__MINGW32__) || defined(__BORLANDC__)
typedef unsigned long caddr_t;
#endif
typedef struct Cache {
Tcl_Obj *firstObjPtr; /* List of free objects for thread */
int numObjects; /* Number of objects for thread */
void *allocCachePtr;
} Cache;
/*
* The overhead on a block is at least 8 bytes. When free, this space contains
* a pointer to the next free block, and the bottom two bits must be zero.
* When in use, the first byte is set to MAGIC, and the second byte is the
* size index. The remaining bytes are for alignment. If range checking is
* enabled then a second word holds the size of the requested block, less 1,
static Cache sharedCache;
#define sharedPtr (&sharedCache)
* rounded up to a multiple of sizeof(RMAGIC). The order of elements is
* critical: ov.magic must overlay the low order bits of ov.next, and ov.magic
* can not be a valid ov.next bit pattern.
*/
union overhead {
union overhead *next; /* when free */
unsigned char padding[TCL_ALLOCALIGN]; /* align struct to TCL_ALLOCALIGN bytes */
struct {
unsigned char magic0; /* magic number */
unsigned char index; /* bucket # */
#if defined(TCL_THREADS)
static Tcl_Mutex *objLockPtr;
static Cache * GetCache(void);
static void MoveObjs(Cache *fromPtr, Cache *toPtr, int numMove);
unsigned char unused; /* unused */
unsigned char magic1; /* other magic number */
#ifndef NDEBUG
unsigned short rmagic; /* range magic number */
unsigned long size; /* actual block size */
unsigned short unused2; /* padding to 8-byte align */
#endif
} ovu;
#define overMagic0 ovu.magic0
#define overMagic1 ovu.magic1
#define bucketIndex ovu.index
#define rangeCheckMagic ovu.rmagic
#define realBlockSize ovu.size
};
#if defined(HAVE_FAST_TSD)
static __thread Cache *tcachePtr;
#define MAGIC 0xef /* magic # on accounting info */
# define GETCACHE(cachePtr) \
#define RMAGIC 0x5555 /* magic # on range info */
do { \
if (!tcachePtr) { \
tcachePtr = GetCache(); \
} \
(cachePtr) = tcachePtr; \
} while (0)
#ifndef NDEBUG
#define RSLOP sizeof(unsigned short)
#else
#define RSLOP 0
#endif
#else /* THREADS, not HAVE_FAST_TSD */
# define GETCACHE(cachePtr) \
do { \
(cachePtr) = TclpGetAllocCache(); \
if ((cachePtr) == NULL) { \
(cachePtr) = GetCache(); \
} \
} while (0)
#endif /* FAST TSD */
#else /* NOT THREADS */
#define OVERHEAD (sizeof(union overhead) + RSLOP)
/*
#define GETCACHE(cachePtr) \
(cachePtr) = (&sharedCache)
#endif /* THREADS */
* Macro to make it easier to refer to the end-of-block guard magic.
*/
#define BLOCK_END(overPtr) \
(*(unsigned short *)((caddr_t)((overPtr) + 1) + (overPtr)->realBlockSize))
/*
*----------------------------------------------------------------------
*
* GetCache ---
*
* Gets per-thread memory cache, allocating it if necessary.
*
* Results:
* nextf[i] is the pointer to the next free block of size 2^(i+3). The
* Pointer to cache.
* smallest allocatable block is MINBLOCK bytes. The overhead information
* precedes the data area returned to the user.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
#define MINBLOCK ((sizeof(union overhead) + (TCL_ALLOCALIGN-1)) & ~(TCL_ALLOCALIGN-1))
#define NBUCKETS (13 - (MINBLOCK >> 4))
#define MAXMALLOC (1<<(NBUCKETS+2))
static union overhead *nextf[NBUCKETS];
/*
#if defined(TCL_THREADS)
static Cache *
GetCache(void)
{
Cache *cachePtr;
/*
* The following structure is used to keep track of all system memory
* currently owned by Tcl. When finalizing, all this memory will be returned
* to the system.
*/
* Get this thread's cache, allocating if necessary.
*/
cachePtr = TclpGetAllocCache();
struct block {
if (cachePtr == NULL) {
struct block *nextPtr; /* Linked list. */
struct block *prevPtr; /* Linked list for big blocks, ensures 8-byte
* alignment for suballocated blocks. */
cachePtr = calloc(1, sizeof(Cache));
};
if (cachePtr == NULL) {
Tcl_Panic("alloc: could not allocate new cache");
}
static struct block *blockList; /* Tracks the suballocated blocks. */
static struct block bigBlocks={ /* Big blocks aren't suballocated. */
&bigBlocks, &bigBlocks
cachePtr->allocCachePtr= NULL;
TclpSetAllocCache(cachePtr);
};
}
return cachePtr;
}
#endif
/*
* The allocator is protected by a special mutex that must be explicitly
* initialized. Futhermore, because Tcl_Alloc may be used before anything else
* in Tcl, we make this module self-initializing after all with the allocInit
* variable.
* TclSetSharedAllocCache, TclSetAllocCache, TclGetAllocCache
*
* These are utility functions for the loadable allocator.
*/
#ifdef TCL_THREADS
static Tcl_Mutex *allocMutexPtr;
#endif
static int allocInit = 0;
void
TclSetSharedAllocCache(
void *allocCachePtr)
{
#ifdef MSTATS
/*
sharedPtr->allocCachePtr = allocCachePtr;
}
* numMallocs[i] is the difference between the number of mallocs and frees for
* a given block size.
void
*/
static unsigned int numMallocs[NBUCKETS+1];
TclSetAllocCache(
void *allocCachePtr)
{
Cache *cachePtr;
#endif
#if !defined(NDEBUG)
GETCACHE(cachePtr);
cachePtr->allocCachePtr = allocCachePtr;
}
#define ASSERT(p) if (!(p)) Tcl_Panic(# p)
#define RANGE_ASSERT(p) if (!(p)) Tcl_Panic(# p)
#else
#define ASSERT(p)
#define RANGE_ASSERT(p)
#endif
/*
* Prototypes for functions used only in this file.
void *
TclGetAllocCache(void)
{
Cache *cachePtr;
*/
static void MoreCore(int bucket);
GETCACHE(cachePtr);
return cachePtr->allocCachePtr;
}
/*
*-------------------------------------------------------------------------
*
* TclInitAlloc --
*
* Initialize the memory system.
*
* Results:
* None.
*
* Side effects:
* Initialize the mutex used to serialize allocations.
* Initialize the mutex used to serialize obj allocations.
* Call the allocator-specific initialization.
*
*-------------------------------------------------------------------------
*/
void
TclInitAlloc(void)
{
/*
* Set the params for the correct allocator
*/
if (!allocInit) {
allocInit = 1;
#ifdef TCL_THREADS
allocMutexPtr = Tcl_GetAllocMutex();
#endif
}
#if defined(TCL_THREADS)
Tcl_Mutex *initLockPtr;
TCL_THREADED = 1;
initLockPtr = Tcl_GetAllocMutex();
Tcl_MutexLock(initLockPtr);
objLockPtr = TclpNewAllocMutex();
TclXpInitAlloc();
Tcl_MutexUnlock(initLockPtr);
#else
TCL_THREADED = 0;
TclXpInitAlloc();
#endif /* THREADS */
#ifdef PURIFY
TCL_PURIFY = 1;
#else
TCL_PURIFY = (getenv("TCL_PURIFY") != NULL);
#endif
}
/*
*-------------------------------------------------------------------------
*----------------------------------------------------------------------
*
* TclFinalizeAllocSubsystem --
* TclFinalizeAlloc --
*
* Release all resources being used by this subsystem, including
* aggressively freeing all memory allocated by TclpAlloc() that has not
* yet been released with TclpFree().
*
* This procedure is used to destroy all private resources used in this
* file.
* After this function is called, all memory allocated with TclpAlloc()
* should be considered unusable.
*
* Results:
* None.
*
* Side effects:
* This subsystem is self-initializing, since memory can be allocated
* Call the allocator-specific finalization.
* before Tcl is formally initialized. After this call, this subsystem
* has been reset to its initial state and is usable again.
*
*-------------------------------------------------------------------------
*----------------------------------------------------------------------
*/
void
TclFinalizeAllocSubsystem(void)
TclFinalizeAlloc(void)
{
unsigned int i;
struct block *blockPtr, *nextPtr;
#if defined(TCL_THREADS)
Tcl_MutexLock(allocMutexPtr);
for (blockPtr = blockList; blockPtr != NULL; blockPtr = nextPtr) {
nextPtr = blockPtr->nextPtr;
TclpSysFree(blockPtr);
}
blockList = NULL;
TclpFreeAllocMutex(objLockPtr);
for (blockPtr = bigBlocks.nextPtr; blockPtr != &bigBlocks; ) {
nextPtr = blockPtr->nextPtr;
TclpSysFree(blockPtr);
blockPtr = nextPtr;
}
objLockPtr = NULL;
bigBlocks.nextPtr = &bigBlocks;
bigBlocks.prevPtr = &bigBlocks;
TclpFreeAllocCache(NULL);
for (i=0 ; i<NBUCKETS ; i++) {
nextf[i] = NULL;
#ifdef MSTATS
numMallocs[i] = 0;
#endif
}
#ifdef MSTATS
numMallocs[i] = 0;
#endif
Tcl_MutexUnlock(allocMutexPtr);
TclXpFinalizeAlloc();
}
/*
*----------------------------------------------------------------------
*
* TclpAlloc --
* TclFreeAllocCache --
*
* Allocate more memory.
* Flush and delete a cache, removing from list of caches.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
char *
TclpAlloc(
#if defined(TCL_THREADS)
void
TclFreeAllocCache(
unsigned int numBytes) /* Number of bytes to allocate. */
void *arg)
{
register union overhead *overPtr;
register long bucket;
register unsigned amount;
struct block *bigBlockPtr = NULL;
Cache *cachePtr = arg;
if (!allocInit) {
/*
/*
* We have to make the "self initializing" because Tcl_Alloc may be
* used before any other part of Tcl. E.g., see main() for tclsh!
*/
* Flush objs.
*/
if (cachePtr->numObjects > 0) {
TclInitAlloc();
Tcl_MutexLock(objLockPtr);
MoveObjs(cachePtr, sharedPtr, cachePtr->numObjects);
Tcl_MutexUnlock(objLockPtr);
}
Tcl_MutexLock(allocMutexPtr);
/*
* First the simple case: we simple allocate big blocks directly.
* Flush the external allocator cache
*/
if (numBytes >= MAXMALLOC - OVERHEAD) {
if (numBytes <= UINT_MAX - OVERHEAD -sizeof(struct block)) {
bigBlockPtr = (struct block *) TclpSysAlloc((unsigned)
TclXpFreeAllocCache(cachePtr->allocCachePtr);
(sizeof(struct block) + OVERHEAD + numBytes), 0);
}
if (bigBlockPtr == NULL) {
}
#endif
Tcl_MutexUnlock(allocMutexPtr);
return NULL;
}
bigBlockPtr->nextPtr = bigBlocks.nextPtr;
bigBlocks.nextPtr = bigBlockPtr;
/*
*----------------------------------------------------------------------
*
* TclSmallAlloc --
bigBlockPtr->prevPtr = &bigBlocks;
bigBlockPtr->nextPtr->prevPtr = bigBlockPtr;
*
overPtr = (union overhead *) (bigBlockPtr + 1);
overPtr->overMagic0 = overPtr->overMagic1 = MAGIC;
overPtr->bucketIndex = 0xff;
#ifdef MSTATS
numMallocs[NBUCKETS]++;
#endif
#ifndef NDEBUG
/*
* Record allocated size of block and bound space with magic numbers.
*/
* Allocate a Tcl_Obj sized block from the per-thread cache.
*
* Results:
* Pointer to uninitialized memory.
*
* Side effects:
* May move blocks from shared cached or allocate new blocks if
* list is empty.
*
*----------------------------------------------------------------------
*/
overPtr->realBlockSize = (numBytes + RSLOP - 1) & ~(RSLOP - 1);
overPtr->rangeCheckMagic = RMAGIC;
BLOCK_END(overPtr) = RMAGIC;
#endif
Tcl_MutexUnlock(allocMutexPtr);
return (void *)(overPtr+1);
}
void *
TclSmallAlloc(void)
{
register Cache *cachePtr;
register Tcl_Obj *objPtr;
int numMove;
Tcl_Obj *newObjsPtr;
GETCACHE(cachePtr);
/*
* Convert amount of memory requested into closest block size stored in
* hash buckets which satisfies request. Account for space used per block
* for accounting.
* Pop the first object.
*/
amount = MINBLOCK; /* size of first bucket */
bucket = MINBLOCK >> 4;
if(cachePtr->firstObjPtr) {
haveObj:
objPtr = cachePtr->firstObjPtr;
cachePtr->firstObjPtr = objPtr->internalRep.otherValuePtr;
while (numBytes + OVERHEAD > amount) {
amount <<= 1;
if (amount == 0) {
Tcl_MutexUnlock(allocMutexPtr);
return NULL;
}
cachePtr->numObjects--;
return objPtr;
}
bucket++;
}
ASSERT(bucket < NBUCKETS);
/*
* If nothing in hash bucket right now, request more memory from the
* system.
* Do it AFTER looking at the queue, so that it doesn't slow down
* non-purify small allocs.
*/
if ((overPtr = nextf[bucket]) == NULL) {
if (TCL_PURIFY) {
MoreCore(bucket);
if ((overPtr = nextf[bucket]) == NULL) {
Tcl_Obj *objPtr = (Tcl_Obj *) TclpAlloc(sizeof(Tcl_Obj));
Tcl_MutexUnlock(allocMutexPtr);
return NULL;
if (objPtr == NULL) {
Tcl_Panic("alloc: could not allocate a new object");
}
return objPtr;
}
/*
* Get this thread's obj list structure and move or allocate new objs if
* Remove from linked list
* necessary.
*/
#if defined(TCL_THREADS)
Tcl_MutexLock(objLockPtr);
numMove = sharedPtr->numObjects;
if (numMove > 0) {
if (numMove > NOBJALLOC) {
numMove = NOBJALLOC;
nextf[bucket] = overPtr->next;
overPtr->overMagic0 = overPtr->overMagic1 = MAGIC;
}
MoveObjs(sharedPtr, cachePtr, numMove);
}
Tcl_MutexUnlock(objLockPtr);
if (cachePtr->firstObjPtr) {
overPtr->bucketIndex = (unsigned char) bucket;
goto haveObj;
}
#ifdef MSTATS
numMallocs[bucket]++;
#endif
#endif
cachePtr->numObjects = numMove = NOBJALLOC;
newObjsPtr = malloc(sizeof(Tcl_Obj) * numMove);
if (newObjsPtr == NULL) {
Tcl_Panic("alloc: could not allocate %d new objects", numMove);
}
#ifndef NDEBUG
/*
* Record allocated size of block and bound space with magic numbers.
*/
while (--numMove >= 0) {
objPtr = &newObjsPtr[numMove];
objPtr->internalRep.otherValuePtr = cachePtr->firstObjPtr;
cachePtr->firstObjPtr = objPtr;
}
overPtr->realBlockSize = (numBytes + RSLOP - 1) & ~(RSLOP - 1);
overPtr->rangeCheckMagic = RMAGIC;
BLOCK_END(overPtr) = RMAGIC;
#endif
goto haveObj;
}
Tcl_MutexUnlock(allocMutexPtr);
return ((char *)(overPtr + 1));
}
/*
*----------------------------------------------------------------------
*
* MoreCore --
* TclSmallFree --
*
* Allocate more memory to the indicated bucket.
*
* Assumes Mutex is already held.
* Return a free Tcl_Obj-sized block to the per-thread cache.
*
* Results:
* None.
*
* Side effects:
* Attempts to get more memory from the system.
* May move free blocks to shared list upon hitting high water mark.
*
*----------------------------------------------------------------------
*/
static void
MoreCore(
int bucket) /* What bucket to allocat to. */
void
TclSmallFree(
void *ptr)
{
register union overhead *overPtr;
Cache *cachePtr;
register long size; /* size of desired block */
long amount; /* amount to allocate */
int numBlocks; /* how many blocks we get */
struct block *blockPtr;
Tcl_Obj *objPtr = ptr;
/*
* sbrk_size <= 0 only for big, FLUFFY, requests (about 2^30 bytes on a
* VAX, I think) or for a negative arg.
*/
if (TCL_PURIFY) {
TclpFree((char *) ptr);
return;
}
size = 1 << (bucket + 3);
ASSERT(size > 0);
amount = MAXMALLOC;
GETCACHE(cachePtr);
numBlocks = amount / size;
ASSERT(numBlocks*size == amount);
blockPtr = (struct block *) TclpSysAlloc((unsigned)
(sizeof(struct block) + amount), 1);
/* no more room! */
/*
if (blockPtr == NULL) {
return;
}
blockPtr->nextPtr = blockList;
blockList = blockPtr;
* Get this thread's list and push on the free Tcl_Obj.
*/
objPtr->internalRep.otherValuePtr = cachePtr->firstObjPtr;
cachePtr->firstObjPtr = objPtr;
cachePtr->numObjects++;
overPtr = (union overhead *) (blockPtr + 1);
#if defined(TCL_THREADS)
/*
* If the number of free objects has exceeded the high water mark, move
* Add new memory allocated to that on free list for this hash bucket.
* some blocks to the shared list.
*/
if (cachePtr->numObjects > NOBJHIGH) {
nextf[bucket] = overPtr;
while (--numBlocks > 0) {
Tcl_MutexLock(objLockPtr);
MoveObjs(cachePtr, sharedPtr, NOBJALLOC);
Tcl_MutexUnlock(objLockPtr);
overPtr->next = (union overhead *)((caddr_t)overPtr + size);
overPtr = (union overhead *)((caddr_t)overPtr + size);
}
overPtr->next = NULL;
#endif
}
/*
*----------------------------------------------------------------------
*
* TclpFree --
* MoveObjs --
*
* Free memory.
* Move Tcl_Obj's between caches.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
void
TclpFree(
char *oldPtr) /* Pointer to memory to free. */
{
register long size;
register union overhead *overPtr;
struct block *bigBlockPtr;
#if defined(TCL_THREADS)
if (oldPtr == NULL) {
return;
}
static void
Tcl_MutexLock(allocMutexPtr);
overPtr = (union overhead *)((caddr_t)oldPtr - sizeof(union overhead));
MoveObjs(
ASSERT(overPtr->overMagic0 == MAGIC); /* make sure it was in use */
ASSERT(overPtr->overMagic1 == MAGIC);
if (overPtr->overMagic0 != MAGIC || overPtr->overMagic1 != MAGIC) {
Tcl_MutexUnlock(allocMutexPtr);
return;
}
Cache *fromPtr,
RANGE_ASSERT(overPtr->rangeCheckMagic == RMAGIC);
RANGE_ASSERT(BLOCK_END(overPtr) == RMAGIC);
size = overPtr->bucketIndex;
if (size == 0xff) {
#ifdef MSTATS
numMallocs[NBUCKETS]--;
#endif
Cache *toPtr,
bigBlockPtr = (struct block *) overPtr - 1;
bigBlockPtr->prevPtr->nextPtr = bigBlockPtr->nextPtr;
bigBlockPtr->nextPtr->prevPtr = bigBlockPtr->prevPtr;
TclpSysFree(bigBlockPtr);
int numMove)
Tcl_MutexUnlock(allocMutexPtr);
return;
}
ASSERT(size < NBUCKETS);
overPtr->next = nextf[size]; /* also clobbers overMagic */
nextf[size] = overPtr;
{
#ifdef MSTATS
numMallocs[size]--;
#endif
register Tcl_Obj *objPtr = fromPtr->firstObjPtr;
Tcl_MutexUnlock(allocMutexPtr);
}
Tcl_Obj *fromFirstObjPtr = objPtr;
/*
*----------------------------------------------------------------------
*
* TclpRealloc --
*
* Reallocate memory.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
char *
TclpRealloc(
char *oldPtr, /* Pointer to alloced block. */
unsigned int numBytes) /* New size of memory. */
{
int i;
union overhead *overPtr;
struct block *bigBlockPtr;
int expensive;
unsigned long maxSize;
if (oldPtr == NULL) {
return TclpAlloc(numBytes);
}
toPtr->numObjects += numMove;
Tcl_MutexLock(allocMutexPtr);
fromPtr->numObjects -= numMove;
overPtr = (union overhead *)((caddr_t)oldPtr - sizeof(union overhead));
ASSERT(overPtr->overMagic0 == MAGIC); /* make sure it was in use */
ASSERT(overPtr->overMagic1 == MAGIC);
if (overPtr->overMagic0 != MAGIC || overPtr->overMagic1 != MAGIC) {
Tcl_MutexUnlock(allocMutexPtr);
return NULL;
}
RANGE_ASSERT(overPtr->rangeCheckMagic == RMAGIC);
RANGE_ASSERT(BLOCK_END(overPtr) == RMAGIC);
i = overPtr->bucketIndex;
/*
* If the block isn't in a bin, just realloc it.
*/
* Find the last object to be moved; set the next one (the first one not
if (i == 0xff) {
struct block *prevPtr, *nextPtr;
bigBlockPtr = (struct block *) overPtr - 1;
prevPtr = bigBlockPtr->prevPtr;
nextPtr = bigBlockPtr->nextPtr;
bigBlockPtr = (struct block *) TclpSysRealloc(bigBlockPtr,
sizeof(struct block) + OVERHEAD + numBytes);
if (bigBlockPtr == NULL) {
Tcl_MutexUnlock(allocMutexPtr);
return NULL;
}
* to be moved) as the first object in the 'from' cache.
if (prevPtr->nextPtr != bigBlockPtr) {
/*
* If the block has moved, splice the new block into the list
* where the old block used to be.
*/
*/
prevPtr->nextPtr = bigBlockPtr;
nextPtr->prevPtr = bigBlockPtr;
}
overPtr = (union overhead *) (bigBlockPtr + 1);
#ifdef MSTATS
numMallocs[NBUCKETS]++;
#endif
#ifndef NDEBUG
/*
* Record allocated size of block and update magic number bounds.
*/
overPtr->realBlockSize = (numBytes + RSLOP - 1) & ~(RSLOP - 1);
BLOCK_END(overPtr) = RMAGIC;
#endif
Tcl_MutexUnlock(allocMutexPtr);
return (char *)(overPtr+1);
}
maxSize = 1 << (i+3);
expensive = 0;
if (numBytes+OVERHEAD > maxSize) {
expensive = 1;
} else if (i>0 && numBytes+OVERHEAD < maxSize/2) {
expensive = 1;
}
if (expensive) {
void *newPtr;
Tcl_MutexUnlock(allocMutexPtr);
newPtr = TclpAlloc(numBytes);
if (newPtr == NULL) {
return NULL;
}
maxSize -= OVERHEAD;
if (maxSize < numBytes) {
numBytes = maxSize;
while (--numMove) {
}
memcpy(newPtr, oldPtr, (size_t) numBytes);
TclpFree(oldPtr);
return newPtr;
objPtr = objPtr->internalRep.otherValuePtr;
}
fromPtr->firstObjPtr = objPtr->internalRep.otherValuePtr;
/*
* Ok, we don't have to copy, it fits as-is
*/
* Move all objects as a block - they are already linked to each other, we
#ifndef NDEBUG
overPtr->realBlockSize = (numBytes + RSLOP - 1) & ~(RSLOP - 1);
BLOCK_END(overPtr) = RMAGIC;
#endif
* just have to update the first and last.
Tcl_MutexUnlock(allocMutexPtr);
return(oldPtr);
}
/*
*----------------------------------------------------------------------
*
* mstats --
*
* Prints two lines of numbers, one showing the length of the free list
* for each size category, the second showing the number of mallocs -
* frees for each size category.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
*/
#ifdef MSTATS
void
mstats(
char *s) /* Where to write info. */
{
register int i, j;
register union overhead *overPtr;
int totalFree = 0, totalUsed = 0;
objPtr->internalRep.otherValuePtr = toPtr->firstObjPtr;
Tcl_MutexLock(allocMutexPtr);
toPtr->firstObjPtr = fromFirstObjPtr;
fprintf(stderr, "Memory allocation statistics %s\nTclpFree:\t", s);
for (i = 0; i < NBUCKETS; i++) {
for (j=0, overPtr=nextf[i]; overPtr; overPtr=overPtr->next, j++) {
fprintf(stderr, " %d", j);
}
}
totalFree += j * (1 << (i + 3));
}
fprintf(stderr, "\nused:\t");
for (i = 0; i < NBUCKETS; i++) {
fprintf(stderr, " %d", numMallocs[i]);
totalUsed += numMallocs[i] * (1 << (i + 3));
}
fprintf(stderr, "\n\tTotal small in use: %d, total free: %d\n",
totalUsed, totalFree);
fprintf(stderr, "\n\tNumber of big (>%d) blocks in use: %d\n",
MAXMALLOC, numMallocs[NBUCKETS]);
Tcl_MutexUnlock(allocMutexPtr);
}
#endif
#else /* !USE_TCLALLOC */
/*
*----------------------------------------------------------------------
*
* TclpAlloc --
*
* Allocate more memory.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
char *
TclpAlloc(
unsigned int numBytes) /* Number of bytes to allocate. */
{
return (char *) malloc(numBytes);
}
/*
*----------------------------------------------------------------------
*
* TclpFree --
*
* Free memory.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
void
TclpFree(
char *oldPtr) /* Pointer to memory to free. */
{
free(oldPtr);
return;
}
/*
*----------------------------------------------------------------------
*
* TclpRealloc --
*
* Reallocate memory.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
char *
TclpRealloc(
char *oldPtr, /* Pointer to alloced block. */
unsigned int numBytes) /* New size of memory. */
{
return (char *) realloc(oldPtr, numBytes);
}
#endif /* !USE_TCLALLOC */
#endif /* !TCL_THREADS */
/*
* Local Variables:
* mode: c
* c-basic-offset: 4
* fill-column: 78
* End:
|