microsoft / microsoft/mimalloc

Help wanted: A question about mi_track_malloc

Open
#1,145 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
C
Stars
13.4k
Forks
1.2k
Avg merge
4d 45m
Merged PRs (30d)
13

Description

I am implementing a custom memory tracker to monitor allocations and deallocations from the mimalloc memory allocator. While I am aware that tools like Valgrind exist, I have opted to develop my own for a more lightweight solution. However, I have encountered a challenge that I hope the community can help me resolve, and I would be immensely grateful for any assistance.

### Description

In a system with limited RAM, address reuse is an inevitable phenomenon, especially in memory pool implementations. This means that a given tuple `(p, size)` can correspond to multiple distinct allocation events over time (we can distinguish them as "different allocations" by capturing a call stack). The difficulty arises during the deallocation phase.

Specifically, since `mi_track_free_size` function only accepts the pointer `p` and the `size` as parameters, it becomes impossible to determine which of the previous allocations, associated with their respective call stacks, this particular `(p, size)` tuple corresponds to.

### What I am doing

My implementation is inserting macro defs: below this:
https://github.com/microsoft/mimalloc/blob/09a27098aa6e9286518bd9c74e6ffa7199c3f04e/include/mimalloc/track.h#L89

```cpp
#elif MY_TRACK_ENABLED
#define MI_TRACK_ENABLED 1
#define MI_TRACK_HEAP_DESTROY 1
#define MI_TRACK_TOOL "MY_TRACE_TOOL"
#include

#define mi_track_init() my_track_init();
#define mi_track_malloc_size(p,r,s,z) my_track_malloc(p,r,s,z)
#define mi_track_free_size(p,size) my_track_free(p,size)
#else
```

### other

- code version I am using: `release 3.0.9`
- I have ensured that mimalloc has linked mytrack dynamically, and soved reentrant recursion problems.
- if my implementation is not the right way, please point out

huge thanks.

Contributor guide

No contributing guide indexed for this repository

Research direction

Read include/mimalloc/track.h around the linked line, along with the mi_track_malloc_size and mi_track_free_size hook definitions used by the custom macros. Determine whether the existing tracking hooks can identify reused addresses, and document a supported approach or the API change needed to distinguish allocation events.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
operating-systems
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.