Ejemplo n.º 1
0
static int
internal_function
heap_trim(heap_info *heap, size_t pad)
{
  mstate ar_ptr = heap->ar_ptr;
  unsigned long pagesz = GLRO(dl_pagesize);
  mchunkptr top_chunk = top(ar_ptr), p, bck, fwd;
  heap_info *prev_heap;
  long new_size, top_size, extra, prev_size, misalign;

  /* Can this heap go away completely? */
  while(top_chunk == chunk_at_offset(heap, sizeof(*heap))) {
    prev_heap = heap->prev;
    prev_size = prev_heap->size - (MINSIZE-2*SIZE_SZ);
    p = chunk_at_offset(prev_heap, prev_size);
    /* fencepost must be properly aligned.  */
    misalign = ((long) p) & MALLOC_ALIGN_MASK;
    p = chunk_at_offset(prev_heap, prev_size - misalign);
    assert(p->size == (0|PREV_INUSE)); /* must be fencepost */
    p = prev_chunk(p);
    new_size = chunksize(p) + (MINSIZE-2*SIZE_SZ) + misalign;
    assert(new_size>0 && new_size<(long)(2*MINSIZE));
    if(!prev_inuse(p))
      new_size += p->prev_size;
    assert(new_size>0 && new_size<HEAP_MAX_SIZE);
    if(new_size + (HEAP_MAX_SIZE - prev_heap->size) < pad + MINSIZE + pagesz)
      break;
    ar_ptr->system_mem -= heap->size;
    arena_mem -= heap->size;
    delete_heap(heap);
    heap = prev_heap;
    if(!prev_inuse(p)) { /* consolidate backward */
      p = prev_chunk(p);
      unlink(p, bck, fwd);
    }
    assert(((unsigned long)((char*)p + new_size) & (pagesz-1)) == 0);
    assert( ((char*)p + new_size) == ((char*)heap + heap->size) );
    top(ar_ptr) = top_chunk = p;
    set_head(top_chunk, new_size | PREV_INUSE);
    /*check_chunk(ar_ptr, top_chunk);*/
  }
  top_size = chunksize(top_chunk);
  extra = (top_size - pad - MINSIZE - 1) & ~(pagesz - 1);
  if(extra < (long)pagesz)
    return 0;
  /* Try to shrink. */
  if(shrink_heap(heap, extra) != 0)
    return 0;
  ar_ptr->system_mem -= extra;
  arena_mem -= extra;

  /* Success. Adjust top accordingly. */
  set_head(top_chunk, (top_size - extra) | PREV_INUSE);
  /*check_chunk(ar_ptr, top_chunk);*/
  return 1;
}
Ejemplo n.º 2
0
void				check_free_heap(t_heap *heap)
{
	t_block				*block;

	if (heap->lock == FALSE)
	{
		block = heap->block;
		if (!(block->next) && !(block->prev) && block->free == TRUE)
			delete_heap(heap);
	}
}
Ejemplo n.º 3
0
int main()
{
    int a[MAX_SIZE];
    int n=0,ch,item;
    while(1)
    {
        printf("\n1. Insert\n2. Delete\n3. Display\n4. Exit\n Enter your choice : ");
        scanf("%d",&ch);
        switch(ch)
        {
            case 1:printf("Enter the item to insert : ");
                    scanf("%d",&item);
                    n=insert_heap(item,a,n);break;
            case 2:n=delete_heap(a,n);break;
            case 3:display(a,n);break;
            case 4:return 0;
            default:printf("Invalid Choice");
        }
    }
    return 0;
}
Ejemplo n.º 4
0
int main() {
  heap = new_heap(1, int_compare);
  insert_(5);
  print_heap(heap, print);
  insert_(3);
  print_heap(heap, print);
  print_extracted();
  insert_(3);
  print_heap(heap, print);
  insert_(2);
  print_heap(heap, print);
  insert_(4);
  print_heap(heap, print);
  insert_(5);
  insert_(5);
  insert_(5);
  insert_(35);
  insert_(1);
  insert_(5);
  print_heap(heap, print);
  insert_(5);
  print_heap(heap, print);
  insert_(8);
  print_heap(heap, print);
  insert_(9);
  print_heap(heap, print);
  insert_(4);
  print_heap(heap, print);
  print_extracted();
  print_extracted();
  print_extracted();
  print_extracted();
  print_extracted();
  print_extracted();
  print_extracted();
  print_extracted();
  print_extracted();
  print_extracted();
  delete_heap(heap);
}
Ejemplo n.º 5
0
int main (int argc, char *argv[])
{
   puzzle best_soln;
   int    best_cost;
   int    i;
   puzzle s, t, u;
   int    lbsf;

   initialize_heap();
   get_puzzle(&s);
   startTime = clock()/1000;
   print_puzzle(s);
   insert_heap(s);
   best_cost = 999;
   lbsf = 0;
   while(heap_size > 0) {
      u = delete_heap();
      if (u.lower_bound > lbsf) {
         lbsf = u.lower_bound;
      }
      if (u.lower_bound >= best_cost) break;
      if (solved(u)) {
         if (u.lower_bound < best_cost) {
            s = u;
            best_cost = u.lower_bound;
         }
      } else {
         for (i = 0; i < possible_moves[u.hole]; i++) {
            t = make_move (u, i);
            insert_heap (t);
         }
      }
   }
   print_solution (s);
   printf("Finished in %i milliseconds.\n", clock()/1000 - startTime);
}
Ejemplo n.º 6
0
int main() 
{
      heap* h = new_heap(_TEST_SIZE, ORD_ASC, compare_integer);
      srand(time(NULL));
      int i;
      for (i=0;i<_TEST_SIZE;i++) 
      {
            int* data = malloc(sizeof(int));
            *data = rand()%1000;
            push_heap(h,data);
            printf("\n[%d]\t%d",i+1,*(int*)h->array[i+1]);
      }
      printf("\n\n\n");
           
      for (i=0;i<_TEST_SIZE+5;i++) 
      {
            int* x = pop_heap(h);
            if (x!=NULL) printf("\n[%d]\t%d",i+1,*x);
      }

      delete_heap(h);
      
      return 0;
}
Ejemplo n.º 7
0
static int
internal_function
heap_trim (heap_info *heap, size_t pad)
{
  mstate ar_ptr = heap->ar_ptr;
  unsigned long pagesz = GLRO (dl_pagesize);
  mchunkptr top_chunk = top (ar_ptr), p, bck, fwd;
  heap_info *prev_heap;
  long new_size, top_size, top_area, extra, prev_size, misalign;

  /* Can this heap go away completely? */
  while (top_chunk == chunk_at_offset (heap, sizeof (*heap)))
    {
      prev_heap = heap->prev;
      prev_size = prev_heap->size - (MINSIZE - 2 * SIZE_SZ);
      p = chunk_at_offset (prev_heap, prev_size);
      /* fencepost must be properly aligned.  */
      misalign = ((long) p) & MALLOC_ALIGN_MASK;
      p = chunk_at_offset (prev_heap, prev_size - misalign);
      assert (p->size == (0 | PREV_INUSE)); /* must be fencepost */
      p = prev_chunk (p);
      new_size = chunksize (p) + (MINSIZE - 2 * SIZE_SZ) + misalign;
      assert (new_size > 0 && new_size < (long) (2 * MINSIZE));
      if (!prev_inuse (p))
        new_size += p->prev_size;
      assert (new_size > 0 && new_size < HEAP_MAX_SIZE);
      if (new_size + (HEAP_MAX_SIZE - prev_heap->size) < pad + MINSIZE + pagesz)
        break;
      ar_ptr->system_mem -= heap->size;
      arena_mem -= heap->size;
      LIBC_PROBE (memory_heap_free, 2, heap, heap->size);
      delete_heap (heap);
      heap = prev_heap;
      if (!prev_inuse (p)) /* consolidate backward */
        {
          p = prev_chunk (p);
          unlink (ar_ptr, p, bck, fwd);
        }
      assert (((unsigned long) ((char *) p + new_size) & (pagesz - 1)) == 0);
      assert (((char *) p + new_size) == ((char *) heap + heap->size));
      top (ar_ptr) = top_chunk = p;
      set_head (top_chunk, new_size | PREV_INUSE);
      /*check_chunk(ar_ptr, top_chunk);*/
    }

  /* Uses similar logic for per-thread arenas as the main arena with systrim
     by preserving the top pad and at least a page.  */
  top_size = chunksize (top_chunk);
  top_area = top_size - MINSIZE - 1;
  if (top_area <= pad)
    return 0;

  extra = ALIGN_DOWN(top_area - pad, pagesz);
  if ((unsigned long) extra < mp_.trim_threshold)
    return 0;

  /* Try to shrink. */
  if (shrink_heap (heap, extra) != 0)
    return 0;

  ar_ptr->system_mem -= extra;
  arena_mem -= extra;

  /* Success. Adjust top accordingly. */
  set_head (top_chunk, (top_size - extra) | PREV_INUSE);
  /*check_chunk(ar_ptr, top_chunk);*/
  return 1;
}