OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkDiscardableMemory.h" | 8 #include "SkDiscardableMemoryPool.h" |
9 #include "SkTypes.h" | 9 #include "SkTypes.h" |
10 | 10 |
11 namespace { | 11 SkDiscardableMemory* SkDiscardableMemory::Create(size_t bytes) { |
12 //////////////////////////////////////////////////////////////////////////////// | 12 return SkGetGlobalDiscardableMemoryPool()->create(bytes); |
13 /** | |
14 * Always successful, never purges. Useful for testing. | |
15 */ | |
16 class SkMockDiscardableMemory : public SkDiscardableMemory { | |
17 public: | |
18 SkMockDiscardableMemory(void*); | |
19 virtual ~SkMockDiscardableMemory(); | |
20 virtual bool lock() SK_OVERRIDE; | |
21 virtual void* data() SK_OVERRIDE; | |
22 virtual void unlock() SK_OVERRIDE; | |
23 private: | |
24 bool fLocked; | |
25 void* fPointer; | |
26 }; | |
27 | |
28 //////////////////////////////////////////////////////////////////////////////// | |
29 | |
30 SkMockDiscardableMemory::SkMockDiscardableMemory(void* ptr) | |
31 : fLocked(true) | |
32 , fPointer(ptr) { // Takes ownership of ptr. | |
33 SkASSERT(fPointer != NULL); | |
34 } | 13 } |
35 | |
36 SkMockDiscardableMemory::~SkMockDiscardableMemory() { | |
37 SkASSERT(!fLocked); | |
38 sk_free(fPointer); | |
39 } | |
40 | |
41 bool SkMockDiscardableMemory::lock() { | |
42 SkASSERT(!fLocked); | |
43 return fLocked = true; | |
44 } | |
45 | |
46 void* SkMockDiscardableMemory::data() { | |
47 SkASSERT(fLocked); | |
48 return fLocked ? fPointer : NULL; | |
49 } | |
50 | |
51 void SkMockDiscardableMemory::unlock() { | |
52 SkASSERT(fLocked); | |
53 fLocked = false; | |
54 } | |
55 //////////////////////////////////////////////////////////////////////////////// | |
56 } // namespace | |
57 | |
58 SkDiscardableMemory* SkDiscardableMemory::Create(size_t bytes) { | |
59 void* ptr = sk_malloc_throw(bytes); | |
60 return (ptr != NULL) ? SkNEW_ARGS(SkMockDiscardableMemory, (ptr)) : NULL; | |
61 } | |
OLD | NEW |