-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmap-tile-cache.c
129 lines (108 loc) · 3.01 KB
/
map-tile-cache.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
* SPDX-FileCopyrightText: 2021 Samuel Cuella <[email protected]>
*
* This file is part of SoFIS - an open source EFIS
*
* SPDX-License-Identifier: GPL-2.0-only
*/
#include <stdio.h>
#include "map-tile-cache.h"
#include "generic-layer.h"
#include "SDL_timer.h"
static MapTileDescriptor *map_tile_cache_oldest(MapTileCache *self);
MapTileCache *map_tile_cache_init(MapTileCache *self, size_t cache_size)
{
self->acache = cache_size;
self->tile_cache = calloc(self->acache, sizeof(MapTileDescriptor));
if(!self->tile_cache)
return NULL;
return self;
}
MapTileCache *map_tile_cache_dispose(MapTileCache *self)
{
for(int i = 0; i < self->ncached; i++)
generic_layer_unref(self->tile_cache[i].layer);
if(self->tile_cache)
free(self->tile_cache);
return self;
}
bool map_tile_cache_set_size(MapTileCache *self, uintf8_t cache_size)
{
size_t old_size;
void *tmp;
old_size = self->acache;
self->acache = cache_size;
tmp = realloc(self->tile_cache, self->acache * sizeof(MapTileDescriptor));
if(!tmp){
self->acache= old_size;
return false;
}
self->tile_cache = tmp;
/*clear cache ?*/
return true;
}
void map_tile_cache_clear(MapTileCache *self)
{
for(int i = 0; i < self->ncached; i++)
generic_layer_unref(self->tile_cache[i].layer);
self->ncached = 0;
}
GenericLayer *map_tile_cache_get(MapTileCache *self,
uintf8_t level, int32_t x, int32_t y)
{
for(int i = 0; i < self->ncached; i++){
if(map_tile_descriptor_match(&self->tile_cache[i],level, x, y)){
self->tile_cache[i].atime = SDL_GetTicks();
return self->tile_cache[i].layer;
}
}
return NULL;
}
bool map_tile_cache_add(MapTileCache *self, GenericLayer *tile,
uintf8_t level, int32_t x, int32_t y)
{
generic_layer_ref(tile);
if(self->ncached == self->acache){
MapTileDescriptor *slot;
slot = map_tile_cache_oldest(self);
generic_layer_unref(slot->layer);
*slot = (MapTileDescriptor){
.layer = tile,
.level = level,
.x = x,
.y = y
};
}else{
self->tile_cache[self->ncached++] = (MapTileDescriptor){
.layer = tile,
.level = level,
.x = x,
.y = y
};
}
return true;
}
/**
* @brief Returns the cache location used by the least used descriptor
* (least recent last usage)
*
* MapTileDescriptor internal usage, not meant to be used by client code
*
* @param self a MapTileCache
* @return The slot location
*/
static MapTileDescriptor *map_tile_cache_oldest(MapTileCache *self)
{
Uint32 now;
MapTileDescriptor *rv;
int rv_idx;
now = SDL_GetTicks();
rv_idx = 0;
for(int i = 0; i < self->ncached; i++){
if(self->tile_cache[i].atime < now){
rv_idx = i;
now = self->tile_cache[i].atime;
}
}
return &self->tile_cache[rv_idx];
}