From e492da2d2bf3a8af8751b495f4e9751b59a30833 Mon Sep 17 00:00:00 2001 From: Andy Wingo Date: Thu, 10 Mar 2022 16:42:06 +0100 Subject: [PATCH] Add heap validation to gcbench * GCBench.c (ValidateTree): New function. --- GCBench.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/GCBench.c b/GCBench.c index 6434fbdfb..903713d3c 100644 --- a/GCBench.c +++ b/GCBench.c @@ -42,6 +42,8 @@ #include #include +#include "assert.h" + #if defined(GC_BDW) #include "bdw.h" #elif defined(GC_SEMI) @@ -170,6 +172,22 @@ static Node* MakeTree(struct context *cx, int iDepth) { } } +static void ValidateTree(Node *tree, int depth) { +#ifndef NDEBUG + ASSERT_EQ(tree->i, 0); + ASSERT_EQ(tree->j, 0); + if (depth == 0) { + ASSERT(!tree->left); + ASSERT(!tree->right); + } else { + ASSERT(tree->left); + ASSERT(tree->right); + ValidateTree(tree->left, depth - 1); + ValidateTree(tree->right, depth - 1); + } +#endif +} + static void TimeConstruction(struct context *cx, int depth) { int iNumIters = NumIters(depth); NodeHandle tempTree = { NULL }; @@ -182,6 +200,7 @@ static void TimeConstruction(struct context *cx, int depth) { for (int i = 0; i < iNumIters; ++i) { HANDLE_SET(tempTree, allocate_node(cx)); Populate(cx, depth, HANDLE_REF(tempTree)); + ValidateTree(HANDLE_REF(tempTree), depth); HANDLE_SET(tempTree, NULL); } long tFinish = currentTime(); @@ -193,6 +212,7 @@ static void TimeConstruction(struct context *cx, int depth) { long tStart = currentTime(); for (int i = 0; i < iNumIters; ++i) { HANDLE_SET(tempTree, MakeTree(cx, depth)); + ValidateTree(HANDLE_REF(tempTree), depth); HANDLE_SET(tempTree, NULL); } long tFinish = currentTime(); @@ -234,6 +254,7 @@ int main() { // Stretch the memory space quickly HANDLE_SET(tempTree, MakeTree(cx, kStretchTreeDepth)); + ValidateTree(HANDLE_REF(tempTree), kStretchTreeDepth); HANDLE_SET(tempTree, NULL); // Create a long lived object @@ -253,6 +274,8 @@ int main() { TimeConstruction(cx, d); } + ValidateTree(HANDLE_REF(longLivedTree), kLongLivedTreeDepth); + if (HANDLE_REF(longLivedTree) == 0 || HANDLE_REF(array)->values[1000] != 1.0/1000) fprintf(stderr, "Failed\n");