44 lines
1.2 KiB
C
44 lines
1.2 KiB
C
// LEMONBBS MODULES: CONNLIST
|
|
// This module allows for storing connection information statically instead of
|
|
// allocating on every connection
|
|
|
|
#include <pthread.h>
|
|
#include <memory.h>
|
|
#include <stdlib.h>
|
|
#include "connlist.h"
|
|
|
|
pthread_mutex_t connListLock = PTHREAD_MUTEX_INITIALIZER;
|
|
struct Connection* connList;
|
|
unsigned int connListLength;
|
|
|
|
void initConnList(unsigned int maxConnCount) {
|
|
size_t allocNeeded = maxConnCount*sizeof(struct Connection);
|
|
connList = malloc(allocNeeded);
|
|
memset(connList, 0, allocNeeded);
|
|
connListLength = maxConnCount;
|
|
}
|
|
|
|
// allocates a new Connection and activates it
|
|
struct Connection* allocateConnection() {
|
|
pthread_mutex_lock(&connListLock);
|
|
struct Connection* result = NULL;
|
|
for (int i = 0; i < connListLength; ++i) {
|
|
if (connList[i].state == 0x00) {
|
|
result = connList+i;
|
|
break;
|
|
}
|
|
}
|
|
if (result != NULL) {
|
|
result->state = 0xFF;
|
|
}
|
|
pthread_mutex_unlock(&connListLock);
|
|
return result;
|
|
}
|
|
|
|
// deactivates and deallocates a Connection
|
|
void deallocateConnection(struct Connection* connection) {
|
|
pthread_mutex_lock(&connListLock);
|
|
memset(connection, 0, sizeof(struct Connection));
|
|
pthread_mutex_unlock(&connListLock);
|
|
}
|