進(jìn)程地址空間
Pages and Paging
bits => bytes => words => pages
page是MMU(memory management unit)的最小地址單元
虛擬地址空間被分割成pages。
機(jī)器架構(gòu)決定了page的size状原。一般來(lái)說(shuō)32位的系統(tǒng)是4KB艾恼,64位的系統(tǒng)是8KB别垮。
Sharing and copy-on-write(COW)
Memory Regions
- text segment: code, string literals, constant variables, and other read-only data.
- stack: process's execution stack.
- data segment or heap:
- bss segment: uninitialized global variables
分配動(dòng)態(tài)內(nèi)存
任何內(nèi)存管理系統(tǒng)的foundation是分配搅幅,使用考抄,和到最后的返還dynamic memory展姐。
// classic C interface for obtaining dynamic memory
#include <stdlib.h>
void *malloc(size_t size);
char *p;
/* give me 2 KB!*/
p = malloc(2048);
if(!p)
perror("malloc");
struct treasure_map *map;
map = malloc(sizeof(struct treasure_map));
if(!map)
perror("malloc");
//c++ need to typecast malloc()'s return
char *name;
/* allocate 512 bytes*/
name = (char *)malloc(512);
if(!name)
perror("malloc");
//wrapper of malloc
// like malloc(), but terminates on failure
void *xmalloc(size_t size)
{
void *p;
p = malloc(size);
if(!p){
perror("xmalloc");
exit(EXIT_FAILURE);
}
return p;
}
Allocating Arrays
#include <stdlib.h>
void *calloc(size_t nr, size_t size);
int *x, *y;
x = malloc(50 * sizeof(int));
if(!x){
perror("malloc")l
return -1;
}
y = calloc(50, sizeof(int));
if(!y){
perror("calloc");
return -1;
}
雖然看上去是一樣的,但上面兩個(gè)的行為是不同的犁柜。malloc不保證分配內(nèi)存的內(nèi)容是什么洲鸠,calloc會(huì)把分配到的內(nèi)存走初始化位0。
/* works identically to malloc(), but memory is zeroed*/
void *malloc0(size_t size)
{
return calloc(1, size);
}
/* like malloc(), but zeros memory and terminated on failure*/
void *xmalloc0(size_t size)
{
void *p;
p = calloc(1, size);
if(!p){
perror("xmalloc0");
exit(EXIT_FAILURE);
}
return p;
}
Resizing Allocations
#include <stdlib.h>
void *realloc(void *ptr, size_t size);
If size is 0, 效果和free()是一樣的馋缅。
struct map *p;
/* allocate memory for two map structures*/
p = calloc(2, sizeof(struct map));
if(!p){
perror("calloc");
return -1;
}
/* use p[0] and p[1]*/
struct map *r;
/* we now need memory for only one map*/
r = realloc(p, sizeof(struct map));
if(!r){
/*note that 'p' is still valid!*/
perror("realloc");
return -1;
}
/*use 'r'*/
free(r);
如果調(diào)用返回失敗扒腕,則p未被觸及,因此仍然有效萤悴。我們可以繼續(xù)使用它瘾腰,并最終需要釋放它。相反稚疹,如果調(diào)用成功居灯,則忽略p祭务,而使用r代替内狗。
Freeing Dynamic Memory
#include <stdlib.h>
void free(void *prt);
Alignment
在編寫(xiě)可移植代碼時(shí)怪嫌,程序員必須小心避免違反對(duì)齊要求。
Allocating alignment memory
在大多數(shù)情況下柳沙,編譯器和C庫(kù)透明地處理對(duì)齊參數(shù).POSIX命令通過(guò)malloc()岩灭、calloc()和realloc()返回的內(nèi)存對(duì)齊以供任何一種標(biāo)準(zhǔn)的C類(lèi)型使用。在linux上赂鲤,這些函數(shù)總是返回在32位系統(tǒng)上沿著8字節(jié)邊界對(duì)齊的內(nèi)存噪径,在64位系統(tǒng)上返回沿16字節(jié)邊界對(duì)齊的內(nèi)存。
/* one or the other -- either suffices */
#define _XOPEN_SOURCE 600
#define _GNU_SOURCE
#include <stdlib.h>
int posix_memalign (void **memptr, size_t alignment, size_t size);
成功的調(diào)用將返回0数初,并且分配size大小并且按照alignment大小對(duì)齊的動(dòng)態(tài)內(nèi)存找爱。
失敗的話,會(huì)設(shè)置errno泡孩。
char *buf;
int ret;
/* allocate 1 KB along a 256-byte boundary */
ret = posix_memalign (&buf, 256, 1024);
if (ret) {
fprintf (stderr, "posix_memalign: %s\n",
strerror (ret));
return ?1;
}
/* use 'buf'... */
free (buf);
Other alignment concerns
Managing the Data Segment
堆中的動(dòng)態(tài)內(nèi)存分配從段的底部向上增長(zhǎng)车摄;棧從段的頂部向下擴(kuò)展到堆。
將兩者分開(kāi)的分界線稱(chēng)為斷點(diǎn)或斷點(diǎn)仑鸥。
#include <unistd.h>
int brk (void *end);
void * sbrk (intptr_t increment);
A call to brk() sets the break point (the end of the data segment) to the address specified by end. On success, it returns 0. On failure, it returns ?1 and sets errno to ENOMEM.
A call to sbrk() increments the end of the data segment by increment bytes, which may be a positive or negative delta. sbrk() returns the revised break point. Thus, an increment of 0 provides the current break point:
printf ("The current break point is %p\n", sbrk (0));
Deliberately, both POSIX and the C standard define neither of these functions. Nearly all Unix systems, however, support one or both. Portable programs should stick to the standards-based interfaces.