分类: LINUX
2010-09-07 20:47:03
$ sudo apt-get libmm14 libmm-dev libmm-dbg
/* Standard Malloc-Style API */ MM *mm_create(size_t, const char *); int mm_permission(MM *, mode_t, uid_t, gid_t); void mm_reset(MM *); void mm_destroy(MM *); int mm_lock(MM *, mm_lock_mode); int mm_unlock(MM *); void *mm_malloc(MM *, size_t); void *mm_realloc(MM *, void *, size_t); void mm_free(MM *, void *); void *mm_calloc(MM *, size_t, size_t); char *mm_strdup(MM *, const char *); size_t mm_sizeof(MM *, const void *); size_t mm_maxsize(void); size_t mm_available(MM *); char *mm_error(void); void mm_display_info(MM *);
#include#include #include #include #include int main(int argc, char* argv[]) { // 创建 10KB 内存池 (最小 8192),"abc" 是创建锁定标识文件名。 MM* pool = mm_create(1024 * 10, "abc"); // 锁定池,在当前目录下创建 abc.sem 文件。 mm_lock(pool, MM_LOCK_RW); // 在池内分配内存块。 int* x = mm_malloc(pool, sizeof(int)); *x = 1234; // 获取池内分配的某个块大小。 printf("%p = %d\n", x, mm_sizeof(pool, x)); // 显式整个池状态信息。 mm_display_info(pool); printf("max:%d, avail:%d\n", mm_maxsize(), mm_available(pool)); getchar(); // 删除 abc.sem,解除锁定。 mm_unlock(pool); // 释放整个池。 mm_destroy(pool); return EXIT_SUCCESS; }
$ gcc -g -o test -lmm main.c $ ldd ./test linux-gate.so.1 => (0xb7729000) libmm.so.14 => /usr/lib/libmm.so.14 (0xb771c000) libc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0xb75d7000) /lib/ld-linux.so.2 (0xb772a000) $ ./test 0xb7850034 = 4 Information for MM memory area = 0xb7850014 - 0xb78a0314 memory size = 10264 memory offset = 40 bytes spare = 10224 bytes free = 0 (0 chunks) bytes allocated = 16 List of free chunks:max:33546216, avail:10224
$ ps aux | grep test yuhen 2406 0.0 0.0 1576 440 pts/1 S+ 19:37 0:00 ./test $ cat /proc/2406/maps 08048000-08049000 r-xp 00000000 fc:00 30456 /home/yuhen/projects/c/test 08049000-0804a000 r--p 00000000 fc:00 30456 /home/yuhen/projects/c/test 0804a000-0804b000 rw-p 00001000 fc:00 30456 /home/yuhen/projects/c/test b7701000-b7703000 rw-p 00000000 00:00 0 b7703000-b7841000 r-xp 00000000 fc:00 690 /lib/tls/i686/cmov/libc-2.10.1.so b7841000-b7842000 ---p 0013e000 fc:00 690 /lib/tls/i686/cmov/libc-2.10.1.so b7842000-b7844000 r--p 0013e000 fc:00 690 /lib/tls/i686/cmov/libc-2.10.1.so b7844000-b7845000 rw-p 00140000 fc:00 690 /lib/tls/i686/cmov/libc-2.10.1.so b7845000-b7848000 rw-p 00000000 00:00 0 b7848000-b784b000 r-xp 00000000 fc:00 50664 /usr/lib/libmm.so.14.0.22 b784b000-b784d000 rw-p 00003000 fc:00 50664 /usr/lib/libmm.so.14.0.22 b784d000-b784f000 rw-p 00000000 00:00 0 b784f000-b7853000 rw-s 00000000 00:09 491521 /SYSV00000000 (deleted) b7853000-b7855000 rw-p 00000000 00:00 0 b7855000-b7856000 r-xp 00000000 00:00 0 [vdso] b7856000-b7871000 r-xp 00000000 fc:00 599 /lib/ld-2.10.1.so b7871000-b7872000 r--p 0001a000 fc:00 599 /lib/ld-2.10.1.so b7872000-b7873000 rw-p 0001b000 fc:00 599 /lib/ld-2.10.1.so bfd3c000-bfd51000 rw-p 00000000 00:00 0 [stack]
#include#include #include #include #include int main(int argc, char* argv[]) { MM* pool = mm_create(1024 * 10, "abc"); /* --------- DUP ------------------ */ char* s1 = mm_malloc(pool, 10); strcpy(s1, "abcd"); char* s2 = mm_strdup(pool, s1); printf("s1=%p,%s, s2=%p,%s\n", s1, s1, s2, s2); printf("[Befor Reset] available: %d\n", mm_available(pool)); /* --------- RESET ----------------- */ mm_reset(pool); printf("[After Reset] available: %d\n", mm_available(pool)); int* x = mm_malloc(pool, sizeof(int)); *x = 0x1234; printf("x=%p,0x%x\n", x, *x); /* --------- ERROR ----------------- */ char* s = mm_malloc(pool, 1024 * 20); if (!s) printf("%s\n", mm_error()); /* --------- INFO ------------------ */ mm_display_info(pool); mm_destroy(pool); return EXIT_SUCCESS; }
c$ ./test s1=0xb78d8034,abcd, s2=0xb78d804c,abcd [Befor Reset] available: 10200 [After Reset] available: 10240 x=0xb78d8034,0x1234 mm:alloc: out of memory Information for MM memory area = 0xb78d8014 - 0xb7928314 memory size = 10264 memory offset = 40 bytes spare = 10224 bytes free = 0 (0 chunks) bytes allocated = 16 List of free chunks:
$ valgrind --leak-check=full ./test ==2654== Memcheck, a memory error detector ==2654== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al. ==2654== Using Valgrind-3.5.0-Debian and LibVEX; rerun with -h for copyright info ==2654== Command: ./test ==2654== ==2654== ==2654== HEAP SUMMARY: ==2654== in use at exit: 0 bytes in 0 blocks ==2654== total heap usage: 0 allocs, 0 frees, 0 bytes allocated ==2654== ==2654== All heap blocks were freed -- no leaks are possible ==2654== ==2654== For counts of detected and suppressed errors, rerun with: -v ==2654== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 15 from 8)
000016c0: 16c0: 55 push ebp 16c1: 89 e5 mov ebp,esp 16c3: 53 push ebx 16c4: e8 4e fd ff ff call 1417 16c9: 81 c3 2b 29 00 00 add ebx,0x292b 16cf: 83 ec 04 sub esp,0x4 16d2: 8b 83 34 01 00 00 mov eax,DWORD PTR [ebx+0x134] 16d8: 85 c0 test eax,eax 16da: 74 08 je 16e4 16dc: 89 04 24 mov DWORD PTR [esp],eax 16df: e8 d8 fa ff ff call 11bc 16e4: 83 c4 04 add esp,0x4 16e7: 5b pop ebx 16e8: 5d pop ebp 16e9: c3 ret 16ea: 8d b6 00 00 00 00 lea esi,[esi+0x0]