📄 alloc.c
字号:
#include <features.h>#include <unistd.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <sys/mman.h>#ifdef L_calloc_dbgvoid *calloc_dbg(size_t num, size_t size, char *function, char *file, int line){ void *ptr; fprintf(stderr, "calloc of %d bytes at %s @%s:%d = ", (int) (num * size), function, file, line); ptr = calloc(num, size); fprintf(stderr, "%p\n", ptr); return ptr;}#endif#ifdef L_malloc_dbgvoid *malloc_dbg(size_t size, char *function, char *file, int line){ void *result; fprintf(stderr, "malloc of %d bytes at %s @%s:%d = ", (int) size, function, file, line); result = malloc(size); fprintf(stderr, "%p\n", result); return result;}#endif#ifdef L_free_dbgvoid free_dbg(void *ptr, char *function, char *file, int line){ fprintf(stderr, "free of %p at %s @%s:%d\n", ptr, function, file, line); free(ptr);}#endif#ifdef L_callocvoid *calloc(size_t num, size_t size){ void *ptr = malloc(num * size); if (ptr) memset(ptr, 0, num * size); return ptr;}#endif#ifdef L_mallocvoid *malloc(size_t size){ void *result;#if 1 /* Some programs will call malloc (0). Lets be strict and return NULL */ if (size == 0) return NULL;#endif result = mmap((void *) 0, size, PROT_READ | PROT_WRITE,#ifdef __UCLIBC_HAS_MMU__ MAP_PRIVATE | MAP_ANONYMOUS, 0, 0#else MAP_SHARED | MAP_ANONYMOUS, 0, 0#endif ); if (result == MAP_FAILED) return 0; return result;}#endif#ifdef L_freevoid free(void *ptr){ munmap(ptr, 0);}#endif#ifdef L_reallocvoid *realloc(void *ptr, size_t size){ void *newptr = NULL; if (size > 0) { newptr = malloc(size); if (newptr && ptr) { memcpy(newptr, ptr, size); free(ptr); } } else free(ptr); return newptr;}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -