1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13:
14:
15: 16: 17: 18: 19: 20: 21: 22:
23:
24: #include "mem.h"
25: #include <sys/syslog.h>
26:
27: 28: 29: 30: 31: 32:
33: LOCAL BOOL chkalloc( void *ptr, int mode, MACB *macb )
34: {
35: QUEUE *aq, *nq;
36: size_t usesz = 0, fresz = 0, sz;
37: int usebk = 0, frebk = 0, npage = 0;
38: BOOL newpg, ptr_ok;
39:
40:
41: newpg = TRUE;
42: ptr_ok = ( ptr == NULL )? TRUE: FALSE;
43: for ( aq = macb->areaque.next; aq != &macb->areaque; aq = aq->next ) {
44:
45: if ( newpg && !chkAreaFlag(aq, AREA_TOP) ) {
46: goto err_found;
47: }
48:
49: if ( chkAreaFlag(aq, AREA_END) ) {
50: if ( newpg ) {
51: goto err_found;
52: }
53: newpg = TRUE;
54: fresz += sizeof(QUEUE);
55: npage++;
56: continue;
57: }
58: newpg = FALSE;
59:
60: nq = aq->next;
61: if ( Mask(aq->next) != nq || nq <= aq || Mask(nq->prev) != aq ) {
62: goto err_found;
63: }
64: sz = (size_t)((VB*)nq - (VB*)aq);
65: if ( sz < sizeof(QUEUE)*3 ) {
66: goto err_found;
67: }
68:
69: if ( chkAreaFlag(aq, AREA_USE) ) {
70: usesz += sz;
71: ++usebk;
72: if ( ptr == (void*)(aq+1) ) {
73: ptr_ok = TRUE;
74: }
75: if ( mode < -1 ) {
76: syslog(LOG_NOTICE, "malloc ptr: 0x%08x [%d B]",
77: aq+1, AreaSize(aq));
78: }
79: } else {
80: fresz += sz;
81: ++frebk;
82: }
83: }
84: if ( !newpg ) {
85: goto err_found;
86: }
87:
88: if ( !ptr_ok ) {
89: syslog(LOG_ERR, "MALLOC: illegal ptr: 0x%08x", ptr);
90: return FALSE;
91: }
92:
93: if ( mode < 0 ) {
94: syslog(LOG_NOTICE,
95: "MALLOC: %d pages, used: %d [%d blks] free: %d [%d blks]",
96: npage, usesz, usebk, fresz, frebk);
97: }
98:
99: return TRUE;
100:
101: err_found:
102: syslog(LOG_ERR, "MALLOC: block corrupted at 0x%08x", aq);
103: return FALSE;
104: }
105:
106:
107:
108: 109: 110: 111: 112: 113: 114:
115: EXPORT void _mem_malloctest( int mode, MACB *_macb )
116: {
117: MACB *macb = AlignMACB(_macb);
118:
119: if ( mode >= 0 ) {
120:
121: macb->testmode = mode;
122: _mem_chkalloc = chkalloc;
123: } else {
124:
125: chkalloc(NULL, mode, macb);
126: }
127: }
128:
129: 130: 131: 132: 133: 134: 135: 136:
137: EXPORT BOOL _mem_malloccheck( void *ptr, MACB *_macb )
138: {
139: MACB *macb = AlignMACB(_macb);
140:
141: return chkalloc(ptr, 0, macb);
142: }