File indexing completed on 2024-04-21 05:41:33

0001 /*
0002     SPDX-FileCopyrightText: 2014-2017 Milian Wolff <mail@milianw.de>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-or-later
0005 */
0006 
0007 #include <cstdio>
0008 #include <cstdlib>
0009 #include <unistd.h>
0010 
0011 #include "util/config.h"
0012 
0013 #if defined(_ISOC11_SOURCE)
0014 #define HAVE_ALIGNED_ALLOC 1
0015 #else
0016 #define HAVE_ALIGNED_ALLOC 0
0017 #endif
0018 
0019 struct Foo
0020 {
0021     Foo()
0022         : i(new int)
0023     {
0024     }
0025     ~Foo()
0026     {
0027         delete i;
0028     }
0029     int* i;
0030 };
0031 
0032 void asdf()
0033 {
0034     int* i = new int;
0035     printf("i in asdf: %p\n", (void*)i);
0036 }
0037 
0038 void bar()
0039 {
0040     asdf();
0041 }
0042 
0043 void laaa()
0044 {
0045     bar();
0046 }
0047 
0048 void split()
0049 {
0050     Foo f;
0051     asdf();
0052     bar();
0053     laaa();
0054 }
0055 
0056 static Foo foo;
0057 
0058 int main()
0059 {
0060     Foo* f = new Foo;
0061     printf("new Foo: %p\n", (void*)f);
0062     delete f;
0063 
0064     char* c = new char[1000];
0065     printf("new char[]: %p\n", (void*)c);
0066     delete[] c;
0067 
0068     void* buf = malloc(100);
0069     printf("malloc: %p\n", buf);
0070     buf = realloc(buf, 200);
0071     printf("realloc: %p\n", buf);
0072     free(buf);
0073 
0074     buf = calloc(5, 5);
0075     printf("calloc: %p\n", buf);
0076 #if HAVE_CFREE
0077     cfree(buf);
0078 #else
0079     free(buf);
0080 #endif
0081 
0082 #if HAVE_ALIGNED_ALLOC
0083     buf = aligned_alloc(16, 160);
0084     printf("aligned_alloc: %p\n", buf);
0085     free(buf);
0086 #endif
0087 
0088     buf = valloc(32);
0089     printf("valloc: %p\n", buf);
0090     free(buf);
0091 
0092     int ret = posix_memalign(&buf, 16, 64);
0093     printf("posix_memalign: %d %p\n", ret, buf);
0094     free(buf);
0095 
0096     for (int i = 0; i < 10; ++i) {
0097         laaa();
0098     }
0099     laaa();
0100 
0101     split();
0102 
0103     return 0;
0104 }