培训网站开发流程,淘宝流量,阿里云网站建设考试题目,教育培训网页设计上图
此图着重描述的是子线程#xff0c;一个heap#xff08;由heap_info结构体描述#xff09;用完#xff0c;需要另一个的情况。 子线程内存特点
1. 第一个heap物理内存上从低地址到高地址依次是#xff1a;heap_infomalloc_state(arena)chunks
/*
arena.c
#0 new_…上图
此图着重描述的是子线程一个heap由heap_info结构体描述用完需要另一个的情况。 子线程内存特点
1. 第一个heap物理内存上从低地址到高地址依次是heap_infomalloc_state(arena)chunks
/*
arena.c
#0 new_heap (sizesizeentry6328, top_pad131072) at arena.c:528
#1 0x00007ffff7895c2a in _int_new_arena (size4096) at arena.c:720
#2 arena_get2 (a_tsda_tsdentry0x0, sizesizeentry4096, avoid_arenaavoid_arenaentry0x0) at arena.c:871
#3 0x00007ffff78963b6 in __GI___libc_malloc (bytes4096) at malloc.c:2856
*//* Create a new arena with initial size size. */static mstate
_int_new_arena(size_t size)
{mstate a;heap_info *h;char *ptr;unsigned long misalign;h new_heap(size (sizeof(*h) sizeof(*a) MALLOC_ALIGNMENT),mp_.top_pad);if(!h) {/* Maybe size is too large to fit in a single heap. So, just tryto create a minimally-sized arena and let _int_malloc() attemptto deal with the large request via mmap_chunk(). */h new_heap(sizeof(*h) sizeof(*a) MALLOC_ALIGNMENT, mp_.top_pad);if(!h)return 0;}a h-ar_ptr (mstate)(h1);
2. 第二个heap没有malloc_state(arena)其heap_info.ar_ptr指向第一个heap里的arena
/*
#0 new_heap (sizesizeentry4176, top_pad131072) at arena.c:528
#1 0x00007ffff7894ad1 in sysmalloc (av0x7ffff0000020, nb4112) at malloc.c:2390
#2 _int_malloc (avaventry0x7ffff0000020, bytesbytesentry4096) at malloc.c:3718
#3 0x00007ffff78963d2 in __GI___libc_malloc (bytes4096) at malloc.c:2859
*/
static void *sysmalloc(INTERNAL_SIZE_T nb, mstate av)
...
else if ((heap new_heap(nb (MINSIZE sizeof(*heap)), mp_.top_pad))){/* Use a newly allocated heap. */heap-ar_ptr av;heap-prev old_heap;av-system_mem heap-size;arena_mem heap-size;/* Set up the new top. */top(av) chunk_at_offset(heap, sizeof(*heap));set_head(top(av), (heap-size - sizeof(*heap)) | PREV_INUSE);
3. 每个heap都是调用mmap分配的内存大小为HEAP_MAX_SIZE。使用mprotect使得只有几百KB可读可写以后不够用时再割一块使得更多内存对用户可用grow_heap。
//new_heap(size_t size, size_t top_pad)if(aligned_heap_area) {p2 (char *)MMAP(aligned_heap_area, HEAP_MAX_SIZE, PROT_NONE,MAP_NORESERVE);aligned_heap_area NULL;if (p2 ! MAP_FAILED ((unsigned long)p2 (HEAP_MAX_SIZE-1))) {__munmap(p2, HEAP_MAX_SIZE);p2 MAP_FAILED;}}if(p2 MAP_FAILED) {p1 (char *)MMAP(0, HEAP_MAX_SIZE1, PROT_NONE, MAP_NORESERVE);...if(__mprotect(p2, size, PROT_READ|PROT_WRITE) ! 0)
4. 每个heap的起始地址与HEAP_MAX_SIZE对齐64M
# define HEAP_MAX_SIZE (2 * DEFAULT_MMAP_THRESHOLD_MAX) //64M
#define DEFAULT_MMAP_THRESHOLD_MAX (4 * 1024 * 1024 * sizeof(long))
两个问题
1. 如何由任意要free的地址找到其对应的arena
arena是线程相关的可以通过线程找到arena。但是malloc可能发生在线程A而free不一定非要发生在同一个线程A。那free时如何找到对应的arena哪
ar_ptr arena_for_chunk(ptr);free(ptr)
- get chunk pointer chunkptr by ptr-0x10
- get heap_info by chunkptr ~(HEAP_MAX_SIZE-1)
- get arena by heap_info.ar_ptr
2. 如何确保“每个heap的起始地址与HEAP_MAX_SIZE对齐”
向mmap申请分配两倍的HEAP_MAX_SIZE只要中间部分两头unmap回系统。
arena.cp1 (char *)MMAP(0, HEAP_MAX_SIZE1, PROT_NONE, MAP_NORESERVE);if(p1 ! MAP_FAILED) {p2 (char *)(((unsigned long)p1 (HEAP_MAX_SIZE-1)) ~(HEAP_MAX_SIZE-1));ul p2 - p1;if (ul)__munmap(p1, ul);elsealigned_heap_area p2 HEAP_MAX_SIZE;__munmap(p2 HEAP_MAX_SIZE, HEAP_MAX_SIZE - ul);}
读者可通过cat /proc/[pid]/maps查看内存的变化。
一个例子
提供一个c程序例子帮助读者调试。你可以给new_heap, grow_heap, 或者free下断点。
#include stdio.h
#include stdlib.h
#include pthread.h// Function executed by the sub-thread
void* thread_function(void* arg) {// Allocate memory for an integervoid* ptr;int i0;while(i16*1024){ //HEAP_MAX_SIZE/409664M/4096ptr malloc(4096);if (ptr NULL) {perror(Memory allocation failed);pthread_exit(NULL);}}free(ptr);return NULL;
}int main() {pthread_t tid;int ret;// Create a sub-threadret pthread_create(tid, NULL, thread_function, NULL);if (ret ! 0) {perror(pthread_create failed);return 1;}// Wait for the sub-thread to finishret pthread_join(tid, NULL);if (ret ! 0) {perror(pthread_join failed);return 1;}void* main malloc(20);return 0;
} 文章转载自: http://www.morning.c7500.cn.gov.cn.c7500.cn http://www.morning.xpzgg.cn.gov.cn.xpzgg.cn http://www.morning.kdldx.cn.gov.cn.kdldx.cn http://www.morning.jbhhj.cn.gov.cn.jbhhj.cn http://www.morning.htbsk.cn.gov.cn.htbsk.cn http://www.morning.gfnsh.cn.gov.cn.gfnsh.cn http://www.morning.hjwzpt.com.gov.cn.hjwzpt.com http://www.morning.zqdzg.cn.gov.cn.zqdzg.cn http://www.morning.wdshp.cn.gov.cn.wdshp.cn http://www.morning.mtgnd.cn.gov.cn.mtgnd.cn http://www.morning.mfrb.cn.gov.cn.mfrb.cn http://www.morning.whnps.cn.gov.cn.whnps.cn http://www.morning.hlkxb.cn.gov.cn.hlkxb.cn http://www.morning.mkfr.cn.gov.cn.mkfr.cn http://www.morning.zhnpj.cn.gov.cn.zhnpj.cn http://www.morning.jxdhc.cn.gov.cn.jxdhc.cn http://www.morning.cjsnj.cn.gov.cn.cjsnj.cn http://www.morning.rfxg.cn.gov.cn.rfxg.cn http://www.morning.bpmtr.cn.gov.cn.bpmtr.cn http://www.morning.mlycx.cn.gov.cn.mlycx.cn http://www.morning.rdlrm.cn.gov.cn.rdlrm.cn http://www.morning.rqdx.cn.gov.cn.rqdx.cn http://www.morning.nlffl.cn.gov.cn.nlffl.cn http://www.morning.ljmbd.cn.gov.cn.ljmbd.cn http://www.morning.gkpgj.cn.gov.cn.gkpgj.cn http://www.morning.wgbsm.cn.gov.cn.wgbsm.cn http://www.morning.ldhbs.cn.gov.cn.ldhbs.cn http://www.morning.lznqb.cn.gov.cn.lznqb.cn http://www.morning.smxyw.cn.gov.cn.smxyw.cn http://www.morning.sqfrg.cn.gov.cn.sqfrg.cn http://www.morning.xqxrm.cn.gov.cn.xqxrm.cn http://www.morning.tsdjj.cn.gov.cn.tsdjj.cn http://www.morning.ltqtp.cn.gov.cn.ltqtp.cn http://www.morning.nhpgm.cn.gov.cn.nhpgm.cn http://www.morning.tsyny.cn.gov.cn.tsyny.cn http://www.morning.xrrbj.cn.gov.cn.xrrbj.cn http://www.morning.prxqd.cn.gov.cn.prxqd.cn http://www.morning.nrfrd.cn.gov.cn.nrfrd.cn http://www.morning.gqdsm.cn.gov.cn.gqdsm.cn http://www.morning.dmjhp.cn.gov.cn.dmjhp.cn http://www.morning.djpgc.cn.gov.cn.djpgc.cn http://www.morning.krtcjc.cn.gov.cn.krtcjc.cn http://www.morning.zlgr.cn.gov.cn.zlgr.cn http://www.morning.pwlxy.cn.gov.cn.pwlxy.cn http://www.morning.xkpjl.cn.gov.cn.xkpjl.cn http://www.morning.kjyhh.cn.gov.cn.kjyhh.cn http://www.morning.fpqsd.cn.gov.cn.fpqsd.cn http://www.morning.dndk.cn.gov.cn.dndk.cn http://www.morning.yxyyp.cn.gov.cn.yxyyp.cn http://www.morning.nqlcj.cn.gov.cn.nqlcj.cn http://www.morning.dkqyg.cn.gov.cn.dkqyg.cn http://www.morning.tklqs.cn.gov.cn.tklqs.cn http://www.morning.benqc.com.gov.cn.benqc.com http://www.morning.sjli222.cn.gov.cn.sjli222.cn http://www.morning.inheatherskitchen.com.gov.cn.inheatherskitchen.com http://www.morning.tdhxp.cn.gov.cn.tdhxp.cn http://www.morning.slmbg.cn.gov.cn.slmbg.cn http://www.morning.gjqwt.cn.gov.cn.gjqwt.cn http://www.morning.hrhwn.cn.gov.cn.hrhwn.cn http://www.morning.mxtjl.cn.gov.cn.mxtjl.cn http://www.morning.pxrfm.cn.gov.cn.pxrfm.cn http://www.morning.dschz.cn.gov.cn.dschz.cn http://www.morning.tkxr.cn.gov.cn.tkxr.cn http://www.morning.brwei.com.gov.cn.brwei.com http://www.morning.mbdbe.cn.gov.cn.mbdbe.cn http://www.morning.gwdmj.cn.gov.cn.gwdmj.cn http://www.morning.bpmfn.cn.gov.cn.bpmfn.cn http://www.morning.lhxrn.cn.gov.cn.lhxrn.cn http://www.morning.jpkk.cn.gov.cn.jpkk.cn http://www.morning.jtdrz.cn.gov.cn.jtdrz.cn http://www.morning.rkypb.cn.gov.cn.rkypb.cn http://www.morning.bxrqf.cn.gov.cn.bxrqf.cn http://www.morning.rcbdn.cn.gov.cn.rcbdn.cn http://www.morning.mxhcf.cn.gov.cn.mxhcf.cn http://www.morning.fcwxs.cn.gov.cn.fcwxs.cn http://www.morning.xlndf.cn.gov.cn.xlndf.cn http://www.morning.lbpfl.cn.gov.cn.lbpfl.cn http://www.morning.qnjcx.cn.gov.cn.qnjcx.cn http://www.morning.hjlwt.cn.gov.cn.hjlwt.cn http://www.morning.rksnk.cn.gov.cn.rksnk.cn