⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 debug.cpp

📁 一个小型C语言编译器
💻 CPP
字号:
#ifdef      DEBUG

#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <stddef.h>



long    debug_mem_alloc_count = 0;
long    debug_mem_alloc_size  = 0;

FILE    *debug_file = NULL;

void* DebugFileInit()
{
    deubg_file = fopen("debug_log.txt","w+");

    if(debug_file == NULL) {
        printf("文件:debug_log.txt 打不开!\r\n");
        exit(0);
    }
}

void* DebugFileDeinit()
{
    assert(debug_file);
    fclose(debug_file);
}


void* DebugFileWrite(char* debug_conten)
{
    assert(debug_file);

    fprintf(debug_file, "%s", debug_content);
}



void* malloc_m(int size, char* filename, int lineno)
{
    void *mem = NULL;
    int  *p_mem_size = NULL;
    char buf[512] = {'\0'};
    

    assert (size > 0);

    mem = malloc(size + sizeof(int));
    if(mem == NULL) {
        printf("内存分配失败!\r\n");
        exit(0);
    }
    p_mem_size = (int*)mem;
    *p_mem_size = size;
    mem = (void*)(p_mem_size+1);
    
    
    ++ debug_mem_alloc_count;
    debug_mem_alloc_size += (long)size;

    
    sprintf(buf, "[file:%s][line:%d] alloc mem size:%d, mem pointer:%p\n",filename, lineno, size, p_mem_size);
    DebugFileWrite(buf);

    return mem;
}


void free_m(void* mem)
{
    char buf[512] = {'\0'};
    int  *p_mem_size;

    assert (mem != NULL);
    
    p_mem_size = (int*)mem;
    --p_mem_size;

    debug_mem_alloc_size -= (long)*p_mem_size;
    -- debug_mem_alloc_count;
    
    sprintf(buf, "[file:%s][line:%d] free mem size:%d, mem pointer:%p\n", filename, lineno, *p_mem_size, p_mem_size);
    DebugFileWrite(buf);

    free((void*)p_mem_size);
}


#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -