@@ -54,6 +54,22 @@ typedef struct ipc_mapped_handle_cache_t {
54
54
55
55
ipc_mapped_handle_cache_global_t * IPC_MAPPED_CACHE_GLOBAL = NULL ;
56
56
57
+ // Returns value of the UMF_MAX_OPENED_IPC_HANDLES environment variable
58
+ // or 0 if it is not set.
59
+ static size_t umfIpcCacheGlobalInitMaxOpenedHandles (void ) {
60
+ const char * max_size_str = getenv ("UMF_MAX_OPENED_IPC_HANDLES" );
61
+ if (max_size_str ) {
62
+ char * endptr ;
63
+ size_t max_size = strtoul (max_size_str , & endptr , 10 );
64
+ if (* endptr == '\0' ) {
65
+ return max_size ;
66
+ }
67
+ LOG_ERR ("Invalid value of UMF_MAX_OPENED_IPC_HANDLES: %s" ,
68
+ max_size_str );
69
+ }
70
+ return 0 ;
71
+ }
72
+
57
73
umf_result_t umfIpcCacheGlobalInit (void ) {
58
74
umf_result_t ret = UMF_RESULT_SUCCESS ;
59
75
ipc_mapped_handle_cache_global_t * cache_global =
@@ -78,8 +94,7 @@ umf_result_t umfIpcCacheGlobalInit(void) {
78
94
goto err_mutex_destroy ;
79
95
}
80
96
81
- // TODO: make max_size configurable via environment variable
82
- cache_global -> max_size = 0 ;
97
+ cache_global -> max_size = umfIpcCacheGlobalInitMaxOpenedHandles ();
83
98
cache_global -> cur_size = 0 ;
84
99
cache_global -> lru_list = NULL ;
85
100
@@ -189,7 +204,16 @@ umfIpcHandleMappedCacheGet(ipc_mapped_handle_cache_handle_t cache,
189
204
if (entry == NULL && cache -> global -> max_size != 0 &&
190
205
cache -> global -> cur_size >= cache -> global -> max_size ) {
191
206
// If max_size is set and the cache is full, evict the least recently used entry.
192
- entry = cache -> global -> lru_list -> prev ;
207
+ // we need to search for the least recently used entry with ref_count == 0
208
+ // The utlist implementation of the doubly-linked list keeps a tail pointer in head->prev
209
+ ipc_handle_cache_entry_t * candidate = cache -> global -> lru_list -> prev ;
210
+ do {
211
+ if (candidate -> ref_count == 0 ) {
212
+ entry = candidate ;
213
+ break ;
214
+ }
215
+ candidate = candidate -> prev ;
216
+ } while (candidate != cache -> global -> lru_list -> prev );
193
217
}
194
218
195
219
if (entry ) { // we have eviction candidate
@@ -242,3 +266,20 @@ umfIpcHandleMappedCacheGet(ipc_mapped_handle_cache_handle_t cache,
242
266
243
267
return ret ;
244
268
}
269
+
270
+ umf_result_t
271
+ umfIpcHandleMappedCacheRelease (ipc_mapped_handle_cache_value_t * cacheValue ) {
272
+ if (!cacheValue ) {
273
+ LOG_ERR ("cacheValue is NULL" );
274
+ return UMF_RESULT_ERROR_INVALID_ARGUMENT ;
275
+ }
276
+
277
+ // get pointer to the entry
278
+ ipc_handle_cache_entry_t * entry =
279
+ (ipc_handle_cache_entry_t * )((char * )cacheValue -
280
+ offsetof(ipc_handle_cache_entry_t , value ));
281
+ // decrement the ref count
282
+ utils_atomic_decrement (& entry -> ref_count );
283
+
284
+ return UMF_RESULT_SUCCESS ;
285
+ }
0 commit comments