summaryrefslogtreecommitdiffstats
path: root/src/fixedmap.h
blob: 076628dd41274564a89cd7301e2e98bc445b3240 (plain) (blame)
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
/*
 * Copyright (c) 2019 Matt Dunwoodie <ncon@noconroy.net>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#ifndef __FIXEDMAP_H__
#define __FIXEDMAP_H__

#include <sys/types.h>
#include <sys/rwlock.h>

struct fixed_map {
	struct rwlock lock;
	uint8_t bits;
	struct map_item {
		enum {
			FM_ITEM_EMPTY = 0,
			FM_ITEM_RESERVED,
			FM_ITEM_FILLED,
		} state;
		uint32_t key;
		void *value;
	} *map;
};

void fm_init(struct fixed_map *);
void fm_destroy(struct fixed_map *);
void fm_resize(struct fixed_map *, size_t);
uint32_t fm_reserve(struct fixed_map *);
void fm_set(struct fixed_map *, uint32_t, void *);
uint32_t fm_insert(struct fixed_map *, void *);
void *fm_lookup(struct fixed_map *, uint32_t);
void fm_remove(struct fixed_map *, uint32_t);

#define FM_SIZE(fm) (1 << (fm)->bits)
#define FM_ITEM(fm, k) ((fm)->map[(k) & ((fm)->bits - 1)])

void
fm_init(struct fixed_map *fm)
{
	rw_init(&fm->lock, "fixed_map");
	fm->bits = 0;
	fm->map = NULL;
}

void
fm_destroy(struct fixed_map *fm)
{
	free(fm->map, M_DEVBUF, 0);
}

void
fm_resize(struct fixed_map *fm, size_t size)
{
	struct map_item *old_map;
	size_t old_size, i;

	if (size <= FM_SIZE(fm))
		return;

	rw_enter_write(&fm->lock);
	old_size = FM_SIZE(fm);
	old_map = fm->map;

	for(fm->bits = 1; size >>= 1; fm->bits++);
	fm->map = mallocarray(FM_SIZE(fm), sizeof(*fm->map), M_DEVBUF, M_WAITOK | M_ZERO);

	if (old_map != NULL) {
		for (i = 0; i < old_size; i++)
			FM_ITEM(fm, old_map[i].key) = old_map[i];
		free(old_map, M_DEVBUF, 0);
	}
	rw_exit_write(&fm->lock);
}

uint32_t
fm_reserve(struct fixed_map *fm)
{
	size_t i;
	struct map_item *item = NULL;

	rw_enter_write(&fm->lock);
	for (i = 0; i < FM_SIZE(fm); i++) {
		if (fm->map[i].state == FM_ITEM_EMPTY) {
			item = &fm->map[i];
			break;
		}
	}

	if (item == NULL)
		panic("unable to find space in fixed map");

	item->key = (arc4random() << fm->bits) + i;
	item->state = FM_ITEM_RESERVED;
	rw_exit_write(&fm->lock);

	return item->key;
}

void
fm_set(struct fixed_map *fm, uint32_t k, void *v)
{
	rw_enter_write(&fm->lock);
	if (FM_ITEM(fm, k).key == k ) {
		FM_ITEM(fm, k).value = v;
		FM_ITEM(fm, k).state = FM_ITEM_FILLED;
	}
	rw_exit_write(&fm->lock);
}

uint32_t
fm_insert(struct fixed_map *fm, void *v)
{
	uint32_t k = fm_reserve(fm);
	fm_set(fm, k, v);
	return k;
}

void *
fm_lookup(struct fixed_map *fm, uint32_t k)
{
	void *v;
	rw_enter_read(&fm->lock);
	v = FM_ITEM(fm, k).key == k ? FM_ITEM(fm, k).value : NULL;
	rw_exit_read(&fm->lock);
	return v;
}

void
fm_remove(struct fixed_map *fm, uint32_t k)
{
	rw_enter_write(&fm->lock);
	if (FM_ITEM(fm, k).key == k) {
		FM_ITEM(fm, k).value = NULL;
		FM_ITEM(fm, k).state = FM_ITEM_EMPTY;
	}
	rw_exit_write(&fm->lock);
}

#endif /* __FIXEDMAP_H__ */