This repository has been archived on 2021-12-16. You can view files and clone it, but cannot push or open issues or pull requests.
lemonbbs/src/connlist.c
2021-12-16 22:16:13 +01:00

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);
}