Add pool and stream alloc/free.

This commit is contained in:
Michael Jumper 2012-10-18 20:57:11 -07:00
parent cd23eac4ee
commit 31729bf62d
2 changed files with 55 additions and 6 deletions

View File

@ -45,6 +45,7 @@
#include "layer.h"
#include "pool.h"
#include "socket.h"
#include "stream.h"
#include "timestamp.h"
/**
@ -352,18 +353,24 @@ struct guac_client {
guac_client_log_handler* log_error_handler;
/**
* Pool of buffer indices. Buffers are simply layers with negative indices. Note that
* because guac_pool always gives non-negative indices starting at 0, the output of
* this guac_pool will be adjusted.
* Pool of buffer indices. Buffers are simply layers with negative indices.
* Note that because guac_pool always gives non-negative indices starting
* at 0, the output of this guac_pool will be adjusted.
*/
guac_pool* __buffer_pool;
/**
* Pool of layer indices. Note that because guac_pool always gives non-negative indices
* starting at 0, the output of this guac_pool will be adjusted.
* Pool of layer indices. Note that because guac_pool always gives
* non-negative indices starting at 0, the output of this guac_pool will
* be adjusted.
*/
guac_pool* __layer_pool;
/**
* Pool of stream indices.
*/
guac_pool* __stream_pool;
};
/**
@ -488,6 +495,25 @@ void guac_client_free_buffer(guac_client* client, guac_layer* layer);
*/
void guac_client_free_layer(guac_client* client, guac_layer* layer);
/**
* Allocates a new stream. An arbitrary index is automatically assigned
* if no previously-allocated stream is available for use.
*
* @param client The proxy client to allocate the layer buffer for.
* @return The next available stream, or a newly allocated stream.
*/
guac_stream* guac_client_alloc_stream(guac_client* client);
/**
* Returns the given stream to the pool of available streams, such that it
* can be reused by any subsequent call to guac_client_alloc_stream().
*
* @param client The proxy client to return the buffer to.
* @param stream The stream to return to the pool of available stream.
*/
void guac_client_free_stream(guac_client* client, guac_stream* stream);
/**
* The default Guacamole client layer, layer 0.
*/

View File

@ -88,13 +88,33 @@ void guac_client_free_buffer(guac_client* client, guac_layer* layer) {
void guac_client_free_layer(guac_client* client, guac_layer* layer) {
/* Release index to pool */
guac_pool_free_int(client->__layer_pool, layer->index - 1);
guac_pool_free_int(client->__layer_pool, layer->index);
/* Free layer */
free(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);
return allocd_stream;
}
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_client* guac_client_alloc() {
/* Allocate new client */
@ -117,6 +137,9 @@ guac_client* guac_client_alloc() {
client->__buffer_pool = guac_pool_alloc(GUAC_BUFFER_POOL_INITIAL_SIZE);
client->__layer_pool = guac_pool_alloc(GUAC_BUFFER_POOL_INITIAL_SIZE);
/* Allocate stream pool */
client->__stream_pool = guac_pool_alloc(0);
return client;
}