Linux System Programming 学习笔记(九) 内存管理
1. 进程地址空间
2. 动态内存
/* obtaining dynamic memory */
#include <stdlib.h>
void * malloc (size_t size);
/* returns a pointer to a block of memory suitable for holding an array of nr elements, each of size bytes */
#include <stdlib.h>
void * calloc (size_t nr, size_t size);
/* resizing (making larger or smaller) existing allocations */
#include <stdlib.h>
void * realloc (void *ptr, size_t size);
3. 数据对齐
/* actual allocation size of the chunk of memory pointed to by ptr */
#include <malloc.h>
size_t malloc_usable_size (void *ptr);
4. 管理数据段
#include <unistd.h>
int brk (void *end);
void * sbrk (intptr_t increment);
5. 匿名内存映射
void *p;
p = mmap (NULL, /* do not care where */
* , /* 512 KB */
PROT_READ | PROT_WRITE, /* read/write */
MAP_ANONYMOUS | MAP_PRIVATE, /* anonymous, private */
−, /* fd (ignored) */
); /* offset (ignored) */
if (p == MAP_FAILED)
perror ("mmap");
else
/* 'p' points at 512 KB of anonymous memory... */
6. 基于栈的动态内存分配
/* make a dynamic memory allocation from the stack */
#include <alloca.h>
void * alloca (size_t size);
7. 变长数组 Variable-Length Arrays
for (i = ; i < n; ++i) {
char foo[i + ];
/* use 'foo'... */
}
8. 选择内存分配机制
9. 内存操作
/* memset() sets the n bytes starting at s to the byte c and returns s */
#include <string.h>
void * memset (void *s, int c, size_t n);
/* compares two chunks of memory for equivalence */
#include <string.h>
int memcmp (const void *s1, const void *s2, size_t n);
因为结构体通常涉及到数据对齐,所以使用memcmp来比较两个结构体是不安全的
/* are two dinghies identical? (BROKEN) */
int compare_dinghies (struct dinghy *a, struct dinghy *b)
{
return memcmp (a, b, sizeof (struct dinghy));
}
上述代码不安全,应该分别比较每个结构体成员:
/* are two dinghies identical? */
int compare_dinghies (struct dinghy *a, struct dinghy *b)
{
int ret;
if (a->nr_oars < b->nr_oars)
return −;
if (a->nr_oars > b->nr_oars)
return ;
ret = strcmp (a->boat_name, b->boat_name);
if (ret)
return ret;
/* and so on, for each member... */
}
/* copies the first n bytes of src to dst, returning dst */
#include <string.h>
void * memmove (void *dst, const void *src, size_t n);
memmove可以正确处理内存区重叠的情况(部分dst位于src之内)
#include <string.h>
void * memcpy (void *dst, const void *src, size_t n)
memcpy在内存区出现重叠时 属于未定义行为
/* scans the n bytes of memory pointed at by s for the character c */
#include <string.h>
void * memchr (const void *s, int c, size_t n);
10. 锁定内存
/* “locking”one or more pages into physical memory, ensuring that they are never paged out to disk */
#include <sys/mman.h>
int mlock (const void *addr, size_t len);
mlock() locks the virtual memory starting at addr and extending for len bytes into physical memory
/* mlockall() locks all of the pages in the current process's address space into physical memory. */
#include <sys/mman.h>
int mlockall (int flags);
Linux System Programming 学习笔记(九) 内存管理的更多相关文章
- Linux System Programming 学习笔记(五) 进程管理
1. 进程是unix系统中两个最重要的基础抽象之一(另一个是文件) A process is a running program A thread is the unit of activity in ...
- Linux System Programming 学习笔记(八) 文件和目录管理
1. 文件和元数据 每个文件都是通过inode引用,每个inode索引节点都具有文件系统中唯一的inode number 一个inode索引节点是存储在Linux文件系统的磁盘介质上的物理对象,也是L ...
- Linux System Programming 学习笔记(一) 介绍
1. Linux系统编程的三大基石:系统调用.C语言库.C编译器 系统调用:内核向用户级程序提供服务的唯一接口.在i386中,用户级程序执行软件中断指令 INT n 之后切换至内核空间 用户程序通过寄 ...
- Linux System Programming 学习笔记(七) 线程
1. Threading is the creation and management of multiple units of execution within a single process 二 ...
- Linux System Programming 学习笔记(四) 高级I/O
1. Scatter/Gather I/O a single system call to read or write data between single data stream and mu ...
- Linux System Programming 学习笔记(十一) 时间
1. 内核提供三种不同的方式来记录时间 Wall time (or real time):actual time and date in the real world Process time:the ...
- Linux System Programming 学习笔记(十) 信号
1. 信号是软中断,提供处理异步事件的机制 异步事件可以是来源于系统外部(例如用户输入Ctrl-C)也可以来源于系统内(例如除0) 内核使用以下三种方法之一来处理信号: (1) 忽略该信号.SIG ...
- Linux System Programming 学习笔记(六) 进程调度
1. 进程调度 the process scheduler is the component of a kernel that selects which process to run next. 进 ...
- Linux System Programming 学习笔记(二) 文件I/O
1.每个Linux进程都有一个最大打开文件数,默认情况下,最大值是1024 文件描述符不仅可以引用普通文件,也可以引用套接字socket,目录,管道(everything is a file) 默认情 ...
随机推荐
- Ubuntu下命令行访问网站
第一步,需要安装一个名为w3m的软件工具,打开终端,输入如下命令 sudo apt-get install w3m 第二步,安装好w3m之后,在终端里面启动w3m,打开一个网址,比如w3m www ...
- java基础—网络编程
一.网络基础概念 首先理清一个概念:网络编程 != 网站编程,网络编程现在一般称为TCP/IP编程.
- C++ NULL与nullptr的区别
C与C++中空指针的区别 在C里面,由于处处都要使用指针,所以导致NULL遍布各地.我们先来看C99是怎么定义NULL的: NULL can be defined as any null pointe ...
- shell脚本,awk取奇数行与偶数行方法。
第一种方法: 第二种方法: 第三种方法:
- cocos2dx for lua 截屏功能
cocos2dx的utils类中包含截图功能,使用方法如下: cc.utils:captureScreen(function(successed,outputFile)--第一个参数是截图成功或者失败 ...
- webpack开始一个项目的步骤
这几天在学习Vue 用到了webpack打包工具 开始一个项目的时候 需要配置很多项 刚开始写的时候 配置文件总是缺什么再去配置什么 创建项目就用了半个小时 后来觉得应该有个步骤 这样 ...
- 禁止按键F5和禁止鼠标右键菜单 js代码
<script language="javascript"> //禁止按键F5 document.onkeydown = function(e){ e = window ...
- [LUOGU] NOIP提高组模拟赛Day1
题外话:以Ingress为题材出的比赛好评,绿军好评 T1 考虑枚举第\(i\)个人作为左边必选的一个人,那左边剩余\(i-1\)个人,选法就是\(2^{i-1}\),也就是可以任意选或不选,右侧剩余 ...
- Ubuntu 18.04安装显卡驱动
安装完双系统,我遇到好几次开机或关机有问题,导致我重装了3次系统,第三次我才知道是显卡驱动问题,Ubuntu预装的开源Nvidia驱动太烂了,需要换官方驱动. 把 nouveau 驱动加入黑名单 $s ...
- 03大端和小端(Big endian and Little endian)
1.大端和小端的问题 对于整型.长整型等数据类型,Big endian 认为第一个字节是最高位字节(按照从低地址到高地址的顺序存放数据的高位字节到低位字节),而 Little endian 则相反 ...