[Krafton Jungle] PintOS 2.0.0
크래프톤 정글 PintOS
 
Loading...
Searching...
No Matches
bitmap.c File Reference
#include "bitmap.h"
#include <debug.h>
#include <limits.h>
#include <round.h>
#include <stdio.h>
#include "threads/malloc.h"
Include dependency graph for bitmap.c:

Classes

struct  bitmap
 

Macros

#define ELEM_BITS   (sizeof (elem_type) * CHAR_BIT)
 

Typedefs

typedef unsigned long elem_type
 

Functions

static size_t elem_idx (size_t bit_idx)
 
static elem_type bit_mask (size_t bit_idx)
 
static size_t elem_cnt (size_t bit_cnt)
 
static size_t byte_cnt (size_t bit_cnt)
 
static elem_type last_mask (const struct bitmap *b)
 
struct bitmapbitmap_create (size_t bit_cnt)
 
struct bitmapbitmap_create_in_buf (size_t bit_cnt, void *block, size_t block_size UNUSED)
 
size_t bitmap_buf_size (size_t bit_cnt)
 
void bitmap_destroy (struct bitmap *b)
 
size_t bitmap_size (const struct bitmap *b)
 
void bitmap_set (struct bitmap *b, size_t idx, bool value)
 
void bitmap_mark (struct bitmap *b, size_t bit_idx)
 
void bitmap_reset (struct bitmap *b, size_t bit_idx)
 
void bitmap_flip (struct bitmap *b, size_t bit_idx)
 
bool bitmap_test (const struct bitmap *b, size_t idx)
 
void bitmap_set_all (struct bitmap *b, bool value)
 
void bitmap_set_multiple (struct bitmap *b, size_t start, size_t cnt, bool value)
 
size_t bitmap_count (const struct bitmap *b, size_t start, size_t cnt, bool value)
 
bool bitmap_contains (const struct bitmap *b, size_t start, size_t cnt, bool value)
 
bool bitmap_any (const struct bitmap *b, size_t start, size_t cnt)
 
bool bitmap_none (const struct bitmap *b, size_t start, size_t cnt)
 
bool bitmap_all (const struct bitmap *b, size_t start, size_t cnt)
 
size_t bitmap_scan (const struct bitmap *b, size_t start, size_t cnt, bool value)
 
size_t bitmap_scan_and_flip (struct bitmap *b, size_t start, size_t cnt, bool value)
 
void bitmap_dump (const struct bitmap *b)
 

Macro Definition Documentation

◆ ELEM_BITS

#define ELEM_BITS   (sizeof (elem_type) * CHAR_BIT)

Typedef Documentation

◆ elem_type

typedef unsigned long elem_type

Function Documentation

◆ bit_mask()

static elem_type bit_mask ( size_t  bit_idx)
inlinestatic
42 {
43 return (elem_type) 1 << (bit_idx % ELEM_BITS);
44}
#define ELEM_BITS
Definition: bitmap.c:22
unsigned long elem_type
Definition: bitmap.c:19
Here is the caller graph for this function:

◆ bitmap_all()

bool bitmap_all ( const struct bitmap b,
size_t  start,
size_t  cnt 
)
260 {
261 return !bitmap_contains (b, start, cnt, false);
262}
bool bitmap_contains(const struct bitmap *b, size_t start, size_t cnt, bool value)
Definition: bitmap.c:230
Here is the call graph for this function:
Here is the caller graph for this function:

◆ bitmap_any()

bool bitmap_any ( const struct bitmap b,
size_t  start,
size_t  cnt 
)
246 {
247 return bitmap_contains (b, start, cnt, true);
248}
Here is the call graph for this function:

◆ bitmap_buf_size()

size_t bitmap_buf_size ( size_t  bit_cnt)
105 {
106 return sizeof (struct bitmap) + byte_cnt (bit_cnt);
107}
static size_t byte_cnt(size_t bit_cnt)
Definition: bitmap.c:54
Definition: bitmap.c:27
size_t bit_cnt
Definition: bitmap.c:28
Here is the call graph for this function:
Here is the caller graph for this function:

◆ bitmap_contains()

bool bitmap_contains ( const struct bitmap b,
size_t  start,
size_t  cnt,
bool  value 
)
230 {
231 size_t i;
232
233 ASSERT (b != NULL);
234 ASSERT (start <= b->bit_cnt);
235 ASSERT (start + cnt <= b->bit_cnt);
236
237 for (i = 0; i < cnt; i++)
238 if (bitmap_test (b, start + i) == value)
239 return true;
240 return false;
241}
bool bitmap_test(const struct bitmap *b, size_t idx)
Definition: bitmap.c:181
#define ASSERT(CONDITION)
Definition: debug.h:30
#define NULL
Definition: stddef.h:4
Here is the call graph for this function:
Here is the caller graph for this function:

◆ bitmap_count()

size_t bitmap_count ( const struct bitmap b,
size_t  start,
size_t  cnt,
bool  value 
)
213 {
214 size_t i, value_cnt;
215
216 ASSERT (b != NULL);
217 ASSERT (start <= b->bit_cnt);
218 ASSERT (start + cnt <= b->bit_cnt);
219
220 value_cnt = 0;
221 for (i = 0; i < cnt; i++)
222 if (bitmap_test (b, start + i) == value)
223 value_cnt++;
224 return value_cnt;
225}
Here is the call graph for this function:

◆ bitmap_create()

struct bitmap * bitmap_create ( size_t  bit_cnt)
73 {
74 struct bitmap *b = malloc (sizeof *b);
75 if (b != NULL) {
76 b->bit_cnt = bit_cnt;
77 b->bits = malloc (byte_cnt (bit_cnt));
78 if (b->bits != NULL || bit_cnt == 0) {
79 bitmap_set_all (b, false);
80 return b;
81 }
82 free (b);
83 }
84 return NULL;
85}
void bitmap_set_all(struct bitmap *b, bool value)
Definition: bitmap.c:191
void * malloc(size_t) __attribute__((malloc))
Definition: malloc.c:85
void free(void *)
Definition: malloc.c:202
elem_type * bits
Definition: bitmap.c:29
Here is the call graph for this function:
Here is the caller graph for this function:

◆ bitmap_create_in_buf()

struct bitmap * bitmap_create_in_buf ( size_t  bit_cnt,
void *  block,
size_t block_size  UNUSED 
)
91 {
92 struct bitmap *b = block;
93
95
96 b->bit_cnt = bit_cnt;
97 b->bits = (elem_type *) (b + 1);
98 bitmap_set_all (b, false);
99 return b;
100}
size_t bitmap_buf_size(size_t bit_cnt)
Definition: bitmap.c:105
static size_t block_size(void *block)
Definition: malloc.c:168
Definition: malloc.c:56
Here is the call graph for this function:

◆ bitmap_destroy()

void bitmap_destroy ( struct bitmap b)
113 {
114 if (b != NULL) {
115 free (b->bits);
116 free (b);
117 }
118}
Here is the call graph for this function:

◆ bitmap_dump()

void bitmap_dump ( const struct bitmap b)
335 {
336 hex_dump (0, b->bits, byte_cnt (b->bit_cnt), false);
337}
void hex_dump(uintptr_t ofs, const void *, size_t size, bool ascii)
Definition: stdio.c:553
Here is the call graph for this function:

◆ bitmap_flip()

void bitmap_flip ( struct bitmap b,
size_t  bit_idx 
)
169 {
170 size_t idx = elem_idx (bit_idx);
171 elem_type mask = bit_mask (bit_idx);
172
173 /* This is equivalent to `b->bits[idx] ^= mask' except that it
174 is guaranteed to be atomic on a uniprocessor machine. See
175 the description of the XOR instruction in [IA32-v2b]. */
176 asm ("lock xorq %1, %0" : "=m" (b->bits[idx]) : "r" (mask) : "cc");
177}
static size_t elem_idx(size_t bit_idx)
Definition: bitmap.c:35
static elem_type bit_mask(size_t bit_idx)
Definition: bitmap.c:42
Here is the call graph for this function:

◆ bitmap_mark()

void bitmap_mark ( struct bitmap b,
size_t  bit_idx 
)
143 {
144 size_t idx = elem_idx (bit_idx);
145 elem_type mask = bit_mask (bit_idx);
146
147 /* This is equivalent to `b->bits[idx] |= mask' except that it
148 is guaranteed to be atomic on a uniprocessor machine. See
149 the description of the OR instruction in [IA32-v2b]. */
150 asm ("lock orq %1, %0" : "=m" (b->bits[idx]) : "r" (mask) : "cc");
151}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ bitmap_none()

bool bitmap_none ( const struct bitmap b,
size_t  start,
size_t  cnt 
)
253 {
254 return !bitmap_contains (b, start, cnt, true);
255}
Here is the call graph for this function:

◆ bitmap_reset()

void bitmap_reset ( struct bitmap b,
size_t  bit_idx 
)
155 {
156 size_t idx = elem_idx (bit_idx);
157 elem_type mask = bit_mask (bit_idx);
158
159 /* This is equivalent to `b->bits[idx] &= ~mask' except that it
160 is guaranteed to be atomic on a uniprocessor machine. See
161 the description of the AND instruction in [IA32-v2a]. */
162 asm ("lock andq %1, %0" : "=m" (b->bits[idx]) : "r" (~mask) : "cc");
163}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ bitmap_scan()

size_t bitmap_scan ( const struct bitmap b,
size_t  start,
size_t  cnt,
bool  value 
)
271 {
272 ASSERT (b != NULL);
273 ASSERT (start <= b->bit_cnt);
274
275 if (cnt <= b->bit_cnt) {
276 size_t last = b->bit_cnt - cnt;
277 size_t i;
278 for (i = start; i <= last; i++)
279 if (!bitmap_contains (b, i, cnt, !value))
280 return i;
281 }
282 return BITMAP_ERROR;
283}
#define BITMAP_ERROR
Definition: bitmap.h:36
Here is the call graph for this function:
Here is the caller graph for this function:

◆ bitmap_scan_and_flip()

size_t bitmap_scan_and_flip ( struct bitmap b,
size_t  start,
size_t  cnt,
bool  value 
)
293 {
294 size_t idx = bitmap_scan (b, start, cnt, value);
295 if (idx != BITMAP_ERROR)
296 bitmap_set_multiple (b, idx, cnt, !value);
297 return idx;
298}
void bitmap_set_multiple(struct bitmap *b, size_t start, size_t cnt, bool value)
Definition: bitmap.c:199
size_t bitmap_scan(const struct bitmap *b, size_t start, size_t cnt, bool value)
Definition: bitmap.c:271
Here is the call graph for this function:
Here is the caller graph for this function:

◆ bitmap_set()

void bitmap_set ( struct bitmap b,
size_t  idx,
bool  value 
)
132 {
133 ASSERT (b != NULL);
134 ASSERT (idx < b->bit_cnt);
135 if (value)
136 bitmap_mark (b, idx);
137 else
138 bitmap_reset (b, idx);
139}
void bitmap_reset(struct bitmap *b, size_t bit_idx)
Definition: bitmap.c:155
void bitmap_mark(struct bitmap *b, size_t bit_idx)
Definition: bitmap.c:143
Here is the call graph for this function:
Here is the caller graph for this function:

◆ bitmap_set_all()

void bitmap_set_all ( struct bitmap b,
bool  value 
)
191 {
192 ASSERT (b != NULL);
193
194 bitmap_set_multiple (b, 0, bitmap_size (b), value);
195}
size_t bitmap_size(const struct bitmap *b)
Definition: bitmap.c:124
Here is the call graph for this function:
Here is the caller graph for this function:

◆ bitmap_set_multiple()

void bitmap_set_multiple ( struct bitmap b,
size_t  start,
size_t  cnt,
bool  value 
)
199 {
200 size_t i;
201
202 ASSERT (b != NULL);
203 ASSERT (start <= b->bit_cnt);
204 ASSERT (start + cnt <= b->bit_cnt);
205
206 for (i = 0; i < cnt; i++)
207 bitmap_set (b, start + i, value);
208}
void bitmap_set(struct bitmap *b, size_t idx, bool value)
Definition: bitmap.c:132
Here is the call graph for this function:
Here is the caller graph for this function:

◆ bitmap_size()

size_t bitmap_size ( const struct bitmap b)
124 {
125 return b->bit_cnt;
126}
Here is the caller graph for this function:

◆ bitmap_test()

bool bitmap_test ( const struct bitmap b,
size_t  idx 
)
181 {
182 ASSERT (b != NULL);
183 ASSERT (idx < b->bit_cnt);
184 return (b->bits[elem_idx (idx)] & bit_mask (idx)) != 0;
185}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ byte_cnt()

static size_t byte_cnt ( size_t  bit_cnt)
inlinestatic
54 {
55 return sizeof (elem_type) * elem_cnt (bit_cnt);
56}
static size_t elem_cnt(size_t bit_cnt)
Definition: bitmap.c:48
Here is the call graph for this function:
Here is the caller graph for this function:

◆ elem_cnt()

static size_t elem_cnt ( size_t  bit_cnt)
inlinestatic
48 {
49 return DIV_ROUND_UP (bit_cnt, ELEM_BITS);
50}
#define DIV_ROUND_UP(X, STEP)
Definition: round.h:10
Here is the caller graph for this function:

◆ elem_idx()

static size_t elem_idx ( size_t  bit_idx)
inlinestatic
35 {
36 return bit_idx / ELEM_BITS;
37}
Here is the caller graph for this function:

◆ last_mask()

static elem_type last_mask ( const struct bitmap b)
inlinestatic
61 {
62 int last_bits = b->bit_cnt % ELEM_BITS;
63 return last_bits ? ((elem_type) 1 << last_bits) - 1 : (elem_type) -1;
64}