blob: eac43f4d649c4e25a3021429927757bde555e84b [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#include <config.h>
35#include "span.h"
36
37#include <string.h> // for NULL, memset
38
39#include "internal_logging.h" // for ASSERT
40#include "page_heap_allocator.h" // for PageHeapAllocator
41#include "static_vars.h" // for Static
42
43namespace tcmalloc {
44
Austin Schuh745610d2015-09-06 18:19:50 -070045Span* NewSpan(PageID p, Length len) {
46 Span* result = Static::span_allocator()->New();
47 memset(result, 0, sizeof(*result));
48 result->start = p;
49 result->length = len;
Austin Schuh745610d2015-09-06 18:19:50 -070050 return result;
51}
52
53void DeleteSpan(Span* span) {
54#ifndef NDEBUG
55 // In debug mode, trash the contents of deleted Spans
56 memset(span, 0x3f, sizeof(*span));
57#endif
58 Static::span_allocator()->Delete(span);
59}
60
61void DLL_Init(Span* list) {
62 list->next = list;
63 list->prev = list;
64}
65
66void DLL_Remove(Span* span) {
67 span->prev->next = span->next;
68 span->next->prev = span->prev;
69 span->prev = NULL;
70 span->next = NULL;
71}
72
73int DLL_Length(const Span* list) {
74 int result = 0;
75 for (Span* s = list->next; s != list; s = s->next) {
76 result++;
77 }
78 return result;
79}
80
81void DLL_Prepend(Span* list, Span* span) {
82 ASSERT(span->next == NULL);
83 ASSERT(span->prev == NULL);
84 span->next = list->next;
85 span->prev = list;
86 list->next->prev = span;
87 list->next = span;
88}
89
90} // namespace tcmalloc