diff options
author | Johan Hedberg <johan.hedberg@nokia.com> | 2007-02-26 10:57:20 +0000 |
---|---|---|
committer | Johan Hedberg <johan.hedberg@nokia.com> | 2007-02-26 10:57:20 +0000 |
commit | 695443f8d5e0da5fbff3b44279a0ed6abafdf3bb (patch) | |
tree | 9b5c355ed0daa832cf50ad1d9b1f9f32a7b75366 | |
parent | 89bf31abc697eab5a05bd508a352193c73991476 (diff) |
Use GLib memory allocation functions
-rw-r--r-- | hcid/dbus-adapter.c | 17 | ||||
-rw-r--r-- | hcid/dbus-common.c | 9 | ||||
-rw-r--r-- | hcid/dbus-database.c | 26 | ||||
-rw-r--r-- | hcid/dbus-hci.c | 67 | ||||
-rw-r--r-- | hcid/dbus-manager.c | 10 | ||||
-rw-r--r-- | hcid/dbus-rfcomm.c | 83 | ||||
-rw-r--r-- | hcid/dbus-sdp.c | 77 | ||||
-rw-r--r-- | hcid/dbus-security.c | 94 | ||||
-rw-r--r-- | hcid/dbus-test.c | 5 | ||||
-rw-r--r-- | hcid/main.c | 18 | ||||
-rw-r--r-- | hcid/security.c | 60 |
11 files changed, 134 insertions, 332 deletions
diff --git a/hcid/dbus-adapter.c b/hcid/dbus-adapter.c index 2fec2542..34ababa1 100644 --- a/hcid/dbus-adapter.c +++ b/hcid/dbus-adapter.c @@ -203,7 +203,7 @@ int pending_remote_name_cancel(struct adapter *adapter) } /* free discovered devices list */ - g_slist_foreach(adapter->found_devices, (GFunc) free, NULL); + g_slist_foreach(adapter->found_devices, (GFunc) g_free, NULL); g_slist_free(adapter->found_devices); adapter->found_devices = NULL; @@ -217,12 +217,7 @@ static struct bonding_request_info *bonding_request_new(bdaddr_t *peer, { struct bonding_request_info *bonding; - bonding = malloc(sizeof(*bonding)); - - if (!bonding) - return NULL; - - memset(bonding, 0, sizeof(*bonding)); + bonding = g_new0(struct bonding_request_info, 1); bacpy(&bonding->bdaddr, peer); @@ -1755,9 +1750,7 @@ static DBusHandlerResult adapter_dc_remote_device(DBusConnection *conn, if(adapter->pending_dc) return error_disconnect_in_progress(conn, msg); - adapter->pending_dc = malloc(sizeof(*adapter->pending_dc)); - if(!adapter->pending_dc) - return DBUS_HANDLER_RESULT_NEED_MEMORY; + adapter->pending_dc = g_new0(struct pending_dc_info, 1); /* Start waiting... */ adapter->pending_dc->timeout_id = @@ -1766,7 +1759,7 @@ static DBusHandlerResult adapter_dc_remote_device(DBusConnection *conn, adapter); if(!adapter->pending_dc->timeout_id) { - free(adapter->pending_dc); + g_free(adapter->pending_dc); adapter->pending_dc = NULL; return DBUS_HANDLER_RESULT_NEED_MEMORY; } @@ -2054,7 +2047,7 @@ static DBusHandlerResult adapter_cancel_bonding(DBusConnection *conn, } adapter->pin_reqs = g_slist_remove(adapter->pin_reqs, pin_req); - free(pin_req); + g_free(pin_req); } g_io_channel_close(adapter->bonding->io); diff --git a/hcid/dbus-common.c b/hcid/dbus-common.c index aabd58cd..2c84a5b2 100644 --- a/hcid/dbus-common.c +++ b/hcid/dbus-common.c @@ -273,11 +273,7 @@ static gboolean system_bus_reconnect(void *data) return TRUE; } - dl = malloc(HCI_MAX_DEV * sizeof(*dr) + sizeof(*dl)); - if (!dl) { - error("Can't allocate memory"); - goto failed; - } + dl = g_malloc0(HCI_MAX_DEV * sizeof(*dr) + sizeof(*dl)); dl->dev_num = HCI_MAX_DEV; dr = dl->dev_req; @@ -300,8 +296,7 @@ failed: if (sk >= 0) close(sk); - if (dl) - free(dl); + g_free(dl); return ret_val; } diff --git a/hcid/dbus-database.c b/hcid/dbus-database.c index 90175592..ab3a6938 100644 --- a/hcid/dbus-database.c +++ b/hcid/dbus-database.c @@ -82,7 +82,7 @@ static void exit_callback(const char *name, void *user_data) unregister_sdp_record(user_record->handle); if (user_record->sender) - free(user_record->sender); + g_free(user_record->sender); free(user_record); } @@ -105,11 +105,7 @@ static DBusHandlerResult add_service_record(DBusConnection *conn, if (len <= 0) return error_invalid_arguments(conn, msg); - user_record = malloc(sizeof(*user_record)); - if (!user_record) - return DBUS_HANDLER_RESULT_NEED_MEMORY; - - memset(user_record, 0, sizeof(*user_record)); + user_record = g_new0(struct record_data, 1); if (sdp_server_enable) { sdp_record = sdp_extract_pdu(record, &scanned); @@ -147,7 +143,7 @@ static DBusHandlerResult add_service_record(DBusConnection *conn, sender = dbus_message_get_sender(msg); - user_record->sender = strdup(sender); + user_record->sender = g_strdup(sender); records = g_slist_append(records, user_record); @@ -175,23 +171,19 @@ static DBusHandlerResult add_service_record_from_xml(DBusConnection *conn, DBUS_TYPE_STRING, &record, DBUS_TYPE_INVALID) == FALSE) return error_invalid_arguments(conn, msg); - user_record = malloc(sizeof(*user_record)); - if (!user_record) - return DBUS_HANDLER_RESULT_NEED_MEMORY; - - memset(user_record, 0, sizeof(*user_record)); + user_record = g_new0(struct record_data, 1); sdp_record = sdp_xml_parse_record(record, strlen(record)); if (!sdp_record) { error("Parsing of XML service record failed"); - free(user_record); + g_free(user_record); return error_failed(conn, msg, EIO); } if (sdp_server_enable) { if (add_record_to_server(sdp_record) < 0) { error("Failed to register service record"); - free(user_record); + g_free(user_record); sdp_record_free(sdp_record); return error_failed(conn, msg, EIO); } @@ -200,7 +192,7 @@ static DBusHandlerResult add_service_record_from_xml(DBusConnection *conn, } else { if (register_sdp_record(sdp_record) < 0) { error("Failed to register service record"); - free(user_record); + g_free(user_record); sdp_record_free(sdp_record); return error_failed(conn, msg, EIO); } @@ -212,7 +204,7 @@ static DBusHandlerResult add_service_record_from_xml(DBusConnection *conn, sender = dbus_message_get_sender(msg); - user_record->sender = strdup(sender); + user_record->sender = g_strdup(sender); records = g_slist_append(records, user_record); @@ -256,7 +248,7 @@ static DBusHandlerResult remove_service_record(DBusConnection *conn, unregister_sdp_record(handle); if (user_record->sender) - free(user_record->sender); + g_free(user_record->sender); free(user_record); diff --git a/hcid/dbus-hci.c b/hcid/dbus-hci.c index 49da2140..5db47f4f 100644 --- a/hcid/dbus-hci.c +++ b/hcid/dbus-hci.c @@ -134,11 +134,8 @@ int found_device_add(GSList **list, bdaddr_t *bdaddr, int8_t rssi, return -EALREADY; } - dev = malloc(sizeof(*dev)); - if (!dev) - return -ENOMEM; + dev = g_new0(struct remote_dev_info, 1); - memset(dev, 0, sizeof(*dev)); bacpy(&dev->bdaddr, bdaddr); dev->rssi = rssi; dev->name_status = name_status; @@ -191,11 +188,8 @@ static int active_conn_append(GSList **list, bdaddr_t *bdaddr, { struct active_conn_info *dev; - dev = malloc(sizeof(*dev)); - if (!dev) - return -1; + dev = g_new0(struct active_conn_info, 1); - memset(dev, 0 , sizeof(*dev)); bacpy(&dev->bdaddr, bdaddr); dev->handle = handle; @@ -442,7 +436,7 @@ int unregister_adapter_path(const char *path) if (adapter->found_devices) { g_slist_foreach(adapter->found_devices, - (GFunc) free, NULL); + (GFunc) g_free, NULL); g_slist_free(adapter->found_devices); adapter->found_devices = NULL; } @@ -456,7 +450,7 @@ int unregister_adapter_path(const char *path) if (adapter->pin_reqs) { g_slist_foreach(adapter->pin_reqs, - (GFunc) free, NULL); + (GFunc) g_free, NULL); g_slist_free(adapter->pin_reqs); adapter->pin_reqs = NULL; } @@ -476,7 +470,7 @@ int unregister_adapter_path(const char *path) dc_pending_timeout_cleanup(adapter); } - free (adapter); + g_free (adapter); unreg: if (!dbus_connection_unregister_object_path (connection, path)) { @@ -503,15 +497,13 @@ int hcid_dbus_register_device(uint16_t id) snprintf(path, sizeof(path), "%s/hci%d", BASE_PATH, id); - adapter = malloc(sizeof(struct adapter)); + adapter = g_try_new0(struct adapter, 1); if (!adapter) { error("Failed to alloc memory to D-Bus path register data (%s)", path); return -1; } - memset(adapter, 0, sizeof(struct adapter)); - adapter->dev_id = id; adapter->pdiscov_resolve_names = 1; @@ -651,19 +643,14 @@ int hcid_dbus_start_device(uint16_t id) * the are active connections before the daemon've started */ - cl = malloc(10 * sizeof(*ci) + sizeof(*cl)); - if (!cl) - goto failed; + cl = g_malloc0(10 * sizeof(*ci) + sizeof(*cl)); cl->dev_id = id; cl->conn_num = 10; ci = cl->conn_info; - if (ioctl(dd, HCIGETCONNLIST, (void *) cl) < 0) { - free(cl); - cl = NULL; + if (ioctl(dd, HCIGETCONNLIST, cl) < 0) goto failed; - } for (i = 0; i < cl->conn_num; i++, ci++) active_conn_append(&adapter->active_conn, @@ -678,8 +665,7 @@ failed: if (dd >= 0) hci_close_dev(dd); - if (cl) - free(cl); + g_free(cl); return ret; } @@ -745,7 +731,7 @@ int hcid_dbus_stop_device(uint16_t id) } if (adapter->found_devices) { - g_slist_foreach(adapter->found_devices, (GFunc) free, NULL); + g_slist_foreach(adapter->found_devices, (GFunc) g_free, NULL); g_slist_free(adapter->found_devices); adapter->found_devices = NULL; } @@ -757,14 +743,14 @@ int hcid_dbus_stop_device(uint16_t id) } if (adapter->pin_reqs) { - g_slist_foreach(adapter->pin_reqs, (GFunc) free, NULL); + g_slist_foreach(adapter->pin_reqs, (GFunc) g_free, NULL); g_slist_free(adapter->pin_reqs); adapter->pin_reqs = NULL; } if (adapter->active_conn) { g_slist_foreach(adapter->active_conn, (GFunc) send_dc_signal, path); - g_slist_foreach(adapter->active_conn, (GFunc) free, NULL); + g_slist_foreach(adapter->active_conn, (GFunc) g_free, NULL); g_slist_free(adapter->active_conn); adapter->active_conn = NULL; } @@ -816,13 +802,8 @@ void hcid_dbus_pending_pin_req_add(bdaddr_t *sba, bdaddr_t *dba) return; } - info = malloc(sizeof(struct pending_pin_info)); - if (!info) { - error("Out of memory when adding new pin request"); - return; - } + info = g_new0(struct pending_pin_info, 1); - memset(info, 0, sizeof(struct pending_pin_info)); bacpy(&info->bdaddr, dba); adapter->pin_reqs = g_slist_append(adapter->pin_reqs, info); @@ -903,7 +884,7 @@ void hcid_dbus_bonding_process_complete(bdaddr_t *local, bdaddr_t *peer, if (l) { void *d = l->data; adapter->pin_reqs = g_slist_remove(adapter->pin_reqs, l->data); - free(d); + g_free(d); if (!status) { message = dev_signal_factory(adapter->dev_id, @@ -1069,7 +1050,7 @@ int found_device_req_name(struct adapter *adapter) /* if failed, request the next element */ /* remove the element from the list */ adapter->found_devices = g_slist_remove(adapter->found_devices, dev); - free(dev); + g_free(dev); /* get the next element */ l = g_slist_find_custom(adapter->found_devices, &match, @@ -1183,7 +1164,7 @@ void hcid_dbus_inquiry_complete(bdaddr_t *local) } /* free discovered devices list */ - g_slist_foreach(adapter->found_devices, (GFunc) free, NULL); + g_slist_foreach(adapter->found_devices, (GFunc) g_free, NULL); g_slist_free(adapter->found_devices); adapter->found_devices = NULL; @@ -1287,7 +1268,7 @@ void hcid_dbus_periodic_inquiry_exit(bdaddr_t *local, uint8_t status) adapter->discov_type &= ~(PERIODIC_INQUIRY | RESOLVE_NAME); /* free discovered devices list */ - g_slist_foreach(adapter->found_devices, (GFunc) free, NULL); + g_slist_foreach(adapter->found_devices, (GFunc) g_free, NULL); g_slist_free(adapter->found_devices); adapter->found_devices = NULL; @@ -1546,7 +1527,7 @@ void hcid_dbus_remote_name(bdaddr_t *local, bdaddr_t *peer, uint8_t status, goto done; /* skip if a new request has been sent */ /* free discovered devices list */ - g_slist_foreach(adapter->found_devices, (GFunc) free, NULL); + g_slist_foreach(adapter->found_devices, (GFunc) g_free, NULL); g_slist_free(adapter->found_devices); adapter->found_devices = NULL; @@ -1624,7 +1605,7 @@ void hcid_dbus_conn_complete(bdaddr_t *local, uint8_t status, uint16_t handle, if (l) { struct pending_pin_req *p = l->data; adapter->pin_reqs = g_slist_remove(adapter->pin_reqs, p); - free(p); + g_free(p); } if (adapter->bonding) @@ -1702,7 +1683,7 @@ void hcid_dbus_disconn_complete(bdaddr_t *local, uint8_t status, if (l) { struct pending_pin_req *p = l->data; adapter->pin_reqs = g_slist_remove(adapter->pin_reqs, p); - free(p); + g_free(p); } /* Check if there is a pending CreateBonding request */ @@ -1750,7 +1731,7 @@ void hcid_dbus_disconn_complete(bdaddr_t *local, uint8_t status, send_message_and_unref(connection, message); adapter->active_conn = g_slist_remove(adapter->active_conn, dev); - free(dev); + g_free(dev); failed: if (peer_addr) @@ -2009,7 +1990,7 @@ void create_bond_req_exit(const char *name, struct adapter *adapter) } adapter->pin_reqs = g_slist_remove(adapter->pin_reqs, p); - free(p); + g_free(p); } g_io_channel_close(adapter->bonding->io); @@ -2131,7 +2112,7 @@ cleanup: * Reset discov_requestor and discover_state in the remote name * request event handler or in the inquiry complete handler. */ - g_slist_foreach(adapter->found_devices, (GFunc) free, NULL); + g_slist_foreach(adapter->found_devices, (GFunc) g_free, NULL); g_slist_free(adapter->found_devices); adapter->found_devices = NULL; @@ -2223,7 +2204,7 @@ cleanup: * Reset pdiscov_requestor and pdiscov_active is done when the * cmd complete event for exit periodic inquiry mode cmd arrives. */ - g_slist_foreach(adapter->found_devices, (GFunc) free, NULL); + g_slist_foreach(adapter->found_devices, (GFunc) g_free, NULL); g_slist_free(adapter->found_devices); adapter->found_devices = NULL; diff --git a/hcid/dbus-manager.c b/hcid/dbus-manager.c index a81cbc8e..b07d3342 100644 --- a/hcid/dbus-manager.c +++ b/hcid/dbus-manager.c @@ -144,11 +144,7 @@ static DBusHandlerResult list_adapters(DBusConnection *conn, if (sk < 0) return error_failed(conn, msg, errno); - dl = malloc(HCI_MAX_DEV * sizeof(*dr) + sizeof(*dl)); - if (!dl) { - close(sk); - return error_out_of_memory(conn, msg); - } + dl = g_malloc0(HCI_MAX_DEV * sizeof(*dr) + sizeof(*dl)); dl->dev_num = HCI_MAX_DEV; dr = dl->dev_req; @@ -165,7 +161,7 @@ static DBusHandlerResult list_adapters(DBusConnection *conn, reply = dbus_message_new_method_return(msg); if (!reply) { close(sk); - free(dl); + g_free(dl); return DBUS_HANDLER_RESULT_NEED_MEMORY; } @@ -189,7 +185,7 @@ static DBusHandlerResult list_adapters(DBusConnection *conn, dbus_message_iter_close_container(&iter, &array_iter); - free(dl); + g_free(dl); close(sk); diff --git a/hcid/dbus-rfcomm.c b/hcid/dbus-rfcomm.c index d99939d1..405764c9 100644 --- a/hcid/dbus-rfcomm.c +++ b/hcid/dbus-rfcomm.c @@ -99,15 +99,14 @@ static char *rfcomm_node_name_from_id(int16_t id, char *dev, size_t len) static void rfcomm_node_free(struct rfcomm_node *node) { - if (node->owner) - free(node->owner); + g_free(node->owner); if (node->io) { g_source_remove(node->io_id); g_io_channel_unref(node->io); } if (node->conn) dbus_connection_unref(node->conn); - free(node); + g_free(node); } static struct rfcomm_node *find_node_by_name(GSList *nodes, const char *name) @@ -161,15 +160,14 @@ static struct pending_connect *find_pending_connect_by_service(const char *bda, static void pending_connect_free(struct pending_connect *c) { - if (c->svc) - free(c->svc); + g_free(c->svc); if (c->io) g_io_channel_unref(c->io); if (c->msg) dbus_message_unref(c->msg); if (c->conn) dbus_connection_unref(c->conn); - free(c); + g_free(c); } static int rfcomm_release(struct rfcomm_node *node, int *err) @@ -242,11 +240,7 @@ static void rfcomm_connect_cb_devnode_opened(int fd, struct pending_connect *c, goto failed; } - node->owner = strdup(dbus_message_get_sender(c->msg)); - if (!node->owner) { - error_failed(c->conn, c->msg, ENOMEM); - goto failed; - } + node->owner = g_strdup(dbus_message_get_sender(c->msg)); /* Check if the caller is still present */ if (!dbus_bus_name_has_owner(c->conn, node->owner, NULL)) { @@ -355,12 +349,7 @@ static gboolean rfcomm_connect_cb(GIOChannel *chan, GIOCondition cond, goto failed; } - node = malloc(sizeof(struct rfcomm_node)); - if (!node) { - error_failed(c->conn, c->msg, ENOMEM); - goto failed; - } - memset(node, 0, sizeof(*node)); + node = g_new0(struct rfcomm_node, 1); /* Create the rfcomm device node */ memset(&req, 0, sizeof(req)); @@ -410,22 +399,10 @@ static int rfcomm_connect(DBusConnection *conn, DBusMessage *msg, bdaddr_t *src, int sk = -1; struct pending_connect *c = NULL; - c = malloc(sizeof(struct pending_connect)); - if (!c) { - if (err) - *err = ENOMEM; - goto failed; - } - memset(c, 0, sizeof(struct pending_connect)); + c = g_new0(struct pending_connect, 1); - if (svc) { - c->svc = strdup(svc); - if (!c->svc) { - if (err) - *err = ENOMEM; - goto failed; - } - } + if (svc) + c->svc = g_strdup(svc); c->laddr.rc_family = AF_BLUETOOTH; bacpy(&c->laddr.rc_bdaddr, src); @@ -512,21 +489,9 @@ static struct rfcomm_node *rfcomm_bind(bdaddr_t *src, const char *bda, str2ba(bda, &req.dst); req.channel = ch; - node = malloc(sizeof(struct rfcomm_node)); - if (!node) { - if (err) - *err = ENOMEM; - return NULL; - } - memset(node, 0, sizeof(struct rfcomm_node)); + node = g_new0(struct rfcomm_node, 1); - node->owner = strdup(owner); - if (!node->owner) { - if (err) - *err = ENOMEM; - rfcomm_node_free(node); - return NULL; - } + node->owner = g_strdup(owner); node->id = ioctl(rfcomm_ctl, RFCOMMCREATEDEV, &req); if (node->id < 0) { @@ -562,23 +527,15 @@ static rfcomm_continue_data_t *rfcomm_continue_data_new(DBusConnection *conn, { rfcomm_continue_data_t *new; - new = malloc(sizeof(*new)); - if (!new) - return NULL; + new = g_new(rfcomm_continue_data_t, 1); - new->dst = strdup(dst); + new->dst = g_strdup(dst); if (!new->dst) { - free(new); - return NULL; - } - - new->svc = strdup(svc); - if (!new->svc) { - free(new->dst); - free(new); + g_free(new); return NULL; } + new->svc = g_strdup(svc); new->conn = dbus_connection_ref(conn); new->msg = dbus_message_ref(msg); new->adapter = adapter; @@ -590,9 +547,9 @@ static void rfcomm_continue_data_free(rfcomm_continue_data_t *d) { dbus_connection_unref(d->conn); dbus_message_unref(d->msg); - free(d->svc); - free(d->dst); - free(d); + g_free(d->svc); + g_free(d->dst); + g_free(d); } static void rfcomm_conn_req_continue(sdp_record_t *rec, void *data, int err) @@ -609,7 +566,7 @@ static void rfcomm_conn_req_continue(sdp_record_t *rec, void *data, int err) if (!sdp_get_access_protos(rec, &protos)) { ch = sdp_get_proto_port(protos, RFCOMM_UUID); - sdp_list_foreach(protos, (sdp_list_func_t)sdp_list_free, NULL); + sdp_list_foreach(protos, (sdp_list_func_t) sdp_list_free, NULL); sdp_list_free(protos, NULL); } if (ch == -1) { @@ -809,7 +766,7 @@ static void rfcomm_bind_req_continue(sdp_record_t *rec, void *data, int err) if (!sdp_get_access_protos(rec, &protos)) { ch = sdp_get_proto_port(protos, RFCOMM_UUID); - sdp_list_foreach(protos, (sdp_list_func_t)sdp_list_free, NULL); + sdp_list_foreach(protos, (sdp_list_func_t) sdp_list_free, NULL); sdp_list_free(protos, NULL); } if (ch == -1) { diff --git a/hcid/dbus-sdp.c b/hcid/dbus-sdp.c index 54de8aaf..da0f13b2 100644 --- a/hcid/dbus-sdp.c +++ b/hcid/dbus-sdp.c @@ -169,18 +169,10 @@ static struct pending_connect *pending_connect_new(DBusConnection *conn, DBusMes if (!dst) return NULL; - c = malloc(sizeof(*c)); - if (!c) - return NULL; - memset(c, 0, sizeof(*c)); + c = g_new0(struct pending_connect, 1); - if (dst) { - c->dst = strdup(dst); - if (!c->dst) { - free(c); - return NULL; - } - } + if (dst) + c->dst = g_strdup(dst); c->conn = dbus_connection_ref(conn); c->rq = dbus_message_ref(msg); @@ -194,8 +186,7 @@ static void pending_connect_free(struct pending_connect *c) if (!c) return; - if (c->dst) - free(c->dst); + g_free(c->dst); if (c->rq) dbus_message_unref(c->rq); @@ -203,7 +194,7 @@ static void pending_connect_free(struct pending_connect *c) if (c->conn) dbus_connection_unref(c->conn); - free(c); + g_free(c); } static struct pending_connect *find_pending_connect(const char *dst) @@ -245,9 +236,7 @@ static int sdp_store_record(const char *src, const char *dst, uint32_t handle, u snprintf(key, sizeof(key), "%s#%08X", dst, handle); - value = malloc(size * 2 + 1); - if (!value) - return -ENOMEM; + value = g_malloc(size * 2 + 1); value[0] = '\0'; @@ -256,7 +245,7 @@ static int sdp_store_record(const char *src, const char *dst, uint32_t handle, u err = textfile_put(filename, key, value); - free(value); + g_free(value); return err; } @@ -277,7 +266,7 @@ static void transaction_context_free(void *udata) if (ctxt->session) sdp_close(ctxt->session); - free(ctxt); + g_free(ctxt); } static get_record_data_t *get_record_data_new(uint16_t dev_id, const char *dst, @@ -286,16 +275,9 @@ static get_record_data_t *get_record_data_new(uint16_t dev_id, const char *dst, { get_record_data_t *n; - n = malloc(sizeof(*n)); - if (!n) - return NULL; - - n->dst = strdup(dst); - if (!n->dst) { - free(n); - return NULL; - } + n = g_new(get_record_data_t, 1); + n->dst = g_strdup(dst); n->dev_id = dev_id; n->search_data = search_data; n->cb = cb; @@ -306,9 +288,9 @@ static get_record_data_t *get_record_data_new(uint16_t dev_id, const char *dst, static void get_record_data_free(get_record_data_t *d) { - free(d->search_data); - free(d->dst); - free(d); + g_free(d->search_data); + g_free(d->dst); + g_free(d); } static inline void get_record_data_call_cb(get_record_data_t *d, @@ -594,12 +576,7 @@ static gboolean sdp_client_connect_cb(GIOChannel *chan, goto failed; } - ctxt = malloc(sizeof(*ctxt)); - if (!ctxt) { - err = ENOMEM; - goto failed; - } - memset(ctxt, 0, sizeof(*ctxt)); + ctxt = g_new0(struct transaction_context, 1); ctxt->conn = dbus_connection_ref(c->conn); ctxt->rq = dbus_message_ref(c->rq); @@ -926,17 +903,11 @@ int get_record_with_handle(DBusConnection *conn, DBusMessage *msg, return -EINPROGRESS; } - rec_handle = malloc(sizeof(uint32_t)); - if (!rec_handle) - return -ENOMEM; + rec_handle = g_new(uint32_t, 1); *rec_handle = handle; d = get_record_data_new(dev_id, dst, rec_handle, cb, data); - if (!d) { - free(rec_handle); - return -ENOMEM; - } if (!(c = connect_request(conn, msg, dev_id, dst, get_rec_with_handle_conn_cb, &err))) { @@ -988,14 +959,10 @@ static void get_rec_with_uuid_comp_cb(uint8_t type, uint16_t err, goto failed; pdata += sizeof(uint16_t); - handle = malloc(sizeof(*handle)); - if (!handle) { - cb_err = ENOMEM; - goto failed; - } - *handle = ntohl(bt_get_unaligned((uint32_t*)pdata)); + handle = g_new(uint32_t, 1); + *handle = ntohl(bt_get_unaligned((uint32_t*) pdata)); - free(d->search_data); + g_free(d->search_data); d->search_data = handle; cb_err = get_rec_with_handle_conn_cb(ctxt); @@ -1055,17 +1022,11 @@ int get_record_with_uuid(DBusConnection *conn, DBusMessage *msg, return -EINPROGRESS; } - sdp_uuid = malloc(sizeof(uuid_t)); - if (!sdp_uuid) - return -ENOMEM; + sdp_uuid = g_new(uuid_t, 1); memcpy(sdp_uuid, uuid, sizeof(uuid_t)); d = get_record_data_new(dev_id, dst, sdp_uuid, cb, data); - if (!d) { - free(sdp_uuid); - return -ENOMEM; - } if (!(c = connect_request(conn, msg, dev_id, dst, get_rec_with_uuid_conn_cb, &err))) { diff --git a/hcid/dbus-security.c b/hcid/dbus-security.c index f6897079..ee3b96eb 100644 --- a/hcid/dbus-security.c +++ b/hcid/dbus-security.c @@ -192,37 +192,19 @@ static struct passkey_agent *passkey_agent_new(struct adapter *adapter, DBusConn { struct passkey_agent *agent; - agent = malloc(sizeof(struct passkey_agent)); - if (!agent) - return NULL; - - memset(agent, 0, sizeof(struct passkey_agent)); + agent = g_new0(struct passkey_agent, 1); agent->adapter = adapter; - agent->name = strdup(name); - if (!agent->name) - goto mem_fail; - - agent->path = strdup(path); - if (!agent->path) - goto mem_fail; + agent->name = g_strdup(name); + agent->path = g_strdup(path); - if (addr) { + if (addr) agent->addr = strdup(addr); - if (!agent->addr) - goto mem_fail; - } agent->conn = dbus_connection_ref(conn); return agent; - -mem_fail: - /* So passkey_agent_free doesn't try to call Relese */ - agent->exited = 1; - passkey_agent_free(agent); - return NULL; } static int agent_cmp(const struct passkey_agent *a, const struct passkey_agent *b) @@ -511,11 +493,11 @@ static void auth_agent_call_cancel(struct auth_agent_req *req) static void auth_agent_free(struct authorization_agent *agent) { - free(agent->name); - free(agent->path); + g_free(agent->name); + g_free(agent->path); dbus_connection_unref(agent->conn); g_slist_free(agent->pending_requests); - free(agent); + g_free(agent); } static struct authorization_agent *auth_agent_new(DBusConnection *conn, @@ -524,29 +506,14 @@ static struct authorization_agent *auth_agent_new(DBusConnection *conn, { struct authorization_agent *agent; - agent = malloc(sizeof(*agent)); - if (!agent) - return NULL; - memset(agent, 0, sizeof(*agent)); + agent = g_new0(struct authorization_agent, 1); - agent->name = strdup(name); - if (!agent->name) - goto failed; - - agent->path = strdup(path); - if (!agent->path) - goto failed; + agent->name = g_strdup(name); + agent->path = g_strdup(path); agent->conn = dbus_connection_ref(conn); return agent; - -failed: - if (agent->name) - free(agent->name); - free(agent); - - return NULL; } static void default_auth_agent_exited(const char *name, void *data) @@ -1042,8 +1009,8 @@ done: dbus_pending_call_cancel(req->call); if (req->call) dbus_pending_call_unref(req->call); - free(req->path); - free(req); + g_free(req->path); + g_free(req); if (agent != default_agent) { agent->adapter->passkey_agents = g_slist_remove(agent->adapter->passkey_agents, @@ -1067,10 +1034,7 @@ static int call_passkey_agent(DBusConnection *conn, debug("Calling PasskeyAgent.Request: name=%s, path=%s", agent->name, agent->path); - req = malloc(sizeof(struct pending_agent_request)); - if (!req) - goto failed; - memset(req, 0, sizeof(struct pending_agent_request)); + req = g_new0(struct pending_agent_request, 1); req->dev = dev; bacpy(&req->sba, sba); bacpy(&req->bda, dba); @@ -1090,10 +1054,8 @@ static int call_passkey_agent(DBusConnection *conn, return 0; failed: - if (req) { - free(req->path); - free(req); - } + g_free(req->path); + g_free(req); hci_send_cmd(dev, OGF_LINK_CTL, OCF_PIN_CODE_NEG_REPLY, 6, dba); @@ -1224,10 +1186,9 @@ done: dbus_pending_call_cancel(req->call); if (req->call) dbus_pending_call_unref(req->call); - if (req->pin) - free(req->pin); - free(req->path); - free(req); + g_free(req->pin); + g_free(req->path); + g_free(req); if (agent != default_agent) { agent->adapter->passkey_agents = g_slist_remove(agent->adapter->passkey_agents, @@ -1251,10 +1212,7 @@ static int call_confirm_agent(DBusConnection *conn, debug("Calling PasskeyAgent.Confirm: name=%s, path=%s", agent->name, agent->path); - req = malloc(sizeof(struct pending_agent_request)); - if (!req) - goto failed; - memset(req, 0, sizeof(struct pending_agent_request)); + req = g_new0(struct pending_agent_request, 1); req->dev = dev; bacpy(&req->sba, sba); bacpy(&req->bda, dba); @@ -1278,10 +1236,9 @@ static int call_confirm_agent(DBusConnection *conn, failed: if (req) { - if (req->pin) - free(req->pin); - free(req->path); - free(req); + g_free(req->pin); + g_free(req->path); + g_free(req); } hci_send_cmd(dev, OGF_LINK_CTL, OCF_PIN_CODE_NEG_REPLY, 6, dba); @@ -1351,10 +1308,9 @@ static void send_cancel_request(struct pending_agent_request *req) dbus_pending_call_cancel(req->call); dbus_pending_call_unref(req->call); - if (req->pin) - free(req->pin); - free(req->path); - free(req); + g_free(req->pin); + g_free(req->path); + g_free(req); } static void release_agent(struct passkey_agent *agent) diff --git a/hcid/dbus-test.c b/hcid/dbus-test.c index 78766a49..1794f276 100644 --- a/hcid/dbus-test.c +++ b/hcid/dbus-test.c @@ -93,10 +93,7 @@ static struct audit *audit_new(DBusConnection *conn, DBusMessage *msg, path = dbus_message_get_path(msg); requestor = dbus_message_get_sender(msg); - audit = malloc(sizeof(struct audit)); - if (!audit) - return NULL; - memset(audit, 0, sizeof(struct audit)); + audit = g_new0(struct audit, 1); audit->requestor = strdup(requestor); if (!audit->requestor) { diff --git a/hcid/main.c b/hcid/main.c index a790b3da..01ca2638 100644 --- a/hcid/main.c +++ b/hcid/main.c @@ -70,19 +70,19 @@ struct device_opts *alloc_device_opts(char *ref) { struct device_list *device; - device = malloc(sizeof(struct device_list)); + device = g_try_new(struct device_list, 1); if (!device) { info("Can't allocate devlist opts buffer: %s (%d)", strerror(errno), errno); exit(1); } - device->ref = strdup(ref); + device->ref = g_strdup(ref); device->next = device_list; device_list = device; memcpy(&device->opts, &default_device, sizeof(struct device_opts)); - device->opts.name = strdup(default_device.name); + device->opts.name = g_strdup(default_device.name); return &device->opts; } @@ -97,11 +97,10 @@ static void free_device_opts(void) } for (device = device_list; device; device = next) { - free(device->ref); - if (device->opts.name) - free(device->opts.name); + g_free(device->ref); + g_free(device->opts.name); next = device->next; - free(device); + g_free(device); } device_list = NULL; @@ -508,14 +507,13 @@ static void init_all_devices(int ctl) struct hci_dev_req *dr; int i; - dl = malloc(HCI_MAX_DEV * sizeof(struct hci_dev_req) + sizeof(uint16_t)); + dl = g_try_malloc0(HCI_MAX_DEV * sizeof(struct hci_dev_req) + sizeof(uint16_t)); if (!dl) { info("Can't allocate devlist buffer: %s (%d)", strerror(errno), errno); exit(1); } - memset(dl, 0, HCI_MAX_DEV * sizeof(struct hci_dev_req) + sizeof(uint16_t)); dl->dev_num = HCI_MAX_DEV; dr = dl->dev_req; @@ -534,7 +532,7 @@ static void init_all_devices(int ctl) } } - free(dl); + g_free(dl); } static void init_defaults(void) diff --git a/hcid/security.c b/hcid/security.c index e14b98e9..cf46674c 100644 --- a/hcid/security.c +++ b/hcid/security.c @@ -68,18 +68,9 @@ struct hci_req_data *hci_req_data_new(int dev_id, const bdaddr_t *dba, uint16_t { struct hci_req_data *data; - data = malloc(sizeof(*data)); - if (!data) - return NULL; - - memset(data, 0, sizeof(*data)); - - data->cparam = malloc(clen); - if (!data->cparam) { - free(data); - return NULL; - } + data = g_new0(struct hci_req_data, 1); + data->cparam = g_malloc(clen); memcpy(data->cparam, cparam, clen); bacpy(&data->dba, dba); @@ -121,8 +112,8 @@ static void hci_req_queue_process(int dev_id) ret_val = hci_send_cmd(dd, data->ogf, data->ocf, data->clen, data->cparam); if (ret_val < 0) { hci_req_queue = g_slist_remove(hci_req_queue, data); - free(data->cparam); - free(data); + g_free(data->cparam); + g_free(data); } } while(ret_val < 0); @@ -159,8 +150,8 @@ void hci_req_queue_remove(int dev_id, bdaddr_t *dba) continue; hci_req_queue = g_slist_remove(hci_req_queue, req); - free(req->cparam); - free(req); + g_free(req->cparam); + g_free(req); } } @@ -187,8 +178,8 @@ static void check_pending_hci_req(int dev_id, int event) /* remove the confirmed cmd */ hci_req_queue = g_slist_remove(hci_req_queue, data); - free(data->cparam); - free(data); + g_free(data->cparam); + g_free(data); } hci_req_queue_process(dev_id); @@ -201,11 +192,7 @@ static inline int get_bdaddr(int dev, bdaddr_t *sba, uint16_t handle, bdaddr_t * char addr[18]; int i; - cl = malloc(10 * sizeof(*ci) + sizeof(*cl)); - if (!cl) - return -ENOMEM; - - memset(cl, 0, 10 * sizeof(*ci) + sizeof(*cl)); + cl = g_malloc0(10 * sizeof(*ci) + sizeof(*cl)); ba2str(sba, addr); cl->dev_id = hci_devid(addr); @@ -213,18 +200,18 @@ static inline int get_bdaddr(int dev, bdaddr_t *sba, uint16_t handle, bdaddr_t * ci = cl->conn_info; if (ioctl(dev, HCIGETCONNLIST, (void *) cl) < 0) { - free(cl); + g_free(cl); return -EIO; } for (i = 0; i < cl->conn_num; i++, ci++) if (ci->handle == handle) { bacpy(dba, &ci->bdaddr); - free(cl); + g_free(cl); return 0; } - free(cl); + g_free(cl); return -ENOENT; } @@ -346,11 +333,7 @@ static void pin_code_request(int dev, bdaddr_t *sba, bdaddr_t *dba) ba2str(sba, sa); ba2str(dba, da); info("pin_code_request (sba=%s, dba=%s)", sa, da); - cr = malloc(sizeof(*cr) + sizeof(*ci)); - if (!cr) - return; - - memset(cr, 0, sizeof(*cr) + sizeof(*ci)); + cr = g_malloc0(sizeof(*cr) + sizeof(*ci)); bacpy(&cr->bdaddr, dba); cr->type = ACL_LINK; @@ -406,12 +389,12 @@ static void pin_code_request(int dev, bdaddr_t *sba, bdaddr_t *dba) hcid_dbus_pending_pin_req_add(sba, &ci->bdaddr); - free(cr); + g_free(cr); return; reject: - free(cr); + g_free(cr); hci_send_cmd(dev, OGF_LINK_CTL, OCF_PIN_CODE_NEG_REPLY, 6, dba); @@ -839,19 +822,12 @@ void start_security_manager(int hdev) return; } - di = malloc(sizeof(*di)); - if (!di) { - error("Can't allocate device info buffer: %s (%d)", - strerror(errno), errno); - close(dev); - return; - } - + di = g_new(struct hci_dev_info, 1); if (hci_devinfo(hdev, di) < 0) { error("Can't get device info: %s (%d)", strerror(errno), errno); close(dev); - free(di); + g_free(di); return; } @@ -859,7 +835,7 @@ void start_security_manager(int hdev) g_io_channel_set_close_on_unref(chan, TRUE); io_data[hdev].watch_id = g_io_add_watch_full(chan, G_PRIORITY_HIGH, G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR, - io_security_event, di, (GDestroyNotify)free); + io_security_event, di, (GDestroyNotify) g_free); io_data[hdev].channel = chan; io_data[hdev].pin_length = -1; |