Skip to content

Commit f4f225e

Browse files
committed
Add appropriate 'const' qualifier to the parameters
The 'const' qualifier is useful for ensuring type safety. Specifically, 'const' can clarify pointer ownership. It is important to note that 'const' applied to the target of a pointer or reference does not imply that the target is constant. Instead, it indicates a read-only view of the object. This means that while the object can not be modified through this particular reference or pointer, it may still be altered through other means. An object is truly immutable only when 'const' is used in its definition. The primary purpose of using 'const' is not to facilitate compiler optimizations, but to safeguard against programming errors.
1 parent 8391ae2 commit f4f225e

File tree

2 files changed

+12
-13
lines changed

2 files changed

+12
-13
lines changed

src/cache.c

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -192,27 +192,26 @@ cache_t *cache_create(int size_bits)
192192
cache_t *cache = malloc(sizeof(cache_t));
193193
if (!cache)
194194
return NULL;
195+
195196
cache_size_bits = size_bits;
196197
cache_size = 1 << size_bits;
197198
for (i = 0; i < THRESHOLD; i++) {
198199
cache->lists[i] = malloc(sizeof(struct list_head));
199-
if (!cache->lists[i]) {
200+
if (!cache->lists[i])
200201
goto free_lists;
201-
}
202202
INIT_LIST_HEAD(cache->lists[i]);
203203
}
204204

205205
cache->map = malloc(sizeof(hashtable_t));
206-
if (!cache->map) {
206+
if (!cache->map)
207207
goto free_lists;
208-
}
208+
209209
cache->map->ht_list_head = malloc(cache_size * sizeof(struct hlist_head));
210-
if (!cache->map->ht_list_head) {
210+
if (!cache->map->ht_list_head)
211211
goto free_map;
212-
}
213-
for (uint32_t i = 0; i < cache_size; i++) {
214-
INIT_HLIST_HEAD(&cache->map->ht_list_head[i]);
215-
}
212+
213+
for (size_t ii = 0; ii < cache_size; ii++)
214+
INIT_HLIST_HEAD(&cache->map->ht_list_head[ii]);
216215
cache->list_size = 0;
217216
cache_mp =
218217
mpool_create(cache_size * sizeof(lfu_entry_t), sizeof(lfu_entry_t));
@@ -229,7 +228,7 @@ cache_t *cache_create(int size_bits)
229228
return NULL;
230229
}
231230

232-
void *cache_get(cache_t *cache, uint32_t key)
231+
void *cache_get(const cache_t *cache, uint32_t key)
233232
{
234233
if (!cache->capacity ||
235234
hlist_empty(&cache->map->ht_list_head[cache_hash(key)]))
@@ -326,7 +325,7 @@ uint32_t cache_freq(struct cache *cache, uint32_t key)
326325
}
327326

328327
#if RV32_HAS(JIT)
329-
bool cache_hot(struct cache *cache, uint32_t key)
328+
bool cache_hot(const struct cache *cache, uint32_t key)
330329
{
331330
if (!cache->capacity ||
332331
hlist_empty(&cache->map->ht_list_head[cache_hash(key)]))

src/cache.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ struct cache *cache_create(int size_bits);
2222
* @key: the key of the specified entry
2323
* @return: the specified entry or NULL
2424
*/
25-
void *cache_get(struct cache *cache, uint32_t key);
25+
void *cache_get(const struct cache *cache, uint32_t key);
2626

2727
/**
2828
* cache_put - insert a new entry into the cache
@@ -47,7 +47,7 @@ void cache_free(struct cache *cache);
4747
* @cache: a pointer points to target cache
4848
* @key: the key of the specified entry
4949
*/
50-
bool cache_hot(struct cache *cache, uint32_t key);
50+
bool cache_hot(const struct cache *cache, uint32_t key);
5151
#endif
5252

5353
uint32_t cache_freq(struct cache *cache, uint32_t key);

0 commit comments

Comments
 (0)