Exemple #1
0
int eval(linkedlist *expr, double *val) {

    int i;
    stack_t stk; /* operator stack */

    stack_init(&stk);

    for(i = 0; i < linkedlist_size(expr); i++) {
        token_t *t = linkedlist_get(expr, i);
        token_t *n2, *n1, *res;

        /* if number, push to stack: */
        if (t->type == TOKEN_NUMBER) {
            stack_push(&stk, t);
            continue;
        }

        /* all operators in the postfix expression are binary. */
        if (stack_size(&stk) < 2) {
            while(!stack_isEmpty(&stk))
                free(stack_pop(&stk));
            /* free the remaining tokens in expr: */
            while(i < linkedlist_size(expr))
                free(linkedlist_get(expr, i++));
            return -1; /* error. */
        }
        n2 = stack_pop(&stk);
        n1 = stack_pop(&stk);

        res = malloc(sizeof(token_t));
        res->type = TOKEN_NUMBER;
        res->op   = 0;
        res->num  = do_op(n1->num, n2->num, t->op);
        stack_push(&stk, res);

        free(n2);
        free(n1);
        free(t);
    }

    if (stack_size(&stk) != 1) {
        while(!stack_isEmpty(&stk)) {
            free(stack_pop(&stk));
        }
        return -1; /* error. */
    }

    *val = ((token_t *) stack_peek(&stk))->num;
    free(stack_pop(&stk));
    return 0;

}
Exemple #2
0
/*
 * prints the bits from the linked list as chars
 * takes chars from stream as needed with extend list
 * until the file reaches EOF.
 * prints the new eof mapping as well
 * fills out the rest of the bits with 0's
 */
void printbits(huffmantree* tree,FILE* stream,FILE* out){
	char** t=table(tree);
	linkedlist* list=treebits(tree,t);
	int counter=0;
	int maxcharlength=(tree->size+1)/2;
	int sum=0;
	int i=0;
	int eof=0;
	while(linkedlist_size(list)){
		//printf("%d\n",linkedlist_size(list));
		if(linkedlist_size(list)<maxcharlength){
			if(maxcharlength>CHAR_BIT) extendlist(list,t,maxcharlength/CHAR_BIT,stream);
			else extendlist(list,t,maxcharlength,stream);
		}
		if(feof(stream)&&!eof){
			extendlistwitheof(list,t);
			eof=1;
		}
		char* frontbit=(char*)linkedlist_rmfront(list);
		sum=sum*2+(*frontbit-'0');
		free(frontbit);
		counter++;
		if (counter%CHAR_BIT==0){
			fprintf(out,"%c",sum);
			sum=0;
		}
	}
	i=0;
	while(counter%CHAR_BIT!=0){
		i++;
		sum=sum*2;
		counter++;
	}
	if(i)fprintf(out,"%c",sum);
	linkedlist_free(list);
	free_table(t);
}
Exemple #3
0
static void work(_lazyworker_t* worker)
{
	_unit* unit = NULL;
    static struct timespec time_wait;

    for(;worker->threadStatus == eThreadStatus_RUNNING;)
    {
        pthread_mutex_lock(&worker->mutex);
        for(;;)
        {
            int que_size = linkedlist_size(worker->sth_todo);
            if(que_size > 0)
            {
                break;
            }
            pthread_cond_wait(&worker->cond, &worker->mutex);
            if(worker->threadStatus == eThreadStatus_CLOSED)
            {
                pthread_mutex_unlock(&worker->mutex);
                pthread_exit((void*) NULL);
            }
        }

        fprintf(stderr, "who wakes me up?");
        struct timeval current;
        gettimeofday(&current, NULL);
        unit = (_unit*)linkedlist_get(worker->sth_todo, 0);
        if(current.tv_sec - unit->t.tv_sec < 1)
        {
            // Be lazy
            time_wait.tv_sec =  unit->t.tv_sec + worker->time_wait.tv_sec;
            time_wait.tv_nsec = unit->t.tv_usec * 1000 + worker->time_wait.tv_nsec;
            fprintf(stderr, "I am too lazy to work, I'll be back in 1 minute. OK?");
            pthread_cond_timedwait(&worker->cond, &worker->mutex, &time_wait);
        }
        else
        {
            unit = (_unit*)linkedlist_remove_at(worker->sth_todo, 0);
            free(unit);
            if(worker->fp != NULL)
            {
            	fprintf(stderr, "OK, now I am ready to work, what's on your mind?\n");
            	worker->fp(worker->args);
            }
        }
        pthread_mutex_unlock(&worker->mutex);
    } // for
}
Exemple #4
0
void lazyworker_invokeLater(lazyworker lazy)
{
    if(lazy == NULL)
    {
        fprintf(stderr, "NULL\n");
        return;
    }
    _lazyworker_t* worker = (_lazyworker_t*) lazy;
    pthread_mutex_lock(&worker->mutex);

    _unit* u = (_unit*) malloc(sizeof(_unit));
    gettimeofday(&u->t, NULL);
    linkedlist_add(worker->sth_todo, u);
    int size;
    size = linkedlist_size(worker->sth_todo);
    if(size > 1)
    {
        _unit* unit = (_unit*)linkedlist_remove_at(worker->sth_todo, 0);
        free(unit);
    }
    pthread_cond_broadcast(&worker->cond);
    pthread_mutex_unlock(&worker->mutex);
    return;
}
Exemple #5
0
int
symexpand(struct idl *idl, struct sym *sym)
{
	char *key;
	struct sym *mem;
	iter_t iter;

	if (IS_EXPANDED(sym)) {
		return 0;
	}
	sym->flags |= FLAGS_EXPANDED;
	sym->orig = sym;

	if (IS_INTERFACE(sym)) {
		const char *pd = hashmap_get(&sym->attrs, "pointer_default");
		idl->ptr_default = PTR_TYPE_UNIQUE;
		if (pd) {
			if (strcmp(pd, "ptr") == 0) {
				idl->ptr_default = PTR_TYPE_PTR;
			} else if (strcmp(pd, "ref") == 0) {
				idl->ptr_default = PTR_TYPE_REF;
			}
		}
	} else if (IS_ENUM(sym)) {
		char buf[16];
		int val = 0;

		linkedlist_iterate(&sym->mems, &iter);
		while ((mem = linkedlist_next(&sym->mems, &iter))) {
			mem->flags = FLAGS_CONST | FLAGS_PRIMATIVE;
			if (mem->value) {
				val = strtoul(mem->value, NULL, 0);
			}
			sprintf(buf, "%d", val++);
			mem->value = dupstr(buf, idl->al);
		}
	} else if (sym->ptr) {
		if (hashmap_get(&sym->attrs, "unique")) {
			sym->ptr_type = PTR_TYPE_UNIQUE;
		} else if (hashmap_get(&sym->attrs, "ptr")) {
			sym->ptr_type = PTR_TYPE_PTR;
		} else if (hashmap_get(&sym->attrs, "ref")) {
			sym->ptr_type = PTR_TYPE_REF;
		} else if (IS_PARAMETER(sym) || IS_OPERATION(sym)) {
			sym->ptr_type = PTR_TYPE_REF;
		} else {
			sym->ptr_type = idl->ptr_default;
		}
	}

	/* If the symbol is typedef'd add it to the table using the
	 * typedef'd name too
	 */
	if (IS_TYPEDEFD(sym) && sym->name) {
		if (IS_ENUM(sym) && (mem = hashmap_get(idl->syms, sym->idl_type))) {
			mem->noemit = 1; /* supress redundant enum */
		}
		key = sym->name;
	} else {
		key = sym->idl_type;
	}
	if (hashmap_get(idl->syms, key) == NULL) {
		if (hashmap_put(idl->syms, key, sym) == -1) {
			AMSG("");
		}
	}

	/* If the symbol has members it is already expanded
	 */
	if (linkedlist_size(&sym->mems) == 0) {
		struct sym *s = symlook(idl, sym->idl_type);
		if (s) {
			sym->interface = s->interface;
			linkedlist_iterate(&s->mems, &iter);
			while ((mem = linkedlist_next(&s->mems, &iter))) {
				struct sym *cpy = symnew(idl->al);
				symcopy(mem, cpy, idl->al);
				linkedlist_add(&sym->mems, cpy);
			}
		} else {
			AMSG("");
			return -1;
		}
	}

	sym->id = idl->symid++;

	/* Perform expansion recursively on all symbols
	 */
	linkedlist_iterate(&sym->mems, &iter);
	while ((mem = linkedlist_next(&sym->mems, &iter))) {
		symexpand(idl, mem);
		mem->parent = sym;
	}

	return 0;
}
int
LinkedlistExercise(int verbose, struct cfg *cfg, char *args[])
{
    int rate, i, n = 0, idx;
	char *str;
    struct linkedlist *l = linkedlist_new(EXERCISE_MED_COUNT, NULL);

	cfg = NULL; args[0] = NULL;

	if (l == NULL) {
		AMSG("");
		return -1;
	}

	rate = EXERCISE_R0;
	for (i = 0; i < EXERCISE_MED_COUNT; i++) {
		if (i == EXERCISE_MED_P1) {
			rate = EXERCISE_R1;
		} else if (i == EXERCISE_MED_P2) {
			rate = EXERCISE_R2;
		} else if (i == EXERCISE_MED_P3) {
			rate = EXERCISE_R3;
		}

		if (rand() % 10 < rate) {
			idx = 0;
	        str = malloc(8);
	        sprintf(str, "%07d", n++);
			if (rand() % 5) {
				idx = linkedlist_size(l);
				if (idx) {
					idx = rand() % idx;
				}
			}
	        if (linkedlist_insert(l, idx, str) == -1) {
				PMNO(errno);
				return -1;
			}
			tcase_printf(verbose, "INSERT: %s size now %d\n", str, linkedlist_size(l));
		} else {
			if (linkedlist_is_empty(l)) {
				tcase_printf(verbose, "EMPTY\n");
			} else {
				idx = rand() % linkedlist_size(l);
		        str = linkedlist_get(l, idx);
				if (linkedlist_remove_data(l, str) == NULL) {
					PMNO(errno);
					return -1;
				}
				if ((idx % 10) == 0) {
					unsigned int count = 0;
					iter_t iter;

					linkedlist_iterate(l, &iter);
					while (linkedlist_next(l, &iter)) {
						count++;
					}
					if (count != linkedlist_size(l)) {
						PMSG("count=%u,linkedlist_size=%u\n", count, linkedlist_size(l));
						return -1;
					}
				}
				if (str) {
	    	    	tcase_printf(verbose, "REMOVE: %s %d\n", str, linkedlist_size(l));
			       	free(str);
				} else {
					PMSG("remove failure");
					return -1;
				}
			}
		}
    }
    linkedlist_del(l, allocator_free, NULL);

    return 0;
}
Exemple #7
0
BOOLEAN KnOSReadQueue(KnQueue_t queue, void* msg, U16BIT msg_size, U16BIT timeout)
{
	S_;
#ifdef _SW_Q_
	BOOLEAN flag = FALSE;
	knQueue_t* que = (knQueue_t*) queue;

	for(;;)
	{
		int size;
		KnOSMutexLock(que->lock);
		size = linkedlist_size(que->queue);
		KnOSMutexUnlock(que->lock);
		if(size > 0)
		{
			M_("size : %d", size);
			break;
		}
		else
		{
			if(KN_INFINITE != timeout)
			{
				if(flag == TRUE)
				{
					return FALSE;
				}
			}
			KnOSSemaphoreWaitTimeout(que->event, timeout);
			flag = TRUE;
		}
	}
	KnOSMutexLock(que->lock);
	void* data_p = linkedlist_remove_at(que->queue, 0);
//	KASSERT(data_p);
	if(data_p == NULL)
	{
		KnOSMutexUnlock(que->lock);
		return FALSE;
	}
	KnOSMutexUnlock(que->lock);
	KnMemcpy(msg, data_p, msg_size);
	return TRUE;


#else
	int flag;
	if(timeout == KN_INFINITE)
	{
		Q_;
		flag = 0;
	}
	else
	{
		Q_;
		flag = IPC_NOWAIT;
	}
	knQueue_t* que = (knQueue_t*) queue;
	Q_;
	KnOSMutexLock(que->lock);
	Q_;
	int ret= msgrcv(que->fd,(void *)msg, msg_size, 0, flag | MSG_NOERROR);
	Q_;
	KnOSMutexUnlock(que->lock);
	if(ret != -1)
	{
		Q_;
		return TRUE;
	}
	Q_;
	return FALSE;
#endif
}