-
Notifications
You must be signed in to change notification settings - Fork 558
Expand file tree
/
Copy pathspan.h
More file actions
837 lines (716 loc) · 29.1 KB
/
Copy pathspan.h
File metadata and controls
837 lines (716 loc) · 29.1 KB
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
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
// Copyright 2019 The TCMalloc Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// A Span is a contiguous run of pages.
#ifndef TCMALLOC_SPAN_H_
#define TCMALLOC_SPAN_H_
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <atomic>
#include <cassert>
#include <cstddef>
#include "absl/base/attributes.h"
#include "absl/base/casts.h"
#include "absl/base/macros.h"
#include "absl/base/nullability.h"
#include "absl/base/optimization.h"
#include "absl/base/thread_annotations.h"
#include "absl/types/span.h"
#include "tcmalloc/common.h"
#include "tcmalloc/internal/config.h"
#include "tcmalloc/internal/linked_list.h"
#include "tcmalloc/internal/logging.h"
#include "tcmalloc/internal/optimization.h"
#include "tcmalloc/internal/prefetch.h"
#include "tcmalloc/internal/range_tracker.h"
#include "tcmalloc/internal/sampled_allocation.h"
#include "tcmalloc/pages.h"
#include "tcmalloc/sizemap.h"
GOOGLE_MALLOC_SECTION_BEGIN
namespace tcmalloc {
namespace tcmalloc_internal {
// Denominator for bitmap scaling factor. The idea is that instead of dividing
// by N we multiply by M = kBitmapScalingDenominator / N and round the resulting
// value.
inline constexpr size_t kBitmapScalingDenominator = 1 << 30;
enum AccessDensityPrediction {
// Predict that the span would be sparsely-accessed.
kSparse = 0,
// Predict that the span would be densely-accessed.
kDense = 1,
kPredictionCounts
};
struct SpanAllocInfo {
size_t objects_per_span;
AccessDensityPrediction density;
};
// Information kept for a span (a contiguous run of pages).
//
// Spans can be in different states. The current state determines set of methods
// that can be called on the span (and the active member in the union below).
// States are:
// - SMALL_OBJECT: the span holds multiple small objects.
// The span is owned by CentralFreeList and is generally on
// CentralFreeList::nonempty_ list (unless has no free objects).
// - LARGE_OBJECT: the span holds a single large object.
// The span can be considered to be owner by user until the object is freed.
// - SAMPLED: the span holds a single sampled object.
// The span can be considered to be owner by user until the object is freed.
// sampled_ == 1.
class Span;
typedef TList<Span> SpanList;
class ABSL_CACHELINE_ALIGNED Span final : public SpanList::Elem {
public:
constexpr Span()
: embed_count_(0),
freelist_(0),
allocated_{std::numeric_limits<uint16_t>::max()},
cache_size_(0),
is_long_lived_span_(0),
nonempty_index_(0),
is_donated_(0),
first_page_(0),
reserved_(0),
is_large_span_(0),
sampled_(0),
large_or_sampled_state_{0, nullptr} {}
explicit Span(Range r)
: embed_count_(0),
freelist_(0),
allocated_{0},
cache_size_(0),
is_long_lived_span_(0),
nonempty_index_(0),
is_donated_(0),
first_page_(r.p.index()),
reserved_(0),
is_large_span_(0),
sampled_(0),
large_or_sampled_state_{0, nullptr} {
TC_ASSERT_GT(r.p, PageId{0});
TC_CHECK_LT(r.p.index(), static_cast<uint64_t>(1) << kMaxPageIdBits);
set_num_pages(r.n);
}
Span(const Span&) = delete;
Span& operator=(const Span&) = delete;
// Allocator/deallocator for spans. Note that these functions are defined
// in static_vars.h, which is weird: see there for why.
static Span* absl_nonnull New(Range r)
#ifndef TCMALLOC_INTERNAL_LEGACY_LOCKING
ABSL_LOCKS_EXCLUDED(pageheap_lock)
#endif
;
static void Delete(Span* absl_nonnull span);
// ---------------------------------------------------------------------------
// Support for sampled allocations.
// There is one-to-one correspondence between a sampled allocation and a span.
// ---------------------------------------------------------------------------
// Mark this span in the "SAMPLED" state. It will store the corresponding
// sampled allocation and update some global counters on the total size of
// sampled allocations.
void Sample(SampledAllocation* absl_nonnull sampled_allocation);
// Unmark this span from its "SAMPLED" state. It will return the sampled
// allocation previously passed to Span::Sample() or nullptr if this is a
// non-sampling span. It will also update the global counters on the total
// size of sampled allocations.
[[nodiscard]] SampledAllocation* absl_nullable Unsample();
// Returns the sampled allocation of the span.
// pageheap_lock is not required, but caller either needs to hold the lock or
// ensure by some other means that the sampling state can't be changed
// concurrently.
// REQUIRES: this is a SAMPLED span.
const SampledAllocation& sampled_allocation() const;
// Is it a sampling span?
// For debug checks. pageheap_lock is not required, but caller needs to ensure
// that sampling state can't be changed concurrently.
bool sampled() const;
bool donated() const { return is_donated_; }
void set_donated(bool value) { is_donated_ = value; }
// ---------------------------------------------------------------------------
// Span memory range.
// ---------------------------------------------------------------------------
// Returns first page of the span.
PageId first_page() const;
// Returns the last page in the span.
PageId last_page() const;
// Sets span first page.
void set_first_page(PageId p);
// Returns start address of the span.
ABSL_ATTRIBUTE_RETURNS_NONNULL void* start_address() const;
// Returns number of pages in the span.
Length num_pages() const;
// Sets number of pages in the span.
void set_num_pages(Length len);
// Total memory bytes in the span.
size_t bytes_in_span() const;
// Returns number of objects allocated in the span.
uint16_t Allocated() const {
return allocated_.load(std::memory_order_relaxed);
}
// Returns index of the non-empty list to which this span belongs to.
uint8_t nonempty_index() const { return nonempty_index_; }
// Records an index of the non-empty list associated with this span.
void set_nonempty_index(uint8_t index) {
nonempty_index_ = index;
TC_ASSERT_EQ(nonempty_index_, index);
}
// ---------------------------------------------------------------------------
// Freelist management.
// Used for spans in CentralFreelist to manage free objects.
// These methods REQUIRE a SMALL_OBJECT span.
// ---------------------------------------------------------------------------
// Indicate whether the Span is empty. Size is used to determine whether
// the span is using a compressed linked list of objects, or a bitmap
// to hold available objects.
[[nodiscard]] bool FreelistEmpty(size_t size,
uint32_t objects_per_span) const;
// Pushes ptr onto freelist unless the freelist becomes full, in which case
// just return false.
//
// If the freelist becomes full, we do not push the object onto the freelist.
template <typename T>
[[nodiscard]] bool FreelistPushBatch(absl::Span<T> batch, size_t size,
uint32_t reciprocal) __restrict__;
// Pops up to N objects from the freelist and returns them in the batch array.
// Returns number of objects actually popped.
[[nodiscard]] size_t FreelistPopBatch(absl::Span<void* absl_nonnull> batch,
size_t size) __restrict__;
// Initialize freelist to contain all objects in the span.
// Pops up to N objects from the freelist and returns them in the batch array.
// Returns number of objects actually popped.
[[nodiscard]] int BuildFreelist(size_t size, size_t count,
absl::Span<void*> batch,
uint64_t alloc_time) __restrict__;
// Prefetch cacheline containing most important span information.
void Prefetch();
// IsValidSizeClass verifies size class parameters from the Span perspective.
static bool IsValidSizeClass(size_t size, Length pages);
// For bitmap'd spans conversion from an offset to an index is performed
// by multiplying by the scaled reciprocal of the object size.
static uint32_t CalcReciprocal(size_t size);
// When central freelist tracks a span, that span is assured to consist of <
// kLargeSpanLength number of pages. This allows us to record number of pages
// in that span in fewer (i.e. kMaxNumPageBits) bits.
static constexpr size_t kMaxNumPageBits = 6;
static constexpr Length kLargeSpanLength = Length((1 << kMaxNumPageBits) - 1);
static_assert(kMaxSize <= kLargeSpanLength.in_bytes());
uint64_t AllocTime() const;
// Returns true if Span will use bitmap for objects of size <size>.
static bool UseBitmapForSize(size_t size);
typedef uint16_t ObjIdx;
// Convert object pointer <-> freelist index.
ObjIdx PtrToIdx(void* ptr, size_t size) const;
ObjIdx* IdxToPtr(ObjIdx idx, size_t size, uintptr_t start) const;
// Convert object pointer <-> freelist index for bitmap managed objects.
ObjIdx BitmapPtrToIdx(void* ptr, size_t size, uint32_t reciprocal) const;
void* BitmapIdxToPtr(ObjIdx idx, size_t size) const;
static constexpr size_t kNonemptyIndexBits = 5;
bool is_long_lived_span() const { return is_long_lived_span_; }
void set_is_long_lived_span(bool value) { is_long_lived_span_ = value; }
private:
// Returns if the span is large (i.e. consists of > kLargeSpanLength number of
// pages) or is sampled.
bool is_large_or_sampled() const { return is_large_span_ || sampled_; }
// See the comment on freelist organization in cc file.
static constexpr ObjIdx kListEnd = -1;
// Actual number of objects that we may cache. This is lower than the total
// cache array size. Some cache entries are reserved or are used for other
// purposes.
//
// TODO(b/527641380): With the enlarged bitmap, microbenchmarks on a single,
// cache-resident span regress. Investigate this. We use a larger cache
// anyways because the multi-span benchmarks that put more pressure on the
// memory subsystem improve anyways.
#ifdef TCMALLOC_INTERNAL_LEGACY_LOCKING
static constexpr size_t kCacheSize = 8;
#else
static constexpr size_t kCacheSize = 12;
#endif
static constexpr size_t kMaxCacheBits = 4;
static_assert(kCacheSize <= (1 << kMaxCacheBits) - 1);
static constexpr size_t kMaxPageIdBits = kAddressBits - kPageShift;
static constexpr size_t kReservedBits = 24;
// Use uint16_t or uint8_t for 16 bit and 8 bit fields instead of bitfields.
// LLVM will generate widen load/store and bit masking operations to access
// bitfields and this hurts performance. Although compiler flag
// -ffine-grained-bitfield-accesses can help the performance if bitfields
// are used here, but the flag could potentially hurt performance in other
// cases so it is not enabled by default. For more information, please
// look at b/35680381 and cl/199502226.
// For available objects stored as a compressed linked list, the index of
// the first object in recorded in freelist_.
struct {
uint16_t embed_count_;
uint16_t freelist_;
};
#ifdef TCMALLOC_INTERNAL_LEGACY_LOCKING
std::atomic<uint16_t>
#else
struct {
uint16_t value;
uint16_t load(std::memory_order) const { return value; }
void store(uint16_t v, std::memory_order) { value = v; }
}
#endif
allocated_; // Number of non-free objects
#ifndef TCMALLOC_INTERNAL_LEGACY_LOCKING
uint8_t cache_size_;
#else
uint8_t cache_size_ : kMaxCacheBits;
#endif
uint8_t is_long_lived_span_ : 1;
uint8_t nonempty_index_ : kNonemptyIndexBits; // The nonempty_ list index for
// this span.
// Has this span allocation resulted in a donation to the filler in the page
// heap? This is used by page heap to compute abandoned pages.
uint8_t is_donated_ : 1;
// The number of bits of the cache space that may be used for bitmap.
static constexpr size_t kBitmapSize = 8 * sizeof(ObjIdx) * kCacheSize;
uint64_t first_page_ : kMaxPageIdBits; // Starting page number.
uint32_t reserved_ : kReservedBits;
// Determines if the span consists of > kLargeSpanLength number of pages.
uint8_t is_large_span_ : 1;
uint8_t sampled_ : 1; // Sampled object?
struct LargeOrSampledState {
uint64_t num_pages;
// Used only for sampled spans (SAMPLED state).
SampledAllocation* sampled_allocation;
};
#ifdef TCMALLOC_INTERNAL_LEGACY_LOCKING
static constexpr size_t kAllocTimeShift = 0;
#else
static constexpr size_t kAllocTimeShift = kMaxNumPageBits;
static constexpr size_t kAllocTimeBits = 64 - kAllocTimeShift;
#endif
struct SmallSpanState {
uint64_t num_pages : kMaxNumPageBits;
#ifndef TCMALLOC_INTERNAL_LEGACY_LOCKING
uint64_t alloc_time : kAllocTimeBits;
#endif
union {
// Used only for spans in CentralFreeList (SMALL_OBJECT state).
// Embed cache of free objects.
ObjIdx cache[Span::kCacheSize];
// Used for spans with in CentralFreeList with fewer than 64 objects.
// Each bit is set to one when the object is available, and zero
// when the object is used.
Bitmap<kBitmapSize> bitmap{};
};
#ifdef TCMALLOC_INTERNAL_LEGACY_LOCKING
uint64_t alloc_time;
#endif
};
// There is nothing inherently fixed about this size, but it is a useful
// indicator that we are using the space and not unintentionally regressing
// it.
static_assert(sizeof(SmallSpanState) == 32);
union {
// When a span consists of greater than kLargeSpanLength number of pages,
// it's the page heap that is allocating an object > kMaxSize. In such a
// scenario, use larger number of bits to record the number of pages in that
// span. Additionally, as central freelist is not tracking that span, we do
// not need to record the state such as bitmap or cache bits. We also do
// not need to record that state when the span is sampled.
LargeOrSampledState large_or_sampled_state_;
// When a span consists of < kLargeSpanLength number of pages, we can record
// the number of pages in kMaxNumPageBits number of bits. Additionally, it's
// likely (although not assured) that the central freelist is tracking that
// span. So, we additionally need to record cache or bitmap for that span.
SmallSpanState small_span_state_;
};
// Helper function for converting a pointer to an index.
static ObjIdx OffsetToIdx(uintptr_t offset, uint32_t reciprocal);
size_t ListPopBatch(void** __restrict batch, size_t N,
size_t size) __restrict__;
bool ListPushBatch(absl::Span<void*> batch, size_t size) __restrict__;
bool ListPushBatch(absl::Span<ObjIdx> batch, size_t size) __restrict__;
// For spans containing 64 or fewer objects, indicate that the object at the
// index has been returned. Always returns true.
bool BitmapPushBatch(absl::Span<void*> batch, size_t size,
uint32_t reciprocal) __restrict__;
bool BitmapPushBatch(absl::Span<ObjIdx> batch, size_t size,
uint32_t reciprocal) __restrict__;
// A bitmap is used to indicate object availability for spans containing
// 64 or fewer objects.
void BuildBitmap(size_t size, size_t count) __restrict__;
// For spans with kBitmapSize or fewer objects populate batch with up to N
// objects. Returns number of objects actually popped.
size_t BitmapPopBatch(absl::Span<void*> batch, size_t size) __restrict__;
[[noreturn]] void ReportDoubleFree(const void* ptr);
ABSL_ATTRIBUTE_RETURNS_NONNULL SampledAllocation* UnsampleSlow();
// Friend class to enable more indepth testing of bitmap code.
friend class SpanTestPeer;
};
inline uint64_t Span::AllocTime() const {
if (is_large_or_sampled()) return 0;
return small_span_state_.alloc_time << kAllocTimeShift;
}
inline Span::ObjIdx* Span::IdxToPtr(ObjIdx idx, size_t size,
uintptr_t start) const {
TC_ASSERT_EQ(small_span_state_.num_pages, 1u);
TC_ASSERT_EQ(start, first_page().start_uintptr());
TC_ASSERT_NE(idx, kListEnd);
uintptr_t off = start + (static_cast<uintptr_t>(idx) << kAlignmentShift);
ObjIdx* ptr = reinterpret_cast<ObjIdx*>(off);
TC_ASSERT_EQ(PtrToIdx(ptr, size), idx);
return ptr;
}
inline Span::ObjIdx Span::PtrToIdx(void* ptr, size_t size) const {
// Object index is an offset from span start divided by kAlignment.
uintptr_t p = reinterpret_cast<uintptr_t>(ptr);
// Classes that use freelist must also use 1 page per span,
// so don't load first_page_ (may be on a different cache line).
TC_ASSERT_EQ(small_span_state_.num_pages, 1u);
TC_ASSERT_EQ(PageIdContaining(ptr), first_page());
uintptr_t off = (p & (kPageSize - 1)) >> kAlignmentShift;
ObjIdx idx = static_cast<ObjIdx>(off);
TC_ASSERT_NE(idx, kListEnd);
TC_ASSERT_EQ(idx, off);
TC_ASSERT_EQ(p, first_page().start_uintptr() +
(static_cast<uintptr_t>(idx) << kAlignmentShift));
return idx;
}
template <typename T>
inline bool Span::FreelistPushBatch(absl::Span<T> batch, size_t size,
uint32_t reciprocal) __restrict__ {
TC_ASSERT(!is_large_or_sampled());
const auto allocated = allocated_.load(std::memory_order_relaxed);
TC_ASSERT_GE(allocated, batch.size());
if (ABSL_PREDICT_FALSE(allocated == batch.size())) {
return false;
}
allocated_.store(allocated - batch.size(), std::memory_order_relaxed);
// Bitmaps are used to record object availability when there are no more than
// kBitmapSize objects in a span.
if (ABSL_PREDICT_TRUE(UseBitmapForSize(size))) {
return BitmapPushBatch(batch, size, reciprocal);
}
return ListPushBatch(batch, size);
}
inline bool Span::ListPushBatch(absl::Span<void*> batch,
size_t size) __restrict__ {
if (cache_size_ < kCacheSize) {
auto cache_writes = std::min(kCacheSize - cache_size_, batch.size());
for (int i = 0; i < cache_writes; ++i) {
// Have empty space in the cache, push there.
const ObjIdx idx = PtrToIdx(batch[i], size);
small_span_state_.cache[cache_size_ + i] = idx;
}
cache_size_ += cache_writes;
batch.remove_prefix(cache_writes);
}
if (batch.empty()) {
return true;
}
// Avoid loading first_page_, since we can infer it from the pointer. It is
// uniform across all objects in batch.
const uintptr_t start =
reinterpret_cast<uintptr_t>(batch[0]) & ~(kPageSize - 1);
#ifndef NDEBUG
for (int i = 1; i < batch.size(); ++i) {
TC_ASSERT_EQ(start,
reinterpret_cast<uintptr_t>(batch[i]) & ~(kPageSize - 1));
}
#endif
for (void* ptr : batch) {
const ObjIdx idx = PtrToIdx(ptr, size);
if (ABSL_PREDICT_TRUE(freelist_ != kListEnd) &&
// -1 because the first slot is used by freelist link.
ABSL_PREDICT_TRUE(embed_count_ != size / sizeof(ObjIdx) - 1)) {
// Push onto the first object on freelist.
ObjIdx* __restrict host = IdxToPtr(freelist_, size, start);
embed_count_++;
host[embed_count_] = idx;
} else {
// Push onto freelist.
*reinterpret_cast<ObjIdx*>(ptr) = freelist_;
freelist_ = idx;
embed_count_ = 0;
}
}
return true;
}
inline bool Span::ListPushBatch(absl::Span<Span::ObjIdx> batch,
size_t size) __restrict__ {
if (cache_size_ < kCacheSize) {
auto cache_writes = std::min(kCacheSize - cache_size_, batch.size());
for (int i = 0; i < cache_writes; ++i) {
// Have empty space in the cache, push there.
const ObjIdx idx = batch[i];
small_span_state_.cache[cache_size_ + i] = idx;
}
cache_size_ += cache_writes;
batch.remove_prefix(cache_writes);
}
if (batch.empty()) {
return true;
}
const uintptr_t start = absl::bit_cast<uintptr_t>(start_address());
ObjIdx freelist = freelist_;
uint16_t embed_count = embed_count_;
ObjIdx* __restrict host;
if (ABSL_PREDICT_TRUE(freelist != kListEnd)) {
host = IdxToPtr(freelist, size, start);
} else {
ObjIdx idx = batch[0];
batch.remove_prefix(1);
host = IdxToPtr(idx, size, start);
*host = kListEnd;
freelist = idx;
embed_count = 0;
}
TC_ASSERT_NE(freelist, kListEnd);
// -1 because the first slot is used by freelist link.
const size_t limit = size / sizeof(ObjIdx) - 1;
for (const ObjIdx idx : batch) {
if (ABSL_PREDICT_TRUE(embed_count != limit)) {
// Push onto the first object on freelist.
embed_count++;
host[embed_count] = idx;
} else {
// Push onto freelist.
ObjIdx* __restrict new_host = IdxToPtr(idx, size, start);
*new_host = freelist;
freelist = idx;
embed_count = 0;
host = new_host;
}
}
freelist_ = freelist;
embed_count_ = embed_count;
return true;
}
inline Span::ObjIdx Span::OffsetToIdx(uintptr_t offset, uint32_t reciprocal) {
// Add kBitmapScalingDenominator / 2 to round to nearest integer.
return static_cast<ObjIdx>(
(offset * reciprocal + kBitmapScalingDenominator / 2) /
kBitmapScalingDenominator);
}
#ifndef TCMALLOC_INTERNAL_LEGACY_LOCKING
inline void* Span::BitmapIdxToPtr(ObjIdx idx, size_t size) const {
uintptr_t off = first_page().start_uintptr() + idx * size;
return reinterpret_cast<void*>(off);
}
#endif
inline Span::ObjIdx Span::BitmapPtrToIdx(void* ptr, size_t size,
uint32_t reciprocal) const {
uintptr_t p = reinterpret_cast<uintptr_t>(ptr);
uintptr_t off = static_cast<uint32_t>(p - first_page().start_uintptr());
ObjIdx idx = OffsetToIdx(off, reciprocal);
TC_ASSERT_EQ(BitmapIdxToPtr(idx, size), ptr);
return idx;
}
inline bool Span::BitmapPushBatch(absl::Span<void*> batch, size_t size,
uint32_t reciprocal) __restrict__ {
size_t before = small_span_state_.bitmap.CountBits();
for (void* ptr : batch) {
ObjIdx idx = BitmapPtrToIdx(ptr, size, reciprocal);
// Set the bit indicating where the object was returned.
bool prior = small_span_state_.bitmap.SetBit(idx);
// Check that the object is not already returned.
(void)prior;
#if !defined(NDEBUG)
if (ABSL_PREDICT_FALSE(prior)) {
ReportDoubleFree(ptr);
}
#endif
}
TC_ASSERT_EQ(before + batch.size(), small_span_state_.bitmap.CountBits());
return true;
}
inline bool Span::BitmapPushBatch(absl::Span<ObjIdx> batch, size_t size,
uint32_t reciprocal) __restrict__ {
size_t before = small_span_state_.bitmap.CountBits();
for (const ObjIdx idx : batch) {
// Check that the object is not already returned.
TC_ASSERT_EQ(small_span_state_.bitmap.GetBit(idx), 0);
// Set the bit indicating where the object was returned.
small_span_state_.bitmap.SetBit(idx);
}
TC_ASSERT_EQ(before + batch.size(), small_span_state_.bitmap.CountBits());
return true;
}
inline const SampledAllocation& Span::sampled_allocation() const {
TC_ASSERT(sampled_);
TC_ASSERT(is_large_or_sampled());
return *large_or_sampled_state_.sampled_allocation;
}
inline bool Span::sampled() const { return sampled_; }
inline PageId Span::first_page() const { return PageId(first_page_); }
inline PageId Span::last_page() const {
if (is_large_or_sampled()) {
return first_page() + Length(large_or_sampled_state_.num_pages) - Length(1);
}
return first_page() + Length(small_span_state_.num_pages) - Length(1);
}
inline void Span::set_first_page(PageId p) {
TC_ASSERT_GT(p, PageId{0});
TC_CHECK_LT(p.index(), static_cast<uint64_t>(1) << kMaxPageIdBits);
first_page_ = p.index();
TC_ASSERT_EQ(first_page_, p.index());
}
inline void* Span::start_address() const {
TC_ASSERT_GT(first_page(), PageId{0});
return first_page().start_addr();
}
inline Length Span::num_pages() const {
if (is_large_or_sampled()) {
return Length(large_or_sampled_state_.num_pages);
}
return Length(small_span_state_.num_pages);
}
inline void Span::set_num_pages(Length len) {
if (len > kLargeSpanLength || sampled()) {
large_or_sampled_state_.num_pages = len.raw_num();
is_large_span_ = len > kLargeSpanLength;
return;
}
TC_ASSERT_LT(len.raw_num(), 1u << kMaxNumPageBits);
small_span_state_.num_pages = len.raw_num();
is_large_span_ = 0;
}
inline size_t Span::bytes_in_span() const ABSL_NO_THREAD_SAFETY_ANALYSIS {
if (is_large_or_sampled()) {
return Length(large_or_sampled_state_.num_pages).in_bytes();
}
return Length(small_span_state_.num_pages).in_bytes();
}
inline bool Span::FreelistEmpty(size_t size, uint32_t objects_per_span) const {
TC_ASSERT(!is_large_or_sampled());
#ifndef TCMALLOC_INTERNAL_LEGACY_LOCKING
(void)size;
return allocated_.load(std::memory_order_relaxed) == objects_per_span;
#else
(void)objects_per_span;
if (UseBitmapForSize(size)) {
return small_span_state_.bitmap.IsZero();
} else {
return cache_size_ == 0 && freelist_ == kListEnd;
}
#endif
}
inline void Span::Prefetch() { PrefetchT0(this); }
inline bool Span::IsValidSizeClass(size_t size, Length pages) {
if (pages > kLargeSpanLength) return false;
if (Span::UseBitmapForSize(size)) {
size_t objects = pages.in_bytes() / size;
return objects <= kBitmapSize;
} else {
return pages == Length(1);
}
}
inline bool Span::UseBitmapForSize(size_t size) {
// Can fit kBitmapSize objects into a bitmap, so determine what the minimum
// object size needs to be in order for that to work. This makes the
// assumption that we don't increase the number of pages at a point where the
// object count ends up exceeding kBitmapSize.
static constexpr size_t kBitmapMinObjectSize = kPageSize / kBitmapSize;
return size >= kBitmapMinObjectSize;
}
inline size_t Span::BitmapPopBatch(absl::Span<void*> batch,
size_t size) __restrict__ {
#ifdef TCMALLOC_INTERNAL_LEGACY_LOCKING
size_t before = small_span_state_.bitmap.CountBits();
size_t count = 0;
// Want to fill the batch either with batch.size() objects, or the number of
// objects remaining in the span.
while (!small_span_state_.bitmap.IsZero() && count < batch.size()) {
size_t offset = small_span_state_.bitmap.FindSet(0);
TC_ASSERT_LT(offset, small_span_state_.bitmap.size());
batch[count] = BitmapIdxToPtr(offset, size);
small_span_state_.bitmap.ClearLowestBit();
count++;
}
TC_ASSERT_EQ(small_span_state_.bitmap.CountBits() + count, before);
allocated_.store(allocated_.load(std::memory_order_relaxed) + count,
std::memory_order_relaxed);
return count;
#else
void** ptrs = batch.data();
size_t popped = small_span_state_.bitmap.PopBatch(
[&](size_t offset) {
*ptrs++ = BitmapIdxToPtr(static_cast<ObjIdx>(offset), size);
},
batch.size());
allocated_.store(allocated_.load(std::memory_order_relaxed) + popped,
std::memory_order_relaxed);
return popped;
#endif
}
inline size_t Span::FreelistPopBatch(const absl::Span<void*> batch,
size_t size) __restrict__ {
TC_ASSERT(!is_large_or_sampled());
// Handle spans with bitmap.size() or fewer objects using a bitmap. We expect
// spans to frequently hold smaller objects.
if (ABSL_PREDICT_TRUE(UseBitmapForSize(size))) {
return BitmapPopBatch(batch, size);
}
return ListPopBatch(batch.data(), batch.size(), size);
}
inline size_t Span::ListPopBatch(void** __restrict batch, size_t N,
size_t size) __restrict__ {
size_t result = 0;
// Pop from cache.
auto csize = cache_size_;
ASSUME(csize <= kCacheSize);
auto cache_reads = csize < N ? csize : N;
const uintptr_t span_start = first_page().start_uintptr();
for (; result < cache_reads; result++) {
batch[result] =
IdxToPtr(small_span_state_.cache[csize - result - 1], size, span_start);
}
// Store this->cache_size_ one time.
cache_size_ = csize - result;
while (result < N) {
if (freelist_ == kListEnd) {
break;
}
ObjIdx* const host = IdxToPtr(freelist_, size, span_start);
uint16_t embed_count = embed_count_;
size_t iter = embed_count;
if (result + embed_count > N) {
iter = N - result;
}
for (size_t i = 0; i < iter; i++) {
// Pop from the first object on freelist.
batch[result + i] = IdxToPtr(host[embed_count - i], size, span_start);
}
embed_count -= iter;
result += iter;
if (result == N) {
embed_count_ = embed_count;
break;
}
// The first object on the freelist is empty, pop it.
TC_ASSERT_EQ(embed_count, 0);
batch[result] = host;
result++;
freelist_ = host[0];
embed_count_ = size / sizeof(ObjIdx) - 1;
}
allocated_.store(allocated_.load(std::memory_order_relaxed) + result,
std::memory_order_relaxed);
return result;
}
inline SampledAllocation* absl_nullable Span::Unsample() {
if (!sampled_) {
return nullptr;
}
return UnsampleSlow();
}
} // namespace tcmalloc_internal
} // namespace tcmalloc
GOOGLE_MALLOC_SECTION_END
#endif // TCMALLOC_SPAN_H_