Generic lock-free kmalloc cache implementation
Conflicts: kernel/mem.c
This commit is contained in:
57
kernel/mem.c
57
kernel/mem.c
@@ -2611,3 +2611,60 @@ int ihk_mc_get_mem_user_page(void *arg0, page_table_t pt, pte_t *ptep, void *pga
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Generic lockless kmalloc cache.
|
||||
*/
|
||||
void *kmalloc_cache_alloc(struct kmalloc_cache_header *cache,
|
||||
size_t size)
|
||||
{
|
||||
struct kmalloc_cache_header *first, *next;
|
||||
|
||||
retry:
|
||||
next = NULL;
|
||||
first = cache->next;
|
||||
|
||||
if (first) {
|
||||
next = first->next;
|
||||
|
||||
if (!__sync_bool_compare_and_swap(&cache->next,
|
||||
first, next)) {
|
||||
goto retry;
|
||||
}
|
||||
}
|
||||
else {
|
||||
int i;
|
||||
kprintf("%s: cache empty, allocating ...\n", __FUNCTION__);
|
||||
for (i = 0; i < 100; ++i) {
|
||||
first = (struct kmalloc_cache_header *)
|
||||
kmalloc(size, IHK_MC_AP_NOWAIT);
|
||||
|
||||
if (!first) {
|
||||
kprintf("%s: ERROR: allocating cache element\n", __FUNCTION__);
|
||||
continue;
|
||||
}
|
||||
|
||||
kmalloc_cache_free(cache, first);
|
||||
}
|
||||
|
||||
goto retry;
|
||||
}
|
||||
|
||||
return (void *)first;
|
||||
}
|
||||
|
||||
void kmalloc_cache_free(struct kmalloc_cache_header *cache, void *elem)
|
||||
{
|
||||
struct kmalloc_cache_header *current = NULL;
|
||||
struct kmalloc_cache_header *new =
|
||||
(struct kmalloc_cache_header *)elem;
|
||||
|
||||
retry:
|
||||
current = cache->next;
|
||||
new->next = current;
|
||||
|
||||
if (!__sync_bool_compare_and_swap(&cache->next, current, new)) {
|
||||
goto retry;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user