deps/libcows/buffers.c
int cbuf_chain_flatten(cbuf_chain *chain) {
    cbuf *cb;
    size_t total_size = 0;

    // Figure out the total amount of data in the chain.
    STAILQ_FOREACH(cb, &chain->head, next) {
        total_size += cb->write_offset - cb->read_offset;
    }
    assert(chain->data_size == total_size);    // prepare to remove the above loop.

    cb = STAILQ_FIRST(&chain->head);
    if(!cb || total_size == cb->write_offset - cb->read_offset) {
        return 0;
    } else {
        cbuf *ncb = cbuf_new(total_size * 2);
        assert(ncb);
        cbuf *tmp_cb;
        STAILQ_FOREACH_SAFE(cb, &chain->head, next, tmp_cb) {
            size_t to_copy = cb->write_offset - cb->read_offset;
            memcpy(ncb->write_offset, cb->read_offset, to_copy);
            ncb->write_offset += to_copy;
            if(cb->free) cb->free(cb->buf);
            free(cb);
        }
        STAILQ_INIT(&chain->head);
        STAILQ_INSERT_HEAD(&chain->head, ncb, next);
        return 1;
    }
}
/*
 * Collapse the chain into the single contiguous space.
 */
Collapse the chain into the single contiguous space.
Collapse the chain into the single contiguous space .
int cbuf_chain_flatten(cbuf_chain *chain) {
    cbuf *cb;
    size_t total_size = 0;
    STAILQ_FOREACH(cb, &chain->head, next) {
        total_size += cb->write_offset - cb->read_offset;
    }
    assert(chain->data_size == total_size);    
    cb = STAILQ_FIRST(&chain->head);
    if(!cb || total_size == cb->write_offset - cb->read_offset) {
        return 0;
    } else {
        cbuf *ncb = cbuf_new(total_size * 2);
        assert(ncb);
        cbuf *tmp_cb;
        STAILQ_FOREACH_SAFE(cb, &chain->head, next, tmp_cb) {
            size_t to_copy = cb->write_offset - cb->read_offset;
            memcpy(ncb->write_offset, cb->read_offset, to_copy);
            ncb->write_offset += to_copy;
            if(cb->free) cb->free(cb->buf);
            free(cb);
        }
        STAILQ_INIT(&chain->head);
        STAILQ_INSERT_HEAD(&chain->head, ncb, next);
        return 1;
    }
}
int cbuf_chain_flatten ( cbuf_chain * chain ) { cbuf * cb ; size_t total_size = 0 ; STAILQ_FOREACH ( cb , & chain -> head , next ) { total_size += cb -> write_offset - cb -> read_offset ; } assert ( chain -> data_size == total_size ) ; cb = STAILQ_FIRST ( & chain -> head ) ; if ( ! cb || total_size == cb -> write_offset - cb -> read_offset ) { return 0 ; } else { cbuf * ncb = cbuf_new ( total_size * 2 ) ; assert ( ncb ) ; cbuf * tmp_cb ; STAILQ_FOREACH_SAFE ( cb , & chain -> head , next , tmp_cb ) { size_t to_copy = cb -> write_offset - cb -> read_offset ; memcpy ( ncb -> write_offset , cb -> read_offset , to_copy ) ; ncb -> write_offset += to_copy ; if ( cb -> free ) cb -> free ( cb -> buf ) ; free ( cb ) ; } STAILQ_INIT ( & chain -> head ) ; STAILQ_INSERT_HEAD ( & chain -> head , ncb , next ) ; return 1 ; } }
Collapse the chain into the single contiguous space.
Collapse the chain into the single contiguous space .
// Figure out the total amount of data in the chain. // prepare to remove the above loop.