mirror of
https://github.com/mollyim/webrtc.git
synced 2025-05-13 05:40:42 +01:00
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}
This commit is contained in:
parent
eb180f8f77
commit
74b373f04a
2 changed files with 24 additions and 48 deletions
|
@ -14,7 +14,6 @@
|
||||||
|
|
||||||
#include "rtc_base/arraysize.h"
|
#include "rtc_base/arraysize.h"
|
||||||
#include "rtc_base/checks.h"
|
#include "rtc_base/checks.h"
|
||||||
#include "rtc_base/string_utils.h"
|
|
||||||
|
|
||||||
namespace rtc {
|
namespace rtc {
|
||||||
|
|
||||||
|
@ -45,25 +44,22 @@ bool hex_decode(char ch, unsigned char* val) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// hex_encode, but separate each byte representation with a delimiter.
|
size_t hex_encode_output_length(size_t srclen, char delimiter) {
|
||||||
// |delimiter| == 0 means no delimiter
|
return delimiter && srclen > 0 ? (srclen * 3 - 1) : (srclen * 2);
|
||||||
// If the buffer is too short, we return 0
|
}
|
||||||
size_t hex_encode_with_delimiter(char* buffer,
|
|
||||||
size_t buflen,
|
// hex_encode shows the hex representation of binary data in ascii, with
|
||||||
|
// |delimiter| between bytes, or none if |delimiter| == 0.
|
||||||
|
void hex_encode_with_delimiter(char* buffer,
|
||||||
const char* csource,
|
const char* csource,
|
||||||
size_t srclen,
|
size_t srclen,
|
||||||
char delimiter) {
|
char delimiter) {
|
||||||
RTC_DCHECK(buffer); // TODO(kwiberg): estimate output size
|
RTC_DCHECK(buffer);
|
||||||
if (buflen == 0)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
// Init and check bounds.
|
// Init and check bounds.
|
||||||
const unsigned char* bsource =
|
const unsigned char* bsource =
|
||||||
reinterpret_cast<const unsigned char*>(csource);
|
reinterpret_cast<const unsigned char*>(csource);
|
||||||
size_t srcpos = 0, bufpos = 0;
|
size_t srcpos = 0, bufpos = 0;
|
||||||
size_t needed = delimiter ? (srclen * 3) : (srclen * 2 + 1);
|
|
||||||
if (buflen < needed)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
while (srcpos < srclen) {
|
while (srcpos < srclen) {
|
||||||
unsigned char ch = bsource[srcpos++];
|
unsigned char ch = bsource[srcpos++];
|
||||||
|
@ -77,10 +73,6 @@ size_t hex_encode_with_delimiter(char* buffer,
|
||||||
++bufpos;
|
++bufpos;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Null terminate.
|
|
||||||
buffer[bufpos] = '\0';
|
|
||||||
return bufpos;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
@ -96,12 +88,11 @@ std::string hex_encode(const char* source, size_t srclen) {
|
||||||
std::string hex_encode_with_delimiter(const char* source,
|
std::string hex_encode_with_delimiter(const char* source,
|
||||||
size_t srclen,
|
size_t srclen,
|
||||||
char delimiter) {
|
char delimiter) {
|
||||||
const size_t kBufferSize = srclen * 3;
|
std::string s(hex_encode_output_length(srclen, delimiter), 0);
|
||||||
char* buffer = STACK_ARRAY(char, kBufferSize);
|
// TODO(nisse): When we can use C++17, switch the below hack with begin to
|
||||||
size_t length =
|
// just s.data().
|
||||||
hex_encode_with_delimiter(buffer, kBufferSize, source, srclen, delimiter);
|
hex_encode_with_delimiter(&*s.begin(), source, srclen, delimiter);
|
||||||
RTC_DCHECK(srclen == 0 || length > 0);
|
return s;
|
||||||
return std::string(buffer, length);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t hex_decode(char* cbuffer,
|
size_t hex_decode(char* cbuffer,
|
||||||
|
|
|
@ -21,31 +21,15 @@
|
||||||
#include <wchar.h>
|
#include <wchar.h>
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
|
|
||||||
#define alloca _alloca
|
|
||||||
#endif // WEBRTC_WIN
|
#endif // WEBRTC_WIN
|
||||||
|
|
||||||
#if defined(WEBRTC_POSIX)
|
#if defined(WEBRTC_POSIX)
|
||||||
#ifdef BSD
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#else // BSD
|
|
||||||
#include <alloca.h>
|
|
||||||
#endif // !BSD
|
|
||||||
#include <strings.h>
|
#include <strings.h>
|
||||||
#endif // WEBRTC_POSIX
|
#endif // WEBRTC_POSIX
|
||||||
|
|
||||||
#include <string>
|
#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 {
|
namespace rtc {
|
||||||
|
|
||||||
const size_t SIZE_UNKNOWN = static_cast<size_t>(-1);
|
const size_t SIZE_UNKNOWN = static_cast<size_t>(-1);
|
||||||
|
@ -65,9 +49,10 @@ size_t strcpyn(char* buffer,
|
||||||
inline std::wstring ToUtf16(const char* utf8, size_t len) {
|
inline std::wstring ToUtf16(const char* utf8, size_t len) {
|
||||||
int len16 = ::MultiByteToWideChar(CP_UTF8, 0, utf8, static_cast<int>(len),
|
int len16 = ::MultiByteToWideChar(CP_UTF8, 0, utf8, static_cast<int>(len),
|
||||||
nullptr, 0);
|
nullptr, 0);
|
||||||
wchar_t* ws = STACK_ARRAY(wchar_t, len16);
|
std::wstring ws(len16, 0);
|
||||||
::MultiByteToWideChar(CP_UTF8, 0, utf8, static_cast<int>(len), ws, len16);
|
::MultiByteToWideChar(CP_UTF8, 0, utf8, static_cast<int>(len), &*ws.begin(),
|
||||||
return std::wstring(ws, len16);
|
ws.size());
|
||||||
|
return ws;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline std::wstring ToUtf16(const std::string& str) {
|
inline std::wstring ToUtf16(const std::string& str) {
|
||||||
|
@ -77,10 +62,10 @@ inline std::wstring ToUtf16(const std::string& str) {
|
||||||
inline std::string ToUtf8(const wchar_t* wide, size_t len) {
|
inline std::string ToUtf8(const wchar_t* wide, size_t len) {
|
||||||
int len8 = ::WideCharToMultiByte(CP_UTF8, 0, wide, static_cast<int>(len),
|
int len8 = ::WideCharToMultiByte(CP_UTF8, 0, wide, static_cast<int>(len),
|
||||||
nullptr, 0, nullptr, nullptr);
|
nullptr, 0, nullptr, nullptr);
|
||||||
char* ns = STACK_ARRAY(char, len8);
|
std::string ns(len8, 0);
|
||||||
::WideCharToMultiByte(CP_UTF8, 0, wide, static_cast<int>(len), ns, len8,
|
::WideCharToMultiByte(CP_UTF8, 0, wide, static_cast<int>(len), &*ns.begin(),
|
||||||
nullptr, nullptr);
|
ns.size(), nullptr, nullptr);
|
||||||
return std::string(ns, len8);
|
return ns;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline std::string ToUtf8(const wchar_t* wide) {
|
inline std::string ToUtf8(const wchar_t* wide) {
|
||||||
|
|
Loading…
Reference in a new issue