Example #1
0
File: _malloc.c Project: lkyunl4/C
void* allocate_block(void * ptr, size_t size){
	size_t ex_size = BLOCK_SIZE(ptr);
	set_used(ptr);
	set_block_size(ptr, size);
	set_next_block(ptr, ex_size, size);

	return ptr + sizeof(header);
}
Example #2
0
void *flx_collector_t::create_empty_array(
  flx::gc::generic::gc_shape_t *shape,
  unsigned long count
)
{
  //fprintf(stderr,"create empty array length %ld\n",count);
  void *p = allocate(shape,count);
  set_used (p, 0); // make sure to override default 1 slot usage
  //fprintf(stderr,"Array at %p, used = %ld, max=%ld\n",p,get_used(p), get_count(p));
  return p;
}
Example #3
0
/* Find the least recently block (to evict soon) */
cache_block* find_LRU(address_t addr, student_cache_t *cache) {
    int index = decode_index(addr,cache);
    cache_LRU *lru;
    cache_block *block;
    /* Find the LRU block from the LRUs array */
    lru = cache->LRUs + index; 
    block = get_block_from_way(index,cache->ways + lru->way_index);
    /* Set as used */
    set_used(cache,index,lru->way_index);
    return block;

}
Example #4
0
 // pos: next position of word to search for.
 // (i, j): current position.
 bool search(string &word, int pos, vector<int> &used, 
           vector<vector<char> > &board, int i, int j) {
     if (pos == word.length()) return true;
     
     int rows = board.size();
     int cols = board[0].size();
     
     set_used(used, cols, i, j, 1);
     
     // left
     if (j > 0) { // not on left edge
         if (word[pos] == board[i][j-1] &&
             ! get_used(used, cols, i, j-1) )
             if (search(word, pos + 1, used, board, i, j-1)) return true;
     }
     
     // right
     if (j < cols - 1) { // not on right edge
         if (word[pos] == board[i][j+1] &&
             ! get_used(used, cols, i, j+1) )
             if (search(word, pos + 1, used, board, i, j+1)) return true;
     }
     
     // up
     if (i > 0) { // not top row.
         if (word[pos] == board[i-1][j] &&
             ! get_used(used, cols, i-1, j) )
             if (search(word, pos + 1, used, board, i-1, j)) return true;
     }
     
     // down
     if (i < rows - 1) {
         if (word[pos] == board[i+1][j] &&
             ! get_used(used, cols, i+1, j) )
             if (search(word, pos + 1, used, board, i+1, j)) return true;            
     }
     
     set_used(used, cols, i, j, 0);
     return false;
 }
Example #5
0
void* get_block(size_t size) {
    item_p i = matched;
    item_p* last = &matched;
    while (i != NULL) {
        if (is_free(i) && block_size(i) >= size) {
            split_item(i, size);
            set_used(i);
            freeblks--;
            usdblks++;
            freemem -= block_size(i);
            usdmem += block_size(i);
            return (void*)i;
        }
        last = &(i->next);
        i = i->next;
    }
    i = get_unmatched_item();
    if (i == NULL)
        return NULL;
    size_t page_size = page_align(size);
    void* data = morecore(page_size);
    if (data == NULL) {
        i->next = unmatched;
        unmatched = i;
        return NULL;
    }
    arena += page_size;
    freemem += page_size;
    usdblks++;
    *last = i;
    i->data = data;
    i->size = page_size;
    split_item(i, size);
    set_used(i);
    freemem -= block_size(i);
    usdmem += block_size(i);
    return (void*)i;
}
Example #6
0
/* Find an invalid block to replace */
cache_block* find_invalid(address_t addr, student_cache_t *cache) {
    int i;
    int index = decode_index(addr,cache);
    cache_block *block;
    /* Loop through the cache line and return a block if it's invalid */
    for(i=0; i<cache->ways_size; i++) {
        block = get_block_from_way(index,cache->ways+i);
        if(!block->valid) {
            set_used(cache,index,i); 
            return block;
        } 
    }
    /* Nothing found so return NULL */
    return NULL;
}
Example #7
0
int
fdir_mkdir (fbuf *the_fbuf,
	    AFSFid dot,
	    AFSFid dot_dot)
{
    DirPage0 *page0;
    DirPage1 *page;
    int ind;
    int i;
    int tmp;
    int ret;

    ret = create_new_page (&page, the_fbuf);
    if (ret)
	return ret;

    page0 = (DirPage0 *)fbuf_buf(the_fbuf);
    memset (&page0->dheader, 0, sizeof(page0->dheader));
    tmp = ENTRIESPERPAGE
	- (sizeof(PageHeader) + sizeof(DirHeader)) / sizeof(DirEntry);
    page0->header.pg_freecount = tmp;
    page0->dheader.map[0]      = tmp;
    page0->header.pg_pgcount   = htons(1);

    for (i = 0; i < 13; ++i)
	set_used (page, i);

    assert (page0->dheader.hash[hashentry(".")] == 0);

    ind = add_to_page (page0, page, 0, ".", dot, 0);

    assert (ind >= 0);

    page0->dheader.hash[hashentry(".")] = htons(ind + 1);

    assert (page0->dheader.hash[hashentry("..")] == 0);

    ind = add_to_page (page0, page, 0, "..", dot_dot, 0);

    assert (ind >= 0);

    page0->dheader.hash[hashentry("..")] = htons(ind + 1);

    return 0;
}
Example #8
0
// add the symbol to the grid and to the usage arrays
void grid_write(latin_grid square, coord position, int symbol) {
  
  // TODO: clean this up
  
  int old_symbol = CELL(square, position->row, position->col);
  
  if (square == square_A) {

    // old value is available
    set_used(&row_used_A, old_symbol, false);
    
    // new value is not
    set_used(&row_used_A, symbol, true);
    
    // diffs
    if (position->col > 0) {
      diff_used_A[row_difference(square, position, old_symbol)]--;
      diff_used_A[row_difference(square, position, symbol)]++;
    }
    
    // cyclic equivalences
    if (cyclic_equivalent_to_1(old_symbol, square->size)) {
      equivalent_to_1_used--;
    }
    if (cyclic_equivalent_to_1(symbol, square->size)) {
      equivalent_to_1_used++;
    }
  } else {
    // old value is available
    set_used(&row_used_B, old_symbol, false);
    
    // new value is not
    set_used(&row_used_B, symbol, true);
    
    // diffs
    set_used(&diff_used_orthogonal, orthogonal_difference(position, old_symbol), false);
    set_used(&diff_used_orthogonal, orthogonal_difference(position, symbol), true);
    if (position->col > 0) {
      diff_used_B[row_difference(square, position, old_symbol)]--;
      diff_used_B[row_difference(square, position, symbol)]++;
      diff_used_diagonal_AB[diagonal_AB_difference(position, old_symbol)]--;
      diff_used_diagonal_AB[diagonal_AB_difference(position, symbol)]++;
    }
    if (position->col < square->size - 1) {
      diff_used_diagonal_BA[diagonal_BA_difference(position, old_symbol)]--;
      diff_used_diagonal_BA[diagonal_BA_difference(position, symbol)]++;
    }
  }
  CELL(square, position->row, position->col) = symbol;
}
Example #9
0
/* Look for a matching tag in the cache */
cache_block* find_block(address_t addr, student_cache_t *cache, int write) {
    int i;
    int index = decode_index(addr,cache);
    int tag = decode_tag(addr,cache);
    cache_block *block;
    /* Loop through the cache line */
    for(i=0; i<cache->ways_size; i++) {
       block = get_block_from_way(index,cache->ways+i);
       if(block->valid && block->tag == tag) {
           /* Found a valid match */
           if(!write || cache->WP==WBWA) {
           /* Only set used if a read or a WBWA write */
               set_used(cache,index,i); 
           }
           return block;
       }
    }
    /* No match so return NULL */
    return NULL;
}
Example #10
0
static int
create_new_page (DirPage1 **ret_page,
		 fbuf *the_fbuf)
{
    int ret;
    DirPage1 *page1;
    size_t len = fbuf_len(the_fbuf);

    ret = fbuf_truncate (the_fbuf, len + AFSDIR_PAGESIZE);
    if (ret)
	return ret;

    page1 = (DirPage1 *)((char *)fbuf_buf(the_fbuf) + len);
    page1->header.pg_pgcount   = htons(0);
    page1->header.pg_tag       = htons(AFSDIRMAGIC);
    page1->header.pg_freecount = ENTRIESPERPAGE - 1;
    memset (page1->header.pg_bitmap, 0, sizeof(page1->header.pg_bitmap));
    set_used (page1, 0);
    *ret_page = page1;

    return 0;
}
Example #11
0
/*
 * Allocate array inside dsm_segment
 */
void
alloc_dsm_array(DsmArray *arr, size_t entry_size, size_t length)
{
	int		i = 0;
	int		size_requested = entry_size * length;
	int min_pos = 0;
	int max_pos = 0;
	bool found = false;
	bool collecting_blocks = false;
	size_t offset = -1;
	size_t total_length = 0;
	BlockHeaderPtr header;
	char *ptr = dsm_segment_address(segment);

	for (i = dsm_cfg->first_free; i<dsm_cfg->blocks_count; )
	{
		header = (BlockHeaderPtr) &ptr[i * dsm_cfg->block_size];
		if (is_free(header))
		{
			if (!collecting_blocks)
			{
				offset = i * dsm_cfg->block_size;
				total_length = dsm_cfg->block_size - sizeof(BlockHeader);
				min_pos = i;
				collecting_blocks = true;
			}
			else
			{
				total_length += dsm_cfg->block_size;
			}
			i++;
		}
		else
		{
			collecting_blocks = false;
			offset = 0;
			total_length = 0;
			i += get_length(header);
		}

		if (total_length >= size_requested)
		{
			max_pos = i-1;
			found = true;
			break;
		}
	}

	/*
	 * If dsm segment size is not enough then resize it (or allocate bigger
	 * for segment SysV and Windows, not implemented yet)
	 */
	if (!found)
	{
		size_t new_blocks_count = dsm_cfg->blocks_count * 2;

		dsm_resize(segment, new_blocks_count * dsm_cfg->block_size);
		init_dsm_table(dsm_cfg->block_size, dsm_cfg->blocks_count, new_blocks_count);
		dsm_cfg->blocks_count = new_blocks_count;

		/* try again */
		return alloc_dsm_array(arr, entry_size, length);
	}

	/* look up for first free block */
	if (dsm_cfg->first_free == min_pos)
	{
		for (; i<dsm_cfg->blocks_count; )
		{
			header = (BlockHeaderPtr) &ptr[i * dsm_cfg->block_size];
			if (is_free(header))
			{
				dsm_cfg->first_free = i;
				break;
			}
			else
			{
				i += get_length(header);
			}
		}
	}

	/* if we found enough of space */
	if (total_length >= size_requested)
	{
		header = (BlockHeaderPtr) &ptr[min_pos * dsm_cfg->block_size];
		*header = set_used(header);
		*header = set_length(header, max_pos - min_pos + 1);

		arr->offset = offset;
		arr->length = length;
	}
}