initial progress on valgrind integration

This commit is contained in:
daan 2022-10-28 19:54:56 -07:00
parent eb29d6b06f
commit 6eeb81ee05
4 changed files with 82 additions and 9 deletions

View file

@ -9,6 +9,7 @@ terms of the MIT license. A copy of the license can be found in the file
#define MIMALLOC_INTERNAL_H
#include "mimalloc-types.h"
#include "mimalloc-track.h"
#if (MI_DEBUG>0)
#define mi_trace_message(...) _mi_trace_message(__VA_ARGS__)
@ -625,21 +626,27 @@ static inline mi_encoded_t mi_ptr_encode(const void* null, const void* p, const
}
static inline mi_block_t* mi_block_nextx( const void* null, const mi_block_t* block, const uintptr_t* keys ) {
MI_TRACK_MEM_DEFINED(block,sizeof(mi_block_t));
mi_block_t* next;
#ifdef MI_ENCODE_FREELIST
return (mi_block_t*)mi_ptr_decode(null, block->next, keys);
next = (mi_block_t*)mi_ptr_decode(null, block->next, keys);
#else
MI_UNUSED(keys); MI_UNUSED(null);
return (mi_block_t*)block->next;
next = (mi_block_t*)block->next;
#endif
MI_TRACK_MEM_NOACCESS(block,sizeof(mi_block_t));
return next;
}
static inline void mi_block_set_nextx(const void* null, mi_block_t* block, const mi_block_t* next, const uintptr_t* keys) {
MI_TRACK_MEM_UNDEFINED(block,sizeof(mi_block_t));
#ifdef MI_ENCODE_FREELIST
block->next = mi_ptr_encode(null, next, keys);
#else
MI_UNUSED(keys); MI_UNUSED(null);
block->next = (mi_encoded_t)next;
#endif
MI_TRACK_MEM_NOACCESS(block,sizeof(mi_block_t));
}
static inline mi_block_t* mi_block_next(const mi_page_t* page, const mi_block_t* block) {

41
include/mimalloc-track.h Normal file
View file

@ -0,0 +1,41 @@
/* ----------------------------------------------------------------------------
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
// ------------------------------------------------------
// Track memory ranges with macros for tools like Valgrind
// or other memory checkers.
// ------------------------------------------------------
#define MI_VALGRIND 1
#if MI_VALGRIND
#include <valgrind/valgrind.h>
#include <valgrind/memcheck.h>
#define MI_TRACK_ZALLOC(p,size,zero) VALGRIND_MALLOCLIKE_BLOCK(p,size,0 /*red zone*/,zero)
#define MI_TRACK_MALLOC(p,size) MI_TRACK_ZALLOC(p,size,false)
#define MI_TRACK_FREE(p) VALGRIND_FREELIKE_BLOCK(p,0 /*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)
#else
#define MI_TRACK_ZALLOC(p,size,zero)
#define MI_TRACK_MALLOC(p,size)
#define MI_TRACK_FREE(p)
#define MI_TRACK_MEM_DEFINED(p,size)
#define MI_TRACK_MEM_UNDEFINED(p,size)
#define MI_TRACK_MEM_NOACCESS(p,size)
#endif
#endif