This is an archive of the discontinued LLVM Phabricator instance.

[libc] Add strdup implementation.
AbandonedPublic

Authored by cgyurgyik on Jun 23 2020, 6:09 AM.

Details

Reviewers
None
Summary

Adds strdup implementation. Memcpy is more efficient since we don't need to check if the character is the null terminator with each iteration.

Diff Detail

Event Timeline

cgyurgyik created this revision.Jun 23 2020, 6:09 AM
cgyurgyik updated this revision to Diff 272701.Jun 23 2020, 6:10 AM

Remove strcmp from strdup testing.

cgyurgyik updated this revision to Diff 272703.Jun 23 2020, 6:12 AM

Add length variable to call strlen once.

Harbormaster completed remote builds in B61387: Diff 272703.
Harbormaster completed remote builds in B61383: Diff 272699.
cgyurgyik updated this revision to Diff 272727.Jun 23 2020, 7:46 AM

Add header for size_t.

abrachet added inline comments.
libc/src/string/strdup.cpp
18

We can't use new unfortunately. Even if operator new was a completely free standing allocator which didn't depend on libc functions we may not provide, operator new[] is not guaranteed to be free-able by free. This function has to wait for malloc

cgyurgyik marked an inline comment as done.Jun 23 2020, 9:08 AM
cgyurgyik added inline comments.
libc/src/string/strdup.cpp
18

Ok thanks. I'll close this for now then.

cgyurgyik abandoned this revision.Jun 23 2020, 9:08 AM

Malloc not available yet.

Add a redirector for malloc/free as a temporary solution?

Add a redirector for malloc/free as a temporary solution?

I just saw this. Can you clarify? I'm not sure how this would be done.

There was a redirector for the round function:
https://reviews.llvm.org/D69020

There was a redirector for the round function:
https://reviews.llvm.org/D69020

Thanks, @tschuett!
@cgyurgyik, That patch has an example of course. In case you decide to add redirectors for free and malloc, feel free to ask questions.