diff options
author | Steven Moreland <smoreland@google.com> | 2021-03-29 17:28:48 +0000 |
---|---|---|
committer | Steven Moreland <smoreland@google.com> | 2021-03-29 17:49:15 +0000 |
commit | e5b0df28eb4179c1971ec143a13146a50a895e58 (patch) | |
tree | 119bfbdd0cda0f024a21ace295c9f1d153f80a0a /base/HidlSupport.cpp | |
parent | 70260b5a12859fa81c5745ada5a65eceb3782ad1 (diff) | |
download | libhidl-e5b0df28eb4179c1971ec143a13146a50a895e58.tar.gz |
hidl_string: empty string opt always applies
Before, it only applied for the default constructor. However, there are
many cases where we can avoid the extra allocation. The most important
is when we allocate a hidl_memory object. Here, the empty string literal
is passed to hidl_string, but we don't actually need an allocation.
Bug: 179720143
Test: N/A
Change-Id: I307305b88e8b8c54cb2e2759b60b7015a3ac82b9
Diffstat (limited to 'base/HidlSupport.cpp')
-rw-r--r-- | base/HidlSupport.cpp | 8 |
1 files changed, 8 insertions, 0 deletions
diff --git a/base/HidlSupport.cpp b/base/HidlSupport.cpp index af805b9..78faa2f 100644 --- a/base/HidlSupport.cpp +++ b/base/HidlSupport.cpp @@ -217,6 +217,14 @@ void hidl_string::copyFrom(const char *data, size_t size) { if (size >= UINT32_MAX) { LOG(FATAL) << "string size can't exceed 2^32 bytes: " << size; } + + if (size == 0) { + mBuffer = kEmptyString; + mSize = 0; + mOwnsBuffer = false; + return; + } + char *buf = (char *)malloc(size + 1); memcpy(buf, data, size); buf[size] = '\0'; |