From 8ee92c0e1cee8267ce0937a967e83904508491c9 Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Mon, 28 Oct 2013 09:11:45 -0700 Subject: [PATCH] Track number of active pool integers. Track output streams. --- src/libguac/client-handlers.c | 8 ++++---- src/libguac/client.c | 31 ++++++++++++++++++++++--------- src/libguac/guacamole/client.h | 7 ++++++- src/libguac/guacamole/pool.h | 27 ++++++++++++++++----------- src/libguac/pool.c | 7 ++++++- 5 files changed, 54 insertions(+), 26 deletions(-) diff --git a/src/libguac/client-handlers.c b/src/libguac/client-handlers.c index 7a69d72d..6f7428c5 100644 --- a/src/libguac/client-handlers.c +++ b/src/libguac/client-handlers.c @@ -148,7 +148,7 @@ int __guac_handle_file(guac_client* client, guac_instruction* instruction) { } /* Initialize stream */ - stream = &(client->__streams[stream_index]); + stream = &(client->__input_streams[stream_index]); stream->index = stream_index; stream->data = NULL; @@ -176,7 +176,7 @@ int __guac_handle_ack(guac_client* client, guac_instruction* instruction) { if (stream_index < 0 || stream_index >= GUAC_CLIENT_MAX_STREAMS) return 0; - stream = &(client->__streams[stream_index]); + stream = &(client->__output_streams[stream_index]); /* Validate initialization of stream */ if (stream->index == GUAC_CLIENT_CLOSED_STREAM_INDEX) @@ -206,7 +206,7 @@ int __guac_handle_blob(guac_client* client, guac_instruction* instruction) { return 0; } - stream = &(client->__streams[stream_index]); + stream = &(client->__input_streams[stream_index]); /* Validate initialization of stream */ if (stream->index == GUAC_CLIENT_CLOSED_STREAM_INDEX) { @@ -253,7 +253,7 @@ int __guac_handle_end(guac_client* client, guac_instruction* instruction) { return 0; } - stream = &(client->__streams[stream_index]); + stream = &(client->__input_streams[stream_index]); if (client->end_handler) result = client->end_handler(client, stream); diff --git a/src/libguac/client.c b/src/libguac/client.c index 7e8debe5..796c2b0e 100644 --- a/src/libguac/client.c +++ b/src/libguac/client.c @@ -97,9 +97,20 @@ void guac_client_free_layer(guac_client* client, guac_layer* layer) { guac_stream* guac_client_alloc_stream(guac_client* client) { - /* Init new stream */ - guac_stream* allocd_stream = malloc(sizeof(guac_stream)); - allocd_stream->index = guac_pool_next_int(client->__stream_pool); + guac_stream* allocd_stream; + int stream_index; + + /* Refuse to allocate beyond maximum */ + if (client->__stream_pool->active == GUAC_CLIENT_MAX_STREAMS) + return NULL; + + /* Allocate stream */ + stream_index = guac_pool_next_int(client->__stream_pool); + + /* Initialize stream */ + allocd_stream = &(client->__output_streams[stream_index]); + allocd_stream->index = stream_index; + allocd_stream->data = NULL; return allocd_stream; @@ -108,10 +119,10 @@ guac_stream* guac_client_alloc_stream(guac_client* client) { void guac_client_free_stream(guac_client* client, guac_stream* stream) { /* Release index to pool */ - guac_pool_free_int(client->__stream_pool, stream->index - 1); - - /* Free stream */ - free(stream); + guac_pool_free_int(client->__stream_pool, stream->index); + + /* Mark stream as closed */ + stream->index = GUAC_CLIENT_CLOSED_STREAM_INDEX; } @@ -143,8 +154,10 @@ guac_client* guac_client_alloc() { client->__stream_pool = guac_pool_alloc(0); /* Initialze streams */ - for (i=0; i__streams[i].index = GUAC_CLIENT_CLOSED_STREAM_INDEX; + for (i=0; i__input_streams[i].index = GUAC_CLIENT_CLOSED_STREAM_INDEX; + client->__output_streams[i].index = GUAC_CLIENT_CLOSED_STREAM_INDEX; + } return client; diff --git a/src/libguac/guacamole/client.h b/src/libguac/guacamole/client.h index f1f79d4d..3bd36e31 100644 --- a/src/libguac/guacamole/client.h +++ b/src/libguac/guacamole/client.h @@ -555,10 +555,15 @@ struct guac_client { */ guac_pool* __stream_pool; + /** + * All available output streams (data going to connected client). + */ + guac_stream __output_streams[GUAC_CLIENT_MAX_STREAMS]; + /** * All available input streams (data coming from connected client). */ - guac_stream __streams[GUAC_CLIENT_MAX_STREAMS]; + guac_stream __input_streams[GUAC_CLIENT_MAX_STREAMS]; }; diff --git a/src/libguac/guacamole/pool.h b/src/libguac/guacamole/pool.h index 629d863b..72deb750 100644 --- a/src/libguac/guacamole/pool.h +++ b/src/libguac/guacamole/pool.h @@ -62,6 +62,11 @@ typedef struct guac_pool { */ int min_size; + /** + * The number of integers currently in use. + */ + int active; + /** * The next integer to be released (after no more integers remain in the * pool. @@ -102,8 +107,8 @@ struct guac_pool_int { * Allocates a new guac_pool having the given minimum size. * * @param size The minimum number of integers which must have been returned by - * guac_pool_next_int before freed integers (previously used integers) - * are allowed to be returned. + * guac_pool_next_int before freed integers (previously used + * integers) are allowed to be returned. * @return A new, empty guac_pool, having the given minimum size. */ guac_pool* guac_pool_alloc(int size); @@ -116,23 +121,23 @@ guac_pool* guac_pool_alloc(int size); void guac_pool_free(guac_pool* pool); /** - * Returns the next available integer from the given guac_pool. All integers returned are - * non-negative, and are returned in sequences, starting from 0. + * Returns the next available integer from the given guac_pool. All integers + * returned are non-negative, and are returned in sequences, starting from 0. * * @param pool The guac_pool to retrieve an integer from. - * @return The next available integer, which may be either an integer not yet returned - * by a call to guac_pool_next_int, or an integer which was previosly returned, - * but has since been freed. + * @return The next available integer, which may be either an integer not yet + * returned by a call to guac_pool_next_int, or an integer which was + * previosly returned, but has since been freed. */ int guac_pool_next_int(guac_pool* pool); /** - * Frees the given integer back into the given guac_pool. The integer given will be - * available for future calls to guac_pool_next_int. + * Frees the given integer back into the given guac_pool. The integer given + * will be available for future calls to guac_pool_next_int. * * @param pool The guac_pool to free the given integer into. - * @param value The integer which should be readded to the given pool, such that it can - * be received by a future call to guac_pool_next_int. + * @param value The integer which should be returned to the given pool, such + * that it can be received by a future call to guac_pool_next_int. */ void guac_pool_free_int(guac_pool* pool, int value); diff --git a/src/libguac/pool.c b/src/libguac/pool.c index 85167619..d36110de 100644 --- a/src/libguac/pool.c +++ b/src/libguac/pool.c @@ -50,6 +50,7 @@ guac_pool* guac_pool_alloc(int size) { /* Initialize empty pool */ pool->min_size = size; + pool->active = 0; pool->__next_value = 0; pool->__head = NULL; pool->__tail = NULL; @@ -79,7 +80,9 @@ int guac_pool_next_int(guac_pool* pool) { int value; - /* If more integers are needed, or we are out of integers, return a new one. */ + pool->active++; + + /* If more integers are needed, return a new one. */ if (pool->__head == NULL || pool->__next_value < pool->min_size) return pool->__next_value++; @@ -111,6 +114,8 @@ void guac_pool_free_int(guac_pool* pool, int value) { pool_int->value = value; pool_int->__next = NULL; + pool->active--; + /* If pool empty, store as sole entry. */ if (pool->__tail == NULL) pool->__head = pool->__tail = pool_int;