OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright 2014 Google Inc. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license that can be | |
5 * found in the LICENSE file. | |
6 */ | |
7 | |
8 #ifndef SkFreeList_DEFINED | |
9 #define SkFreeList_DEFINED | |
10 | |
11 #include "SkTInternalSList.h" | |
robertphillips
2014/03/07 16:01:35
Is this needed?
iancottrell
2014/03/07 16:11:14
Yes.
| |
12 #include "SkRect.h" | |
robertphillips
2014/03/07 16:01:35
Is this needed?
iancottrell
2014/03/07 16:11:14
Done.
| |
13 #include "SkTDArray.h" | |
robertphillips
2014/03/07 16:01:35
Do we need to include SkBBoxHierarchy.h?
iancottrell
2014/03/07 16:11:14
Done.
| |
14 #include "SkBBoxHierarchy.h" | |
15 | |
16 | |
17 /** | |
18 * An implementation of a self growing free list of objects. | |
reed1
2014/03/07 15:18:38
I still don't grok what this class does. Is it eff
robertphillips
2014/03/07 16:01:35
Apropos this comment, renaming it SkBlockMgr might
iancottrell
2014/03/07 16:02:34
I have renamed some stuff and added comments to ma
| |
19 * Constructors and destructors will be called on the objects. | |
20 * All memory will be reclaimed when the free list is destroyed, so the free | |
21 * list must survive longer than you are using any item taken from it. | |
22 */ | |
robertphillips
2014/03/07 16:01:35
growth -> numItemsPerBlock?
iancottrell
2014/03/07 16:11:14
Done.
| |
23 template<typename T, int growth = 4096/sizeof(T)> class SkFreeList { | |
24 public: | |
25 SkFreeList() {} | |
26 ~SkFreeList() { | |
robertphillips
2014/03/07 16:01:35
Couldn't we add an assert here that all the manage
iancottrell
2014/03/07 16:11:14
There is no reason that should have to be the case
| |
27 while(!fBlocks.isEmpty()) { SkDELETE(fBlocks.pop()); } | |
reed1
2014/03/07 15:18:38
style nit:
while (..) {
...
}
iancottrell
2014/03/07 16:02:34
Done.
| |
28 } | |
29 | |
30 /** | |
31 * Push an item into the free list. | |
32 * The item does not have to have come from the free list, but if it did not | |
33 * it must have a lifetime greater than the free list does. | |
34 * This method is *not* thread safe. | |
35 */ | |
36 void push(T* entry) { | |
37 fAvailable.push(entry); | |
38 } | |
39 | |
40 /** | |
41 * Takes all the items from another list. | |
reed1
2014/03/07 15:18:38
- what is the state of other after this call?
- in
iancottrell
2014/03/07 16:02:34
Sorry, I fixed the docs on list, but this was copi
| |
42 */ | |
43 void takeAll(SkTInternalSList<T>* other) { | |
44 fAvailable.takeAll(other); | |
45 } | |
46 | |
47 /** | |
48 * Get an item from the free list. | |
49 * If the free list has no items, it will grow. | |
50 * The free list will never shrink, it will use the maximum memory it has | |
51 * ever reached until destroyed. | |
52 * The returned item is only valid as long as the free list has not been | |
53 * destroyed, at that point all memory allocated by grow will have been | |
54 * reclaimed. | |
55 * This method is *not* thread safe. | |
56 */ | |
57 T* pop() { | |
reed1
2014/03/07 15:18:38
wow, this name is really confusing, since pop() is
iancottrell
2014/03/07 16:02:34
Renamed, and comments added.
| |
58 if (fAvailable.isEmpty()) { grow(); } | |
reed1
2014/03/07 15:18:38
style nit:
if (...) {
...
}
iancottrell
2014/03/07 16:02:34
Done.
| |
59 return fAvailable.pop(); | |
60 } | |
61 private: | |
62 /** | |
63 * The type for a new block of entries for the list. | |
64 */ | |
65 struct Block { | |
66 T entries[growth]; | |
67 SK_DECLARE_INTERNAL_SLIST_INTERFACE(Block); | |
68 }; | |
69 SkTInternalSList<Block> fBlocks; | |
70 SkTInternalSList<T> fAvailable; | |
71 | |
72 /** | |
73 * When the free list runs out of items, this method is called to allocate | |
74 * a new block of them. | |
75 * It calls the constructors and then pushes the nodes into the available | |
76 * list. | |
77 */ | |
78 void grow() { | |
79 Block* block = SkNEW(Block); | |
80 fBlocks.push(block); | |
81 for(int index = 0; index < growth; ++index) { | |
robertphillips
2014/03/07 16:01:35
this->push
iancottrell
2014/03/07 16:11:14
Already changed.
| |
82 push(&block->entries[index]); | |
83 } | |
84 } | |
85 | |
86 }; | |
87 | |
88 #endif | |
OLD | NEW |