|
8 | 8 | *
|
9 | 9 | ****/
|
10 | 10 |
|
| 11 | +#include "../include/netlibc.h" |
| 12 | +#include "../include/netlibc/log.h" |
11 | 13 | #include "../include/netlibc/string.h"
|
12 | 14 |
|
13 | 15 | #include <stdlib.h>
|
14 | 16 | #include <sys/time.h>
|
15 | 17 | #include <time.h>
|
16 | 18 |
|
| 19 | +netlibc_ctx_t *__netlibc_ctx = NULL; |
| 20 | + |
| 21 | +void __netlibc_ctx_init(bool mem_debug_enabled) { |
| 22 | + __netlibc_ctx = calloc(1, sizeof(netlibc_ctx_t)); |
| 23 | + |
| 24 | + __netlibc_ctx->mem_debug_enabled = mem_debug_enabled; |
| 25 | + __netlibc_ctx->mem_allocations = NULL; |
| 26 | + __netlibc_ctx->mem_allocations_count = 0; |
| 27 | + __netlibc_ctx->mem_free_count = 0; |
| 28 | + |
| 29 | + // Register the cleanup_function |
| 30 | + if (atexit(__netlibc_ctx_exit) != 0) { |
| 31 | + PANIC("atexit registration failed"); |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +void __netlibc_ctx_exit() { |
| 36 | + if (__netlibc_ctx->mem_debug_enabled) { |
| 37 | + printf("[MEM_DEBUG] ALLOCATIONS COUNT: " U64_FORMAT_SPECIFIER " \n", |
| 38 | + __netlibc_ctx->mem_allocations_count); |
| 39 | + printf("[MEM_DEBUG] FREE COUNT: " U64_FORMAT_SPECIFIER " \n", |
| 40 | + __netlibc_ctx->mem_free_count); |
| 41 | + |
| 42 | + u64 allocated_bytes = 0; |
| 43 | + for (u64 i = 0; i < __netlibc_ctx->mem_allocations_count; i++) { |
| 44 | + allocated_bytes += __netlibc_ctx->mem_allocations[i].size; |
| 45 | + } |
| 46 | + |
| 47 | + printf("[MEM_DEBUG] ALLOCATED BYTES: " U64_FORMAT_SPECIFIER " \n", |
| 48 | + allocated_bytes); |
| 49 | + |
| 50 | + u64 freed_bytes = 0; |
| 51 | + for (u64 i = 0; i < __netlibc_ctx->mem_allocations_count; i++) { |
| 52 | + if (__netlibc_ctx->mem_allocations[i].freed) { |
| 53 | + freed_bytes += __netlibc_ctx->mem_allocations[i].size; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + printf("[MEM_DEBUG] FREED BYTES: " U64_FORMAT_SPECIFIER " \n", freed_bytes); |
| 58 | + |
| 59 | + printf("\n\n"); |
| 60 | + |
| 61 | + u64 leaked_bytes = 0; |
| 62 | + for (u64 i = 0; i < __netlibc_ctx->mem_allocations_count; i++) { |
| 63 | + if (!__netlibc_ctx->mem_allocations[i].freed) { |
| 64 | + leaked_bytes += __netlibc_ctx->mem_allocations[i].size; |
| 65 | + |
| 66 | + printf("[MEM_DEBUG] LEAK OF " U64_FORMAT_SPECIFIER |
| 67 | + " BYTES IN %p AT %s:" U64_FORMAT_SPECIFIER "\n", |
| 68 | + __netlibc_ctx->mem_allocations[i].size, |
| 69 | + __netlibc_ctx->mem_allocations[i].address, |
| 70 | + __netlibc_ctx->mem_allocations[i].file, |
| 71 | + __netlibc_ctx->mem_allocations[i].line); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + printf("[MEM_DEBUG] LEAKED BYTES: " U64_FORMAT_SPECIFIER " \n", |
| 76 | + leaked_bytes); |
| 77 | + |
| 78 | + printf("\n"); |
| 79 | + } |
| 80 | + |
| 81 | + free(__netlibc_ctx->mem_allocations); |
| 82 | + |
| 83 | + free(__netlibc_ctx); |
| 84 | +} |
| 85 | + |
17 | 86 | // returns the UNIX timestamp in microseconds
|
18 | 87 | u64 get_timestamp_us() {
|
19 | 88 | struct timeval tv;
|
|
0 commit comments