#include "dynbuf.h"
#include "util.h"

struct dynbuf* db_init(size_t len)
{
    struct dynbuf* db = xmalloc(sizeof(struct dynbuf) + len);
    db->len = len;
    db->count = 0;
    return db;
}

struct dynbuf* db_extend(struct dynbuf* db, char* buf, size_t len)
{
    if (db->count + len > db->len)
    {
        db = xrealloc(db, sizeof(struct dynbuf) + (db->len + len) * 2);
        db->len = (db->len + len) * 2;
    }

    memcpy(&db->buf + db->count, buf, len);
    db->count += len;

    return db;
}

short dynbuf_destroy(struct dynbuf* db)
{
    free(db);
}
