 Chromium Code Reviews
 Chromium Code Reviews Issue 7671034:
  doubly-linked free-lists for thread caches and page heaps  (Closed) 
  Base URL: http://git.chromium.org/git/chromium.git@trunk
    
  
    Issue 7671034:
  doubly-linked free-lists for thread caches and page heaps  (Closed) 
  Base URL: http://git.chromium.org/git/chromium.git@trunk| 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); | |
| 
jar (doing other things)
2011/08/26 18:56:29
style nit: don't leave spaces after type, and befo
 
bxx
2011/08/26 21:45:10
Done.
 | |
| 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)); | |
| 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)) = n; | |
| 82 } | |
| 83 | |
| 84 // Makes the memory pointed at |t| a singleton doubly linked list. | |
| 
jar (doing other things)
2011/08/26 18:56:29
nit: "...memory pointed at |t| ..."
  should be:
 
bxx
2011/08/26 21:45:10
Done.
 | |
| 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. | |
| 
jar (doing other things)
2011/08/26 18:56:29
The last line should read:
"|list| must not be NU
 
bxx
2011/08/26 21:45:10
Done.
 | |
| 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 // |*head| must not be NULL. | |
| 
jar (doing other things)
2011/08/26 18:56:29
nit: Delete line 124.  *head can be null IFF N ==
 
bxx
2011/08/26 21:45:10
Done.
 | |
| 125 void FL_PopRange(void **head, int N, void **start, void **end) { | |
| 
jar (doing other things)
2011/08/26 18:56:29
nit: N --->  n
 
bxx
2011/08/26 21:45:10
Original implementation uses N.
 | |
| 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 |