File:
[ELWIX - Embedded LightWeight unIX -] /
libelwix /
example /
test_mem.c
Revision
1.7:
download - view:
text,
annotated -
select for diffs -
revision graph
Wed Aug 14 16:05:42 2024 UTC (6 months, 1 week ago) by
misho
Branches:
MAIN
CVS tags:
elwix6_7,
elwix6_6,
elwix6_5,
elwix6_4,
elwix6_3,
elwix6_2,
elwix6_1,
HEAD,
ELWIX6_6,
ELWIX6_5,
ELWIX6_4,
ELWIX6_2,
ELWIX6_1,
ELWIX6_0
Version 6.0
Changelog:
- New calloc() wrapper
- New option --enable-mpool-mem-zero
1: #include <stdio.h>
2: #include <string.h>
3: #include <stdlib.h>
4: #include <elwix.h>
5:
6:
7: int show(unsigned int size, unsigned int act, unsigned int inact, void *data, unsigned int dlen)
8: {
9: if (!act && !inact)
10: return -1;
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: return 0;
20: }
21:
22: int
23: main(int argc, char **argv)
24: {
25: mpool_t *mp;
26: void *addr;
27: int i;
28: u_long curr, real;
29:
30: printf("whether default memory mapper is elwix? %d\n", elwix_mm_inuse());
31:
32: for (i = 0; i < 3; i++) {
33: addr = malloc(4000);
34: printf("addr=%p\n", addr);
35: free(addr);
36: }
37:
38: mp = mpool_init(0);
39: if (!mp) {
40: printf("Error:: #%d - %s\n", elwix_GetErrno(), elwix_GetError());
41: return 1;
42: }
43: /*
44: mpool_getquota(mp, &curr, NULL, NULL);
45: printf("___current=%lu\n", curr);
46: */
47:
48: printf(">>> get 3 allocs\n");
49: for (i = 0; i < 3; i++) {
50: addr = mpool_malloc(mp, 4000, "mdaaa 4000");
51: if (!addr) {
52: printf("Error:: #%d - %s\n", elwix_GetErrno(), elwix_GetError());
53: mpool_destroy(&mp);
54: return 2;
55: }
56: printf("addr=%p chkaddr=%d addr_size=%d\n", addr, mpool_chkaddr(addr), mpool_getsizebyaddr(addr));
57:
58: strlcpy(addr, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\ndddddd\n",
59: mpool_getsizebyaddr(addr));
60: mpool_free(mp, addr, 0);
61: }
62:
63: mpool_getquota(mp, &curr, &real, NULL);
64: printf("___current=%lu ___real=%lu\n", curr, real);
65:
66: printf("0) calls.act=%lu calls.inact=%lu calls.free=%lu; bytes.act=%lu bytes.inact=%lu bytes.free=%lu;\n",
67: mp->pool_calls.alloc, mp->pool_calls.cache, mp->pool_calls.free,
68: mp->pool_bytes.alloc, mp->pool_bytes.cache, mp->pool_bytes.free);
69: mpool_statistics(mp, show, NULL, 0);
70:
71: printf(">>> realloc 4000 to 5010 addr=%p\n", addr);
72: addr = mpool_realloc(mp, addr, 5010, "|||||||||||||");
73: if (!addr)
74: printf("Error:: #%d - %s\n", elwix_GetErrno(), elwix_GetError());
75: mpool_getquota(mp, &curr, &real, NULL);
76: printf("addr=%p_current=%lu_real=%lu\n", addr, curr, real);
77:
78: printf("1) calls.act=%lu calls.inact=%lu calls.free=%lu; bytes.act=%lu bytes.inact=%lu bytes.free=%lu;\n",
79: mp->pool_calls.alloc, mp->pool_calls.cache, mp->pool_calls.free,
80: mp->pool_bytes.alloc, mp->pool_bytes.cache, mp->pool_bytes.free);
81: mpool_statistics(mp, show, NULL, 0);
82:
83:
84: printf(">>> free addr=%p\n", addr);
85: if (addr)
86: mpool_free(mp, addr, 0);
87:
88: printf("2) calls.act=%lu calls.inact=%lu calls.free=%lu; bytes.act=%lu bytes.inact=%lu bytes.free=%lu;\n",
89: mp->pool_calls.alloc, mp->pool_calls.cache, mp->pool_calls.free,
90: mp->pool_bytes.alloc, mp->pool_bytes.cache, mp->pool_bytes.free);
91: mpool_statistics(mp, show, NULL, 0);
92:
93: printf(">>> purge inactive memory\n");
94: mpool_purge(mp, 0);
95:
96: mpool_getquota(mp, &curr, &real, NULL);
97: printf("___current=%lu ___real=%lu\n", curr, real);
98:
99: printf("3) calls.act=%lu calls.inact=%lu calls.free=%lu; bytes.act=%lu bytes.inact=%lu bytes.free=%lu;\n",
100: mp->pool_calls.alloc, mp->pool_calls.cache, mp->pool_calls.free,
101: mp->pool_bytes.alloc, mp->pool_bytes.cache, mp->pool_bytes.free);
102: mpool_statistics(mp, show, NULL, 0);
103:
104: printf(">>> alloc new 4000\n");
105: addr = mpool_malloc(mp, 4000, "mdaaa 4000");
106: if (!addr) {
107: printf("Error:: #%d - %s\n", elwix_GetErrno(), elwix_GetError());
108: mpool_destroy(&mp);
109: return 2;
110: }
111: printf("addr=%p chkaddr=%d addr_size=%d\n", addr, mpool_chkaddr(addr), mpool_getsizebyaddr(addr));
112: printf(">>> and free new 4000 addr=%p\n", addr);
113: mpool_free(mp, addr, 0);
114:
115: printf("4) calls.act=%lu calls.inact=%lu calls.free=%lu; bytes.act=%lu bytes.inact=%lu bytes.free=%lu;\n",
116: mp->pool_calls.alloc, mp->pool_calls.cache, mp->pool_calls.free,
117: mp->pool_bytes.alloc, mp->pool_bytes.cache, mp->pool_bytes.free);
118: mpool_statistics(mp, show, NULL, 0);
119:
120:
121: printf("realloc 4000 to 100000 and to 2000000\n");
122: addr = mpool_malloc(mp, 4000, "mdaaa 4000");
123: printf("malloc addr=%p chkaddr=%d addr_size=%d\n", addr, mpool_chkaddr(addr), mpool_getsizebyaddr(addr));
124: addr = mpool_realloc(mp, addr, 100000, "ohche");
125: printf("realloc addr=%p chkaddr=%d addr_size=%d\n", addr, mpool_chkaddr(addr), mpool_getsizebyaddr(addr));
126: addr = mpool_realloc(mp, addr, 2000000, NULL);
127: printf("realloc addr=%p chkaddr=%d addr_size=%d\n", addr, mpool_chkaddr(addr), mpool_getsizebyaddr(addr));
128: mpool_free(mp, addr, 0);
129: printf("5) calls.act=%lu calls.inact=%lu calls.free=%lu; bytes.act=%lu bytes.inact=%lu bytes.free=%lu;\n",
130: mp->pool_calls.alloc, mp->pool_calls.cache, mp->pool_calls.free,
131: mp->pool_bytes.alloc, mp->pool_bytes.cache, mp->pool_bytes.free);
132: mpool_statistics(mp, show, NULL, 0);
133:
134: addr = mpool_calloc(mp, 56, 1, NULL);
135: printf("calloc addr=%p chkaddr=%d addr_size=%d\n", addr, mpool_chkaddr(addr), mpool_getsizebyaddr(addr));
136: mpool_free(mp, addr, 0);
137: addr = mpool_calloc(mp, 60, 1, "alabala");
138: printf("calloc addr=%p chkaddr=%d addr_size=%d\n", addr, mpool_chkaddr(addr), mpool_getsizebyaddr(addr));
139: mpool_free(mp, addr, 0);
140:
141: mpool_dump(mp, NULL);
142: mpool_destroy(&mp);
143: return 0;
144: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>