2022-10-29 05:54:56 +03:00
|
|
|
/* ----------------------------------------------------------------------------
|
|
|
|
Copyright (c) 2018-2021, Microsoft Research, Daan Leijen
|
|
|
|
This is free software; you can redistribute it and/or modify it under the
|
|
|
|
terms of the MIT license. A copy of the license can be found in the file
|
|
|
|
"LICENSE" at the root of this distribution.
|
|
|
|
-----------------------------------------------------------------------------*/
|
|
|
|
#pragma once
|
|
|
|
#ifndef MIMALLOC_TRACK_H
|
|
|
|
#define MIMALLOC_TRACK_H
|
|
|
|
|
2023-03-05 02:17:13 +03:00
|
|
|
/* ------------------------------------------------------------------------------------------------------
|
|
|
|
Track memory ranges with macros for tools like Valgrind
|
|
|
|
address sanitizer, or other memory checkers.
|
2022-10-29 05:54:56 +03:00
|
|
|
|
2023-03-05 02:17:13 +03:00
|
|
|
The macros are set up such that the size passed to `mi_track_free_size`
|
2023-03-05 22:01:51 +03:00
|
|
|
matches the size of the allocation, or the new size of a `mi_track_resize` (currently unused though).
|
2023-03-05 02:17:13 +03:00
|
|
|
|
|
|
|
The `size` is either byte precise (and what the user requested) if `MI_PADDING` is enabled,
|
|
|
|
or otherwise it is the full block size which may be larger than the original request.
|
2023-03-05 22:01:51 +03:00
|
|
|
Aligned pointers in a block are signaled right after a `mi_track_malloc`
|
|
|
|
with the `mi_track_align` macro. The corresponding `mi_track_free` still
|
|
|
|
uses the block start pointer and original size (corresponding to the `mi_track_malloc`).
|
2023-03-05 02:17:13 +03:00
|
|
|
-------------------------------------------------------------------------------------------------------*/
|
2023-03-05 01:49:02 +03:00
|
|
|
|
2022-10-29 05:54:56 +03:00
|
|
|
#if MI_VALGRIND
|
2022-10-29 21:43:09 +03:00
|
|
|
|
2023-03-06 05:18:41 +03:00
|
|
|
#define MI_TRACK_ENABLED 1
|
|
|
|
#define MI_TRACK_HEAP_DESTROY 1 // track free of individual blocks on heap_destroy
|
|
|
|
#define MI_TRACK_TOOL "valgrind"
|
2022-10-29 21:43:09 +03:00
|
|
|
|
2022-10-29 05:54:56 +03:00
|
|
|
#include <valgrind/valgrind.h>
|
|
|
|
#include <valgrind/memcheck.h>
|
|
|
|
|
2023-03-05 22:01:51 +03:00
|
|
|
#define mi_track_malloc_size(p,reqsize,size,zero) VALGRIND_MALLOCLIKE_BLOCK(p,size,MI_PADDING_SIZE /*red zone*/,zero)
|
|
|
|
#define mi_track_free_size(p,_size) VALGRIND_FREELIKE_BLOCK(p,MI_PADDING_SIZE /*red zone*/)
|
|
|
|
#define mi_track_resize(p,oldsize,newsize) VALGRIND_RESIZEINPLACE_BLOCK(p,oldsize,newsize,MI_PADDING_SIZE /*red zone*/)
|
|
|
|
#define mi_track_mem_defined(p,size) VALGRIND_MAKE_MEM_DEFINED(p,size)
|
|
|
|
#define mi_track_mem_undefined(p,size) VALGRIND_MAKE_MEM_UNDEFINED(p,size)
|
|
|
|
#define mi_track_mem_noaccess(p,size) VALGRIND_MAKE_MEM_NOACCESS(p,size)
|
2022-10-29 05:54:56 +03:00
|
|
|
|
2022-11-28 21:55:19 +03:00
|
|
|
#elif MI_ASAN
|
|
|
|
|
2023-03-06 05:18:41 +03:00
|
|
|
#define MI_TRACK_ENABLED 1
|
|
|
|
#define MI_TRACK_HEAP_DESTROY 0
|
|
|
|
#define MI_TRACK_TOOL "asan"
|
2022-11-28 21:55:19 +03:00
|
|
|
|
|
|
|
#include <sanitizer/asan_interface.h>
|
|
|
|
|
2023-03-05 22:01:51 +03:00
|
|
|
#define mi_track_malloc_size(p,reqsize,size,zero) ASAN_UNPOISON_MEMORY_REGION(p,size)
|
|
|
|
#define mi_track_free_size(p,size) ASAN_POISON_MEMORY_REGION(p,size)
|
|
|
|
#define mi_track_mem_defined(p,size) ASAN_UNPOISON_MEMORY_REGION(p,size)
|
|
|
|
#define mi_track_mem_undefined(p,size) ASAN_UNPOISON_MEMORY_REGION(p,size)
|
|
|
|
#define mi_track_mem_noaccess(p,size) ASAN_POISON_MEMORY_REGION(p,size)
|
2022-11-28 21:55:19 +03:00
|
|
|
|
2022-10-29 05:54:56 +03:00
|
|
|
#else
|
|
|
|
|
2023-03-06 05:18:41 +03:00
|
|
|
#define MI_TRACK_ENABLED 0
|
|
|
|
#define MI_TRACK_HEAP_DESTROY 0
|
|
|
|
#define MI_TRACK_TOOL "none"
|
2022-10-29 21:43:09 +03:00
|
|
|
|
2023-03-05 22:01:51 +03:00
|
|
|
#define mi_track_malloc_size(p,reqsize,size,zero)
|
2022-11-28 21:55:19 +03:00
|
|
|
#define mi_track_free_size(p,_size)
|
2023-03-05 22:01:51 +03:00
|
|
|
#define mi_track_align(p,alignedp,offset,size)
|
|
|
|
#define mi_track_resize(p,oldsize,newsize)
|
2022-12-03 02:23:43 +03:00
|
|
|
#define mi_track_mem_defined(p,size)
|
|
|
|
#define mi_track_mem_undefined(p,size)
|
|
|
|
#define mi_track_mem_noaccess(p,size)
|
2022-10-29 05:54:56 +03:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2023-03-05 22:01:51 +03:00
|
|
|
#ifndef mi_track_resize
|
|
|
|
#define mi_track_resize(p,oldsize,newsize) mi_track_free_size(p,oldsize); mi_track_malloc(p,newsize,false)
|
2023-03-05 01:49:02 +03:00
|
|
|
#endif
|
|
|
|
|
2023-03-05 22:01:51 +03:00
|
|
|
#ifndef mi_track_align
|
|
|
|
#define mi_track_align(p,alignedp,offset,size) mi_track_mem_noaccess(p,offset)
|
2023-03-05 01:49:02 +03:00
|
|
|
#endif
|
|
|
|
|
2023-03-05 22:01:51 +03:00
|
|
|
#if MI_PADDING
|
|
|
|
#define mi_track_malloc(p,reqsize,zero) \
|
|
|
|
if ((p)!=NULL) { \
|
|
|
|
mi_assert_internal(mi_usable_size(p)==(reqsize)); \
|
|
|
|
mi_track_malloc_size(p,reqsize,reqsize,zero); \
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define mi_track_malloc(p,reqsize,zero) \
|
|
|
|
if ((p)!=NULL) { \
|
|
|
|
mi_assert_internal(mi_usable_size(p)>=(reqsize)); \
|
|
|
|
mi_track_malloc_size(p,reqsize,mi_usable_size(p),zero); \
|
|
|
|
}
|
|
|
|
#endif
|
2023-03-05 01:49:02 +03:00
|
|
|
|
2022-10-29 05:54:56 +03:00
|
|
|
#endif
|