/src/skia/third_party/externals/dng_sdk/source/dng_ref_counted_block.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /*****************************************************************************/ |
2 | | // Copyright 2006 Adobe Systems Incorporated |
3 | | // All Rights Reserved. |
4 | | // |
5 | | // NOTICE: Adobe permits you to use, modify, and distribute this file in |
6 | | // accordance with the terms of the Adobe license agreement accompanying it. |
7 | | /*****************************************************************************/ |
8 | | |
9 | | /* $Id: //mondo/dng_sdk_1_4/dng_sdk/source/dng_ref_counted_block.cpp#1 $ */ |
10 | | /* $DateTime: 2012/05/30 13:28:51 $ */ |
11 | | /* $Change: 832332 $ */ |
12 | | /* $Author: tknoll $ */ |
13 | | |
14 | | /*****************************************************************************/ |
15 | | |
16 | | #include <new> |
17 | | |
18 | | #include "dng_ref_counted_block.h" |
19 | | |
20 | | #include "dng_exceptions.h" |
21 | | |
22 | | /*****************************************************************************/ |
23 | | |
24 | | dng_ref_counted_block::dng_ref_counted_block () |
25 | | |
26 | | : fBuffer (NULL) |
27 | | |
28 | 0 | { |
29 | | |
30 | 0 | } |
31 | | |
32 | | /*****************************************************************************/ |
33 | | |
34 | | dng_ref_counted_block::dng_ref_counted_block (uint32 size) |
35 | | |
36 | | : fBuffer (NULL) |
37 | | |
38 | 0 | { |
39 | | |
40 | 0 | Allocate (size); |
41 | | |
42 | 0 | } |
43 | | |
44 | | /*****************************************************************************/ |
45 | | |
46 | | dng_ref_counted_block::~dng_ref_counted_block () |
47 | 0 | { |
48 | | |
49 | 0 | Clear (); |
50 | | |
51 | 0 | } |
52 | | |
53 | | /*****************************************************************************/ |
54 | | |
55 | | void dng_ref_counted_block::Allocate (uint32 size) |
56 | 0 | { |
57 | | |
58 | 0 | Clear (); |
59 | | |
60 | 0 | if (size) |
61 | 0 | { |
62 | | |
63 | 0 | fBuffer = malloc (size + sizeof (header)); |
64 | | |
65 | 0 | if (!fBuffer) |
66 | 0 | { |
67 | | |
68 | 0 | ThrowMemoryFull (); |
69 | | |
70 | 0 | } |
71 | | |
72 | 0 | new (fBuffer) header (size); |
73 | |
|
74 | 0 | } |
75 | | |
76 | 0 | } |
77 | | |
78 | | /*****************************************************************************/ |
79 | | |
80 | | void dng_ref_counted_block::Clear () |
81 | 0 | { |
82 | | |
83 | 0 | if (fBuffer) |
84 | 0 | { |
85 | | |
86 | |
|
87 | 0 | bool doFree = false; |
88 | |
|
89 | 0 | header *blockHeader = (struct header *)fBuffer; |
90 | |
|
91 | 0 | { |
92 | | |
93 | 0 | dng_lock_mutex lock (&blockHeader->fMutex); |
94 | |
|
95 | 0 | if (--blockHeader->fRefCount == 0) |
96 | 0 | doFree = true; |
97 | 0 | } |
98 | |
|
99 | 0 | if (doFree) |
100 | 0 | { |
101 | | |
102 | 0 | blockHeader->~header (); |
103 | |
|
104 | 0 | free (fBuffer); |
105 | |
|
106 | 0 | } |
107 | | |
108 | 0 | fBuffer = NULL; |
109 | | |
110 | 0 | } |
111 | | |
112 | 0 | } |
113 | | |
114 | | /*****************************************************************************/ |
115 | | |
116 | | dng_ref_counted_block::dng_ref_counted_block (const dng_ref_counted_block &data) |
117 | | : fBuffer (NULL) |
118 | 0 | { |
119 | |
|
120 | 0 | header *blockHeader = (struct header *)data.fBuffer; |
121 | |
|
122 | 0 | dng_lock_mutex lock (&blockHeader->fMutex); |
123 | |
|
124 | 0 | blockHeader->fRefCount++; |
125 | |
|
126 | 0 | fBuffer = blockHeader; |
127 | |
|
128 | 0 | } |
129 | | |
130 | | /*****************************************************************************/ |
131 | | |
132 | | dng_ref_counted_block & dng_ref_counted_block::operator= (const dng_ref_counted_block &data) |
133 | 0 | { |
134 | |
|
135 | 0 | if (this != &data) |
136 | 0 | { |
137 | 0 | Clear (); |
138 | |
|
139 | 0 | header *blockHeader = (struct header *)data.fBuffer; |
140 | |
|
141 | 0 | dng_lock_mutex lock (&blockHeader->fMutex); |
142 | |
|
143 | 0 | blockHeader->fRefCount++; |
144 | |
|
145 | 0 | fBuffer = blockHeader; |
146 | |
|
147 | 0 | } |
148 | |
|
149 | 0 | return *this; |
150 | |
|
151 | 0 | } |
152 | | |
153 | | /*****************************************************************************/ |
154 | | |
155 | | void dng_ref_counted_block::EnsureWriteable () |
156 | 0 | { |
157 | |
|
158 | 0 | if (fBuffer) |
159 | 0 | { |
160 | |
|
161 | 0 | header *possiblySharedHeader = (header *)fBuffer; |
162 | |
|
163 | 0 | { |
164 | | |
165 | 0 | dng_lock_mutex lock (&possiblySharedHeader->fMutex); |
166 | |
|
167 | 0 | if (possiblySharedHeader->fRefCount > 1) |
168 | 0 | { |
169 | |
|
170 | 0 | fBuffer = NULL; |
171 | |
|
172 | 0 | Allocate ((uint32)possiblySharedHeader->fSize); |
173 | |
|
174 | 0 | memcpy (Buffer (), |
175 | 0 | ((char *)possiblySharedHeader) + sizeof (struct header), // could just do + 1 w/o cast, but this makes the type mixing more explicit |
176 | 0 | possiblySharedHeader->fSize); |
177 | |
|
178 | 0 | possiblySharedHeader->fRefCount--; |
179 | |
|
180 | 0 | } |
181 | |
|
182 | 0 | } |
183 | |
|
184 | 0 | } |
185 | 0 | } |
186 | | |
187 | | /*****************************************************************************/ |