Add libwebsockets_ensure_user_space

This patch unifies the code for per-connection user allocation, and allows
it to be allocated earlier, duiring HTTP service time.  It's also OK to
attempt allocation multiple times, it will just return any existing
allocation.

Signed-off-by: Alex Bligh <alex@alex.org.uk>
This commit is contained in:
Alex Bligh 2011-11-07 17:19:25 +08:00 committed by Andy Green
parent 2f5b9bd30e
commit 49146dbcce
3 changed files with 27 additions and 33 deletions

View file

@ -91,17 +91,8 @@ handshake_00(struct libwebsocket_context *context, struct libwebsocket *wsi)
goto bail;
/* allocate the per-connection user memory (if any) */
if (wsi->protocol->per_session_data_size) {
wsi->user_space = malloc(
wsi->protocol->per_session_data_size);
if (wsi->user_space == NULL) {
fprintf(stderr, "Out of memory for "
"conn user space\n");
goto bail;
}
} else
wsi->user_space = NULL;
if (wsi->protocol->per_session_data_size && !libwebsocket_ensure_user_space(wsi))
goto bail;
/* create the response packet */
@ -275,17 +266,8 @@ handshake_0405(struct libwebsocket_context *context, struct libwebsocket *wsi)
}
/* allocate the per-connection user memory (if any) */
if (wsi->protocol->per_session_data_size) {
wsi->user_space = malloc(
wsi->protocol->per_session_data_size);
if (wsi->user_space == NULL) {
fprintf(stderr, "Out of memory for "
"conn user space\n");
goto bail;
}
} else
wsi->user_space = NULL;
if (wsi->protocol->per_session_data_size && !libwebsocket_ensure_user_space(wsi))
goto bail;
/* create the response packet */

View file

@ -1373,17 +1373,8 @@ check_accept:
accept_ok:
/* allocate the per-connection user memory (if any) */
if (wsi->protocol->per_session_data_size) {
wsi->user_space = malloc(
wsi->protocol->per_session_data_size);
if (wsi->user_space == NULL) {
fprintf(stderr, "Out of memory for "
"conn user space\n");
goto bail2;
}
} else
wsi->user_space = NULL;
if (wsi->protocol->per_session_data_size && !libwebsocket_ensure_user_space(wsi))
goto bail2;
/* clear his proxy connection timeout */
@ -3096,3 +3087,21 @@ libwebsocket_is_final_fragment(struct libwebsocket *wsi)
{
return wsi->final;
}
void *
libwebsocket_ensure_user_space(struct libwebsocket *wsi)
{
/* allocate the per-connection user memory (if any) */
if (wsi->protocol->per_session_data_size && !wsi->user_space) {
wsi->user_space = malloc(
wsi->protocol->per_session_data_size);
if (wsi->user_space == NULL) {
fprintf(stderr, "Out of memory for "
"conn user space\n");
return NULL;
}
memset(wsi->user_space, 0, wsi->protocol->per_session_data_size);
}
return wsi->user_space;
}

View file

@ -647,6 +647,9 @@ libwebsocket_get_socket_fd(struct libwebsocket *wsi);
LWS_EXTERN int
libwebsocket_is_final_fragment(struct libwebsocket *wsi);
LWS_EXTERN void *
libwebsocket_ensure_user_space(struct libwebsocket *wsi);
LWS_EXTERN int
libwebsocket_rx_flow_control(struct libwebsocket *wsi, int enable);