OLD | NEW |
1 #include "SkVarAlloc.h" | 1 #include "SkVarAlloc.h" |
2 | 2 |
3 // We use non-standard malloc diagnostic methods to make sure our allocations ar
e sized well. | 3 // We use non-standard malloc diagnostic methods to make sure our allocations ar
e sized well. |
4 #if defined(SK_BUILD_FOR_MAC) | 4 #if defined(SK_BUILD_FOR_MAC) |
5 #include <malloc/malloc.h> | 5 #include <malloc/malloc.h> |
6 #elif defined(SK_BUILD_FOR_LINUX) | 6 #elif defined(SK_BUILD_FOR_LINUX) |
7 #include <malloc.h> | 7 #include <malloc.h> |
8 #endif | 8 #endif |
9 | 9 |
| 10 enum { |
| 11 kMinLgSize = 4, // The smallest block we'd ever want to allocate is 16B, |
| 12 kMaxLgSize = 16, // and we see no benefit allocating blocks larger than 64K
. |
| 13 }; |
| 14 |
10 struct SkVarAlloc::Block { | 15 struct SkVarAlloc::Block { |
11 Block* prev; | 16 Block* prev; |
12 char* data() { return (char*)(this + 1); } | 17 char* data() { return (char*)(this + 1); } |
13 | 18 |
14 static Block* Alloc(Block* prev, size_t size, unsigned flags) { | 19 static Block* Alloc(Block* prev, size_t size, unsigned flags) { |
15 SkASSERT(size >= sizeof(Block)); | 20 SkASSERT(size >= sizeof(Block)); |
16 Block* b = (Block*)sk_malloc_flags(size, flags); | 21 Block* b = (Block*)sk_malloc_flags(size, flags); |
17 b->prev = prev; | 22 b->prev = prev; |
18 return b; | 23 return b; |
19 } | 24 } |
20 }; | 25 }; |
21 | 26 |
22 SkVarAlloc::SkVarAlloc() | 27 SkVarAlloc::SkVarAlloc() |
23 : fByte(NULL) | 28 : fByte(NULL) |
24 , fRemaining(0) | 29 , fRemaining(0) |
25 , fLgMinSize(4) | 30 , fLgSize(kMinLgSize) |
26 , fBlock(NULL) {} | 31 , fBlock(NULL) {} |
27 | 32 |
28 SkVarAlloc::~SkVarAlloc() { | 33 SkVarAlloc::~SkVarAlloc() { |
29 Block* b = fBlock; | 34 Block* b = fBlock; |
30 while (b) { | 35 while (b) { |
31 Block* prev = b->prev; | 36 Block* prev = b->prev; |
32 sk_free(b); | 37 sk_free(b); |
33 b = prev; | 38 b = prev; |
34 } | 39 } |
35 } | 40 } |
36 | 41 |
37 void SkVarAlloc::makeSpace(size_t bytes, unsigned flags) { | 42 void SkVarAlloc::makeSpace(size_t bytes, unsigned flags) { |
38 SkASSERT(SkIsAlignPtr(bytes)); | 43 SkASSERT(SkIsAlignPtr(bytes)); |
39 | 44 |
40 size_t alloc = 1<<(fLgMinSize++); | 45 size_t alloc = 1<<fLgSize; |
41 while (alloc < bytes + sizeof(Block)) { | 46 while (alloc < bytes + sizeof(Block)) { |
42 alloc *= 2; | 47 alloc *= 2; |
43 } | 48 } |
44 fBlock = Block::Alloc(fBlock, alloc, flags); | 49 fBlock = Block::Alloc(fBlock, alloc, flags); |
45 fByte = fBlock->data(); | 50 fByte = fBlock->data(); |
46 fRemaining = alloc - sizeof(Block); | 51 fRemaining = alloc - sizeof(Block); |
47 | 52 |
| 53 if (fLgSize < kMaxLgSize) { |
| 54 fLgSize++; |
| 55 } |
| 56 |
48 #if defined(SK_BUILD_FOR_MAC) | 57 #if defined(SK_BUILD_FOR_MAC) |
49 SkASSERT(alloc == malloc_good_size(alloc)); | 58 SkASSERT(alloc == malloc_good_size(alloc)); |
50 #elif defined(SK_BUILD_FOR_LINUX) | 59 #elif defined(SK_BUILD_FOR_LINUX) |
51 SkASSERT(alloc == malloc_usable_size(fByte - sizeof(Block))); | 60 SkASSERT(alloc == malloc_usable_size(fByte - sizeof(Block))); |
52 #endif | 61 #endif |
53 } | 62 } |
OLD | NEW |