Commit 81a47d98 authored by Ben Walker's avatar Ben Walker Committed by Jim Harris
Browse files

sock/posix: Move repeated socket allocation code to a function

parent 913f780e
Loading
Loading
Loading
Loading
+29 −27
Original line number Diff line number Diff line
@@ -206,6 +206,33 @@ spdk_posix_sock_set_sendbuf(struct spdk_sock *_sock, int sz)
	return 0;
}

static struct spdk_posix_sock *
_spdk_posix_sock_alloc(int fd)
{
	struct spdk_posix_sock *sock;
	int rc;

	sock = calloc(1, sizeof(*sock));
	if (sock == NULL) {
		SPDK_ERRLOG("sock allocation failed\n");
		return NULL;
	}

	sock->fd = fd;

	rc = spdk_posix_sock_set_recvbuf(&sock->base, SO_RCVBUF_SIZE);
	if (rc) {
		/* Not fatal */
	}

	rc = spdk_posix_sock_set_sendbuf(&sock->base, SO_SNDBUF_SIZE);
	if (rc) {
		/* Not fatal */
	}

	return sock;
}

static struct spdk_sock *
spdk_posix_sock_create(const char *ip, int port, enum spdk_posix_sock_create_type type)
{
@@ -330,25 +357,13 @@ retry:
		return NULL;
	}

	sock = calloc(1, sizeof(*sock));
	sock = _spdk_posix_sock_alloc(fd);
	if (sock == NULL) {
		SPDK_ERRLOG("sock allocation failed\n");
		close(fd);
		return NULL;
	}

	sock->fd = fd;

	rc = spdk_posix_sock_set_recvbuf(&sock->base, SO_RCVBUF_SIZE);
	if (rc) {
		/* Not fatal */
	}

	rc = spdk_posix_sock_set_sendbuf(&sock->base, SO_SNDBUF_SIZE);
	if (rc) {
		/* Not fatal */
	}

	return &sock->base;
}

@@ -394,25 +409,12 @@ spdk_posix_sock_accept(struct spdk_sock *_sock)
		return NULL;
	}

	new_sock = calloc(1, sizeof(*sock));
	new_sock = _spdk_posix_sock_alloc(fd);
	if (new_sock == NULL) {
		SPDK_ERRLOG("sock allocation failed\n");
		close(fd);
		return NULL;
	}

	new_sock->fd = fd;

	rc = spdk_posix_sock_set_recvbuf(&new_sock->base, SO_RCVBUF_SIZE);
	if (rc) {
		/* Not fatal */
	}

	rc = spdk_posix_sock_set_sendbuf(&new_sock->base, SO_SNDBUF_SIZE);
	if (rc) {
		/* Not fatal */
	}

	return &new_sock->base;
}