Add libwebsockets_ensure_user_space
authorAlex Bligh <alex@alex.org.uk>
Mon, 7 Nov 2011 09:19:25 +0000 (17:19 +0800)
committerAndy Green <andy.green@linaro.org>
Mon, 7 Nov 2011 09:19:25 +0000 (17:19 +0800)
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>
lib/handshake.c
lib/libwebsockets.c
lib/libwebsockets.h

index 41ba01c..4cc698e 100644 (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 */
 
index 50aeaee..7d691c4 100644 (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;
+}
index bb41428..d65d8c1 100644 (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);