Revision 2c8d9f06 nbd.c

b/nbd.c
89 89
};
90 90

  
91 91
struct NBDExport {
92
    int refcount;
92 93
    BlockDriverState *bs;
93 94
    off_t dev_offset;
94 95
    off_t size;
......
664 665
        qemu_set_fd_handler2(client->sock, NULL, NULL, NULL, NULL);
665 666
        close(client->sock);
666 667
        client->sock = -1;
668
        nbd_export_put(client->exp);
667 669
        g_free(client);
668 670
    }
669 671
}
......
722 724
{
723 725
    NBDExport *exp = g_malloc0(sizeof(NBDExport));
724 726
    QSIMPLEQ_INIT(&exp->requests);
727
    exp->refcount = 1;
725 728
    exp->bs = bs;
726 729
    exp->dev_offset = dev_offset;
727 730
    exp->nbdflags = nbdflags;
......
731 734

  
732 735
void nbd_export_close(NBDExport *exp)
733 736
{
734
    while (!QSIMPLEQ_EMPTY(&exp->requests)) {
735
        NBDRequest *first = QSIMPLEQ_FIRST(&exp->requests);
736
        QSIMPLEQ_REMOVE_HEAD(&exp->requests, entry);
737
        qemu_vfree(first->data);
738
        g_free(first);
737
    assert(exp->refcount == 1);
738

  
739
    /* stub */
740
}
741

  
742
void nbd_export_get(NBDExport *exp)
743
{
744
    assert(exp->refcount > 0);
745
    exp->refcount++;
746
}
747

  
748
void nbd_export_put(NBDExport *exp)
749
{
750
    assert(exp->refcount > 0);
751
    if (exp->refcount == 1) {
752
        nbd_export_close(exp);
739 753
    }
740 754

  
741
    g_free(exp);
755
    if (--exp->refcount == 0) {
756
        while (!QSIMPLEQ_EMPTY(&exp->requests)) {
757
            NBDRequest *first = QSIMPLEQ_FIRST(&exp->requests);
758
            QSIMPLEQ_REMOVE_HEAD(&exp->requests, entry);
759
            qemu_vfree(first->data);
760
            g_free(first);
761
        }
762

  
763
        g_free(exp);
764
    }
742 765
}
743 766

  
744 767
static int nbd_can_read(void *opaque);
......
1011 1034
    client->close = close;
1012 1035
    qemu_co_mutex_init(&client->send_lock);
1013 1036
    qemu_set_fd_handler2(csock, nbd_can_read, nbd_read, NULL, client);
1037

  
1038
    nbd_export_get(exp);
1014 1039
    return client;
1015 1040
}

Also available in: Unified diff