示例#1
0
static inline void *
_item_buf(const struct varyad *v, size_t i)
{
    if (i) {
        return _head(v) + _size(v, i - 1);
    } else {
        return _head(v);
    }
}
示例#2
0
void *_pktbuf_internal_alloc(size_t size)
{
    _used_t *node = _head(), *old_next, *new_next;

    if ((size == 0) || (size > NG_PKTBUF_SIZE)) {
        return NULL;
    }

    if (node->size == 0) {          /* if head is currently not initialized */
        if (node->next == NULL || (_start_idx(node->next) >= _total_sz(size))) {
            /* if enough space is there */
            node->size = size;  /* just take it */
            node->pkts = 1;

            return _data(node);
        }
        else if (node->next != NULL) {
            /* else go directly to next allocation if it exists */
            node = node->next;
        }
    }

    while ((node->next != NULL)
           /* and if space between current and next allocation is not big enough */
           && ((_start_idx(node->next) - _end_idx(node)) < _total_sz(size))) {
        node = node->next;
    }

    /* jump ahead size of current packet */
    new_next = (_used_t *)(((uint8_t *)node) + __al_total_sz(node));

    if ((((uint8_t *)new_next) + size) > (((uint8_t *)_head()) + NG_PKTBUF_SIZE)) {
        /* new packet does not fit into _pktbuf */
        return NULL;
    }

    old_next = node->next;
    node->next = new_next;
    node->next->next = old_next;

    node = new_next;
    node->size = size;
    node->pkts = 1;

#ifdef DEVELHELP

    if ((_end_idx(node) + 1) > _pktbuf_max_bytes) {
        _pktbuf_max_bytes = _end_idx(node) + 1;
    }

#endif

    return _data(node);
}
示例#3
0
// Mark list
Box *ListBox::tag(Data *d, DataLink *dl)
{
    if (!isEmpty())
    {
	// In a list: mark all children
	_head() = _head()->tag(d, dl);
	_tail() = _tail()->tag(d, dl);
    }

    // The list itself is never marked, since it is not drawn
    return this;
}
示例#4
0
/**
 * If the object has only constant values, it pre-evalutes the object.
 * so that the expression doesn't need to be evaluated repeatedly.
 */
static muse_cell json_share_object_expr( muse_env *env, muse_cell objexpr )
{
	muse_cell listitems = _tail(_head(_tail(objexpr)));

	while ( listitems ) {
		if ( !json_is_constant( env, _next(&listitems) ) )
			return objexpr;
	}

	return muse_eval( env, objexpr, MUSE_FALSE );
}
示例#5
0
/**
 * Returns arr if sub-expresions are not constant.
 * Otherwise creates a constant vector.
 */
static muse_cell json_share_array_expr( muse_env *env, muse_cell arr )
{
	muse_cell items = _tail(arr);

	while ( items ) {
		if ( !json_is_constant( env, _head(items) ) )
			return arr;

		items = _tail(items);
	}

	return muse_eval( env, arr, MUSE_FALSE );
}
示例#6
0
/**
 * Checks whether the given item is a JSON constant.
 */
static muse_boolean json_is_constant( muse_env *env, muse_cell item )
{
	switch ( _cellt(item) ) {
		case MUSE_TEXT_CELL:
		case MUSE_INT_CELL:
		case MUSE_FLOAT_CELL:
		case MUSE_NATIVEFN_CELL:
			return MUSE_TRUE;
		case MUSE_CONS_CELL:
			return _isquote(_head(item));
		default:
			return MUSE_FALSE;
	}
}
示例#7
0
static void json_write_object( muse_port_t p, muse_cell obj )
{
	muse_env *env = p->env;
	muse_cell plist = object_plist( env, obj );
	port_putc( '{', p );
	while ( plist ) {
		muse_cell ht = _next(&plist);
		json_write_string(p, _symname(_head(ht)));
		port_putc(':',p);
		json_write(p,_tail(ht));
		if ( plist )
			port_putc( ',', p );
	}
	port_putc( '}', p );
}
示例#8
0
static void json_write_hash( muse_port_t p, muse_cell obj )
{
	muse_env *env = p->env;
	int sp = _spos();
	muse_cell alist = fn_hashtable_to_alist( env, NULL, _cons(obj,MUSE_NIL) );
	_unwind(sp);
	port_putc( '{', p );
	while ( alist ) {
		muse_cell ht = _next(&alist);
		json_write_string(p, _symname(_head(ht)));
		port_putc(':',p);
		json_write(p,_tail(ht));
		if ( alist )
			port_putc( ',', p );
	}
	port_putc( '}', p );
}
示例#9
0
size_t
varyad_push(struct varyad **v, void *data, size_t size, int realloc)
{
    if (_avail(*v) < size + sizeof(size_t)) {
        if (!realloc) {
            errno = ENOMEM;
            return 0;
        } else {
            struct varyad *const tmp = _realloc(*v);
            if (!tmp) {
                return 0;
            } else {
                *v = tmp;
            }
        }
    }
    const size_t off = (*v)->rank ? _size(*v, (*v)->rank-1) : 0;
    memcpy(_head(*v) + off, data, size);
    _set_size(*v, (*v)->rank++, off + size);
    return (*v)->size;
}
    ListNode* addTwoNumbers(ListNode* list1, ListNode* list2) {
        _clear();
        if (list1 == nullptr || list2 == nullptr) {
            return nullptr;
        }

        int val = list1->val + list2->val;
        ListNode* _head(new ListNode(val % 10));
        list1 = list1->next;
        list2 = list2->next;

        ListNode* ptr = _head;
        while (list1 != nullptr && list2 != nullptr) {
            val = list1->val + list2->val + val / 10;
            ptr->next = new ListNode(val % 10);
            ptr = ptr->next;
            list1 = list1->next;
            list2 = list2->next;
        }

        while (list1 != nullptr) {
            val = list1->val + val / 10;
            ptr->next = new ListNode(val % 10);
            ptr = ptr->next;
            list1 = list1->next;
        }

        while (list2 != nullptr) {
            val = list2->val + val / 10;
            ptr->next = new ListNode(val % 10);
            ptr = ptr->next;
            list2 = list2->next;
        }

        if (val / 10 != 0) {
            ptr->next = new ListNode(1);
        }

        return _head;
    }
示例#11
0
static _used_t *_find(_used_t **prev_ptr, _used_t **node_ptr, const void *ptr)
{
    _used_t *node = _head(), *prev = NULL;

    if (ptr != NULL) {
        while (node != NULL) {
            if (_in_data_range(node, ptr)) {
                *prev_ptr = prev;
                *node_ptr = node;
                return node;
            }

            prev = node;
            node = (_used_t *)node->next;
        }
    }

    *prev_ptr = NULL;
    *node_ptr = NULL;

    return NULL;
}
示例#12
0
// E <- 'd' ('a' 'b' | 'd') 
bool is_E(_iterator &it){
	_head(2);
	if(_is('d') && _L(1) && _is('a') && _is('b') && _C(1) && _is('d') && _R){ return true; }
	return _false; 
}
示例#13
0
// This example is equivalent to is_C: 
// D <- ('a' | '') 'b'
bool is_D(_iterator &it){ 
	_head(2);
	if(_L(1) && _is('a') && _C(1) && true && _R && _is('b')){ return true; }
	return _false; 
}
示例#14
0
// The next example is 
// C <- 'a' 'b' | 'b' 
bool is_C(_iterator &it){ 
	_head(1);
	if(_is('a') && _is('b') && _C(0) && _is('b')){ return true; }
	return _false; 
}
示例#15
0
// The first example of tiny-peg is the following: 
// A <- 'a' 'b' 'c' 'd' 'e'
bool is_A(_iterator &it){ 
	_head(1); 
	if(_is('a') && _is('b') && _is('c') && _is('d') && _is('e')){ return true; }
	return _false; 
}
示例#16
0
// G <- 'a' (G | '')
bool is_G(_iterator &it){ 
	_head(2);
	if(_is('a') && _L(1) && is_G(it) && _C(1) && true && _R){ return true; }
	return _false; 
}
示例#17
0
// This example is already appeared: 
// F <- ('a' | 'b' | ('c' 'd' ('e' | 'f') ) ('g' | 'h') ) 'i' | 'j'
bool is_F(_iterator &it){ 
	_head(4);
	if(_L(1) && _is('a') && _C(1) && _is('b') && _C(1) && _L(2) && _is('c') && _is('d') && _L(3) && _is('e') && _C(3) && _is('f') && _R && _R && _L(2) && _is('g') && _C(2) && _is('h') && _R && _R && _is('i') && _C(0) && _is('j')){ return true; }
	return _false; 
}