 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 <assert.h> | |
| 38 #include <stddef.h> | |
| 39 | |
| 40 #define MEMORY_CHECK(v1, v2) if (v1 != v2) DieFromMemoryCorruption(); | |
| 41 #define ASSERT(x) assert(x) | |
| 42 namespace { | |
| 43 // Intentionally cause a segmentation fault | |
| 44 inline void DieFromMemoryCorruption() { | |
| 45 char *p = NULL; | |
| 46 *p += 1; // Segv | |
| 47 } | |
| 48 | |
| 49 // Returns value of the Previous pointer w/out running a sanity check | |
| 50 inline void *FL_Previous_No_Check(void *t) { | |
| 51 return *(reinterpret_cast<void**> (static_cast<void **>(t) + 1)); | |
| 
jar (doing other things)
2011/08/20 02:40:30
I suspect (not sure) this code would be a lot clea
 
bxx
2011/08/24 00:19:24
I am currently following the style in which the si
 
jar (doing other things)
2011/08/25 02:07:50
This file is entirely written by you, so things li
 | |
| 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 ASSERT(FL_Next_No_Check(previous) == t); | |
| 
jar (doing other things)
2011/08/20 02:40:30
I would expect these to be places where we validat
 
bxx
2011/08/24 00:19:24
Done.
 | |
| 66 return previous; | |
| 67 } | |
| 68 | |
| 69 void *FL_Next(void *t) { | |
| 70 void *next = FL_Next_No_Check(t); | |
| 71 if (next) | |
| 72 ASSERT(FL_Previous_No_Check(next) == t); | |
| 
jar (doing other things)
2011/08/20 02:40:30
Again, this this should IMO definitely be MEMORY_C
 
bxx
2011/08/24 00:19:24
Done.
 | |
| 73 return next; | |
| 74 } | |
| 75 | |
| 76 inline void FL_SetPrevious(void *t, void *n) { | |
| 77 *(reinterpret_cast<void**> (static_cast<void **>(t) + 1)) = n; | |
| 
jar (doing other things)
2011/08/20 02:40:30
I don't think the second cast (on the left) is nee
 
bxx
2011/08/24 00:19:24
Done.
 | |
| 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 | |
| 85 // doubly linked list. | |
| 86 inline void FL_Init(void *t) { | |
| 87 FL_SetPrevious(t, NULL); | |
| 88 FL_SetNext(t, NULL); | |
| 89 } | |
| 90 | |
| 91 // Pushes element to a linked list | |
| 92 // located at *list. When this call returns, list | |
| 93 // will point to the new head of the linked list. | |
| 
jar (doing other things)
2011/08/20 02:40:30
nit: use 80 characters of width so you don't need
 
bxx
2011/08/24 00:19:24
Done.
 | |
| 94 void FL_Push(void **list, void *element) { | |
| 95 void *old = *list; | |
| 96 if (old == NULL) { // build singleton list | |
| 
jar (doing other things)
2011/08/20 02:40:30
nit: Comments should start with caps, be a complet
 
bxx
2011/08/24 00:19:24
I have been fixing this in my code, but I see that
 | |
| 97 FL_Init(element); | |
| 98 } else { | |
| 99 if (FL_Next(old)) | |
| 
jar (doing other things)
2011/08/20 02:40:30
This is going deeper into the list than any pointe
 
bxx
2011/08/24 00:19:24
Done.
 | |
| 100 MEMORY_CHECK(FL_Previous(FL_Next(old)), old); | |
| 
jar (doing other things)
2011/08/20 02:40:30
At best, you should use the *_No_Check versions in
 
bxx
2011/08/24 00:19:24
The potential issue I see with these NULL checks a
 | |
| 101 FL_SetNext(element, old); | |
| 102 FL_SetPrevious(old, element); | |
| 103 FL_SetPrevious(element, NULL); | |
| 104 } | |
| 105 *list = element; | |
| 106 } | |
| 107 | |
| 108 // Pops the top element off the linked list that begins at | |
| 109 // *list, and upates *list to point to the next element | |
| 110 // in the list. Return the address of the element that | |
| 111 // was removed from the linked list. *list must not be NULL. | |
| 112 void *FL_Pop(void **list) { | |
| 113 void *result = *list; | |
| 
jar (doing other things)
2011/08/20 02:40:30
Here again I'd suggest the test:
MEMORY_CHECK(FL_
 
bxx
2011/08/24 00:19:24
using ASSERT
On 2011/08/20 02:40:30, jar wrote:
 | |
| 114 if (result && FL_Next(result)) // pointer sanity check | |
| 
jar (doing other things)
2011/08/20 02:40:30
result is always non-null (unless we make a mistak
 
bxx
2011/08/24 00:19:24
Done.
 | |
| 115 MEMORY_CHECK(FL_Previous(FL_Next(result)), result); | |
| 
jar (doing other things)
2011/08/20 02:40:30
Again, the above does this same test for 3 times (
 
bxx
2011/08/24 00:19:24
Done.
 | |
| 116 | |
| 117 *list = FL_Next(*list); // Fix remainder of list | |
| 118 if (*list != NULL) | |
| 119 FL_SetPrevious(*list, NULL); | |
| 120 | |
| 121 return result; | |
| 122 } | |
| 123 | |
| 124 // Remove N elements from a linked list to which head points. head will be | |
| 125 // modified to point to the new head. start will point to the first | |
| 126 // node of the range, end points to last node in range | |
| 127 // This function assumes that you aren't trying to pop N > FL_Size(*head) | |
| 128 // nodes, and that *head is not NULL. | |
| 129 void FL_PopRange(void **head, int N, void **start, void **end) { | |
| 130 if (N == 0) { | |
| 131 *start = NULL; | |
| 132 *end = NULL; | |
| 133 return; | |
| 134 } | |
| 135 | |
| 136 *start = *head; // remember the first node in the range | |
| 137 void *tmp = *head; | |
| 138 for (int i = 1; i < N; ++i) // find end of range | |
| 139 tmp = FL_Next(tmp); | |
| 
jar (doing other things)
2011/08/20 02:40:30
This code is definitely a place to do validation,
 
bxx
2011/08/24 00:19:24
Done.
 | |
| 140 | |
| 141 *end = tmp; // end now set to point to last node in range | |
| 142 *head = FL_Next(*end); | |
| 143 FL_SetNext(*end, NULL); // Unlink range from list. | |
| 144 | |
| 145 if (*head && FL_Next(*head)) { // fixup popped list | |
| 146 MEMORY_CHECK(FL_Previous(FL_Next(*head)), *head); | |
| 
jar (doing other things)
2011/08/20 02:40:30
This is again going one step too far in the checki
 
bxx
2011/08/24 00:19:24
Done.
 | |
| 147 FL_SetPrevious(*head, NULL); | |
| 
jar (doing other things)
2011/08/20 02:40:30
This is seemingly a bug.  We don't need to conditi
 
bxx
2011/08/24 00:19:24
Done.
 | |
| 148 } | |
| 149 } | |
| 150 | |
| 151 // Pushes the nodes of the doubly linked list that begins at the | |
| 152 // node located at start and ends at the node end | |
| 153 // into the linked list at location *head | |
| 154 // *head is updated to point be the new head of the list. | |
| 155 // *head must not be NULL. | |
| 156 void FL_PushRange(void **head, void *start, void *end) { | |
| 157 if (!start) return; | |
| 
jar (doing other things)
2011/08/20 02:40:30
I'd suggest either asserting that end is NULL, or
 
bxx
2011/08/24 00:19:24
We cannot make the assumption that if start is NUL
 | |
| 158 | |
| 159 // Sanity check ends of list to push before pushing | |
| 160 if (FL_Next(start)) | |
| 161 MEMORY_CHECK(FL_Previous(FL_Next(start)), start); | |
| 
jar (doing other things)
2011/08/20 02:40:30
I have mixed feelings about whether that test is c
 
bxx
2011/08/24 00:19:24
Used ASSERT.
On 2011/08/20 02:40:30, jar wrote:
 | |
| 162 if (FL_Previous(end)) | |
| 163 MEMORY_CHECK(FL_Next(FL_Previous(end)), end); | |
| 
jar (doing other things)
2011/08/20 02:40:30
I'd also vote for a check here:
ASSERT(FL_Next_No_
 
bxx
2011/08/24 00:19:24
Done.
 | |
| 164 | |
| 165 if (*head != NULL) { | |
| 166 if (FL_Next(*head)) | |
| 167 MEMORY_CHECK(FL_Previous(FL_Next(*head)), *head); | |
| 168 | |
| 169 FL_SetNext(end, *head); | |
| 170 FL_SetPrevious(*head, end); | |
| 
jar (doing other things)
2011/08/20 02:40:30
Again I'd like to first see a:
MEMORY_CHECK(FL_Pre
 
bxx
2011/08/24 00:19:24
Used ASSERT
On 2011/08/20 02:40:30, jar wrote:
 | |
| 171 } | |
| 172 *head = start; | |
| 173 } | |
| 174 | |
| 175 } // namespace tcmalloc | |
| 176 | |
| 177 #endif // TCMALLOC_USE_DOUBLYLINKED_FREELIST | |
| OLD | NEW |