From a568a0ba23c1c2f40148f6715c9f76dba9dc1780 Mon Sep 17 00:00:00 2001
From: "oljas.jarasbaev" <oljas.jarasbaev@demlabs.net>
Date: Tue, 9 Jul 2024 07:25:27 +0000
Subject: [PATCH] Bugfix 12148

---
 modules/chain/dap_chain_cell.c         | 17 +++++++++++++++--
 modules/chain/include/dap_chain_cell.h |  3 +++
 2 files changed, 18 insertions(+), 2 deletions(-)

diff --git a/modules/chain/dap_chain_cell.c b/modules/chain/dap_chain_cell.c
index 750c05c7ab..e930eddcd9 100644
--- a/modules/chain/dap_chain_cell.c
+++ b/modules/chain/dap_chain_cell.c
@@ -41,7 +41,7 @@
 #define DAP_CHAIN_CELL_FILE_SIGNATURE 0xfa340bef153eba48
 #define DAP_CHAIN_CELL_FILE_TYPE_RAW 0
 #define DAP_CHAIN_CELL_FILE_TYPE_COMPRESSED 1
-#define DAP_MAPPED_VOLUME_LIMIT ( 1 << 28 ) // 256 MB for now, may be should be configurable?
+#define DAP_MAPPED_VOLUME_LIMIT ( 1 << 20 ) // 256 MB for now, may be should be configurable?
 /**
   * @struct dap_chain_cell_file_header
   */
@@ -172,6 +172,9 @@ DAP_STATIC_INLINE int s_cell_map_new_volume(dap_chain_cell_t *a_cell, size_t a_f
                         a_cell->file_storage_path, a_cell->id.uint64, errno);
         return -1;
     }
+#ifdef DAP_OS_DARWIN
+    a_cell->cur_vol_start = l_volume_start;
+#endif
 #endif
     a_cell->map_pos = a_cell->map + l_offset;
     a_cell->map_range_bounds = dap_list_append(a_cell->map_range_bounds, a_cell->map);
@@ -523,6 +526,16 @@ static int s_cell_file_atom_add(dap_chain_cell_t *a_cell, dap_chain_atom_ptr_t a
     debug_if (s_debug_more && a_cell->chain->is_mapped, L_DEBUG, "After writing an atom of size %lu, stream pos of %s is %lu and map shift is %lu", 
                                             a_atom_size, a_cell->file_storage_path, ftell(a_cell->file_storage),
                                             (size_t)(a_cell->map_pos - a_cell->map));
+#ifdef DAP_OS_DARWIN
+    if (a_cell->chain->is_mapped) {
+        if ( MAP_FAILED == (a_cell->map = mmap(a_cell->map, dap_page_roundup(DAP_MAPPED_VOLUME_LIMIT), PROT_READ|PROT_WRITE,
+                                            MAP_PRIVATE|MAP_FIXED, fileno(a_cell->file_storage), a_cell->cur_vol_start)) ) {
+            log_it(L_ERROR, "Chain cell \"%s\" 0x%016"DAP_UINT64_FORMAT_X" cannot be remapped, errno %d",
+                            a_cell->file_storage_path, a_cell->id.uint64, errno);
+            return -1;
+        }
+    }
+#endif
     return 0;
 }
 
@@ -602,7 +615,7 @@ ssize_t dap_chain_cell_file_append(dap_chain_cell_t *a_cell, const void *a_atom,
         ++l_count;
         l_total_res = a_atom_size + sizeof(uint64_t);
     }
-
+    
     if (l_total_res) {
         fflush(a_cell->file_storage);
 #ifdef DAP_OS_WINDOWS
diff --git a/modules/chain/include/dap_chain_cell.h b/modules/chain/include/dap_chain_cell.h
index 88ba245e16..db47d0150a 100644
--- a/modules/chain/include/dap_chain_cell.h
+++ b/modules/chain/include/dap_chain_cell.h
@@ -38,6 +38,9 @@ typedef struct dap_chain_cell {
     FILE *file_storage;
     uint8_t file_storage_type;
     dap_list_t *map_range_bounds;
+#ifdef DAP_OS_DARWIN
+    size_t cur_vol_start;
+#endif
     pthread_rwlock_t storage_rwlock;
     UT_hash_handle hh;
 } dap_chain_cell_t;
-- 
GitLab