1 // SPDX-License-Identifier: GPL-2.0
3 #include "alloc_cache.h"
5 void io_alloc_cache_free(struct io_alloc_cache
*cache
,
6 void (*free
)(const void *))
13 while ((entry
= io_alloc_cache_get(cache
)) != NULL
)
16 kvfree(cache
->entries
);
17 cache
->entries
= NULL
;
20 /* returns false if the cache was initialized properly */
21 bool io_alloc_cache_init(struct io_alloc_cache
*cache
,
22 unsigned max_nr
, unsigned int size
,
23 unsigned int init_bytes
)
25 cache
->entries
= kvmalloc_array(max_nr
, sizeof(void *), GFP_KERNEL
);
30 cache
->max_cached
= max_nr
;
31 cache
->elem_size
= size
;
32 cache
->init_clear
= init_bytes
;
36 void *io_cache_alloc_new(struct io_alloc_cache
*cache
, gfp_t gfp
)
40 obj
= kmalloc(cache
->elem_size
, gfp
);
41 if (obj
&& cache
->init_clear
)
42 memset(obj
, 0, cache
->init_clear
);