Skip to content

Commit

Permalink
Rightsize the storage structures
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Sustrik <[email protected]>
  • Loading branch information
sustrik committed Apr 22, 2018
1 parent cbc581a commit 141200a
Show file tree
Hide file tree
Showing 13 changed files with 45 additions and 32 deletions.
3 changes: 1 addition & 2 deletions dtls.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ struct dill_dtls_sock {
unsigned int mem : 1;
};

DILL_CT_ASSERT(sizeof(struct dill_dtls_storage) >=
sizeof(struct dill_dtls_sock));
DILL_CHECK_STORAGE(dill_dtls_sock, dill_dtls_storage)

static void dill_dtls_init(void);
static BIO *dill_dtls_new_cbio(void *mem);
Expand Down
3 changes: 1 addition & 2 deletions http.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ struct dill_http_sock {
char rxbuf[1024];
};

DILL_CT_ASSERT(sizeof(struct dill_http_storage) >=
sizeof(struct dill_http_sock));
DILL_CHECK_STORAGE(dill_http_sock, dill_http_storage)

static void *dill_http_hquery(struct dill_hvfs *hvfs, const void *type) {
struct dill_http_sock *obj = (struct dill_http_sock*)hvfs;
Expand Down
5 changes: 2 additions & 3 deletions ipc.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ struct dill_ipc_conn {
unsigned int mem : 1;
};

DILL_CT_ASSERT(sizeof(struct dill_ipc_storage) >= sizeof(struct dill_ipc_conn));
DILL_CHECK_STORAGE(dill_ipc_conn, dill_ipc_storage)

static void *dill_ipc_hquery(struct dill_hvfs *hvfs, const void *type) {
struct dill_ipc_conn *self = (struct dill_ipc_conn*)hvfs;
Expand Down Expand Up @@ -210,8 +210,7 @@ struct dill_ipc_listener {
unsigned int mem : 1;
};

DILL_CT_ASSERT(sizeof(struct dill_ipc_listener_storage) >=
sizeof(struct dill_ipc_listener));
DILL_CHECK_STORAGE(dill_ipc_listener, dill_ipc_listener_storage)

static void *dill_ipc_listener_hquery(struct dill_hvfs *hvfs, const void *type) {
struct dill_ipc_listener *self = (struct dill_ipc_listener*)hvfs;
Expand Down
24 changes: 12 additions & 12 deletions libdill.h
Original file line number Diff line number Diff line change
Expand Up @@ -459,9 +459,9 @@ DILL_EXPORT int dill_ipaddr_equal(
/* TCP protocol. */
/******************************************************************************/

struct dill_tcp_listener_storage {char _[72];};
struct dill_tcp_listener_storage {char _[56];};

struct dill_tcp_storage {char _[2080];};
struct dill_tcp_storage {char _[2064];};

DILL_EXPORT int dill_tcp_listen(
struct dill_ipaddr *addr,
Expand Down Expand Up @@ -510,11 +510,11 @@ DILL_EXPORT int dill_tcp_close(
/* IPC protocol. */
/******************************************************************************/

struct dill_ipc_listener_storage {char _[40];};
struct dill_ipc_listener_storage {char _[24];};

struct dill_ipc_storage {char _[2080];};
struct dill_ipc_storage {char _[2064];};

struct dill_ipc_pair_storage {char _[4160];};
struct dill_ipc_pair_storage {char _[4128];};

DILL_EXPORT int dill_ipc_listen(
const char *addr,
Expand Down Expand Up @@ -570,7 +570,7 @@ DILL_EXPORT int dill_ipc_pair_mem(
/* Messages are prefixed by size. */
/******************************************************************************/

struct dill_prefix_storage {char _[72];};
struct dill_prefix_storage {char _[56];};

#define DILL_PREFIX_BIG_ENDIAN 0
#define DILL_PREFIX_LITTLE_ENDIAN 1
Expand Down Expand Up @@ -601,7 +601,7 @@ DILL_EXPORT int dill_prefix_detach(
/* Messages are suffixed by specified string of bytes. */
/******************************************************************************/

struct dill_suffix_storage {char _[144];};
struct dill_suffix_storage {char _[128];};

DILL_EXPORT int dill_suffix_attach(
int s,
Expand All @@ -628,7 +628,7 @@ DILL_EXPORT int dill_suffix_detach(
/* Each UDP packet is treated as a separate message. */
/******************************************************************************/

struct dill_udp_storage {char _[88];};
struct dill_udp_storage {char _[72];};

DILL_EXPORT int dill_udp_open(
struct dill_ipaddr *local,
Expand Down Expand Up @@ -674,7 +674,7 @@ DILL_EXPORT ssize_t dill_udp_recvl(
/* HTTP */
/******************************************************************************/

struct dill_http_storage {char _[1312];};
struct dill_http_storage {char _[1296];};

DILL_EXPORT int dill_http_attach(
int s);
Expand Down Expand Up @@ -742,7 +742,7 @@ DILL_EXPORT int dill_http_recvfield(
/* TLS protocol. */
/******************************************************************************/

struct dill_tls_storage {char _[88];};
struct dill_tls_storage {char _[72];};

DILL_EXPORT int dill_tls_attach_server(
int s,
Expand Down Expand Up @@ -826,7 +826,7 @@ DILL_EXPORT int dill_dtls_detach(
/* WebSockets protocol. */
/******************************************************************************/

struct dill_ws_storage {char _[288];};
struct dill_ws_storage {char _[176];};

#define DILL_WS_BINARY 0
#define DILL_WS_TEXT 1
Expand Down Expand Up @@ -939,7 +939,7 @@ DILL_EXPORT int dill_ws_response_key(
/* Implementes terminal handshake on the top of any message-based protocol. */
/******************************************************************************/

struct dill_term_storage {char _[104];};
struct dill_term_storage {char _[88];};

DILL_EXPORT int dill_term_attach(
int s,
Expand Down
3 changes: 1 addition & 2 deletions prefix.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ struct dill_prefix_sock {
unsigned int mem : 1;
};

DILL_CT_ASSERT(sizeof(struct dill_prefix_storage) >=
sizeof(struct dill_prefix_sock));
DILL_CHECK_STORAGE(dill_prefix_sock, dill_prefix_storage)

static void *dill_prefix_hquery(struct dill_hvfs *hvfs, const void *type) {
struct dill_prefix_sock *self = (struct dill_prefix_sock*)hvfs;
Expand Down
3 changes: 1 addition & 2 deletions suffix.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ struct dill_suffix_sock {
unsigned int mem : 1;
};

DILL_CT_ASSERT(sizeof(struct dill_suffix_storage) >=
sizeof(struct dill_suffix_sock));
DILL_CHECK_STORAGE(dill_suffix_sock, dill_suffix_storage)

static void *dill_suffix_hquery(struct dill_hvfs *hvfs, const void *type) {
struct dill_suffix_sock *self = (struct dill_suffix_sock*)hvfs;
Expand Down
5 changes: 2 additions & 3 deletions tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ struct dill_tcp_conn {
unsigned int mem : 1;
};

DILL_CT_ASSERT(sizeof(struct dill_tcp_storage) >= sizeof(struct dill_tcp_conn));
DILL_CHECK_STORAGE(dill_tcp_conn, dill_tcp_storage)

static void *dill_tcp_hquery(struct dill_hvfs *hvfs, const void *type) {
struct dill_tcp_conn *self = (struct dill_tcp_conn*)hvfs;
Expand Down Expand Up @@ -205,8 +205,7 @@ struct dill_tcp_listener {
unsigned int mem : 1;
};

DILL_CT_ASSERT(sizeof(struct dill_tcp_listener_storage) >=
sizeof(struct dill_tcp_listener));
DILL_CHECK_STORAGE(dill_tcp_listener, dill_tcp_listener_storage)

static void *dill_tcp_listener_hquery(struct dill_hvfs *hvfs,
const void *type) {
Expand Down
3 changes: 1 addition & 2 deletions term.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ struct dill_term_sock {
unsigned int mem : 1;
};

DILL_CT_ASSERT(sizeof(struct dill_term_storage) >=
sizeof(struct dill_term_sock));
DILL_CHECK_STORAGE(dill_term_sock, dill_term_storage)

static void *dill_term_hquery(struct dill_hvfs *hvfs, const void *type) {
struct dill_term_sock *self = (struct dill_term_sock*)hvfs;
Expand Down
2 changes: 1 addition & 1 deletion tls.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ struct dill_tls_sock {
unsigned int mem : 1;
};

DILL_CT_ASSERT(sizeof(struct dill_tls_storage) >= sizeof(struct dill_tls_sock));
DILL_CHECK_STORAGE(dill_tls_sock, dill_tls_storage)

static void dill_tls_init(void);
static BIO *dill_tls_new_cbio(void *mem);
Expand Down
2 changes: 1 addition & 1 deletion udp.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ struct dill_udp_sock {
unsigned int mem : 1;
};

DILL_CT_ASSERT(sizeof(struct dill_udp_storage) >= sizeof(struct dill_udp_sock));
DILL_CHECK_STORAGE(dill_udp_sock, dill_udp_storage)

static void *dill_udp_hquery(struct dill_hvfs *hvfs, const void *type) {
struct dill_udp_sock *obj = (struct dill_udp_sock*)hvfs;
Expand Down
3 changes: 3 additions & 0 deletions utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@

#include "utils.h"

void dill_print_size(char x) {
}

int dill_maxfds(void) {
/* Get the maximum number of file descriptors. */
struct rlimit rlim;
Expand Down
19 changes: 18 additions & 1 deletion utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,24 @@
#define DILL_CT_ASSERT_HELPER1(prefix, line) \
DILL_CT_ASSERT_HELPER2(prefix, line)
#define DILL_CT_ASSERT(x) \
typedef int DILL_CT_ASSERT_HELPER1(ct_assert_,__COUNTER__) [(x) ? 1 : -1]
typedef int DILL_CT_ASSERT_HELPER1(ct_assert_,__COUNTER__) [(x) ? 1 : -1];

/* DILL_CHECK_STORAGE checks whether struct 'type' fits into struct 'storage'.
If DILL_PRINT_SIZES macro is defined it will also print the size
of 'type'. */
#if defined DILL_PRINT_SIZES
#define DILL_CHECK_STORAGE(type, storage) \
static void dill_print_size_2_##type(void) { \
char x[sizeof(struct type)]; \
dill_print_size(&x); \
} \
DILL_CT_ASSERT(sizeof(struct type) <= sizeof(struct storage))
#else
#define DILL_CHECK_STORAGE(type, storage) \
DILL_CT_ASSERT(sizeof(struct type) <= sizeof(struct storage))
#endif

void dill_print_size(char x);

/* Optimisation hints. */
#if defined __GNUC__ || defined __llvm__
Expand Down
2 changes: 1 addition & 1 deletion ws.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ struct dill_ws_sock {
uint8_t msg[128];
};

DILL_CT_ASSERT(sizeof(struct dill_ws_storage) >= sizeof(struct dill_ws_sock));
DILL_CHECK_STORAGE(dill_ws_sock, dill_ws_storage)

static void *dill_ws_hquery(struct dill_hvfs *hvfs, const void *type);
static void dill_ws_hclose(struct dill_hvfs *hvfs);
Expand Down

0 comments on commit 141200a

Please sign in to comment.