亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 學院 > 開發設計 > 正文

The Linux Programming Interface 筆記之malloc和free的實現

2019-11-14 09:40:25
字體:
來源:轉載
供稿:網友
        Although malloc()andfree() PRovide an interface for allocating memory that ismuch easier to use than brk() and sbrk(), it is still possible to make various programmingerrors when using them. Understanding how malloc() and free()are implemented

provides us with insights into the causes of these errors and how we can avoid them.

       The implementation of malloc()is straightforward. It first scans the list of memoryblocks previously released by free() in order to find one whose size is larger thanor equal to its requirements. (Different strategies may be employed for this scan,depending on the implementation; for example, first-fit or best-fit.) If the block isexactly the right size, then it is returned to the caller. If it is larger, then it is split, sothat a block of the correct size is returned to the caller and a smaller free block is

left on the free list.

       If no block on the free list is large enough, then malloc() callssbrk() to allocatemore memory. To reduce the number of calls to sbrk(), rather than allocatingexactly the number of bytes required, malloc() increases the program break inlarger units (some multiple of the virtual memory page size), putting the excessmemory onto the free list.

       Looking at the implementation of free(), things start to become more interesting.When free() places a block of memory onto the free list, how does it know whatsize that block is? This is done via a trick. When malloc() allocates the block, itallocates extra bytes to hold an integer containing the size of the block. This integer islocated at the beginning of the block; the address actually returned to the callerpoints to the location just past this length value, as shown in Figure 7-1.

        When a block is placed on the (doubly linked) free list,free()uses the bytes of theblock itself in order to add the block to the list, as shown in Figure 7-2.

        As blocks are deallocated and reallocated over time, the blocks of the free list willbecome intermingled with blocks of allocated, in-use memory, as shown in Figure 7-3.

        Now consider the fact that C allows us to create pointers to any location in theheap, and modify the locations they point to, including the length,previous free block,and next free block pointers maintained by free() and malloc(). Add this to the precedingdescription, and we have a fairly combustible mix when it comes to creatingobscure programming bugs. For example, if, via a misdirected pointer, we accidentallyincrease one of the length values preceding an allocated block of memory, andsubsequently deallocate that block, then free()will record the wrong size block ofmemory on the free list. Subsequently, malloc() may reallocate this block, leading toa scenario where the program has pointers to two blocks of allocated memory thatit understands to be distinct, but which actually overlap. Numerous other picturesof what could go wrong can be drawn.         To avoid these types of errors, we should observe the following rules:> After we allocate a block of memory, we should be careful not to touch anybytes outside the range of that block. This could occur, for example, as a resultof faulty pointer arithmetic or off-by-one errors in loops updating the contents ofa block.> It is an error to free the same piece of allocated memory more than once. Withglibc on linux, we often get a segmentation violation (SIGSEGV signal). This isgood, because it alerts us that we’ve made a programming error. However,more generally, freeing the same memory twice leads to unpredictable behavior.> We should never call free() with a pointer value that wasn’t obtained by a call toone of the functions in the malloc package.> If we are writing a long-running program (e.g., a shell or a network daemonprocess) that repeatedly allocates memory for various purposes, then weshould ensure that we deallocate any memory after we have finished using it.Failure to do so means that the heap will steadily grow until we reach the limitsof available virtual memory, at which point further attempts to allocate memory fail. Such a condition is known as amemory leak.

Tools and libraries for malloc debugging

        Failure to observe the rules listed above can lead to the creation of bugs that areobscure and difficult to reproduce. The task of finding such bugs can be easedconsiderably by using the malloc debugging tools provided by glibc or one of a numberof malloc debugging libraries that are designed for this purpose.

        Among the malloc debugging tools provided by glibc are the following:

> The mtrace() and muntrace() functions allow a program to turn tracing of memoryallocation calls on and off. These functions are used in conjunction withthe MALLOC_TRACE environment variable, which should be defined to contain thename of a file to which tracing information should be written. When mtrace() iscalled, it checks to see whether this file is defined and can be opened for writing; if so,then all calls to functions in the malloc package are traced andrecorded in the file. Since the resulting file is not easily human-readable, ascript—also called mtrace—is provided to analyze the file and produce a readablesummary. For security reasons, calls to mtrace()are ignored by set-user-ID andset-group-ID programs.

> The mcheck() and mprobe() functions allow a program to perform consistencychecks on blocks of allocated memory; for example, catching errors such asattempting to write to a location past the end of a block of allocated memory.These functions provide functionality that somewhat overlaps with the mallocdebugging libraries described below. Programs that employ these functionsmust be linked with the mcheck library using the cc -lmcheck option.

> The MALLOC_CHECK_ environment variable (note the trailing underscore) serves asimilar purpose to mcheck() and mprobe(). (One notable difference between thetwo techniques is that using MALLOC_CHECK_ doesn’t require modification andrecompilation of the program.) By setting this variable to different integer values,we can control how a program responds to memory allocation errors. Possiblesettings are: 0, meaning ignore errors; 1, meaning print diagnostic errors onstderr; and 2, meaning call abort() to terminate the program. Not all memoryallocation and deallocation errors are detected via the use of MALLOC_CHECK_; itfinds just the common ones. However, this technique is fast, easy to use, andhas low run-time overhead compared with the use of malloc debugging libraries.For security reasons, the setting of MALLOC_CHECK_ is ignored by set-user-ID andset-group-ID programs.

        Further information about all of the above features can be found in the glibc manual.

        A malloc debugging library offers the same API as the standard malloc package,but does extra work to catch memory allocation bugs. In order to use such alibrary, we link our application against that library instead of the malloc package inthe standard C library. Because these libraries typically Operate at the cost of slowerrun-time operation, increased memory consumption, or both, we should use themonly for debugging purposes, and then return to linking with the standard mallocpackage for the production version of an application. Among such libraries areElectric Fence (http://www.perens.com/FreeSoftware/),dmalloc(http://dmalloc.com/),Valgrind (http://valgrind.org/), andInsure++(http://www.parasoft.com/).

        Both Valgrind and Insure++ are capable of detecting many other kinds of bugsaside from those associated with heap allocation. See their respective web sitesfor details.

Controlling and monitoring the malloc package

        The glibc manual describes a range of nonstandard functions that can be used tomonitor and control the allocation of memory by functions in the malloc package,including the following:

> The mallopt() function modifies various parameters that control the algorithmused by malloc(). For example, one such parameter specifies the minimumamount of releasable space that must exist at the end of the free list beforesbrk() is used to shrink the heap. Another parameter specifies an upper limitfor the size of blocks that will be allocated from the heap; blocks larger thanthis are allocated using the mmap() system call (refer to Section 49.7).

> The mallinfo() function returns a structure containing various statistics aboutthe memory allocated by malloc().

        Many UNIX implementations provide versions of mallopt() and mallinfo().However, the interfaces offered by these functions vary across implementations, so theyare not portable.


上一篇:通道

下一篇:源代碼管理工具概述

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
欧美黑人巨大精品一区二区| 亚洲精品720p| 欧美亚洲另类视频| 日韩av中文字幕在线| 日韩精品高清在线观看| 精品亚洲一区二区三区四区五区| 欧美一性一乱一交一视频| 国产亚洲美女久久| 在线观看日韩欧美| 欧美日韩免费网站| 国产精品久久久久久久久久久不卡| 美女国内精品自产拍在线播放| 91精品国产自产在线| 亚洲一区二区三区sesese| 性欧美长视频免费观看不卡| 伦伦影院午夜日韩欧美限制| 热99精品只有里视频精品| 国产玖玖精品视频| 国产精品欧美在线| 高潮白浆女日韩av免费看| 美女撒尿一区二区三区| 欧美日韩第一页| 欧美日韩色婷婷| 亚洲一区二区中文字幕| 性亚洲最疯狂xxxx高清| 欧美性xxxx极品hd满灌| 国产精品久久久久久久午夜| 国产成人精彩在线视频九色| 亚洲午夜未删减在线观看| 国产xxx69麻豆国语对白| 欧美大秀在线观看| 亚洲精品在线观看www| 欧美视频在线观看 亚洲欧| 亚洲成人激情视频| 中文字幕欧美精品在线| 国产一区二区日韩| 欧美理论电影在线观看| 国产精品99蜜臀久久不卡二区| 久久亚洲国产精品| 人九九综合九九宗合| 免费不卡欧美自拍视频| 精品久久久免费| 亚洲四色影视在线观看| 久久久视频在线| 日韩专区中文字幕| 久久久伊人日本| 久久久国产精品亚洲一区| 久久成人国产精品| www国产91| 国产精品99久久久久久www| 亚洲免费人成在线视频观看| 国产欧美日韩免费| 欧美一级高清免费播放| 久久久极品av| 中文字幕久久久av一区| 欧美黑人巨大xxx极品| 上原亚衣av一区二区三区| 精品国内产的精品视频在线观看| 欧美日韩国产va另类| 亚洲美女福利视频网站| 欧美一级淫片aaaaaaa视频| 欧美www视频在线观看| 欧美超级乱淫片喷水| www欧美日韩| 色综合老司机第九色激情| 中文字幕免费精品一区高清| 久久久之久亚州精品露出| 国产在线观看一区二区三区| 欧美肥老妇视频| 中文字幕国产日韩| 亚洲第一区中文99精品| 国产精品高清免费在线观看| 亚洲另类欧美自拍| 久久国产精品久久久久久| 91在线视频一区| 亚洲最新中文字幕| 欧美激情视频在线免费观看 欧美视频免费一| 97久久久免费福利网址| 91高清视频免费| 国产一区在线播放| 欧美色欧美亚洲高清在线视频| 日韩亚洲第一页| 欧美精品一区三区| 欧美日韩亚洲精品内裤| 精品久久久久久久久久久久久久| 成人黄色生活片| 日韩精品黄色网| 亚洲午夜色婷婷在线| 色偷偷噜噜噜亚洲男人的天堂| 一区二区成人精品| 欧美精品久久久久久久久久| 亚洲精品按摩视频| 国产精品第一第二| 国产成人精品视频在线| 午夜精品久久久久久久99热| 国产精品av电影| 国产在线视频91| 亚洲欧美精品中文字幕在线| 亚洲跨种族黑人xxx| 欧美日韩中文字幕日韩欧美| 亚洲一区999| 日韩欧美有码在线| 国产日韩在线看| 欧美极品少妇与黑人| 国产精品免费看久久久香蕉| 国产成人激情视频| 亚洲女人天堂色在线7777| 色av吧综合网| 国产精品高潮呻吟久久av黑人| 久久久精品久久| 亚洲欧美视频在线| 欧美一级成年大片在线观看| 麻豆一区二区在线观看| 成人444kkkk在线观看| 国产亚洲精品久久久久久777| 日韩欧美中文字幕在线播放| 日韩在线观看免费网站| 欧美大全免费观看电视剧大泉洋| 这里只有视频精品| 超薄丝袜一区二区| 爽爽爽爽爽爽爽成人免费观看| 中文字幕亚洲专区| 久久久噜噜噜久久久| 亚洲综合成人婷婷小说| 亚洲国产美女精品久久久久∴| 欧美极品美女视频网站在线观看免费| 欧美日韩免费网站| 欧美精品久久久久久久免费观看| 成人午夜在线观看| 国产精品久久久久久久久久尿| 欧美高清视频在线| 亚洲老司机av| 自拍偷拍亚洲在线| 亚洲国产欧美一区二区丝袜黑人| 91在线观看欧美日韩| 成人信息集中地欧美| 色婷婷亚洲mv天堂mv在影片| 精品国内产的精品视频在线观看| 亚洲欧美精品中文字幕在线| 亚洲第一天堂av| 国产精品视频999| 最新69国产成人精品视频免费| 中文字幕一区电影| 国产精品白丝jk喷水视频一区| 亚洲一级一级97网| 亚洲91精品在线| 欧美视频在线观看免费网址| 欧美小视频在线| 韩剧1988免费观看全集| 欧美性做爰毛片| 色噜噜亚洲精品中文字幕| 美日韩丰满少妇在线观看| 亚洲男人天堂九九视频| 亚洲激情自拍图| 国产97色在线|日韩| 精品视频—区二区三区免费| 91精品国产自产在线观看永久| 国产成人精品在线视频| 久久久久久久999| 亚洲自拍偷拍色片视频| 国产精品香蕉在线观看| 国内精品400部情侣激情| 国产亚洲精品激情久久| 国产精品毛片a∨一区二区三区|国|