Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3)

Side by Side Diff: third_party/tcmalloc/chromium/src/free_list.h

Issue 7671034: doubly-linked free-lists for thread caches and page heaps (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: clean up code, improve macro name Created 9 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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
jar (doing other things) 2011/08/25 02:07:50 wording nit: I think you meant to say "declaration
bxx 2011/08/25 20:23:18 Done.
33 // linked list functions use void * as storage. The original author
jar (doing other things) 2011/08/25 02:07:50 wording nit: "The basic singly linked list functio
bxx 2011/08/25 20:23:18 Done.
34 // of that code is Sanjay Ghemawat <opensource@google.com> The basic
jar (doing other things) 2011/08/25 02:07:50 nit: Period before "The basic"
bxx 2011/08/25 20:23:18 Done.
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
jar (doing other things) 2011/08/25 02:07:50 wording nit: the phrase "directly inline" is also
bxx 2011/08/25 20:23:18 Done.
37 // enough to hold 2 void * pointers, this generally means that each
jar (doing other things) 2011/08/25 02:07:50 nit: We always put the * next to the type, with no
bxx 2011/08/25 20:23:18 Done.
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
jar (doing other things) 2011/08/25 02:07:50 nit: this sentence has facts only applicable to 64
bxx 2011/08/25 20:23:18 Done.
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) || defined(_WIN64)
59 // 8 byte pointers require that min class size holds at least 16 bytes.
60 #define IS_64_BIT_POINTERS 1
61 #define FIRST_USABLE_SIZE_CLASS 2
62 #else
63 #define IS_64_BIT_POINTERS 0
64 #define FIRST_USABLE_SIZE_CLASS 1
jar (doing other things) 2011/08/25 02:07:50 This name is still a bit odd, as there is a size c
bxx 2011/08/25 20:23:18 Done.
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 IS_64_BIT_POINTERS 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_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698