-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitmap.c
57 lines (53 loc) · 1.66 KB
/
bitmap.c
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
// Macros used in the bitset algorithms.
#define INDEX_FROM_BIT(a) (a/(8*4))
#define OFFSET_FROM_BIT(a) (a%(8*4))
// Static function to set a bit in the frames bitset
static void set_frame(uint32_t frame_addr)
{
uint32_t frame = frame_addr/0x1000;
uint32_t idx = INDEX_FROM_BIT(frame);
uint32_t off = OFFSET_FROM_BIT(frame);
frames[idx] |= (0x1 << off);
}
// Static function to clear a bit in the frames bitset
static void clear_frame(uint32_t frame_addr)
{
uint32_t frame = frame_addr/0x1000;
uint32_t idx = INDEX_FROM_BIT(frame);
uint32_t off = OFFSET_FROM_BIT(frame);
frames[idx] &= ~(0x1 << off);
}
// Static function to test if a bit is set.
static uint32_t test_frame(uint32_t frame_addr)
{
uint32_t frame = frame_addr/0x1000;
uint32_t idx = INDEX_FROM_BIT(frame);
uint32_t off = OFFSET_FROM_BIT(frame);
return (frames[idx] & (0x1 << off));
}
// Static function to find the first free frame.
static uint32_t first_free_frame()
{
uint32_t i, j;
for (i = 0; i < INDEX_FROM_BIT(total_frames); i++) // loop over every bitmask byte
{
//fb_write("\n");
//fb_write_hex(frames[i]);
if (frames[i] != 0xFFFFFFFF) // nothing free, exit early.
{
// at least one bit is free here.
for (j = 0; j < 32; j++)
{
uint32_t toTest = 0x1 << j;
if ( !(frames[i]&toTest) )
{
// return the index of the free frame
// e.g. the nth frame out of 4096 frames (16mb) is free
//fb_write("first free frame idx: ");
//fb_write_dec(i*4*8+j);
return i*4*8+j;
}
}
}
}
}