Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/hotspot/os/windows/perfMemory_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1606,10 +1606,8 @@ static void open_file_mapping(int vmid, char** addrp, size_t* sizep, TRAPS) {
// using resource arrays for these names prevents the leaks
// that would otherwise occur.
//
char* rfilename = NEW_RESOURCE_ARRAY(char, strlen(filename) + 1);
char* robjectname = NEW_RESOURCE_ARRAY(char, strlen(objectname) + 1);
strcpy(rfilename, filename);
strcpy(robjectname, objectname);
char* rfilename = ResourceArea::strdup(THREAD, filename);
char* robjectname = ResourceArea::strdup(THREAD, objectname);

// free the c heap resources that are no longer needed
FREE_C_HEAP_ARRAY(char, luser);
Expand Down
3 changes: 1 addition & 2 deletions src/hotspot/share/classfile/classLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1510,8 +1510,7 @@ char* ClassLoader::get_canonical_path(const char* orig, Thread* thread) {
char* canonical_path = NEW_RESOURCE_ARRAY_IN_THREAD(thread, char, JVM_MAXPATHLEN);
ResourceMark rm(thread);
// os::native_path writes into orig_copy
char* orig_copy = NEW_RESOURCE_ARRAY_IN_THREAD(thread, char, strlen(orig)+1);
strcpy(orig_copy, orig);
char* orig_copy = ResourceArea::strdup(thread, orig);
if ((CanonicalizeEntry)(os::native_path(orig_copy), canonical_path, JVM_MAXPATHLEN) < 0) {
return nullptr;
}
Expand Down
3 changes: 1 addition & 2 deletions src/hotspot/share/classfile/modules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -639,8 +639,7 @@ const char* Modules::ArchivedProperty::get_numbered_property_as_sorted_string()
if (prop_value == nullptr) {
break;
}
char* p = resource_allocate_bytes(strlen(prop_value) + 1);
strcpy(p, prop_value);
char* p = ResourceArea::strdup(prop_value);
while (*p == ',') p++; // skip leading commas
while (*p) {
char* next = strchr(p, ',');
Expand Down
7 changes: 7 additions & 0 deletions src/hotspot/share/memory/arena.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,13 @@ class Arena : public CHeapObjBase {
MemTag get_mem_tag() const { return _mem_tag; }
Tag get_tag() const { return _tag; }

char* strdup(const char* s) {
const size_t sz = strlen(s) + 1;
char* ptr = (char*)Amalloc(sz);
memcpy(ptr, s, sz);
return ptr;
}

private:
// Reset this Arena to empty, access will trigger grow if necessary
void reset(void) {
Expand Down
8 changes: 8 additions & 0 deletions src/hotspot/share/memory/resourceArea.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,14 @@ class ResourceArea: public Arena {
assert(_max == state._max, "Sanity check: idempotence");
}
}

static char* strdup(const char* src) {
return static_cast<Arena*>(Thread::current()->resource_area())->strdup(src);
}

static char* strdup(Thread* thread, const char* src) {
return static_cast<Arena*>(thread->resource_area())->strdup(src);
}
};


Expand Down
3 changes: 1 addition & 2 deletions src/hotspot/share/prims/jvmtiEnvBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -468,8 +468,7 @@ JvmtiEnvBase::get_all_native_method_prefixes(int* count_ptr) {
// retrieve a prefix and so that it is safe against asynchronous changes
// copy it into the resource area
char* prefix = prefixes[j];
char* prefix_copy = NEW_RESOURCE_ARRAY(char, strlen(prefix)+1);
strcpy(prefix_copy, prefix);
char* prefix_copy = ResourceArea::strdup(prefix);
prefix_array->at_put_grow(total_count++, prefix_copy);
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/hotspot/share/services/heapDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2709,8 +2709,7 @@ HeapDumper::~HeapDumper() {
// returns the error string (resource allocated), or null
char* HeapDumper::error_as_C_string() const {
if (error() != nullptr) {
char* str = NEW_RESOURCE_ARRAY(char, strlen(error())+1);
strcpy(str, error());
char* str = ResourceArea::strdup(error());
return str;
} else {
return nullptr;
Expand Down
10 changes: 10 additions & 0 deletions test/hotspot/gtest/memory/test_arena.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,3 +381,13 @@ TEST_VM(Arena, different_chunk_sizes) {
Arena ar7(mtTest, Arena::Tag::tag_other, random_arena_chunk_size());
}
}

TEST_VM(Arena, string_duplicate)
{
char testString[] = "this is a test string";
Arena ar(mtTest);
auto copy = ar.strdup(&testString[0]);
int result = strcmp(testString, copy);
ASSERT_TRUE(0 == result);
ASSERT_NE(copy, &testString[0]);
}