Track number of active pool integers. Track output streams.

This commit is contained in:
Michael Jumper 2013-10-28 09:11:45 -07:00
parent 50fbd5dabb
commit 8ee92c0e1c
5 changed files with 54 additions and 26 deletions

View File

@ -148,7 +148,7 @@ int __guac_handle_file(guac_client* client, guac_instruction* instruction) {
} }
/* Initialize stream */ /* Initialize stream */
stream = &(client->__streams[stream_index]); stream = &(client->__input_streams[stream_index]);
stream->index = stream_index; stream->index = stream_index;
stream->data = NULL; 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) if (stream_index < 0 || stream_index >= GUAC_CLIENT_MAX_STREAMS)
return 0; return 0;
stream = &(client->__streams[stream_index]); stream = &(client->__output_streams[stream_index]);
/* Validate initialization of stream */ /* Validate initialization of stream */
if (stream->index == GUAC_CLIENT_CLOSED_STREAM_INDEX) if (stream->index == GUAC_CLIENT_CLOSED_STREAM_INDEX)
@ -206,7 +206,7 @@ int __guac_handle_blob(guac_client* client, guac_instruction* instruction) {
return 0; return 0;
} }
stream = &(client->__streams[stream_index]); stream = &(client->__input_streams[stream_index]);
/* Validate initialization of stream */ /* Validate initialization of stream */
if (stream->index == GUAC_CLIENT_CLOSED_STREAM_INDEX) { if (stream->index == GUAC_CLIENT_CLOSED_STREAM_INDEX) {
@ -253,7 +253,7 @@ int __guac_handle_end(guac_client* client, guac_instruction* instruction) {
return 0; return 0;
} }
stream = &(client->__streams[stream_index]); stream = &(client->__input_streams[stream_index]);
if (client->end_handler) if (client->end_handler)
result = client->end_handler(client, stream); result = client->end_handler(client, stream);

View File

@ -97,9 +97,20 @@ void guac_client_free_layer(guac_client* client, guac_layer* layer) {
guac_stream* guac_client_alloc_stream(guac_client* client) { guac_stream* guac_client_alloc_stream(guac_client* client) {
/* Init new stream */ guac_stream* allocd_stream;
guac_stream* allocd_stream = malloc(sizeof(guac_stream)); int stream_index;
allocd_stream->index = guac_pool_next_int(client->__stream_pool);
/* 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; 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) { void guac_client_free_stream(guac_client* client, guac_stream* stream) {
/* Release index to pool */ /* Release index to pool */
guac_pool_free_int(client->__stream_pool, stream->index - 1); guac_pool_free_int(client->__stream_pool, stream->index);
/* Free stream */ /* Mark stream as closed */
free(stream); stream->index = GUAC_CLIENT_CLOSED_STREAM_INDEX;
} }
@ -143,8 +154,10 @@ guac_client* guac_client_alloc() {
client->__stream_pool = guac_pool_alloc(0); client->__stream_pool = guac_pool_alloc(0);
/* Initialze streams */ /* Initialze streams */
for (i=0; i<GUAC_CLIENT_MAX_STREAMS; i++) for (i=0; i<GUAC_CLIENT_MAX_STREAMS; i++) {
client->__streams[i].index = GUAC_CLIENT_CLOSED_STREAM_INDEX; client->__input_streams[i].index = GUAC_CLIENT_CLOSED_STREAM_INDEX;
client->__output_streams[i].index = GUAC_CLIENT_CLOSED_STREAM_INDEX;
}
return client; return client;

View File

@ -555,10 +555,15 @@ struct guac_client {
*/ */
guac_pool* __stream_pool; 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). * All available input streams (data coming from connected client).
*/ */
guac_stream __streams[GUAC_CLIENT_MAX_STREAMS]; guac_stream __input_streams[GUAC_CLIENT_MAX_STREAMS];
}; };

View File

@ -62,6 +62,11 @@ typedef struct guac_pool {
*/ */
int min_size; 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 * The next integer to be released (after no more integers remain in the
* pool. * pool.
@ -102,8 +107,8 @@ struct guac_pool_int {
* Allocates a new guac_pool having the given minimum size. * Allocates a new guac_pool having the given minimum size.
* *
* @param size The minimum number of integers which must have been returned by * @param size The minimum number of integers which must have been returned by
* guac_pool_next_int before freed integers (previously used integers) * guac_pool_next_int before freed integers (previously used
* are allowed to be returned. * integers) are allowed to be returned.
* @return A new, empty guac_pool, having the given minimum size. * @return A new, empty guac_pool, having the given minimum size.
*/ */
guac_pool* guac_pool_alloc(int 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); void guac_pool_free(guac_pool* pool);
/** /**
* Returns the next available integer from the given guac_pool. All integers returned are * Returns the next available integer from the given guac_pool. All integers
* non-negative, and are returned in sequences, starting from 0. * returned are non-negative, and are returned in sequences, starting from 0.
* *
* @param pool The guac_pool to retrieve an integer from. * @param pool The guac_pool to retrieve an integer from.
* @return The next available integer, which may be either an integer not yet returned * @return The next available integer, which may be either an integer not yet
* by a call to guac_pool_next_int, or an integer which was previosly returned, * returned by a call to guac_pool_next_int, or an integer which was
* but has since been freed. * previosly returned, but has since been freed.
*/ */
int guac_pool_next_int(guac_pool* pool); int guac_pool_next_int(guac_pool* pool);
/** /**
* Frees the given integer back into the given guac_pool. The integer given will be * Frees the given integer back into the given guac_pool. The integer given
* available for future calls to guac_pool_next_int. * will be available for future calls to guac_pool_next_int.
* *
* @param pool The guac_pool to free the given integer into. * @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 * @param value The integer which should be returned to the given pool, such
* be received by a future call to guac_pool_next_int. * that it can be received by a future call to guac_pool_next_int.
*/ */
void guac_pool_free_int(guac_pool* pool, int value); void guac_pool_free_int(guac_pool* pool, int value);

View File

@ -50,6 +50,7 @@ guac_pool* guac_pool_alloc(int size) {
/* Initialize empty pool */ /* Initialize empty pool */
pool->min_size = size; pool->min_size = size;
pool->active = 0;
pool->__next_value = 0; pool->__next_value = 0;
pool->__head = NULL; pool->__head = NULL;
pool->__tail = NULL; pool->__tail = NULL;
@ -79,7 +80,9 @@ int guac_pool_next_int(guac_pool* pool) {
int value; 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) if (pool->__head == NULL || pool->__next_value < pool->min_size)
return pool->__next_value++; return pool->__next_value++;
@ -111,6 +114,8 @@ void guac_pool_free_int(guac_pool* pool, int value) {
pool_int->value = value; pool_int->value = value;
pool_int->__next = NULL; pool_int->__next = NULL;
pool->active--;
/* If pool empty, store as sole entry. */ /* If pool empty, store as sole entry. */
if (pool->__tail == NULL) if (pool->__tail == NULL)
pool->__head = pool->__tail = pool_int; pool->__head = pool->__tail = pool_int;