linked_list.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. // Some very basic linked list functions for dealing with using void * as
  34. // storage.
  35. #ifndef TCMALLOC_LINKED_LIST_H_
  36. #define TCMALLOC_LINKED_LIST_H_
  37. #include <stddef.h>
  38. namespace tcmalloc {
  39. inline void *SLL_Next(void *t) {
  40. return *(reinterpret_cast<void**>(t));
  41. }
  42. inline void SLL_SetNext(void *t, void *n) {
  43. *(reinterpret_cast<void**>(t)) = n;
  44. }
  45. inline void SLL_Push(void **list, void *element) {
  46. SLL_SetNext(element, *list);
  47. *list = element;
  48. }
  49. inline void *SLL_Pop(void **list) {
  50. void *result = *list;
  51. *list = SLL_Next(*list);
  52. return result;
  53. }
  54. // Remove N elements from a linked list to which head points. head will be
  55. // modified to point to the new head. start and end will point to the first
  56. // and last nodes of the range. Note that end will point to NULL after this
  57. // function is called.
  58. inline void SLL_PopRange(void **head, int N, void **start, void **end) {
  59. if (N == 0) {
  60. *start = NULL;
  61. *end = NULL;
  62. return;
  63. }
  64. void *tmp = *head;
  65. for (int i = 1; i < N; ++i) {
  66. tmp = SLL_Next(tmp);
  67. }
  68. *start = *head;
  69. *end = tmp;
  70. *head = SLL_Next(tmp);
  71. // Unlink range from list.
  72. SLL_SetNext(tmp, NULL);
  73. }
  74. inline void SLL_PushRange(void **head, void *start, void *end) {
  75. if (!start) return;
  76. SLL_SetNext(end, *head);
  77. *head = start;
  78. }
  79. inline size_t SLL_Size(void *head) {
  80. int count = 0;
  81. while (head) {
  82. count++;
  83. head = SLL_Next(head);
  84. }
  85. return count;
  86. }
  87. } // namespace tcmalloc
  88. #endif // TCMALLOC_LINKED_LIST_H_