[Ada] Eliminate redundant overflow checks for conversions from fixed-point
[official-gcc.git] / libbacktrace / instrumented_alloc.c
blobc2792b55f62790b1ca63957958c7affaa9dc8503
1 /* instrumented_alloc.c -- Memory allocation instrumented to fail when
2 requested, for testing purposes.
3 Copyright (C) 2018-2019 Free Software Foundation, Inc.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
7 met:
9 (1) Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
12 (2) Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in
14 the documentation and/or other materials provided with the
15 distribution.
17 (3) The name of the author may not be used to
18 endorse or promote products derived from this software without
19 specific prior written permission.
21 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30 IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 POSSIBILITY OF SUCH DAMAGE. */
33 /* Include all the header files of alloc here, to make sure they're not
34 processed when including alloc.c below, such that the redefinitions of malloc
35 and realloc are only effective in alloc.c itself. This does not work for
36 config.h, because it's not wrapped in "#ifndef CONFIG_H\n#define CONFIG_H"
37 and "#endif" but that does not seem to be harmful. */
39 #include "config.h"
41 #include <errno.h>
42 #include <stdlib.h>
43 #include <sys/types.h>
44 #include <inttypes.h>
46 #include "backtrace.h"
47 #include "internal.h"
49 extern void *instrumented_malloc (size_t size);
50 extern void *instrumented_realloc (void *ptr, size_t size);
52 #define malloc instrumented_malloc
53 #define realloc instrumented_realloc
54 #include "alloc.c"
55 #undef malloc
56 #undef realloc
58 static uint64_t nr_allocs = 0;
59 static uint64_t fail_at_alloc = 0;
61 extern int at_fail_alloc_p (void);
62 extern uint64_t get_nr_allocs (void);
63 extern void set_fail_at_alloc (uint64_t);
65 void *
66 instrumented_malloc (size_t size)
68 void *res;
70 if (at_fail_alloc_p ())
71 return NULL;
73 res = malloc (size);
74 if (res != NULL)
75 nr_allocs++;
77 return res;
80 void *
81 instrumented_realloc (void *ptr, size_t size)
83 void *res;
85 if (size != 0)
87 if (at_fail_alloc_p ())
88 return NULL;
91 res = realloc (ptr, size);
92 if (res != NULL)
93 nr_allocs++;
95 return res;
98 int
99 at_fail_alloc_p (void)
101 return fail_at_alloc == nr_allocs + 1;
104 uint64_t
105 get_nr_allocs (void)
107 return nr_allocs;
110 void
111 set_fail_at_alloc (uint64_t nr)
113 fail_at_alloc = nr;