/* * PURPOSE: This function creates a new block store device by allocate memory to it. * And will display error and terminate if there's any error happens in any parts. * INPUTS: * No input. * RETURN: * If no errors during the allocation then it return a new block store device * else return NULL * **/ block_store_t *block_store_create() { block_store_t *bs = malloc(sizeof(block_store_t)); if (bs) { bs->fbm = bitmap_create(BLOCK_COUNT); if (bs->fbm) { bs->dbm = bitmap_create(BLOCK_COUNT); if (bs->dbm) { // Eh, calloc, why not (technically a security risk if we don't) bs->data_blocks = calloc(BLOCK_SIZE, BLOCK_COUNT - FBM_SIZE); if (bs->data_blocks) { for (size_t idx = 0; idx < FBM_SIZE; ++idx) { bitmap_set(bs->fbm, idx); bitmap_set(bs->dbm, idx); } block_store_errno = BS_OK; return bs; } bitmap_destroy(bs->dbm); } bitmap_destroy(bs->fbm); } free(bs); } block_store_errno = BS_MEMORY; return NULL; }
int cmd_checkfs(struct super_block *sb, struct context *c) { struct bitmap *i_freemap, *b_freemap; int ret; if (c->nargs != 1) { return -EINVAL; } ret = bitmap_create(BLOCK_SIZE * INODE_FREEMAP_SIZE * BITS_PER_WORD, &i_freemap); if (ret < 0) return ret; ret = bitmap_create(BLOCK_SIZE * BLOCK_FREEMAP_SIZE * BITS_PER_WORD, &b_freemap); if (ret < 0) return ret; testfs_checkfs(sb, i_freemap, b_freemap, 0); if (!bitmap_equal(sb->inode_freemap, i_freemap)) { printf("inode freemap is not consistent\n"); } if (!bitmap_equal(sb->block_freemap, b_freemap)) { printf("block freemap is not consistent\n"); } printf("nr of allocated inodes = %d\n", bitmap_nr_allocated(sb->inode_freemap)); printf("nr of allocated blocks = %d\n", bitmap_nr_allocated(sb->block_freemap)); return 0; }
/** *Initialize the page swapping related data-structures */ void initialize_swap_table () { swap_partition = block_get_role (BLOCK_SWAP); lock_init (&swap_lock); // Check if no swap partition is available then swapping is disabled if (swap_partition == NULL) { // meaning that swapping is disabled but programs should still run // until there is enough physical memory avaiable swap_sectors_btmp = bitmap_create (0); } else { // Create bitmap for total number of page swap sectors // (i.e. number of sectors rounded to per-page sectors) in swap partition // Total number of swap sectors = (total block-size of swap partition) // /(per page swap sectors) swap_sectors_btmp = bitmap_create ( block_size (swap_partition) / NUM_OF_PER_PAGE_SECTORS); } // If unable to create swap sectors bitmap, meaning kernel memory is full - PANIL :( if (swap_sectors_btmp == NULL) { PANIC("Swap Sectors bitmap creation failed!!! :("); } // Reset all bits in this bitmap - to zero bitmap_set_all (swap_sectors_btmp, 0); }
block_store_t *block_store_create() { // While assembly-wise it shouldn't change, it looks cleaner, // even if we have extra free/destruct calls on the error path // (but then again, who cares about the length of the error path?) // This will probably have to be encapsulated eventually, like bitmap's creations // (also because import is a MEEEESSSSSSSSS) // (Actually, with bitmap_overlay now, we may just be able to call create whenever) // (Although file-backed stuff will throw a wrench in it, but it's VERY different (just an fd?)) // (file-backing is a headache for some other day) block_store_t *bs = calloc(sizeof(block_store_t), 1); if (bs) { if ((bs->data_blocks = calloc(BLOCK_SIZE, BLOCK_COUNT)) && // Eh, calloc, why not (technically a security risk if we don't) (bs->fbm = bitmap_overlay(BLOCK_COUNT, bs->data_blocks)) && (bs->dbm = bitmap_create(BLOCK_COUNT))) { for (size_t idx = 0; idx < FBM_BLOCK_COUNT; ++idx) { bitmap_set(bs->fbm, idx); } bitmap_format(bs->dbm, 0xFF); // we have never synced, mark all as changed bs->flags = DIRTY; bs->fd = -1; bs_errno = BS_OK; return bs; } free(bs->data_blocks); bitmap_destroy(bs->dbm); bitmap_destroy(bs->fbm); free(bs); } bs_errno = BS_MEMORY; return NULL; }
static inline void ptest_bitmap_bit_set_p(uint32 count) { s_bitmap_t *bitmap; native_wide_t min, max; min = 0xda1; max = 0xdeac; PERFORMANCE_TEST_BEGIN(bitmap_bit_set_p); bitmap = bitmap_create(min, max); PERFORMANCE_TEST_CHECKPOINT; while (count--) { bitmap_bit_set_p(bitmap, min); bitmap_bit_set_p(bitmap, max); } PERFORMANCE_TEST_ENDPOINT; bitmap_destroy(&bitmap); PERFORMANCE_TEST_RESULT(bitmap_bit_set_p); }
/* * Initialize the coremap and bitmap to describe the page table. We steal memory * from RAM to store these structures. Initialize each of the entry of the page * table with the page address with respect to the base address of the coremap * stored in RAM. */ void init_coremap() { int i; u_int32_t ram_max = mips_ramsize(); u_int32_t ram_user_base = ram_stealmem(0); //size of the coremap in PAGE_SIZE unit coremap_size = (ram_max-ram_user_base-PAGE_SIZE)/PAGE_SIZE; //bitmap to keep track of the availability state of the coremap memory core_memmap = bitmap_create(coremap_size); //allocate the coremap into memory by stealing some memory from RAM coremap = (struct _PTE*)kmalloc(coremap_size * sizeof(struct _PTE)); //base address for the coremap in the ram, it starts from where the swaparea //ended coremap_base = ram_stealmem(0); //set each of the page address for(i = 0; i < coremap_size; i++) { coremap[i].paddr = (coremap_base + (i * PAGE_SIZE)); coremap[i].status = PAGE_FREE; coremap[i].pid = 0; } }
//Initialization function to allocate teh swap table and setup the BLOCK_SWAP //space on disk to swap memory in and out. void swap_init (void) { block_sector_t num_sectors; st = malloc(sizeof(struct swap_table)); if(st == NULL) PANIC ("Failed to malloc swap table during swap initialization"); lock_init(&st->swap_table_lock); //Get the block that contains the swap space st->swap_block = block_get_role(BLOCK_SWAP); if(st->swap_block != NULL) num_sectors = block_size(st->swap_block); else { PANIC ("Failed to get block swap space during swap initialization"); } //bitmap_create declared in bitmap.h. Allocates correct size. st->map = bitmap_create((size_t)num_sectors); if(st->map == NULL) { free(st); PANIC ("Failed to create swap bitmap during swap initialization"); } }
void swap_init(void) { swap_disk = disk_get(1,1); swap_bitmap = bitmap_create(disk_size(swap_disk)/SECTOR_PER_PAGE); bitmap_set_all(swap_bitmap, 0); }
void swap_init(void) { swap_block = block_get_role(BLOCK_SWAP); swap_map = bitmap_create(block_size(swap_block)/SECTORS_PER_PAGE); bitmap_set_all(swap_map,SWAP_FREE); lock_init(&swap_lock); }
struct file * file_open(char *name) { int result; struct file *f = malloc(sizeof(struct file)); if (f == NULL) { goto done; } f->f_fd = open(name, O_CREAT | O_RDWR, S_IRWXU); if (f->f_fd == -1) { goto cleanup_malloc; } f->f_size = io_size(f->f_fd); unsigned bitmapbytes = FILE_BITMAP_PAGES * PAGESIZE; if (f->f_size == 0) { // if we are creating the file for the first time, allocate the // first page for the bitmap, and mark the page as taken f->f_page_bitmap = bitmap_create(bitmapbytes * 8); if (f->f_page_bitmap == NULL) { goto cleanup_fd; } for (unsigned i = 0; i < FILE_BITMAP_PAGES; i++) { bitmap_mark(f->f_page_bitmap, i); } result = file_sync_bitmap(f); if (result) { bitmap_destroy(f->f_page_bitmap); goto cleanup_fd; } f->f_size += bitmapbytes; } else { // if we are initing from an already existing file, read the first // page and init the bitmap using that page unsigned char buf[bitmapbytes]; bzero(buf, bitmapbytes); result = io_read(f->f_fd, buf, bitmapbytes); if (result) { goto cleanup_fd; } f->f_page_bitmap = bitmap_init(bitmapbytes * 8, buf); if (f->f_page_bitmap == NULL) { goto cleanup_fd; } } // seek back to beginning on opening result = lseek(f->f_fd, 0, SEEK_SET); f->f_last_alloc_page = FILE_BITMAP_PAGES - 1; if (result) { goto cleanup_fd; } goto done; cleanup_fd: assert(close(f->f_fd) == 0); cleanup_malloc: free(f); f = NULL; done: return f; }
void swap_init (void) { swap_disk = disk_get (1, 1); swap_slot = bitmap_create ((size_t)disk_size (swap_disk)); lock_init (&swap_lock); lock_init (&swap_bitmap_lock); }
void init_frame_table(size_t frame_cnt) { //Perform hash table init, struct inits here fr_table = malloc (sizeof(struct frame_table)); lock_init(&fr_table->lock); hash_init(&fr_table->ft, frame_hash, frame_less, NULL); //bitmap_init(fr_table->bm_frames, frame_cnt); fr_table->bm_frames = bitmap_create(frame_cnt); }
/* Initializes the free map. */ void free_map_init (void) { free_map = bitmap_create (disk_size (filesys_disk)); if (free_map == NULL) PANIC ("bitmap creation failed--disk is too large"); bitmap_mark (free_map, FREE_MAP_SECTOR); bitmap_mark (free_map, ROOT_DIR_SECTOR); }
/* Initializes the free map. */ void free_map_init (void) { free_map = bitmap_create (block_size (fs_device)); if (free_map == NULL) PANIC ("bitmap creation failed--file system device is too large"); bitmap_mark (free_map, FREE_MAP_SECTOR); bitmap_mark (free_map, ROOT_DIR_SECTOR); }
/* * swap_bootstrap: Initializes swap information and finishes * bootstrapping the VM so that processes can use it. * * Synchronization: none (runs during boot before anyone else uses VM) */ void swap_bootstrap(size_t pmemsize) { int rv; struct stat st; char path[sizeof(swapfilename)]; off_t minsize; strcpy(path, swapfilename); rv = vfs_open(path, O_RDWR, &swapstore); if (rv) { kprintf("swap: Error %d opening swapfile %s\n", rv, swapfilename); kprintf("swap: Please create swapfile/swapdisk.\n"); panic("swap: Unable to continue.\n"); } minsize = pmemsize*10; VOP_STAT(swapstore, &st); if (st.st_size < minsize) { kprintf("swap: swapfile %s is only %lu bytes.\n", swapfilename, (unsigned long) st.st_size); kprintf("swap: with %lu bytes of physical memory it should " "be at least\n", (unsigned long) pmemsize); kprintf(" %lu bytes (%lu blocks).\n", (unsigned long) minsize, (unsigned long) minsize / 512); kprintf("swap: Please extend it.\n"); panic("swap: Unable to continue.\n"); } kprintf("swap: swapping to %s (%lu bytes; %lu pages)\n", swapfilename, (unsigned long) st.st_size, (unsigned long) st.st_size / PAGE_SIZE); swap_total_pages = st.st_size / PAGE_SIZE; swap_free_pages = swap_total_pages; swap_reserved_pages = 0; swapmap = bitmap_create(st.st_size/PAGE_SIZE); DEBUG(DB_VM, "creating swap map with %d entries\n", st.st_size/PAGE_SIZE); if (swapmap == NULL) { panic("swap: No memory for swap bitmap\n"); } swaplock = lock_create("swaplock"); if (swaplock == NULL) { panic("swap: No memory for swap lock\n"); } /* mark the first page of swap used so we can check for errors */ bitmap_mark(swapmap, 0); swap_free_pages--; }
//Initializes swap table void start_swap (void){ swap_block = block_get_role (BLOCK_SWAP); //Sets the swap role to block if (!swap_block) return; //Returns if swap is not blocked swap_map = bitmap_create( block_size(swap_block) / SECTORS_PER_PAGE ); //Makes a bitmap for swap if (!swap_map) return; //Returns if there is no swap map bitmap_set_all(swap_map, SWAP_FREE); //Calls bitmap with the map and free arguments for swap lock_init(&swap_lock); //Calls the lock intialization with the help of swap lock }
void swap_init (void) { ASSERT (PGSIZE % BLOCK_SECTOR_SIZE == 0); blocks_per_page = DIV_ROUND_UP (PGSIZE, BLOCK_SECTOR_SIZE); swap_block = block_get_role (BLOCK_SWAP); total_swap_size = block_size (swap_block) / blocks_per_page; swap_bitmap = bitmap_create (total_swap_size); lock_init (&swap_lock); }
/* Initializes the inode module. */ void inode_init (void) { list_init (&buffcachelist); list_init (&open_inodes); buffcache = (uint8_t*)malloc(BUFF_CACHE_SIZE); cache_bitmap = bitmap_create(N_BUFFERS); if(INODE_DEBUG) printf("inode_init(): cache_bitmap NULL! N_BUFFERS: %d init_ram_pages %d \n", N_BUFFERS, 4*(init_ram_pages>>4)); bitmap_set_all(cache_bitmap, FREE); }
int sal_osd_init() { CHECK(NULL == g_osd_args, -1, "reinit error, please uninit first.\n"); int ret = -1; unsigned int i = 0; unsigned int j = 0; g_osd_args = (sal_osd_args*)malloc(sizeof(sal_osd_args)); CHECK(NULL != g_osd_args, -1, "malloc %d bytes failed.\n", sizeof(sal_osd_args)); memset(g_osd_args, 0, sizeof(sal_osd_args)); pthread_mutex_init(&g_osd_args->mutex, NULL); g_osd_args->path_hzk = "gbk.dzk"; g_osd_args->path_asc = "asc16.dzk"; g_osd_args->gap = 8; ret = bitmap_create(g_osd_args->path_hzk, 1, g_osd_args->path_asc); CHECK(ret == 0, -1, "Error with %d.\n", ret); g_osd_args->language = 0; // 中文 for (i = OSD_STREAM_TYPE_MAIN; i < OSD_STREAM_TYPE_BUTT; i++) { for (j = OSD_TIME; j < OSD_BUTT; j++) { g_osd_args->item[i][j].enable = (i <= OSD_STREAM_TYPE_SUB) ? g_config.video[i].enable : g_config.jpeg.enable; if (j == OSD_TIME) { g_osd_args->item[i][j].tmformat = 0; g_osd_args->item[i][j].location = OSD_LEFT_TOP; } else if (j == OSD_TITLE) { g_osd_args->item[i][j].location = OSD_RIGHT_BOTTOM; g_osd_args->item[i][j].type = TITLE_ADD_ALL; memset(g_osd_args->item[i][j].buffer, 0, sizeof(g_osd_args->item[i][j].buffer)); strcpy(g_osd_args->item[i][j].buffer, "ipcam"); ret = osd_load_cfg("/etc/title.cfg", i, j); CHECK(ret == 0, -1, "Error with %#x.\n", ret); memset(g_osd_args->item[i][j].buffer_bak, 0, sizeof(g_osd_args->item[i][j].buffer_bak)); strcpy(g_osd_args->item[i][j].buffer_bak, g_osd_args->item[i][j].buffer); } } } g_osd_args->running = 1; ret = pthread_create(&g_osd_args->pid, NULL, osd_proc, NULL); CHECK(ret == 0, -1, "Error with %s.\n", strerror(errno)); return 0; }
void swaptable_init(void) { swap_block = block_get_role(BLOCK_SWAP); lock_init(&swap_lock); block_sector_t swapfile_numsectors = block_size(swap_block); uint32_t swapfile_numbytes = swapfile_numsectors * BLOCK_SECTOR_SIZE; uint32_t swapfile_numpages = swapfile_numbytes / PGSIZE; swapmap = bitmap_create(swapfile_numpages); }
void coremap_init() { int v_size = ( mainbus_ramsize()-(ram_stealmem(0) + PAGE_SIZE) )/PAGE_SIZE; mem_map = bitmap_create(v_size); coremap = (struct page_table_entry*)kmalloc(v_size * sizeof(struct page_table_entry)); coremap_start = ram_stealmem(0); for(int i = 0; i < v_size; i++) { coremap[i].paddr = (coremap_start + (i * PAGE_SIZE)); } coremap_size = v_size; kprintf("COREMAP INIT: %d %d\n",coremap_start,coremap_size); }
void init_swap () { lock_init(&swap_lock); swap = block_get_role(BLOCK_SWAP); //number of sectors each sector is 512kb so we need 8 sectors for one page uint32_t size = block_size (swap); //create a bitmap that represents the swap table, each entry refers to consecutive blocks that represent one page swap_table = bitmap_create (size/SECPP); }
/* Initialize swap device and swap table */ bool swap_init () { sp_device = block_get_role (BLOCK_SWAP); lock_init (&swap_set_lock); /* Bitmap for swap */ swap_free_map = bitmap_create (block_size (sp_device)); if (swap_free_map == NULL) { return false; } return true; }
void coreswap_init() { struct stat temp; VOP_STAT(swap_file, &temp); coreswap_size = temp.st_size/PAGE_SIZE; swap_map = bitmap_create(coreswap_size); coreswap = (struct page_table_entry*)kmalloc(coreswap_size * sizeof(struct page_table_entry)); int coreswap_start=0; kprintf("COREMAP INIT: %d %d\n",coremap_start,coremap_size); for(int i = 0; i < temp.st_size/PAGE_SIZE; i++) { coreswap[i].paddr = (coreswap_start + (i * PAGE_SIZE)); } kprintf("COREMAP INIT: %d %d\n",coremap_start,coremap_size); }
void cache_init (void) { lock_init (&cache_lock); used_slots = bitmap_create (CACHE_SIZE); int i = 0; for (i = 0; i < CACHE_SIZE; ++i) { _cache_slot_init (i); lock_init (&cache[i].cs_lock); cond_init (&cache[i].no_rw_cond); } }
/* Sets up swap. */ void swap_init(void) { //block from the swap space swap_device = block_get_role(BLOCK_SWAP); if (!swap_device) { return; } swap_bitmap = bitmap_create(block_size(swap_device) / SECTORS_PER_PAGE); if (!swap_bitmap) { return; } bitmap_set_all(swap_bitmap, SWAP_FREE); lock_init(&swap_lock); }
int testfs_init_super_block(const char *file, struct super_block **sbp) { struct super_block *sb = malloc(sizeof(struct super_block)); char block[BLOCK_SIZE]; int ret, sock; if (!sb) { return -ENOMEM; } if ((sock = open(file, O_RDWR)) < 0) { return errno; } else if ((sb->dev = fdopen(sock, "r+")) == NULL) { return errno; } read_blocks(sb, block, 0, 1); memcpy(&sb->sb, block, sizeof(struct dsuper_block)); ret = bitmap_create(BLOCK_SIZE * INODE_FREEMAP_SIZE * BITS_PER_WORD, &sb->inode_freemap); if (ret < 0) return ret; read_blocks(sb, bitmap_getdata(sb->inode_freemap), sb->sb.inode_freemap_start, INODE_FREEMAP_SIZE); ret = bitmap_create(BLOCK_SIZE * BLOCK_FREEMAP_SIZE * BITS_PER_WORD, &sb->block_freemap); if (ret < 0) return ret; read_blocks(sb, bitmap_getdata(sb->block_freemap), sb->sb.block_freemap_start, BLOCK_FREEMAP_SIZE); inode_hash_init(); *sbp = sb; return 0; }
void init_swap_structures (void) { /* Initialise the BLOCK_SWAP device */ block_device = block_get_role (BLOCK_SWAP); /* Calculate how big we need to make the bitmap. Everything is initialised to false in the bitmap, which is the default behaviour in the underlying ADT */ size_t size = block_size (block_device) / SECTORS_PER_PAGE; if ((swap_slot_map = bitmap_create (size)) == NULL) PANIC ("Could not allocate memory for swap table"); }
/* initialize swap slot */ void vm_swap_init() { swap_slot = block_get_role(BLOCK_SWAP); if (!swap_slot) PANIC("Swap slot is wrong\n"); size_t bit_cnt = block_size(swap_slot) / SECTORS_PER_PAGE; swap_partition = bitmap_create(bit_cnt); if (!swap_partition) PANIC("Swap partition is wrong\n"); bitmap_set_all(swap_partition, 0); lock_init(&swap_lock); }
/* Swap Table access methods. */ void swap_init () { struct disk *swap = disk_get (1,1); // swap 1:1 channel:device no if (!swap) PANIC ("No swap disk found\n"); disk_sector_t size = disk_size (swap); swap_table = bitmap_create (size); if (!swap_table) PANIC ("Cannot create bitmap for the swap table\n"); lock_init (&swap_table_lock); }