Migrate to guac_pool for layer and buffer pools.

This commit is contained in:
Michael Jumper 2012-09-06 17:55:24 -07:00
parent d0bc7f2d7e
commit 6adce8bab0
3 changed files with 26 additions and 138 deletions

View File

@ -43,6 +43,7 @@
#include "instruction.h" #include "instruction.h"
#include "layer.h" #include "layer.h"
#include "pool.h"
#include "resource.h" #include "resource.h"
#include "socket.h" #include "socket.h"
#include "timestamp.h" #include "timestamp.h"
@ -352,42 +353,14 @@ struct guac_client {
guac_client_log_handler* log_error_handler; guac_client_log_handler* log_error_handler;
/** /**
* The index of the next available buffer. * Pool of buffer indices.
*/ */
int __next_buffer_index; guac_pool* __buffer_pool;
/** /**
* The head pointer of the list of all available (allocated but not used) * Pool of layer indices.
* buffers.
*/ */
guac_layer* __available_buffers; guac_pool* __layer_pool;
/**
* Pointer to the last buffer in the list of all available buffers.
*/
guac_layer* __last_available_buffer;
/**
* The index of the next available layer.
*/
int __next_layer_index;
/**
* The head pointer of the list of all available (allocated but not used)
* layers.
*/
guac_layer* __available_layers;
/**
* Pointer to the last layer in the list of all available layers.
*/
guac_layer* __last_available_layer;
/**
* The head pointer of the list of all allocated layers, regardless of use
* status.
*/
guac_layer* __all_layers;
}; };

View File

@ -63,17 +63,6 @@ struct guac_layer {
*/ */
char* uri; char* uri;
/**
* The next allocated layer in the list of all layers.
*/
guac_layer* __next;
/**
* The next available (unused) layer in the list of
* allocated but free'd layers.
*/
guac_layer* __next_available;
}; };
#endif #endif

View File

@ -44,6 +44,7 @@
#include "error.h" #include "error.h"
#include "layer.h" #include "layer.h"
#include "plugin.h" #include "plugin.h"
#include "pool.h"
#include "protocol.h" #include "protocol.h"
#include "socket.h" #include "socket.h"
#include "time.h" #include "time.h"
@ -51,43 +52,17 @@
guac_layer __GUAC_DEFAULT_LAYER = { guac_layer __GUAC_DEFAULT_LAYER = {
.index = 0, .index = 0,
.uri = "layer://0", .uri = "layer://0",
.__next = NULL,
.__next_available = NULL
}; };
const guac_layer* GUAC_DEFAULT_LAYER = &__GUAC_DEFAULT_LAYER; const guac_layer* GUAC_DEFAULT_LAYER = &__GUAC_DEFAULT_LAYER;
guac_layer* guac_client_alloc_layer(guac_client* client) { guac_layer* guac_client_alloc_layer(guac_client* client) {
guac_layer* allocd_layer; /* Init new layer */
guac_layer* allocd_layer = malloc(sizeof(guac_layer));
/* If available layers, pop off first available layer */ allocd_layer->index = guac_pool_next_int(client->__layer_pool)+1;
if (client->__next_layer_index > GUAC_BUFFER_POOL_INITIAL_SIZE && allocd_layer->uri = malloc(64);
client->__available_layers != NULL) { snprintf(allocd_layer->uri, 64, "layer://%i", allocd_layer->index);
allocd_layer = client->__available_layers;
client->__available_layers = client->__available_layers->__next_available;
/* If last layer, reset last available layer pointer */
if (allocd_layer == client->__last_available_layer)
client->__last_available_layer = NULL;
}
/* If no available layer, allocate new layer, add to __all_layers list */
else {
/* Init new layer */
allocd_layer = malloc(sizeof(guac_layer));
allocd_layer->index = client->__next_layer_index++;
allocd_layer->uri = malloc(64);
snprintf(allocd_layer->uri, 64, "layer://%i", allocd_layer->index);
/* Add to __all_layers list */
allocd_layer->__next = client->__all_layers;
client->__all_layers = allocd_layer;
}
return allocd_layer; return allocd_layer;
@ -95,35 +70,11 @@ guac_layer* guac_client_alloc_layer(guac_client* client) {
guac_layer* guac_client_alloc_buffer(guac_client* client) { guac_layer* guac_client_alloc_buffer(guac_client* client) {
guac_layer* allocd_layer; /* Init new layer */
guac_layer* allocd_layer = malloc(sizeof(guac_layer));
/* If available layers, pop off first available buffer */ allocd_layer->index = -guac_pool_next_int(client->__buffer_pool) - 1;
if (client->__next_buffer_index < -GUAC_BUFFER_POOL_INITIAL_SIZE && allocd_layer->uri = malloc(64);
client->__available_buffers != NULL) { snprintf(allocd_layer->uri, 64, "layer://%i", allocd_layer->index);
allocd_layer = client->__available_buffers;
client->__available_buffers = client->__available_buffers->__next_available;
/* If last buffer, reset last available buffer pointer */
if (allocd_layer == client->__last_available_buffer)
client->__last_available_buffer = NULL;
}
/* If no available buffer, allocate new buffer, add to __all_layers list */
else {
/* Init new layer */
allocd_layer = malloc(sizeof(guac_layer));
allocd_layer->index = client->__next_buffer_index--;
allocd_layer->uri = malloc(64);
snprintf(allocd_layer->uri, 64, "layer://%i", allocd_layer->index);
/* Add to __all_layers list */
allocd_layer->__next = client->__all_layers;
client->__all_layers = allocd_layer;
}
return allocd_layer; return allocd_layer;
@ -131,29 +82,21 @@ guac_layer* guac_client_alloc_buffer(guac_client* client) {
void guac_client_free_buffer(guac_client* client, guac_layer* layer) { void guac_client_free_buffer(guac_client* client, guac_layer* layer) {
/* Add layer to tail of pool of available buffers */ /* Release index to pool */
if (client->__last_available_buffer != NULL) guac_pool_free_int(client->__buffer_pool, -layer->index - 1);
client->__last_available_buffer->__next_available = layer;
if (client->__available_buffers == NULL) /* Free layer */
client->__available_buffers = layer; free(layer);
client->__last_available_buffer = layer;
layer->__next_available = NULL;
} }
void guac_client_free_layer(guac_client* client, guac_layer* layer) { void guac_client_free_layer(guac_client* client, guac_layer* layer) {
/* Add layer to tail of pool of available layers */ /* Release index to pool */
if (client->__last_available_layer != NULL) guac_pool_free_int(client->__layer_pool, layer->index - 1);
client->__last_available_layer->__next_available = layer;
if (client->__available_layers == NULL) /* Free layer */
client->__available_layers = layer; free(layer);
client->__last_available_layer = layer;
layer->__next_available = NULL;
} }
@ -175,12 +118,8 @@ guac_client* guac_client_alloc() {
client->state = GUAC_CLIENT_RUNNING; client->state = GUAC_CLIENT_RUNNING;
client->__all_layers = NULL; client->__buffer_pool = guac_pool_alloc(GUAC_BUFFER_POOL_INITIAL_SIZE);
client->__available_buffers = client->__last_available_buffer = NULL; client->__layer_pool = guac_pool_alloc(GUAC_BUFFER_POOL_INITIAL_SIZE);
client->__available_layers = client->__last_available_layer = NULL;
client->__next_buffer_index = -1;
client->__next_layer_index = 1;
return client; return client;
@ -195,19 +134,6 @@ void guac_client_free(guac_client* client) {
} }
/* Free all layers */
while (client->__all_layers != NULL) {
/* Get layer, update layer pool head */
guac_layer* layer = client->__all_layers;
client->__all_layers = layer->__next;
/* Free layer */
free(layer->uri);
free(layer);
}
free(client); free(client);
} }