#include #include #include #include //#include #include #include #include "client.h" static client *first_client = NULL; /* head of client list */ /************************************************* *** FUNCTIONS TO ACCESS CLIENT REPRESENTATION *** *************************************************/ /*** INIT CLIENT STRUCTURE AND ADD CLIENT TO CLIENT LIST ***/ client *add_client( client_t clientid, unsigned short max_views, unsigned short max_buffers ) { // TODO: This allocation is messed up, and probably dangerous. Redo. char *addr; client *new_client = (client *)&addr[0]; /* get memory for the client header */ addr = malloc( sizeof(buffer)*max_buffers + sizeof(view)*max_views + sizeof(client) ); if (!addr) return NULL; new_client = (client *)&addr[0]; bzero(new_client, sizeof(client)); new_client->views = (view *)&addr[sizeof(client)]; new_client->buffers = (buffer *)&addr[sizeof(client) + max_views*sizeof(view)]; new_client->max_views = max_views; new_client->max_buffers = max_buffers; bzero(new_client->buffers, sizeof(buffer) * max_buffers); bzero(new_client->views, sizeof(view) * max_views); if (clientid) { new_client->tid = clientid; // TODO: Fix getting object label. // get_object_label( clientid, &new_client->label[0], CLIENT_LABEL_LEN - 1 ); } /* insert client struct into client list */ CHAIN_LISTELEMENT( first_client, next, first_client, new_client ); return new_client; } /*** REMOVE CLIENT FROM NITPICKER SESSION ***/ void remove_client( client_t clientid ) { int i; client *c = find_client(clientid); if (!c) return; /* close all views */ for (i = 0; i < c->max_views; i++) nitpicker_destroy_view( clientid, i); // nitpicker_destroy_view( clientid, i, NULL ); /* remove all buffers */ for (i = 0; i < c->max_buffers; i++) nitpicker_remove_buffer( clientid, i, NULL ); /* exclude client from client list */ UNCHAIN_LISTELEMENT( client, first_client, next, c ); /* unmap client memory */ detach_buffer(c); } /*** FIND THE CLIENT STRUCTURE FOR A GIVEN CLIENT ID ***/ client *find_client( client_t clientid ) { client *c; for (c = first_client; c && (c->tid != clientid); c = c->next); return c; } /*************************** *** INTERFACE FUNCTIONS *** ***************************/ /*** INTERFACE: IMPORT AND INITIALIZE MEMORY FROM CLIENT ***/ int nitpicker_initialize_client( client_t clientid, unsigned short max_views, unsigned short max_buffers) { char *addr; if (!max_views || !max_buffers) return -2; /* do not allow double donations */ remove_client(clientid); if (!add_client( clientid, max_views, max_buffers )) return -1; return 0; }