2022-10-28 19:54:56 -07: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-04 15:17:13 -08:00
|
|
|
/* ------------------------------------------------------------------------------------------------------
|
|
|
|
Track memory ranges with macros for tools like Valgrind
|
|
|
|
address sanitizer, or other memory checkers.
|
2022-10-28 19:54:56 -07:00
|
|
|
|
2023-03-04 15:17:13 -08:00
|
|
|
The macros are set up such that the size passed to `mi_track_free_size`
|
|
|
|
matches the size of the allocation, or the newsize of a `mi_track_resize` (currently unused though).
|
|
|
|
|
|
|
|
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-04 14:49:02 -08:00
|
|
|
|
2022-10-28 19:54:56 -07:00
|
|
|
#if MI_VALGRIND
|
2022-10-29 11:43:09 -07:00
|
|
|
|
|
|
|
#define MI_TRACK_ENABLED 1
|
2022-11-28 10:55:19 -08:00
|
|
|
#define MI_TRACK_TOOL "valgrind"
|
2022-10-29 11:43:09 -07:00
|
|
|
|
2022-10-28 19:54:56 -07:00
|
|
|
#include <valgrind/valgrind.h>
|
|
|
|
#include <valgrind/memcheck.h>
|
|
|
|
|
2022-10-30 10:45:51 -07:00
|
|
|
#define mi_track_malloc(p,size,zero) VALGRIND_MALLOCLIKE_BLOCK(p,size,MI_PADDING_SIZE /*red zone*/,zero)
|
2022-12-03 00:23:43 +01:00
|
|
|
#define mi_track_resize(p,oldsize,newsize) VALGRIND_RESIZEINPLACE_BLOCK(p,oldsize,newsize,MI_PADDING_SIZE /*red zone*/)
|
2023-03-04 14:49:02 -08:00
|
|
|
#define mi_track_free_size(p,_size) VALGRIND_FREELIKE_BLOCK(p,MI_PADDING_SIZE /*red zone*/)
|
2022-10-29 10:44:10 -07:00
|
|
|
#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-28 19:54:56 -07:00
|
|
|
|
2022-11-28 10:55:19 -08:00
|
|
|
#elif MI_ASAN
|
|
|
|
|
|
|
|
#define MI_TRACK_ENABLED 1
|
|
|
|
#define MI_TRACK_TOOL "asan"
|
|
|
|
|
|
|
|
#include <sanitizer/asan_interface.h>
|
|
|
|
|
|
|
|
#define mi_track_malloc(p,size,zero) ASAN_UNPOISON_MEMORY_REGION(p,size)
|
|
|
|
#define mi_track_resize(p,oldsize,newsize) ASAN_POISON_MEMORY_REGION(p,oldsize); ASAN_UNPOISON_MEMORY_REGION(p,newsize)
|
|
|
|
#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-10-28 19:54:56 -07:00
|
|
|
#else
|
|
|
|
|
2022-10-29 11:43:09 -07:00
|
|
|
#define MI_TRACK_ENABLED 0
|
2022-11-28 10:55:19 -08:00
|
|
|
#define MI_TRACK_TOOL "none"
|
2022-10-29 11:43:09 -07:00
|
|
|
|
2022-12-03 00:23:43 +01:00
|
|
|
#define mi_track_malloc(p,size,zero)
|
|
|
|
#define mi_track_resize(p,oldsize,newsize)
|
2022-11-28 10:55:19 -08:00
|
|
|
#define mi_track_free_size(p,_size)
|
2022-12-03 00:23:43 +01:00
|
|
|
#define mi_track_mem_defined(p,size)
|
|
|
|
#define mi_track_mem_undefined(p,size)
|
|
|
|
#define mi_track_mem_noaccess(p,size)
|
2022-10-28 19:54:56 -07:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2023-03-04 14:49:02 -08:00
|
|
|
#ifndef mi_track_free
|
|
|
|
#define mi_track_free(p) mi_track_free_size(p,mi_usable_size(p));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef mi_track_resize
|
|
|
|
#define mi_track_resize(p,oldsize,newsize) mi_track_free_size(p,oldsize); mi_track_malloc(p,newsize,false)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2022-10-28 19:54:56 -07:00
|
|
|
#endif
|