| 1 | /* |
| 2 | * Copyright (c) 2000-2011 by Hewlett-Packard Development Company. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED |
| 6 | * OR IMPLIED. ANY USE IS AT YOUR OWN RISK. |
| 7 | * |
| 8 | * Permission is hereby granted to use or copy this program |
| 9 | * for any purpose, provided the above notices are retained on all copies. |
| 10 | * Permission to modify the code and to distribute modified code is granted, |
| 11 | * provided the above notices are retained, and a notice that the code was |
| 12 | * modified is included with the above copyright notice. |
| 13 | */ |
| 14 | |
| 15 | #ifndef GC_LEAK_DETECTOR_H |
| 16 | #define GC_LEAK_DETECTOR_H |
| 17 | |
| 18 | /* |
| 19 | * Include this header file (e.g., via gcc `--include` option) to turn |
| 20 | * the collector into a leak detector. |
| 21 | */ |
| 22 | |
| 23 | #ifndef GC_DEBUG |
| 24 | # define GC_DEBUG |
| 25 | #endif |
| 26 | #include "gc.h" |
| 27 | |
| 28 | #ifndef GC_DONT_INCLUDE_STDLIB |
| 29 | /* |
| 30 | * We ensure the platform `stdlib.h` and `string.h` files are included |
| 31 | * before redirecting `malloc` and the accompanying functions. |
| 32 | */ |
| 33 | # include <stdlib.h> |
| 34 | # include <string.h> |
| 35 | #endif |
| 36 | |
| 37 | #undef malloc |
| 38 | #define malloc(n) GC_MALLOC(n) |
| 39 | #undef calloc |
| 40 | #define calloc(m, n) GC_MALLOC((m) * (n)) |
| 41 | #undef free |
| 42 | #define free(p) GC_FREE(p) |
| 43 | #undef realloc |
| 44 | #define realloc(p, n) GC_REALLOC(p, n) |
| 45 | #undef reallocarray |
| 46 | #define reallocarray(p, m, n) GC_REALLOC(p, (m) * (n)) |
| 47 | |
| 48 | #undef strdup |
| 49 | #define strdup(s) GC_STRDUP(s) |
| 50 | #undef strndup |
| 51 | #define strndup(s, n) GC_STRNDUP(s, n) |
| 52 | |
| 53 | #ifdef GC_REQUIRE_WCSDUP |
| 54 | /* |
| 55 | * The collector should be built with `GC_REQUIRE_WCSDUP` macro defined |
| 56 | * as well to redirect `wcsdup`. |
| 57 | */ |
| 58 | # include <wchar.h> |
| 59 | # undef wcsdup |
| 60 | # define wcsdup(s) GC_WCSDUP(s) |
| 61 | #endif |
| 62 | |
| 63 | /* |
| 64 | * The following routines for the aligned objects allocation |
| 65 | * (`aligned_alloc`, `valloc`, etc.) do not have their debugging |
| 66 | * counterparts. Note that `free()` called for such objects may output |
| 67 | * a warning that the pointer has no debugging info. |
| 68 | */ |
| 69 | |
| 70 | #undef aligned_alloc |
| 71 | #define aligned_alloc(a, n) GC_memalign(a, n) /*< identical to `memalign` */ |
| 72 | #undef memalign |
| 73 | #define memalign(a, n) GC_memalign(a, n) |
| 74 | #undef posix_memalign |
| 75 | #define posix_memalign(p, a, n) GC_posix_memalign(p, a, n) |
| 76 | |
| 77 | /* These are defined in the C23 standard. */ |
| 78 | #undef free_sized |
| 79 | #define free_sized(p, n) (free(p), (void)(n)) |
| 80 | #undef free_aligned_sized |
| 81 | #define free_aligned_sized(p, a, n) \ |
| 82 | (GC_free(p) /* non-debug */, (void)(a), (void)(n)) |
| 83 | |
| 84 | #undef cfree /*< obsolete */ |
| 85 | #if defined(sun) || defined(__sun) |
| 86 | # define cfree(p, m, n) ((void)(m), (void)(n), free(p)) |
| 87 | #else |
| 88 | # define cfree(p) free(p) |
| 89 | #endif |
| 90 | |
| 91 | #undef _aligned_malloc |
| 92 | #define _aligned_malloc(n, a) GC_memalign(a, n) /*< reverse args order */ |
| 93 | #undef _aligned_free |
| 94 | #define _aligned_free(p) GC_free(p) /*< non-debug */ |
| 95 | |
| 96 | #ifndef GC_NO_VALLOC |
| 97 | # undef valloc |
| 98 | # define valloc(n) GC_valloc(n) |
| 99 | # undef pvalloc |
| 100 | # define pvalloc(n) GC_pvalloc(n) /*< obsolete */ |
| 101 | #endif |
| 102 | |
| 103 | #undef malloc_usable_size /*< available in `glibc` */ |
| 104 | #define malloc_usable_size(p) GC_size(p) |
| 105 | #undef malloc_size /*< available in Darwin */ |
| 106 | #define malloc_size(p) GC_size(p) |
| 107 | #undef _msize /*< available in Windows CRT */ |
| 108 | #define _msize(p) GC_size(p) |
| 109 | |
| 110 | #ifndef CHECK_LEAKS |
| 111 | /** |
| 112 | * Note 1: `CHECK_LEAKS` does not have `GC_` prefix (preserved for |
| 113 | * backward compatibility). |
| 114 | * Note 2: `GC_gcollect()` is also called automatically in the |
| 115 | * find-leak mode at program exit. |
| 116 | */ |
| 117 | # define CHECK_LEAKS() GC_gcollect() |
| 118 | #endif |
| 119 | |
| 120 | #endif /* GC_LEAK_DETECTOR_H */ |
| 121 | |