Skip to content

Commit

Permalink
Fixed overallocation of memory (#232)
Browse files Browse the repository at this point in the history
* Fixed overallocation of memory

* Fix overallocation of memory without modifying function definition
  • Loading branch information
nathanjjohnson7 authored Dec 9, 2024
1 parent 8606de6 commit e968ca2
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ struct us_listen_socket_t *us_socket_context_listen(int ssl, struct us_socket_co
return 0;
}

struct us_poll_t *p = us_create_poll(context->loop, 0, sizeof(struct us_listen_socket_t));
struct us_poll_t *p = us_create_poll(context->loop, 0, sizeof(struct us_listen_socket_t) - sizeof(struct us_poll_t));
us_poll_init(p, listen_socket_fd, POLL_TYPE_SEMI_SOCKET);
us_poll_start(p, context->loop, LIBUS_SOCKET_READABLE);

Expand Down Expand Up @@ -294,7 +294,7 @@ struct us_listen_socket_t *us_socket_context_listen_unix(int ssl, struct us_sock
return 0;
}

struct us_poll_t *p = us_create_poll(context->loop, 0, sizeof(struct us_listen_socket_t));
struct us_poll_t *p = us_create_poll(context->loop, 0, sizeof(struct us_listen_socket_t) - sizeof(struct us_poll_t));
us_poll_init(p, listen_socket_fd, POLL_TYPE_SEMI_SOCKET);
us_poll_start(p, context->loop, LIBUS_SOCKET_READABLE);

Expand Down Expand Up @@ -325,7 +325,7 @@ struct us_socket_t *us_socket_context_connect(int ssl, struct us_socket_context_
}

/* Connect sockets are semi-sockets just like listen sockets */
struct us_poll_t *p = us_create_poll(context->loop, 0, sizeof(struct us_socket_t) + socket_ext_size);
struct us_poll_t *p = us_create_poll(context->loop, 0, sizeof(struct us_socket_t) - sizeof(struct us_poll_t) + socket_ext_size);
us_poll_init(p, connect_socket_fd, POLL_TYPE_SEMI_SOCKET);
us_poll_start(p, context->loop, LIBUS_SOCKET_WRITABLE);

Expand Down Expand Up @@ -354,7 +354,7 @@ struct us_socket_t *us_socket_context_connect_unix(int ssl, struct us_socket_con
}

/* Connect sockets are semi-sockets just like listen sockets */
struct us_poll_t *p = us_create_poll(context->loop, 0, sizeof(struct us_socket_t) + socket_ext_size);
struct us_poll_t *p = us_create_poll(context->loop, 0, sizeof(struct us_socket_t) - sizeof(struct us_poll_t) + socket_ext_size);
us_poll_init(p, connect_socket_fd, POLL_TYPE_SEMI_SOCKET);
us_poll_start(p, context->loop, LIBUS_SOCKET_WRITABLE);

Expand Down
4 changes: 2 additions & 2 deletions src/crypto/openssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ void *us_internal_ssl_socket_context_get_native_handle(struct us_internal_ssl_so
struct us_internal_ssl_socket_context_t *us_internal_create_child_ssl_socket_context(struct us_internal_ssl_socket_context_t *context, int context_ext_size) {
/* Create a new non-SSL context */
struct us_socket_context_options_t options = {0};
struct us_internal_ssl_socket_context_t *child_context = (struct us_internal_ssl_socket_context_t *) us_create_socket_context(0, context->sc.loop, sizeof(struct us_internal_ssl_socket_context_t) + context_ext_size, options);
struct us_internal_ssl_socket_context_t *child_context = (struct us_internal_ssl_socket_context_t *) us_create_socket_context(0, context->sc.loop, sizeof(struct us_internal_ssl_socket_context_t) - sizeof(struct us_socket_context_t) + context_ext_size, options);

/* The only thing we share is SSL_CTX */
child_context->ssl_context = context->ssl_context;
Expand Down Expand Up @@ -645,7 +645,7 @@ struct us_internal_ssl_socket_context_t *us_internal_create_ssl_socket_context(s
}

/* Otherwise ee continue by creating a non-SSL context, but with larger ext to hold our SSL stuff */
struct us_internal_ssl_socket_context_t *context = (struct us_internal_ssl_socket_context_t *) us_create_socket_context(0, loop, sizeof(struct us_internal_ssl_socket_context_t) + context_ext_size, options);
struct us_internal_ssl_socket_context_t *context = (struct us_internal_ssl_socket_context_t *) us_create_socket_context(0, loop, sizeof(struct us_internal_ssl_socket_context_t) - sizeof(struct us_socket_context_t) + context_ext_size, options);

/* I guess this is the only optional callback */
context->on_server_name = NULL;
Expand Down
4 changes: 2 additions & 2 deletions src/eventing/epoll_kqueue.c
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ unsigned int us_internal_accept_poll_event(struct us_poll_t *p) {
/* Timer */
#ifdef LIBUS_USE_EPOLL
struct us_timer_t *us_create_timer(struct us_loop_t *loop, int fallthrough, unsigned int ext_size) {
struct us_poll_t *p = us_create_poll(loop, fallthrough, sizeof(struct us_internal_callback_t) + ext_size);
struct us_poll_t *p = us_create_poll(loop, fallthrough, sizeof(struct us_internal_callback_t) - sizeof(struct us_poll_t) + ext_size);
int timerfd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK | TFD_CLOEXEC);
if (timerfd == -1) {
return NULL;
Expand Down Expand Up @@ -372,7 +372,7 @@ void us_timer_set(struct us_timer_t *t, void (*cb)(struct us_timer_t *t), int ms
/* Async (internal helper for loop's wakeup feature) */
#ifdef LIBUS_USE_EPOLL
struct us_internal_async *us_internal_create_async(struct us_loop_t *loop, int fallthrough, unsigned int ext_size) {
struct us_poll_t *p = us_create_poll(loop, fallthrough, sizeof(struct us_internal_callback_t) + ext_size);
struct us_poll_t *p = us_create_poll(loop, fallthrough, sizeof(struct us_internal_callback_t) - sizeof(struct us_poll_t) + ext_size);
us_poll_init(p, eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC), POLL_TYPE_CALLBACK);

struct us_internal_callback_t *cb = (struct us_internal_callback_t *) p;
Expand Down
2 changes: 1 addition & 1 deletion src/udp.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ struct us_udp_socket_t *us_create_udp_socket(struct us_loop_t *loop, struct us_u
int ext_size = 0;
int fallthrough = 0;

struct us_poll_t *p = us_create_poll(loop, fallthrough, sizeof(struct us_internal_udp_t) + ext_size);
struct us_poll_t *p = us_create_poll(loop, fallthrough, sizeof(struct us_internal_udp_t) - sizeof(struct us_poll_t) + ext_size);
us_poll_init(p, fd, POLL_TYPE_CALLBACK);

struct us_internal_udp_t *cb = (struct us_internal_udp_t *) p;
Expand Down

0 comments on commit e968ca2

Please sign in to comment.