blob: 70688933c55f8f8eb33d3c69fc326185a1551183 [file] [log] [blame]
Austin Schuh745610d2015-09-06 18:19:50 -07001// -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
2// Copyright (c) 2008, Google Inc.
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
8//
9// * Redistributions of source code must retain the above copyright
10// notice, this list of conditions and the following disclaimer.
11// * Redistributions in binary form must reproduce the above
12// copyright notice, this list of conditions and the following disclaimer
13// in the documentation and/or other materials provided with the
14// distribution.
15// * Neither the name of Google Inc. nor the names of its
16// contributors may be used to endorse or promote products derived from
17// this software without specific prior written permission.
18//
19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31// ---
32// Author: Sanjay Ghemawat <opensource@google.com>
33//
34// A Span is a contiguous run of pages.
35
36#ifndef TCMALLOC_SPAN_H_
37#define TCMALLOC_SPAN_H_
38
39#include <config.h>
Brian Silverman20350ac2021-11-17 18:19:55 -080040#include <set>
Austin Schuh745610d2015-09-06 18:19:50 -070041#include "common.h"
Brian Silverman20350ac2021-11-17 18:19:55 -080042#include "base/logging.h"
43#include "page_heap_allocator.h"
Austin Schuh745610d2015-09-06 18:19:50 -070044
45namespace tcmalloc {
46
Brian Silverman20350ac2021-11-17 18:19:55 -080047struct SpanBestFitLess;
48struct Span;
49
50// Store a pointer to a span along with a cached copy of its length.
51// These are used as set elements to improve the performance of
52// comparisons during tree traversal: the lengths are inline with the
53// tree nodes and thus avoid expensive cache misses to dereference
54// the actual Span objects in most cases.
55struct SpanPtrWithLength {
56 explicit SpanPtrWithLength(Span* s);
57
58 Span* span;
59 Length length;
60};
61typedef std::set<SpanPtrWithLength, SpanBestFitLess, STLPageHeapAllocator<SpanPtrWithLength, void> > SpanSet;
62
63// Comparator for best-fit search, with address order as a tie-breaker.
64struct SpanBestFitLess {
65 bool operator()(SpanPtrWithLength a, SpanPtrWithLength b) const;
66};
67
Austin Schuh745610d2015-09-06 18:19:50 -070068// Information kept for a span (a contiguous run of pages).
69struct Span {
70 PageID start; // Starting page number
71 Length length; // Number of pages in span
72 Span* next; // Used when in link list
73 Span* prev; // Used when in link list
Brian Silverman20350ac2021-11-17 18:19:55 -080074 union {
75 void* objects; // Linked list of free objects
76
77 // Span may contain iterator pointing back at SpanSet entry of
78 // this span into set of large spans. It is used to quickly delete
79 // spans from those sets. span_iter_space is space for such
80 // iterator which lifetime is controlled explicitly.
81 char span_iter_space[sizeof(SpanSet::iterator)];
82 };
Austin Schuh745610d2015-09-06 18:19:50 -070083 unsigned int refcount : 16; // Number of non-free objects
84 unsigned int sizeclass : 8; // Size-class for small objects (or 0)
85 unsigned int location : 2; // Is the span on a freelist, and if so, which?
86 unsigned int sample : 1; // Sampled object?
Brian Silverman20350ac2021-11-17 18:19:55 -080087 bool has_span_iter : 1; // Iff span_iter_space has valid
88 // iterator. Only for debug builds.
Austin Schuh745610d2015-09-06 18:19:50 -070089
Brian Silverman20350ac2021-11-17 18:19:55 -080090 // Sets iterator stored in span_iter_space.
91 // Requires has_span_iter == 0.
92 void SetSpanSetIterator(const SpanSet::iterator& iter);
93 // Copies out and destroys iterator stored in span_iter_space.
94 SpanSet::iterator ExtractSpanSetIterator();
Austin Schuh745610d2015-09-06 18:19:50 -070095
96 // What freelist the span is on: IN_USE if on none, or normal or returned
97 enum { IN_USE, ON_NORMAL_FREELIST, ON_RETURNED_FREELIST };
98};
99
Brian Silverman20350ac2021-11-17 18:19:55 -0800100inline SpanPtrWithLength::SpanPtrWithLength(Span* s)
101 : span(s),
102 length(s->length) {
103}
104
105inline bool SpanBestFitLess::operator()(SpanPtrWithLength a, SpanPtrWithLength b) const {
106 if (a.length < b.length)
107 return true;
108 if (a.length > b.length)
109 return false;
110 return a.span->start < b.span->start;
111}
112
113inline void Span::SetSpanSetIterator(const SpanSet::iterator& iter) {
114 ASSERT(!has_span_iter);
115 has_span_iter = 1;
116
117 new (span_iter_space) SpanSet::iterator(iter);
118}
119
120inline SpanSet::iterator Span::ExtractSpanSetIterator() {
121 typedef SpanSet::iterator iterator_type;
122
123 ASSERT(has_span_iter);
124 has_span_iter = 0;
125
126 iterator_type* this_iter =
127 reinterpret_cast<iterator_type*>(span_iter_space);
128 iterator_type retval = *this_iter;
129 this_iter->~iterator_type();
130 return retval;
131}
Austin Schuh745610d2015-09-06 18:19:50 -0700132
133// Allocator/deallocator for spans
134Span* NewSpan(PageID p, Length len);
135void DeleteSpan(Span* span);
136
137// -------------------------------------------------------------------------
138// Doubly linked list of spans.
139// -------------------------------------------------------------------------
140
141// Initialize *list to an empty list.
142void DLL_Init(Span* list);
143
144// Remove 'span' from the linked list in which it resides, updating the
145// pointers of adjacent Spans and setting span's next and prev to NULL.
146void DLL_Remove(Span* span);
147
148// Return true iff "list" is empty.
149inline bool DLL_IsEmpty(const Span* list) {
150 return list->next == list;
151}
152
153// Add span to the front of list.
154void DLL_Prepend(Span* list, Span* span);
155
156// Return the length of the linked list. O(n)
157int DLL_Length(const Span* list);
158
159} // namespace tcmalloc
160
161#endif // TCMALLOC_SPAN_H_