Linux System Programming note 9 ——Memory Management
2014-06-06 09:23
357 查看
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);
#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);
相关文章推荐
- Linux System Programming note 5—— Process Management
- Linux System Programming note 11 ——Time
- Linux System Programming note 6—— Adcanced Process Management
- Linux System Programming note 8 ——File and Directory Management
- Linux System Programming note 2 ——File I/O
- Linux System Programming note 10 —— Signal
- Linux System Programming note 8 ——File and Directory Management
- Linux System Programming note 4 —— Advanced File I/O
- Linux System Programming note 7 —— Threading
- Linux System Programming note 1
- Linux System Programming --Chapter Eight
- Linux System Programming --Chapter Six
- Linux System Programming --Chapter Nine
- Linux System Programming -- Appendix
- Linux System Programming --Chapter Five
- Understanding Unix/Linux Programming note:chapter 1:more函数的流程图
- Linux System Programming --Chapter Five
- Linux System Programming --Chapter Four
- Linux System Programming -- Appendix
- Linux System Programming --Chapter Two