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 ...
随机推荐
- Crusher Django 学习笔记4 使用Model
http://crusher-milling.blogspot.com/2013/09/crusher-django-tutorial4-using-basic.html 顺便学习一下FQ Crush ...
- table的边框线的设置
http://hi.baidu.com/weisuotang/item/a1d98ec298c0aa49a8ba9447 http://www.cnblogs.com/xinlei/archive/2 ...
- 【ajax跨域】原因原理解决
1.安全,跨域cookie iframe 2.很简单,就是利用<script>标签没有跨域限制的“漏洞”(历史遗迹啊)来达到与第三方通讯的目的.当需要通讯时,本站脚本创建一个<scr ...
- Ubuntu 14.04安装Chromium浏览器并添加Flash插件Pepper Flash Player
安装方法Ubuntu 14.04及衍生版本用户命令: 因为默认库里面有Chromium和Pepper Flash Player,安装非常容易,打开终端,输入以下命令: sudo apt-get upd ...
- java移位操作符
<<:左移操作符,右边补0,相当于乘二乘二... >>:右移操作符,左边补符号位(正数补0,负数补1),相当于除二除二... >>>:无符号右移,左边补0,相 ...
- RUP(Rational Unified Process)
RUP Rational Unified Process 目前阶段在学习UML,怎么会写RUP呢?学习UML是为了更好的把系统搭建好,RUP也是一样,为系统服务! 软件危机 美国国家总审计局,在198 ...
- 微软职位内部推荐-Senior Speech TTS
微软近期Open的职位: Job Description: Responsibilities Do you want to change the way the world interacts wit ...
- Another 20 Docs and Guides for Front-End Developers
http://www.sitepoint.com/another-20-docs-guides-front-end-developers/?utm_medium=email&utm_campa ...
- 生鲜电商的O2O之道
- HDU 1423 Greatest Common Increasing Subsequence LCIS
题目链接: 题目 Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...