Ejemplo n.º 1
0
void heap_stat(heapstat_t* used, heapstat_t* free) {
    HEAP_VALIDATE;

    used->size = 0;
    used->count = 0;
    free->size = 0;
    free->count = 0;

    size_t offset = 0;
    while (offset < heap_size) {
        header_t* h = offset_header(heap, offset);

        if (is_type(h, HT_FREE)) {
            free->size += h->e.size;
            free->count++;
        } else if (is_type(h, HT_USED) || is_type(h, HT_PROTECTED)) {
            used->size += h->e.size;
            used->count++;
        } else {
            consout("Heap corrupted (magic)\n");
            HEAP_EXIT;
        }
        offset += h->e.size;
    }
}
Ejemplo n.º 2
0
void heap_validate(const char* file, const int lineno) {
    //	consout("%04x !\n", context->programCounter);
    size_t used_m = 0;
    size_t free_m = 0;

    size_t free_l = 0;

    // Traverse through heap memory:
    size_t offset = 0;
    while (offset < heap_size) {
        header_t* h = offset_header(heap, offset);

        if (is_type(h, HT_FREE)) {
            free_m += h->e.size;
        } else if (is_type(h, HT_USED) || is_type(h, HT_PROTECTED)) {
            used_m += h->e.size;
        } else {
            consout("Heap corrupted (magic)\n");
            heap_dump();
            heap_exit(file, lineno);
        }

        // Advance pointer using size of element:
        offset += h->e.size;
    }

    if (used_m + free_m != heap_size) {
        consout("Heap corrupted (size)\n");
        heap_exit(file, lineno);
    }

    // Validate free list:
    if (free_list != NULL) {
        header_t* h = free_list;
        header_t* prev = NULL;

        free_l = 0;
        while (h != NULL) {
            if (is_type(h, HT_FREE)) {
                free_l += h->e.size;
            } else {
                consout("Heap corrupted (free magic)\n");
                heap_exit(file, lineno);
            }
            if (prev != NULL && h < prev) {
                consout("Heap corrupted (free list order)\n");
                heap_exit(file, lineno);
            }
            prev = h;
            h = h->e.next;
        }
    }

    if (free_l != free_m) {
        consout("Heap corrupted (free size)\n");
        heap_exit(file, lineno);
    }
}
Ejemplo n.º 3
0
void print_object(pointer P, symbol_table* table)
{
    FILE* out = stdout;
    if(P == NIL)
    {
        fputs("NIL", out);
        return;
    }

    switch(get_type_id(P))
    {
    case DT_Pair:
        if(is_type(pair_car(P), DT_Pair))
        {
            putc('(', out);
            print_object(pair_car(P), table);
            putc(')', out);
        }
        else
            print_object(pair_car(P), table);

        putc(' ', out);
        if(pair_cdr(P) != NIL)
        {
            if(!is_type(pair_cdr(P), DT_Pair))
                fputs(". ", out);

            print_object(pair_cdr(P), table);
        }
        break;
    case DT_Symbol:
        fputs(string_from_symbol(table, *get_symbol(P)), out);
        break;
    case DT_Int:
        fprintf(out, "%d", get_int(P));
        break;
    case DT_Real:
        fprintf(out, "%f", get_real(P));
        break;
    case DT_String:
        fputs(get_string(P), out);
        break;
    case DT_Char:
        putc(get_char(P), out);
        break;
    case DT_TypeInfo:
        print_typeinfo(P, table, out);
        break;
    case DT_Invalid:
        fputs("#INVALID#", out);
        break;
    case DT_Any:
        fputs("#ANY#", out);
        break;
    }
}
Ejemplo n.º 4
0
void heap_sweep(unsigned int markValue) {
    HEAP_VALIDATE;

    // TODO Clear free list and rebuild during this sweep ?

    header_t* h = heap;
    size_t offset = 0;
    // previous is != NULL, if the previous was free entry:
    header_t* previous = NULL;

    while (offset < heap_size) {
        h = offset_header(heap, offset);
        if (is_type(h, HT_FREE)) {
            previous = h;
            offset += h->e.size;
        } else if (is_type(h, HT_USED)) {
            if (h->e.mark != markValue) {
                //				consoutli("FREE: %p, mark = %d\n", h, h->e.mark);
                //--------------------------
                // Garbage collect this one:
                //--------------------------
                size_t previousSize = 0;
                if (previous != NULL) {
                    // h shall be merged with previous, remember the size of previous:
                    previousSize = previous->e.size;
                }

                // This call might merge with previous and next element on heap:
                heap_free(h);

                if (previous != NULL) {
                    // h might have been merged with previous; don't count previous size twice
                    // (previous->e.size might have been increased with the size of h during the merge):
                    offset += previous->e.size - previousSize;
                    // don't change previous
                } else {
                    offset += h->e.size;
                    previous = h;
                }
            } else {
                offset += h->e.size;
                previous = NULL;
            }
        } else if (is_type(h, HT_PROTECTED)) {
            previous = NULL;
            offset += h->e.size;
        } else {
            consout("Heap corrupted (magic)\n");
            HEAP_EXIT;
        }
    }

    HEAP_VALIDATE;
}
Ejemplo n.º 5
0
void pre_erase_term(Term* term)
{
    // If this term declares a Type, then clear the Type.declaringTerm pointer,
    // before it becomes invalid.
    if (is_type(term) && as_type(term_value(term))->declaringTerm == term)
        as_type(term_value(term))->declaringTerm = NULL;
}
Ejemplo n.º 6
0
int	get_type(const char *format, int i, t_a *arg)
{
	arg->type = format[i];
	if (is_type(format[i]))
		return (i + 1);
	return (i);
}
Ejemplo n.º 7
0
Archivo: print.cpp Proyecto: shaurz/amp
void print(Value x)
{
	if (is_nil(x))
		prints("nil");
	else if (is_eof(x))
		printf("#eof");
	else if (is_fixnum(x))
		printf("%d", as_fixnum(x));
	else if (is_bool(x))
		printf("%s", as_bool(x) ? "true" : "false");
	else if (is_char(x))
		printf("'%c'", as_char(x));
	else if (is_pair(x))
		print_list(x);
	else if (is_symbol(x))
		prints(as_symbol(x)->value);
	else if (is_string(x))
		print_string(as_string(x));
	else if (is_procedure(x))
		printf("#<procedure %s>", as_procedure(x)->name->value);
	else if (is_module(x))
		printf("#<module>");
	else if (is_type(x))
		printf("#<type %s>", as_type(x)->name->value);
	else if (is_ptr(x))
		printf("#<object %p>", as_ptr(x));
	else if (is_undefined(x))
		printf("#undefined");
	else
		printf("#ufo");
}
Ejemplo n.º 8
0
    PExprAST
    Parser::parse_function()
    {
        get_next_token(); // eat the Define token

        if (!is_type(current_tok))
            throw ParserException(ParserExceptionType::ExpectedType);

        PPrototypeAST prototype = parse_prototype();

        if (current_tok != Token::StartContent)
            throw ParserException(ParserExceptionType::ExpectedStartContent);
        get_next_token(); // eat '['

        InstList imp;
        while (current_tok != Token::EndContent)
        {
            PExprAST inst = parse_expr();

            if (current_tok != Token::EndInstr)
                throw ParserException(ParserExceptionType::ExpectedEndOfInstr);
            get_next_token(); // eat ':'

            imp.push_back(inst);
        }
        get_next_token(); // eat ']'

        return PExprAST(new FunctionAST(prototype, imp));
    }
Ejemplo n.º 9
0
Type* Type_cast_specializeType(Term* caller)
{
    if (is_value(caller->input(0)) && is_type(caller->input(0)))
        return as_type(term_value(caller->input(0)));

    return NULL;
}
Ejemplo n.º 10
0
void detail::core_terminal_t::reset()
{
	if (is_type(OUTPUT))
		set_state(STATE_OUT);
	else
		set_state(STATE_INP_ACTIVE);
}
Ejemplo n.º 11
0
    PPrototypeAST
    Parser::parse_prototype()
    {
        Type return_type = get_type(current_tok);

        get_next_token(); // eat type token

        std::string name;
        //We should read identifier or entry point token
        if (current_tok == Token::Identifier)
        {
            name = lex.get_last_token_value<std::string>();
            get_next_token(); // eat identifier
        }
        else if (current_tok == Token::EntryPoint)
        {
            name = "Coq";
            get_next_token(); // eat identifier
        }
        else
            throw ParserException(ParserExceptionType::ExpectedIdentifier);

        if (current_tok != Token::StartArg)
            throw ParserException(ParserExceptionType::ExpectedStartArg);
        get_next_token(); // eat '<'

        //Read types of arguments
        ArgList args;
        if (current_tok != Token::EndArg)
            while (true)
            {
                if (!is_type(current_tok))
                    throw ParserException(ParserExceptionType::ExpectedType);

                Type arg_type = get_type(current_tok);
                get_next_token(); // eat type

                // Users are allowed to do not specify a name
                std::string arg_name;
                if (current_tok == Token::Identifier)
                {
                    name = lex.get_last_token_value<std::string>();
                    get_next_token();
                }

                args.push_back(ArgPair(arg_type, arg_name));

                if (current_tok == Token::EndArg)
                    break;

                if (current_tok != Token::ArgSep)
                    throw ParserException(ParserExceptionType::ExpectedEndOfArg);

                get_next_token(); //eat separator ','
        }
        get_next_token(); // eat '>'

        return PPrototypeAST(new PrototypeAST(return_type, name, args));
    }
Ejemplo n.º 12
0
int heap_used() {
    HEAP_VALIDATE;

    int size = 0;

    size_t offset = 0;
    while (offset < heap_size) {
        header_t* h = offset_header(heap, offset);

        if (is_type(h, HT_USED) || is_type(h, HT_PROTECTED)) {
            size += h->e.size;
        }
        offset += h->e.size;
    }

    return size * sizeof (align_t);
}
Ejemplo n.º 13
0
void append_in_place(pointer P, pointer V)
{
    assert(is_type(P, DT_Pair));
    if(cdr(P) == NIL)
        set_cdr(P, V);
    else
        append_in_place(cdr(P), V);
}
Ejemplo n.º 14
0
int main (int argc, char* argv[]) {

    int a = add_symbol("a", type_int);    
    printf("1 = %d\n", a);
    
    printf("1 = %d\n", add_symbol("b", type_double));

    printf("1 = %d\n", add_symbol("c", type_char));

    printf("0 = %d\n", add_symbol("a", type_char));     // ERRO: já declarada
    
    printf("0 = %d\n", add_symbol("b", type_double));   // ERRO: Já declarada

    printf("1 = %d\n", is_type("a", type_int));

    printf("1 = %d\n", is_type("b", type_int));
}
Ejemplo n.º 15
0
static int json_getattr(const char *path, struct stat *stbuf)
{
  int res = 0, i;
  cJSON *element = NULL;

  memset(stbuf, 0, sizeof(struct stat));

  if(!strcmp(path, "/")) {
    stbuf->st_mode = S_IFDIR | 0755;
    stbuf->st_nlink = 2;
    
    return res;
  }

  for (i = 0; i < size; i++) {
    if (!element_exist(array[i], "name") || !is_type(array[i], "name", cJSON_String))
      continue;

    if (!strcmp(path+1, cJSON_GetObjectItem(array[i], "name")->valuestring)) {
      element = array[i];

      if (element_exist(array[i], "mode") && is_type(array[i], "mode", cJSON_Number))
        stbuf->st_mode = S_IFREG | cJSON_GetObjectItem(array[i], "mode")->valueint;
       else
        stbuf->st_mode = S_IFREG | 0444;

      if (element_exist(array[i], "nlink") && is_type(array[i], "nlink", cJSON_Number))
        stbuf->st_nlink = cJSON_GetObjectItem(array[i], "nlink")->valueint;
      else
        stbuf->st_nlink = 1;

      if (element_exist(array[i], "content") && is_type(array[i], "content", cJSON_String))
        stbuf->st_size = strlen(cJSON_GetObjectItem(array[i], "content")->valuestring);
      else
        stbuf->st_size = 0;

      break;
    }
  }
  
  if (!element)
    res = -ENOENT;

  return res;
}
Ejemplo n.º 16
0
void heap_free(header_t* h) {
    HEAP_VALIDATE;

    if (h == NULL) {
        consout("Element is NULL\n");
        HEAP_EXIT;
    }

    if (!is_type(h, HT_USED) && !is_type(h, HT_PROTECTED)) {
        consout("Magic\n");
        HEAP_EXIT;
    }

    set_type(h, HT_FREE);
    list_insert_ordered(&free_list, h);

    HEAP_VALIDATE;
}
Ejemplo n.º 17
0
void Lex_analyzer::addToken(int tokenType, QString tokenValue){
    TokenType *t = new TokenType;
    Token *tok = new Token;
    switch(tokenType){
    case 1:
        if(is_keyword(tokenValue,t)){
            qDebug() <<"Token" <<tokenValue <<"found in keywords: id=" <<t->id  <<"type=" <<t->type <<endl;
            tok->id=t->id;
            tok->tokclass = "keywords";
            tok->toktype = t->type;
            tok->value = tokenValue;
            tokens->append(tok);
        }else if(is_type(tokenValue,t)){
            qDebug() <<"Token" <<tokenValue <<"found in types: id=" <<t->id  <<"type=" <<t->type <<endl;
            tok->id=t->id;
            tok->tokclass = "types";
            tok->toktype = t->type;
            tok->value = tokenValue;
            tokens->append(tok);
        }else{
            is_id(tokenValue, t);
            qDebug() <<"Token" <<tokenValue <<"found in/inserted to ids: id=" <<t->id  <<"type=" <<t->type <<endl;
            tok->id=t->id;
            tok->tokclass = "ids";
            tok->toktype = t->type;
            tok->value = tokenValue;
            tokens->append(tok);
        }
        break;
    case 2:
        is_const(tokenValue, t);
        qDebug() <<"Token" <<tokenValue <<"found in/inserted to constants: id=" <<t->id  <<"type=" <<t->type <<endl;
        tok->id=t->id;
        tok->tokclass = "constants";
        tok->toktype = t->type;
        tok->value = tokenValue;
        tokens->append(tok);
        break;
    case 3:
    case 4:
        if(is_operator(tokenValue, t)){
            qDebug() <<"Token" <<tokenValue <<"found in operators/separators: id=" <<t->id  <<"type=" <<t->type <<endl;
            tok->id=t->id;
            tok->tokclass = "separators";
            tok->toktype = t->type;
            tok->value = tokenValue;
            tokens->append(tok);
        }else{
            qDebug() <<"Error unknown token type: " <<tokenType  <<endl;
        }
        break;
    default:
        qDebug() <<"Error unknown token type: " <<tokenType  <<endl;
    }
    qDebug() <<"Adding token of type[" <<tokenType <<"] = " <<tokenValue  <<endl;
}
Ejemplo n.º 18
0
void ObjectTypeDB::get_extensions_for_type(const StringName& p_type,List<String> *p_extensions) {

	const StringName *K=NULL;

	while((K=resource_base_extensions.next(K))) {
		StringName cmp = resource_base_extensions[*K];
		if (is_type(cmp,p_type))
			p_extensions->push_back(*K);
	}
}
Ejemplo n.º 19
0
void qz_get_args(qz_state_t* st, qz_obj_t* args, const char* spec, ...)
{
  va_list ap;
  va_start(ap, spec);

  size_t nargs = 0;
  for(const char* s = spec; *s; /**/)
  {
    qz_obj_t* obj = va_arg(ap, qz_obj_t*);

    int type_char = *s++;
    int eval = (*s == '~') ? (s++, 0) : 1;
    int optional = (*s == '?') ? (s++, 1) : 0;

    if(qz_is_pair(*args)) {
      /* pull argument from list and advance */
      *obj = qz_first(*args);
      *args = qz_rest(*args);

      if(eval) {
        /* evaluate argument */
        *obj = qz_eval(st, *obj);
      }
      else {
        /* don't evaluate argument */
        *obj = qz_ref(st, *obj);
      }
      qz_push_safety(st, *obj);
      nargs++;

      /* check argument type */
      if(!is_type(*obj, type_char)) {
        char msg[64];
        sprintf(msg, "expected %s at argument %ld", type_name(type_char), nargs);
        qz_error(st, msg, obj, NULL);
      }
    }
    else if(qz_is_null(*args))  {
      /* allow optional arguments */
      if(optional) {
        *obj = QZ_NONE;
        continue; /* missing optional argument */
      }
      char msg[64];
      sprintf(msg, "missing %s at argument %ld", type_name(type_char), nargs);
      qz_error(st, msg, NULL);
    }
    else {
      qz_error(st, "improper argument list", args, NULL);
    }
  }
  qz_pop_safety(st, nargs);

  va_end(ap);
}
Ejemplo n.º 20
0
    Type* specializeType(Term* caller)
    {
        Term* input = caller->input(0);
        if (input == NULL)
            return &ANY_T;

        if (is_value(input) && is_type(input))
            return as_type(input);

        return &ANY_T;
    }
Ejemplo n.º 21
0
void destroy_list(pointer P)
{
    if(is_type(P, DT_Pair))
    {
        if(pair_car(P) != NIL)
            destroy_list(pair_car(P));
        if(pair_cdr(P) != NIL)
            destroy_list(pair_cdr(P));
    }
    ploy_free(P);
}
Ejemplo n.º 22
0
static void check_tore(type_or_ent tore, void *env)
{
	bool *fine = (bool*)env;

	if (is_type(tore.typ)) {
		*fine &= check_type(tore.typ);
	} else {
		assert(is_entity(tore.ent));
		*fine &= check_entity(tore.ent);
	}
}
Ejemplo n.º 23
0
Type* type_make_specializeType(Term* caller)
{
    Term* input = caller->input(0);
    if (input == NULL)
        return TYPES.any;

    if (is_value(input) && is_type(input))
        return as_type(input);

    return TYPES.any;
}
Ejemplo n.º 24
0
int		count_for_obj(char **str, int i, t_pars *pars)
{
  int		j;

  j = 0;
  while (str && str[i] && is_type(str[i], pars) == -1)
    {
      i++;
      j++;
    }
  return (j);
}
Ejemplo n.º 25
0
bool is_considered_config(Term* term)
{
    if (term == NULL) return false;
    if (has_empty_name(term)) return false;
    if (!is_value(term)) return false;
    if (is_declared_state(term)) return false;
    if (is_hidden(term)) return false;
    if (is_function(term)) return false;
    if (is_type(term)) return false;

    return true;
}
Ejemplo n.º 26
0
bool fits_lookup_type(Term* term, Symbol type)
{
    switch (type) {
        case s_LookupAny:
            return true;
        case s_LookupType:
            return is_type(term);
        case s_LookupFunction:
            return is_function(term);
    }
    internal_error("unknown type in fits_lookup_type");
    return false;
}
Ejemplo n.º 27
0
static int json_read(const char *path, char *buf, size_t size, off_t offset,
                      struct fuse_file_info *fi)
{
  size_t len;
  char *str;

  (void) fi;

  int i;
  cJSON *element;

  for (i = 0; i < size; i++) {
    if (!element_exist(array[i], "name") || !is_type(array[i], "name", cJSON_String))
      continue;

    if (!strcmp(path+1, cJSON_GetObjectItem(array[i], "name")->valuestring)) {
      element = array[i];

      str = (element_exist(array[i], "content") && is_type(array[i], "content", cJSON_String)) ? cJSON_GetObjectItem(array[i], "content")->valuestring : "";

      len = strlen(str);
      
      if (offset < len) {
        if (offset + size > len)
          size = len - offset;
        
        memcpy(buf, str + offset, size);
      } else
       size = 0;

      return size;
    }
  }

  if (!element)
    return -ENOENT;

  return size;
}
Ejemplo n.º 28
0
void ObjectTypeDB::get_inheriters_from( const StringName& p_type,List<StringName> *p_types) {

	OBJTYPE_LOCK;

	const StringName *k=NULL;

	while((k=types.next(k))) {

		if (*k!=p_type && is_type(*k,p_type))
			p_types->push_back(*k);
	}

}
Ejemplo n.º 29
0
std::string generate_cpp_headers(Branch* branch)
{
    std::stringstream out;

    for (int i=0; i < branch->length(); i++) {
        if (branch->get(i) == NULL) continue;

        Term* term = branch->get(i);

        if (is_type(term))
            out << cpp_accessor_for_type(as_type(term_value(term)));
    }

    return out.str();
}
Ejemplo n.º 30
0
Name list_get_parameter_type(caValue* parameter)
{
    if (is_null(parameter))
        return name_Untyped;
    if (is_type(parameter))
        return name_UniformListType;

    if (is_list(parameter)) {
        if ((list_length(parameter) == 2) && is_list(list_get(parameter, 0)))
            return name_StructType;
        else
            return name_AnonStructType;
    }
    return name_Invalid;
}