Example #1
0
char *databuf_export(DataBuf *db)
{
    DATABUF_VALIDATE(db);
    databuf_shift_data_to_beginning(db);
    DATABUF_VALIDATE(db);
    return db->alloc_ptr;
}
Example #2
0
int databuf_append(DataBuf *db, const char *src, size_t src_size)
{
    size_t new_size;

    DATABUF_VALIDATE(db);

    if (src == NULL || src_size == 0) return 0;

    new_size = db->len+src_size;

#ifdef DEBUG
    if (debug) databuf_print(db, 1, "databuf_append() size=%zd", src_size);
#endif
    if ((new_size > db->alloc_size) ||
        ((db->flags & DATABUF_FLAG_PRESERVE_HEAD) && !databuf_tail_available(db, src_size))) {
        /* not enough room, we must realloc */
        void *new_alloc;
        
        databuf_shift_data_to_beginning(db);
        if ((new_alloc = realloc(db->alloc_ptr, new_size))) {
            db->alloc_ptr  = new_alloc;
            db->alloc_size = new_size;
        } else {
            return -1;           /* realloc failed */
        }
    } else {
        /* we can fit within current allocation, but can we append? */
        if (!databuf_tail_available(db, src_size)) {
            /* we can't append in place, must create room at tail by shifting
               data forward to the beginning of the  allocation block */
            databuf_shift_data_to_beginning(db);
        }
    }
#ifdef DEBUG
    if (debug) databuf_print(db, 1, "databuf_append() about to memmove()");
#endif
    /* pointers all set up and room availble, move the data and update */
    memmove(databuf_end(db), src, src_size);
    db->len = new_size;
    db->max_len = MAX(db->max_len, new_size);
#ifdef DEBUG
    if (debug) databuf_print(db, 1, "databuf_append() conclusion");
#endif
    DATABUF_VALIDATE(db);
    return 1;
}
Example #3
0
int databuf_compress(DataBuf *db)
{
    void *new_alloc;

    DATABUF_VALIDATE(db);
    if (databuf_beg(db) == NULL || db->len == 0) return 0;
    databuf_shift_data_to_beginning(db);
    if ((new_alloc = realloc(db->alloc_ptr, db->len))) {
        db->alloc_ptr  = new_alloc;
        db->alloc_size = db->len;
    } else {
        return -1;           /* realloc failed */
    }
    
    DATABUF_VALIDATE(db);
    return 1;
}