-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitmap.c
More file actions
64 lines (51 loc) · 1.19 KB
/
bitmap.c
File metadata and controls
64 lines (51 loc) · 1.19 KB
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
#include <aria/slab.h>
#include <aria/bitmap.h>
#include <aria/string.h>
#include <aria/debug.h>
int bitmap_alloc(struct bitmap *bitmap, int *ret)
{
if (bitmap == NULL || ret == NULL)
RETURN_ERROR;
if (bitmap->data == NULL)
goto init;
for (int i = 0; i < bitmap->size; i++) {
if (BIT_TEST(bitmap->data, i) == 0) {
BIT_SET(bitmap->data, i);
*ret = i;
return 0;
}
}
init:
if (bitmap->data == NULL || bitmap->resizable) {
if (!bitmap->size)
bitmap->size = 2;
else
bitmap->size *= 2;
bitmap->data = realloc(bitmap->data, DIV_ROUNDUP(bitmap->size, 8));
if (bitmap->data == NULL)
RETURN_ERROR;
return bitmap_alloc(bitmap, ret);
}
return -1;
}
int bitmap_free(struct bitmap *bitmap, int index)
{
if (bitmap == NULL)
RETURN_ERROR;
if (index > bitmap->size)
return 0;
BIT_CLEAR(bitmap->data, index);
return 0;
}
int bitmap_dup(struct bitmap *bitmap, struct bitmap *dest)
{
if (bitmap == NULL || dest == NULL)
RETURN_ERROR;
dest->size = bitmap->size;
dest->resizable = bitmap->resizable;
dest->data = alloc(DIV_ROUNDUP(bitmap->size, 8));
if (dest->data == NULL)
RETURN_ERROR;
memcpy8(dest->data, bitmap->data, DIV_ROUNDUP(bitmap->size, 8));
return 0;
}