Esempio n. 1
0
void compile_iter_loop(compiler_wrapper *cw, ast_node *root)
{
    ast_loop_node *node = (ast_loop_node *)root;

    compile(cw, node->onloop);
    append_op(cw, LI_MAKE_ITER, node->lineno);

    int tagOut = next_if_tag(cw);
    int tagLoop = next_if_tag(cw);

    int start = (int)cw->rops.count;

    append_op(cw, LI_NEXT_ITER_OR_JUMP, node->lineno);
    append_op(cw, tagOut, node->lineno);
    append_op(cw, -1, node->lineno);
    append_op(cw, -1, node->lineno);
    append_op(cw, -1, node->lineno);

    append_var_info(cw, ((ast_value_node *)(node->init))->value.s, 0, node->lineno);
    append_op(cw, LI_POP, node->lineno);

    if(node->condition)
    {
        append_op(cw, LI_ITER_INDEX, node->lineno);
        append_var_info(cw, ((ast_value_node *)(node->condition))->value.s, 0, node->lineno);
        append_op(cw, LI_POP, node->lineno);
    }

    lky_object *wrapLoop = lobjb_build_int(tagLoop);    
    lky_object *wrapOut = lobjb_build_int(tagOut);
    pool_add(&ast_memory_pool, wrapLoop);
    pool_add(&ast_memory_pool, wrapOut);

    arr_append(&cw->loop_start_stack, wrapLoop);
    arr_append(&cw->loop_end_stack, wrapOut);

    compile_compound(cw, node->payload->next);

    arr_remove(&cw->loop_start_stack, NULL, cw->loop_start_stack.count - 1);
    arr_remove(&cw->loop_end_stack, NULL, cw->loop_end_stack.count - 1);

    append_op(cw, tagLoop, node->lineno);

    append_op(cw, LI_JUMP, node->lineno); // Add the jump to the start location
    unsigned char buf[4];
    int_to_byte_array(buf, start);
    append_op(cw, buf[0], node->lineno);
    append_op(cw, buf[1], node->lineno);
    append_op(cw, buf[2], node->lineno);
    append_op(cw, buf[3], node->lineno);

    append_op(cw, tagOut, node->lineno);
    append_op(cw, LI_POP, node->lineno);

    cw->save_val = 1;
}
Esempio n. 2
0
File: buffer.c Progetto: jnbek/cblog
/* bufrelease • decrease the reference count and free the buffer if needed */
void
bufrelease(struct buf *buf) {
	if (!buf || !buf->unit) return;
	buf->ref -= 1;
	if (buf->ref == 0) {
#ifdef TRACK_BUFFERS
		int i = 0;
		while (i < all_buffers.size)
			if (all_buffers.item[i] == buf)
				parr_remove(&all_buffers, i);
			else i += 1;
#endif
#ifdef TRACK_BUFFER_DEBUG
		int i = 0;
		struct buf_debug_data *bdd = all_buffers.base;
		while (i < all_buffers.size)
			if (bdd[i].buf == buf)
				arr_remove(&all_buffers, i);
			else i += 1;
#endif
#ifdef BUFFER_STATS
		buffer_stat_nb -= 1;
		buffer_stat_alloc_bytes -= buf->asize;
#endif
		free(buf->data);
		free(buf); } }
Esempio n. 3
0
static void new_void_arr_minus(void**state)
{
    arr_t * arr = arr_new(5);
    arr_add(arr, 3, -2);
    arr_add(arr, 1, -1);
    assert_int_equal(arr_minus(arr), 1);
    arr_remove(&arr);
}
Esempio n. 4
0
static void _new_remove_length(void **state)
{
    arr_t * arr = arr_new(5);
    assert_int_equal(arr_length(arr), 5);
    arr_remove(&arr);
    assert_null(arr);
    arr = arr_new(5000000);
    assert_null(arr);
}
Esempio n. 5
0
static void new_void_arr_find(void**state)
{
    arr_t * arr = arr_new(5);
    int mas[5] = {1 , 3 , 3 ,3 ,5};
    for(int i = 0 ; i<5 ; i++)
    arr_add(arr, i, mas[i]);
    assert_int_equal(arr_find(arr) , 3);
    arr_remove(&arr);
}
Esempio n. 6
0
static void new_void__arr_add(void **state)
{
    arr_t * arr = arr_new(5);
    assert_int_equal(arr_add(arr, 3, 10), 0);
    assert_int_equal(arr_add(arr, 6, 10), 1);
    int elem = 0;
    assert_int_equal(arr_get(arr, 3, &elem), 1);
    assert_int_equal(elem, 10);
    assert_int_equal(arr_get(arr, 37, &elem), 0);
    arr_remove(&arr);
}
Esempio n. 7
0
static void
avl_clear_intern (avl_t avl, int free_data)
{
  arr_t arr;
  avl_entry_t curr;
  size_t size;

  /* Create a new array to be used as a stack */
  arr = arr_init ();

  /* Iterate over every node */
  arr_insert_back (arr, avl->root);
  while ((size = arr_size (arr)) > 0)
    {
      /* Get the current element from the stack */
      curr = (avl_entry_t) arr_get (arr, size-1);
      arr_remove (arr, size-1);

      /* Check for invalid nodes */
      if (curr == NULL)
	continue;

      /* Push children onto the stack */
      arr_insert_back (arr, curr->left);
      arr_insert_back (arr, curr->right);

      /* Free parent */
      if (free_data && curr->data != NULL)
	free (curr->data);
      avl->free (curr->key);
      free (curr);
    }

  /* Cleanup the array */
  arr_destroy (arr);

  /* Update AVL Struct */
  avl->size = 0;
  avl->root = NULL;

  return;
}
Esempio n. 8
0
void compile_loop(compiler_wrapper *cw, ast_node *root)
{
    ast_loop_node *node = (ast_loop_node *)root;

    // if(node->init && node->condition && !node->onloop)
    //    return compile_iter_loop(cw, root);

    int tagOut = next_if_tag(cw); // Prepare the exit tag
    int tagLoop = next_if_tag(cw); // Prepare the continue tag

    if(node->init) // If we have a for loop, compile the init
    {
        char save = cw->save_val;
        cw->save_val = 0;
        compile(cw, node->init);
        if(!cw->save_val)
            append_op(cw, LI_POP, node->lineno);
        cw->save_val = save;
    }

    int start = (int)cw->rops.count; // The start location (for loop jumps)

    compile(cw, node->condition); // Append the tag for the unknown end location
    append_op(cw, LI_JUMP_FALSE, node->lineno);
    append_op(cw, tagOut, node->lineno);
    append_op(cw, -1, node->lineno); // Note that we use for bytes to represent jump locations.
    append_op(cw, -1, node->lineno); // This allows us to index locations beyond 255 in the
    append_op(cw, -1, node->lineno); // interpreter.
    
    lky_object *wrapLoop = lobjb_build_int(tagLoop);
    lky_object *wrapOut = lobjb_build_int(tagOut);
    pool_add(&ast_memory_pool, wrapLoop);
    pool_add(&ast_memory_pool, wrapOut);
    arr_append(&cw->loop_start_stack, wrapLoop);
    arr_append(&cw->loop_end_stack, wrapOut);

    compile_compound(cw, node->payload->next);

    arr_remove(&cw->loop_start_stack, NULL, cw->loop_start_stack.count - 1);
    arr_remove(&cw->loop_end_stack, NULL, cw->loop_end_stack.count - 1);

    append_op(cw, tagLoop, node->lineno);

    if(node->onloop) // If a for loop, compile the onloop.
    {
        char save = cw->save_val;
        cw->save_val = 0;
        compile(cw, node->onloop);
        if(!cw->save_val)
            append_op(cw, LI_POP, node->lineno);
        cw->save_val = save;
    }

    append_op(cw, LI_JUMP, node->lineno); // Add the jump to the start location
    unsigned char buf[4];
    int_to_byte_array(buf, start);

    append_op(cw, buf[0], node->lineno);
    append_op(cw, buf[1], node->lineno);
    append_op(cw, buf[2], node->lineno);
    append_op(cw, buf[3], node->lineno);

    append_op(cw, tagOut, node->lineno);

    cw->save_val = 1;
}
Esempio n. 9
0
File: newdar.cpp Progetto: pwoo/admb
/**
 * Description not yet available.
 * \param
 */
void arr_free(double_and_int * varr)
{
  // This routines frees up a memory block and
  // consolidates the free blocks if possible
  //cout<< "calling arr_free\n";
  char * temp_ptr;
  arr_link * ptr;

  temp_ptr = (char *) varr;

  //temp=sizeof(double_and_int);
//  ptr = *(arr_link **) (temp_ptr-temp);
  ptr = *(arr_link **) (temp_ptr);

  //mark this block free

  ptr->status = 0;
 // cout <<"Marking arr_link with adress  "<<farptr_tolong(ptr)<<"free\n";

  // if there is a block after this add this one to the free list
  if (ptr->next) arr_free_add(ptr);

  if (!ptr->next)  // Is this the last link?
  {
    // Check to see if ptr->prev is free and should be deleted as well
    // ... but first check to see if ptr is first block in list
    // which will be indicated by ptr->prev being a NULL pointer
    if (ptr->prev && !ptr->prev->status)
    {
      // delete ptr->prev
      gradient_structure::ARR_LIST1->last = ptr->prev->prev;
      //if (gradient_structure::ARR_LIST1->last ==0)
       // cout << "gradient_structure::ARR_LIST1->last =0 " << endl;

      gradient_structure::ARR_LIST1->last_offset -= ptr->size + ptr->prev->size;
      arr_free_remove(ptr->prev);
      arr_remove(&(ptr->prev));
    }
    else
    {
      gradient_structure::ARR_LIST1->last = ptr->prev;
      //if (gradient_structure::ARR_LIST1->last ==0)
       // cout << "gradient_structure::ARR_LIST1->last =0 " << endl;
      gradient_structure::ARR_LIST1->last_offset -= ptr->size;
    }
    arr_remove(&ptr);
  }
  else
  {
     // There is another link after this one?

    if (!ptr->next->status) // If yes is it free?
    {
      // add its memory capacity to the present one and delete it

      ptr->size += ptr->next->size;

      arr_free_remove(ptr->next);
      arr_remove(&ptr->next);
    }

    if (ptr->prev)  // Is there another link before this one?
    {
      if (!ptr->prev->status) // If yes is it free?
      {
        // we will keep ptr->prev and add ptr to it

        ptr->prev->size += ptr->size;
        arr_free_remove(ptr);
        arr_remove(&ptr);
      }
    }
  }
}