Skip to content

Commit 0db5511

Browse files
toxaartDavid Holmes
authored andcommitted
8354969: Add strdup function for ResourceArea
Reviewed-by: dholmes, iklam
1 parent f8fc7ee commit 0db5511

File tree

8 files changed

+31
-12
lines changed

8 files changed

+31
-12
lines changed

src/hotspot/os/windows/perfMemory_windows.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1606,10 +1606,8 @@ static void open_file_mapping(int vmid, char** addrp, size_t* sizep, TRAPS) {
16061606
// using resource arrays for these names prevents the leaks
16071607
// that would otherwise occur.
16081608
//
1609-
char* rfilename = NEW_RESOURCE_ARRAY(char, strlen(filename) + 1);
1610-
char* robjectname = NEW_RESOURCE_ARRAY(char, strlen(objectname) + 1);
1611-
strcpy(rfilename, filename);
1612-
strcpy(robjectname, objectname);
1609+
char* rfilename = ResourceArea::strdup(THREAD, filename);
1610+
char* robjectname = ResourceArea::strdup(THREAD, objectname);
16131611

16141612
// free the c heap resources that are no longer needed
16151613
FREE_C_HEAP_ARRAY(char, luser);

src/hotspot/share/classfile/classLoader.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1507,8 +1507,7 @@ char* ClassLoader::get_canonical_path(const char* orig, Thread* thread) {
15071507
char* canonical_path = NEW_RESOURCE_ARRAY_IN_THREAD(thread, char, JVM_MAXPATHLEN);
15081508
ResourceMark rm(thread);
15091509
// os::native_path writes into orig_copy
1510-
char* orig_copy = NEW_RESOURCE_ARRAY_IN_THREAD(thread, char, strlen(orig)+1);
1511-
strcpy(orig_copy, orig);
1510+
char* orig_copy = ResourceArea::strdup(thread, orig);
15121511
if ((CanonicalizeEntry)(os::native_path(orig_copy), canonical_path, JVM_MAXPATHLEN) < 0) {
15131512
return nullptr;
15141513
}

src/hotspot/share/classfile/modules.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -641,8 +641,7 @@ const char* Modules::ArchivedProperty::get_numbered_property_as_sorted_string()
641641
if (prop_value == nullptr) {
642642
break;
643643
}
644-
char* p = resource_allocate_bytes(strlen(prop_value) + 1);
645-
strcpy(p, prop_value);
644+
char* p = ResourceArea::strdup(prop_value);
646645
while (*p == ',') p++; // skip leading commas
647646
while (*p) {
648647
char* next = strchr(p, ',');

src/hotspot/share/memory/arena.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,13 @@ class Arena : public CHeapObjBase {
216216
MemTag get_mem_tag() const { return _mem_tag; }
217217
Tag get_tag() const { return _tag; }
218218

219+
char* strdup(const char* s) {
220+
const size_t sz = strlen(s) + 1;
221+
char* ptr = (char*)Amalloc(sz);
222+
memcpy(ptr, s, sz);
223+
return ptr;
224+
}
225+
219226
private:
220227
// Reset this Arena to empty, access will trigger grow if necessary
221228
void reset(void) {

src/hotspot/share/memory/resourceArea.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,14 @@ class ResourceArea: public Arena {
143143
assert(_max == state._max, "Sanity check: idempotence");
144144
}
145145
}
146+
147+
static char* strdup(const char* src) {
148+
return static_cast<Arena*>(Thread::current()->resource_area())->strdup(src);
149+
}
150+
151+
static char* strdup(Thread* thread, const char* src) {
152+
return static_cast<Arena*>(thread->resource_area())->strdup(src);
153+
}
146154
};
147155

148156

src/hotspot/share/prims/jvmtiEnvBase.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -468,8 +468,7 @@ JvmtiEnvBase::get_all_native_method_prefixes(int* count_ptr) {
468468
// retrieve a prefix and so that it is safe against asynchronous changes
469469
// copy it into the resource area
470470
char* prefix = prefixes[j];
471-
char* prefix_copy = NEW_RESOURCE_ARRAY(char, strlen(prefix)+1);
472-
strcpy(prefix_copy, prefix);
471+
char* prefix_copy = ResourceArea::strdup(prefix);
473472
prefix_array->at_put_grow(total_count++, prefix_copy);
474473
}
475474
}

src/hotspot/share/services/heapDumper.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2709,8 +2709,7 @@ HeapDumper::~HeapDumper() {
27092709
// returns the error string (resource allocated), or null
27102710
char* HeapDumper::error_as_C_string() const {
27112711
if (error() != nullptr) {
2712-
char* str = NEW_RESOURCE_ARRAY(char, strlen(error())+1);
2713-
strcpy(str, error());
2712+
char* str = ResourceArea::strdup(error());
27142713
return str;
27152714
} else {
27162715
return nullptr;

test/hotspot/gtest/memory/test_arena.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,3 +381,13 @@ TEST_VM(Arena, different_chunk_sizes) {
381381
Arena ar7(mtTest, Arena::Tag::tag_other, random_arena_chunk_size());
382382
}
383383
}
384+
385+
TEST_VM(Arena, string_duplicate)
386+
{
387+
char testString[] = "this is a test string";
388+
Arena ar(mtTest);
389+
auto copy = ar.strdup(&testString[0]);
390+
int result = strcmp(testString, copy);
391+
ASSERT_TRUE(0 == result);
392+
ASSERT_NE(copy, &testString[0]);
393+
}

0 commit comments

Comments
 (0)