Reland "Delete STACK_ARRAY macro, and use of alloca"
This is a reland of 74b373f04a69b279e45b0792d86c594cb33d22c1
Original change's description:
> Delete STACK_ARRAY macro, and use of alloca
>
> Refactor the few uses of STACK_ARRAY to avoid an extra copy
> on the stack.
>
> Bug: webrtc:6424
> Change-Id: I5c8f3c0381523db0ead31207d949df9a286c76ba
> Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/137806
> Commit-Queue: Niels Moller <nisse@webrtc.org>
> Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
> Cr-Commit-Position: refs/heads/master@{#28038}
Bug: webrtc:6424
Change-Id: Id635ccdfae12157cbb3ab9089c5e4a9f77f742ce
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/138211
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Reviewed-by: Henrik Boström <hbos@webrtc.org>
Commit-Queue: Niels Moller <nisse@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#28052}
diff --git a/rtc_base/string_utils.h b/rtc_base/string_utils.h
index bc33284..f8ad126 100644
--- a/rtc_base/string_utils.h
+++ b/rtc_base/string_utils.h
@@ -21,31 +21,15 @@
#include <wchar.h>
#include <windows.h>
-#define alloca _alloca
#endif // WEBRTC_WIN
#if defined(WEBRTC_POSIX)
-#ifdef BSD
#include <stdlib.h>
-#else // BSD
-#include <alloca.h>
-#endif // !BSD
#include <strings.h>
#endif // WEBRTC_POSIX
#include <string>
-///////////////////////////////////////////////////////////////////////////////
-// Generic string/memory utilities
-///////////////////////////////////////////////////////////////////////////////
-
-#define STACK_ARRAY(TYPE, LEN) \
- static_cast<TYPE*>(::alloca((LEN) * sizeof(TYPE)))
-
-///////////////////////////////////////////////////////////////////////////////
-// Traits simplifies porting string functions to be CTYPE-agnostic
-///////////////////////////////////////////////////////////////////////////////
-
namespace rtc {
const size_t SIZE_UNKNOWN = static_cast<size_t>(-1);
@@ -65,9 +49,10 @@
inline std::wstring ToUtf16(const char* utf8, size_t len) {
int len16 = ::MultiByteToWideChar(CP_UTF8, 0, utf8, static_cast<int>(len),
nullptr, 0);
- wchar_t* ws = STACK_ARRAY(wchar_t, len16);
- ::MultiByteToWideChar(CP_UTF8, 0, utf8, static_cast<int>(len), ws, len16);
- return std::wstring(ws, len16);
+ std::wstring ws(len16, 0);
+ ::MultiByteToWideChar(CP_UTF8, 0, utf8, static_cast<int>(len), &*ws.begin(),
+ len16);
+ return ws;
}
inline std::wstring ToUtf16(const std::string& str) {
@@ -77,10 +62,10 @@
inline std::string ToUtf8(const wchar_t* wide, size_t len) {
int len8 = ::WideCharToMultiByte(CP_UTF8, 0, wide, static_cast<int>(len),
nullptr, 0, nullptr, nullptr);
- char* ns = STACK_ARRAY(char, len8);
- ::WideCharToMultiByte(CP_UTF8, 0, wide, static_cast<int>(len), ns, len8,
- nullptr, nullptr);
- return std::string(ns, len8);
+ std::string ns(len8, 0);
+ ::WideCharToMultiByte(CP_UTF8, 0, wide, static_cast<int>(len), &*ns.begin(),
+ len8, nullptr, nullptr);
+ return ns;
}
inline std::string ToUtf8(const wchar_t* wide) {