From 0883de2c491a8c79aec13d25bab429aea4362a10 Mon Sep 17 00:00:00 2001 From: Luke Bratch Date: Sun, 22 Dec 2019 20:16:57 +0000 Subject: Significantly reduce memory usage by only initialising channel struct elements when they are used for the first time. --- blabouncer.c | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) (limited to 'blabouncer.c') diff --git a/blabouncer.c b/blabouncer.c index a9eab97..5ee48b5 100644 --- a/blabouncer.c +++ b/blabouncer.c @@ -136,6 +136,7 @@ int connecttoircserver(SSL_CTX **serverctx, SSL **server_ssl, int *serversockfd, // ircdstate.reconnecting is not set here as we want to track reconnections separately // ircdstate.clientchangetime and ircdstate.clientsnonetime not set here as they are set at startup and only changed when clients connect/disconnect // ircdstate.clientcodes not set here, set on startup and whenever a client sets one + // ircdstate->maxchannelcount not set here, set on startup in dochat() // Populate nicks[0] and username from our configuration file for now, real IRCd may change them later (TODO - Is this true of username?) strcpy(ircdstate->ircnick, settings->ircnicks[0]); @@ -435,14 +436,9 @@ void dochat(int *serversockfd, int *clientsockfd, struct settings *settings) { // Struct of channels we're in struct channel *channels; channels = malloc(sizeof(struct channel) * MAXCHANNELS); - // Set initial channel names to empty strings - for (int i = 0; i < MAXCHANNELS; i++) { - channels[i].name[0] = '\0'; - // And all the nicks within it - for (int j = 0; j < MAXCHANNICKS; j++) { - channels[i].nicks[j][0] = '\0'; - } - } + ircdstate.maxchannelcount = 0; + // Each channel struct element is only initialised upon channel creation to avoid consuming lots of memory here. + // The memory is never released once a channel is parted - TODO - Can this channel struct memory usage be improved? // Initialise OpenSSL (used for both client and server) init_openssl(); @@ -710,14 +706,13 @@ void dochat(int *serversockfd, int *clientsockfd, struct settings *settings) { if (strncmp(outgoingmsg, "listchannels", strlen("listchannels")) == 0) { printf("STDIN command starting: listchannels\n"); - int channelcount = getchannelcount(channels); + int channelcount = getchannelcount(channels, ircdstate.maxchannelcount); - for (int i = 0; i < channelcount; i++) { + for (int i = 0; i < ircdstate.maxchannelcount; i++) { printf("Checking channel[%d] out of %d.\n", i, channelcount); - // Skip this one and increment channelcount if it's a blank channel + // Skip this one if it's a blank channel if (!channels[i].name[0]) { - debugprint(DEBUG_FULL, "Skipping channel[%d], incrementing channelcount.\n", i); - channelcount++; + debugprint(DEBUG_FULL, "Skipping blank channel channel[%d].\n", i); continue; } -- cgit v1.2.3