-
Notifications
You must be signed in to change notification settings - Fork 0
/
fast_slob.h
164 lines (132 loc) · 3.96 KB
/
fast_slob.h
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
* fast_slob.h: a simple but fast linked-list based buffer allocator
* Copyright (c) 2012 Rong Shen <rong1129@gmail.com>
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifndef _FAST_SLOB_H
#define _FAST_SLOB_H
#include <linux/kernel.h>
#include <linux/spinlock.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
#include <linux/cache.h>
#define MIN_ALLOC_SIZE sizeof(char *)
struct fast_slob {
spinlock_t lock;
size_t bucket_size;
int min_alloc_size;
int max_alloc_size;
int alloc_size_shift;
int num_buckets;
char *start, *end;
char *buckets[0];
};
static inline struct fast_slob *fast_slob_create(size_t size, int max_alloc_size, int alloc_size_shift, int num_buckets)
{
struct fast_slob *slob;
size_t bucket_size, min_alloc_size;
char *start, *end, *buf;
int i;
if (max_alloc_size < MIN_ALLOC_SIZE || alloc_size_shift < 1 || num_buckets < 1)
return NULL;
bucket_size = L1_CACHE_ALIGN(size / num_buckets);
if (bucket_size * num_buckets > size)
bucket_size = (size / num_buckets) & ~(L1_CACHE_BYTES - 1);
if (bucket_size < max_alloc_size)
return NULL;
min_alloc_size = max_alloc_size >> (alloc_size_shift * (num_buckets - 1));
if (min_alloc_size < MIN_ALLOC_SIZE)
return NULL;
slob = kmalloc(sizeof(*slob) + num_buckets * sizeof(char *), GFP_KERNEL);
if (!slob)
return NULL;
slob->start = vmalloc_user(size);
if (!slob->start) {
kfree(slob);
return NULL;
}
slob->end = slob->start + size;
slob->bucket_size = bucket_size;
slob->min_alloc_size = min_alloc_size;
slob->max_alloc_size = max_alloc_size;
slob->alloc_size_shift = alloc_size_shift;
slob->num_buckets = num_buckets;
for (i = 0; i < num_buckets; i++) {
start = slob->start + i * bucket_size;
end = start + bucket_size;
slob->buckets[i] = start;
while (start < end) {
buf = start;
start += ALIGN(min_alloc_size, MIN_ALLOC_SIZE);
*(char **)buf = (start < end) ? start : NULL;
}
min_alloc_size <<= alloc_size_shift;
}
spin_lock_init(&slob->lock);
return slob;
}
static inline void fast_slob_destroy(struct fast_slob *slob)
{
vfree(slob->start);
kfree(slob);
}
static inline void *fast_slob_alloc(struct fast_slob *slob, size_t size)
{
size_t alloc_size = slob->min_alloc_size;
char *p;
int i;
spin_lock(&slob->lock);
for (i = 0; i < slob->num_buckets; i++) {
if (alloc_size >= size && slob->buckets[i]) {
p = slob->buckets[i];
slob->buckets[i] = *(char **)p;
spin_unlock(&slob->lock);
return p;
}
alloc_size <<= slob->alloc_size_shift;
}
spin_unlock(&slob->lock);
return NULL;
}
static inline int fast_slob_bucket(struct fast_slob *slob, void *p)
{
size_t off, alloc_size;
int idx;
if ((char *)p < slob->start || (char *)p >= slob->end)
return -1;
off = (char *)p - slob->start;
idx = off / slob->bucket_size;
alloc_size = slob->min_alloc_size << (idx * slob->alloc_size_shift);
if ((off - idx * slob->bucket_size) % ALIGN(alloc_size, MIN_ALLOC_SIZE))
return -1;
return idx;
}
static inline void _fast_slob_free(struct fast_slob *slob, int idx, void *p)
{
spin_lock(&slob->lock);
*(char **)p = slob->buckets[idx];
slob->buckets[idx] = p;
spin_unlock(&slob->lock);
}
static inline void fast_slob_free(struct fast_slob *slob, void *p)
{
int idx;
if ((idx = fast_slob_bucket(slob, p)) < 0) {
printk(KERN_WARNING "fast_slob: try to free an invalid buffer with address %p\n", p);
return;
}
_fast_slob_free(slob, idx, p);
}
#endif /* _FAST_SLOB_H */