Coverage Report

Created: 2025-12-31 06:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gdal/gcore/gdalproxypool.cpp
Line
Count
Source
1
/******************************************************************************
2
 *
3
 * Project:  GDAL Core
4
 * Purpose:  A dataset and raster band classes that differ the opening of the
5
 *           underlying dataset in a limited pool of opened datasets.
6
 * Author:   Even Rouault <even dot rouault at spatialys.com>
7
 *
8
 ******************************************************************************
9
 * Copyright (c) 2008-2013, Even Rouault <even dot rouault at spatialys.com>
10
 *
11
 * SPDX-License-Identifier: MIT
12
 ****************************************************************************/
13
14
#include "cpl_port.h"
15
#include "gdal_proxy.h"
16
17
#include <algorithm>
18
#include <cstdio>
19
#include <cstdlib>
20
#include <cstring>
21
22
#include "cpl_conv.h"
23
#include "cpl_error.h"
24
#include "cpl_hash_set.h"
25
#include "cpl_multiproc.h"
26
#include "cpl_string.h"
27
#include "gdal.h"
28
#include "gdal_priv.h"
29
30
//! @cond Doxygen_Suppress
31
32
/* We *must* share the same mutex as the gdaldataset.cpp file, as we are */
33
/* doing GDALOpen() calls that can indirectly call GDALOpenShared() on */
34
/* an auxiliary dataset ... */
35
/* Then we could get dead-locks in multi-threaded use case */
36
37
/* ******************************************************************** */
38
/*                         GDALDatasetPool                              */
39
/* ******************************************************************** */
40
41
/* This class is a singleton that maintains a pool of opened datasets */
42
/* The cache uses a LRU strategy */
43
44
class GDALDatasetPool;
45
static GDALDatasetPool *singleton = nullptr;
46
47
void GDALNullifyProxyPoolSingleton()
48
0
{
49
0
    singleton = nullptr;
50
0
}
51
52
struct _GDALProxyPoolCacheEntry
53
{
54
    GIntBig responsiblePID;
55
    char *pszFileNameAndOpenOptions;
56
    char *pszOwner;
57
    GDALDataset *poDS;
58
    GIntBig nRAMUsage;
59
60
    /* Ref count of the cached dataset */
61
    int refCount;
62
63
    GDALProxyPoolCacheEntry *prev;
64
    GDALProxyPoolCacheEntry *next;
65
};
66
67
// This variable prevents a dataset that is going to be opened in
68
// GDALDatasetPool::_RefDataset from increasing refCount if, during its
69
// opening, it creates a GDALProxyPoolDataset.
70
// We increment it before opening or closing a cached dataset and decrement
71
// it afterwards
72
// The typical use case is a VRT made of simple sources that are VRT
73
// We don't want the "inner" VRT to take a reference on the pool, otherwise
74
// there is a high chance that this reference will not be dropped and the pool
75
// remain ghost.
76
static thread_local int refCountOfDisabledRefCount = 0;
77
78
class GDALDatasetPool
79
{
80
  private:
81
    bool bInDestruction = false;
82
83
    /* Ref count of the pool singleton */
84
    /* Taken by "toplevel" GDALProxyPoolDataset in its constructor and released
85
     */
86
    /* in its destructor. See also refCountOfDisabledRefCount for the difference
87
     */
88
    /* between toplevel and inner GDALProxyPoolDataset */
89
    int refCount = 0;
90
91
    int maxSize = 0;
92
    int currentSize = 0;
93
    int64_t nMaxRAMUsage = 0;
94
    int64_t nRAMUsage = 0;
95
    GDALProxyPoolCacheEntry *firstEntry = nullptr;
96
    GDALProxyPoolCacheEntry *lastEntry = nullptr;
97
98
    /* Caution : to be sure that we don't run out of entries, size must be at */
99
    /* least greater or equal than the maximum number of threads */
100
    explicit GDALDatasetPool(int maxSize, int64_t nMaxRAMUsage);
101
    ~GDALDatasetPool();
102
    GDALProxyPoolCacheEntry *_RefDataset(const char *pszFileName,
103
                                         GDALAccess eAccess,
104
                                         CSLConstList papszOpenOptions,
105
                                         int bShared, bool bForceOpen,
106
                                         const char *pszOwner);
107
    void _CloseDatasetIfZeroRefCount(const char *pszFileName,
108
                                     CSLConstList papszOpenOptions,
109
                                     GDALAccess eAccess, const char *pszOwner);
110
111
#ifdef DEBUG_PROXY_POOL
112
    // cppcheck-suppress unusedPrivateFunction
113
    void ShowContent();
114
    void CheckLinks();
115
#endif
116
117
    CPL_DISALLOW_COPY_ASSIGN(GDALDatasetPool)
118
119
  public:
120
    static void Ref();
121
    static void Unref();
122
    static GDALProxyPoolCacheEntry *RefDataset(const char *pszFileName,
123
                                               GDALAccess eAccess,
124
                                               char **papszOpenOptions,
125
                                               int bShared, bool bForceOpen,
126
                                               const char *pszOwner);
127
    static void UnrefDataset(GDALProxyPoolCacheEntry *cacheEntry);
128
    static void CloseDatasetIfZeroRefCount(const char *pszFileName,
129
                                           CSLConstList papszOpenOptions,
130
                                           GDALAccess eAccess,
131
                                           const char *pszOwner);
132
133
    static void PreventDestroy();
134
    static void ForceDestroy();
135
};
136
137
/************************************************************************/
138
/*                         GDALDatasetPool()                            */
139
/************************************************************************/
140
141
GDALDatasetPool::GDALDatasetPool(int maxSizeIn, int64_t nMaxRAMUsageIn)
142
0
    : maxSize(maxSizeIn), nMaxRAMUsage(nMaxRAMUsageIn)
143
0
{
144
0
}
145
146
/************************************************************************/
147
/*                        ~GDALDatasetPool()                            */
148
/************************************************************************/
149
150
GDALDatasetPool::~GDALDatasetPool()
151
0
{
152
0
    bInDestruction = true;
153
0
    GDALProxyPoolCacheEntry *cur = firstEntry;
154
0
    GIntBig responsiblePID = GDALGetResponsiblePIDForCurrentThread();
155
0
    while (cur)
156
0
    {
157
0
        GDALProxyPoolCacheEntry *next = cur->next;
158
0
        CPLFree(cur->pszFileNameAndOpenOptions);
159
0
        CPLFree(cur->pszOwner);
160
0
        CPLAssert(cur->refCount == 0);
161
0
        if (cur->poDS)
162
0
        {
163
0
            GDALSetResponsiblePIDForCurrentThread(cur->responsiblePID);
164
0
            GDALClose(cur->poDS);
165
0
        }
166
0
        CPLFree(cur);
167
0
        cur = next;
168
0
    }
169
0
    GDALSetResponsiblePIDForCurrentThread(responsiblePID);
170
0
}
171
172
#ifdef DEBUG_PROXY_POOL
173
/************************************************************************/
174
/*                            ShowContent()                             */
175
/************************************************************************/
176
177
void GDALDatasetPool::ShowContent()
178
{
179
    GDALProxyPoolCacheEntry *cur = firstEntry;
180
    int i = 0;
181
    while (cur)
182
    {
183
        printf("[%d] pszFileName=%s, owner=%s, refCount=%d, " /*ok*/
184
               "responsiblePID=%d\n",
185
               i,
186
               cur->pszFileNameAndOpenOptions ? cur->pszFileNameAndOpenOptions
187
                                              : "(null)",
188
               cur->pszOwner ? cur->pszOwner : "(null)", cur->refCount,
189
               (int)cur->responsiblePID);
190
        i++;
191
        cur = cur->next;
192
    }
193
}
194
195
/************************************************************************/
196
/*                             CheckLinks()                             */
197
/************************************************************************/
198
199
void GDALDatasetPool::CheckLinks()
200
{
201
    GDALProxyPoolCacheEntry *cur = firstEntry;
202
    int i = 0;
203
    while (cur)
204
    {
205
        CPLAssert(cur == firstEntry || cur->prev->next == cur);
206
        CPLAssert(cur == lastEntry || cur->next->prev == cur);
207
        ++i;
208
        CPLAssert(cur->next != nullptr || cur == lastEntry);
209
        cur = cur->next;
210
    }
211
    (void)i;
212
    CPLAssert(i == currentSize);
213
}
214
#endif
215
216
/************************************************************************/
217
/*                       GetFilenameAndOpenOptions()                    */
218
/************************************************************************/
219
220
static std::string GetFilenameAndOpenOptions(const char *pszFileName,
221
                                             CSLConstList papszOpenOptions)
222
0
{
223
0
    std::string osFilenameAndOO(pszFileName);
224
0
    for (int i = 0; papszOpenOptions && papszOpenOptions[i]; ++i)
225
0
    {
226
0
        osFilenameAndOO += "||";
227
0
        osFilenameAndOO += papszOpenOptions[i];
228
0
    }
229
0
    return osFilenameAndOO;
230
0
}
231
232
/************************************************************************/
233
/*                            _RefDataset()                             */
234
/************************************************************************/
235
236
GDALProxyPoolCacheEntry *
237
GDALDatasetPool::_RefDataset(const char *pszFileName, GDALAccess eAccess,
238
                             CSLConstList papszOpenOptions, int bShared,
239
                             bool bForceOpen, const char *pszOwner)
240
0
{
241
0
    CPLMutex **pMutex = GDALGetphDLMutex();
242
0
    CPLMutexHolderD(pMutex);
243
244
0
    if (bInDestruction)
245
0
        return nullptr;
246
247
0
    const GIntBig responsiblePID = GDALGetResponsiblePIDForCurrentThread();
248
249
0
    const auto EvictEntryWithZeroRefCount =
250
0
        [this, responsiblePID](bool evictEntryWithOpenedDataset)
251
0
    {
252
0
        GDALProxyPoolCacheEntry *cur = firstEntry;
253
0
        GDALProxyPoolCacheEntry *candidate = nullptr;
254
0
        while (cur)
255
0
        {
256
0
            GDALProxyPoolCacheEntry *next = cur->next;
257
258
0
            if (cur->refCount == 0 &&
259
0
                (!evictEntryWithOpenedDataset || cur->nRAMUsage > 0))
260
0
            {
261
0
                candidate = cur;
262
0
            }
263
264
0
            cur = next;
265
0
        }
266
0
        if (candidate == nullptr)
267
0
            return false;
268
269
0
        nRAMUsage -= candidate->nRAMUsage;
270
0
        candidate->nRAMUsage = 0;
271
272
0
        CPLFree(candidate->pszFileNameAndOpenOptions);
273
0
        candidate->pszFileNameAndOpenOptions = nullptr;
274
275
0
        if (candidate->poDS)
276
0
        {
277
            /* Close by pretending we are the thread that GDALOpen'ed this */
278
            /* dataset */
279
0
            GDALSetResponsiblePIDForCurrentThread(candidate->responsiblePID);
280
281
0
            refCountOfDisabledRefCount++;
282
0
            GDALClose(candidate->poDS);
283
0
            refCountOfDisabledRefCount--;
284
285
0
            candidate->poDS = nullptr;
286
0
            GDALSetResponsiblePIDForCurrentThread(responsiblePID);
287
0
        }
288
0
        CPLFree(candidate->pszOwner);
289
0
        candidate->pszOwner = nullptr;
290
291
0
        if (!evictEntryWithOpenedDataset && candidate != firstEntry)
292
0
        {
293
            /* Recycle this entry for the to-be-opened dataset and */
294
            /* moves it to the top of the list */
295
0
            if (candidate->prev)
296
0
                candidate->prev->next = candidate->next;
297
298
0
            if (candidate->next)
299
0
                candidate->next->prev = candidate->prev;
300
0
            else
301
0
            {
302
0
                CPLAssert(candidate == lastEntry);
303
0
                lastEntry->prev->next = nullptr;
304
0
                lastEntry = lastEntry->prev;
305
0
            }
306
0
            candidate->prev = nullptr;
307
0
            candidate->next = firstEntry;
308
0
            firstEntry->prev = candidate;
309
0
            firstEntry = candidate;
310
311
#ifdef DEBUG_PROXY_POOL
312
            CheckLinks();
313
#endif
314
0
        }
315
316
0
        return true;
317
0
    };
318
319
0
    GDALProxyPoolCacheEntry *cur = firstEntry;
320
321
0
    const std::string osFilenameAndOO =
322
0
        GetFilenameAndOpenOptions(pszFileName, papszOpenOptions);
323
324
0
    while (cur)
325
0
    {
326
0
        GDALProxyPoolCacheEntry *next = cur->next;
327
328
0
        if (cur->refCount >= 0 && cur->pszFileNameAndOpenOptions &&
329
0
            osFilenameAndOO == cur->pszFileNameAndOpenOptions &&
330
0
            ((bShared && cur->responsiblePID == responsiblePID &&
331
0
              ((cur->pszOwner == nullptr && pszOwner == nullptr) ||
332
0
               (cur->pszOwner != nullptr && pszOwner != nullptr &&
333
0
                strcmp(cur->pszOwner, pszOwner) == 0))) ||
334
0
             (!bShared && cur->refCount == 0)))
335
0
        {
336
0
            if (cur != firstEntry)
337
0
            {
338
                /* Move to begin */
339
0
                if (cur->next)
340
0
                    cur->next->prev = cur->prev;
341
0
                else
342
0
                    lastEntry = cur->prev;
343
0
                cur->prev->next = cur->next;
344
0
                cur->prev = nullptr;
345
0
                firstEntry->prev = cur;
346
0
                cur->next = firstEntry;
347
0
                firstEntry = cur;
348
349
#ifdef DEBUG_PROXY_POOL
350
                CheckLinks();
351
#endif
352
0
            }
353
354
0
            cur->refCount++;
355
0
            return cur;
356
0
        }
357
358
0
        cur = next;
359
0
    }
360
361
0
    if (!bForceOpen)
362
0
        return nullptr;
363
364
0
    if (currentSize == maxSize)
365
0
    {
366
0
        if (!EvictEntryWithZeroRefCount(false))
367
0
        {
368
0
            CPLError(
369
0
                CE_Failure, CPLE_AppDefined,
370
0
                "Too many threads are running for the current value of the "
371
0
                "dataset pool size (%d).\n"
372
0
                "or too many proxy datasets are opened in a cascaded way.\n"
373
0
                "Try increasing GDAL_MAX_DATASET_POOL_SIZE.",
374
0
                maxSize);
375
0
            return nullptr;
376
0
        }
377
378
0
        CPLAssert(firstEntry);
379
0
        cur = firstEntry;
380
0
    }
381
0
    else
382
0
    {
383
        /* Prepend */
384
0
        cur = static_cast<GDALProxyPoolCacheEntry *>(
385
0
            CPLCalloc(1, sizeof(GDALProxyPoolCacheEntry)));
386
0
        if (lastEntry == nullptr)
387
0
            lastEntry = cur;
388
0
        cur->prev = nullptr;
389
0
        cur->next = firstEntry;
390
0
        if (firstEntry)
391
0
            firstEntry->prev = cur;
392
0
        firstEntry = cur;
393
0
        currentSize++;
394
#ifdef DEBUG_PROXY_POOL
395
        CheckLinks();
396
#endif
397
0
    }
398
399
0
    cur->pszFileNameAndOpenOptions = CPLStrdup(osFilenameAndOO.c_str());
400
0
    cur->pszOwner = (pszOwner) ? CPLStrdup(pszOwner) : nullptr;
401
0
    cur->responsiblePID = responsiblePID;
402
0
    cur->refCount = -1;  // to mark loading of dataset in progress
403
0
    cur->nRAMUsage = 0;
404
405
0
    refCountOfDisabledRefCount++;
406
0
    const int nFlag =
407
0
        ((eAccess == GA_Update) ? GDAL_OF_UPDATE : GDAL_OF_READONLY) |
408
0
        GDAL_OF_RASTER | GDAL_OF_VERBOSE_ERROR;
409
0
    CPLConfigOptionSetter oSetter("CPL_ALLOW_VSISTDIN", "NO", true);
410
411
    // Release mutex while opening dataset to avoid lock contention.
412
0
    CPLReleaseMutex(*pMutex);
413
0
    auto poDS = GDALDataset::Open(pszFileName, nFlag, nullptr, papszOpenOptions,
414
0
                                  nullptr);
415
0
    CPLAcquireMutex(*pMutex, 1000.0);
416
417
0
    cur->poDS = poDS;
418
0
    cur->refCount = 1;
419
420
0
    refCountOfDisabledRefCount--;
421
422
0
    if (cur->poDS)
423
0
    {
424
0
        cur->nRAMUsage =
425
0
            std::max<GIntBig>(0, cur->poDS->GetEstimatedRAMUsage());
426
0
        nRAMUsage += cur->nRAMUsage;
427
0
    }
428
429
0
    if (nMaxRAMUsage > 0 && cur->nRAMUsage > 0)
430
0
    {
431
0
        while (nRAMUsage > nMaxRAMUsage && nRAMUsage != cur->nRAMUsage &&
432
0
               EvictEntryWithZeroRefCount(true))
433
0
        {
434
            // ok
435
0
        }
436
0
    }
437
438
0
    return cur;
439
0
}
440
441
/************************************************************************/
442
/*                   _CloseDatasetIfZeroRefCount()                      */
443
/************************************************************************/
444
445
void GDALDatasetPool::_CloseDatasetIfZeroRefCount(const char *pszFileName,
446
                                                  CSLConstList papszOpenOptions,
447
                                                  GDALAccess /* eAccess */,
448
                                                  const char *pszOwner)
449
0
{
450
    // May fix https://github.com/OSGeo/gdal/issues/4318
451
0
    if (bInDestruction)
452
0
        return;
453
454
0
    GDALProxyPoolCacheEntry *cur = firstEntry;
455
0
    GIntBig responsiblePID = GDALGetResponsiblePIDForCurrentThread();
456
457
0
    const std::string osFilenameAndOO =
458
0
        GetFilenameAndOpenOptions(pszFileName, papszOpenOptions);
459
460
0
    while (cur)
461
0
    {
462
0
        GDALProxyPoolCacheEntry *next = cur->next;
463
464
0
        if (cur->refCount == 0 && cur->pszFileNameAndOpenOptions &&
465
0
            osFilenameAndOO == cur->pszFileNameAndOpenOptions &&
466
0
            ((pszOwner == nullptr && cur->pszOwner == nullptr) ||
467
0
             (pszOwner != nullptr && cur->pszOwner != nullptr &&
468
0
              strcmp(cur->pszOwner, pszOwner) == 0)) &&
469
0
            cur->poDS != nullptr)
470
0
        {
471
            /* Close by pretending we are the thread that GDALOpen'ed this */
472
            /* dataset */
473
0
            GDALSetResponsiblePIDForCurrentThread(cur->responsiblePID);
474
475
0
            GDALDataset *poDS = cur->poDS;
476
477
0
            nRAMUsage -= cur->nRAMUsage;
478
0
            cur->nRAMUsage = 0;
479
480
0
            cur->poDS = nullptr;
481
0
            CPLFree(cur->pszFileNameAndOpenOptions);
482
0
            cur->pszFileNameAndOpenOptions = nullptr;
483
0
            CPLFree(cur->pszOwner);
484
0
            cur->pszOwner = nullptr;
485
486
0
            refCountOfDisabledRefCount++;
487
0
            GDALClose(poDS);
488
0
            refCountOfDisabledRefCount--;
489
490
0
            GDALSetResponsiblePIDForCurrentThread(responsiblePID);
491
0
            break;
492
0
        }
493
494
0
        cur = next;
495
0
    }
496
0
}
497
498
/************************************************************************/
499
/*                       GDALGetMaxDatasetPoolSize()                    */
500
/************************************************************************/
501
502
/** Return the maximum number of datasets simultaneously opened in the
503
 * dataset pool.
504
 */
505
int GDALGetMaxDatasetPoolSize()
506
0
{
507
0
    int nSize = atoi(CPLGetConfigOption("GDAL_MAX_DATASET_POOL_SIZE", "100"));
508
0
    if (nSize < 2)
509
0
        nSize = 2;
510
0
    else if (nSize > 1000)
511
0
        nSize = 1000;
512
0
    return nSize;
513
0
}
514
515
/************************************************************************/
516
/*                                 Ref()                                */
517
/************************************************************************/
518
519
void GDALDatasetPool::Ref()
520
0
{
521
0
    CPLMutexHolderD(GDALGetphDLMutex());
522
0
    if (singleton == nullptr)
523
0
    {
524
525
        // Try to not consume more than 25% of the usable RAM
526
0
        GIntBig l_nMaxRAMUsage =
527
0
            (CPLGetUsablePhysicalRAM() - GDALGetCacheMax64()) / 4;
528
0
        const char *pszMaxRAMUsage =
529
0
            CPLGetConfigOption("GDAL_MAX_DATASET_POOL_RAM_USAGE", nullptr);
530
0
        if (pszMaxRAMUsage)
531
0
        {
532
0
            l_nMaxRAMUsage = std::strtoll(pszMaxRAMUsage, nullptr, 10);
533
0
            if (strstr(pszMaxRAMUsage, "MB"))
534
0
                l_nMaxRAMUsage *= 1024 * 1024;
535
0
            else if (strstr(pszMaxRAMUsage, "GB"))
536
0
                l_nMaxRAMUsage *= 1024 * 1024 * 1024;
537
0
        }
538
539
0
        singleton =
540
0
            new GDALDatasetPool(GDALGetMaxDatasetPoolSize(), l_nMaxRAMUsage);
541
0
    }
542
0
    if (refCountOfDisabledRefCount == 0)
543
0
        singleton->refCount++;
544
0
}
545
546
/* keep that in sync with gdaldrivermanager.cpp */
547
void GDALDatasetPool::PreventDestroy()
548
0
{
549
0
    CPLMutexHolderD(GDALGetphDLMutex());
550
0
    if (!singleton)
551
0
        return;
552
0
    refCountOfDisabledRefCount++;
553
0
}
554
555
/* keep that in sync with gdaldrivermanager.cpp */
556
extern void GDALDatasetPoolPreventDestroy();
557
558
void GDALDatasetPoolPreventDestroy()
559
0
{
560
0
    GDALDatasetPool::PreventDestroy();
561
0
}
562
563
/************************************************************************/
564
/*                               Unref()                                */
565
/************************************************************************/
566
567
void GDALDatasetPool::Unref()
568
0
{
569
0
    CPLMutexHolderD(GDALGetphDLMutex());
570
0
    if (!singleton)
571
0
    {
572
0
        CPLAssert(false);
573
0
        return;
574
0
    }
575
0
    if (refCountOfDisabledRefCount == 0)
576
0
    {
577
0
        singleton->refCount--;
578
0
        if (singleton->refCount == 0)
579
0
        {
580
0
            delete singleton;
581
0
            singleton = nullptr;
582
0
        }
583
0
    }
584
0
}
585
586
/* keep that in sync with gdaldrivermanager.cpp */
587
void GDALDatasetPool::ForceDestroy()
588
0
{
589
0
    CPLMutexHolderD(GDALGetphDLMutex());
590
0
    if (!singleton)
591
0
        return;
592
0
    refCountOfDisabledRefCount--;
593
0
    CPLAssert(refCountOfDisabledRefCount == 0);
594
0
    singleton->refCount = 0;
595
0
    delete singleton;
596
0
    singleton = nullptr;
597
0
}
598
599
/* keep that in sync with gdaldrivermanager.cpp */
600
extern void GDALDatasetPoolForceDestroy();
601
602
void GDALDatasetPoolForceDestroy()
603
0
{
604
0
    GDALDatasetPool::ForceDestroy();
605
0
}
606
607
/************************************************************************/
608
/*                           RefDataset()                               */
609
/************************************************************************/
610
611
GDALProxyPoolCacheEntry *
612
GDALDatasetPool::RefDataset(const char *pszFileName, GDALAccess eAccess,
613
                            char **papszOpenOptions, int bShared,
614
                            bool bForceOpen, const char *pszOwner)
615
0
{
616
0
    return singleton->_RefDataset(pszFileName, eAccess, papszOpenOptions,
617
0
                                  bShared, bForceOpen, pszOwner);
618
0
}
619
620
/************************************************************************/
621
/*                       UnrefDataset()                                 */
622
/************************************************************************/
623
624
void GDALDatasetPool::UnrefDataset(GDALProxyPoolCacheEntry *cacheEntry)
625
0
{
626
0
    CPLMutexHolderD(GDALGetphDLMutex());
627
0
    cacheEntry->refCount--;
628
0
}
629
630
/************************************************************************/
631
/*                   CloseDatasetIfZeroRefCount()                       */
632
/************************************************************************/
633
634
void GDALDatasetPool::CloseDatasetIfZeroRefCount(const char *pszFileName,
635
                                                 CSLConstList papszOpenOptions,
636
                                                 GDALAccess eAccess,
637
                                                 const char *pszOwner)
638
0
{
639
0
    CPLMutexHolderD(GDALGetphDLMutex());
640
0
    singleton->_CloseDatasetIfZeroRefCount(pszFileName, papszOpenOptions,
641
0
                                           eAccess, pszOwner);
642
0
}
643
644
struct GetMetadataElt
645
{
646
    char *pszDomain;
647
    char **papszMetadata;
648
};
649
650
static unsigned long hash_func_get_metadata(const void *_elt)
651
0
{
652
0
    const GetMetadataElt *elt = static_cast<const GetMetadataElt *>(_elt);
653
0
    return CPLHashSetHashStr(elt->pszDomain);
654
0
}
655
656
static int equal_func_get_metadata(const void *_elt1, const void *_elt2)
657
0
{
658
0
    const GetMetadataElt *elt1 = static_cast<const GetMetadataElt *>(_elt1);
659
0
    const GetMetadataElt *elt2 = static_cast<const GetMetadataElt *>(_elt2);
660
0
    return CPLHashSetEqualStr(elt1->pszDomain, elt2->pszDomain);
661
0
}
662
663
static void free_func_get_metadata(void *_elt)
664
0
{
665
0
    GetMetadataElt *elt = static_cast<GetMetadataElt *>(_elt);
666
0
    CPLFree(elt->pszDomain);
667
0
    CSLDestroy(elt->papszMetadata);
668
0
    CPLFree(elt);
669
0
}
670
671
struct GetMetadataItemElt
672
{
673
    char *pszName;
674
    char *pszDomain;
675
    char *pszMetadataItem;
676
};
677
678
static unsigned long hash_func_get_metadata_item(const void *_elt)
679
0
{
680
0
    const GetMetadataItemElt *elt =
681
0
        static_cast<const GetMetadataItemElt *>(_elt);
682
0
    return CPLHashSetHashStr(elt->pszName) ^ CPLHashSetHashStr(elt->pszDomain);
683
0
}
684
685
static int equal_func_get_metadata_item(const void *_elt1, const void *_elt2)
686
0
{
687
0
    const GetMetadataItemElt *elt1 =
688
0
        static_cast<const GetMetadataItemElt *>(_elt1);
689
0
    const GetMetadataItemElt *elt2 =
690
0
        static_cast<const GetMetadataItemElt *>(_elt2);
691
0
    return CPLHashSetEqualStr(elt1->pszName, elt2->pszName) &&
692
0
           CPLHashSetEqualStr(elt1->pszDomain, elt2->pszDomain);
693
0
}
694
695
static void free_func_get_metadata_item(void *_elt)
696
0
{
697
0
    GetMetadataItemElt *elt = static_cast<GetMetadataItemElt *>(_elt);
698
0
    CPLFree(elt->pszName);
699
0
    CPLFree(elt->pszDomain);
700
0
    CPLFree(elt->pszMetadataItem);
701
0
    CPLFree(elt);
702
0
}
703
704
/* ******************************************************************** */
705
/*                     GDALProxyPoolDataset                             */
706
/* ******************************************************************** */
707
708
/* Note : the bShared parameter must be used with caution. You can */
709
/* set it to TRUE  for being used as a VRT source : in that case, */
710
/* VRTSimpleSource will take care of destroying it when there are no */
711
/* reference to it (in VRTSimpleSource::~VRTSimpleSource()) */
712
/* However this will not be registered as a genuine shared dataset, like it */
713
/* would have been with MarkAsShared(). But MarkAsShared() is not usable for */
714
/* GDALProxyPoolDataset objects, as they share the same description as their */
715
/* underlying dataset. So *NEVER* call MarkAsShared() on a GDALProxyPoolDataset
716
 */
717
/* object */
718
719
/* pszOwner is only honoured in the bShared case, and restrict the scope */
720
/* of the sharing. Only calls to _RefDataset() with the same value of */
721
/* pszOwner can effectively use the same dataset. The use case is */
722
/* to avoid 2 VRTs (potentially the same one) opened by a single thread,
723
 * pointing to */
724
/* the same source datasets. In that case, they would use the same dataset */
725
/* So even if the VRT handles themselves are used from different threads, since
726
 */
727
/* the underlying sources are shared, that might cause crashes (#6939). */
728
/* But we want to allow a same VRT referencing the same source dataset,*/
729
/* for example if it has multiple bands. So in practice the value of pszOwner */
730
/* is the serialized value (%p formatting) of the VRT dataset handle. */
731
732
GDALProxyPoolDataset::GDALProxyPoolDataset(
733
    const char *pszSourceDatasetDescription, int nRasterXSizeIn,
734
    int nRasterYSizeIn, GDALAccess eAccessIn, int bSharedIn,
735
    const char *pszProjectionRefIn, const GDALGeoTransform *pGT,
736
    const char *pszOwner)
737
0
    : responsiblePID(GDALGetResponsiblePIDForCurrentThread()),
738
0
      pszProjectionRef(pszProjectionRefIn ? CPLStrdup(pszProjectionRefIn)
739
0
                                          : nullptr)
740
0
{
741
0
    GDALDatasetPool::Ref();
742
743
0
    SetDescription(pszSourceDatasetDescription);
744
745
0
    nRasterXSize = nRasterXSizeIn;
746
0
    nRasterYSize = nRasterYSizeIn;
747
0
    eAccess = eAccessIn;
748
749
0
    bShared = CPL_TO_BOOL(bSharedIn);
750
0
    m_pszOwner = pszOwner ? CPLStrdup(pszOwner) : nullptr;
751
752
0
    if (pGT)
753
0
    {
754
0
        m_gt = *pGT;
755
0
        m_bHasSrcGeoTransform = true;
756
0
    }
757
758
0
    if (pszProjectionRefIn)
759
0
    {
760
0
        m_poSRS = new OGRSpatialReference();
761
0
        m_poSRS->importFromWkt(pszProjectionRefIn);
762
0
        m_bHasSrcSRS = true;
763
0
    }
764
0
}
765
766
/* Constructor where the parameters (raster size, etc.) are obtained
767
 * by opening the underlying dataset.
768
 */
769
GDALProxyPoolDataset::GDALProxyPoolDataset(
770
    const char *pszSourceDatasetDescription, GDALAccess eAccessIn,
771
    int bSharedIn, const char *pszOwner)
772
0
    : responsiblePID(GDALGetResponsiblePIDForCurrentThread())
773
0
{
774
0
    GDALDatasetPool::Ref();
775
776
0
    SetDescription(pszSourceDatasetDescription);
777
778
0
    eAccess = eAccessIn;
779
780
0
    bShared = CPL_TO_BOOL(bSharedIn);
781
0
    m_pszOwner = pszOwner ? CPLStrdup(pszOwner) : nullptr;
782
0
}
783
784
/************************************************************************/
785
/*                              Create()                                */
786
/************************************************************************/
787
788
/* Instantiate a GDALProxyPoolDataset where the parameters (raster size, etc.)
789
 * are obtained by opening the underlying dataset.
790
 * Its bands are also instantiated.
791
 */
792
GDALProxyPoolDataset *GDALProxyPoolDataset::Create(
793
    const char *pszSourceDatasetDescription, CSLConstList papszOpenOptionsIn,
794
    GDALAccess eAccessIn, int bSharedIn, const char *pszOwner)
795
0
{
796
0
    std::unique_ptr<GDALProxyPoolDataset> poSelf(new GDALProxyPoolDataset(
797
0
        pszSourceDatasetDescription, eAccessIn, bSharedIn, pszOwner));
798
0
    poSelf->SetOpenOptions(papszOpenOptionsIn);
799
0
    GDALDataset *poUnderlyingDS = poSelf->RefUnderlyingDataset();
800
0
    if (!poUnderlyingDS)
801
0
        return nullptr;
802
0
    poSelf->nRasterXSize = poUnderlyingDS->GetRasterXSize();
803
0
    poSelf->nRasterYSize = poUnderlyingDS->GetRasterYSize();
804
0
    if (poUnderlyingDS->GetGeoTransform(poSelf->m_gt) == CE_None)
805
0
        poSelf->m_bHasSrcGeoTransform = true;
806
0
    const auto poSRS = poUnderlyingDS->GetSpatialRef();
807
0
    if (poSRS)
808
0
    {
809
0
        poSelf->m_poSRS = poSRS->Clone();
810
0
        poSelf->m_bHasSrcSRS = true;
811
0
    }
812
0
    for (int i = 1; i <= poUnderlyingDS->GetRasterCount(); ++i)
813
0
    {
814
0
        auto poSrcBand = poUnderlyingDS->GetRasterBand(i);
815
0
        if (!poSrcBand)
816
0
        {
817
0
            poSelf->UnrefUnderlyingDataset(poUnderlyingDS);
818
0
            return nullptr;
819
0
        }
820
0
        int nSrcBlockXSize, nSrcBlockYSize;
821
0
        poSrcBand->GetBlockSize(&nSrcBlockXSize, &nSrcBlockYSize);
822
0
        poSelf->AddSrcBandDescription(poSrcBand->GetRasterDataType(),
823
0
                                      nSrcBlockXSize, nSrcBlockYSize);
824
0
    }
825
0
    poSelf->UnrefUnderlyingDataset(poUnderlyingDS);
826
0
    return poSelf.release();
827
0
}
828
829
/************************************************************************/
830
/*                    ~GDALProxyPoolDataset()                           */
831
/************************************************************************/
832
833
GDALProxyPoolDataset::~GDALProxyPoolDataset()
834
0
{
835
0
    GDALDatasetPool::CloseDatasetIfZeroRefCount(
836
0
        GetDescription(), papszOpenOptions, eAccess, m_pszOwner);
837
838
    /* See comment in constructor */
839
    /* It is not really a genuine shared dataset, so we don't */
840
    /* want ~GDALDataset() to try to release it from its */
841
    /* shared dataset hashset. This will save a */
842
    /* "Should not happen. Cannot find %s, this=%p in phSharedDatasetSet" debug
843
     * message */
844
0
    bShared = false;
845
846
0
    CPLFree(pszProjectionRef);
847
0
    CPLFree(pszGCPProjection);
848
0
    if (nGCPCount)
849
0
    {
850
0
        GDALDeinitGCPs(nGCPCount, pasGCPList);
851
0
        CPLFree(pasGCPList);
852
0
    }
853
0
    if (metadataSet)
854
0
        CPLHashSetDestroy(metadataSet);
855
0
    if (metadataItemSet)
856
0
        CPLHashSetDestroy(metadataItemSet);
857
0
    CPLFree(m_pszOwner);
858
0
    if (m_poSRS)
859
0
        m_poSRS->Release();
860
0
    if (m_poGCPSRS)
861
0
        m_poGCPSRS->Release();
862
863
0
    GDALDatasetPool::Unref();
864
0
}
865
866
/************************************************************************/
867
/*                        SetOpenOptions()                              */
868
/************************************************************************/
869
870
void GDALProxyPoolDataset::SetOpenOptions(CSLConstList papszOpenOptionsIn)
871
0
{
872
0
    CPLAssert(papszOpenOptions == nullptr);
873
0
    papszOpenOptions = CSLDuplicate(papszOpenOptionsIn);
874
0
}
875
876
/************************************************************************/
877
/*                    AddSrcBandDescription()                           */
878
/************************************************************************/
879
880
void GDALProxyPoolDataset::AddSrcBandDescription(GDALDataType eDataType,
881
                                                 int nBlockXSize,
882
                                                 int nBlockYSize)
883
0
{
884
0
    SetBand(nBands + 1, new GDALProxyPoolRasterBand(this, nBands + 1, eDataType,
885
0
                                                    nBlockXSize, nBlockYSize));
886
0
}
887
888
/************************************************************************/
889
/*                    AddSrcBand()                                      */
890
/************************************************************************/
891
892
void GDALProxyPoolDataset::AddSrcBand(int nBand, GDALDataType eDataType,
893
                                      int nBlockXSize, int nBlockYSize)
894
0
{
895
0
    SetBand(nBand, new GDALProxyPoolRasterBand(this, nBand, eDataType,
896
0
                                               nBlockXSize, nBlockYSize));
897
0
}
898
899
/************************************************************************/
900
/*                    RefUnderlyingDataset()                            */
901
/************************************************************************/
902
903
GDALDataset *GDALProxyPoolDataset::RefUnderlyingDataset() const
904
0
{
905
0
    return RefUnderlyingDataset(true);
906
0
}
907
908
GDALDataset *GDALProxyPoolDataset::RefUnderlyingDataset(bool bForceOpen) const
909
0
{
910
    /* We pretend that the current thread is responsiblePID, that is */
911
    /* to say the thread that created that GDALProxyPoolDataset object. */
912
    /* This is for the case when a GDALProxyPoolDataset is created by a */
913
    /* thread and used by other threads. These other threads, when doing actual
914
     */
915
    /* IO, will come there and potentially open the underlying dataset. */
916
    /* By doing this, they can indirectly call GDALOpenShared() on .aux file */
917
    /* for example. So this call to GDALOpenShared() must occur as if it */
918
    /* was done by the creating thread, otherwise it will not be correctly
919
     * closed afterwards... */
920
    /* To make a long story short : this is necessary when warping with
921
     * ChunkAndWarpMulti */
922
    /* a VRT of GeoTIFFs that have associated .aux files */
923
0
    GIntBig curResponsiblePID = GDALGetResponsiblePIDForCurrentThread();
924
0
    GDALSetResponsiblePIDForCurrentThread(responsiblePID);
925
0
    cacheEntry =
926
0
        GDALDatasetPool::RefDataset(GetDescription(), eAccess, papszOpenOptions,
927
0
                                    GetShared(), bForceOpen, m_pszOwner);
928
0
    GDALSetResponsiblePIDForCurrentThread(curResponsiblePID);
929
0
    if (cacheEntry != nullptr)
930
0
    {
931
0
        if (cacheEntry->poDS != nullptr)
932
0
            return cacheEntry->poDS;
933
0
        else
934
0
            GDALDatasetPool::UnrefDataset(cacheEntry);
935
0
    }
936
0
    return nullptr;
937
0
}
938
939
/************************************************************************/
940
/*                    UnrefUnderlyingDataset()                        */
941
/************************************************************************/
942
943
void GDALProxyPoolDataset::UnrefUnderlyingDataset(
944
    CPL_UNUSED GDALDataset *poUnderlyingDataset) const
945
0
{
946
0
    if (cacheEntry != nullptr)
947
0
    {
948
0
        CPLAssert(cacheEntry->poDS == poUnderlyingDataset);
949
0
        if (cacheEntry->poDS != nullptr)
950
0
            GDALDatasetPool::UnrefDataset(cacheEntry);
951
0
    }
952
0
}
953
954
/************************************************************************/
955
/*                         FlushCache()                                 */
956
/************************************************************************/
957
958
CPLErr GDALProxyPoolDataset::FlushCache(bool bAtClosing)
959
0
{
960
0
    CPLErr eErr = CE_None;
961
0
    GDALDataset *poUnderlyingDataset = RefUnderlyingDataset(false);
962
0
    if (poUnderlyingDataset)
963
0
    {
964
0
        eErr = poUnderlyingDataset->FlushCache(bAtClosing);
965
0
        UnrefUnderlyingDataset(poUnderlyingDataset);
966
0
    }
967
0
    return eErr;
968
0
}
969
970
/************************************************************************/
971
/*                        SetSpatialRef()                               */
972
/************************************************************************/
973
974
CPLErr GDALProxyPoolDataset::SetSpatialRef(const OGRSpatialReference *poSRS)
975
0
{
976
0
    m_bHasSrcSRS = false;
977
0
    return GDALProxyDataset::SetSpatialRef(poSRS);
978
0
}
979
980
/************************************************************************/
981
/*                        GetSpatialRef()                               */
982
/************************************************************************/
983
984
const OGRSpatialReference *GDALProxyPoolDataset::GetSpatialRef() const
985
0
{
986
0
    if (m_bHasSrcSRS)
987
0
        return m_poSRS;
988
0
    else
989
0
    {
990
0
        if (m_poSRS)
991
0
            m_poSRS->Release();
992
0
        m_poSRS = nullptr;
993
0
        auto poSRS = GDALProxyDataset::GetSpatialRef();
994
0
        if (poSRS)
995
0
            m_poSRS = poSRS->Clone();
996
0
        return m_poSRS;
997
0
    }
998
0
}
999
1000
/************************************************************************/
1001
/*                        SetGeoTransform()                             */
1002
/************************************************************************/
1003
1004
CPLErr GDALProxyPoolDataset::SetGeoTransform(const GDALGeoTransform &gt)
1005
0
{
1006
0
    m_gt = gt;
1007
0
    m_bHasSrcGeoTransform = false;
1008
0
    return GDALProxyDataset::SetGeoTransform(gt);
1009
0
}
1010
1011
/************************************************************************/
1012
/*                        GetGeoTransform()                             */
1013
/************************************************************************/
1014
1015
CPLErr GDALProxyPoolDataset::GetGeoTransform(GDALGeoTransform &gt) const
1016
0
{
1017
0
    if (m_bHasSrcGeoTransform)
1018
0
    {
1019
0
        gt = m_gt;
1020
0
        return CE_None;
1021
0
    }
1022
0
    else
1023
0
    {
1024
0
        return GDALProxyDataset::GetGeoTransform(gt);
1025
0
    }
1026
0
}
1027
1028
/************************************************************************/
1029
/*                            GetMetadata()                             */
1030
/************************************************************************/
1031
1032
char **GDALProxyPoolDataset::GetMetadata(const char *pszDomain)
1033
0
{
1034
0
    if (metadataSet == nullptr)
1035
0
        metadataSet =
1036
0
            CPLHashSetNew(hash_func_get_metadata, equal_func_get_metadata,
1037
0
                          free_func_get_metadata);
1038
1039
0
    GDALDataset *poUnderlyingDataset = RefUnderlyingDataset();
1040
0
    if (poUnderlyingDataset == nullptr)
1041
0
        return nullptr;
1042
1043
0
    char **papszUnderlyingMetadata =
1044
0
        poUnderlyingDataset->GetMetadata(pszDomain);
1045
1046
0
    GetMetadataElt *pElt =
1047
0
        static_cast<GetMetadataElt *>(CPLMalloc(sizeof(GetMetadataElt)));
1048
0
    pElt->pszDomain = (pszDomain) ? CPLStrdup(pszDomain) : nullptr;
1049
0
    pElt->papszMetadata = CSLDuplicate(papszUnderlyingMetadata);
1050
0
    CPLHashSetInsert(metadataSet, pElt);
1051
1052
0
    UnrefUnderlyingDataset(poUnderlyingDataset);
1053
1054
0
    return pElt->papszMetadata;
1055
0
}
1056
1057
/************************************************************************/
1058
/*                        GetMetadataItem()                             */
1059
/************************************************************************/
1060
1061
const char *GDALProxyPoolDataset::GetMetadataItem(const char *pszName,
1062
                                                  const char *pszDomain)
1063
0
{
1064
0
    if (metadataItemSet == nullptr)
1065
0
        metadataItemSet = CPLHashSetNew(hash_func_get_metadata_item,
1066
0
                                        equal_func_get_metadata_item,
1067
0
                                        free_func_get_metadata_item);
1068
1069
0
    GDALDataset *poUnderlyingDataset = RefUnderlyingDataset();
1070
0
    if (poUnderlyingDataset == nullptr)
1071
0
        return nullptr;
1072
1073
0
    const char *pszUnderlyingMetadataItem =
1074
0
        poUnderlyingDataset->GetMetadataItem(pszName, pszDomain);
1075
1076
0
    GetMetadataItemElt *pElt = static_cast<GetMetadataItemElt *>(
1077
0
        CPLMalloc(sizeof(GetMetadataItemElt)));
1078
0
    pElt->pszName = (pszName) ? CPLStrdup(pszName) : nullptr;
1079
0
    pElt->pszDomain = (pszDomain) ? CPLStrdup(pszDomain) : nullptr;
1080
0
    pElt->pszMetadataItem = (pszUnderlyingMetadataItem)
1081
0
                                ? CPLStrdup(pszUnderlyingMetadataItem)
1082
0
                                : nullptr;
1083
0
    CPLHashSetInsert(metadataItemSet, pElt);
1084
1085
0
    UnrefUnderlyingDataset(poUnderlyingDataset);
1086
1087
0
    return pElt->pszMetadataItem;
1088
0
}
1089
1090
/************************************************************************/
1091
/*                      GetInternalHandle()                             */
1092
/************************************************************************/
1093
1094
void *GDALProxyPoolDataset::GetInternalHandle(const char *pszRequest)
1095
0
{
1096
0
    CPLError(
1097
0
        CE_Warning, CPLE_AppDefined,
1098
0
        "GetInternalHandle() cannot be safely called on a proxy pool dataset\n"
1099
0
        "as the returned value may be invalidated at any time.\n");
1100
0
    return GDALProxyDataset::GetInternalHandle(pszRequest);
1101
0
}
1102
1103
/************************************************************************/
1104
/*                     GetGCPSpatialRef()                               */
1105
/************************************************************************/
1106
1107
const OGRSpatialReference *GDALProxyPoolDataset::GetGCPSpatialRef() const
1108
0
{
1109
0
    GDALDataset *poUnderlyingDataset = RefUnderlyingDataset();
1110
0
    if (poUnderlyingDataset == nullptr)
1111
0
        return nullptr;
1112
1113
0
    if (m_poGCPSRS)
1114
0
    {
1115
0
        m_poGCPSRS->Release();
1116
0
        m_poGCPSRS = nullptr;
1117
0
    }
1118
1119
0
    const auto poUnderlyingGCPSRS = poUnderlyingDataset->GetGCPSpatialRef();
1120
0
    if (poUnderlyingGCPSRS)
1121
0
        m_poGCPSRS = poUnderlyingGCPSRS->Clone();
1122
1123
0
    UnrefUnderlyingDataset(poUnderlyingDataset);
1124
1125
0
    return m_poGCPSRS;
1126
0
}
1127
1128
/************************************************************************/
1129
/*                            GetGCPs()                                 */
1130
/************************************************************************/
1131
1132
const GDAL_GCP *GDALProxyPoolDataset::GetGCPs()
1133
0
{
1134
0
    GDALDataset *poUnderlyingDataset = RefUnderlyingDataset();
1135
0
    if (poUnderlyingDataset == nullptr)
1136
0
        return nullptr;
1137
1138
0
    if (nGCPCount)
1139
0
    {
1140
0
        GDALDeinitGCPs(nGCPCount, pasGCPList);
1141
0
        CPLFree(pasGCPList);
1142
0
        pasGCPList = nullptr;
1143
0
    }
1144
1145
0
    const GDAL_GCP *pasUnderlyingGCPList = poUnderlyingDataset->GetGCPs();
1146
0
    nGCPCount = poUnderlyingDataset->GetGCPCount();
1147
0
    if (nGCPCount)
1148
0
        pasGCPList = GDALDuplicateGCPs(nGCPCount, pasUnderlyingGCPList);
1149
1150
0
    UnrefUnderlyingDataset(poUnderlyingDataset);
1151
1152
0
    return pasGCPList;
1153
0
}
1154
1155
/************************************************************************/
1156
/*                     GDALProxyPoolDatasetCreate()                     */
1157
/************************************************************************/
1158
1159
GDALProxyPoolDatasetH GDALProxyPoolDatasetCreate(
1160
    const char *pszSourceDatasetDescription, int nRasterXSize, int nRasterYSize,
1161
    GDALAccess eAccess, int bShared, const char *pszProjectionRef,
1162
    const double *padfGeoTransform)
1163
0
{
1164
0
    return reinterpret_cast<GDALProxyPoolDatasetH>(new GDALProxyPoolDataset(
1165
0
        pszSourceDatasetDescription, nRasterXSize, nRasterYSize, eAccess,
1166
0
        bShared, pszProjectionRef,
1167
0
        reinterpret_cast<const GDALGeoTransform *>(padfGeoTransform)));
1168
0
}
1169
1170
/************************************************************************/
1171
/*                       GDALProxyPoolDatasetDelete()                   */
1172
/************************************************************************/
1173
1174
void GDALProxyPoolDatasetDelete(GDALProxyPoolDatasetH hProxyPoolDataset)
1175
0
{
1176
0
    delete reinterpret_cast<GDALProxyPoolDataset *>(hProxyPoolDataset);
1177
0
}
1178
1179
/************************************************************************/
1180
/*              GDALProxyPoolDatasetAddSrcBandDescription()             */
1181
/************************************************************************/
1182
1183
void GDALProxyPoolDatasetAddSrcBandDescription(
1184
    GDALProxyPoolDatasetH hProxyPoolDataset, GDALDataType eDataType,
1185
    int nBlockXSize, int nBlockYSize)
1186
0
{
1187
0
    reinterpret_cast<GDALProxyPoolDataset *>(hProxyPoolDataset)
1188
0
        ->AddSrcBandDescription(eDataType, nBlockXSize, nBlockYSize);
1189
0
}
1190
1191
/* ******************************************************************** */
1192
/*                    GDALProxyPoolRasterBand()                         */
1193
/* ******************************************************************** */
1194
1195
GDALProxyPoolRasterBand::GDALProxyPoolRasterBand(GDALProxyPoolDataset *poDSIn,
1196
                                                 int nBandIn,
1197
                                                 GDALDataType eDataTypeIn,
1198
                                                 int nBlockXSizeIn,
1199
                                                 int nBlockYSizeIn)
1200
0
{
1201
0
    poDS = poDSIn;
1202
0
    nBand = nBandIn;
1203
0
    eDataType = eDataTypeIn;
1204
0
    nRasterXSize = poDSIn->GetRasterXSize();
1205
0
    nRasterYSize = poDSIn->GetRasterYSize();
1206
0
    nBlockXSize = nBlockXSizeIn;
1207
0
    nBlockYSize = nBlockYSizeIn;
1208
0
}
1209
1210
/* ******************************************************************** */
1211
/*                    GDALProxyPoolRasterBand()                         */
1212
/* ******************************************************************** */
1213
1214
GDALProxyPoolRasterBand::GDALProxyPoolRasterBand(
1215
    GDALProxyPoolDataset *poDSIn, GDALRasterBand *poUnderlyingRasterBand)
1216
0
{
1217
0
    poDS = poDSIn;
1218
0
    nBand = poUnderlyingRasterBand->GetBand();
1219
0
    eDataType = poUnderlyingRasterBand->GetRasterDataType();
1220
0
    nRasterXSize = poUnderlyingRasterBand->GetXSize();
1221
0
    nRasterYSize = poUnderlyingRasterBand->GetYSize();
1222
0
    poUnderlyingRasterBand->GetBlockSize(&nBlockXSize, &nBlockYSize);
1223
0
}
1224
1225
/* ******************************************************************** */
1226
/*                   ~GDALProxyPoolRasterBand()                         */
1227
/* ******************************************************************** */
1228
GDALProxyPoolRasterBand::~GDALProxyPoolRasterBand()
1229
0
{
1230
0
    if (metadataSet)
1231
0
        CPLHashSetDestroy(metadataSet);
1232
0
    if (metadataItemSet)
1233
0
        CPLHashSetDestroy(metadataItemSet);
1234
0
    CPLFree(pszUnitType);
1235
0
    CSLDestroy(papszCategoryNames);
1236
0
    if (poColorTable)
1237
0
        delete poColorTable;
1238
1239
0
    for (int i = 0; i < nSizeProxyOverviewRasterBand; i++)
1240
0
    {
1241
0
        if (papoProxyOverviewRasterBand[i])
1242
0
            delete papoProxyOverviewRasterBand[i];
1243
0
    }
1244
0
    CPLFree(papoProxyOverviewRasterBand);
1245
0
    if (poProxyMaskBand)
1246
0
        delete poProxyMaskBand;
1247
0
}
1248
1249
/************************************************************************/
1250
/*                AddSrcMaskBandDescriptionFromUnderlying()             */
1251
/************************************************************************/
1252
1253
void GDALProxyPoolRasterBand::AddSrcMaskBandDescriptionFromUnderlying()
1254
0
{
1255
0
    if (poProxyMaskBand != nullptr)
1256
0
        return;
1257
0
    GDALRasterBand *poUnderlyingBand = RefUnderlyingRasterBand();
1258
0
    if (poUnderlyingBand == nullptr)
1259
0
        return;
1260
0
    auto poSrcMaskBand = poUnderlyingBand->GetMaskBand();
1261
0
    int nSrcBlockXSize, nSrcBlockYSize;
1262
0
    poSrcMaskBand->GetBlockSize(&nSrcBlockXSize, &nSrcBlockYSize);
1263
0
    poProxyMaskBand = new GDALProxyPoolMaskBand(
1264
0
        cpl::down_cast<GDALProxyPoolDataset *>(poDS), this,
1265
0
        poSrcMaskBand->GetRasterDataType(), nSrcBlockXSize, nSrcBlockYSize);
1266
0
    UnrefUnderlyingRasterBand(poUnderlyingBand);
1267
0
}
1268
1269
/************************************************************************/
1270
/*                 AddSrcMaskBandDescription()                          */
1271
/************************************************************************/
1272
1273
void GDALProxyPoolRasterBand::AddSrcMaskBandDescription(
1274
    GDALDataType eDataTypeIn, int nBlockXSizeIn, int nBlockYSizeIn)
1275
0
{
1276
0
    CPLAssert(poProxyMaskBand == nullptr);
1277
0
    poProxyMaskBand = new GDALProxyPoolMaskBand(
1278
0
        cpl::down_cast<GDALProxyPoolDataset *>(poDS), this, eDataTypeIn,
1279
0
        nBlockXSizeIn, nBlockYSizeIn);
1280
0
}
1281
1282
/************************************************************************/
1283
/*                  RefUnderlyingRasterBand()                           */
1284
/************************************************************************/
1285
1286
GDALRasterBand *
1287
GDALProxyPoolRasterBand::RefUnderlyingRasterBand(bool bForceOpen) const
1288
0
{
1289
0
    GDALDataset *poUnderlyingDataset =
1290
0
        (cpl::down_cast<GDALProxyPoolDataset *>(poDS))
1291
0
            ->RefUnderlyingDataset(bForceOpen);
1292
0
    if (poUnderlyingDataset == nullptr)
1293
0
        return nullptr;
1294
1295
0
    GDALRasterBand *poBand = poUnderlyingDataset->GetRasterBand(nBand);
1296
0
    if (poBand == nullptr)
1297
0
    {
1298
0
        (cpl::down_cast<GDALProxyPoolDataset *>(poDS))
1299
0
            ->UnrefUnderlyingDataset(poUnderlyingDataset);
1300
0
    }
1301
0
    else if (nBlockXSize <= 0 || nBlockYSize <= 0)
1302
0
    {
1303
        // Here we try to load nBlockXSize&nBlockYSize from underlying band
1304
        // but we must guarantee that we will not access directly to
1305
        // nBlockXSize/nBlockYSize before RefUnderlyingRasterBand() is called
1306
0
        int nSrcBlockXSize, nSrcBlockYSize;
1307
0
        poBand->GetBlockSize(&nSrcBlockXSize, &nSrcBlockYSize);
1308
0
        const_cast<GDALProxyPoolRasterBand *>(this)->nBlockXSize =
1309
0
            nSrcBlockXSize;
1310
0
        const_cast<GDALProxyPoolRasterBand *>(this)->nBlockYSize =
1311
0
            nSrcBlockYSize;
1312
0
    }
1313
1314
0
    return poBand;
1315
0
}
1316
1317
/************************************************************************/
1318
/*                  UnrefUnderlyingRasterBand()                       */
1319
/************************************************************************/
1320
1321
void GDALProxyPoolRasterBand::UnrefUnderlyingRasterBand(
1322
    GDALRasterBand *poUnderlyingRasterBand) const
1323
0
{
1324
0
    if (poUnderlyingRasterBand)
1325
0
        (cpl::down_cast<GDALProxyPoolDataset *>(poDS))
1326
0
            ->UnrefUnderlyingDataset(poUnderlyingRasterBand->GetDataset());
1327
0
}
1328
1329
/************************************************************************/
1330
/*                             FlushCache()                             */
1331
/************************************************************************/
1332
1333
CPLErr GDALProxyPoolRasterBand::FlushCache(bool bAtClosing)
1334
0
{
1335
0
    GDALRasterBand *poUnderlyingRasterBand = RefUnderlyingRasterBand(false);
1336
0
    if (poUnderlyingRasterBand)
1337
0
    {
1338
0
        CPLErr eErr = poUnderlyingRasterBand->FlushCache(bAtClosing);
1339
0
        UnrefUnderlyingRasterBand(poUnderlyingRasterBand);
1340
0
        return eErr;
1341
0
    }
1342
0
    return CE_None;
1343
0
}
1344
1345
/************************************************************************/
1346
/*                            GetMetadata()                             */
1347
/************************************************************************/
1348
1349
char **GDALProxyPoolRasterBand::GetMetadata(const char *pszDomain)
1350
0
{
1351
0
    if (metadataSet == nullptr)
1352
0
        metadataSet =
1353
0
            CPLHashSetNew(hash_func_get_metadata, equal_func_get_metadata,
1354
0
                          free_func_get_metadata);
1355
1356
0
    GDALRasterBand *poUnderlyingRasterBand = RefUnderlyingRasterBand();
1357
0
    if (poUnderlyingRasterBand == nullptr)
1358
0
        return nullptr;
1359
1360
0
    char **papszUnderlyingMetadata =
1361
0
        poUnderlyingRasterBand->GetMetadata(pszDomain);
1362
1363
0
    GetMetadataElt *pElt =
1364
0
        static_cast<GetMetadataElt *>(CPLMalloc(sizeof(GetMetadataElt)));
1365
0
    pElt->pszDomain = (pszDomain) ? CPLStrdup(pszDomain) : nullptr;
1366
0
    pElt->papszMetadata = CSLDuplicate(papszUnderlyingMetadata);
1367
0
    CPLHashSetInsert(metadataSet, pElt);
1368
1369
0
    UnrefUnderlyingRasterBand(poUnderlyingRasterBand);
1370
1371
0
    return pElt->papszMetadata;
1372
0
}
1373
1374
/************************************************************************/
1375
/*                        GetMetadataItem()                             */
1376
/************************************************************************/
1377
1378
const char *GDALProxyPoolRasterBand::GetMetadataItem(const char *pszName,
1379
                                                     const char *pszDomain)
1380
0
{
1381
0
    if (metadataItemSet == nullptr)
1382
0
        metadataItemSet = CPLHashSetNew(hash_func_get_metadata_item,
1383
0
                                        equal_func_get_metadata_item,
1384
0
                                        free_func_get_metadata_item);
1385
1386
0
    GDALRasterBand *poUnderlyingRasterBand = RefUnderlyingRasterBand();
1387
0
    if (poUnderlyingRasterBand == nullptr)
1388
0
        return nullptr;
1389
1390
0
    const char *pszUnderlyingMetadataItem =
1391
0
        poUnderlyingRasterBand->GetMetadataItem(pszName, pszDomain);
1392
1393
0
    GetMetadataItemElt *pElt = static_cast<GetMetadataItemElt *>(
1394
0
        CPLMalloc(sizeof(GetMetadataItemElt)));
1395
0
    pElt->pszName = (pszName) ? CPLStrdup(pszName) : nullptr;
1396
0
    pElt->pszDomain = (pszDomain) ? CPLStrdup(pszDomain) : nullptr;
1397
0
    pElt->pszMetadataItem = (pszUnderlyingMetadataItem)
1398
0
                                ? CPLStrdup(pszUnderlyingMetadataItem)
1399
0
                                : nullptr;
1400
0
    CPLHashSetInsert(metadataItemSet, pElt);
1401
1402
0
    UnrefUnderlyingRasterBand(poUnderlyingRasterBand);
1403
1404
0
    return pElt->pszMetadataItem;
1405
0
}
1406
1407
/* ******************************************************************** */
1408
/*                       GetCategoryNames()                             */
1409
/* ******************************************************************** */
1410
1411
char **GDALProxyPoolRasterBand::GetCategoryNames()
1412
0
{
1413
0
    GDALRasterBand *poUnderlyingRasterBand = RefUnderlyingRasterBand();
1414
0
    if (poUnderlyingRasterBand == nullptr)
1415
0
        return nullptr;
1416
1417
0
    CSLDestroy(papszCategoryNames);
1418
0
    papszCategoryNames = nullptr;
1419
1420
0
    char **papszUnderlyingCategoryNames =
1421
0
        poUnderlyingRasterBand->GetCategoryNames();
1422
0
    if (papszUnderlyingCategoryNames)
1423
0
        papszCategoryNames = CSLDuplicate(papszUnderlyingCategoryNames);
1424
1425
0
    UnrefUnderlyingRasterBand(poUnderlyingRasterBand);
1426
1427
0
    return papszCategoryNames;
1428
0
}
1429
1430
/* ******************************************************************** */
1431
/*                           GetUnitType()                              */
1432
/* ******************************************************************** */
1433
1434
const char *GDALProxyPoolRasterBand::GetUnitType()
1435
0
{
1436
0
    GDALRasterBand *poUnderlyingRasterBand = RefUnderlyingRasterBand();
1437
0
    if (poUnderlyingRasterBand == nullptr)
1438
0
        return nullptr;
1439
1440
0
    CPLFree(pszUnitType);
1441
0
    pszUnitType = nullptr;
1442
1443
0
    const char *pszUnderlyingUnitType = poUnderlyingRasterBand->GetUnitType();
1444
0
    if (pszUnderlyingUnitType)
1445
0
        pszUnitType = CPLStrdup(pszUnderlyingUnitType);
1446
1447
0
    UnrefUnderlyingRasterBand(poUnderlyingRasterBand);
1448
1449
0
    return pszUnitType;
1450
0
}
1451
1452
/* ******************************************************************** */
1453
/*                          GetColorTable()                             */
1454
/* ******************************************************************** */
1455
1456
GDALColorTable *GDALProxyPoolRasterBand::GetColorTable()
1457
0
{
1458
0
    GDALRasterBand *poUnderlyingRasterBand = RefUnderlyingRasterBand();
1459
0
    if (poUnderlyingRasterBand == nullptr)
1460
0
        return nullptr;
1461
1462
0
    if (poColorTable)
1463
0
        delete poColorTable;
1464
0
    poColorTable = nullptr;
1465
1466
0
    GDALColorTable *poUnderlyingColorTable =
1467
0
        poUnderlyingRasterBand->GetColorTable();
1468
0
    if (poUnderlyingColorTable)
1469
0
        poColorTable = poUnderlyingColorTable->Clone();
1470
1471
0
    UnrefUnderlyingRasterBand(poUnderlyingRasterBand);
1472
1473
0
    return poColorTable;
1474
0
}
1475
1476
/* ******************************************************************** */
1477
/*                           GetOverview()                              */
1478
/* ******************************************************************** */
1479
1480
GDALRasterBand *GDALProxyPoolRasterBand::GetOverview(int nOverviewBand)
1481
0
{
1482
0
    if (nOverviewBand >= 0 && nOverviewBand < nSizeProxyOverviewRasterBand)
1483
0
    {
1484
0
        if (papoProxyOverviewRasterBand[nOverviewBand])
1485
0
            return papoProxyOverviewRasterBand[nOverviewBand];
1486
0
    }
1487
1488
0
    GDALRasterBand *poUnderlyingRasterBand = RefUnderlyingRasterBand();
1489
0
    if (poUnderlyingRasterBand == nullptr)
1490
0
        return nullptr;
1491
1492
0
    GDALRasterBand *poOverviewRasterBand =
1493
0
        poUnderlyingRasterBand->GetOverview(nOverviewBand);
1494
0
    if (poOverviewRasterBand == nullptr)
1495
0
    {
1496
0
        UnrefUnderlyingRasterBand(poUnderlyingRasterBand);
1497
0
        return nullptr;
1498
0
    }
1499
1500
0
    if (nOverviewBand >= nSizeProxyOverviewRasterBand)
1501
0
    {
1502
0
        papoProxyOverviewRasterBand =
1503
0
            static_cast<GDALProxyPoolOverviewRasterBand **>(
1504
0
                CPLRealloc(papoProxyOverviewRasterBand,
1505
0
                           sizeof(GDALProxyPoolOverviewRasterBand *) *
1506
0
                               (nOverviewBand + 1)));
1507
0
        for (int i = nSizeProxyOverviewRasterBand; i < nOverviewBand + 1; i++)
1508
0
            papoProxyOverviewRasterBand[i] = nullptr;
1509
0
        nSizeProxyOverviewRasterBand = nOverviewBand + 1;
1510
0
    }
1511
1512
0
    papoProxyOverviewRasterBand[nOverviewBand] =
1513
0
        new GDALProxyPoolOverviewRasterBand(
1514
0
            cpl::down_cast<GDALProxyPoolDataset *>(poDS), poOverviewRasterBand,
1515
0
            this, nOverviewBand);
1516
1517
0
    UnrefUnderlyingRasterBand(poUnderlyingRasterBand);
1518
1519
0
    return papoProxyOverviewRasterBand[nOverviewBand];
1520
0
}
1521
1522
/* ******************************************************************** */
1523
/*                     GetRasterSampleOverview()                        */
1524
/* ******************************************************************** */
1525
1526
GDALRasterBand *
1527
GDALProxyPoolRasterBand::GetRasterSampleOverview(GUIntBig /* nDesiredSamples */)
1528
0
{
1529
0
    CPLError(CE_Failure, CPLE_AppDefined,
1530
0
             "GDALProxyPoolRasterBand::GetRasterSampleOverview : not "
1531
0
             "implemented yet");
1532
0
    return nullptr;
1533
0
}
1534
1535
/* ******************************************************************** */
1536
/*                           GetMaskBand()                              */
1537
/* ******************************************************************** */
1538
1539
GDALRasterBand *GDALProxyPoolRasterBand::GetMaskBand()
1540
0
{
1541
0
    if (poProxyMaskBand)
1542
0
        return poProxyMaskBand;
1543
1544
0
    GDALRasterBand *poUnderlyingRasterBand = RefUnderlyingRasterBand();
1545
0
    if (poUnderlyingRasterBand == nullptr)
1546
0
        return nullptr;
1547
1548
0
    GDALRasterBand *poMaskBand = poUnderlyingRasterBand->GetMaskBand();
1549
1550
0
    poProxyMaskBand = new GDALProxyPoolMaskBand(
1551
0
        cpl::down_cast<GDALProxyPoolDataset *>(poDS), poMaskBand, this);
1552
1553
0
    UnrefUnderlyingRasterBand(poUnderlyingRasterBand);
1554
1555
0
    return poProxyMaskBand;
1556
0
}
1557
1558
/* ******************************************************************** */
1559
/*             GDALProxyPoolOverviewRasterBand()                        */
1560
/* ******************************************************************** */
1561
1562
GDALProxyPoolOverviewRasterBand::GDALProxyPoolOverviewRasterBand(
1563
    GDALProxyPoolDataset *poDSIn, GDALRasterBand *poUnderlyingOverviewBand,
1564
    GDALProxyPoolRasterBand *poMainBandIn, int nOverviewBandIn)
1565
0
    : GDALProxyPoolRasterBand(poDSIn, poUnderlyingOverviewBand),
1566
0
      poMainBand(poMainBandIn), nOverviewBand(nOverviewBandIn)
1567
0
{
1568
0
}
1569
1570
/* ******************************************************************** */
1571
/*                  ~GDALProxyPoolOverviewRasterBand()                  */
1572
/* ******************************************************************** */
1573
1574
GDALProxyPoolOverviewRasterBand::~GDALProxyPoolOverviewRasterBand()
1575
0
{
1576
0
    CPLAssert(nRefCountUnderlyingMainRasterBand == 0);
1577
0
}
1578
1579
/* ******************************************************************** */
1580
/*                    RefUnderlyingRasterBand()                         */
1581
/* ******************************************************************** */
1582
1583
GDALRasterBand *
1584
GDALProxyPoolOverviewRasterBand::RefUnderlyingRasterBand(bool bForceOpen) const
1585
0
{
1586
0
    poUnderlyingMainRasterBand =
1587
0
        poMainBand->RefUnderlyingRasterBand(bForceOpen);
1588
0
    if (poUnderlyingMainRasterBand == nullptr)
1589
0
        return nullptr;
1590
1591
0
    nRefCountUnderlyingMainRasterBand++;
1592
0
    return poUnderlyingMainRasterBand->GetOverview(nOverviewBand);
1593
0
}
1594
1595
/* ******************************************************************** */
1596
/*                  UnrefUnderlyingRasterBand()                         */
1597
/* ******************************************************************** */
1598
1599
void GDALProxyPoolOverviewRasterBand::UnrefUnderlyingRasterBand(
1600
    GDALRasterBand * /* poUnderlyingRasterBand */) const
1601
0
{
1602
0
    poMainBand->UnrefUnderlyingRasterBand(poUnderlyingMainRasterBand);
1603
0
    nRefCountUnderlyingMainRasterBand--;
1604
0
}
1605
1606
/* ******************************************************************** */
1607
/*                     GDALProxyPoolMaskBand()                          */
1608
/* ******************************************************************** */
1609
1610
GDALProxyPoolMaskBand::GDALProxyPoolMaskBand(
1611
    GDALProxyPoolDataset *poDSIn, GDALRasterBand *poUnderlyingMaskBand,
1612
    GDALProxyPoolRasterBand *poMainBandIn)
1613
0
    : GDALProxyPoolRasterBand(poDSIn, poUnderlyingMaskBand)
1614
0
{
1615
0
    poMainBand = poMainBandIn;
1616
1617
0
    poUnderlyingMainRasterBand = nullptr;
1618
0
    nRefCountUnderlyingMainRasterBand = 0;
1619
0
}
1620
1621
/* ******************************************************************** */
1622
/*                     GDALProxyPoolMaskBand()                          */
1623
/* ******************************************************************** */
1624
1625
GDALProxyPoolMaskBand::GDALProxyPoolMaskBand(
1626
    GDALProxyPoolDataset *poDSIn, GDALProxyPoolRasterBand *poMainBandIn,
1627
    GDALDataType eDataTypeIn, int nBlockXSizeIn, int nBlockYSizeIn)
1628
0
    : GDALProxyPoolRasterBand(poDSIn, 1, eDataTypeIn, nBlockXSizeIn,
1629
0
                              nBlockYSizeIn),
1630
0
      poMainBand(poMainBandIn)
1631
0
{
1632
0
}
1633
1634
/* ******************************************************************** */
1635
/*                          ~GDALProxyPoolMaskBand()                    */
1636
/* ******************************************************************** */
1637
1638
GDALProxyPoolMaskBand::~GDALProxyPoolMaskBand()
1639
0
{
1640
0
    CPLAssert(nRefCountUnderlyingMainRasterBand == 0);
1641
0
}
1642
1643
/* ******************************************************************** */
1644
/*                    RefUnderlyingRasterBand()                         */
1645
/* ******************************************************************** */
1646
1647
GDALRasterBand *
1648
GDALProxyPoolMaskBand::RefUnderlyingRasterBand(bool bForceOpen) const
1649
0
{
1650
0
    poUnderlyingMainRasterBand =
1651
0
        poMainBand->RefUnderlyingRasterBand(bForceOpen);
1652
0
    if (poUnderlyingMainRasterBand == nullptr)
1653
0
        return nullptr;
1654
1655
0
    nRefCountUnderlyingMainRasterBand++;
1656
0
    return poUnderlyingMainRasterBand->GetMaskBand();
1657
0
}
1658
1659
/* ******************************************************************** */
1660
/*                  UnrefUnderlyingRasterBand()                         */
1661
/* ******************************************************************** */
1662
1663
void GDALProxyPoolMaskBand::UnrefUnderlyingRasterBand(
1664
    GDALRasterBand * /* poUnderlyingRasterBand */) const
1665
0
{
1666
0
    poMainBand->UnrefUnderlyingRasterBand(poUnderlyingMainRasterBand);
1667
0
    nRefCountUnderlyingMainRasterBand--;
1668
0
}
1669
1670
//! @endcond