Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add lru cache #841

Merged
merged 15 commits into from
Dec 20, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: add lru cache module and tests
  • Loading branch information
jean-roland committed Dec 20, 2024
commit 26293609878093145d693a67cdd2ee0be139b9ac
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -503,6 +503,7 @@ if(UNIX OR MSVC)
add_executable(z_api_bytes_test ${PROJECT_SOURCE_DIR}/tests/z_api_bytes_test.c)
add_executable(z_api_encoding_test ${PROJECT_SOURCE_DIR}/tests/z_api_encoding_test.c)
add_executable(z_refcount_test ${PROJECT_SOURCE_DIR}/tests/z_refcount_test.c)
add_executable(z_lru_cache_test ${PROJECT_SOURCE_DIR}/tests/z_lru_cache_test.c)

target_link_libraries(z_data_struct_test zenohpico::lib)
target_link_libraries(z_channels_test zenohpico::lib)
@@ -521,6 +522,7 @@ if(UNIX OR MSVC)
target_link_libraries(z_api_bytes_test zenohpico::lib)
target_link_libraries(z_api_encoding_test zenohpico::lib)
target_link_libraries(z_refcount_test zenohpico::lib)
target_link_libraries(z_lru_cache_test zenohpico::lib)

configure_file(${PROJECT_SOURCE_DIR}/tests/modularity.py ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/modularity.py COPYONLY)
configure_file(${PROJECT_SOURCE_DIR}/tests/raweth.py ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/raweth.py COPYONLY)
@@ -545,6 +547,7 @@ if(UNIX OR MSVC)
add_test(z_api_bytes_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_api_bytes_test)
add_test(z_api_encoding_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_api_encoding_test)
add_test(z_refcount_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_refcount_test)
add_test(z_lru_cache_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_lru_cache_test)
endif()

if(BUILD_MULTICAST)
69 changes: 69 additions & 0 deletions include/zenoh-pico/collections/lru_cache.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//
// Copyright (c) 2024 ZettaScale Technology
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
//
// Contributors:
// ZettaScale Zenoh Team, <zenoh@zettascale.tech>
//

#ifndef ZENOH_PICO_COLLECTIONS_LRUCACHE_H
#define ZENOH_PICO_COLLECTIONS_LRUCACHE_H

#include <stdint.h>

#include "zenoh-pico/collections/element.h"

#ifdef __cplusplus
extern "C" {
#endif

// TODO: move to config
#define Z_FEATURE_CACHE_TREE 0

// Three way comparison function pointer
typedef int (*_z_lru_val_cmp_f)(const void *first, const void *second);

// Node struct: {node_data; generic type}
typedef void _z_lru_cache_node_t;

/*-------- Dynamically allocated vector --------*/
/**
* A least recently used cache implementation
*/
typedef struct _z_lru_cache_t {
size_t capacity; // Max number of node
size_t len; // Number of node
_z_lru_cache_node_t *head; // List head
_z_lru_cache_node_t *tail; // List tail
#if Z_FEATURE_CACHE_TREE == 1
_z_lru_cache_node_t *root; // Tree root
#endif
} _z_lru_cache_t;

_z_lru_cache_t _z_lru_cache_init(size_t capacity);
void *_z_lru_cache_get(_z_lru_cache_t *cache, void *value, _z_lru_val_cmp_f compare);
z_result_t _z_lru_cache_insert(_z_lru_cache_t *cache, void *value, size_t value_size, _z_lru_val_cmp_f compare);
void _z_lru_cache_delete(_z_lru_cache_t *cache);

#define _Z_LRU_CACHE_DEFINE(name, type, compare_f) \
typedef _z_lru_cache_t name##_lru_cache_t; \
static inline name##_lru_cache_t name##_lru_cache_init(size_t capacity) { return _z_lru_cache_init(capacity); } \
static inline type *name##_lru_cache_get(name##_lru_cache_t *cache, type *val) { \
return (type *)_z_lru_cache_get(cache, (void *)val, compare_f); \
} \
static inline z_result_t name##_lru_cache_insert(name##_lru_cache_t *cache, type *val) { \
return _z_lru_cache_insert(cache, (void *)val, sizeof(type), compare_f); \
} \
static inline void name##_lru_cache_delete(name##_lru_cache_t *cache) { _z_lru_cache_delete(cache); }

#ifdef __cplusplus
}
#endif

#endif /* ZENOH_PICO_COLLECTIONS_LRUCACHE_H */
Loading