Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 1 | // -*- 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 | |
| 43 | namespace tcmalloc { |
| 44 | |
| 45 | #ifdef SPAN_HISTORY |
| 46 | void Event(Span* span, char op, int v = 0) { |
| 47 | span->history[span->nexthistory] = op; |
| 48 | span->value[span->nexthistory] = v; |
| 49 | span->nexthistory++; |
| 50 | if (span->nexthistory == sizeof(span->history)) span->nexthistory = 0; |
| 51 | } |
| 52 | #endif |
| 53 | |
| 54 | Span* NewSpan(PageID p, Length len) { |
| 55 | Span* result = Static::span_allocator()->New(); |
| 56 | memset(result, 0, sizeof(*result)); |
| 57 | result->start = p; |
| 58 | result->length = len; |
| 59 | #ifdef SPAN_HISTORY |
| 60 | result->nexthistory = 0; |
| 61 | #endif |
| 62 | return result; |
| 63 | } |
| 64 | |
| 65 | void DeleteSpan(Span* span) { |
| 66 | #ifndef NDEBUG |
| 67 | // In debug mode, trash the contents of deleted Spans |
| 68 | memset(span, 0x3f, sizeof(*span)); |
| 69 | #endif |
| 70 | Static::span_allocator()->Delete(span); |
| 71 | } |
| 72 | |
| 73 | void DLL_Init(Span* list) { |
| 74 | list->next = list; |
| 75 | list->prev = list; |
| 76 | } |
| 77 | |
| 78 | void DLL_Remove(Span* span) { |
| 79 | span->prev->next = span->next; |
| 80 | span->next->prev = span->prev; |
| 81 | span->prev = NULL; |
| 82 | span->next = NULL; |
| 83 | } |
| 84 | |
| 85 | int DLL_Length(const Span* list) { |
| 86 | int result = 0; |
| 87 | for (Span* s = list->next; s != list; s = s->next) { |
| 88 | result++; |
| 89 | } |
| 90 | return result; |
| 91 | } |
| 92 | |
| 93 | void DLL_Prepend(Span* list, Span* span) { |
| 94 | ASSERT(span->next == NULL); |
| 95 | ASSERT(span->prev == NULL); |
| 96 | span->next = list->next; |
| 97 | span->prev = list; |
| 98 | list->next->prev = span; |
| 99 | list->next = span; |
| 100 | } |
| 101 | |
| 102 | } // namespace tcmalloc |