xref: /aosp_15_r20/external/compiler-rt/test/profile/Inputs/instrprof-alloc.c (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot /* This test case tests that when static allocation for value
2*7c3d14c8STreehugger Robot  * profiler is on, no malloc/calloc calls will be invoked by
3*7c3d14c8STreehugger Robot  * profile runtime library. */
4*7c3d14c8STreehugger Robot #include <stdlib.h>
foo()5*7c3d14c8STreehugger Robot __attribute__((noinline)) void foo() {}
foo2()6*7c3d14c8STreehugger Robot __attribute__((noinline)) void foo2() {}
7*7c3d14c8STreehugger Robot void (*FP)();
8*7c3d14c8STreehugger Robot int MainEntered = 0;
9*7c3d14c8STreehugger Robot int CallocCalled = 0;
10*7c3d14c8STreehugger Robot int MallocCalled = 0;
11*7c3d14c8STreehugger Robot 
12*7c3d14c8STreehugger Robot extern void *__real_calloc(size_t s, size_t n);
13*7c3d14c8STreehugger Robot extern void *__real_malloc(size_t s);
14*7c3d14c8STreehugger Robot 
__wrap_calloc(size_t s,size_t n)15*7c3d14c8STreehugger Robot void *__wrap_calloc(size_t s, size_t n) {
16*7c3d14c8STreehugger Robot   if (MainEntered)
17*7c3d14c8STreehugger Robot     CallocCalled = 1;
18*7c3d14c8STreehugger Robot   return __real_calloc(s, n);
19*7c3d14c8STreehugger Robot }
__wrap_malloc(size_t s)20*7c3d14c8STreehugger Robot void *__wrap_malloc(size_t s) {
21*7c3d14c8STreehugger Robot   if (MainEntered)
22*7c3d14c8STreehugger Robot     MallocCalled = 1;
23*7c3d14c8STreehugger Robot   return __real_malloc(s);
24*7c3d14c8STreehugger Robot }
25*7c3d14c8STreehugger Robot 
getFP(int i)26*7c3d14c8STreehugger Robot void getFP(int i) {
27*7c3d14c8STreehugger Robot   if (i % 2)
28*7c3d14c8STreehugger Robot     FP = foo;
29*7c3d14c8STreehugger Robot   else
30*7c3d14c8STreehugger Robot     FP = foo2;
31*7c3d14c8STreehugger Robot }
32*7c3d14c8STreehugger Robot 
main()33*7c3d14c8STreehugger Robot int main() {
34*7c3d14c8STreehugger Robot   int i;
35*7c3d14c8STreehugger Robot   MainEntered = 1;
36*7c3d14c8STreehugger Robot   for (i = 0; i < 100; i++) {
37*7c3d14c8STreehugger Robot     getFP(i);
38*7c3d14c8STreehugger Robot     FP();
39*7c3d14c8STreehugger Robot   }
40*7c3d14c8STreehugger Robot   return CallocCalled + MallocCalled;
41*7c3d14c8STreehugger Robot }
42