 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> | |
| 31 | |
| 32 // This file contains declarations of functions that implement doubly | |
| 33 // linked lists and definitions of functions that implement singly | |
| 34 // linked lists. The singly linked lists are null terminated, use raw | |
| 35 // pointers to link neighboring elements, and these pointers are | |
| 36 // stored at the start of each element, independently of the | |
| 37 // elements's size. Because pointers are stored within each element, | |
| 38 // each element must be large enough to store two raw pointers if | |
| 39 // doubly linked lists are employed, or one raw pointer if singly | |
| 40 // linked lists are employed. On machines with 64 bit pointers, this | |
| 41 // means elements must be at least 16 bytes in size for doubly linked | |
| 42 // list support, and 8 bytes for singly linked list support. No | |
| 43 // attempts are made to preserve the data in elements stored in the | |
| 44 // list. | |
| 45 // | |
| 46 // Given a machine with pointers of size N (on a 64bit machine N=8, on | |
| 47 // a 32bit machine, N=4), the list pointers are stored in the | |
| 48 // following manner: | |
| 49 // -In doubly linked lists, the |next| pointer is stored in the first N | |
| 50 // bytes of the node and the |previous| pointer is writtend into the | |
| 51 // second N bytes. | |
| 52 // -In singly linked lists, the |next| pointer is stored in the first N | |
| 53 // bytes of the node. | |
| 54 // | |
| 55 // For both types of lists: when a pop operation is performed on a non | |
| 56 // empty list, the new list head becomes that which is pointed to by | |
| 57 // the former head's |next| pointer. If the list is doubly linked, the | |
| 58 // new head |previous| pointer gets changed from pointing to the former | |
| 59 // head to NULL. | |
| 60 // | |
| 61 // The original author of the singly liked list code is Sanjay | |
| 62 // Ghemawat <opensource@google.com>. | |
| 63 #ifndef TCMALLOC_FREE_LIST_H_ | |
| 64 #define TCMALLOC_FREE_LIST_H_ | |
| 65 | |
| 66 #include <stddef.h> | |
| 67 | |
| 68 namespace tcmalloc { | |
| 69 | |
| 70 #ifdef TCMALLOC_USE_DOUBLYLINKED_FREELIST | |
| 71 static const int kPointerSize = sizeof(void *); | |
| 72 | |
| 73 // size class information for common.h/common.cc. | |
| 74 #if defined(_LP64) || defined(_WIN64) | |
| 75 // 8 byte pointers require that min class size holds at least 16 bytes. | |
| 76 #define HAS_64_BIT_POINTERS 1 | |
| 77 #define FIRST_NON_0_SIZE_CLASS 2 | |
| 78 #else | |
| 79 #define HAS_64_BIT_POINTERS 0 | |
| 80 #define FIRST_NON_0_SIZE_CLASS 1 | |
| 81 #endif | |
| 82 | |
| 83 void *FL_Previous(void *t); | |
| 84 void *FL_Next(void *t); | |
| 85 void *FL_SetPrevious(void *t, void *n); | |
| 86 void *FL_SetNext(void *t, void *n); | |
| 87 void FL_Init(void *t); | |
| 88 void FL_Push(void **list, void *element); | |
| 89 void *FL_Pop(void **list); | |
| 90 void FL_PopRange(void **head, int N, void **start, void **end); | |
| 
jar (doing other things)
2011/08/26 18:56:29
nit: even if the old code (below) used an upper ca
 
bxx
2011/08/26 21:45:10
Done.
 | |
| 91 void FL_PushRange(void **head, void *start, void *end); | |
| 92 | |
| 93 #else // TCMALLOC_USE_DOUBLYLINKED_FREELIST not defined | |
| 94 | |
| 95 // size class information for common.h/common.cc. | |
| 96 #define HAS_64_BIT_POINTERS 0 | |
| 97 #define FIRST_NON_0_SIZE_CLASS 1 | |
| 98 | |
| 99 inline void *FL_Next(void *t) { | |
| 100 return *(reinterpret_cast<void**>(t)); | |
| 101 } | |
| 102 | |
| 103 inline void FL_SetNext(void *t, void *n) { | |
| 104 *(reinterpret_cast<void**>(t)) = n; | |
| 105 } | |
| 106 | |
| 107 inline void FL_Init(void *t) { | |
| 108 FL_SetNext(t, NULL); | |
| 109 } | |
| 110 | |
| 111 inline void FL_Push(void **list, void *element) { | |
| 112 FL_SetNext(element, *list); | |
| 113 *list = element; | |
| 114 } | |
| 115 | |
| 116 inline void *FL_Pop(void **list) { | |
| 117 void *result = *list; | |
| 118 *list = FL_Next(*list); | |
| 119 return result; | |
| 120 } | |
| 121 | |
| 122 // Removes |N| elements from a linked list to which |head| points. | |
| 123 // |head| will be modified to point to the new |head|. |start| and | |
| 124 // |end| will point to the first and last nodes of the range. Note | |
| 125 // that |end| will point to NULL after this function is called. | |
| 126 inline void FL_PopRange(void **head, int N, void **start, void **end) { | |
| 127 if (N == 0) { | |
| 128 *start = NULL; | |
| 129 *end = NULL; | |
| 130 return; | |
| 131 } | |
| 132 | |
| 133 *start = *head; | |
| 134 void *tmp = *head; | |
| 135 for (int i = 1; i < N; ++i) { // Find the |N|th element to pop. | |
| 136 tmp = FL_Next(tmp); | |
| 137 } | |
| 138 *end = tmp; | |
| 139 | |
| 140 *head = FL_Next(tmp); // Unlink range from original list. | |
| 141 FL_SetNext(tmp, NULL); | |
| 142 } | |
| 143 | |
| 144 inline void FL_PushRange(void **head, void *start, void *end) { | |
| 145 if (!start) return; | |
| 146 FL_SetNext(end, *head); | |
| 147 *head = start; | |
| 148 } | |
| 149 | |
| 150 #endif // TCMALLOC_USE_DOUBLYLINKED_FREELIST | |
| 151 #define FIRST_NON_0_SIZE FIRST_NON_0_SIZE_CLASS * 2 | |
| 152 | |
| 153 inline size_t FL_Size(void *head) { | |
| 154 int count = 0; | |
| 155 while (head) { | |
| 156 count++; | |
| 157 head = FL_Next(head); | |
| 158 } | |
| 159 return count; | |
| 160 } | |
| 161 | |
| 162 } // namespace tcmalloc | |
| 163 | |
| 164 #endif // TCMALLOC_FREE_LIST_H_ | |
| OLD | NEW |