[Pdbg] [PATCH 12/17] libsbefifo: Refactor sbefifo_init / sbefifo_free api
Amitay Isaacs
amitay at ozlabs.org
Thu Oct 31 17:34:23 AEDT 2019
This allows to create sbefifo context without having to connect using
specific backend.
Signed-off-by: Amitay Isaacs <amitay at ozlabs.org>
---
libsbefifo/connect.c | 34 ++++++++++++++++++++++++----------
libsbefifo/libsbefifo.h | 3 +++
2 files changed, 27 insertions(+), 10 deletions(-)
diff --git a/libsbefifo/connect.c b/libsbefifo/connect.c
index 08194e1..96614ce 100644
--- a/libsbefifo/connect.c
+++ b/libsbefifo/connect.c
@@ -24,25 +24,42 @@
#include "libsbefifo.h"
#include "sbefifo_private.h"
-int sbefifo_connect(const char *fifo_path, struct sbefifo_context **out)
+int sbefifo_init(struct sbefifo_context **out)
{
struct sbefifo_context *sctx;
- int fd, rc;
sctx = malloc(sizeof(struct sbefifo_context));
- if (!sctx) {
- fprintf(stderr, "Memory allocation error\n");
+ if (!sctx)
return ENOMEM;
- }
*sctx = (struct sbefifo_context) {
.fd = -1,
};
+ *out = sctx;
+ return 0;
+}
+
+void sbefifo_free(struct sbefifo_context *sctx)
+{
+ if (sctx->ffdc)
+ free(sctx->ffdc);
+
+ free(sctx);
+}
+
+int sbefifo_connect(const char *fifo_path, struct sbefifo_context **out)
+{
+ struct sbefifo_context *sctx;
+ int fd, rc;
+
+ rc = sbefifo_init(&sctx);
+ if (rc)
+ return rc;
+
fd = open(fifo_path, O_RDWR | O_SYNC);
if (fd < 0) {
rc = errno;
- fprintf(stderr, "Error opening fifo %s\n", fifo_path);
free(sctx);
return rc;
}
@@ -58,10 +75,7 @@ void sbefifo_disconnect(struct sbefifo_context *sctx)
if (sctx->fd != -1)
close(sctx->fd);
- if (sctx->ffdc)
- free(sctx->ffdc);
-
- free(sctx);
+ sbefifo_free(sctx);
}
void sbefifo_debug(const char *fmt, ...)
diff --git a/libsbefifo/libsbefifo.h b/libsbefifo/libsbefifo.h
index 4d45bbf..be0defd 100644
--- a/libsbefifo/libsbefifo.h
+++ b/libsbefifo/libsbefifo.h
@@ -48,6 +48,9 @@
struct sbefifo_context;
+int sbefifo_init(struct sbefifo_context **out);
+void sbefifo_free(struct sbefifo_context *sctx);
+
int sbefifo_connect(const char *fifo_path, struct sbefifo_context **out);
void sbefifo_disconnect(struct sbefifo_context *sctx);
--
2.21.0
More information about the Pdbg
mailing list