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