Commit 4f05a25a authored by tzik's avatar tzik Committed by Commit Bot

Take program counter for FROM_HERE in its static constructor

Before this CL, the FROM_HERE macro directly called GetProgramCounter()
and passed the value to the Location constructor. However, that costs
a few bytes for each FROM_HERE invocation.

This CL moves the program counter calculation into a static constructor
of Location for smaller binary size.

This reduces the stripped binary size on x86_64 Linux by 36864 bytes.

Change-Id: Ic49550abb4bf1e4e29756c7fc876ddfd85b5d3b2
Reviewed-on: https://chromium-review.googlesource.com/662086
Commit-Queue: Taiju Tsuiki <tzik@chromium.org>
Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Cr-Commit-Position: refs/heads/master@{#501475}
parent 7d04c9ed
...@@ -2,15 +2,16 @@ ...@@ -2,15 +2,16 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "build/build_config.h" #include "base/location.h"
#if defined(COMPILER_MSVC) #if defined(COMPILER_MSVC)
#include <intrin.h> #include <intrin.h>
#endif #endif
#include "base/location.h" #include "base/compiler_specific.h"
#include "base/strings/string_number_conversions.h" #include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h" #include "base/strings/stringprintf.h"
#include "build/build_config.h"
namespace base { namespace base {
...@@ -77,18 +78,30 @@ LocationSnapshot::LocationSnapshot(const Location& location) ...@@ -77,18 +78,30 @@ LocationSnapshot::LocationSnapshot(const Location& location)
LocationSnapshot::~LocationSnapshot() = default; LocationSnapshot::~LocationSnapshot() = default;
//------------------------------------------------------------------------------
#if defined(COMPILER_MSVC)
__declspec(noinline)
#endif
BASE_EXPORT const void* GetProgramCounter() {
#if defined(COMPILER_MSVC) #if defined(COMPILER_MSVC)
return _ReturnAddress(); #define RETURN_ADDRESS() _ReturnAddress()
#elif defined(COMPILER_GCC) && !defined(OS_NACL) #elif defined(COMPILER_GCC) && !defined(OS_NACL)
return __builtin_extract_return_addr(__builtin_return_address(0)); #define RETURN_ADDRESS() \
__builtin_extract_return_addr(__builtin_return_address(0))
#else #else
return nullptr; #define RETURN_ADDRESS() nullptr
#endif #endif
// static
NOINLINE Location Location::CreateFromHere(const char* file_name) {
return Location(file_name, RETURN_ADDRESS());
}
// static
NOINLINE Location Location::CreateFromHere(const char* function_name,
const char* file_name,
int line_number) {
return Location(function_name, file_name, line_number, RETURN_ADDRESS());
}
//------------------------------------------------------------------------------
NOINLINE const void* GetProgramCounter() {
return RETURN_ADDRESS();
} }
} // namespace base } // namespace base
...@@ -77,6 +77,11 @@ class BASE_EXPORT Location { ...@@ -77,6 +77,11 @@ class BASE_EXPORT Location {
void Write(bool display_filename, bool display_function_name, void Write(bool display_filename, bool display_function_name,
std::string* output) const; std::string* output) const;
static Location CreateFromHere(const char* file_name);
static Location CreateFromHere(const char* function_name,
const char* file_name,
int line_number);
private: private:
const char* function_name_ = nullptr; const char* function_name_ = nullptr;
const char* file_name_ = nullptr; const char* file_name_ = nullptr;
...@@ -105,15 +110,14 @@ BASE_EXPORT const void* GetProgramCounter(); ...@@ -105,15 +110,14 @@ BASE_EXPORT const void* GetProgramCounter();
// Full source information should be included. // Full source information should be included.
#define FROM_HERE FROM_HERE_WITH_EXPLICIT_FUNCTION(__func__) #define FROM_HERE FROM_HERE_WITH_EXPLICIT_FUNCTION(__func__)
#define FROM_HERE_WITH_EXPLICIT_FUNCTION(function_name) \ #define FROM_HERE_WITH_EXPLICIT_FUNCTION(function_name) \
::base::Location(function_name, __FILE__, __LINE__, \ ::base::Location::CreateFromHere(function_name, __FILE__, __LINE__)
::base::GetProgramCounter())
#else #else
// TODO(http://crbug.com/760702) remove the __FILE__ argument from these calls. // TODO(http://crbug.com/760702) remove the __FILE__ argument from these calls.
#define FROM_HERE ::base::Location(__FILE__, ::base::GetProgramCounter()) #define FROM_HERE ::base::Location::CreateFromHere(__FILE__)
#define FROM_HERE_WITH_EXPLICIT_FUNCTION(function_name) \ #define FROM_HERE_WITH_EXPLICIT_FUNCTION(function_name) \
::base::Location(function_name, __FILE__, -1, ::base::GetProgramCounter()) ::base::Location::CreateFromHere(function_name, __FILE__, -1)
#endif #endif
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment