| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| // See https://llvm.org/LICENSE.txt for license information. |
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| // RUN: cxx_compiler cxx_11 %s -c -o %t.o |
| // RUN: linker cxx_11 %t.o -o %t%exeext |
| // RUN: runtool %t%exeext | grep "TEST PASSED" |
| |
| #include <stdio.h> |
| #include <new> |
| |
| // No cookie required for a trivial object |
| |
| #define BUFF_SIZE 4096 |
| |
| // Globals |
| unsigned char alloc_buff[BUFF_SIZE]; |
| int new_calls = 0; |
| int delete_calls = 0; |
| |
| void *operator new[](size_t size) { new_calls++; return alloc_buff; } |
| void operator delete[](void *p) { delete_calls++; } |
| |
| struct trivial { }; |
| |
| int new_test() { |
| int errors = 0; |
| trivial *ptr = new trivial[2]; |
| |
| if((void *)ptr != (void *)alloc_buff) { |
| errors++; |
| printf("ERROR: new_test() pointers differ!\n"); |
| } |
| |
| delete [] ptr; |
| |
| return errors; |
| } |
| |
| int placement_new_test() { |
| int errors = 0; |
| trivial *ptr = new (alloc_buff) trivial[5]; |
| |
| if((void *)ptr != (void *)alloc_buff) { |
| errors++; |
| printf("ERROR: new_test() pointers differ!\n"); |
| } |
| |
| return errors; |
| } |
| |
| int main(int argc, char *argv[]) { |
| int retval = 0; |
| |
| retval += new_test(); |
| retval += placement_new_test(); |
| |
| if(retval) { |
| printf("TEST FAILED\n"); |
| retval = 1; |
| } else { |
| printf("TEST PASSED\n"); |
| retval = 0; |
| } |
| |
| return retval; |
| } |