Skip to content

Commit c3579d7

Browse files
committed
Integrate the cache code in libgpuarray to avoid an external dependency
1 parent fbc3f91 commit c3579d7

5 files changed

Lines changed: 758 additions & 5 deletions

File tree

‎CMakeLists.txt‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required(VERSION 2.8)
1+
cmake_minimum_required(VERSION 3.0)
22

33
PROJECT(libgpuarray C)
44

@@ -12,6 +12,8 @@ execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_SOURCE_DIR}/l
1212
set(LIBRARY_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/lib)
1313

1414
set(CMAKE_OSX_ARCHITECTURES x86_64)
15+
set(CMAKE_C_VISIBILITY_PRESET hidden)
16+
set(CMAKE_VISIBILITY_INLINES_HIDDEN 1)
1517

1618
add_subdirectory(src)
1719
add_subdirectory(tests)

‎src/CMakeLists.txt‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ macro (set_rel var)
3333
endmacro()
3434

3535
set_rel(GPUARRAY_SRC
36+
cache/lru.c
37+
cache/twoq.c
3638
gpuarray_types.c
3739
gpuarray_error.c
3840
gpuarray_util.c
@@ -124,12 +126,9 @@ add_library(gpuarray SHARED ${GPUARRAY_SRC})
124126
set_target_properties(gpuarray PROPERTIES
125127
COMPILE_FLAGS "-DGPUARRAY_BUILDING_DLL -DGPUARRAY_SHARED"
126128
INSTALL_NAME_DIR ${CMAKE_INSTALL_PREFIX}/lib
129+
MACOSX_RPATH OFF
127130
)
128131

129-
if (CMAKE_MAJOR_VERSION GREATER 2)
130-
set_target_properties(gpuarray PROPERTIES MACOSX_RPATH OFF)
131-
endif()
132-
133132
add_library(gpuarray-static STATIC ${GPUARRAY_SRC})
134133

135134
target_link_libraries(gpuarray cache)

‎src/cache.h‎

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#ifndef CACHE_H
2+
#define CACHE_H
3+
4+
#include <stdint.h>
5+
#include <stdlib.h>
6+
7+
typedef void *cache_key_t;
8+
typedef void *cache_value_t;
9+
10+
typedef int (*cache_eq_fn)(cache_key_t, cache_key_t);
11+
typedef uint32_t (*cache_hash_fn)(cache_key_t);
12+
typedef void (*cache_freek_fn)(cache_key_t);
13+
typedef void (*cache_freev_fn)(cache_value_t);
14+
15+
typedef struct _cache cache;
16+
17+
struct _cache {
18+
/**
19+
* Add the specified value to the cache under the key k, replacing
20+
* any previous value.
21+
*
22+
* The value and key belong to the cache and will be freed with the
23+
* supplied free functions whether the add is successful or not.
24+
*
25+
* The key and value data must stay valid until they are explicitely
26+
* released by the cache when it calls the supplied free functions.
27+
*
28+
* NULL is not a valid value or key.
29+
*
30+
* Returns 0 if value was added sucessfully and some other value otherwise.
31+
*/
32+
int (*add)(cache *c, cache_key_t k, cache_value_t v);
33+
34+
/**
35+
* Remove the data associated with k from the cache. The value and
36+
* the key will be free with the supplied free functions.
37+
*
38+
* The passed in key is not claimed by the cache and need only be
39+
* valid until the call returns. It will not be freed through the
40+
* key free function.
41+
*
42+
* Returns 1 if the key was in the cache and 0 if not.
43+
*/
44+
int (*del)(cache *c, const cache_key_t k);
45+
46+
/**
47+
* Get the data entry associated with k.
48+
*
49+
* The passed in key is not claimed by the cache and need only be
50+
* valid until the call returns. It will not be freed through the
51+
* key free function.
52+
*
53+
* Returns NULL if the key is not found, a value otherwise.
54+
*/
55+
cache_value_t (*get)(cache *c, const cache_key_t k);
56+
57+
/**
58+
* Releases all entries in the cache as well as all of the support
59+
* structures.
60+
*
61+
* This must NOT free the passed in pointer.
62+
*/
63+
void (*destroy)(cache *c);
64+
cache_eq_fn keq;
65+
cache_hash_fn khash;
66+
cache_freek_fn kfree;
67+
cache_freev_fn vfree;
68+
/* Extra data goes here depending on cache type */
69+
};
70+
71+
static void nofree(void *v) {}
72+
73+
cache *cache_lru(size_t max_size, size_t elasticity,
74+
cache_eq_fn keq, cache_hash_fn khash,
75+
cache_freek_fn kfree, cache_freev_fn vfree);
76+
77+
cache *cache_twoq(size_t hot_size, size_t warm_size,
78+
size_t cold_size, size_t elasticity,
79+
cache_eq_fn keq, cache_hash_fn khash,
80+
cache_freek_fn kfree, cache_freev_fn vfree);
81+
82+
/* API functions */
83+
static inline int cache_add(cache *c, cache_key_t k, cache_value_t v) {
84+
return c->add(c, k, v);
85+
}
86+
87+
static inline int cache_del(cache *c, cache_key_t k) {
88+
return c->del(c, k);
89+
}
90+
91+
static inline cache_value_t cache_get(cache *c, cache_key_t k) {
92+
return c->get(c, k);
93+
}
94+
95+
static inline void cache_destroy(cache *c) {
96+
c->destroy(c);
97+
free(c);
98+
}
99+
100+
#endif

0 commit comments

Comments
 (0)