예제 #1
0
파일: jv-lang.c 프로젝트: DonCN/haiku
static struct symtab *
get_java_class_symtab (void)
{
  if (class_symtab == NULL)
    {
      struct objfile *objfile = get_dynamics_objfile ();
      struct blockvector *bv;
      struct block *bl;
      class_symtab = allocate_symtab ("<java-classes>", objfile);
      class_symtab->language = language_java;
      bv = (struct blockvector *)
	obstack_alloc (&objfile->objfile_obstack,
		       sizeof (struct blockvector) + sizeof (struct block *));
      BLOCKVECTOR_NBLOCKS (bv) = 1;
      BLOCKVECTOR (class_symtab) = bv;

      /* Allocate dummy STATIC_BLOCK. */
      bl = allocate_block (&objfile->objfile_obstack);
      BLOCK_DICT (bl) = dict_create_linear (&objfile->objfile_obstack,
					    NULL);
      BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK) = bl;

      /* Allocate GLOBAL_BLOCK.  */
      bl = allocate_block (&objfile->objfile_obstack);
      BLOCK_DICT (bl) = dict_create_hashed_expandable ();
      BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK) = bl;
      class_symtab->free_func = free_class_block;
    }
  return class_symtab;
}
예제 #2
0
/* [caml_fl_allocate] does not set the header of the newly allocated block.
   The calling function must do it before any GC function gets called.
   [caml_fl_allocate] returns a head pointer.
*/
char *caml_fl_allocate (mlsize_t wo_sz)
{
  char *cur, *prev;
                                  Assert (sizeof (char *) == sizeof (value));
                                  Assert (fl_prev != NULL);
                                  Assert (wo_sz >= 1);
    /* Search from [fl_prev] to the end of the list. */
  prev = fl_prev;
  cur = Next (prev);
  while (cur != NULL){                             Assert (Is_in_heap (cur));
    if (Wosize_bp (cur) >= wo_sz){
      return allocate_block (Whsize_wosize (wo_sz), prev, cur);
    }
    prev = cur;
    cur = Next (prev);
  }
  fl_last = prev;
    /* Search from the start of the list to [fl_prev]. */
  prev = Fl_head;
  cur = Next (prev);
  while (prev != fl_prev){
    if (Wosize_bp (cur) >= wo_sz){
      return allocate_block (Whsize_wosize (wo_sz), prev, cur);
    }
    prev = cur;
    cur = Next (prev);
  }
    /* No suitable block was found. */
  return NULL;
}
예제 #3
0
int main()
{
    char *buffer1, *buffer2, *buffer3, *buffer4;
	
	//buffer1 = (char *)allocate_block(100);
	//printf("Address of allocated memory is:%d\n", (int)buffer1); 
    buffer1 = (char *)allocate_block(9995);
	printf("Address of allocated memory is:%d\n", (int)buffer1);
    buffer1 = (char *)allocate_block(300);
	printf("Address of allocated memory is:%d\n", (int)buffer1);
    return 0;
}
예제 #4
0
struct chunked_blob *buddy_allocate_blob(size_t size)
{
	int i;
	int orders[CHUNKS_COUNT];
	struct chunked_blob *blob;
	value_size_to_block_sizes(size, orders);

	blob = allocate_block(orders[0]);
	blob->size = size;

	for (i = 1; i < CHUNKS_COUNT && orders[i] >= 0; i++)
		blob->other_chunks[i-1] = allocate_block(orders[i]);

	return blob;
}
예제 #5
0
 /// \effects Allocates a single \concept{concept_node,node} by removing it from the free list.
 /// If the free list is empty, a new memory block will be allocated and put onto it.
 /// The new block size will be \ref next_capacity() big.
 /// \returns A node of size \ref node_size() suitable aligned,
 /// i.e. suitable for any type where <tt>sizeof(T) < node_size()</tt>.
 /// \throws Anything thrown by the used implementation allocator's allocation function if a growth is needed.
 void* allocate_node()
 {
     if (free_list_.empty())
         allocate_block();
     FOONATHAN_MEMORY_ASSERT(!free_list_.empty());
     return free_list_.allocate();
 }
예제 #6
0
static
void *allocate_block(int order)
{
	struct block *buddy;
	struct block *p;

	if (order > MAX_ORDER)
		abort();

	p = blocks_orders[order];
	if (p) {
		assert(p->pprev == blocks_orders + order);
		dequeue_free(p);
		assert(((struct free_block *)p)->order == order);
		goto out;
	}

	if (order == MAX_ORDER)
		return allocate_max_order_block();

	/*
         * struct block *blocks_orders_snapshot[MAX_ORDER+1];
	 * memcpy(blocks_orders_snapshot, blocks_orders, sizeof(blocks_orders));
         */

	p = (struct block *)allocate_block(order+1) - 1;
	buddy = (struct block *)((char *)p + (1 << order));
	buddy->next = USED_MARKER;
	buddy->pprev = 0;
	enqueue_free(buddy, order);
out:
	return p + 1;
}
예제 #7
0
void type_ext2_block_bitmap___allocate (char *command_line)

/*

This function starts allocating block from the current position. Allocating involves setting the correct bits
in the bitmap. This function is a vector version of allocate_block below - We just run on the blocks that
we need to allocate, and call allocate_block for each one.

*/

{
	long entry_num,num=1;
	char *ptr,buffer [80];
	
	ptr=parse_word (command_line,buffer);					/* Get the number of blocks to allocate */
	if (*ptr!=0) {
		ptr=parse_word (ptr,buffer);
		num=atol (buffer);
	}
	
	entry_num=block_bitmap_info.entry_num;
										/* Check for limits */
	if (num > file_system_info.super_block.s_blocks_per_group-entry_num) {
		wprintw (command_win,"Error - There aren't that much blocks in the group\n");	
		refresh_command_win ();return;				
	}
	
	while (num) {								/* And call allocate_block */
		allocate_block (entry_num);					/* for each block */
		num--;entry_num++;
	}
	
	dispatch ("show");							/* Show the result */
}
예제 #8
0
void
mpi_process_group::make_distributed_object()
{
  if (my_block_number() == 0) {
    allocate_block();

    for (std::size_t i = 0; i < impl_->incoming.size(); ++i) {
      if (my_block_number() >= (int)impl_->incoming[i].next_header.size()) {
        impl_->incoming[i].next_header
          .push_back(impl_->incoming[i].headers.begin());
      } else {
        impl_->incoming[i].next_header[my_block_number()] =
          impl_->incoming[i].headers.begin();
      }

#ifdef DEBUG
      if (process_id(*this) == 0) {
        std::cerr << "Allocated tag block " << my_block_number() << std::endl;
      }
#endif
    }
  } else {
    // Clear out the existing triggers
    std::vector<shared_ptr<trigger_base> >()
      .swap(impl_->blocks[my_block_number()]->triggers); 
  }

  // Clear out the receive handler
  impl_->blocks[my_block_number()]->on_receive = 0;
}
예제 #9
0
파일: mm.c 프로젝트: sunpan9209/15213
// Returns a pointer if block of sufficient size is available 
// will allocate a new block if none are free
static void* find_free_block(int index, size_t size){
    REQUIRES(0 <= index && index < NUM_FREE_LISTS);

    void* block;
    void* current;
    int new_index = index; 

    while(new_index < NUM_FREE_LISTS){
    	current = free_lists[new_index];

    	while(current != NULL){
    		if(block_size(current) >= size){
    			// if(new_index > index){
    			// 	block = split_block(new_index);
    			// 	block_mark(block, 0);
    			// 	return block;
    			// }
    			block = current;
    			block_mark(block, 0);
    			remove_block_from_list(new_index, block);
    		}
    		current = block_next(current);
    	}
        new_index++;
    }
    assert(aligned(block));
    block = allocate_block(size);

    ENSURES(block != NULL);
    return block;
}
예제 #10
0
ServerBlock* DiskBackedBlockMap::get_block_for_writing(const BlockId& block_id){
	/** If block is not in block map, allocate space for it
	 * Otherwise, if the block is not in memory, allocate space for it.
	 * Set in_memory and dirty_flag
	 */
	ServerBlock* block = block_map_.block(block_id);
	size_t block_size = sip_tables_.block_size(block_id);
	if (block == NULL) {
		std::stringstream msg;
		msg << "S " << sip_mpi_attr_.global_rank();
		msg << " : getting uninitialized block " << block_id << ".  Creating zero block for writing"<< std::endl;
		SIP_LOG(std::cout << msg.str() << std::flush);
		block = allocate_block(NULL, block_size);
	    block_map_.insert_block(block_id, block);
	} else {
		if (!block->is_in_memory())
			block->allocate_in_memory_data();
	}

	block->set_in_memory();
	block->set_dirty();

	policy_.touch(block_id);

	return block;
}
예제 #11
0
파일: block.c 프로젝트: ejrh/ejrh
static void bitmap_set(FS *fs, LOCATION location, int allocated)
{
    unsigned long int bitmap_num = location / fs->bitmap_size;
    unsigned long int bitmap_offset = location % fs->bitmap_size;
    unsigned long int bitmap_byte = bitmap_offset / 8;
    unsigned long int bitmap_bit = bitmap_offset % 8;
    BLOCK *bitmap;
    unsigned char *byte;
    int prev;

    if (fs->bitmaps[bitmap_num] == 0)
    {
        bitmap = allocate_block(fs, B_BITMAP, 0);
        fs->bitmaps[bitmap_num] = bitmap->location;
        bitmap_set(fs, bitmap->location, 1);
        set_flag(fs->superblock, F_DIRTY);
    }
    else
        bitmap = get_block(fs, fs->bitmaps[bitmap_num], 1);
    
    byte = (unsigned char *) bitmap->buffer + sizeof(struct bitmap_block_header) + bitmap_byte;
    prev = (*byte & (1 << bitmap_bit)) != 0;
    if (allocated)
        *byte = *byte | (1 << bitmap_bit);
    else
        *byte = *byte & ~(1 << bitmap_bit);
    set_flag(bitmap, F_DIRTY);
    
    if (prev == allocated)
        error("Not toggling bit!\n");
    
    //printf("Set bit %ld to %d from %d\n", location, allocated, prev);
}
예제 #12
0
파일: Pool.c 프로젝트: galexander/libamqp
static
amqp_memory_block_t *allocate_new_memory_block(amqp_memory_pool_t *pool, amqp_memory_block_t **head)
{
    amqp_memory_block_t *result = allocate_block(pool);

    int i;
    if ((result->header.next = *head) != 0)
    {
        result->header.next->header.previous = result;
    }
    result->header.previous = 0;
    *head = result;

    result->header.mask.bits = -1UL & pool->allocations_mask;

    for (i = 0; i < pool->allocations_per_block; i++)
    {
        amqp_memory_allocation_t *allocation = allocation_from_index(pool, result, i);
        allocation->header.block = result;
        allocation->header.index = i;
        allocation->header.leading_guard = (uint32_t) leading_mask;
        allocation->data.fragments[pool->object_size_in_fragments] = (size_t) trailing_mask;
    }
    return result;
}
예제 #13
0
 /// \effects Creates it by specifying the size each \concept{concept_node,node} will have,
 /// the initial block size for the arena and the implementation allocator.
 /// If the \c node_size is less than the \c min_node_size, the \c min_node_size will be the actual node size.
 /// It will allocate an initial memory block with given size from the implementation allocator
 /// and puts it onto the free list.
 /// \requires \c node_size must be a valid \concept{concept_node,node size}
 /// and \c block_size must be a non-zero value.
 memory_pool(std::size_t node_size, std::size_t block_size,
             allocator_type allocator = allocator_type())
 : leak_checker(info().name),
   block_list_(block_size, detail::move(allocator)),
   free_list_(node_size)
 {
     allocate_block();
 }
예제 #14
0
파일: write.c 프로젝트: libguestfs/hivex
/* Insert node into existing lf/lh-record at position.
 * This allocates a new record and marks the old one as unused.
 */
static size_t
insert_lf_record (hive_h *h, size_t old_offs, size_t posn,
                  const char *name, hive_node_h node)
{
  assert (IS_VALID_BLOCK (h, old_offs));

  /* Work around C stupidity.
   * http://www.redhat.com/archives/libguestfs/2010-February/msg00056.html
   */
  int test = block_id_eq (h, old_offs, "lf") || block_id_eq (h, old_offs, "lh");
  assert (test);

  struct ntreg_lf_record *old_lf =
    (struct ntreg_lf_record *) ((char *) h->addr + old_offs);
  size_t nr_keys = le16toh (old_lf->nr_keys);

  if (nr_keys == UINT16_MAX) {
    SET_ERRNO (EOVERFLOW,
               "cannot extend record because it already contains the maximum number of subkeys (%zu)",
               nr_keys);
    return 0;
  }
  nr_keys++; /* in new record ... */

  size_t seg_len = sizeof (struct ntreg_lf_record) + (nr_keys-1) * 8;

  /* Copy the old_lf->id in case it moves during allocate_block. */
  char id[2];
  memcpy (id, old_lf->id, sizeof id);

  size_t new_offs = allocate_block (h, seg_len, id);
  if (new_offs == 0)
    return 0;

  /* old_lf could have been invalidated by allocate_block. */
  old_lf = (struct ntreg_lf_record *) ((char *) h->addr + old_offs);

  struct ntreg_lf_record *new_lf =
    (struct ntreg_lf_record *) ((char *) h->addr + new_offs);
  new_lf->nr_keys = htole16 (nr_keys);

  /* Copy the keys until we reach posn, insert the new key there, then
   * copy the remaining keys.
   */
  size_t i;
  for (i = 0; i < posn; ++i)
    new_lf->keys[i] = old_lf->keys[i];

  new_lf->keys[i].offset = htole32 (node - 0x1000);
  calc_hash (new_lf->id, name, new_lf->keys[i].hash);

  for (i = posn+1; i < nr_keys; ++i)
    new_lf->keys[i] = old_lf->keys[i-1];

  /* Old block is unused, return new block. */
  mark_block_unused (h, old_offs);
  return new_offs;
}
예제 #15
0
/* Return Newly allocated block number */
static uint32_t
_add_block(int *ptr)
{
	uint32_t block_no = allocate_block();

	if (block_no) {
		*ptr = block_no;
		memset(ospfs_block(block_no), 0, OSPFS_BLKSIZE);
	}
	return block_no;
}
예제 #16
0
void DiskBackedBlockMap::read_block_from_disk(ServerBlock*& block, const BlockId& block_id, size_t block_size){
    /** Allocates space for a block, reads block from disk
     * into newly allocated space, sets the in_memory flag,
     * inserts into block_map_ if needed
     */
	block = allocate_block(block, block_size);
	server_timer_.start_timer(current_line(), ServerTimer::READTIME);
	disk_backed_arrays_io_.read_block_from_disk(block_id, block);
	server_timer_.pause_timer(current_line(), ServerTimer::READTIME);
	block->set_in_memory();
}
예제 #17
0
파일: filesystem.c 프로젝트: ezhangle/recc
void set_file_size_given_inode(unsigned int inode, unsigned int target_size){
	assert(!inodes[inode].is_directory);
	if(inodes[inode].first_block_initialized){
		assert(0 && "Not implemented.");
	}else{
		unsigned int block_index;
		inodes[inode].first_block = allocate_block();
		inodes[inode].first_block_initialized = 1;

		blocks[inodes[inode].first_block].header.previous_block = 0;
		blocks[inodes[inode].first_block].header.previous_block_initialized = 0;

		blocks[inodes[inode].first_block].header.next_block = 0;
		blocks[inodes[inode].first_block].header.next_block_initialized = 0;
		block_index = inodes[inode].first_block;

		while(target_size > 0){
			unsigned int bytes_to_expand;
			unsigned int new_block_index;
			if(target_size <= (DATA_WORDS_PER_BLOCK * sizeof(unsigned int))){
				bytes_to_expand = target_size;
			}else{
				bytes_to_expand = (DATA_WORDS_PER_BLOCK * sizeof(unsigned int));
			}
			blocks[block_index].header.bytes_used = bytes_to_expand;
			target_size -= bytes_to_expand;
			/*  Check if more blocks need to be allocated */
			if(target_size > 0){
				new_block_index = allocate_block();
				blocks[block_index].header.next_block = new_block_index;
				blocks[block_index].header.next_block_initialized = 1;
				blocks[new_block_index].header.next_block = 0;
				blocks[new_block_index].header.next_block_initialized = 0;
				blocks[new_block_index].header.previous_block = block_index;
				blocks[new_block_index].header.previous_block_initialized = 1;
				block_index = new_block_index;
			}
		}
	}
}
예제 #18
0
파일: write.c 프로젝트: libguestfs/hivex
/* Insert node into existing li-record at position.  Pretty much the
 * same as insert_lf_record above, but the record layout is a bit
 * different.
 */
static size_t
insert_li_record (hive_h *h, size_t old_offs, size_t posn,
                  const char *name, hive_node_h node)
{
  assert (IS_VALID_BLOCK (h, old_offs));
  assert (block_id_eq (h, old_offs, "li"));

  struct ntreg_ri_record *old_li =
    (struct ntreg_ri_record *) ((char *) h->addr + old_offs);
  size_t nr_offsets = le16toh (old_li->nr_offsets);

  if (nr_offsets == UINT16_MAX) {
    SET_ERRNO (EOVERFLOW,
               "cannot extend record because it already contains the maximum number of subkeys (%zu)",
               nr_offsets);
    return 0;
  }
  nr_offsets++; /* in new record ... */

  size_t seg_len = sizeof (struct ntreg_ri_record) + (nr_offsets-1) * 4;

  /* Copy the old_li->id in case it moves during allocate_block. */
  char id[2];
  memcpy (id, old_li->id, sizeof id);

  size_t new_offs = allocate_block (h, seg_len, id);
  if (new_offs == 0)
    return 0;

  /* old_li could have been invalidated by allocate_block. */
  old_li = (struct ntreg_ri_record *) ((char *) h->addr + old_offs);

  struct ntreg_ri_record *new_li =
    (struct ntreg_ri_record *) ((char *) h->addr + new_offs);
  new_li->nr_offsets = htole16 (nr_offsets);

  /* Copy the offsets until we reach posn, insert the new offset
   * there, then copy the remaining offsets.
   */
  size_t i;
  for (i = 0; i < posn; ++i)
    new_li->offset[i] = old_li->offset[i];

  new_li->offset[i] = htole32 (node - 0x1000);

  for (i = posn+1; i < nr_offsets; ++i)
    new_li->offset[i] = old_li->offset[i-1];

  /* Old block is unused, return new block. */
  mark_block_unused (h, old_offs);
  return new_offs;
}
예제 #19
0
 //------------------------------------------------------------------------
 inline void outline::add_cur_cell()
 {
     if(m_cur_cell.area | m_cur_cell.cover)
     {
         if((m_num_cells & cell_block_mask) == 0)
         {
             if(m_num_blocks >= cell_block_limit) return;
             allocate_block();
         }
         *m_cur_cell_ptr++ = m_cur_cell;
         m_num_cells++;
     }
 }
 //------------------------------------------------------------------------
 inline void outline_aa::add_cur_cell()
 {
     if(m_cur_cell.area | m_cur_cell.cover)
     {
         if((m_num_cells & cell_block_mask) == 0)
         {
             if(m_num_blocks >= cell_block_limit) return;
             allocate_block();
         }
         *m_cur_cell_ptr++ = m_cur_cell;
         ++m_num_cells;
         if(m_cur_cell.x < m_min_x) m_min_x = m_cur_cell.x;
         if(m_cur_cell.x > m_max_x) m_max_x = m_cur_cell.x;
     }
 }
예제 #21
0
파일: arena.c 프로젝트: Melab/gvmt
void *gvmt_allocate(size_t n, GVMT_Arena a) {
    struct arena_block *free = a->next;
//    printf("Allocate request for %u\n", n);
    n = roundup(n, sizeof (union align));
    if (n > free->limit - free->avail) {
        free = allocate_block(n);
        free->next = a->next;
        a->next = free;
    }
    char *result = free->avail;
    free->avail += n;
    assert(a->next->limit - result >= n);
    memset(result, 0, n);
    return result;
}
예제 #22
0
        void push_back (value_type x,value_type y,unsigned command)
        {
            unsigned block = pos_ >> block_shift;
            if (block >= num_blocks_)
            {
                allocate_block(block);
            }
            value_type* vertex = vertexs_[block] + ((pos_ & block_mask) << 1);
            unsigned char* cmd= commands_[block] + (pos_ & block_mask);
	    
            *cmd = static_cast<unsigned char>(command);
            *vertex++ = x;
            *vertex   = y;
            ++pos_;
        }
예제 #23
0
파일: lab3.c 프로젝트: alexc4051/ICS53_lab3
/*** function run_heap ***
Run the memory allocation as outlined in lab 3:
https://eee.uci.edu/16s/36680/labs/lab3_malloc.pdf
*/
void heap_alloc() {
  char *heap = malloc (400);
  struct Command input; // Input read in from the command line.
  // Set all values in the heap to zero
  memset(heap, '\0', 400);
  *(header_t*)heap = 400;
  // Run the heap_alloc's loop.
  while(1) {
    // Read in input from the console and run the command.
    if(read_command(&input) > 0) {
      // Quit condition.
      if(strcmp(input.program, "quit") == 0) {
        // End Program
        free_command(&input);
        break;
      }
      else if (strcmp(input.program, "allocate") == 0 && input.len == 2){
		    //Allocate Heap Space
        allocate_block(heap, input.array);
  	  }
  	  else if (strcmp(input.program, "free") == 0 && input.len == 2){
  		  //Free Heap Space
        free_block(heap, input.array);
  	  }
  	  else if (strcmp(input.program, "blocklist") == 0 && input.len == 1){
  		  //Prints out Block Info
        print_blocklist(heap, input.array);
  	  }
  	  else if (strcmp(input.program, "writeheap") == 0 && input.len == 4){
  		  //Writes X chars to heap block
        write_block(heap, input.array);
  	  }
  	  else if (strcmp(input.program, "printheap") == 0 && input.len == 3){
  		  //Prints out heap w/out header
        print_heap(heap, input.array);
  	  }
  	  else{
  		  fprintf(stderr, "Error: Invalid command or invalid arguments.\n");
  	  }
  	} else {
        fprintf(stderr, "Error: Unable to read in input.\n");
  	}
    free_command(&input);
  }
  // Deallocate the heap.
  free(heap);
}
예제 #24
0
파일: write.c 프로젝트: libguestfs/hivex
/* Create a completely new lh-record containing just the single node. */
static size_t
new_lh_record (hive_h *h, const char *name, hive_node_h node)
{
  static const char id[2] = { 'l', 'h' };
  size_t seg_len = sizeof (struct ntreg_lf_record);
  size_t offset = allocate_block (h, seg_len, id);
  if (offset == 0)
    return 0;

  struct ntreg_lf_record *lh =
    (struct ntreg_lf_record *) ((char *) h->addr + offset);
  lh->nr_keys = htole16 (1);
  lh->keys[0].offset = htole32 (node - 0x1000);
  calc_hash ("lh", name, lh->keys[0].hash);

  return offset;
}
예제 #25
0
ServerBlock* DiskBackedBlockMap::get_block_for_reading(const BlockId& block_id){
	/** If block is not in block map, there is an error !!
	 * Otherwise, if the block is in memory, read and return or
	 * if it is only on disk, read it in, store in block map and return.
	 * Set in_memory flag.
	 */
	ServerBlock* block = block_map_.block(block_id);
	size_t block_size = sip_tables_.block_size(block_id);
	if (block == NULL) {
		// Error !

		std::stringstream errmsg;
		errmsg << " S " << sip_mpi_attr_.global_rank();
		errmsg << " : Asking for block " << block_id << ". It has not been put/prepared before !"<< std::endl;
		std::cout << errmsg.str() << std::flush;
		
		sip::fail(errmsg.str());

		// WARNING DISABLED !
		if (false){
			std::stringstream msg;
			msg << "S " << sip_mpi_attr_.global_rank();
			msg << " : getting uninitialized block " << block_id << ".  Creating zero block "<< std::endl;
			std::cout << msg.str() << std::flush;
			block = allocate_block(NULL, block_size);
			block_map_.insert_block(block_id, block);
		}


	} else {
		if(!block->is_in_memory())
			if (block->is_on_disk()){
				read_block_from_disk(block, block_id, block_size);
            } else {
				sip::fail("get_block_for_reading : ServerBlock neither on memory or on disk !");
            }
	}

	block->set_in_memory();

	policy_.touch(block_id);

	sip::check(block != NULL, "Block is NULL in Server get_block_for_reading, should not happen !");
	return block;
}
예제 #26
0
파일: inode.c 프로젝트: samath/140-v2
/* Initializes an inode with LENGTH bytes of data and
   writes the new inode to sector SECTOR on the file system
   device.
   Returns true if successful.
   Returns false if memory or disk allocation fails. */
bool
inode_create (block_sector_t sector, off_t length, bool isdir)
{
  struct inode_disk *disk_inode = NULL;
  bool success = false;

  ASSERT (length >= 0);

  /* If this assertion fails, the inode structure is not exactly
     one sector in size, and you should fix that. */
  ASSERT (sizeof *disk_inode == BLOCK_SECTOR_SIZE);

  disk_inode = calloc (1, sizeof *disk_inode);
  if (disk_inode != NULL)
    {
      disk_inode->length = length;
      disk_inode->magic = INODE_MAGIC;
      disk_inode->isdir = isdir ? 1 : 0;

      // Initialize all block entries to -1 (NO_BLOCK).
      int i = 0;
      for(; i < TOTAL_BLOCKS; i++) disk_inode->blocks[i] = NO_BLOCK;

      block_write_cache (fs_device, sector, disk_inode, 0,
                         BLOCK_SECTOR_SIZE, true, -1);
      success = true;

      /* Allocate blocks for inode. */
      struct inode inode;
      inode.data = *disk_inode;
      inode.sector = sector;
      int num_sectors = bytes_to_sectors (length);
      for(i = 0; i < num_sectors; i++) { 
        if (allocate_block (&inode, i) == NO_BLOCK) {
          /* Allocation failed.  Free other blocks. */
          close_blocks_for_inode (&inode);
          success = false;
          break;
        }
      }

      free (disk_inode);
    }
  return success;
}
예제 #27
0
파일: filesystem.c 프로젝트: ezhangle/recc
unsigned int create_new_directory_entry_block_pointing_at_inode(unsigned int previous, unsigned char * file_name, unsigned int inode){
	unsigned int rtn = allocate_block();
	struct directory_entry * entry;
	unsigned int namelen = (unsigned int)strlen((const char *)file_name);
	unsigned int i;
	assert(namelen + 1 < DATA_WORDS_PER_BLOCK * sizeof(unsigned int));
	blocks[rtn].header.next_block = 0;
	blocks[rtn].header.next_block_initialized = 0;
	blocks[rtn].header.previous_block = previous;
	blocks[rtn].header.previous_block_initialized = 1;
	blocks[rtn].header.bytes_used = sizeof(unsigned int) + namelen + 1;
	entry = (struct directory_entry *)&blocks[rtn].data[0];
	entry->inode = inode;
	for(i = 0; i < namelen; i++){
		entry->file_name[i] = file_name[i];
	}
	entry->file_name[i] = 0; /*  Null termination */
	return rtn;
}
예제 #28
0
파일: cproxy.c 프로젝트: AmesianX/RosWine
static BOOL fill_stubless_table( IUnknownVtbl *vtbl, DWORD num )
{
    const void **entry = (const void **)(vtbl + 1);
    DWORD i, j;

    if (num - 3 > BLOCK_SIZE * MAX_BLOCKS)
    {
        FIXME( "%u methods not supported\n", num );
        return FALSE;
    }
    for (i = 0; i < (num - 3 + BLOCK_SIZE - 1) / BLOCK_SIZE; i++)
    {
        const struct thunk *block = method_blocks[i];
        if (!block && !(block = allocate_block( i ))) return FALSE;
        for (j = 0; j < BLOCK_SIZE && j < num - 3 - i * BLOCK_SIZE; j++, entry++)
            if (*entry == (LPVOID)-1) *entry = &block[j];
    }
    return TRUE;
}
예제 #29
0
파일: _malloc.c 프로젝트: lkyunl4/C
void* _malloc(size_t size){
	if(!initialized)
		mem_initialize();

	void* cur = HEAP_START;
	void* end = HEAP_END;
	size += sizeof(header);

	do {
		if(IS_USED(cur) || BLOCK_SIZE(cur) < size)
			continue;

		return allocate_block(cur, size);

	} while(NEXT_BLOCK(cur) >= end);

	/* TODO:단편화 확인 및 재배열 코드 추가 */
	/* 또는 가상메모리 사용할지 고민 중 */

	return NULL;
}
예제 #30
0
static void	*realloc_larger(void *ptr, t_block *block, size_t size)
{
	t_block	*next;
	void	*new_ptr;

	next = block->next;

	if (next != NULL && next->free && next->size + MALLOC_BLOCK_SIZE >= size)
	{
		merge_block_with_next(block);
		split_block(block, size);
		return (ptr);
	}
	else
	{
		new_ptr = allocate_block(size);
		memcpy(new_ptr, ptr, size);
		block->free = 1;
		return (new_ptr);
	}
}