added small memory debugger to detect memory leaks
[NeonServV5.git] / src / cmd_global_meminfo.c
diff --git a/src/cmd_global_meminfo.c b/src/cmd_global_meminfo.c
new file mode 100644 (file)
index 0000000..bdcf45a
--- /dev/null
@@ -0,0 +1,118 @@
+/* cmd_global_meminfo.c - NeonServ v5.3
+ * Copyright (C) 2011-2012  Philipp Kreil (pk910)
+ * 
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License 
+ * along with this program. If not, see <http://www.gnu.org/licenses/>. 
+ */
+
+#include "cmd_global.h"
+#include "memoryInfo.h"
+
+/*
+* no arguments
+*/
+
+CMD_BIND(global_cmd_meminfo) {
+    #ifndef ENABLE_MEMORY_DEBUG
+    reply(getTextBot(), user, "NS_MEMINFO_DISABLED");
+    #else
+    if(argc > 0) {
+        struct Table *table;
+        int elementcount = 0;
+        struct memoryInfoLines *element, *elements;
+        elements = getMemoryInfoLines(argv[0]);
+        for(element = elements; element; element = element->next) {
+            elementcount++;
+        }
+        table = table_init(4, elementcount+2, 0);
+        char *content[4];
+        content[0] = get_language_string(user, "NS_MEMINFO_LINE");
+        content[1] = get_language_string(user, "NS_MEMINFO_COUNT");
+        content[2] = get_language_string(user, "NS_MEMINFO_SIZE");
+        content[3] = get_language_string(user, "NS_MEMINFO_TOTAL");
+        table_add(table, content);
+        char lineBuf[20];
+        char countBuf[20];
+        char sizeBuf[20];
+        char totalBuf[50];
+        unsigned int total_allocations = 0;
+        unsigned int total_allocated = 0;
+        for(element = elements; element; element = element->next) {
+            sprintf(lineBuf, "%u", element->line);
+            content[0] = lineBuf;
+            sprintf(countBuf, "%u", element->allocations);
+            total_allocations += element->allocations;
+            content[1] = countBuf;
+            sprintf(sizeBuf, "%uB", element->allocate);
+            content[2] = sizeBuf;
+            sprintf(totalBuf, "%u (%.2f kB)", (element->allocate * element->allocations), ((float)(element->allocate * element->allocations) / 1024));
+            total_allocated += (element->allocate * element->allocations);
+            content[3] = totalBuf;
+            table_add(table, content);
+        }
+        content[0] = "Total";
+        sprintf(countBuf, "%u", total_allocations);
+        content[1] = countBuf;
+        content[2] = "*";
+        sprintf(sizeBuf, "%u (%.2f kB)", total_allocated, ((float)total_allocated / 1024));
+        content[3] = sizeBuf;
+        table_add(table, content);
+        char **table_lines = table_end(table);
+        int i;
+        for(i = 0; i < table->entrys; i++) {
+            reply(getTextBot(), user, table_lines[i]);
+        }
+        table_free(table);
+    } else {
+        struct Table *table;
+        int elementcount = 0;
+        struct memoryInfoFiles *element, *elements;
+        elements = getMemoryInfoFiles();
+        for(element = elements; element; element = element->next) {
+            elementcount++;
+        }
+        table = table_init(3, elementcount+2, 0);
+        char *content[3];
+        content[0] = get_language_string(user, "NS_MEMINFO_NAME");
+        content[1] = get_language_string(user, "NS_MEMINFO_COUNT");
+        content[2] = get_language_string(user, "NS_MEMINFO_SIZE");
+        table_add(table, content);
+        char countBuf[20];
+        char sizeBuf[50];
+        unsigned int total_allocations = 0;
+        unsigned int total_allocated = 0;
+        for(element = elements; element; element = element->next) {
+            content[0] = element->filename;
+            sprintf(countBuf, "%u", element->allocations);
+            total_allocations += element->allocations;
+            content[1] = countBuf;
+            sprintf(sizeBuf, "%u (%.2f kB)", element->allocated, ((float)element->allocated / 1024));
+            total_allocated += element->allocated;
+            content[2] = sizeBuf;
+            table_add(table, content);
+        }
+        content[0] = "Total";
+        sprintf(countBuf, "%u", total_allocations);
+        content[1] = countBuf;
+        sprintf(sizeBuf, "%u (%.2f kB)", total_allocated, ((float)total_allocated / 1024));
+        content[2] = sizeBuf;
+        table_add(table, content);
+        char **table_lines = table_end(table);
+        int i;
+        for(i = 0; i < table->entrys; i++) {
+            reply(getTextBot(), user, table_lines[i]);
+        }
+        table_free(table);
+    }
+    #endif
+}
\ No newline at end of file