aboutsummaryrefslogtreecommitdiff
path: root/include/buddy.h
blob: a8414f5ea92cf2fe40145e5c6c2450f9df33cf58 (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
// SPDX-License-Identifier: Apache-2.0
/*
 * Simple power-of-two buddy allocation mechanism.
 *
 * Copyright 2016 IBM Corp.
 */

#ifndef __BUDDY_H
#define __BUDDY_H

#include "bitmap.h"

#define BUDDY_MAX_ORDER	30

struct buddy {
	/* max_order is both the height of the tree - 1 and the ^2 of the
	 * size of the lowest level.
	 *
	 * So if we have 512k elements, max_order is 19, which gives us
	 * a 20 levels tree.
	 *
	 * The max supported order is 30 for now. We can increase that
	 * later if really needed but the performance is going to be
	 * already pretty bad if we go near that limit.
	 */
	unsigned int max_order;

	/* For each order, we keep track of how many free modes we
	 * have there to speed up searches.
	 */
	unsigned int freecounts[BUDDY_MAX_ORDER + 1];
	bitmap_elem_t     map[];
};

extern struct buddy *buddy_create(unsigned int max_order);
extern void buddy_destroy(struct buddy *b);

extern int buddy_alloc(struct buddy *b, unsigned int order);
extern bool buddy_reserve(struct buddy *b, unsigned int index, unsigned int order);
extern void buddy_free(struct buddy *b, unsigned int index, unsigned int order);
extern void buddy_reset(struct buddy *b);

#endif /* __BUDDY_H */