chrome中tcmalloc的使用
chrome中内存分配采用了第三方库tcmalloc,这个库主要提供给应用程序内存管理方面的优化,按资料说内存存取速度会从300ns降到50ns。更具体的关于这个tcmalloc的信息大家可以查网上的资料看看, 本人对tcmalloc的实现不是很熟,这里主要向熟悉一下chrome的代码,主要说说 tcmalloc如何集成到了chrome中,通过研究这个,一是看看如何使用tcmalloc,另外可以对window下VC的内存C runtime的库有更多的了解。
tcmalloc主要提供了自有的一套内存分配的函数,来替换我们常使用的new delete等VC++默认提供的内存分配实现。
chrome中的tcmalloc的源码位于third_party下,源代码通过GYP引入到chrome中的allocator项目中,我们重点可以看下 allocator_shim.cc中的代码。该文件中对外提供了内存分配的malloc, free和reallocator等函数,用来接管系统默认提供的内存非配函数。
extern "C" {
void* malloc(size_t size) __THROW {
void* ptr;
for (;;) {
#ifdef ENABLE_DYNAMIC_ALLOCATOR_SWITCHING
switch (allocator) {
case JEMALLOC:
ptr = je_malloc(size);
break;
case WINHEAP:
case WINLFH:
ptr = win_heap_malloc(size);
break;
case TCMALLOC:
default:
ptr = do_malloc(size);
break;
}
#else
// TCMalloc case.
ptr = do_malloc(size);
#endif
if (ptr)
return ptr;
if (!new_mode || !call_new_handler(true))
break;
}
return ptr;
}
void free(void* p) __THROW {
#ifdef ENABLE_DYNAMIC_ALLOCATOR_SWITCHING
switch (allocator) {
case JEMALLOC:
je_free(p);
return;
case WINHEAP:
case WINLFH:
win_heap_free(p);
return;
}
#endif
// TCMalloc case.
do_free(p);
}
void* realloc(void* ptr, size_t size) __THROW {
// Webkit is brittle for allocators that return NULL for malloc(0). The
// realloc(0, 0) code path does not guarantee a non-NULL return, so be sure
// to call malloc for this case.
if (!ptr)
return malloc(size);
void* new_ptr;
for (;;) {
#ifdef ENABLE_DYNAMIC_ALLOCATOR_SWITCHING
switch (allocator) {
case JEMALLOC:
new_ptr = je_realloc(ptr, size);
break;
case WINHEAP:
case WINLFH:
new_ptr = win_heap_realloc(ptr, size);
break;
case TCMALLOC:
default:
new_ptr = do_realloc(ptr, size);
break;
}
#else
// TCMalloc case.
new_ptr = do_realloc(ptr, size);
#endif
// Subtle warning: NULL return does not alwas indicate out-of-memory. If
// the requested new size is zero, realloc should free the ptr and return
// NULL.
if (new_ptr || !size)
return new_ptr;
if (!new_mode || !call_new_handler(true))
break;
}
return new_ptr;
}
通过这三个函数的代码我们可以看出,chrome提供了进行内存库切换的枚举, 不同的枚举值对应不同的具体malloc的实现转发,具体由JEMalloc, tcmalloc和默认等几种不同的内存库可以使用。
按照我们的经验,如果直接编译的话,那么就会有两份的malloc的实现,此时链接是不会通过的,chrome中如何解决?
chrome下的base中allocator相关的项目有4个,其中有一个名为libcmt的项目,其中只有一个python的脚本, 代码如下:
#!/usr/bin/env python # Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#
# This script takes libcmt.lib for VS2005/08/10/12/13 and removes the allocation
# related functions from it.
#
# Usage: prep_libc.py <VCLibDir> <OutputDir> <arch>
#
# VCLibDir is the path where VC is installed, something like:
# C:\Program Files\Microsoft Visual Studio 8\VC\lib
# OutputDir is the directory where the modified libcmt file should be stored.
# arch is either 'ia32' or 'x64' import os
import shutil
import subprocess
import sys def run(command, filter=None):
"""Run |command|, removing any lines that match |filter|. The filter is
to remove the echoing of input filename that 'lib' does."""
popen = subprocess.Popen(
command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
out, _ = popen.communicate()
for line in out.splitlines():
if filter and line.strip() != filter:
print line
return popen.returncode def main():
bindir = 'SELF_X86'
objdir = 'INTEL'
vs_install_dir = sys.argv[1]
outdir = sys.argv[2]
if "x64" in sys.argv[3]:
bindir = 'SELF_64_amd64'
objdir = 'amd64'
vs_install_dir = os.path.join(vs_install_dir, 'amd64')
output_lib = os.path.join(outdir, 'libcmt.lib')
shutil.copyfile(os.path.join(vs_install_dir, 'libcmt.lib'), output_lib)
shutil.copyfile(os.path.join(vs_install_dir, 'libcmt.pdb'),
os.path.join(outdir, 'libcmt.pdb')) vspaths = [
'build\\intel\\mt_obj\\',
'f:\\dd\\vctools\\crt_bld\\' + bindir + \
'\\crt\\src\\build\\' + objdir + '\\mt_obj\\',
'F:\\dd\\vctools\\crt_bld\\' + bindir + \
'\\crt\\src\\build\\' + objdir + '\\mt_obj\\nativec\\\\',
'F:\\dd\\vctools\\crt_bld\\' + bindir + \
'\\crt\\src\\build\\' + objdir + '\\mt_obj\\nativecpp\\\\',
'f:\\binaries\\Intermediate\\vctools\\crt_bld\\' + bindir + \
'\\crt\\prebuild\\build\\INTEL\\mt_obj\\cpp_obj\\\\',
] objfiles = ['malloc', 'free', 'realloc', 'new', 'delete', 'new2', 'delete2',
'align', 'msize', 'heapinit', 'expand', 'heapchk', 'heapwalk',
'heapmin', 'sbheap', 'calloc', 'recalloc', 'calloc_impl',
'new_mode', 'newopnt', 'newaopnt']
for obj in objfiles:
for vspath in vspaths:
cmd = ('lib /nologo /ignore:4006,4014,4221 /remove:%s%s.obj %s' %
(vspath, obj, output_lib))
run(cmd, obj + '.obj') if __name__ == "__main__":
sys.exit(main())
这段代码主要 是从libcmt.lib中移出 malloc, free, new 等 的obj的默认实现, 就可以使用到我们所提供的malloc了。
chrome中tcmalloc的使用的更多相关文章
- chrome中不可见字符引发的float问题
起因是刷知乎时碰到这么个问题:https://www.zhihu.com/question/41400503 问题代码如下: <!DOCTYPE html> <html lang=& ...
- Chrome 中的彩蛋,一款小游戏,你知道吗?
今天看到一篇文章,介绍chrome中的彩蛋,带着好奇心进去看了一眼,没想到发现了一款小游戏,个人觉得还不错,偶尔可以玩一下,放松放松心情!^_^ 当 Chrome 无法连接到互联网时, 或者上着网突然 ...
- 在 Chrome 中调试 Android 浏览器
最近需要使用 Chrome Developer Tools 调试 Android 浏览器,但是官方指南并不是很好使,经过一番折腾,终于调试成功了,在此把经验分享给需要的朋友. Chrome Devel ...
- firefox与chrome中对select下拉框中的option支持问题
firefox可以直接修改option的字体样式,但是chrome中option的字体样式是继承select的,这个是在项目中遇到的,具体的可以看一下 http://www.cnblogs.com/r ...
- Chrome中的Device模块调式响应性设计
Chrome中的Device模块调式响应性设计 阅读目录 启用Device模块 Device模块设置介绍 自定义预设介绍 查看media queries 触发触摸事件 回到顶部 启用Device模块 ...
- 在桌面chrome中调试android设备中的web页面
准备工作 1, 桌面版chrome 2, Android设备(安装有chrome浏览器) 3, Android-sdk Android-sdk安装及设置 SKD安装 从http://developer ...
- Ajax请求在IE和Google Chrome中可以响应,在Firefox中无法响应
在工作中碰到这么一个问题,发送ajax请求,在IE和chrome中可以正常的响应,但是在Firefox中无法响应,代码如下: JS代码: function Sure(obj) { var statu ...
- Google Chrome中的高性能网络 (三)
使用预连接优化了TCP连接管理 已经预解析到了主机名,也有了由OmniBox和Chrome Predictor提供信号,预示着用户未来的操作.为什么再进一步连接到目标主机,在用户真正发起请求前完成TC ...
- Google Chrome中的高性能网络(二)
Chrome Predictor的预测功能优化 Chrome会随着使用变得更快. 它这个特性是通过一个单例对象Predictor来实现的.这个对象在浏览器内核进程(Browser Kernel Pro ...
随机推荐
- js关闭页面(兼容浏览器)
function closewindow() { window.opener = null; window.open("", "_self"); window. ...
- Eclipse下jad反编译之“类文件查看器”不能处理给定的输入错误解决
Eclipse中的插件下载,安装和配置可以参考我的另一篇文章:MyEclipse反编译Class文件 下面重点讲解如何使用jad反编译 1.在DOS窗口中,到class所在目录,直接运行 >ja ...
- .NET4.5中WCF中默认生成的basicHttpsBinding的研究
起因: 使用.net4.5建立了一个空白的WCF服务.默认使用的绑定配置是basicHttpsBinding. 问题发现: 1.用客户端进行服务引用,生成了默认的配置文件,其中绑定配置是basicHt ...
- matlab实现判断是否能否生成严格对角占优矩阵
如题: function X = IsStrictDiagMatrix(A) % input: A matrix % output: The matrix after transformation % ...
- startDiscovery() and startLeScan().
You have to start a scan for Classic Bluetooth devices with startDiscovery() and a scan for Bluetoot ...
- 阿里云之OSS 开放存储服务开发笔记
在使用云服务以后,你不用考虑他是否能承受压力,而是费用.不要考虑是否被攻击,而是他的API实现.本人开发阿里云服务也走了些崎岖之路,写下以备忘之. 阿里云的开放存储服务可以提供文件的存储服务,开放了上 ...
- 2014 Multi-University Training Contest 10
官方解题报告:http://blog.sina.com.cn/s/blog_6bddecdc0102v01l.html A simple brute force problem. http://acm ...
- 基于HOOK和MMF的Windows密码渗透技术
随着计算机与网络的普及,信息安全越来越成为人们所普遍关心的大事.密码的渗透与反渗透在此领域表现的愈演愈烈.本文深入分析了各个版本Windows密码的特点,尤其是针对windws2K/XP安全性提高的情 ...
- 【Asp.net MVC ---杂七杂八】
@RenderSection 母模板:_mainLayout.cshtml <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitio ...
- uva 11181
直接枚举计算就行: #include<cstdio> #include<cstring> #include<algorithm> #define maxn 22 u ...