2022-07-07 23:16:20 +03:00
# ifndef IOU_ALLOC_CACHE_H
# define IOU_ALLOC_CACHE_H
2022-07-07 23:20:54 +03:00
/*
* Don ' t allow the cache to grow beyond this size .
*/
2024-03-17 03:23:44 +03:00
# define IO_ALLOC_CACHE_MAX 128
2022-07-07 23:20:54 +03:00
static inline bool io_alloc_cache_put ( struct io_alloc_cache * cache ,
2024-03-21 00:19:44 +03:00
void * entry )
2022-07-07 23:16:20 +03:00
{
2023-04-04 15:39:57 +03:00
if ( cache - > nr_cached < cache - > max_cached ) {
2024-03-21 00:19:44 +03:00
if ( ! kasan_mempool_poison_object ( entry ) )
return false ;
cache - > entries [ cache - > nr_cached + + ] = entry ;
2022-07-07 23:20:54 +03:00
return true ;
}
return false ;
2022-07-07 23:16:20 +03:00
}
2024-03-21 00:19:44 +03:00
static inline void * io_alloc_cache_get ( struct io_alloc_cache * cache )
2022-07-07 23:16:20 +03:00
{
2024-03-21 00:19:44 +03:00
if ( cache - > nr_cached ) {
void * entry = cache - > entries [ - - cache - > nr_cached ] ;
2022-07-07 23:16:20 +03:00
2023-12-20 01:29:05 +03:00
kasan_mempool_unpoison_object ( entry , cache - > elem_size ) ;
2023-02-23 19:43:52 +03:00
return entry ;
2022-07-07 23:16:20 +03:00
}
return NULL ;
}
2024-03-21 00:19:44 +03:00
/* returns false if the cache was initialized properly */
static inline bool io_alloc_cache_init ( struct io_alloc_cache * cache ,
2023-04-04 15:39:57 +03:00
unsigned max_nr , size_t size )
2022-07-07 23:16:20 +03:00
{
2024-03-21 00:19:44 +03:00
cache - > entries = kvmalloc_array ( max_nr , sizeof ( void * ) , GFP_KERNEL ) ;
if ( cache - > entries ) {
cache - > nr_cached = 0 ;
cache - > max_cached = max_nr ;
cache - > elem_size = size ;
return false ;
}
return true ;
2022-07-07 23:16:20 +03:00
}
static inline void io_alloc_cache_free ( struct io_alloc_cache * cache ,
2024-03-21 00:19:44 +03:00
void ( * free ) ( const void * ) )
2022-07-07 23:16:20 +03:00
{
2024-03-21 00:19:44 +03:00
void * entry ;
if ( ! cache - > entries )
return ;
2022-07-07 23:16:20 +03:00
2024-03-21 00:19:44 +03:00
while ( ( entry = io_alloc_cache_get ( cache ) ) ! = NULL )
2023-02-23 19:43:52 +03:00
free ( entry ) ;
2024-03-21 00:19:44 +03:00
kvfree ( cache - > entries ) ;
cache - > entries = NULL ;
2022-07-07 23:16:20 +03:00
}
# endif