span.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. // Author: Sanjay Ghemawat <opensource@google.com>
  32. #include <config.h>
  33. #include "span.h"
  34. #include <string.h> // for NULL, memset
  35. #include "internal_logging.h" // for ASSERT
  36. #include "page_heap_allocator.h" // for PageHeapAllocator
  37. #include "static_vars.h" // for Static
  38. namespace tcmalloc {
  39. #ifdef SPAN_HISTORY
  40. void Event(Span* span, char op, int v = 0) {
  41. span->history[span->nexthistory] = op;
  42. span->value[span->nexthistory] = v;
  43. span->nexthistory++;
  44. if (span->nexthistory == sizeof(span->history)) span->nexthistory = 0;
  45. }
  46. #endif
  47. Span* NewSpan(PageID p, Length len) {
  48. Span* result = Static::span_allocator()->New();
  49. memset(result, 0, sizeof(*result));
  50. result->start = p;
  51. result->length = len;
  52. #ifdef SPAN_HISTORY
  53. result->nexthistory = 0;
  54. #endif
  55. return result;
  56. }
  57. void DeleteSpan(Span* span) {
  58. #ifndef NDEBUG
  59. // In debug mode, trash the contents of deleted Spans
  60. memset(span, 0x3f, sizeof(*span));
  61. #endif
  62. Static::span_allocator()->Delete(span);
  63. }
  64. void DLL_Init(Span* list) {
  65. list->next = list;
  66. list->prev = list;
  67. }
  68. void DLL_Remove(Span* span) {
  69. span->prev->next = span->next;
  70. span->next->prev = span->prev;
  71. span->prev = NULL;
  72. span->next = NULL;
  73. }
  74. int DLL_Length(const Span* list) {
  75. int result = 0;
  76. for (Span* s = list->next; s != list; s = s->next) {
  77. result++;
  78. }
  79. return result;
  80. }
  81. void 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. } // namespace tcmalloc