 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 // --- Author: Rebecca Shapiro <bxx@google.com> This file contains | |
| 31 // definitions of functions that implement doubly linked lists and | |
| 32 // functions that implement singly linked lists. The basic singly | |
| 33 // linked list functions use void * as storage. The original author | |
| 34 // of that code is Sanjay Ghemawat <opensource@google.com> The basic | |
| 35 // doubly linked list functions store pointers to other nodes as void | |
| 36 // * pointers directly inline. A node of this list needs to be big | |
| 37 // enough to hold 2 void * pointers, this generally means that each | |
| 38 // node needs at least 16 byes (on a 64 bit machine). The next pointer | |
| 39 // is written into the first 8 bytes of the node and the previous | |
| 40 // pointer is writtend into the second 8 bytes. All pointers point to | |
| 41 // the first byte of the next/previous node. The meaning of | |
| 42 // "previous" and "next" is fairly arbitrary, but consistent. It | |
| 43 // turns out that when the head is popped, the new head becomes | |
| 44 // whatever is pointed to by the previous head's next pointer. The | |
| 45 // head's previous pointer points to NULL, the node at the end of the | |
| 46 // list has a next pointer that also poitns to NULL. | |
| 47 #ifndef TCMALLOC_FREE_LIST_H_ | |
| 48 #define TCMALLOC_FREE_LIST_H_ | |
| 49 | |
| 50 #include <stddef.h> | |
| 51 | |
| 52 namespace tcmalloc { | |
| 53 | |
| 54 #ifdef TCMALLOC_USE_DOUBLYLINKED_FREELIST | |
| 55 static const int kPointerSize = sizeof(void *); | |
| 56 | |
| 57 // size class information for common.h/common.cc. | |
| 58 #if defined _LP64 | |
| 
jschuh
2011/08/24 17:22:30
For windows I'm pretty sure you'll need something
 
bxx
2011/08/24 22:02:09
Done. As a general question: if the binaries we co
 | |
| 59 // 8 byte pointers require that min class size holds at least 16 bytes. | |
| 60 #define AVOID_8_BYTE_CLASSES 1 | |
| 
jschuh
2011/08/24 17:22:30
I really dislike this macro name. Maybe something
 
bxx
2011/08/24 22:02:09
Done.
 | |
| 61 #define FIRST_USABLE_SIZE_CLASS 2 | |
| 
jschuh
2011/08/24 17:22:30
I don't like this one either. Maybe something like
 
bxx
2011/08/24 22:02:09
I need to allow for a size class for size 0 malloc
 | |
| 62 #else | |
| 63 #define AVOID_8_BYTE_CLASSES 0 | |
| 64 #define FIRST_USABLE_SIZE_CLASS 1 | |
| 65 #endif | |
| 66 | |
| 67 void *FL_Previous(void *t); | |
| 68 void *FL_Next(void *t); | |
| 69 void *FL_SetPrevious(void *t, void *n); | |
| 70 void *FL_SetNext(void *t, void *n); | |
| 71 void FL_Init(void *t); | |
| 72 void FL_Push(void **list, void *element); | |
| 73 void *FL_Pop(void **list); | |
| 74 void FL_PopRange(void **head, int N, void **start, void **end); | |
| 75 void FL_PushRange(void **head, void *start, void *end); | |
| 76 | |
| 77 #else // TCMALLOC_USE_DOUBLYLINKED_FREELIST not defined | |
| 78 | |
| 79 // size class information for common.h/common.cc. | |
| 80 #define AVOID_8_BYTE_CLASSES 0 | |
| 81 #define FIRST_USABLE_SIZE_CLASS 1 | |
| 82 | |
| 83 inline void *FL_Next(void *t) { | |
| 84 return *(reinterpret_cast<void**>(t)); | |
| 85 } | |
| 86 | |
| 87 inline void FL_SetNext(void *t, void *n) { | |
| 88 *(reinterpret_cast<void**>(t)) = n; | |
| 89 } | |
| 90 | |
| 91 inline void FL_Init(void *t) { | |
| 92 FL_SetNext(t, NULL); | |
| 93 } | |
| 94 | |
| 95 inline void FL_Push(void **list, void *element) { | |
| 96 FL_SetNext(element, *list); | |
| 97 *list = element; | |
| 98 } | |
| 99 | |
| 100 inline void *FL_Pop(void **list) { | |
| 101 void *result = *list; | |
| 102 *list = FL_Next(*list); | |
| 103 return result; | |
| 104 } | |
| 105 | |
| 106 // Remove N elements from a linked list to which head points. head will be | |
| 107 // modified to point to the new head. start and end will point to the first | |
| 108 // and last nodes of the range. Note that end will point to NULL after this | |
| 109 // function is called. | |
| 110 inline void FL_PopRange(void **head, int N, void **start, void **end) { | |
| 111 if (N == 0) { | |
| 112 *start = NULL; | |
| 113 *end = NULL; | |
| 114 return; | |
| 115 } | |
| 116 | |
| 117 *start = *head; | |
| 118 void *tmp = *head; | |
| 119 for (int i = 1; i < N; ++i) { // Find the Nth element to pop. | |
| 120 tmp = FL_Next(tmp); | |
| 121 } | |
| 122 *end = tmp; | |
| 123 | |
| 124 *head = FL_Next(tmp); // Unlink range from original list. | |
| 125 FL_SetNext(tmp, NULL); | |
| 126 } | |
| 127 | |
| 128 inline void FL_PushRange(void **head, void *start, void *end) { | |
| 129 if (!start) return; | |
| 130 FL_SetNext(end, *head); | |
| 131 *head = start; | |
| 132 } | |
| 133 | |
| 134 #endif // TCMALLOC_USE_DOUBLYLINKED_FREELIST | |
| 135 | |
| 136 inline size_t FL_Size(void *head) { | |
| 137 int count = 0; | |
| 138 while (head) { | |
| 139 count++; | |
| 140 head = FL_Next(head); | |
| 141 } | |
| 142 return count; | |
| 143 } | |
| 144 | |
| 145 } // namespace tcmalloc | |
| 146 | |
| 147 #endif // TCMALLOC_FREE_LIST_H_ | |
| OLD | NEW |