/src/gdal/gcore/gdal_rasterblock.h
Line | Count | Source |
1 | | /****************************************************************************** |
2 | | * |
3 | | * Name: gdal_rasterblock.h |
4 | | * Project: GDAL Core |
5 | | * Purpose: Declaration of GDALRasterBlock class |
6 | | * Author: Frank Warmerdam, warmerdam@pobox.com |
7 | | * |
8 | | ****************************************************************************** |
9 | | * Copyright (c) 1998, Frank Warmerdam |
10 | | * Copyright (c) 2007-2014, Even Rouault <even dot rouault at spatialys.com> |
11 | | * |
12 | | * SPDX-License-Identifier: MIT |
13 | | ****************************************************************************/ |
14 | | |
15 | | #ifndef GDALRASTERBLOCK_H_INCLUDED |
16 | | #define GDALRASTERBLOCK_H_INCLUDED |
17 | | |
18 | | #include "cpl_port.h" |
19 | | #include "cpl_atomic_ops.h" |
20 | | #include "gdal.h" |
21 | | |
22 | | /* ******************************************************************** */ |
23 | | /* GDALRasterBlock */ |
24 | | /* ******************************************************************** */ |
25 | | |
26 | | class GDALRasterBand; |
27 | | |
28 | | /** A single raster block in the block cache. |
29 | | * |
30 | | * And the global block manager that manages a least-recently-used list of |
31 | | * blocks from various datasets/bands */ |
32 | | class CPL_DLL GDALRasterBlock final |
33 | | { |
34 | | friend class GDALAbstractBandBlockCache; |
35 | | |
36 | | GDALDataType eType = GDT_Unknown; |
37 | | |
38 | | bool bDirty = false; |
39 | | volatile int nLockCount = 0; |
40 | | |
41 | | int nXOff = 0; |
42 | | int nYOff = 0; |
43 | | |
44 | | int nXSize = 0; |
45 | | int nYSize = 0; |
46 | | |
47 | | void *pData = nullptr; |
48 | | |
49 | | GDALRasterBand *poBand = nullptr; |
50 | | |
51 | | GDALRasterBlock *poNext = nullptr; |
52 | | GDALRasterBlock *poPrevious = nullptr; |
53 | | |
54 | | bool bMustDetach = false; |
55 | | |
56 | | CPL_INTERNAL void Detach_unlocked(void); |
57 | | CPL_INTERNAL void Touch_unlocked(void); |
58 | | |
59 | | CPL_INTERNAL void RecycleFor(int nXOffIn, int nYOffIn); |
60 | | |
61 | | public: |
62 | | GDALRasterBlock(GDALRasterBand *, int, int); |
63 | | GDALRasterBlock(int nXOffIn, int nYOffIn); /* only for lookup purpose */ |
64 | | ~GDALRasterBlock(); |
65 | | |
66 | | CPLErr Internalize(void); |
67 | | void Touch(void); |
68 | | void MarkDirty(void); |
69 | | void MarkClean(void); |
70 | | |
71 | | /** Increment the lock count */ |
72 | | int AddLock(void) |
73 | 0 | { |
74 | 0 | return CPLAtomicInc(&nLockCount); |
75 | 0 | } |
76 | | |
77 | | /** Decrement the lock count */ |
78 | | int DropLock(void) |
79 | 0 | { |
80 | 0 | return CPLAtomicDec(&nLockCount); |
81 | 0 | } |
82 | | |
83 | | void Detach(); |
84 | | |
85 | | CPLErr Write(); |
86 | | |
87 | | /** Return the data type |
88 | | * @return data type |
89 | | */ |
90 | | GDALDataType GetDataType() const |
91 | 0 | { |
92 | 0 | return eType; |
93 | 0 | } |
94 | | |
95 | | /** Return the x offset of the top-left corner of the block |
96 | | * @return x offset |
97 | | */ |
98 | | int GetXOff() const |
99 | 0 | { |
100 | 0 | return nXOff; |
101 | 0 | } |
102 | | |
103 | | /** Return the y offset of the top-left corner of the block |
104 | | * @return y offset |
105 | | */ |
106 | | int GetYOff() const |
107 | 0 | { |
108 | 0 | return nYOff; |
109 | 0 | } |
110 | | |
111 | | /** Return the width of the block |
112 | | * @return width |
113 | | */ |
114 | | int GetXSize() const |
115 | 0 | { |
116 | 0 | return nXSize; |
117 | 0 | } |
118 | | |
119 | | /** Return the height of the block |
120 | | * @return height |
121 | | */ |
122 | | int GetYSize() const |
123 | 0 | { |
124 | 0 | return nYSize; |
125 | 0 | } |
126 | | |
127 | | /** Return the dirty flag |
128 | | * @return dirty flag |
129 | | */ |
130 | | int GetDirty() const |
131 | 0 | { |
132 | 0 | return bDirty; |
133 | 0 | } |
134 | | |
135 | | /** Return the data buffer |
136 | | * @return data buffer |
137 | | */ |
138 | | void *GetDataRef(void) |
139 | 0 | { |
140 | 0 | return pData; |
141 | 0 | } |
142 | | |
143 | | /** Return the block size in bytes |
144 | | * @return block size. |
145 | | */ |
146 | | GPtrDiff_t GetBlockSize() const |
147 | 0 | { |
148 | 0 | return static_cast<GPtrDiff_t>(nXSize) * nYSize * |
149 | 0 | GDALGetDataTypeSizeBytes(eType); |
150 | 0 | } |
151 | | |
152 | | int TakeLock(); |
153 | | int DropLockForRemovalFromStorage(); |
154 | | |
155 | | /// @brief Accessor to source GDALRasterBand object. |
156 | | /// @return source raster band of the raster block. |
157 | | GDALRasterBand *GetBand() |
158 | 0 | { |
159 | 0 | return poBand; |
160 | 0 | } |
161 | | |
162 | | static void FlushDirtyBlocks(); |
163 | | static int FlushCacheBlock(int bDirtyBlocksOnly = FALSE); |
164 | | static void Verify(); |
165 | | |
166 | | static void EnterDisableDirtyBlockFlush(); |
167 | | static void LeaveDisableDirtyBlockFlush(); |
168 | | |
169 | | #ifdef notdef |
170 | | static void CheckNonOrphanedBlocks(GDALRasterBand *poBand); |
171 | | void DumpBlock(); |
172 | | static void DumpAll(); |
173 | | #endif |
174 | | |
175 | | /* Should only be called by GDALDestroyDriverManager() */ |
176 | | //! @cond Doxygen_Suppress |
177 | | CPL_INTERNAL static void DestroyRBMutex(); |
178 | | //! @endcond |
179 | | |
180 | | private: |
181 | | CPL_DISALLOW_COPY_ASSIGN(GDALRasterBlock) |
182 | | }; |
183 | | |
184 | | #endif |