| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2011, Google Inc. |
| 2 // All rights reserved. |
| 3 // |
| 4 // Redistribution and use in source and binary forms, with or without |
| 5 // modification, are permitted provided that the following conditions are |
| 6 // met: |
| 7 // |
| 8 // * Redistributions of source code must retain the above copyright |
| 9 // notice, this list of conditions and the following disclaimer. |
| 10 // * Redistributions in binary form must reproduce the above |
| 11 // copyright notice, this list of conditions and the following disclaimer |
| 12 // in the documentation and/or other materials provided with the |
| 13 // distribution. |
| 14 // * Neither the name of Google Inc. nor the names of its |
| 15 // contributors may be used to endorse or promote products derived from |
| 16 // this software without specific prior written permission. |
| 17 // |
| 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 |
| 30 // --- |
| 31 // Author: Rebecca Shapiro <bxx@google.com> |
| 32 // This file contains functions that implement doubly linked |
| 33 // linked lists. |
| 34 |
| 35 #ifdef TCMALLOC_USE_DOUBLYLINKED_FREELIST |
| 36 |
| 37 #include <stddef.h> |
| 38 #include "internal_logging.h" //for ASSERT |
| 39 |
| 40 #define MEMORY_CHECK(v1, v2) if (v1 != v2) DieFromMemoryCorruption(); |
| 41 namespace { |
| 42 // Intentionally causes a segmentation fault. |
| 43 inline void DieFromMemoryCorruption() { |
| 44 char *p = NULL; |
| 45 *p += 1; // Segfault. |
| 46 } |
| 47 |
| 48 // Returns value of the |previous| pointer w/out running a sanity |
| 49 // check. |
| 50 inline void *FL_Previous_No_Check(void *t) { |
| 51 return reinterpret_cast<void**>(t)[1]; |
| 52 } |
| 53 |
| 54 // Returns value of the |next| pointer w/out running a sanity check. |
| 55 inline void *FL_Next_No_Check(void *t) { |
| 56 return reinterpret_cast<void**>(t)[0]; |
| 57 } |
| 58 |
| 59 } // namespace |
| 60 |
| 61 namespace tcmalloc { |
| 62 void *FL_Previous(void *t) { |
| 63 void *previous = FL_Previous_No_Check(t); |
| 64 if (previous) |
| 65 MEMORY_CHECK(FL_Next_No_Check(previous), t); |
| 66 return previous; |
| 67 } |
| 68 |
| 69 void *FL_Next(void *t) { |
| 70 void *next = FL_Next_No_Check(t); |
| 71 if (next) |
| 72 MEMORY_CHECK(FL_Previous_No_Check(next), t); |
| 73 return next; |
| 74 } |
| 75 |
| 76 inline void FL_SetPrevious(void *t, void *n) { |
| 77 reinterpret_cast<void**>(t)[1] = n; |
| 78 } |
| 79 |
| 80 inline void FL_SetNext(void *t, void *n) { |
| 81 reinterpret_cast<void**>(t)[0] = n; |
| 82 } |
| 83 |
| 84 // Makes the element at |t| a singleton doubly linked list. |
| 85 inline void FL_Init(void *t) { |
| 86 FL_SetPrevious(t, NULL); |
| 87 FL_SetNext(t, NULL); |
| 88 } |
| 89 |
| 90 // Pushes element to a linked list whose first element is at |
| 91 // |*list|. When this call returns, |list| will point to the new head |
| 92 // of the linked list. |
| 93 void FL_Push(void **list, void *element) { |
| 94 void *old = *list; |
| 95 if (old == NULL) { // Builds singleton list. |
| 96 FL_Init(element); |
| 97 } else { |
| 98 ASSERT(FL_Previous_No_Check(old) == NULL); |
| 99 FL_SetNext(element, old); |
| 100 FL_SetPrevious(old, element); |
| 101 FL_SetPrevious(element, NULL); |
| 102 } |
| 103 *list = element; |
| 104 } |
| 105 |
| 106 // Pops the top element off the linked list whose first element is at |
| 107 // |*list|, and updates |*list| to point to the next element in the |
| 108 // list. Returns the address of the element that was removed from the |
| 109 // linked list. |list| must not be NULL. |
| 110 void *FL_Pop(void **list) { |
| 111 void *result = *list; |
| 112 ASSERT(FL_Previous_No_Check(result) == NULL); |
| 113 *list = FL_Next(result); |
| 114 if (*list != NULL) |
| 115 FL_SetPrevious(*list, NULL); |
| 116 |
| 117 return result; |
| 118 } |
| 119 |
| 120 // Remove |n| elements from linked list at whose first element is at |
| 121 // |*head|. |head| will be modified to point to the new head. |
| 122 // |start| will point to the first node of the range, |end| will point |
| 123 // to the last node in the range. |n| must be <= FL_Size(|*head|) |
| 124 // If |n| > 0, |head| must not be NULL. |
| 125 void FL_PopRange(void **head, int n, void **start, void **end) { |
| 126 if (n == 0) { |
| 127 *start = NULL; |
| 128 *end = NULL; |
| 129 return; |
| 130 } |
| 131 |
| 132 *start = *head; // Remember the first node in the range. |
| 133 void *tmp = *head; |
| 134 for (int i = 1; i < n; ++i) // Find end of range. |
| 135 tmp = FL_Next(tmp); |
| 136 |
| 137 *end = tmp; // |end| now set to point to last node in range. |
| 138 *head = FL_Next(*end); |
| 139 FL_SetNext(*end, NULL); // Unlink range from list. |
| 140 |
| 141 if (*head ) { // Fixup popped list. |
| 142 FL_SetPrevious(*head, NULL); |
| 143 } |
| 144 } |
| 145 |
| 146 // Pushes the nodes in the list begginning at |start| whose last node |
| 147 // is |end| into the linked list at |*head|. |*head| is updated to |
| 148 // point be the new head of the list. |head| must not be NULL. |
| 149 void FL_PushRange(void **head, void *start, void *end) { |
| 150 if (!start) return; |
| 151 |
| 152 // Sanity checking of ends of list to push is done by calling |
| 153 // FL_Next and FL_Previous. |
| 154 FL_Next(start); |
| 155 FL_Previous(end); |
| 156 ASSERT(FL_Previous_No_Check(start) == NULL); |
| 157 ASSERT(FL_Next_No_Check(end) == NULL); |
| 158 |
| 159 if (*head) { |
| 160 MEMORY_CHECK(FL_Previous_No_Check(*head), NULL); |
| 161 FL_SetNext(end, *head); |
| 162 FL_SetPrevious(*head, end); |
| 163 } |
| 164 *head = start; |
| 165 } |
| 166 |
| 167 } // namespace tcmalloc |
| 168 |
| 169 #endif // TCMALLOC_USE_DOUBLYLINKED_FREELIST |
| OLD | NEW |