您的位置:首页 > 运维架构 > Linux

《Linux/Unix系统编程手册》读书笔记4

2014-04-20 23:50 302 查看
《Linux/Unix系统编程手册》读书笔记 目录

第7章:

内存分配

通过增加堆的大小分配内存,通过提升program break位置的高度来分配内存。

基本学过C语言的都用过malloc来分配内存,而malloc都基于brk()和sbrk()。

#include <unistd.h>

int brk(void *end_data_segment);

int *sbrk(intptr_t increment);


brk()系统调用会将program break设置为end_data_segment的位置。成功调用返回0,失败返回-1。

sbrk()系统调用会将program break原来的位置增加increment的大小。成功调用返回原来program break的位置,失败返回(void *)-1。

PS:sbrk(0)返回program break的当前位置。

malloc()函数,C使用该函数在堆上分配和释放内存。

#include <stdlib.h>

void *malloc(size_t size);


malloc分配size字节大小的内存。并返回指向新分配的内存的起始位置的指针,该内存未经初始化;失败返回NULL。

通常对返回的指针进行显式类型转换来获得自己想要的类型的指针。

实际上每次调用malloc分配内存时,会额外分配多几个字节来记录这块内存的大小。(如下图所示)

/*
* =====================================================================================
*
*       Filename:  free_and_sbrk.c
*
*    Description:
*
*        Version:  1.0
*        Created:  2014年03月19日 11时37分09秒
*       Revision:  none
*       Compiler:  gcc
*
*         Author:  alan (), alan19920626@gmail.com
*   Organization:
*
* =====================================================================================
*/

#include "tlpi_hdr.h"

#define MAX_ALLOCS 1000000

int main(int argc, char *argv[]){
char *ptr[MAX_ALLOCS];
int freeStep, freeMin, freeMax, blockSize, numAllocs, j;

printf("\n");

if(argc < 3 || strcmp(argv[1], "--help") == 0)
usageErr("%s num-allocs blocksize [step [min [max]]]\n", argv[0]);

numAllocs = getInt(argv[1], GN_GT_0, "num-allocs");

if(numAllocs > MAX_ALLOCS)
cmdLineErr("num-allocs > %d\n", MAX_ALLOCS);

blockSize = getInt(argv[2], GN_GT_0 | GN_ANY_BASE, "blocksize");

freeStep = (argc > 3) ? getInt(argv[3], GN_GT_0, "step") : 1;
freeMin = (argc > 4) ? getInt(argv[4], GN_GT_0, "min") : 1;
freeMax = (argc > 5) ? getInt(argv[5], GN_GT_0, "max") : numAllocs;

if(freeMax > numAllocs)
cmdLineErr("free-max > num-allocs\n");

printf("Initial program break:          %10p\n", sbrk(0));

printf("Allocing %d*%d bytes\n", numAllocs, blockSize);

for(j = 0; j < numAllocs; j++){
ptr[j] = malloc(blockSize);
if(ptr[j] == NULL)
errExit("malloc");
}

printf("Program break is now:           %10p\n", sbrk(0));

printf("Freeing blocks from %d to %d in steps of %d\n", freeMin, freeMax, freeStep);
for(j = freeMin-1; j < freeMax; j += freeStep)
free(ptr[j]);

printf("After free(), program break is %10p\n", sbrk(0));

exit(EXIT_SUCCESS);
}


View Code

/*
* =====================================================================================
*
*       Filename:  free_and_sbrk.c
*
*    Description:
*
*        Version:  1.0
*        Created:  2014年03月19日 11时37分09秒
*       Revision:  none
*       Compiler:  gcc
*
*         Author:  alan (), alan19920626@gmail.com
*   Organization:
*
* =====================================================================================
*/

#include "tlpi_hdr.h"

#define MAX_ALLOCS 1000000

int main(int argc, char *argv[]){
char *ptr[MAX_ALLOCS];
int freeStep, freeMin, freeMax, blockSize, numAllocs, j;

printf("\n");

if(argc < 3 || strcmp(argv[1], "--help") == 0)
usageErr("%s num-allocs blocksize [step [min [max]]]\n", argv[0]);

numAllocs = getInt(argv[1], GN_GT_0, "num-allocs");

if(numAllocs > MAX_ALLOCS)
cmdLineErr("num-allocs > %d\n", MAX_ALLOCS);

blockSize = getInt(argv[2], GN_GT_0 | GN_ANY_BASE, "blocksize");

freeStep = (argc > 3) ? getInt(argv[3], GN_GT_0, "step") : 1;
freeMin = (argc > 4) ? getInt(argv[4], GN_GT_0, "min") : 1;
freeMax = (argc > 5) ? getInt(argv[5], GN_GT_0, "max") : numAllocs;

if(freeMax > numAllocs)
cmdLineErr("free-max > num-allocs\n");

printf("Initial program break:          %10p\n", sbrk(0));

printf("Allocing %d*%d bytes\n", numAllocs, blockSize);

for(j = 0; j < numAllocs; j++){
ptr[j] = malloc(blockSize);
if(ptr[j] == NULL)
errExit("malloc");
printf("After malloc(), program break is: %10p\n", sbrk(0));
}

printf("Program break is now:           %10p\n", sbrk(0));

printf("Freeing blocks from %d to %d in steps of %d\n", freeMin, freeMax, freeStep);
for(j = freeMin-1; j < freeMax; j += freeStep)
free(ptr[j]);

printf("After free(), program break is %10p\n", sbrk(0));

exit(EXIT_SUCCESS);
}


测试结果:

lancelot@debian:~/Code/tlpi$ ./7-1 15 10 2

Initial program break:           0x1913000
Allocing 15*10 bytes
After malloc(), program break is:  0x1934000
After malloc(), program break is:  0x1934000
After malloc(), program break is:  0x1934000
After malloc(), program break is:  0x1934000
After malloc(), program break is:  0x1934000
After malloc(), program break is:  0x1934000
After malloc(), program break is:  0x1934000
After malloc(), program break is:  0x1934000
After malloc(), program break is:  0x1934000
After malloc(), program break is:  0x1934000
After malloc(), program break is:  0x1934000
After malloc(), program break is:  0x1934000
After malloc(), program break is:  0x1934000
After malloc(), program break is:  0x1934000
After malloc(), program break is:  0x1934000
Program break is now:            0x1934000
Freeing blocks from 1 to 15 in steps of 2
After free(), program break is  0x1934000


可以清楚的看得出来,内存块只是调用了一次sbrk()来改变program break的位置,分配15块10个字节的内存块,只是第一次的时候调用了sbrk分配了1000个字节的内存块,并将990个字节大小的内存块放到空闲内存块列表,供以后malloc的调用使用。。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: