Annotation of libelwix/example/test_mem.c, revision 1.5
1.1 misho 1: #include <stdio.h>
1.2 misho 2: #include <string.h>
1.1 misho 3: #include <stdlib.h>
4: #include <elwix.h>
5:
6:
7: void show(u_int size, u_int act, u_int inact)
8: {
9: if (!act && !inact)
10: return;
11:
12: if (size < 1024)
13: printf("Statistics:: BUCKET %uB size, %u active, %u inactive\n", size, act, inact);
14: else if (size < 1024 * 1024)
15: printf("Statistics:: BUCKET %uKB size, %u active, %u inactive\n", size / 1024, act, inact);
16: else
17: printf("Statistics:: BUCKET %uMB size, %u active, %u inactive\n", size / (1024 * 1024), act, inact);
18: }
19:
20: int
21: main(int argc, char **argv)
22: {
23: mpool_t *mp;
24: void *addr;
25: int i;
1.4 misho 26: u_long curr, real;
1.1 misho 27:
1.3 misho 28: printf("whether default memory mapper is elwix? %d\n", elwix_mm_inuse());
29:
1.1 misho 30: for (i = 0; i < 3; i++) {
31: addr = malloc(4000);
32: printf("addr=%p\n", addr);
33: free(addr);
34: }
35:
36: mp = mpool_init(0);
37: if (!mp) {
38: printf("Error:: #%d - %s\n", elwix_GetErrno(), elwix_GetError());
39: return 1;
40: }
41: /*
1.4 misho 42: mpool_getquota(mp, &curr, NULL, NULL);
1.1 misho 43: printf("___current=%lu\n", curr);
44: */
45:
1.5 ! misho 46: printf(">>> get 3 allocs\n");
1.1 misho 47: for (i = 0; i < 3; i++) {
48: addr = mpool_malloc(mp, 4000, "mdaaa 4000");
49: if (!addr) {
50: printf("Error:: #%d - %s\n", elwix_GetErrno(), elwix_GetError());
51: mpool_destroy(&mp);
52: return 2;
53: }
54: printf("addr=%p chkaddr=%d addr_size=%d\n", addr, mpool_chkaddr(addr), mpool_getsizebyaddr(addr));
55:
56: strlcpy(addr, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\ndddddd\n",
57: mpool_getsizebyaddr(addr));
58: // mpool_free(mp, addr, 0);
59: }
60:
1.4 misho 61: mpool_getquota(mp, &curr, &real, NULL);
62: printf("___current=%lu ___real=%lu\n", curr, real);
1.1 misho 63:
64: printf("0) calls.act=%lu calls.inact=%lu calls.free=%lu; bytes.act=%lu bytes.inact=%lu bytes.free=%lu;\n",
65: mp->pool_calls.alloc, mp->pool_calls.cache, mp->pool_calls.free,
66: mp->pool_bytes.alloc, mp->pool_bytes.cache, mp->pool_bytes.free);
67: mpool_statistics(mp, show);
68:
1.5 ! misho 69: printf(">>> realloc 4000 to 5010 addr=%p\n", addr);
1.1 misho 70: addr = mpool_realloc(mp, addr, 5010, "|||||||||||||");
71: if (!addr)
72: printf("Error:: #%d - %s\n", elwix_GetErrno(), elwix_GetError());
1.4 misho 73: mpool_getquota(mp, &curr, &real, NULL);
74: printf("addr=%p_current=%lu_real=%lu\n", addr, curr, real);
1.1 misho 75:
76: printf("1) calls.act=%lu calls.inact=%lu calls.free=%lu; bytes.act=%lu bytes.inact=%lu bytes.free=%lu;\n",
77: mp->pool_calls.alloc, mp->pool_calls.cache, mp->pool_calls.free,
78: mp->pool_bytes.alloc, mp->pool_bytes.cache, mp->pool_bytes.free);
79: mpool_statistics(mp, show);
80:
81:
1.5 ! misho 82: printf(">>> free addr=%p\n", addr);
1.1 misho 83: if (addr)
84: mpool_free(mp, addr, 0);
85:
86: printf("2) calls.act=%lu calls.inact=%lu calls.free=%lu; bytes.act=%lu bytes.inact=%lu bytes.free=%lu;\n",
87: mp->pool_calls.alloc, mp->pool_calls.cache, mp->pool_calls.free,
88: mp->pool_bytes.alloc, mp->pool_bytes.cache, mp->pool_bytes.free);
89: mpool_statistics(mp, show);
90:
1.5 ! misho 91: printf(">>> purge inactive memory\n");
1.1 misho 92: mpool_purge(mp, 0);
93:
1.4 misho 94: mpool_getquota(mp, &curr, &real, NULL);
95: printf("___current=%lu ___real=%lu\n", curr, real);
1.1 misho 96:
97: printf("3) calls.act=%lu calls.inact=%lu calls.free=%lu; bytes.act=%lu bytes.inact=%lu bytes.free=%lu;\n",
98: mp->pool_calls.alloc, mp->pool_calls.cache, mp->pool_calls.free,
99: mp->pool_bytes.alloc, mp->pool_bytes.cache, mp->pool_bytes.free);
100: mpool_statistics(mp, show);
101:
1.5 ! misho 102: printf(">>> alloc new 4000\n");
1.1 misho 103: addr = mpool_malloc(mp, 4000, "mdaaa 4000");
104: if (!addr) {
105: printf("Error:: #%d - %s\n", elwix_GetErrno(), elwix_GetError());
106: mpool_destroy(&mp);
107: return 2;
108: }
109: printf("addr=%p chkaddr=%d addr_size=%d\n", addr, mpool_chkaddr(addr), mpool_getsizebyaddr(addr));
1.5 ! misho 110: printf(">>> and free new 4000 addr=%p\n", addr);
1.1 misho 111: mpool_free(mp, addr, 0);
112:
113: printf("4) calls.act=%lu calls.inact=%lu calls.free=%lu; bytes.act=%lu bytes.inact=%lu bytes.free=%lu;\n",
114: mp->pool_calls.alloc, mp->pool_calls.cache, mp->pool_calls.free,
115: mp->pool_bytes.alloc, mp->pool_bytes.cache, mp->pool_bytes.free);
116: mpool_statistics(mp, show);
1.5 ! misho 117:
! 118:
! 119: printf("realloc 4000 to 100000 and to 2000000\n");
! 120: addr = mpool_malloc(mp, 4000, "mdaaa 4000");
! 121: printf("malloc addr=%p chkaddr=%d addr_size=%d\n", addr, mpool_chkaddr(addr), mpool_getsizebyaddr(addr));
! 122: addr = mpool_realloc(mp, addr, 100000, "ohche");
! 123: printf("realloc addr=%p chkaddr=%d addr_size=%d\n", addr, mpool_chkaddr(addr), mpool_getsizebyaddr(addr));
! 124: addr = mpool_realloc(mp, addr, 2000000, NULL);
! 125: printf("realloc addr=%p chkaddr=%d addr_size=%d\n", addr, mpool_chkaddr(addr), mpool_getsizebyaddr(addr));
! 126: mpool_free(mp, addr, 0);
! 127: printf("5) calls.act=%lu calls.inact=%lu calls.free=%lu; bytes.act=%lu bytes.inact=%lu bytes.free=%lu;\n",
! 128: mp->pool_calls.alloc, mp->pool_calls.cache, mp->pool_calls.free,
! 129: mp->pool_bytes.alloc, mp->pool_bytes.cache, mp->pool_bytes.free);
! 130: mpool_statistics(mp, show);
! 131:
! 132:
! 133: mpool_dump(mp, NULL);
1.1 misho 134: mpool_destroy(&mp);
135: return 0;
136: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>