]> PatchyGit - patchyircd.git/commitdiff
replace dynarr with a vector<char>
authorEgor <egor@patchysicp.xyz>
Fri, 13 Jun 2025 01:10:45 +0000 (04:10 +0300)
committerEgor <egor@patchysicp.xyz>
Fri, 13 Jun 2025 01:18:16 +0000 (04:18 +0300)
src/irc.cpp
src/irc.h
src/ircfmt.cpp
src/main.cpp
src/util.cpp
src/util.h

index 7825168..2ee5f23 100644 (file)
@@ -5,28 +5,26 @@
 #include "ircfmt.h"
 #include "passwd.h"
 #include "job.h"
-
-struct dynarr databuf;
-
+std::vector<char> databuf;
 clientmap client_map = {};
 channelmap channel_map = {};
 stringset autojoin = {};
 
 const char *hostname = 0;
 void sendbuf(struct client_data *client) {
-       log_client(client, "SERVER: %.*s", (int)databuf.len, (char*)databuf.buf);
-       sendClient(client, databuf.buf, databuf.len);
+       log_client(client, "SERVER: %.*s", (int)databuf.size(), (char*)databuf.data());
+       sendClient(client, databuf.data(), databuf.size());
 }
 static void chanlog_time(time_t timestamp) {
        if (fchanlog) {
-               fprintf(fchanlog, "$%lu %.*s", (unsigned long)timestamp, (int)databuf.len, databuf.buf);
+               fprintf(fchanlog, "$%lu %.*s", (unsigned long)timestamp, (int)databuf.size(), databuf.data());
                fflush(fchanlog);
        }
 }
 static void chanlog(void) {
        extern FILE *fchanlog;
        if (fchanlog) {
-               fprintf(fchanlog, "%.*s", (int)databuf.len, databuf.buf);
+               fprintf(fchanlog, "%.*s", (int)databuf.size(), databuf.data());
                fflush(fchanlog);
        }
 }
@@ -237,7 +235,7 @@ static void cont_pass(struct client_data *client, int res) {
 }
 int processPacket(struct client_data *client, char *source, char *command, int argc, char *args[]) {
        (void)source;
-       databuf.len = 0;
+       databuf.clear();
        if(strcmp(command, "") == 0) {
                return 0;
        } else if(strcmp(command, "PING") == 0) {
@@ -282,8 +280,8 @@ int processPacket(struct client_data *client, char *source, char *command, int a
                                if (client->caps & c->caps)
                                append_strf(&databuf, "%s ", c->name);
                        }
-                       if (databuf.buf[databuf.len-1] == ' ')
-                               databuf.len--;
+                       if (databuf.back() == ' ')
+                               databuf.pop_back();
                        append_strf(&databuf, "\r\n");
                        sendbuf(client);
                } else if (strcmp(args[0], "REQ") == 0) {
@@ -1474,11 +1472,11 @@ void channel_sendtopic(struct client_data *client, struct channel *chan, int joi
 }
 void channel_sendnames(struct client_data *client, struct channel *chan)
 {
-       databuf.len = 0;
+       databuf.clear();
        for (auto cl : chan->members)
                if (rpl_namreply(client, chan, cl)) {
                        sendbuf(client);
-                       databuf.len = 0;
+                       databuf.clear();
                        rpl_namreply(client, chan, cl);
                }
        if (rpl_namreply(client, chan, NULL))
index ab9f47f..5fccdd6 100644 (file)
--- a/src/irc.h
+++ b/src/irc.h
@@ -28,7 +28,7 @@ void channel_part(struct client_data *client, struct channel *chan, const char *
 void channel_kill(struct channel *chan);
 void channel_mode(struct client_data *client, struct channel *chan, char *args[]);
 void channel_topic(struct client_data *src, struct channel *chan, const char *topic, const char *who, time_t when);
-extern struct dynarr databuf;
+extern std::vector<char> databuf;
 extern FILE *fchanlog;
 extern const char *hostname;
 #endif
index 08a0b0c..80a3b7e 100644 (file)
@@ -466,7 +466,7 @@ void rpl_version(struct client_data *client, const char *ver, const char *commen
 int rpl_namreply(struct client_data *client, struct channel *chan, struct client_data *cl) // 353
 {
        if (!cl) {
-               if (databuf.len) {
+               if (databuf.size()) {
                        append_str(&databuf, "\r\n");
                        return 1;
                } else {
@@ -474,11 +474,11 @@ int rpl_namreply(struct client_data *client, struct channel *chan, struct client
                }
        }
        const char *prefix = format_prefixes(client, cl, chan);
-       if (databuf.len == 0) {
+       if (databuf.empty()) {
                const char *chanchar = chan->dmode & DMODE_SECRET ? "@" : "=";
                copy_strf(&databuf, ":%s 353 %s %s %s :", hostname, client->nick, chanchar, chan->name);
        } else {
-               if (databuf.len + 1 + strlen(prefix) + strlen(cl->nick) > 510) {
+               if (databuf.size() + 1 + strlen(prefix) + strlen(cl->nick) > 510) {
                        append_str(&databuf, "\r\n");
                        return 1;
                }
index a979e4e..49cb835 100644 (file)
@@ -217,8 +217,6 @@ int main(int argc, char *argv[])
 #ifdef HAVE_LUA
        admin_init();
 #endif
-       databuf.buf = (char *)malloc(sizeof(char) * 512);
-       databuf.cap = 512;
 
        struct tls *tls_ctx = NULL;
 #ifdef HAVE_LIBTLS
@@ -422,7 +420,6 @@ int main(int argc, char *argv[])
 #ifdef HAVE_LIBTLS
        tls_free(tls_ctx);
 #endif
-       free(databuf.buf);
        /* free any persistent channels */
        for (auto chan : channel_map.pop_view())
                channel_kill(chan);
index be8292a..b9227c5 100644 (file)
@@ -5,22 +5,13 @@
 #include <stdio.h>
 #include <string.h>
 #include <stdarg.h>
-static void append_strn(struct dynarr *d, const char *s, int len){
-       if (d->len + len > d->cap){
-               while (d->len + len > d->cap)
-                       d->cap *= 2;
-               char *ndata = (char *)realloc(d->buf, d->cap);
-               if(!ndata)
-                       abortf("append_strn: allocation failure\n");
-               d->buf = ndata;
-       }
-       memcpy(d->buf + d->len, s, len);
-       d->len += len;
+static void append_strn(std::vector<char> *v, const char *s, int len) {
+       v->insert(v->end(), s, s+len);
 }
-void append_str(struct dynarr *d, const char *s){
-       append_strn(d, s, strlen(s));
+void append_str(std::vector<char> *v, const char *s) {
+       append_strn(v, s, strlen(s));
 }
-void append_strf(struct dynarr *d, const char *s, ...){
+void append_strf(std::vector<char> *v, const char *s, ...) {
        va_list ap;
        va_start(ap, s);
        for (;;) {
@@ -30,21 +21,21 @@ void append_strf(struct dynarr *d, const char *s, ...){
                        int len = strlen(s);
                        if (len) {
                                if (s[len-1] == '\n' && (len == 1 || s[len-2] != '\r')) {
-                                       append_strn(d, s, len-1);
-                                       append_strn(d, "\r\n", 2);
+                                       append_strn(v, s, len-1);
+                                       append_strn(v, "\r\n", 2);
                                } else {
-                                       append_strn(d, s, len);
+                                       append_strn(v, s, len);
                                }
                        }
                        break;
                }
                if (end[1] == 's') { // %s
-                       append_strn(d, s, end-s);
+                       append_strn(v, s, end-s);
                        const char *c = va_arg(ap, const char *);
-                       append_str(d, c ? c : "(null)");
+                       append_str(v, c ? c : "(null)");
                        s = end + 2;
                } else if (end[1] == 'u') { // %u
-                       append_strn(d, s, end-s);
+                       append_strn(v, s, end-s);
                        unsigned c = va_arg(ap, unsigned);
                        char buf[11] = {0};
                        char *p = &buf[10];
@@ -54,10 +45,10 @@ void append_strf(struct dynarr *d, const char *s, ...){
                                *--p = '0' + c%10;
                                c /= 10;
                        }
-                       append_str(d, p);
+                       append_str(v, p);
                        s = end + 2;
                } else if (end[1] == '%') { // %%
-                       append_strn(d, s, end-s+1);
+                       append_strn(v, s, end-s+1);
                        s = end + 2;
                } else { // invalid
                        break;
index cc2bafb..67bd529 100644 (file)
@@ -6,16 +6,10 @@
 #include <stdlib.h>
 #include <string.h>
 #include <time.h>
-
-struct dynarr {
-       char *buf;
-       size_t len;
-       size_t cap;
-};
-
-void append_str(struct dynarr *d, const char *s);
-void append_strf(struct dynarr *d, const char *s, ...);
-#define copy_strf(d, ...) append_strf(((d)->len=0, (d)), __VA_ARGS__)
+#include <vector>
+void append_str(std::vector<char> *v, const char *s);
+void append_strf(std::vector<char> *v, const char *s, ...);
+#define copy_strf(d, ...) append_strf(((d)->clear(), (d)), __VA_ARGS__)
 void stringupper(char *s);
 void stringlower(char *s);
 #define abortf(...) (fprintf(stderr, "(abort) " __VA_ARGS__), abort())