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: code style fixes Created 9 years, 4 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 // ---
31 // Author: Rebecca Shapiro <bxx@google.com>
32 // This file contains definitions of functions that implement
33 // doubly linked lists and functions that implement singly
34 // linked lists.
35 // The basic singly linked list functions use void * as storage. The
36 // original author of that code is Sanjay Ghemawat <opensource@google.com>
37 // The basic doubly linked list functions store pointers
38 // to other nodes as void * pointers directly inline.
39 // A node of this list needs to be big enough to hold 2 void * pointers,
40 // this generally means that each node needs at least 16 byes. The next
jar (doing other things) 2011/08/20 02:40:30 On a 32 bit platform, you don't need anything more
bxx 2011/08/24 00:19:24 What would be the proper way to check the type of
41 // pointer is written into the first 8 bytes of the node and the previous
42 // pointer is writtend into the second 8 bytes. All pointers point to
43 // the first byte of the next/previous node.
44 // The meaning of "previous" and "next" is fairly arbitrary, but consistent.
45 // It turns out that when the head is popped, the new head becomes whatever
46 // is pointed to by the previous head's next pointer. The head's previous
47 // pointer points to NULL, the node at the end of the list has a next
48 // pointer that also poitns to NULL.
49 #ifndef TCMALLOC_FREE_LIST_H_
50 #define TCMALLOC_FREE_LIST_H_
51
52 #include <stddef.h>
53
54 namespace tcmalloc {
55
56 #ifdef TCMALLOC_USE_DOUBLYLINKED_FREELIST
57
58 // size class information for common.h/common.cc
59 #define MIN_SIZE_CLASS 2
jar (doing other things) 2011/08/20 02:40:30 This should really depend on 64 vs 32 bit architec
bxx 2011/08/24 00:19:24 Done.
60 #define FIRST_SIZE_CLASS MIN_SIZE_CLASS
61
62 void *FL_Previous(void *t);
63 void *FL_Next(void *t);
64 void *FL_SetPrevious(void *t, void *n);
65 void *FL_SetNext(void *t, void *n);
66 void FL_Init(void *t);
67 void FL_Push(void **list, void *element);
68 void *FL_Pop(void **list);
69 void FL_PopRange(void **head, int N, void **start, void **end);
70 void FL_PushRange(void **head, void *start, void *end);
71
72 #else // TCMALLOC_USE_DOUBLYLINKED_FREELIST not defined
73
74 // size class information for common.h/common.cc
75 #define MIN_SIZE_CLASS 0
76 #define FIRST_SIZE_CLASS 1
77
78 inline void *FL_Next(void *t) {
79 return *(reinterpret_cast<void**>(t));
80 }
81
82 inline void FL_SetNext(void *t, void *n) {
83 *(reinterpret_cast<void**>(t)) = n;
84 }
85
86 inline void FL_Init(void *t) {
87 FL_SetNext(t, NULL);
88 }
89
90 inline void FL_Push(void **list, void *element) {
91 FL_SetNext(element, *list);
92 *list = element;
93 }
94
95 inline void *FL_Pop(void **list) {
96 void *result = *list;
97 *list = FL_Next(*list);
98 return result;
99 }
100
101 // Remove N elements from a linked list to which head points. head will be
102 // modified to point to the new head. start and end will point to the first
103 // and last nodes of the range. Note that end will point to NULL after this
104 // function is called.
105 inline void FL_PopRange(void **head, int N, void **start, void **end) {
106 if (N == 0) {
107 *start = NULL;
108 *end = NULL;
jar (doing other things) 2011/08/20 02:40:30 It is not a big deal... but this assignment to *en
bxx 2011/08/24 00:19:24 I included this check because I wanted to be consi
109 return;
110 }
111
112 *start = *head;
113 void *tmp = *head;
114 for (int i = 1; i < N; ++i) { // Find the Nth element to pop
115 tmp = FL_Next(tmp);
116 }
117 *end = tmp;
118
119 *head = FL_Next(tmp); // Unlink range from original list
120 FL_SetNext(tmp, NULL);
121 }
122
123 inline void FL_PushRange(void **head, void *start, void *end) {
124 if (!start) return;
125 FL_SetNext(end, *head);
126 *head = start;
127 }
128
129 #endif // TCMALLOC_USE_DOUBLYLINKED_FREELIST
130
131 inline size_t FL_Size(void *head) {
132 int count = 0;
133 while (head) {
134 count++;
135 head = FL_Next(head);
136 }
137 return count;
138 }
139
140 } // namespace tcmalloc
141
142 #endif // TCMALLOC_FREE_LIST_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698