From 882c7d2f1afce6264b6a9bb8ff95329ba19b499e Mon Sep 17 00:00:00 2001
From: Uwe Schulzweida <uwe.schulzweida@mpimet.mpg.de>
Date: Mon, 17 Oct 2022 09:03:02 +0200
Subject: [PATCH] Added environment variable CDI_CHUNK_CACHE_MAX to set the
 maximum chunk cache size

---
 ChangeLog               | 1 +
 doc/tex/environment.tex | 1 +
 src/cdi_int.c           | 4 ++++
 src/cdi_int.h           | 1 +
 4 files changed, 7 insertions(+)

diff --git a/ChangeLog b/ChangeLog
index 4f20705eb..a0f138e77 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,6 +1,7 @@
 2022-10-16  Uwe Schulzweida
 
         * Added environment variable CDI_CHUNK_CACHE to set the NetCDF4 chunk cache size
+        * Added environment variable CDI_CHUNK_CACHE_MAX to set the maximum chunk cache size
 
 2022-10-14  Uwe Schulzweida
 
diff --git a/doc/tex/environment.tex b/doc/tex/environment.tex
index a2b98254b..d5140a13c 100644
--- a/doc/tex/environment.tex
+++ b/doc/tex/environment.tex
@@ -12,6 +12,7 @@ The following table describes the environment variables that affect {\CDI}.
 {\bfseries Variable name}           &  {\bfseries Default} & {\bfseries Description} \\ \hline
 CDI\_CONVERT\_CUBESPHERE &        1   & Convert cubed-sphere data to unstructured grid. \\
 CDI\_CHUNK\_CACHE            &         0   & Set the NetCDF4 chunk cache size. \\
+CDI\_CHUNK\_CACHE\_MAX  &         0   & Set maximum chunk cache size. \\
 CDI\_GRIB1\_TEMPLATE          &   None   &  Path to a GRIB1 template file for GRIB\_API. \\
 CDI\_GRIB2\_TEMPLATE          &   None   &  Path to a GRIB2 template file for GRIB\_API. \\
 CDI\_INVENTORY\_MODE        &   None   &  Set to time to skip double variable entries. \\
diff --git a/src/cdi_int.c b/src/cdi_int.c
index 683373c88..16cc2b97f 100644
--- a/src/cdi_int.c
+++ b/src/cdi_int.c
@@ -40,6 +40,7 @@ int CDI_CMOR_Mode = 0;
 int CDI_Reduce_Dim = 0;
 size_t CDI_Netcdf_Hdr_Pad = 0UL;
 size_t CDI_Chunk_Cache = 0UL;
+size_t CDI_Chunk_Cache_Max = 0UL;
 bool CDI_Netcdf_Lazy_Grid_Load = false;
 
 char *cdiPartabPath = NULL;
@@ -367,6 +368,9 @@ cdiInitialize(void)
       value = cdi_getenv_int("CDI_CHUNK_CACHE");
       if (value >= 0) CDI_Chunk_Cache = (size_t) value;
 
+      value = cdi_getenv_int("CDI_CHUNK_CACHE_MAX");
+      if (value >= 0) CDI_Chunk_Cache_Max = (size_t) value;
+
       envstr = getenv("CDI_GRIB1_TEMPLATE");
       if (envstr) CDI_GRIB1_Template = envstr;
 
diff --git a/src/cdi_int.h b/src/cdi_int.h
index 9767ae25d..be23d8b58 100644
--- a/src/cdi_int.h
+++ b/src/cdi_int.h
@@ -379,6 +379,7 @@ extern int CDI_CMOR_Mode;
 extern int CDI_Reduce_Dim;
 extern size_t CDI_Netcdf_Hdr_Pad;
 extern size_t CDI_Chunk_Cache;
+extern size_t CDI_Chunk_Cache_Max;
 extern bool CDI_Netcdf_Lazy_Grid_Load;
 extern int STREAM_Debug;
 
-- 
GitLab