[PATCH] osmocom-bb[master]: VIRT-PHY: mcast_sock: Avoid dynamic allocation of osmo_fd me...

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Harald Welte gerrit-no-reply at lists.osmocom.org
Thu Jul 13 12:32:07 UTC 2017


Review at  https://gerrit.osmocom.org/3232

VIRT-PHY: mcast_sock: Avoid dynamic allocation of osmo_fd member

Change-Id: I11e0137849797eb67047b4ddca0e917c599ed909
---
M src/host/virt_phy/include/virtphy/osmo_mcast_sock.h
M src/host/virt_phy/src/shared/osmo_mcast_sock.c
2 files changed, 19 insertions(+), 24 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/osmocom-bb refs/changes/32/3232/1

diff --git a/src/host/virt_phy/include/virtphy/osmo_mcast_sock.h b/src/host/virt_phy/include/virtphy/osmo_mcast_sock.h
index abf2c48..3c5954d 100644
--- a/src/host/virt_phy/include/virtphy/osmo_mcast_sock.h
+++ b/src/host/virt_phy/include/virtphy/osmo_mcast_sock.h
@@ -4,11 +4,11 @@
 #include <osmocom/core/select.h>
 
 struct mcast_server_sock {
-	struct osmo_fd *osmo_fd;
+	struct osmo_fd osmo_fd;
 };
 
 struct mcast_client_sock {
-	struct osmo_fd *osmo_fd;
+	struct osmo_fd osmo_fd;
 	struct ip_mreq *mcast_group;
 };
 
diff --git a/src/host/virt_phy/src/shared/osmo_mcast_sock.c b/src/host/virt_phy/src/shared/osmo_mcast_sock.c
index fdbbb88..a06d706 100644
--- a/src/host/virt_phy/src/shared/osmo_mcast_sock.c
+++ b/src/host/virt_phy/src/shared/osmo_mcast_sock.c
@@ -19,11 +19,8 @@
 	struct mcast_server_sock *serv_sock = talloc_zero(ctx, struct mcast_server_sock);
 	int rc;
 
-	/* TODO: why allocate those dynamically ?!? */
-	serv_sock->osmo_fd = talloc_zero(serv_sock, struct osmo_fd);
-
 	/* setup mcast server socket */
-	rc = osmo_sock_init_ofd(serv_sock->osmo_fd, AF_INET, SOCK_DGRAM, IPPROTO_UDP,
+	rc = osmo_sock_init_ofd(&serv_sock->osmo_fd, AF_INET, SOCK_DGRAM, IPPROTO_UDP,
 				tx_mcast_group, tx_mcast_port, OSMO_SOCK_F_CONNECT);
 	if (rc < 0) {
 		perror("Failed to create Multicast Server Socket");
@@ -32,7 +29,7 @@
 
 	/* determines whether sent mcast packets should be looped back to the local sockets.
 	 * loopback must be enabled if the mcast client is on the same machine */
-	if (setsockopt(serv_sock->osmo_fd->fd, IPPROTO_IP, IP_MULTICAST_LOOP,
+	if (setsockopt(serv_sock->osmo_fd.fd, IPPROTO_IP, IP_MULTICAST_LOOP,
 			&loopback, sizeof(loopback)) < 0) {
 		perror("Failed to configure multicast loopback.\n");
 		return NULL;
@@ -53,15 +50,14 @@
 	int rc, loopback = 1, all = 0;
 
 	/* TODO: why allocate those dynamically ?!? */
-	client_sock->osmo_fd = talloc_zero(client_sock, struct osmo_fd);
 	client_sock->mcast_group = talloc_zero(client_sock, struct ip_mreq);
 
-	client_sock->osmo_fd->cb = fd_rx_cb;
-	client_sock->osmo_fd->when = BSC_FD_READ;
-	client_sock->osmo_fd->data = osmo_fd_data;
+	client_sock->osmo_fd.cb = fd_rx_cb;
+	client_sock->osmo_fd.when = BSC_FD_READ;
+	client_sock->osmo_fd.data = osmo_fd_data;
 
 	/* Create mcast client socket */
-	rc = osmo_sock_init_ofd(client_sock->osmo_fd, AF_INET, SOCK_DGRAM, IPPROTO_UDP,
+	rc = osmo_sock_init_ofd(&client_sock->osmo_fd, AF_INET, SOCK_DGRAM, IPPROTO_UDP,
 				NULL, mcast_port, OSMO_SOCK_F_BIND);
 	if (rc < 0) {
 		perror("Could not create mcast client socket");
@@ -71,7 +67,7 @@
 	/* Enable loopback of msgs to the host. */
 	/* Loopback must be enabled for the client, so multiple
 	 * processes are able to receive a mcast package. */
-	rc = setsockopt(client_sock->osmo_fd->fd, IPPROTO_IP, IP_MULTICAST_LOOP,
+	rc = setsockopt(client_sock->osmo_fd.fd, IPPROTO_IP, IP_MULTICAST_LOOP,
 			&loopback, sizeof(loopback));
 	if (rc < 0) {
 		perror("Failed to enable IP_MULTICAST_LOOP");
@@ -81,7 +77,7 @@
 	/* Configure and join the multicast group */
 	client_sock->mcast_group->imr_multiaddr.s_addr = inet_addr(mcast_group);
 	client_sock->mcast_group->imr_interface.s_addr = htonl(INADDR_ANY);
-	rc = setsockopt(client_sock->osmo_fd->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
+	rc = setsockopt(client_sock->osmo_fd.fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
 			client_sock->mcast_group, sizeof(*client_sock->mcast_group));
 	if (rc < 0) {
 		perror("Failed to join to mcast goup");
@@ -90,7 +86,7 @@
 
 	/* this option will set the delivery option so that only packets
 	 * from sockets we are subscribed to via IP_ADD_MEMBERSHIP are received */
-	if (setsockopt(client_sock->osmo_fd->fd, IPPROTO_IP, IP_MULTICAST_ALL, &all, sizeof(all)) < 0) {
+	if (setsockopt(client_sock->osmo_fd.fd, IPPROTO_IP, IP_MULTICAST_ALL, &all, sizeof(all)) < 0) {
 		perror("Failed to modify delivery policy to explicitly joined.\n");
 		return NULL;
 	}
@@ -119,13 +115,13 @@
 int mcast_client_sock_rx(struct mcast_client_sock *client_sock, void* buf,
                          int buf_len)
 {
-	return recv(client_sock->osmo_fd->fd, buf, buf_len, 0);
+	return recv(client_sock->osmo_fd.fd, buf, buf_len, 0);
 }
 
 int mcast_server_sock_tx(struct mcast_server_sock *serv_sock, void* data,
                          int data_len)
 {
-	return send(serv_sock->osmo_fd->fd, data, data_len, 0);
+	return send(serv_sock->osmo_fd.fd, data, data_len, 0);
 }
 
 int mcast_bidir_sock_tx(struct mcast_bidir_sock *bidir_sock, void* data,
@@ -141,20 +137,19 @@
 
 void mcast_client_sock_close(struct mcast_client_sock *client_sock)
 {
-	setsockopt(client_sock->osmo_fd->fd, IPPROTO_IP, IP_DROP_MEMBERSHIP,
+	setsockopt(client_sock->osmo_fd.fd, IPPROTO_IP, IP_DROP_MEMBERSHIP,
 		   client_sock->mcast_group, sizeof(*client_sock->mcast_group));
-	osmo_fd_unregister(client_sock->osmo_fd);
-	client_sock->osmo_fd->fd = -1;
-	client_sock->osmo_fd->when = 0;
-	close(client_sock->osmo_fd->fd);
+	osmo_fd_unregister(&client_sock->osmo_fd);
+	client_sock->osmo_fd.fd = -1;
+	client_sock->osmo_fd.when = 0;
+	close(client_sock->osmo_fd.fd);
 	talloc_free(client_sock->mcast_group);
-	talloc_free(client_sock->osmo_fd);
 	talloc_free(client_sock);
 
 }
 void mcast_server_sock_close(struct mcast_server_sock *serv_sock)
 {
-	close(serv_sock->osmo_fd->fd);
+	close(serv_sock->osmo_fd.fd);
 	talloc_free(serv_sock);
 }
 

-- 
To view, visit https://gerrit.osmocom.org/3232
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I11e0137849797eb67047b4ddca0e917c599ed909
Gerrit-PatchSet: 1
Gerrit-Project: osmocom-bb
Gerrit-Branch: master
Gerrit-Owner: Harald Welte <laforge at gnumonks.org>



More information about the gerrit-log mailing list