static void add_block(int index) { header *h = find_block(pool_block(index), pool_size(index)); insert(block_pool[index], h); pool_total += pool_block(index); free_ptr[index] = h->h_memory; free_count[index] = pool_count(index); }
int student_read(address_t addr, student_cache_t *cache, stat_t *stats){ cache_block *block; /* Increment stats */ stats->accesses++; stats->reads++; /* Check if in cache */ block = find_block(addr,cache,0); if(block == NULL) { /* It's a miss so look for an invalid spot */ block = find_invalid(addr,cache); if(block == NULL) { /* No invalids so evict the LRU */ block = find_LRU(addr,cache); /* Save it if's dirty and we're in WB */ if(block->dirty && cache->WP == WBWA) { transfer_to_memory(cache,stats); } } /* Set up the block for the new address */ block->dirty = 0; block->valid = 1; block->tag = decode_tag(addr,cache); /* Count the miss and transfer block from memory */ stats->read_miss++; transfer_from_memory(cache,stats); return 0; } /* It's a hit so count and return */ stats->read_hit++; return 1; }
int radeon_mem_free( DRM_IOCTL_ARGS ) { DRM_DEVICE; drm_radeon_private_t *dev_priv = dev->dev_private; drm_radeon_mem_free_t memfree; struct mem_block *block, **heap; if ( !dev_priv ) { DRM_ERROR( "%s called with no initialization\n", __FUNCTION__ ); return DRM_ERR(EINVAL); } DRM_COPY_FROM_USER_IOCTL( memfree, (drm_radeon_mem_free_t *)data, sizeof(memfree) ); heap = get_heap( dev_priv, memfree.region ); if (!heap || !*heap) return DRM_ERR(EFAULT); block = find_block( *heap, memfree.region_offset ); if (!block) return DRM_ERR(EFAULT); if (block->pid != DRM_CURRENTPID) return DRM_ERR(EPERM); free_block( block ); return 0; }
void *mm_malloc(size_t size) { if (size > 0) { if (!start) { start = request_space(size); if (!start) { return NULL; } start->free = 0; last = start; return start+1; } else { struct metadata *block; block = find_block(size); if (!block) { block = request_space(size); if (!block) { return NULL; } } else { if ((block->size - size) >= (sizeof(struct metadata))) { split(block, size); } } block->free = 0; void *ptr = (void *)block + sizeof(struct metadata); memset(ptr, 0, size); return ptr; } } return NULL; }
/* Free the block whose first page (aka block leader) is specified * by "page_idx". return 1 on success and 0 otherwise. */ int free_block(page_idx_t page_idx) { (void)remove_alloc_block(page_idx); lm_page_t* pi = alloc_info->page_info; lm_page_t* page = pi + page_idx; int order = page->order; ASSERT (find_block(page_idx, order, NULL) == 0); /* Consolidate adjacent buddies */ int page_num = alloc_info->page_num; page_id_t page_id = page_idx_to_id(page_idx); int min_page_id = alloc_info->idx_2_id_adj; while (1) { page_id_t buddy_id = page_id ^ (1<<order); if (buddy_id < min_page_id) break; page_idx_t buddy_idx = page_id_to_idx(buddy_id); if (buddy_idx >= page_num || pi[buddy_idx].order != order || !is_page_leader(pi + buddy_idx) || is_allocated_blk(pi + buddy_idx)) { break; } remove_free_block(buddy_idx, order, 0); reset_page_leader(alloc_info->page_info + buddy_idx); page_id = page_id < buddy_id ? page_id : buddy_id; order++; } add_free_block(page_id_to_idx(page_id), order); return 1; }
//allocate memory void *malloc(size_t size){ t_block newBlock, last; size_t newSize = align8(size); if(base){ last = (t_block) base; newBlock = find_block(&last, newSize); if(newBlock){ //can we split if((newBlock->size - newSize) >= (OFFSET + PTR_SIZE)){ split_block(newBlock, newSize); } newBlock->block_free = false; } else { //Can't find block, try extending the heap newBlock = extend_heap(last, newSize); if(!newBlock){ //cannot extend heap return 0x0; } } } else { //first call newBlock = extend_heap(0x0, newSize); if(!newBlock){ //failed to extend return 0x0; } base = newBlock; } return newBlock->data; }
int smsa_vwrite( SMSA_VIRTUAL_ADDRESS addr, uint32_t len, unsigned char *buf ) { // First check if the length is 0 // Write nothing and return if so if(len == 0) { return 0; } uint32_t drum, block, block_orig, offset; int i; int read_bytes = 0; unsigned char temp[SMSA_BLOCK_SIZE];; drum = find_drum(addr); block = find_block(addr); block_orig = block; offset = addr & 0xff; // Loop to iterate through blocks do { // Check if drum is in bounds if( (drum > SMSA_DISK_ARRAY_SIZE) || (drum < 0) ) { logMessage( LOG_ERROR_LEVEL, "The drum is out of bounds [%d]\n", drum ); return -1; } // Check if block is in bounds if( (block > SMSA_MAX_BLOCK_ID) || (block < 0) ) { logMessage( LOG_ERROR_LEVEL, "The block is out of bounds [%d]\n", block ); return -1; } // Check if original block if( block == block_orig ) { i = offset; smsa_operation( SEEK_DRUM, NULL ); // Seek drum smsa_operation( SEEK_BLOCK, NULL ); // Seek block } else { i = 0; } if( (block == SMSA_BLOCK_SIZE) ) { block =0; drum++; smsa_operation( SEEK_DRUM, NULL ); // Seek drum } smsa_operation( DISK_READ, temp); // Read disk block++; // Loop to write into buffer do { temp[i] = buf[read_bytes]; read_bytes++; i++; } while( (i<SMSA_BLOCK_SIZE) && (read_bytes<len) ); smsa_operation( SEEK_PREVIOUS_BLOCK, NULL ); // Seek previous block smsa_operation( DISK_WRITE, temp ); // Write disk } while(read_bytes<len); return 0; }
/*----------------------------------------------------------------------------- * free *----------------------------------------------------------------------------*/ void m_free_( void *memptr, char *file, int lineno ) { MemBlock *block = NULL; Bool result; init_module(); /* if input is NULL, do nothing */ if ( memptr == NULL ) return; block = find_block( memptr, file, lineno ); check( block, "memory free at %s:%d failed", file, lineno ); /* delete from list to avoid recursion atexit() if overflow */ DL_DELETE(g_mem_blocks, block ); /* check fences */ result = check_fences( block ); check( result, "memory free at %s:%d failed", file, lineno ); error: /* delete memory blocks */ if ( block ) { if ( block->destructor ) /* destroy children */ block->destructor( memptr ); /* user destructor */ free( block ); /* destroy itself */ } }
int i915_mem_free(struct drm_device *dev, void *data, struct drm_file *file_priv) { struct drm_i915_private *dev_priv = dev->dev_private; struct drm_i915_mem_free *memfree = data; struct mem_block *block, **heap; if (!dev_priv) { DRM_ERROR("%s called with no initialization\n", __FUNCTION__); return -EINVAL; } heap = get_heap(dev_priv, memfree->region); if (!heap || !*heap) return -EFAULT; block = find_block(*heap, memfree->region_offset); if (!block) return -EFAULT; if (block->file_priv != file_priv) return -EPERM; mark_block(dev, block, 0); free_block(block); return 0; }
int blocks_collide(BLOCK_LIST *blocks, BLOCK_LIST *other_blocks, int h_change, int v_change, int h_wrap, int v_wrap) { int hpos; int vpos; if (blocks != NULL) { hpos = ((BLOCK *)(blocks->data))->hpos + h_change; if (hpos >= h_wrap) { hpos -= h_wrap; } else if (hpos < 0) { hpos += h_wrap; } vpos = ((BLOCK *)(blocks->data))->vpos + v_change; if (vpos >= v_wrap) { vpos -= v_wrap; } else if (vpos < 0) { vpos += v_wrap; } if (find_block(other_blocks, hpos, vpos) != NULL) { return TRUE; } return blocks_collide(blocks->next, other_blocks, h_change, v_change, h_wrap, v_wrap); } return FALSE; }
/* BPLUS find and add key functions */ static int find_ix(ENTRY *pe, IX_DESC *pix, int find) { int level, off, ret; RECPOS ads; //ENTRY found; pci = pix; ads = 0L; level = ret = 0; while (ads != NULLREC) { pci->level = level; retrieve_block(level, ads); /*printf("block_ptr = %p.\n", block_ptr);*/ if (find_block(pe, &off) == 0) ret = 1; if (ret && find) break; if (off == -1) { ads = block_ptr->p0; } else { ads = ENT_ADR(block_ptr, off)->idxptr; } CO(level++) = off; } return ret; }
void* mm_malloc(size_t size) { #ifdef MM_USE_STUBS return calloc(1, size); #else s_block_ptr block, last; size_t s; s = align4(size); if(foot){ last = foot; block = find_block(last, s); if(block){ if((block->size - s) >= (BLOCK_SIZE + 4)) split_block(block, s); block->free = 0; } else{ block = extend_heap(last, s); if(!block) return NULL; } } else{ block = extend_heap(NULL, s); if(!block) return NULL; foot = block; } return block->data; //#error Not implemented. #endif }
void mm_free(void* ptr) { if (!ptr) { return; } s_block_ptr target = find_block(ptr); target->free = 1; return; }
std::vector<unsigned int> RangeParser::operator()(const std::string &s) { range = s; check_range(); ans.clear(); while (!range.empty()) { find_block(); parse_block(); } return ans; }
vector<unsigned int> RP::operator()(const string &s){ range=s; check_range(); ans.clear(); while(range.size()!=0){ find_block(); parse_block(); } return ans; }
void *m_destroy_atexit_( void *memptr, char *file, int lineno ) { MemBlock *block; block = find_block( memptr, file, lineno ); check( block, "m_destroy_atexit at %s:%d failed", file, lineno ); block->flags.destroy_atextit = TRUE; error: return memptr; }
void *m_set_in_collection_( void *memptr, Bool in_collection, char *file, int lineno ) { MemBlock *block; block = find_block( memptr, file, lineno ); check( block, "m_%s_in_collection at %s:%d failed", in_collection ? "set" : "clear", file, lineno ); block->flags.in_collection = in_collection; error: return memptr; }
CFGBlock* add_block(int ip, bool loop=false) { CFGBlock* blk = find_block(ip); if(blk) return blk; blk = new CFGBlock(ip, loop); // Inherit the current exception handler blk->set_exception_handler(current_->exception_handler()); set_block(ip, blk); return blk; }
/*----------------------------------------------------------------------------- * Destructors *----------------------------------------------------------------------------*/ void *m_set_destructor_( void *memptr, destructor_t destructor, char *file, int lineno ) { MemBlock *block; block = find_block( memptr, file, lineno ); check( block, "m_set_destructor at %s:%d failed", file, lineno ); block->destructor = destructor; error: return memptr; }
int par_exist(char *block, char *name) { Block *bp; Par *pp; if (!now_open) ath_error("par_exist: No open parameter file\n"); if (block == NULL) ath_error("par_exist: no block name specified\n"); if (name == NULL) ath_error("par_exist: no par name specified\n"); bp = find_block(block); if (bp == NULL) return 0; pp = find_par(bp,name); return (pp == NULL ? 0 : 1); }
//-------------------------------------------------------------- static void find_block_boundaries(file_desc_t* fdesc, bblklist_t* blist, uint32_t startaddr) { avr_instr_t instr; uint32_t currAddr; succ_t succ; // Set the file pointer to the start of first instruction obj_file_seek(fdesc, startaddr, SEEK_SET); // Insert the first boundary insert_blkbnd(blist, startaddr); #if defined(DBGMODE) && defined(DBG_FIND_BLK_BND) printf("\n"); #endif currAddr = startaddr; succ.branchflag = 0; succ.fallflag = 0; // Start reading the instructions - First pass through the file while (obj_file_read(fdesc, &instr, sizeof(avr_instr_t), 1) != 0) { #ifdef BBIG_ENDIAN instr.rawVal = Flip_int16(instr.rawVal); #endif #if defined(DBGMODE) && defined(DBG_FIND_BLK_BND) printf("0x%5x: %4x ", currAddr, instr.rawVal); decode_avr_instr_word(&instr); #endif find_succ(fdesc, currAddr, &instr, &succ); if (succ.branchflag == 1) { insert_blkbnd(blist, succ.branchaddr); } if (succ.fallflag == 1) { insert_blkbnd(blist, succ.falladdr); } currAddr += 2; #if defined(DBGMODE) && defined(DBG_FIND_BLK_BND) printf("\n"); #endif } // Insert a dummy boundary at the last address insert_blkbnd(blist, currAddr); // Basic Block with StartAddress blist->blk_st = find_block(blist, startaddr); return; }
static char *par_getsl(char *block, char *name) { Block *bp; Par *pp; if (!now_open) ath_error("par_gets: No open parameter file\n"); if (block == NULL) ath_error("par_gets: no block name specified\n"); if (name == NULL) ath_error("par_gets: no par name specified\n"); bp = find_block(block); if (bp == NULL) ath_error("par_gets: Block \"%s\" not found\n",block); pp = find_par(bp,name); if (pp == NULL) ath_error("par_gets: Par \"%s\" not found in Block \"%s\"\n",name,block); return pp->value; }
odp_shm_t odp_shm_lookup(const char *name) { uint32_t i; odp_shm_t hdl; odp_spinlock_lock(&odp_shm_tbl->lock); if (find_block(name, &i) == 0) { odp_spinlock_unlock(&odp_shm_tbl->lock); return ODP_SHM_INVALID; } hdl = odp_shm_tbl->block[i].hdl; odp_spinlock_unlock(&odp_shm_tbl->lock); return hdl; }
static t_block *use_free_block(size_t size, t_block *last) { t_block *block; last = g_base; block = find_block(&last, size); if (block) { if (block->size - size >= (SIZE + sizeof(int))) split_memory(block, size); block->free = 0; } else block = extend_memory(last, size); return (block); }
/*----------------------------------------------------------------------------- * realloc *----------------------------------------------------------------------------*/ void *m_realloc_( void *memptr, size_t size, char *file, int lineno ) { MemBlock *block, *next_block; Bool result; init_module(); /* if input is NULL, behave as malloc */ if ( memptr == NULL ) return m_malloc_( size, file, lineno ); /* find the block */ block = find_block( memptr, file, lineno ); check( block, "memory realloc (%u bytes) failed at %s:%d", size, file, lineno ); /* delete from list as realloc may move block */ next_block = block->next; /* remember position */ DL_DELETE(g_mem_blocks, block); /* check fences */ result = check_fences( block ); check( result, "memory realloc (%u bytes) failed at %s:%d", size, file, lineno ); /* reallocate and create new end fence */ block = realloc( block, BLOCK_SIZE( size ) ); check( block, "memory realloc (%u bytes) failed at %s:%d", size, file, lineno ); /* update block */ block->client_size = size; block->file = file; block->lineno = lineno; /* fill end fence */ memset( END_FENCE_PTR( block ), FENCE_SIGN, FENCE_SIZE ); /* add to list at the same location as before */ if (next_block == NULL) DL_APPEND(g_mem_blocks, block); else DL_PREPEND_ELEM(g_mem_blocks, next_block, block); return CLIENT_PTR( block ); error: return NULL; }
void *gc_alloc(value *desc, unsigned size, value *sp) { unsigned alloc_size; word *p = NULL; header *h; if (debug['z']) gc_collect(sp); size = round_up(size+4, BYTES_PER_WORD); if (size <= MAX_SMALL_BYTES) { /* Try to allocate from the appropriate pool */ unsigned index = pool_map(size); alloc_size = pool_size(index); ASSERT(alloc_size >= size); if (free_count[index] == 0) { while (pool_total + pool_block(index) > heap_size && free_count[index] == 0) scavenge(sp, pool_block(index)); if (free_count[index] == 0) add_block(index); } p = (word *) free_ptr[index]; free_ptr[index] += alloc_size; free_count[index]--; } else { /* Allocate whole pages */ alloc_size = round_up(size, GC_PAGESIZE); while (pool_total + alloc_size > heap_size) scavenge(sp, alloc_size); h = find_block(alloc_size, alloc_size); insert(block_pool[n_sizes], h); pool_total += alloc_size; p = (word *) h->h_memory; } alloc_since_gc += alloc_size; DEBUG_PRINT('c', ("[Alloc %d %p]", size, p)); *p = (word) desc; return p+1; }
int filesys_init(void) { int i; unsigned int root_inode; unsigned long block_num; for(i=2;i<twoMB;i++) { file.short_ptr[i] = 0; } file.short_ptr[0] = 7931; //Number of blocks available to store data initially file.short_ptr[1] = initial_inode_cnt; //Number of Inode structures available initially inode_ptr = file.char_ptr + 256; filesys_ptr = file.char_ptr + bitmap_limit; root_inode = find_inode(); block_num = find_block(); inode_ptr[root_inode].type = '0'; inode_ptr[root_inode].size = 0; inode_ptr[root_inode].location[0] = block_num; for(i=1;i<10;i++) inode_ptr[root_inode].location[i] = 0; for(i=0;i<1024;i++) { file_des[i].open = 0; file_des[i].inode = -1; file_des[i].offset = 0; write_pos[i] = -16; } open_files_count = 0; log[root_inode].parent[0] = "\0"; log[root_inode].dir[0] = "/"; log[root_inode].inode = 0; files_num = 1; file_system_begin(); return 1; }
int student_write(address_t addr, student_cache_t *cache, stat_t *stats){ cache_block *block; /* Increment stats */ stats->accesses++; stats->writes++; /* Send to memory if in WTWNA mode */ if(cache->WP == WTWNA) { transfer_to_memory(cache,stats); } block = find_block(addr,cache,1); if(block == NULL) { /* It's a miss so quit if in WTWNA */ if(cache->WP == WTWNA) { return 0; } /* Look for an invalid spot in WBWA */ block = find_invalid(addr,cache); if(block == NULL) { /* No invalids so evict the LRU */ block = find_LRU(addr,cache); /* Save it if's dirty */ if(block->dirty) { transfer_to_memory(cache,stats); } } /* Set up the block for the new address */ block->valid = 1; block->dirty = 1; block->tag = decode_tag(addr,cache); /* Count the miss and transfer block from memory */ stats->write_miss++; transfer_from_memory(cache,stats); return 0; } /* It's a hit so count it and mark as dirty */ block->dirty = 1; stats->write_hit++; return 1; }
void *malloc(size_t size) { t_block ptr, last; size_t temp_s = size; if (first_block) { /*First find a block*/ printf("mark first_block is not NULL now.\n"); last = first_block; ptr = find_block(&last, temp_s); /*split if yes*/ if (ptr) { printf("mark start of split_block"); if((ptr->size - temp_s) >= BLOCK_SIZE + 1) split_block(ptr, temp_s); ptr->is_free = FREE_N; } else { /*No fitting block, extend it*/ printf("====extend block because theres no fit block====\n"); ptr = extend_block(last, temp_s); if(!ptr) { fprintf(stderr, "extend block failed because of{ %s }\n", strerror(errno)); return NULL; } } } else { /*first time to allocate memory*/ ptr = extend_block(NULL, temp_s); if(!ptr) return NULL; first_block = ptr; } printf("====end of page ====\n"); return (void*)ptr->data; }
void find_backward_gotos() { VMMethod::Iterator iter(stream_, stream_size_); while(!iter.end()) { switch(iter.op()) { case InstructionSequence::insn_goto: case InstructionSequence::insn_goto_if_true: case InstructionSequence::insn_goto_if_false: if(iter.operand1() < iter.position()) { if(!find_block(iter.operand1())) { CFGBlock* blk = new CFGBlock(iter.operand1(), true); set_block(iter.operand1(), blk); } } break; } iter.inc(); } }