summaryrefslogtreecommitdiff
path: root/lwis_allocator.h
blob: b72f0bad3bd015e9de756600ae9ab887c4e99e0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* SPDX-License-Identifier: GPL-2.0 */
/*
 * LWIS Recycling Memory Allocator
 *
 * Copyright (c) 2021 Google Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#ifndef LWIS_ALLOCATOR_H_
#define LWIS_ALLOCATOR_H_

#include <linux/mutex.h>
#include "lwis_commands.h"
#include "lwis_device.h"

struct lwis_allocator_block {
	int type;
	void *ptr;
	struct lwis_allocator_block *next;
	struct lwis_allocator_block *prev;
	struct hlist_node node;
};

struct lwis_allocator_block_pool {
	char name[LWIS_MAX_NAME_STRING_LEN];
	struct lwis_allocator_block *free;
	uint32_t free_count;
	struct lwis_allocator_block *in_use;
	uint32_t in_use_count;
};

struct lwis_allocator_block_mgr {
	spinlock_t lock;
	struct lwis_allocator_block_pool pool_4k;
	struct lwis_allocator_block_pool pool_8k;
	struct lwis_allocator_block_pool pool_16k;
	struct lwis_allocator_block_pool pool_32k;
	struct lwis_allocator_block_pool pool_64k;
	struct lwis_allocator_block_pool pool_128k;
	struct lwis_allocator_block_pool pool_256k;
	struct lwis_allocator_block_pool pool_512k;
	struct lwis_allocator_block_pool pool_large;
	/* Hash table of allocated buffers keyed by allocated addresses */
	DECLARE_HASHTABLE(allocated_blocks, BUFFER_HASH_BITS);
	int ref_count;
};

/*
 *  lwis_allocator_init: Initialize the recycling memory allocator
 */
int lwis_allocator_init(struct lwis_device *lwis_dev);

/*
 *  lwis_allocator_release: Release the recycling memory allocator
 *  and its resources
 */
void lwis_allocator_release(struct lwis_device *lwis_dev);

/*
 *  lwis_allocator_allocate: Allocate a block from the recycling memory allocator
 */
void *lwis_allocator_allocate(struct lwis_device *lwis_dev, size_t size, gfp_t gfp_flags);

/*
 *  lwis_allocator_free: Free a block to the recycling memory allocator
 */
void lwis_allocator_free(struct lwis_device *lwis_dev, void *ptr);

#endif /* LWIS_ALLOCATOR_H_ */