blob: 83feda19c5eb68bd54862f7723f6baeab3081c82 [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>
40#include "common.h"
41
42namespace tcmalloc {
43
44// Information kept for a span (a contiguous run of pages).
45struct Span {
46 PageID start; // Starting page number
47 Length length; // Number of pages in span
48 Span* next; // Used when in link list
49 Span* prev; // Used when in link list
50 void* objects; // Linked list of free objects
51 unsigned int refcount : 16; // Number of non-free objects
52 unsigned int sizeclass : 8; // Size-class for small objects (or 0)
53 unsigned int location : 2; // Is the span on a freelist, and if so, which?
54 unsigned int sample : 1; // Sampled object?
55
56#undef SPAN_HISTORY
57#ifdef SPAN_HISTORY
58 // For debugging, we can keep a log events per span
59 int nexthistory;
60 char history[64];
61 int value[64];
62#endif
63
64 // What freelist the span is on: IN_USE if on none, or normal or returned
65 enum { IN_USE, ON_NORMAL_FREELIST, ON_RETURNED_FREELIST };
66};
67
68#ifdef SPAN_HISTORY
69void Event(Span* span, char op, int v = 0);
70#else
71#define Event(s,o,v) ((void) 0)
72#endif
73
74// Allocator/deallocator for spans
75Span* NewSpan(PageID p, Length len);
76void DeleteSpan(Span* span);
77
78// -------------------------------------------------------------------------
79// Doubly linked list of spans.
80// -------------------------------------------------------------------------
81
82// Initialize *list to an empty list.
83void DLL_Init(Span* list);
84
85// Remove 'span' from the linked list in which it resides, updating the
86// pointers of adjacent Spans and setting span's next and prev to NULL.
87void DLL_Remove(Span* span);
88
89// Return true iff "list" is empty.
90inline bool DLL_IsEmpty(const Span* list) {
91 return list->next == list;
92}
93
94// Add span to the front of list.
95void DLL_Prepend(Span* list, Span* span);
96
97// Return the length of the linked list. O(n)
98int DLL_Length(const Span* list);
99
100} // namespace tcmalloc
101
102#endif // TCMALLOC_SPAN_H_