varch/doc/valloc.en.md

3.3 KiB

Introduction

Memory dynamic allocation refers to the process of dynamically allocating memory space during the execution of a program based on the program's requirements. This is done to store data or create objects. It is usually implemented using pointers. By calling memory allocation functions provided by the system (such as malloc, calloc, etc.), memory space is requested. Once the request is successful, a pointer pointing to the allocated memory space is returned, and then this pointer can be used in the program to access the allocated memory area.

The advantages of memory dynamic allocation include the ability to allocate memory space dynamically, which helps avoid waste and improves the efficiency of memory usage. Moreover, the dynamically allocated memory space can also be released dynamically according to the program's needs, preventing memory leaks and errors. However, there are also some disadvantages when using memory dynamic allocation. For example, issues like memory leaks and memory fragmentation are prone to occur, and programmers need to have certain skills and pay attention to some details to use memory dynamic allocation correctly and efficiently.

Here, a small tool named valloc that can check the usage of dynamic memory is introduced. It can be used to calculate the usage of dynamic memory and check whether there are any unreleased memory fragments. By using its methods to replace the regular memory allocation methods, the usage situation of dynamic memory can be counted.

Interfaces

Checking Unreleased Memory

void v_check_unfree(void);

This function is used to check the memory that has not been released yet.

Getting the Count of Allocated Memory Blocks

int v_check_count(void);

It is used to obtain the count of the memory blocks that have been allocated.

Getting the Total Size of Used Memory

int v_check_used(void);

This function helps to get the total size of the memory that has been used.

Usage

It's quite simple to use, and the steps are as follows:

1 Add the valloc.h header file to the source file that needs to perform statistics. Add it after stdlib.h. It doesn't matter which line it is as long as it's after stdlib.h. The memory functions (such as malloc()) in valloc.h will override those in stdlib.h, and the functions in valloc.h will be used instead.

2 In the places where output is needed, just call the APIs of valloc.

Example

The following is an example code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "init.h"
#include "valloc.h"

static void test(void)
{
    void* p = NULL;
    printf("malloc %p\r\n", malloc(0));
    p = realloc(NULL, 100);
    p = malloc(64);
    p = malloc(50);
    printf("realloc %p\r\n", realloc(NULL, 0));
    printf("%d\r\n", *(int *)p);
    free(p);

    v_check_unfree();
    printf("count = %d\r\n", v_check_count());
    printf("used = %d\r\n", v_check_used());
}
init_export_app(test);

The result is:

malloc 007B5B50
realloc 007B5C20
8086040
address: 007B5B50, size: 0, file: test/test_valloc.c, line: 10
address: 007B5B88, size: 100, file: test/test_valloc.c, line: 11
address: 007B5C20, size: 0, file: test/test_valloc.c, line: 14
address: 007B5C68, size: 64, file: test/test_valloc.c, line: 12
count = 4
used = 164