Example #1
0
/**
 * This function searches an available block with appropriate size.
 */
static struct block_desc *find_suit_block(size_t req_size) {
	/* Set the pointer(iterator) on the begin of our free memory */
	struct block_desc *md = (void *) current_space;

	printf("looking for: %d\n", req_size);
#if 0
	printf("current_free_space 0x%x\n", (uint32_t)current_space);
#endif

	/*Check address correction*/
	if (correct_address(current_space) == 0) {
		printf("address 0x%X is not correct!!!  \n", (uint32_t) current_space);
		return NULL;
	}
	/* We iterates under our pool while current block is not available
	 * or req_size of current block less then req_size.
	 * If the iterator exceed the end of the our available memory we couldn't
	 *  allocate appropriate block and return NULL */
	while ((!get_available(md)) || (!suit_size(md, req_size))) {
		md = (void *) (((size_t) md) + md->size);
#if 0
		printf("md = 0x%X\n", (uint32_t)md);
		printf("block is not able for this new block \n");
#endif
		/*Check the end of the memory*/
		if (((uint32_t) md) >= (uint32_t) (memory) + sizeof(memory)) {
			return NULL;
		}
	}
#if 0
	printf("need %d\n", (uint32_t)(req_size) + (uint32_t)(BLOCK_DESC_SIZE));
	printf("find 0x%x\n", (uint32_t)md);
#endif
	return md;
}
Example #2
0
void		*malloc(unsigned int taille)
{
  t_blk		*new_blk;
  unsigned char	psize;
  char val[33];

  taille += sizeof(t_blk);
  sup_power(&taille, &psize);
  if (!(new_blk = get_available(psize)))
    {
      new_blk = sbrk(taille);
      add_blk(new_blk, psize);
    }
  else
    {
      new_blk->is_available = 0;
    }
  my_putstr("taille :\n");
  my_putnbr(psize);
  my_putchar('\n');

  conv_10_to_16(new_blk, val);
  my_putstr(val);
  my_putchar('\n');
  show_alloc_mem();
  return (new_blk + 1);
}
Example #3
0
/*Resolve defragmentation with a next block*/
static void defragmentation(struct block_desc *md) {
	struct block_desc *next_md;

	next_md = (void *) (((size_t) md) + md->size);

	if (!correct_address((char *) md)) {
		return;
	}

	if (get_available(md)) {
		/*Look at the next block and it is free, paste it*/
		if (get_available(next_md)) {
			md->size = md->size + next_md->size;
			defragmentation(md);
		}
	}
	defragmentation(next_md);
}
Example #4
0
/**
 * 根据request size来决定是通过small bin还是big bin还是direct 来分配内存
 */
void* alloc_mem(mem_pool_t *pool,intptr_t size){
  intptr_t real_size=size+head_size();
  void *p;
  if(real_size<=SMALL_THRESHOLD){
    pthread_mutex_lock(&(pool->small_mutex));
    p=get_available(pool->small_bin,real_size,SMALL_THRESHOLD,pool);
    pthread_mutex_unlock(&(pool->small_mutex));
  }else if(real_size<=BIG_THRESHOLD){
    pthread_mutex_lock(&(pool->big_mutex));
    p=get_available(pool->big_bin,real_size,BIG_THRESHOLD,pool);
    pthread_mutex_unlock(&(pool->big_mutex));
  }else{
    pthread_mutex_lock(&(pool->direct_mutex));
    p=direct_alloc(&(pool->direct_head),real_size,pool);
    pthread_mutex_unlock(&(pool->direct_mutex));
  }
  return p;
}
Example #5
0
/* This is synthetic algorithm of memory allocation tests with different situation */
static void input_mem(void) {
	int p = 0;
	struct block_desc *md;
	md = (void *) memory;

	while (correct_address((char *) md)) {
		printf("block num = %d\n ", p);
		printf("   address = 0x%X\n", (uint32_t) md);
		printf("   available = %d\n", get_available(md));
		printf("   size = 0x%X\n", (uint32_t) md->size);
		md = (void *) (((size_t) md) + md->size);
		p++;
	}
}
Example #6
0
size_t buffer_read(char* buffer, unsigned char* buf, size_t len,
                   int on_empty) {
  uint32_t available;
  uint32_t bytes;
  uint32_t tail;
  uint32_t size;
  uint32_t read;
  if (is_empty(buffer)) return on_empty;
  available = get_available(buffer);
  bytes = available > len ? len : available;
  tail = get_tail(buffer);
  size = get_size(buffer);
  read = 0;
  while (read < bytes) {
    char* value_pointer = buffer + kDataIndex + tail;
    *(buf + read) = *value_pointer;
    tail = (tail + 1) % size;
    read++;
  }
  set_tail(buffer, tail);
  return read;
}
Example #7
0
bool thread_pool::stop_condition()
{
    return get_available() == get_size() && is_empty() && !is_running_;
}
Example #8
0
uint32_t get_free_space(char* buffer) {
  return get_size(buffer) - get_available(buffer) - 1;
}