* Copyright (c) 2021, 2022 SAP SE. All rights reserved.
* Copyright (c) 2021, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
#include "precompiled.hpp"
#include "memory/allocation.hpp"
#include "runtime/os.hpp"
#include "services/memTracker.hpp"
#include "services/nmtPreInit.hpp"
#include "utilities/debug.hpp"
#include "utilities/ostream.hpp"
#include "unittest.hpp"
#define LOG(...)
static void* os_malloc(size_t s) { return os::malloc(s, mtTest); }
static void* os_realloc(void* old, size_t s) { return os::realloc(old, s, mtTest); }
static void log_state() {
char tmp[256];
stringStream ss(tmp, sizeof(tmp));
NMTPreInit::print_state(&ss);
LOG("%s", tmp);
}
class TestAllocations {
void* p1, *p2, *p3, *p4;
public:
TestAllocations() {
test_pre();
}
void test_pre() {
assert(!MemTracker::is_initialized(),
"This should be run in pre-init phase (as part of C++ dyn. initialization)");
LOG("corner cases, pre-init (%d)", os::current_process_id());
log_state();
p1 = os_malloc(100);
os::free(os_malloc(0));
p2 = os_realloc(os_malloc(10), 20);
p3 = os_realloc(os_malloc(20), 10);
p4 = os_realloc(nullptr, 10);
os_realloc(os_realloc(os_malloc(20), 0), 30);
os::free(os_malloc(20));
os::free(os_realloc(os_malloc(20), 30));
os::free(nullptr);
DEBUG_ONLY(NMTPreInit::verify();)
log_state();
}
void test_post() {
assert(MemTracker::is_initialized(),
"This should be run in post-init phase (from inside a TEST_VM test)");
LOG("corner cases, post-init (%d)", os::current_process_id());
log_state();
p1 = os_realloc(p1, 140);
p2 = os_realloc(p2, 150);
p3 = os_realloc(p3, 50);
p4 = os_realloc(p4, 8);
DEBUG_ONLY(NMTPreInit::verify();)
log_state();
}
void free_all() {
assert(MemTracker::is_initialized(),
"This should be run in post-init phase (from inside a TEST_VM test)");
LOG("corner cases, free-all (%d)", os::current_process_id());
log_state();
os::free(p1); os::free(p2); os::free(p3); os::free(p4);
DEBUG_ONLY(NMTPreInit::verify();)
log_state();
}
};
static TestAllocations g_test_allocations;
TEST_VM(NMTPreInit, pre_to_post_allocs) {
g_test_allocations.test_post();
g_test_allocations.free_all();
}