Linux System Programming note 9 ——Memory Management

1. Allocating Dynamic Memory
#include <stdlib.h>

void *malloc(size_t size);

2. Allocating Arrays

#include <stdlib.h>

void *calloc(size_t nr, size_t size);//分配的内存被初始化为0

3. Resizing Allocations

#include <stdlib.h>

void *realloc(void *ptr, size_t size);

4. Freeing Dynamci Memory

#include <stdlib.h>

void free(void *ptr);

5. Alignment

Allocating aligned memory

#define _XOPEN_SOURCE 600
#define _GNU_SOURCE

#include <stdlib.h>

int posix_memalign(void **memptr, size_t alignment, size_t size);

6. Managing the Data Segment

#include <unistd.h>

int brk(void *end);

void *sbrk(intptr_t increment);

7. Adcanced Memory Allocation

#include <malloc.h>

int mallopt(int param, int value);

param:
M_CHECK_ACTION
M_MMAP_MAX
M_MMAP_THRESHOLD
M_NXFAST
M_PERTURB
M_TOP_PAD
M_TRIM_THRESHOLD

8. Fine-Tuning with malloc_usable_size() and malloc_trim()

#include <malloc.h>

size_t malloc_usable_size(void *ptr); //返回实际上分配的内存的大小,可能比请求分配的内存大
#include <malloc.h>

int malloc_trim(size_t padding);

9. Debugging Memory Allocations
$ MALLOC_CHECK_=1 ./rudder

10. Obtaining Statistics
#include <malloc.h>

struct mallinfo mallinfo(void);

struct mallinfo {
     int arena;
     
};


11. Stack-Based Allocation

#include <alloca.h>

void *alloca(size_t size);

12. Duplicating String on the Stack

#define _GNU_SOURCE
#include  <string.h>

char *strdupa(const char *s);
char *strndupa(const char *s, size_t n);

13. Manipulating Memory
13.1 Setting Bytes
#include <string.h>

void *memset(void *s, int c, size_t n);

#include <strings.h>

void bzero(void *s, size_t n); //已经过时了,原存在于BSD中,现在建议使用memset()

13.2 Comparing Bytes

#include <string.h>

int memcmp(const void *s1, const void *s2, size_t n);

#include <strings.h>
int bcmp(const void *s1, const void *s2, size_t n); //已经过时了,原存在于BSD中

13.3 Moving Bytes

#include <string.h>

void *memmove(void *dst, const void *src, size_t n);

#include <strings.h>
void bcopy(const void *src, void *dst, size_t n); //已经过时了,原存在于BSD中

#include <string.h>

void *memcpy(void *dst, const void *src, size_t n);

#include <string.h>

void *memccpy(void *dst, const void *src, int c, size_t n);  //拷贝n个字符中c前的字符,返回地址为c字符之后开始的地址。

13.4 Searching Bytes

#include <string.h>

void *memchr(const void *s, int c, size_t n);

#define _GNU_SOURCE
#include <string.h>

void *memrchr(const void *s, int c, size_t n);  //从后面向前搜索

#define _GNU_SOURCE
#define <string.h>

void *memmem(const void *haystack, 
                        size_t haystacklen, 
                        const void *needle,
                        size_t needlelen);   //从haystack返回needle Bytes


14. Locking Memory
14.1 Locking Part of an Address Space
#include <sys/mman.h>

int mlock(const void *addr, size_t len);

14.2 Locking All of an Address Space
#include <sys/mman.h>

int mlockall(int flags);

flags:
MCL_CURRENT
MCL_FUTURE

14.3 Unlocking Memory
#include <sys/mman.h>

int munlock(const void *addr, size_t len);
int munlockall(void);

默认情况下内存锁定最大为32k

14.4 Is a Page in Physical Memory?

#include <unistd.h>
#include <sys/mman.h>

int mincore(void *start,
                 size_t length,
                 unsigned char *vec);












  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值