Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
MallocGuard.h
Go to the documentation of this file.
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#ifndef _Stroika_Foundation_Debug_MallocGuard_h_
5#define _Stroika_Foundation_Debug_MallocGuard_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
9#if qStroika_Foundation_Common_Platform_Windows
10#include <windows.h>
11
12#include <tchar.h>
13#endif
14#include <array>
15
16#include "Stroika/Foundation/Characters/SDKChar.h"
17#include "Stroika/Foundation/Characters/SDKString.h"
18#include "Stroika/Foundation/Common/Common.h"
21
22/**
23 * \file
24 *
25 * \note Code-Status: <a href="Code-Status.md#Beta">Beta</a>
26 *
27 * TODO:
28 * @todo http://stroika-bugs.sophists.com/browse/STK-621 stopped testing regularly in Stroika
29 * 2.0a217 (october 2017).
30 *
31 * @todo Support on Windows/MSVC
32 *
33 * @todo Maybe support generically (not sure if/how possible)
34 *
35 * @todo Finish support for stuff like posix_memalign () and other functions that maybe needed
36 *
37 * @todo Consider doing an implementation with the 'wrap' logic'
38 * http://stackoverflow.com/questions/262439/create-a-wrapper-function-for-malloc-and-free-in-c
39 * --wrap=symbol
40 * void *__wrap_malloc (size_t c)
41 * {
42 * printf ("malloc called with %zu\n", c);
43 * return __real_malloc (c);
44 * }
45 */
46
47namespace Stroika::Foundation::Debug {
48
49 /**
50 * If qStroika_Foundation_Debug_MallocGuard defined to 1, wrap malloc(), free () etc, to do extra checking for corruption, double free
51 * write off the ends, etc.
52 *
53 * This works in DEBUG or RELEASE builds.
54 *
55 * This - so far - only works for GCC.
56 *
57 * This may not work perfectly if you call some weird malloc variants, or mix __libc_malloc with free (), etc.
58 *
59 * \note You can enable this feature with
60 * ./configure ... --malloc-guard true
61 *
62 * \note If you can use ./configure --sanitize=address, that probably works better. Or - possibly - using valgrind.
63 *
64 * \note On detected errors, this will call std::terminate ();
65 * In Debug or Release versions (if you call Debug::RegisterDefaultFatalErrorHandlers ()) - you will get a stack trace dumped
66 * and typically a core file - when errors are detected. Though maybe not, since that stuff all allocates memory, and clearly thats
67 * not working well when we fail...
68 */
69#if !defined(qStroika_Foundation_Debug_MallocGuard)
70#define qStroika_Foundation_Debug_MallocGuard 0
71#endif
72
73 /**
74 * qStroika_Foundation_Debug_MallocGuard_GuardSize can be 0, or any integer number greater;
75 *
76 * \pre qStroika_Foundation_Debug_MallocGuard
77 */
78#if !defined(qStroika_Foundation_Debug_MallocGuard_GuardSize)
79#define qStroika_Foundation_Debug_MallocGuard_GuardSize 16
80#endif
81
82}
83
84/*
85 ********************************************************************************
86 ***************************** Implementation Details ***************************
87 ********************************************************************************
88 */
89#include "MallocGuard.inl"
90
91#endif /*_Stroika_Foundation_Debug_MallocGuard_h_*/