點(diǎn)擊上方藍(lán)色“混說Linux”,選擇“設(shè)為星標(biāo)”
第一時(shí)間看干貨文章


7.22.3?節(jié)里,有如下說法:The order and contiguity of storage allocated by successive calls to the aligned_alloc, calloc, malloc, and realloc functions is unspecified. The pointer returned if the allocation succeeds is suitably aligned so that it may be assigned to a pointer to any type of object with a fundamental alignment requirement and then used to access such an object or an array of such objects in the space allocated (until the space is explicitly deallocated). The lifetime of an allocated object extends from the allocation until the deallocation. Each such allocation shall yield a pointer to an object disjoint from any other object. The pointer returned points to the start (lowest byte address) of the allocated space. If the space cannot be allocated, a null pointer is returned. If the size of the space requested is zero, the behavior is implementation-defined: either a null pointer is returned to indicate an error, or the behavior is as if the size were some nonzero value, except that the returned pointer shall not be used to access an object.
malloc?接到的參數(shù)為 0 時(shí),其行為是由實(shí)現(xiàn)定義的(implementation-defined)。malloc?返回的數(shù)值進(jìn)行任何假設(shè)。malloc(0)?這種天怒人怨的代碼。glibc?的源代碼,依此了解在?glibc?下,malloc(0)?的行為。在?glibc2.27/malloc/malloc.c?中,有如下注釋:/*
??malloc(size_t?n)
??Returns?a?pointer?to?a?newly?allocated?chunk?of?at?least?n?bytes,?or?null
??if?no?space?is?available.?Additionally,?on?failure,?errno?is
??set?to?ENOMEM?on?ANSI?C?systems.
??If?n?is?zero,?malloc?returns?a?minumum-sized?chunk.?(The?minimum
??size?is?16?bytes?on?most?32bit?systems,?and?24?or?32?bytes?on?64bit
??systems.)??On?most?systems,?size_t?is?an?unsigned?type,?so?calls
??with?negative?arguments?are?interpreted?as?requests?for?huge?amounts
??of?space,?which?will?often?fail.?The?maximum?supported?value?of?n
??differs?across?systems,?but?is?in?all?cases?less?than?the?maximum
??representable?value?of?a?size_t.
*/
malloc(0)?時(shí),我們實(shí)際會(huì)拿到一個(gè)指向一小塊內(nèi)存的指針,這個(gè)指針指向的(分配給我們的)內(nèi)存的大小是由機(jī)器決定的。checked_request2size?實(shí)現(xiàn)的。/*?pad?request?bytes?into?a?usable?size?--?internal?version?*/
#define?request2size(req)?????????????????????????????????????????\
??(((req)?+?SIZE_SZ?+?MALLOC_ALIGN_MASK????MINSIZE?:??????????????????????????????????????????????????????\
???((req)?+?SIZE_SZ?+?MALLOC_ALIGN_MASK)?&?~MALLOC_ALIGN_MASK)
/*?Same,?except?also?perform?an?argument?and?result?check.??First,?we?check
???that?the?padding?done?by?request2size?didn't?result?in?an?integer
???overflow.??Then?we?check?(using?REQUEST_OUT_OF_RANGE)?that?the?resulting
???size?isn't?so?large?that?a?later?alignment?would?lead?to?another?integer
???overflow.??*/
#define?checked_request2size(req,?sz)?\
({????????\
??(sz)?=?request2size?(req);?????\
??if?(((sz)?(req))??????\
??????||?REQUEST_OUT_OF_RANGE?(sz))?\
????{????????\
??????__set_errno?(ENOMEM);?????\
??????return?0;???????\
????}????????\
})
MINSIZE?,這個(gè)?MINSIZE?的相關(guān)定義如下:/*?The?smallest?possible?chunk?*/
#define?MIN_CHUNK_SIZE????????(offsetof(struct?malloc_chunk,?fd_nextsize))
/*?The?smallest?size?we?can?malloc?is?an?aligned?minimal?chunk?*/
#define?MINSIZE??\
??(unsigned?long)(((MIN_CHUNK_SIZE+MALLOC_ALIGN_MASK)?&?~MALLOC_ALIGN_MASK))/*?The?corresponding?bit?mask?value.??*/
#define?MALLOC_ALIGN_MASK?(MALLOC_ALIGNMENT?-?1)
/*?MALLOC_ALIGNMENT?is?the?minimum?alignment?for?malloc'ed?chunks.??It
???must?be?a?power?of?two?at?least?2?*?SIZE_SZ,?even?on?machines?for
???which?smaller?alignments?would?suffice.?It?may?be?defined?as?larger
???than?this?though.?Note?however?that?code?and?data?structures?are
???optimized?for?the?case?of?8-byte?alignment.??*/
#define?MALLOC_ALIGNMENT?(2?*?SIZE_SZ????????__alignof__?(long?double)?:?2?*?SIZE_SZ)
#ifndef?INTERNAL_SIZE_T
#?define?INTERNAL_SIZE_T?size_t
#endif
/*?The?corresponding?word?size.??*/
#define?SIZE_SZ?(sizeof?(INTERNAL_SIZE_T))
/*
??This?struct?declaration?is?misleading?(but?accurate?and?necessary).
??It?declares?a?"view"?into?memory?allowing?access?to?necessary
??fields?at?known?offsets?from?a?given?base.?See?explanation?below.
*/
struct?malloc_chunk?{
??INTERNAL_SIZE_T??????mchunk_prev_size;??/*?Size?of?previous?chunk?(if?free).??*/
??INTERNAL_SIZE_T??????mchunk_size;???????/*?Size?in?bytes,?including?overhead.?*/
??struct?malloc_chunk*?fd;?????????/*?double?links?--?used?only?if?free.?*/
??struct?malloc_chunk*?bk;
??/*?Only?used?for?large?blocks:?pointer?to?next?larger?size.??*/
??struct?malloc_chunk*?fd_nextsize;?/*?double?links?--?used?only?if?free.?*/
??struct?malloc_chunk*?bk_nextsize;
};
//?GCC?提供
/*?Offset?of?member?MEMBER?in?a?struct?of?type?TYPE.?*/
#define?offsetof(TYPE,?MEMBER)?__builtin_offsetof?(TYPE,?MEMBER)
glibc?在我們的電腦上運(yùn)行時(shí)?malloc?出的最小空間的大小了。計(jì)算完后,還可以根據(jù)?malloc_usable_size?判斷自己的計(jì)算是否正確,樣例代碼如下:#include?
#include?
int?main(void)?{
????char?*p?=?malloc(0);
????printf("Address:?0x%x.\nLength:?%ld.\n",p,malloc_usable_size(p));
????return?0;
}
glibc?下,執(zhí)行?malloc?會(huì)得到一個(gè)指向分配給我們的大小為?24?字節(jié)的內(nèi)存空間的指針。glibc?下的結(jié)果,在其他 C 標(biāo)準(zhǔn)庫實(shí)現(xiàn)內(nèi),可能你會(huì)得到一個(gè)空指針。因?yàn)闃?biāo)準(zhǔn)中提到了,對(duì)于?malloc(0)?這種故意挑事的代碼,實(shí)現(xiàn)時(shí)可以返回一個(gè)空指針作為回禮。
往期推薦