コード例 #1
0
ファイル: stacklet.c プロジェクト: saghul/python-fibers
/* Save more of the C stack away, up to 'target_stop'.
 */
static void g_clear_stack(struct stacklet_s *g_target,
                          struct stacklet_thread_s *thrd)
{
    struct stacklet_s *current = thrd->g_stack_chain_head;
    char *target_stop = g_target->stack_stop;
    check_valid(g_target);

    /* save and unlink stacklets that are completely within
       the area to clear. */
    while (current != NULL && current->stack_stop <= target_stop) {
        struct stacklet_s *prev = current->stack_prev;
        check_valid(current);
        current->stack_prev = NULL;
        if (current != g_target) {
            /* don't bother saving away g_target, because
               it would be immediately restored */
            g_save(current, current->stack_stop
#ifdef DEBUG_DUMP
                   , 1
#endif
                   );
        }
        current = prev;
    }

    /* save a partial stack */
    if (current != NULL && current->stack_start < target_stop)
        g_save(current, target_stop
#ifdef DEBUG_DUMP
               , 1
#endif
               );

    thrd->g_stack_chain_head = current;
}
コード例 #2
0
ファイル: timer_heap_test.c プロジェクト: aaronjheng/grpc
static void shrink_test(void) {
  gpr_log(GPR_INFO, "shrink_test");

  grpc_timer_heap pq;
  size_t i;
  size_t expected_size;

  /* A large random number to allow for multiple shrinkages, at least 512. */
  const size_t num_elements = (size_t)rand() % 2000 + 512;

  grpc_timer_heap_init(&pq);

  /* Create a priority queue with many elements.  Make sure the Size() is
     correct. */
  for (i = 0; i < num_elements; ++i) {
    GPR_ASSERT(i == pq.timer_count);
    grpc_timer_heap_add(&pq, create_test_elements(1));
  }
  GPR_ASSERT(num_elements == pq.timer_count);

  /* Remove elements until the Size is 1/4 the original size. */
  while (pq.timer_count > num_elements / 4) {
    grpc_timer *const te = pq.timers[pq.timer_count - 1];
    grpc_timer_heap_remove(&pq, te);
    gpr_free(te);
  }
  GPR_ASSERT(num_elements / 4 == pq.timer_count);

  /* Expect that Capacity is in the right range:
     Size * 2 <= Capacity <= Size * 4 */
  GPR_ASSERT(pq.timer_count * 2 <= pq.timer_capacity);
  GPR_ASSERT(pq.timer_capacity <= pq.timer_count * 4);
  check_valid(&pq);

  /* Remove the rest of the elements.  Check that the Capacity is not more than
     4 times the Size and not less than 2 times, but never goes below 16. */
  expected_size = pq.timer_count;
  while (pq.timer_count > 0) {
    const size_t which = (size_t)rand() % pq.timer_count;
    grpc_timer *te = pq.timers[which];
    grpc_timer_heap_remove(&pq, te);
    gpr_free(te);
    expected_size--;
    GPR_ASSERT(expected_size == pq.timer_count);
    GPR_ASSERT(pq.timer_count * 2 <= pq.timer_capacity);
    if (pq.timer_count >= 8) {
      GPR_ASSERT(pq.timer_capacity <= pq.timer_count * 4);
    } else {
      GPR_ASSERT(16 <= pq.timer_capacity);
    }
    check_valid(&pq);
  }

  GPR_ASSERT(0 == pq.timer_count);
  GPR_ASSERT(pq.timer_capacity >= 16 && pq.timer_capacity < 32);

  grpc_timer_heap_destroy(&pq);
}
コード例 #3
0
ファイル: alarm_heap_test.c プロジェクト: Abioy/kythe
static void test1(void) {
  grpc_alarm_heap pq;
  const int num_test_elements = 200;
  const int num_test_operations = 10000;
  int i;
  grpc_alarm *test_elements = create_test_elements(num_test_elements);
  gpr_uint8 *inpq = gpr_malloc(num_test_elements);

  grpc_alarm_heap_init(&pq);
  memset(inpq, 0, num_test_elements);
  GPR_ASSERT(grpc_alarm_heap_is_empty(&pq));
  check_valid(&pq);
  for (i = 0; i < num_test_elements; ++i) {
    GPR_ASSERT(!contains(&pq, &test_elements[i]));
    grpc_alarm_heap_add(&pq, &test_elements[i]);
    check_valid(&pq);
    GPR_ASSERT(contains(&pq, &test_elements[i]));
    inpq[i] = 1;
    check_pq_top(test_elements, &pq, inpq, num_test_elements);
  }
  for (i = 0; i < num_test_elements; ++i) {
    /* Test that check still succeeds even for element that wasn't just
       inserted. */
    GPR_ASSERT(contains(&pq, &test_elements[i]));
  }

  GPR_ASSERT(pq.alarm_count == num_test_elements);

  check_pq_top(test_elements, &pq, inpq, num_test_elements);

  for (i = 0; i < num_test_operations; ++i) {
    int elem_num = rand() % num_test_elements;
    grpc_alarm *el = &test_elements[elem_num];
    if (!inpq[elem_num]) { /* not in pq */
      GPR_ASSERT(!contains(&pq, el));
      el->deadline = random_deadline();
      grpc_alarm_heap_add(&pq, el);
      GPR_ASSERT(contains(&pq, el));
      inpq[elem_num] = 1;
      check_pq_top(test_elements, &pq, inpq, num_test_elements);
      check_valid(&pq);
    } else {
      GPR_ASSERT(contains(&pq, el));
      grpc_alarm_heap_remove(&pq, el);
      GPR_ASSERT(!contains(&pq, el));
      inpq[elem_num] = 0;
      check_pq_top(test_elements, &pq, inpq, num_test_elements);
      check_valid(&pq);
    }
  }

  grpc_alarm_heap_destroy(&pq);
  gpr_free(test_elements);
  gpr_free(inpq);
}
コード例 #4
0
 bool check_valid(TreeNode* root, long long min_val,
                 long long max_val) {
     if (!root) {
         return true;
     }
     if (root->val <= min_val || root->val >= max_val) {
         return false;
     }
     return check_valid(root->left, min_val, root->val) &&
             check_valid(root->right, root->val, max_val);
 }
コード例 #5
0
ファイル: hexiom.c プロジェクト: slowfrog/hexiom
int solve(t_pos *pos) {
  if (check_valid(pos)) {
    return solve_step(pos);
  } else {
    return IMPOSSIBLE;
  }
}
コード例 #6
0
ファイル: shader.cpp プロジェクト: XanClic/dake
void dake::gl::shader::load(const char *file)
{
    check_valid();

    FILE *fp = fopen(dake::gl::find_resource_filename(file).c_str(), "rb");
    if (!fp) {
        throw std::invalid_argument("Could not open the given shader file");
    }

    name = std::string(file);

    fseek(fp, 0, SEEK_END);
    size_t len = ftell(fp);
    rewind(fp);

    char *src = new char[len + 1];
    if (fread(src, 1, len, fp) < len) {
        const char *err = strerror(errno);
        fclose(fp);
        delete[] src;
        throw std::runtime_error("Failed to read shader code from " + name
                                 + ": " + err);
    }
    src[len] = 0;

    fclose(fp);

    glShaderSource(id, 1, const_cast<const GLchar **>(&src), nullptr);
    delete[] src;
}
コード例 #7
0
ファイル: shader.cpp プロジェクト: XanClic/dake
bool dake::gl::program::link(void)
{
    check_valid();

    glLinkProgram(id);

    GLint status;
    glGetProgramiv(id, GL_LINK_STATUS, &status);
    if (status == GL_TRUE) {
        linked = true;
        return true;
    }

    GLint illen;
    glGetProgramiv(id, GL_INFO_LOG_LENGTH, &illen);

    if (illen <= 1) {
        throw std::runtime_error("Error linking shaders " + name + " to program: Reason unknown");
    } else {
        char *msg = new char[illen + 1];

        glGetProgramInfoLog(id, illen, nullptr, msg);
        msg[illen] = 0;

        std::string msg_str(msg);
        delete[] msg;

        throw std::runtime_error("Error linking shaders " + name + " to program: " + msg_str);
    }
}
コード例 #8
0
ファイル: shader.cpp プロジェクト: XanClic/dake
bool dake::gl::shader::compile(void)
{
    check_valid();

    glCompileShader(id);

    GLint status;
    glGetShaderiv(id, GL_COMPILE_STATUS, &status);
    if (status == GL_TRUE) {
        compiled = true;
        return true;
    }

    GLint illen;
    glGetShaderiv(id, GL_INFO_LOG_LENGTH, &illen);

    if (illen <= 1) {
        throw std::runtime_error("Error compiling " + std::string(shader_type_string(t)) + " shader " + name + ": Reason unknown");
    } else {
        char *msg = new char[illen + 1];

        glGetShaderInfoLog(id, illen, nullptr, msg);
        msg[illen] = 0;

        std::string msg_str(msg);
        delete[] msg;

        throw std::runtime_error("Error compiling " + std::string(shader_type_string(t)) + " shader " + name + ": " + msg_str);
    }

    return false;
}
コード例 #9
0
int		sudoki_bi()
{
  char		***tab;
  t_list	*list;
  t_present	*present;
  int		i;

  if ((tab = get_all_tab()) == NULL)
    return (-1);
  i = -1;
  while (++i < my_tablen(tab))
    {
      if ((present = get_present(tab[i])) == NULL ||
	  (list = get_list(tab[i])) == NULL)
	return (-1);
      filler(tab[i], list, present);
      if (check_valid(tab[i]) == 1)
        error_aff_tab();
      else
        aff_table(tab[i]);
      if (i < (my_tablen(tab) - 1))
        write(1, "####################\n", 21);
      free_function_bis(list, present);
    }
  free_function(tab);
  return (0);
}
コード例 #10
0
bool class_source_parser::parse_tag(){
	while (!f_.eof()){
		std::string line;
		if (! getline(line) ) { return false; }
		if (check_valid(line)) {
			if (check_tag_begin(line)){
				parse_tag();
			}
			if (check_tag_end(line)){
				return true;
			}
			if (check_keys_begin(line)){
				parse_keys();
			}
			if (check_allow_link(line)){
				continue;
			}
			if (check_allow_global(line)){
				continue;
			}
			if (check_remove_key(line)){
			}
		}else{
			if (!current_.empty()){
				// adding error messages for each unclosed entity
				add_open_tag_error();
				close_opened_tags();
			}
			return true;
		}
	}
	return true;
}
コード例 #11
0
bool class_source_parser::parse_block(){
	while (!f_.eof()){
		std::string line;
		if (! getline(line) ) { return false; }
		if ( check_valid(line)) {
			if (check_allow_type(line)) continue;
			if (check_remove_type(line)) continue;
			if (check_parent_begin(line)) continue;

			if (check_tag_begin(line)){
				parse_tag();
				continue;
			}
			check_parent_end(line);
		}
		else{
			// wiki-block is closed. checking stack of opened tags
			if (!current_.empty()){
				add_open_tag_error();
				close_opened_tags();
				// continue working
			}
			// block is closed and all tags are closed.
			return true;

		}
	}// end while
	return false;
}
コード例 #12
0
ファイル: SampleMinesEnvironment.c プロジェクト: 10sun/DRL-AI
void updatePosition(world_description_t *aWorld, int theAction){
	/* When the move would result in hitting an obstacles, the agent simply doesn't move */
	int newRow = aWorld->agentRow;
	int newCol = aWorld->agentCol;

	
	if (theAction == 0){/*move down*/
		newCol = aWorld->agentCol - 1;
	}
	if (theAction == 1){ /*move up*/
		newCol = aWorld->agentCol + 1;
	}
	if (theAction == 2){/*move left*/
		newRow = aWorld->agentRow - 1;
	}
	if (theAction == 3){/*move right*/
		newRow = aWorld->agentRow + 1;
	}


	/*Check if new position is out of bounds or inside an obstacle */
	if(check_valid(aWorld,newRow,newCol)){
   		aWorld->agentRow = newRow;
   		aWorld->agentCol = newCol;
	}
}
コード例 #13
0
ファイル: test-recurrence.c プロジェクト: 573/gnucash
static void test_specific(PeriodType pt, guint16 mult,
                          GDateMonth sm, GDateDay sd, GDateYear sy,
                          GDateMonth rm, GDateDay rd, GDateYear ry,
                          GDateMonth nm, GDateDay nd, GDateYear ny)
{
    GDate start;
    GDate ref, next, true_next;
    Recurrence r;

    g_date_set_dmy(&start, sd, sm, sy);
    g_date_set_dmy(&ref, rd, rm, ry);
    g_date_set_dmy(&true_next, nd, nm, ny);


    recurrenceSet(&r, mult, pt, &start, WEEKEND_ADJ_NONE);
    recurrenceNextInstance(&r, &ref, &next);

    check_valid(&next, &ref, &start, mult, pt, WEEKEND_ADJ_NONE);
    if (!test_equal(&next, &true_next))
    {
        gchar s1[21], s2[21], s3[21];
        g_date_strftime(s1, 20, "%x", &start);
        g_date_strftime(s2, 20, "%x", &ref);
        g_date_strftime(s3, 20, "%x", &true_next);
        printf("pt = %d; mult = %d; start = %s; ref = %s; true_next = %s\n",
               pt, mult, s1, s2, s3);
    }
}
コード例 #14
0
ファイル: reliable.c プロジェクト: nyn531/cs144
/* This function only gets called when the process is running as a
 * server and must handle connections from multiple clients.  You have
 * to look up the rel_t structure based on the address in the
 * sockaddr_storage passed in.  If this is a new connection (sequence
 * number 1), you will need to allocate a new conn_t using rel_create
 * ().  (Pass rel_create NULL for the conn_t, so it will know to
 * allocate a new connection.)
 */
void
rel_demux (const struct config_common *cc,
	   const struct sockaddr_storage *ss,
	   packet_t *pkt, size_t len)
{
  if (!check_valid(pkt, len)) return;

  rel_t * r = rel_list;
  
  while (r) {
    if (r->ss && addreq(ss, r->ss)) {
      break;
    }
    r = r->next; 
  }

  if (!r) {
    if (ntohl(pkt->seqno) == 1) {
      r = rel_create(NULL, ss, cc);
    } else {
      return;
    }
  }

  rel_recvpkt(r, pkt, len);
}
コード例 #15
0
ファイル: config.cpp プロジェクト: PoignardAzur/wesnoth
config::const_attr_itors config::attribute_range() const
{
	check_valid();

	return const_attr_itors(const_attribute_iterator(values.begin()),
	                        const_attribute_iterator(values.end()));
}
コード例 #16
0
ファイル: stacklet.c プロジェクト: saghul/python-fibers
STATIC_NOINLINE
void g_initialstub(struct stacklet_thread_s *thrd,
                   stacklet_run_fn run, void *run_arg)
{
    struct stacklet_s *result;

    /* The following call returns twice! */
    result = (struct stacklet_s *) _stacklet_switchstack(g_initial_save_state,
                                                         g_restore_state,
                                                         thrd);
    if (result == NULL && thrd->g_source != NULL) {
        /* First time it returns.  Only g_initial_save_state() has run
           and has created 'g_source'.  Call run(). */
        thrd->g_current_stack_stop = thrd->g_current_stack_marker;
        result = run(thrd->g_source, run_arg);

        /* Then switch to 'result'. */
        check_valid(result);
        thrd->g_target = result;
        _stacklet_switchstack(g_destroy_state, g_restore_state, thrd);

        _check_failed("we should not return here");
        abort();
    }
    /* The second time it returns. */
}
コード例 #17
0
int		my_getnbr(const char *str)
{
  char		sign;
  unsigned int	i;
  int		length;
  int		result;
  unsigned char	overflow;

  sign = '+';
  i = 0;
  length = my_strlen(str);
  result = 0;
  overflow = 0;
  while (i < length && check_valid(str[i]) && !overflow)
    {
      if (str[i] == '+' || str[i] == '-')
	{
	  add_sign(&sign, str[i]);
	}
      else
	{
	  overflow = update_result(&result, str[i], sign);
	}
      i = i + 1;
    }
  return (result);
}
コード例 #18
0
ファイル: config.cpp プロジェクト: PoignardAzur/wesnoth
bool config::matches(const config &filter) const
{
	check_valid(filter);

	for (const attribute &i : filter.attribute_range())
	{
		const attribute_value *v = get(i.first);
		if (!v || *v != i.second) return false;
	}

	for (const any_child &i : filter.all_children_range())
	{
		if (i.key == "not") {
			if (matches(i.cfg)) return false;
			continue;
		}
		bool found = false;
		for (const config &j : child_range(i.key)) {
			if (j.matches(i.cfg)) {
				found = true;
				break;
			}
		}
		if(!found) return false;
	}
	return true;
}
コード例 #19
0
ファイル: config.cpp プロジェクト: PoignardAzur/wesnoth
config &config::add_child_at(const std::string &key, const config &val, unsigned index)
{
	check_valid(val);

	child_list& v = children[key];
	if(index > v.size()) {
		throw error("illegal index to add child at");
	}

	v.insert(v.begin()+index,new config(val));

	bool inserted = false;

	const child_pos value(children.find(key),index);

	std::vector<child_pos>::iterator ord = ordered_children.begin();
	for(; ord != ordered_children.end(); ++ord) {
		if (ord->pos != value.pos) continue;
		if (!inserted && ord->index == index) {
			ord = ordered_children.insert(ord,value);
			inserted = true;
		} else if (ord->index >= index) {
			ord->index++;
		}
	}

	if(!inserted) {
		ordered_children.push_back(value);
	}

	return *v[index];
}
コード例 #20
0
ファイル: config.cpp プロジェクト: PoignardAzur/wesnoth
void config::append_attributes(const config &cfg)
{
	check_valid(cfg);
	for (const attribute &v : cfg.values) {
		values[v.first] = v.second;
	}
}
コード例 #21
0
ファイル: config.cpp プロジェクト: PoignardAzur/wesnoth
config &config::find_child(const std::string &key, const std::string &name,
	const std::string &value)
{
	check_valid();

	const child_map::iterator i = children.find(key);
	if(i == children.end()) {
		DBG_CF << "Key »" << name << "« value »" << value
				<< "« pair not found as child of key »" << key << "«.\n";

		return invalid;
	}

	const child_list::iterator j = std::find_if(i->second.begin(),
	                                            i->second.end(),
	                                            config_has_value(name,value));
	if(j != i->second.end()) {
		return **j;
	} else {
		DBG_CF << "Key »" << name << "« value »" << value
				<< "« pair not found as child of key »" << key << "«.\n";

		return invalid;
	}
}
コード例 #22
0
void get_neighbors(const int idx, 
				   const int imgHeight, const int imgWidth,
			       int *p_neighbors, int *pnum_neighbors)
{
	int pn_idx;
	int nb_row_idx, nb_col_idx;
	int row_idx, col_idx;
	pixel2idx(idx, imgHeight, &row_idx, &col_idx);
	(*pnum_neighbors) = 0;
	int row_delta, col_delta;
	for(row_delta =-1; row_delta<=1; row_delta++)
	{
		for(col_delta=-1; col_delta<=1; col_delta++)
		{
			nb_row_idx = row_idx+row_delta;
			nb_col_idx = col_idx+col_delta;
			if(check_valid(nb_row_idx, nb_col_idx,
				           row_idx, col_idx,
						   imgHeight, imgWidth))
			{
				idx2pixel(nb_row_idx, nb_col_idx, imgHeight, &pn_idx);
				p_neighbors[(*pnum_neighbors)] = pn_idx;
				(*pnum_neighbors)++;
			}
		}
	}
}
コード例 #23
0
ファイル: config.cpp プロジェクト: Coffee--/wesnoth-old
void config::append_children(const config &cfg)
{
	check_valid(cfg);

	BOOST_FOREACH(const any_child &value, cfg.all_children_range()) {
		add_child(value.key, value.cfg);
	}
}
コード例 #24
0
ファイル: config.cpp プロジェクト: niegenug/wesnoth
void config::append_children(const config &cfg, const std::string& key)
{
	check_valid(cfg);

	BOOST_FOREACH(const config &value, cfg.child_range(key)) {
		add_child(key, value);
	}
}
コード例 #25
0
ファイル: config.cpp プロジェクト: PoignardAzur/wesnoth
void config::append_children(const config &cfg, const std::string& key)
{
	check_valid(cfg);

	for (const config &value : cfg.child_range(key)) {
		add_child(key, value);
	}
}
コード例 #26
0
ファイル: config.cpp プロジェクト: PoignardAzur/wesnoth
void config::append_children(const config &cfg)
{
	check_valid(cfg);

	for (const any_child &value : cfg.all_children_range()) {
		add_child(value.key, value.cfg);
	}
}
コード例 #27
0
ファイル: config.cpp プロジェクト: PoignardAzur/wesnoth
void config::swap(config& cfg)
{
	check_valid(cfg);

	values.swap(cfg.values);
	children.swap(cfg.children);
	ordered_children.swap(cfg.ordered_children);
}
コード例 #28
0
ファイル: config.cpp プロジェクト: PoignardAzur/wesnoth
std::string config::debug() const
{
	check_valid();

	std::ostringstream outstream;
	outstream << *this;
	return outstream.str();
}
コード例 #29
0
ファイル: config.cpp プロジェクト: PoignardAzur/wesnoth
config config::get_diff(const config& c) const
{
	check_valid(c);

	config res;
	get_diff(c, res);
	return res;
}
コード例 #30
0
ファイル: shader.cpp プロジェクト: XanClic/dake
void dake::gl::shader::source(const char *src)
{
    check_valid();

    name = "unknown";

    glShaderSource(id, 1, const_cast<const GLchar **>(&src), nullptr);
}