示例#1
0
int netsys_queue_add(struct nqueue *q, void *elem)
{
    int code;
    unsigned long new_end;

    new_end = q->table_end + 1;
    if (new_end == q->table_size) new_end = 0;

    if (new_end == q->table_start) {
	/* We have to resize */
	struct nqueue q1;
	int n;
	
	code = netsys_queue_init(&q1, q->table_size * 2);
	if (code != 0) return code;
	
	if (q->table_start <= q->table_end) {
	    n = q->table_end - q->table_start;
	    memcpy(q1.table, 
		   q->table + q->table_start,
		   n * sizeof(void *));
	}
	else {
	    int n1;
	    n1 = q->table_size - q->table_start;
	    memcpy(q1.table, 
		   q->table + q->table_start,
		   n1 * sizeof(void *));
	    memcpy(q1.table + n1,
		   q->table,
		   q->table_end * sizeof(void *));
	    n = n1 + q->table_end;
	}

	free(q->table);
	q->table = q1.table;
	q->table_size = q1.table_size;
	q->table_start = 0;
	q->table_end = n;
	new_end = n+1;
    }

    *(q->table + q->table_end) = elem;
    q->table_end = new_end;

    return 0;
}
static int prep_stat_queue(void)
{
    int code, need_init;
    need_init=0;
    if (stat_queue == NULL) {
	stat_queue = (struct nqueue *) malloc(sizeof(struct nqueue));
	if (stat_queue == NULL) {
	    errno = ENOMEM;
	    return (-1);
	};
	need_init=1;
    };
    if (need_init || stat_queue->table == NULL) {
	code = netsys_queue_init(stat_queue, 256);
	if (code != 0) return code;
    }
    else
	netsys_queue_clear(stat_queue);
    return 0;
}