From 9d414316da3d92c791f49df61099920dddc48bd4 Mon Sep 17 00:00:00 2001 From: Kyle Kearney Date: Tue, 29 Oct 2019 16:25:32 -0700 Subject: [PATCH] TDBStore: Fix potential alignment issue in default addresses When 10 pages is larger than 2 sectors, align the selected size down to be an even multiple of the sector size, to ensure that the allocated space divides cleanly in half for garbage collection. --- features/storage/kvstore/tdbstore/TDBStore.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/features/storage/kvstore/tdbstore/TDBStore.cpp b/features/storage/kvstore/tdbstore/TDBStore.cpp index f367283887..a3ccc7a700 100644 --- a/features/storage/kvstore/tdbstore/TDBStore.cpp +++ b/features/storage/kvstore/tdbstore/TDBStore.cpp @@ -1439,7 +1439,8 @@ int TDBStore::get_default_flash_addresses(bd_addr_t *start_address, bd_size_t *s } // Let's work from end of the flash backwards - bd_addr_t curr_addr = flash.get_flash_start() + flash.get_flash_size(); + bd_addr_t end_of_flash = flash.get_flash_start() + flash.get_flash_size(); + bd_addr_t curr_addr = end_of_flash; bd_size_t sector_space = 0; for (int i = STORE_SECTORS; i; i--) { @@ -1453,7 +1454,9 @@ int TDBStore::get_default_flash_addresses(bd_addr_t *start_address, bd_size_t *s *size = sector_space; } else { curr_addr -= page_space; - *size = page_space; + // Align to 2 sector boundary so that garbage collection works properly + curr_addr = align_down(curr_addr, 2 * flash.get_sector_size(curr_addr)); + *size = end_of_flash - curr_addr; } // Store- and application-sectors mustn't overlap