Exemplo n.º 1
0
 double get_pow(double x, int n) {
     if (n == 1) return x;
     if (n == 0) return 1;
     double tmp = get_pow(x, n / 2);
     if (n % 2) return tmp * tmp * x;
     else return tmp * tmp;
 }
Exemplo n.º 2
0
static void *ralloc(void *ptr, size_t size, enum m_state state) {
	//LOG("Request for size change to %zd\n", size);
	
	if (size == 0) {
		shee(ptr);
		return NULL;
	}
	if (ptr == NULL) {
		return shalloc(size);
	}

	struct block *b = B_HEAD(ptr);
	unsigned long size_pow = get_pow(B_SIZE + size);
	/* No need for allocation, size would be the same */
	if (b->k_size == size_pow) {
		return ptr;
	}

	void *new_ptr;
	if (state == ZERO) {
		new_ptr = shcalloc(1, size);
	} else {
		new_ptr = shalloc(size);
	}
	/* In case of failure, previous data is lost */
	if (new_ptr == NULL) {
		shee(ptr);
		return NULL;
	}
	/* Previous data must be preserved, if possible */
	memcpy(new_ptr, ptr, MIN(B_DATA_SIZE(b), B_DATA_SIZE(B_HEAD(new_ptr))));
	shee(ptr);

	return new_ptr;
}
Exemplo n.º 3
0
 double pow(double x, int n) {
     double ret = get_pow(x, abs(n));
     if (n < 0) {
         ret = 1 / ret;
     }
     return ret;
 }
double primary()
{
	Token t = ts.get();
	switch (t.kind) {
	case '(':
	{	double d = expression();
		t = ts.get();
		if (t.kind != ')') error("'(' expected");
		return d;
	}
	case sqrtsymb:
        return get_square();
    case powsymb:
        return get_pow();
	case '-':
		return - primary();
    case name:
    {
        t2 = ts.get();
        cin >>
        if(t.kind == '=') return set_value();
        return get_value(t.name);
    }

    case '+':
        return primary();
	case number:
        return t.value;
	default:
		error("primary expected");
	}
}
Exemplo n.º 5
0
static node_t *get_spow(token_stack_t *stack, GError **err)
{
    const token_t *token;
    node_t *node;
    GError *tmp_err = NULL;

    token = token_peak(stack);

    if (!token) {
        set_error(err, "Expected '(', number, constant or function", token);
        return NULL;
    }

    if (token->type == TOK_OPERATOR && token->val.op == '-') {
        g_free(token_pop(stack));
        node = g_malloc(sizeof(node_t));
        node->type = NODE_OPERATOR;
        node->val.op = OP_UMINUS;
        node->left = NULL;
        node->right = get_spow(stack, &tmp_err);
        if (tmp_err)
            g_propagate_error(err, tmp_err);
    } else {
        node = get_pow(stack, &tmp_err);
        if (tmp_err)
            g_propagate_error(err, tmp_err);
    }

    return node;
}
Exemplo n.º 6
0
void *shcalloc(size_t nmemb, size_t size) {
	/* Nothing to do */
	if (nmemb == 0 || size == 0) {
		return NULL;
	}

	return alloc(get_pow(B_SIZE + nmemb*size), ZERO);
}
Exemplo n.º 7
0
void *shalloc(size_t size) {
	/* Nothing to do */
	if (size == 0) {
		return NULL;
	}

	return alloc(get_pow(B_SIZE + size), FREE);
}
Exemplo n.º 8
0
static int init_pool() {
	/* Get current heap break address */
	if ((mp.base_addr = sbrk(0)) == (void *) -1) {
		perror("sbrk");
		return -1;
	}

	/* First block on heap will contain 'avail' array */
	/* First-block initialization for the avail array containing heads of lists with available memory, it has length of (max pool size + 1) */
	/* Logarithm of the space, we can adress */
	mp.max_size = CHAR_BIT*sizeof(void *);
	/* We want to have available pointers to all the memory we can adress */
	mp.init_size = get_pow(B_SIZE + (mp.max_size + 1)*(sizeof(struct avail_head)));
	if (sbrk(pow2(mp.init_size)) == (void *) -1) {
		perror("sbrk");
		return -1;
	}
	mp.pool_size = mp.init_size;

	/* Initialize the first block, which will contain 'avail' array */
	struct block *first_b;
	first_b = (struct block *) mp.base_addr;
	first_b->state = USED;
	first_b->k_size = mp.init_size;

	LOG("--## POOL INIT INFO ##--\n");
	LOG("## Heap memory begins at base_addr = %p\n", mp.base_addr);
	LOG("## Size of the first block is init_size = 2^%lu = %luB\n", mp.init_size, pow2(mp.init_size));
	LOG("## Maximum pool size is max_size = 2^%lu = %luB\n", mp.max_size, -1L);

	/* Init 'avail', array of cyclic linked list */
	mp.avail = (struct avail_head *) B_DATA(first_b);
	int i;
	for (i=0; i <= mp.max_size; i++) {
		mp.avail[i].next = (struct block *) &mp.avail[i];
		mp.avail[i].prev = (struct block *) &mp.avail[i];
	}

	first_b->prev = (struct block *) &mp.avail[first_b->k_size];
	first_b->next = (struct block *) &mp.avail[first_b->k_size];

	//TODO WIP
	int err;
	if ((err = pthread_create(&cleaner_th, NULL, cleaning_work, NULL)) != 0) {
		printf("pthread_create() ERROR %d!\n", err);
		return -1;
	}
	if ((err = pthread_detach(cleaner_th)) != 0) {
		printf("pthread_detach() ERROR %d!\n", err);
		return -1;
	}
	//TODO WIP
	LOG("--## END OF POOL INIT INFO ##--\n");

	return 0;
}
Exemplo n.º 9
0
int parse_sub(char* subnet_string, uint32_t* subnet, uint32_t* subnet_mask)
{

	int valid = 0;
	unsigned int A,B,C,D,E,F,G,H;
	int read_int = sscanf(subnet_string, "%u.%u.%u.%u/%u.%u.%u.%u", &A, &B, &C, &D, &E, &F, &G, &H);
	if(read_int >= 5)
	{
		if( A <= 255 && B <= 255 && C <= 255 && D <= 255)
		{
			unsigned char* sub = (unsigned char*)(subnet);
			unsigned char* msk = (unsigned char*)(subnet_mask);
			
			*( sub ) = (unsigned char)A;
			*( sub + 1 ) = (unsigned char)B;
			*( sub + 2 ) = (unsigned char)C;
			*( sub + 3 ) = (unsigned char)D;

			if(read_int == 5)
			{
				unsigned int mask = E;
				if(mask <= 32)
				{
					int msk_index;
					for(msk_index=0; msk_index*8 < mask; msk_index++)
					{
						int bit_index;
						msk[msk_index] = 0;
						for(bit_index=0; msk_index*8 + bit_index < mask && bit_index < 8; bit_index++)
						{
							msk[msk_index] = msk[msk_index] + get_pow(2, 7-bit_index);
						}
					}
				}
				valid = 1;
			}
			if(read_int == 8)
			{
				if( E <= 255 && F <= 255 && G <= 255 && H <= 255)
				*( msk ) = (unsigned char)E;
				*( msk + 1 ) = (unsigned char)F;
				*( msk + 2 ) = (unsigned char)G;
				*( msk + 3 ) = (unsigned char)H;
				valid = 1;
			}
		}
	}
	if(valid)
	{
		*subnet = (*subnet & *subnet_mask );
	}
	return valid;
}
Exemplo n.º 10
0
void	aff_nb(int nb, int stock)
{
  char	c;
  int	pow;

  pow = get_pow(nb);
  while (pow != 1)
    {
      stock = nb / pow;
      c = stock + 48;
      nb = nb - (stock * pow);
      pow = pow / 10;
      my_putchar(c);
      is_easy(nb);
    }
}
Exemplo n.º 11
0
/**
 * 項
 *
 * @param[in] calc calcinfo構造体
 * @return 値
 */
static dbl
term(calcinfo *calc)
{
    dbl x = 0.0, y = 0.0; /* 値 */

    dbglog("start");

    if (is_error(calc))
        return EX_ERROR;

    x = factor(calc);
    dbglog(calc->fmt, x);

    while (true) {
        if (calc->ch == '*') {
            readch(calc);
            x *= factor(calc);
        } else if (calc->ch == '/') {
            readch(calc);
            y = factor(calc);
            if (y == 0) { /* ゼロ除算エラー */
                set_errorcode(calc, E_DIVBYZERO);
                return EX_ERROR;
            }
            x /= y;
        } else if (calc->ch == '^') {
            readch(calc);
            y = factor(calc);
            x = get_pow(calc, x, y);
        } else {
            break;
        }
    }
    dbglog(calc->fmt, x);
    return x;
}
Exemplo n.º 12
0
static void *alloc(unsigned long size_pow, enum m_state state) {
	/* Pool not initialized */
	if (mp.base_addr == NULL) {
		/* Try to initialize it */
		if (init_pool() == -1) {
			errno = ENOMEM;
			return NULL;
		}
	}

//	LOG("BEFORE ALLOC\n");
//	print_avail_map();

	/* Size in power of 2, which needs to be reserved */
	//LOG("Request for 2^%lu = %luB\n", size_pow, pow2(size_pow));
	/* Size of the first suitable block which is available (in pow of 2) */
	unsigned long j;
	/* Find the value of 'j' in available memory space if possible */
	//LOG("--## FINDING J ##--\n");
	for (j=size_pow; j < mp.pool_size; j++) {
		if (!list_empty(&mp.avail[j])) {
			break;
		}
		//LOG("j=%lu not suitable\n", j);
	}
	/* Do we need to enlarge the pool? - by making buddy for existing largest block */
	//LOG("--## ENLARGING ##--\n");
	while (list_empty(&mp.avail[j])) {
		//LOG("j=%lu <= pool_size=%lu <= max=%lu\n", j, mp.pool_size, mp.max_size);
		/* Cannot adress this amount of memory */
		if (get_pow((unsigned long)mp.base_addr) + size_pow >= mp.max_size) {
		//TODO ktery pouzit?
		//if (mp.max_size >= mp.max_size) {
			LOG("Maximum size reached!\n");
			errno = ENOMEM;
			return NULL;
		}
		//LOG("Enlarging the pool.\n");
		void *new_addr;
		if ((new_addr = sbrk(pow2(mp.pool_size))) == (void *)-1) {
			LOG("sbrk pool-enlarge error!\n");
			errno = ENOMEM;
			return NULL;
		}
		//TODO bez prepsani zpusobuje neporadek s valgrindem
		//memset(new_addr, 0, pow2(mp.pool_size));

		/* Pool was enlarged, we have twice as much space */
		mp.pool_size++;
		/* New memory block, buddy for previous block, will live in this space */
		//LOG("new_addr = %p\n", new_addr);
		struct block *nb = (struct block *) new_addr;
		nb->state = FREE;
		nb->k_size = mp.pool_size - 1;
		//LOG("k_size = %lu\n", nb->k_size);
	
		/* Avail array must be edited, we've got new free block of size 2^(nb->k_size) */
		struct block *p;
		p = mp.avail[nb->k_size].next;
		//LOG("p point %p head to %p\n", p, &mp.avail[nb->k_size]);
		nb->next = p;
		p->prev = nb;
		nb->prev = (struct block *) &mp.avail[nb->k_size];
		mp.avail[nb->k_size].next = nb;
	}

	/* We now have the 'j' value set */
	//LOG("--## REMOVING BLOCK FROM AVAIL ARRAY ##--\n");
	/* Remove this block from avail array */
	struct block *l, *p;
	l = mp.avail[j].prev;
	//LOG("L position = %p\n", l);
	p = l->prev;
	mp.avail[j].prev = p;
	p->next = (struct block *) &mp.avail[j];
	enum m_state block_state = l->state;
	l->state = USED;

	/* Now we need to divide the block if space in it is too large */
	//LOG("--## DIVIDING BLOCK ##--\n");
	while (j != size_pow) {
		//LOG("divination for j=%lu\n", j);
		j--;
		p = (struct block *)((unsigned long)l + pow2(j));
		//LOG("pointering to %p\n", p);
		p->state = FREE;
		p->k_size = j;
		p->prev = (struct block *) &mp.avail[j];
		p->next = (struct block *) &mp.avail[j];
		/* Add this block into avail array */
		mp.avail[j].prev = p;
		mp.avail[j].next = p;
	}
	l->k_size = size_pow;
	
	/* Does the memory need to be cleared? */
	if (state == ZERO && block_state != ZERO) {
		memset(B_DATA(l), 0, B_DATA_SIZE(l));
	}

//	LOG("AFTER ALLOC\n");
//	print_avail_map();
//	LOG("\n");

	return B_DATA(l);
}
Exemplo n.º 13
0
/* 
 * implement a simple function to get positive powers of positive integers so we don't have to mess with math.h 
 * all we really need are powers of 2 for calculating netmask
 * This is only called a couple of times, so speed isn't an issue either
 */
static unsigned long get_pow(unsigned long base, unsigned long pow)
{
	unsigned long ret = pow == 0 ? 1 : base*get_pow(base, pow-1);
	return ret;
}
Exemplo n.º 14
0
static void print_bandwidth_args( struct ipt_bandwidth_info* info )
{
	if(info->cmp == BANDWIDTH_CHECK)
	{
		if(info->check_type == BANDWIDTH_CHECK_NOSWAP)
		{
			printf("--bcheck ");
		}
		else
		{
			printf("--bcheck_with_src_dst_swap ");
		}
	}
	printf("--id %s ", info->id);



	if(info->cmp != BANDWIDTH_CHECK)
	{
		/* determine current time in seconds since epoch, with offset for current timezone */
		int minuteswest = get_minutes_west();
		time_t now;
		time(&now);
		now = now - (minuteswest*60);

		if(info->type == BANDWIDTH_COMBINED)
		{
			printf("--type combined ");
		}
		if(info->type == BANDWIDTH_INDIVIDUAL_SRC)
		{
			printf("--type individual_src ");
		}
		if(info->type == BANDWIDTH_INDIVIDUAL_DST)
		{
			printf("--type individual_dst ");
		}
		if(info->type == BANDWIDTH_INDIVIDUAL_LOCAL)
		{
			printf("--type individual_local ");
		}
		if(info->type == BANDWIDTH_INDIVIDUAL_REMOTE)
		{
			printf("--type individual_remote ");
		}


		if(info->local_subnet != 0)
		{
			unsigned char* sub = (unsigned char*)(&(info->local_subnet));
			int msk_bits=0;
			int pow=0;
			for(pow=0; pow<32; pow++)
			{
				uint32_t test = get_pow(2, pow);
				msk_bits = ( (info->local_subnet_mask & test) == test) ? msk_bits+1 : msk_bits;
			}
			printf("--subnet %u.%u.%u.%u/%u ", (unsigned char)sub[0], (unsigned char)sub[1], (unsigned char)sub[2], (unsigned char)sub[3], msk_bits); 
		}
		if(info->cmp == BANDWIDTH_GT)
		{
			printf("--greater_than %lld ", info->bandwidth_cutoff);
		}
		if(info->cmp == BANDWIDTH_LT)
		{
			printf("--less_than %lld ", info->bandwidth_cutoff);
		}
		if (info->type == BANDWIDTH_COMBINED) /* too much data to print for multi types, have to use socket to get/set data */
		{
			if( info->reset_interval != BANDWIDTH_NEVER && info->next_reset != 0 && info->next_reset < now)
			{
				/* 
				 * current bandwidth only gets reset when first packet after reset interval arrives, so output
				 * zero if we're already past interval, but no packets have arrived 
				 */
				printf("--current_bandwidth 0 ");
			}
			else 
			{
				printf("--current_bandwidth %lld ", info->current_bandwidth);
			}
		}
		if(info->reset_is_constant_interval)
		{
			printf("--reset_interval %ld ", info->reset_interval);
		}
		else
		{
			if(info->reset_interval == BANDWIDTH_MINUTE)
			{
				printf("--reset_interval minute ");
			}
			else if(info->reset_interval == BANDWIDTH_HOUR)
			{
				printf("--reset_interval hour ");
			}
			else if(info->reset_interval == BANDWIDTH_DAY)
			{
				printf("--reset_interval day ");
			}
			else if(info->reset_interval == BANDWIDTH_WEEK)
			{
				printf("--reset_interval week ");
			}
			else if(info->reset_interval == BANDWIDTH_MONTH)
			{
				printf("--reset_interval month ");
			}
		}
		if(info->reset_time > 0)
		{
			printf("--reset_time %ld ", info->reset_time);
		}
		if(info->num_intervals_to_save > 0)
		{
			printf("--intervals_to_save %d ", info->num_intervals_to_save);
		}
	}
}