OLD | NEW |
1 /* | |
2 * Copyright 2015 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 #include "SkVarAlloc.h" | 1 #include "SkVarAlloc.h" |
9 | 2 |
10 // 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. |
11 #if defined(SK_BUILD_FOR_MAC) | 4 #if defined(SK_BUILD_FOR_MAC) |
12 #include <malloc/malloc.h> | 5 #include <malloc/malloc.h> |
13 #elif defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_FOR_WIN32) | 6 #elif defined(SK_BUILD_FOR_UNIX) || defined(SK_BUILD_FOR_WIN32) |
14 #include <malloc.h> | 7 #include <malloc.h> |
15 #endif | 8 #endif |
16 | 9 |
17 struct SkVarAlloc::Block { | 10 struct SkVarAlloc::Block { |
18 Block* prev; | 11 Block* prev; |
19 char* data() { return (char*)(this + 1); } | 12 char* data() { return (char*)(this + 1); } |
20 | 13 |
21 static Block* Alloc(Block* prev, size_t size, unsigned flags) { | 14 static Block* Alloc(Block* prev, size_t size, unsigned flags) { |
22 SkASSERT(size >= sizeof(Block)); | 15 SkASSERT(size >= sizeof(Block)); |
23 Block* b = (Block*)sk_malloc_flags(size, flags); | 16 Block* b = (Block*)sk_malloc_flags(size, flags); |
24 b->prev = prev; | 17 b->prev = prev; |
25 return b; | 18 return b; |
26 } | 19 } |
27 }; | 20 }; |
28 | 21 |
29 SkVarAlloc::SkVarAlloc(size_t minLgSize) | 22 SkVarAlloc::SkVarAlloc(size_t minLgSize) |
30 : fByte(NULL) | 23 : fByte(NULL) |
31 , fRemaining(0) | 24 , fRemaining(0) |
32 , fLgSize(minLgSize) | 25 , fLgSize(minLgSize) |
33 , fBlock(NULL) {} | 26 , fBlock(NULL) {} |
34 | 27 |
35 SkVarAlloc::SkVarAlloc(size_t minLgSize, char* storage, size_t len) | |
36 : fByte(storage) | |
37 , fRemaining(len) | |
38 , fLgSize(minLgSize) | |
39 , fBlock(NULL) {} | |
40 | |
41 SkVarAlloc::~SkVarAlloc() { | 28 SkVarAlloc::~SkVarAlloc() { |
42 Block* b = fBlock; | 29 Block* b = fBlock; |
43 while (b) { | 30 while (b) { |
44 Block* prev = b->prev; | 31 Block* prev = b->prev; |
45 sk_free(b); | 32 sk_free(b); |
46 b = prev; | 33 b = prev; |
47 } | 34 } |
48 } | 35 } |
49 | 36 |
50 void SkVarAlloc::makeSpace(size_t bytes, unsigned flags) { | 37 void SkVarAlloc::makeSpace(size_t bytes, unsigned flags) { |
(...skipping 27 matching lines...) Expand all Loading... |
78 #endif | 65 #endif |
79 } | 66 } |
80 | 67 |
81 size_t SkVarAlloc::approxBytesAllocated() const { | 68 size_t SkVarAlloc::approxBytesAllocated() const { |
82 size_t sum = 0; | 69 size_t sum = 0; |
83 for (Block* b = fBlock; b; b = b->prev) { | 70 for (Block* b = fBlock; b; b = b->prev) { |
84 sum += heap_size(b); | 71 sum += heap_size(b); |
85 } | 72 } |
86 return sum; | 73 return sum; |
87 } | 74 } |
OLD | NEW |