static CharDriverState *qemu_chr_open_ringbuf(ChardevRingbuf *opts,

                                              Error **errp)

{

    CharDriverState *chr;

    RingBufCharDriver *d;



    chr = g_malloc0(sizeof(CharDriverState));

    d = g_malloc(sizeof(*d));



    d->size = opts->has_size ? opts->size : 65536;



    

    if (d->size & (d->size - 1)) {

        error_setg(errp, "size of ringbuf chardev must be power of two");

        goto fail;

    }



    d->prod = 0;

    d->cons = 0;

    d->cbuf = g_malloc0(d->size);



    chr->opaque = d;

    chr->chr_write = ringbuf_chr_write;

    chr->chr_close = ringbuf_chr_close;



    return chr;



fail:

    g_free(d);

    g_free(chr);

    return NULL;

}
