Skip to content

Commit fff0a4d

Browse files
committed
fix problem with cache
1 parent 196e983 commit fff0a4d

3 files changed

Lines changed: 11 additions & 3 deletions

File tree

src/main/terrain/terrain.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ static float getGeigHtAmslMeters(const gpsLocation_t *loc)
9292

9393
// find the grid
9494
gridBlock_t *grid = &(findGridCache(&info)->gridBlock);
95+
if(grid == NULL){
96+
return TERRAIN_STATUS_CACHE_FULL;
97+
}
9598

9699
// check we have all 4 required heights, it's check if grid is loaded from SD card
97100
if (!checkBitmap(grid, info.idx_x, info.idx_y) || !checkBitmap(grid, info.idx_x, info.idx_y + 1) || !checkBitmap(grid, info.idx_x + 1, info.idx_y) || !checkBitmap(grid, info.idx_x + 1, info.idx_y + 1)) {

src/main/terrain/terrain.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#define TERRAIN_STATUS_WRONG_BITMAP (-3)
3737
#define TERRAIN_STATUS_NO_DATA (-4)
3838
#define TERRAIN_STATUS_NO_CARD (-5)
39+
#define TERRAIN_STATUS_CACHE_FULL (-6)
3940

4041
// MAVLink sends 4x4 grids
4142
#define TERRAIN_GRID_MAVLINK_SIZE 4

src/main/terrain/terrain_utils.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ uint32_t eastBlocks(gridBlock_t *gridBlock)
177177
*/
178178
gridCache_t* findGridCache(gridInfo_t *info)
179179
{
180-
uint16_t oldest_i = 0;
180+
int16_t oldest_i = -1;
181181

182182
// see if we have that grid
183183
const timeMs_t nowMs = millis();
@@ -190,13 +190,17 @@ gridCache_t* findGridCache(gridInfo_t *info)
190190
cache[i].lastAccessMs = nowMs;
191191
return &cache[i];
192192
}
193-
if (cache[i].lastAccessMs < cache[oldest_i].lastAccessMs) {
194-
oldest_i = i;
193+
if (cache[i].lastAccessMs < cache[oldest_i].lastAccessMs && cache[oldest_i].state != GRID_CACHE_DISKWAIT) {
194+
oldest_i = (int16_t)i;
195195
}
196196
}
197197

198198
// Not found. Use the oldest grid and make it this grid,
199199
// initially unpopulated
200+
if(oldest_i < 0){
201+
return NULL;
202+
}
203+
200204
gridCache_t *gridCache = &cache[oldest_i];
201205
memset(gridCache, 0, sizeof(gridCache_t));
202206

0 commit comments

Comments
 (0)