net: introduce NetClientState destructor

To allow allocating an array of NetClientState and free it once, this patch
introduces destructor of NetClientState. Which could do type specific free,
which could be used by multiqueue to free the array once.

Signed-off-by: Jason Wang <jasowang@redhat.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
This commit is contained in:
Jason Wang 2013-01-30 19:12:27 +08:00 committed by Anthony Liguori
parent 18a1541a8d
commit f7860455fd
2 changed files with 15 additions and 4 deletions

View File

@ -35,6 +35,7 @@ typedef ssize_t (NetReceive)(NetClientState *, const uint8_t *, size_t);
typedef ssize_t (NetReceiveIOV)(NetClientState *, const struct iovec *, int); typedef ssize_t (NetReceiveIOV)(NetClientState *, const struct iovec *, int);
typedef void (NetCleanup) (NetClientState *); typedef void (NetCleanup) (NetClientState *);
typedef void (LinkStatusChanged)(NetClientState *); typedef void (LinkStatusChanged)(NetClientState *);
typedef void (NetClientDestructor)(NetClientState *);
typedef struct NetClientInfo { typedef struct NetClientInfo {
NetClientOptionsKind type; NetClientOptionsKind type;
@ -58,6 +59,7 @@ struct NetClientState {
char *name; char *name;
char info_str[256]; char info_str[256];
unsigned receive_disabled : 1; unsigned receive_disabled : 1;
NetClientDestructor *destructor;
}; };
typedef struct NICState { typedef struct NICState {

View File

@ -182,11 +182,17 @@ static char *assign_name(NetClientState *nc1, const char *model)
return g_strdup(buf); return g_strdup(buf);
} }
static void qemu_net_client_destructor(NetClientState *nc)
{
g_free(nc);
}
static void qemu_net_client_setup(NetClientState *nc, static void qemu_net_client_setup(NetClientState *nc,
NetClientInfo *info, NetClientInfo *info,
NetClientState *peer, NetClientState *peer,
const char *model, const char *model,
const char *name) const char *name,
NetClientDestructor *destructor)
{ {
nc->info = info; nc->info = info;
nc->model = g_strdup(model); nc->model = g_strdup(model);
@ -204,7 +210,7 @@ static void qemu_net_client_setup(NetClientState *nc,
QTAILQ_INSERT_TAIL(&net_clients, nc, next); QTAILQ_INSERT_TAIL(&net_clients, nc, next);
nc->send_queue = qemu_new_net_queue(nc); nc->send_queue = qemu_new_net_queue(nc);
nc->destructor = destructor;
} }
NetClientState *qemu_new_net_client(NetClientInfo *info, NetClientState *qemu_new_net_client(NetClientInfo *info,
@ -217,7 +223,8 @@ NetClientState *qemu_new_net_client(NetClientInfo *info,
assert(info->size >= sizeof(NetClientState)); assert(info->size >= sizeof(NetClientState));
nc = g_malloc0(info->size); nc = g_malloc0(info->size);
qemu_net_client_setup(nc, info, peer, model, name); qemu_net_client_setup(nc, info, peer, model, name,
qemu_net_client_destructor);
return nc; return nc;
} }
@ -279,7 +286,9 @@ static void qemu_free_net_client(NetClientState *nc)
} }
g_free(nc->name); g_free(nc->name);
g_free(nc->model); g_free(nc->model);
g_free(nc); if (nc->destructor) {
nc->destructor(nc);
}
} }
void qemu_del_net_client(NetClientState *nc) void qemu_del_net_client(NetClientState *nc)