X-Git-Url: https://git.saurik.com/apple/dyld.git/blobdiff_plain/842490583351ae7f965714913a51f32fbde2867b..6cae9b637796eb548e85897ce8a2a058bb5799cf:/testing/test-cases/operator-new.dtest/main.cxx?ds=sidebyside diff --git a/testing/test-cases/operator-new.dtest/main.cxx b/testing/test-cases/operator-new.dtest/main.cxx index d985e8f..1773a03 100644 --- a/testing/test-cases/operator-new.dtest/main.cxx +++ b/testing/test-cases/operator-new.dtest/main.cxx @@ -13,24 +13,56 @@ // will turn around and call operator new in this main exectuable // -static void* ptr; +static void* myLastNewAllocation; +static void* myLastDelete; +// Note: this is not weak. That is specifically suppported void* operator new(size_t s) throw (std::bad_alloc) { - ptr = malloc(s); - return ptr; + myLastNewAllocation = malloc(s); + return myLastNewAllocation; +} + +struct Foo { + int bytes[10]; +}; + +// Note: this is weak and because it is in main executable should override OS +__attribute__((weak)) +void operator delete(void* p) throw() +{ + myLastDelete = p; + ::free(p); } int main() { printf("[BEGIN] operator-new\n"); + // test that OS's operator new[] redirects to my operator new + myLastNewAllocation = NULL; char* stuff = new char[24]; - if ( (void*)stuff == ptr ) - printf("[PASS] operator-new\n"); - else - printf("[FAIL] operator-new\n"); + if ( (void*)stuff != myLastNewAllocation ) { + printf("[FAIL] operator-new system array allocator not redirected through my operator new\n"); + return 0; + } + + // test that program uses my operator new + myLastNewAllocation = NULL; + Foo* foo = new Foo(); + if ( (void*)foo != myLastNewAllocation ) { + printf("[FAIL] operator-new allocation not redirected though my operator new\n"); + return 0; + } + + // + delete foo; + if ( (void*)foo != myLastDelete ) { + printf("[FAIL] operator-new deallocation not redirected though my operator delete\n"); + return 0; + } + printf("[PASS] operator-new\n"); return 0; }