summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke T. Shumaker <lukeshu@lukeshu.com>2024-10-29 01:02:02 -0600
committerLuke T. Shumaker <lukeshu@lukeshu.com>2024-10-29 01:40:02 -0600
commit7ff738390e55d57a0f513c467a9da3b08c6902ab (patch)
tree384bd03462a9790c9c747b895cf5de0677659db7
parentaffe77a2b068a9de236f0d7703092dbfafacff5e (diff)
Add a way to make private object members
-rw-r--r--lib9p/include/lib9p/srv.h20
-rw-r--r--lib9p/srv.c16
-rw-r--r--libhw/host_include/libhw/host_net.h8
-rw-r--r--libhw/host_net.c2
-rw-r--r--libhw/rp2040_hwspi.c1
-rw-r--r--libhw/rp2040_include/libhw/rp2040_hwspi.h4
-rw-r--r--libhw/rp2040_include/libhw/w5500.h7
-rw-r--r--libhw/w5500.c1
-rw-r--r--libmisc/include/libmisc/private.h37
9 files changed, 85 insertions, 11 deletions
diff --git a/lib9p/include/lib9p/srv.h b/lib9p/include/lib9p/srv.h
index 0617e57..ab7ec43 100644
--- a/lib9p/include/lib9p/srv.h
+++ b/lib9p/include/lib9p/srv.h
@@ -11,6 +11,7 @@
#include <libcr_ipc/rpc.h>
#include <libcr_ipc/chan.h>
#include <libhw/generic/net.h>
+#include <libmisc/private.h>
#include <lib9p/9p.h>
@@ -23,21 +24,14 @@ struct lib9p_srv_ctx {
uint32_t uid;
char *uname;
+ BEGIN_PRIVATE(LIB9P_SRV_H)
_lib9p_srv_flushch_t _flushch;
+ END_PRIVATE(LIB9P_SRV_H)
};
-static inline bool lib9p_srv_flush_requested(struct lib9p_srv_ctx *ctx) {
- assert(ctx);
- return _lib9p_srv_flushch_can_send(&ctx->_flushch);
-}
+bool lib9p_srv_flush_requested(struct lib9p_srv_ctx *ctx);
-static inline int lib9p_srv_acknowledge_flush(struct lib9p_srv_ctx *ctx) {
- assert(ctx);
- assert(_lib9p_srv_flushch_can_send(&ctx->_flushch));
- lib9p_error(&ctx->basectx, LINUX_ECANCELED, "request canceled by flush");
- _lib9p_srv_flushch_send(&ctx->_flushch, true);
- return -1;
-}
+int lib9p_srv_acknowledge_flush(struct lib9p_srv_ctx *ctx);
/* interface definitions ******************************************************/
@@ -47,6 +41,7 @@ struct __lib9p_srv_file;
typedef struct __lib9p_srv_file {
struct lib9p_srv_file_vtable *vtable;
+ BEGIN_PRIVATE(LIB9P_SRV_H)
/* Managed by srv.c, but should be cloned by ->vtable->clone(). */
struct __lib9p_srv_file *_parent_dir; /* clone this
@@ -54,6 +49,7 @@ typedef struct __lib9p_srv_file {
/* ref type 1: an entry in fidmap
* ref type 2: ->_parent_dir of another file */
unsigned int _refcount;
+ END_PRIVATE(LIB9P_SRV_H)
} implements_lib9p_srv_file;
struct lib9p_srv_file_vtable {
@@ -103,7 +99,9 @@ struct lib9p_srv {
implements_lib9p_srv_file *(*rootdir)(struct lib9p_srv_ctx *, char *treename);
/* For internal use */
+ BEGIN_PRIVATE(LIB9P_SRV_H)
_lib9p_srv_reqch_t _reqch;
+ END_PRIVATE(LIB9P_SRV_H)
};
/**
diff --git a/lib9p/srv.c b/lib9p/srv.c
index 3a3807c..9192794 100644
--- a/lib9p/srv.c
+++ b/lib9p/srv.c
@@ -16,9 +16,25 @@
#include <libcr_ipc/select.h>
#include <libmisc/vcall.h>
+#define IMPLEMENTATION_FOR_LIB9P_SRV_H YES
#include <lib9p/srv.h>
#include "internal.h"
+/* context ********************************************************************/
+
+bool lib9p_srv_flush_requested(struct lib9p_srv_ctx *ctx) {
+ assert(ctx);
+ return _lib9p_srv_flushch_can_send(&ctx->_flushch);
+}
+
+int lib9p_srv_acknowledge_flush(struct lib9p_srv_ctx *ctx) {
+ assert(ctx);
+ assert(_lib9p_srv_flushch_can_send(&ctx->_flushch));
+ lib9p_error(&ctx->basectx, LINUX_ECANCELED, "request canceled by flush");
+ _lib9p_srv_flushch_send(&ctx->_flushch, true);
+ return -1;
+}
+
/* structs ********************************************************************/
#define FIDFLAG_OPEN_R (1<<0)
diff --git a/libhw/host_include/libhw/host_net.h b/libhw/host_include/libhw/host_net.h
index 1b1ba7a..bfef5c9 100644
--- a/libhw/host_include/libhw/host_net.h
+++ b/libhw/host_include/libhw/host_net.h
@@ -9,20 +9,26 @@
#include <stdint.h> /* for uint16_6 */
+#include <libmisc/private.h>
+
#include <libhw/generic/net.h>
struct _hostnet_tcp_conn {
implements_net_stream_conn;
+ BEGIN_PRIVATE(LIBHW_HOST_NET_H)
int fd;
uint64_t read_deadline_ns;
+ END_PRIVATE(LIBHW_HOST_NET_H)
};
struct hostnet_tcp_listener {
implements_net_stream_listener;
+ BEGIN_PRIVATE(LIBHW_HOST_NET_H)
int fd;
struct _hostnet_tcp_conn active_conn;
+ END_PRIVATE(LIBHW_HOST_NET_H)
};
void hostnet_tcp_listener_init(struct hostnet_tcp_listener *self, uint16_t port);
@@ -30,8 +36,10 @@ void hostnet_tcp_listener_init(struct hostnet_tcp_listener *self, uint16_t port)
struct hostnet_udp_conn {
implements_net_packet_conn;
+ BEGIN_PRIVATE(LIBHW_HOST_NET_H)
int fd;
uint64_t read_deadline_ns;
+ END_PRIVATE(LIBHW_HOST_NET_H)
};
void hostnet_udp_conn_init(struct hostnet_udp_conn *self, uint16_t port);
diff --git a/libhw/host_net.c b/libhw/host_net.c
index 1563ced..904019a 100644
--- a/libhw/host_net.c
+++ b/libhw/host_net.c
@@ -23,6 +23,8 @@
#include <libmisc/vcall.h>
#include <libhw/generic/bootclock.h>
+
+#define IMPLEMENTATION_FOR_LIBHW_HOST_NET_H YES
#include <libhw/host_net.h>
/* common *********************************************************************/
diff --git a/libhw/rp2040_hwspi.c b/libhw/rp2040_hwspi.c
index 96c3d14..b4adeb7 100644
--- a/libhw/rp2040_hwspi.c
+++ b/libhw/rp2040_hwspi.c
@@ -12,6 +12,7 @@
#include <libmisc/vcall.h>
+#define IMPLEMENTATION_FOR_LIBHW_RP2040_HWSPI_H YES
#include <libhw/rp2040_hwspi.h>
static void rp2040_hwspi_readwritev(implements_spi *, const struct bidi_iovec *iov, int iovcnt);
diff --git a/libhw/rp2040_include/libhw/rp2040_hwspi.h b/libhw/rp2040_include/libhw/rp2040_hwspi.h
index db8285d..8d90dbb 100644
--- a/libhw/rp2040_include/libhw/rp2040_hwspi.h
+++ b/libhw/rp2040_include/libhw/rp2040_hwspi.h
@@ -10,6 +10,8 @@
#include <pico/binary_info.h> /* for bi_* */
+#include <libmisc/private.h>
+
#include <libhw/generic/spi.h>
enum rp2040_hwspi_instance {
@@ -20,8 +22,10 @@ enum rp2040_hwspi_instance {
struct rp2040_hwspi {
implements_spi;
+ BEGIN_PRIVATE(LIBHW_RP2040_HWSPI_H)
void /*spi_inst_t*/ *inst;
uint pin_cs;
+ END_PRIVATE(LIBHW_RP2040_HWSPI_H)
};
/**
diff --git a/libhw/rp2040_include/libhw/w5500.h b/libhw/rp2040_include/libhw/w5500.h
index 6f12c39..037d4f1 100644
--- a/libhw/rp2040_include/libhw/w5500.h
+++ b/libhw/rp2040_include/libhw/w5500.h
@@ -9,6 +9,7 @@
#include <libcr_ipc/sema.h>
#include <libcr_ipc/mutex.h>
+#include <libmisc/private.h>
#include <libhw/generic/net.h>
#include <libhw/generic/spi.h>
@@ -17,13 +18,16 @@ struct _w5500_tcp_conn {
/* const-after-init */
implements_net_stream_conn;
/* mutable */
+ BEGIN_PRIVATE(LIBHW_W5500_H)
bool read_open;
bool write_open;
+ END_PRIVATE(LIBHW_W5500_H)
};
struct _w5500_tcp_listener {
/* const-after-init */
implements_net_stream_listener;
+ BEGIN_PRIVATE(LIBHW_W5500_H)
uint8_t socknum;
struct _w5500_tcp_conn active_conn;
@@ -31,11 +35,13 @@ struct _w5500_tcp_listener {
uint16_t port;
cr_mutex_t cmd_mu;
cr_sema_t listen_sema, read_sema;
+ END_PRIVATE(LIBHW_W5500_H)
};
struct w5500 {
/* const-after-init */
implements_spi *spidev;
+ BEGIN_PRIVATE(LIBHW_W5500_H)
uint pin_intr;
uint pin_reset;
struct net_eth_addr hwaddr;
@@ -44,6 +50,7 @@ struct w5500 {
uint16_t next_local_port;
struct _w5500_tcp_listener listeners[8];
cr_sema_t intr;
+ END_PRIVATE(LIBHW_W5500_H)
};
/**
diff --git a/libhw/w5500.c b/libhw/w5500.c
index a141fb3..0d9cd01 100644
--- a/libhw/w5500.c
+++ b/libhw/w5500.c
@@ -73,6 +73,7 @@
#include <libcr/coroutine.h> /* for cr_yield() */
#include <libmisc/vcall.h> /* for VCALL_SELF() */
+#define IMPLEMENTATION_FOR_LIBHW_W5500_H YES
#include <libhw/w5500.h>
#include "w5500_ll.h"
diff --git a/libmisc/include/libmisc/private.h b/libmisc/include/libmisc/private.h
new file mode 100644
index 0000000..0eb8c42
--- /dev/null
+++ b/libmisc/include/libmisc/private.h
@@ -0,0 +1,37 @@
+/* libmisc/private.h - Utilities to hide struct fields
+ *
+ * Copyright (C) 2024 Luke T. Shumaker <lukeshu@lukeshu.com>
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+#ifndef _LIBMISC_CPPUTIL_H_
+#define _LIBMISC_CPPUTIL_H_
+
+/* primitive utilities */
+
+#define _SECOND(a, b, ...) b
+
+#define _CAT(a, b) a ## b
+#define _CAT2(a, b) _CAT(a, b)
+#define _EAT(...)
+#define _EXPAND(...) __VA_ARGS__
+
+/* conditionals */
+
+#define _T xxTxx
+#define _F xxFxx
+
+#define _SENTINEL() bogus, _T /* a magic sentinel value */
+#define _IS_SENTINEL(...) _SECOND(__VA_ARGS__, _F)
+
+#define _IF(cond) _CAT(_IF__, cond) /* _IF(cond)(then)(else) */
+#define _IF__xxTxx(...) __VA_ARGS__ _EAT
+#define _IF__xxFxx(...) _EXPAND
+
+/* high-level functionality */
+
+#define YES _SENTINEL()
+#define BEGIN_PRIVATE(name) _IF(_IS_SENTINEL(IMPLEMENTATION_FOR_##name))()(struct {)
+#define END_PRIVATE(name) _IF(_IS_SENTINEL(IMPLEMENTATION_FOR_##name))()(} _CAT2(_HIDDEN_, __COUNTER__);)
+
+#endif /* _LIBMISC_CPPUTIL_H_ */