int main(void) { bitfield_t * bitz = bitfield_create(100); bitfield_set(bitz, 17); bitfield_set(bitz, 75); bitfield_set(bitz, 90); printf("count is %d\n", (int)bitfield_count(bitz)); bitfield_clear(bitz, 17); for(int i = 0; i < 100; ++i) { if(bitfield_query(bitz, i)) printf("%d: 1\n", i); else printf("%d: 0\n", i); } printf("----\n"); bitfield_set_all(bitz); printf("count is %d\n", (int)bitfield_count(bitz)); bitfield_clear_all(bitz); printf("count is %d\n", (int)bitfield_count(bitz)); bitfield_destroy(bitz); return 0; }
/** * Frees a swap entry at a given offset. * @param offset */ void swap_free(int offset) { assert(offset % PAGESIZE == 0); int bit_number = offset / PAGESIZE; assert(bit_number <= MAX_SWAP_ENTRIES); assert(bitfield_get(swap_bitfield, bit_number)); bitfield_set(swap_bitfield, bit_number, 0); }
/** * Initializes datastructures required for swapping. * This is called by pager_init. */ void swap_init() { TAILQ_INIT(&active_pages_head); TAILQ_INIT(&swapping_pages_head); // initialize the bitfield for the swap file swap_bitfield = malloc(MAX_SWAP_ENTRIES / 8); for(int i=0; i < MAX_SWAP_ENTRIES; i++) { bitfield_set(swap_bitfield, i, 0); } }
/** * Frees a previously allocated frame. * Frame is pushed on the stack and the corresponding bit in the bit field is * set to 0. * * @param frame start address of frame to be freed */ void frame_free(L4_Word_t frame) { assert(is_valid_frame_address(frame)); if(bitfield_get(frame)) { frame_stack_add(frame); bitfield_set(frame, 0); } else { dprintf(0, "WARNING: %s: Trying to free frame which is already free.\n", __FUNCTION__); } }
/** * Finds an empty place in the swap file. * * @return Start offset of free space in swap file or -1 if * swap is full. */ static int allocate_swap_entry(void) { for(int i=0; i<MAX_SWAP_ENTRIES; i++) { if(!bitfield_get(swap_bitfield, i)) { bitfield_set(swap_bitfield, i, 1); return i*PAGESIZE; } } return -1; }
/** * Allocates a frame (PAGESIZE bytes) in physical memory. * The top element is removed from the stack and the corresponding * bit in the bit field is set to 1. * * @return start address of the frame or NULL in case there are no more frames left */ L4_Word_t frame_alloc(void) { if(stack_count >= 1) { L4_Word_t frame = frame_stack_remove(); bitfield_set(frame, 1); memset((void*)frame, 0, PAGESIZE); // make sure we don't leak data to other processes return frame; } else { dprintf(1, "WARNING: %s: Ran out of physical memory :-(.\n", __FUNCTION__); return (L4_Word_t) NULL; } }
/** * Initialize the frame table. * * @param low lowest physical memory address * @param high highest physical memory address */ void frame_init(L4_Word_t low, L4_Word_t high) { start = low; end = high; L4_Word_t memsize = end - start; // preconditions assert(low < high); assert(memsize % PAGESIZE == 0); // our code is based on that assumption L4_Word_t frame_count = memsize / PAGESIZE; L4_Word_t frame_table_size = frame_count * sizeof(frame_t); L4_Word_t bit_field_size = (frame_count / 8) + 1; dprintf(2, "Physical Memory starts at address: %d\n", start); dprintf(2, "Physical Memory ends at address: %d\n", end); dprintf(2, "Memory Size: %d bytes\n", memsize); dprintf(2, "Frame Count: %d frames\n", frame_count); dprintf(2, "Frame List Structure size: %d bytes\n", sizeof(frame_t)); dprintf(2, "Frame Table Size: %d bytes\n", frame_table_size); dprintf(2, "Bit field size: %d bytes\n", bit_field_size); frame_stack_start = (frame_t*) malloc(frame_table_size); // this is never freed but it's ok assert(frame_stack_start != NULL); stack_count = 0; //bitfield_start = (char*) (frame_stack_start + frame_table_size); bitfield_start = (char*) malloc(bit_field_size); // this is never freed but it's ok assert(bitfield_start != NULL); dprintf(2, "Physical Frames will start at address: %d\n", start); L4_Word_t frame_iter; // for better debugging we add the frames in reverse order on the stack // so calls to frame_alloc will return with the first frames first for(frame_iter = end-PAGESIZE; frame_iter >= start; frame_iter -= PAGESIZE) { frame_stack_add(frame_iter); bitfield_set(frame_iter, 0); } dprintf(2, "Stack count now is: %d\n", stack_count); #ifdef SWAP_TEST stack_count = SWAP_FRAMES_LIMIT; dprintf(0, "WARNING: running with artificially decreased frame number: %d\n", stack_count); #endif }
static void data_write(struct eval_context *context, struct field *field, int64_t val) { int64_t oldval = bitfield_set(context->data, field->is_signed, field->offset, field->nbits, val); if (oldval != val) context->modified = 1; }