1 | #include <time.h> |
---|
2 | #include <string.h> |
---|
3 | #include <mylimits.h> |
---|
4 | |
---|
5 | #define TRACK_LEVEL 1 |
---|
6 | #define CHECK_LEVEL 1 |
---|
7 | // keep every ith address: define to 0 if no keeping |
---|
8 | #define KEEP_LEVEL 20 |
---|
9 | // #define KEEP_LEVEL 0 |
---|
10 | |
---|
11 | // #define MAX_CELLS 500000 |
---|
12 | // #define MAX_CELLS 100000 |
---|
13 | #define MAX_CELLS 100000 |
---|
14 | #define KEEP_ADDR 100 |
---|
15 | // #define KEEP_ADDR 0 |
---|
16 | #define END_CHECK_LEVEL 5 |
---|
17 | |
---|
18 | #ifdef OM_TEST_MALLOC |
---|
19 | #define OM_EMULATE_OMALLOC |
---|
20 | #endif |
---|
21 | |
---|
22 | |
---|
23 | #include <omalloc/omStructs.h> |
---|
24 | |
---|
25 | struct omMemCell_s |
---|
26 | { |
---|
27 | void* addr; |
---|
28 | omBin bin; |
---|
29 | unsigned long spec; |
---|
30 | omBin orig_bin; |
---|
31 | }; |
---|
32 | |
---|
33 | typedef struct omMemCell_s omMemCell_t; |
---|
34 | typedef omMemCell_t* omMemCell; |
---|
35 | |
---|
36 | extern omMemCell_t cells[]; |
---|
37 | void TestAlloc(omMemCell cell, unsigned long spec); |
---|
38 | void TestRealloc(omMemCell cell, unsigned long spec); |
---|
39 | void TestFree(omMemCell cell); |
---|
40 | |
---|
41 | #if CHECK_LEVEL > 2 |
---|
42 | #define myprintf(format, args...) \ |
---|
43 | printf(format, ## args) |
---|
44 | #define myfflush(what) fflush(what) |
---|
45 | #else |
---|
46 | #define myprintf(format, args...) do {} while (0) |
---|
47 | #define myfflush(what) do {} while (0) |
---|
48 | #endif |
---|
49 | |
---|
50 | #define IS_STICKY_BIN(spec) (spec & 1) |
---|
51 | // #define IS_STICKY_BIN(spec) (0) |
---|
52 | #define GET_SIZE(spec) (spec & ((((unsigned long) 1) << 14) -1)) |
---|
53 | #define SET_SIZE(spec, size) spec = ((spec & ~((((unsigned long) 1) << 14) -1)) | (size)) |
---|
54 | #define IS_ALIGNED(spec) (spec & (((unsigned long) 1) << 15)) |
---|
55 | #define IS_ZERO(spec) (spec & (((unsigned long) 1) << 16)) |
---|
56 | #define IS_BIN(spec) (spec & (((unsigned long) 1) << 17)) |
---|
57 | #define IS_SPEC_BIN(spec) (spec & (((unsigned long) 1) << 18)) |
---|
58 | #define IS_INLINE(spec) (spec & (((unsigned long) 1) << 19)) |
---|
59 | #define DO_FREE(spec) (!(spec & (((unsigned long) 1) << 20)) && !(spec & (((unsigned long) 1) << 21))) |
---|
60 | #define DO_REALLOC(spec) ((spec & (((unsigned long) 1) << 20)) && (spec & (((unsigned long) 1) << 21))) |
---|
61 | #define DO_DUP(spec) ((spec & (((unsigned long) 1) << 20)) && ! (spec & (((unsigned long) 1) << 21))) |
---|
62 | #if CHECK_LEVEL > 0 |
---|
63 | //#define DO_CHECK(spec) 1 |
---|
64 | #define DO_CHECK(spec) (spec & (((unsigned long) 1) << 22)) |
---|
65 | #define DO_FREE_CHECK(spec) (spec & (((unsigned long) 1) << 23)) |
---|
66 | #else |
---|
67 | #define DO_CHECK(spec) 0 |
---|
68 | #define DO_FREE_CHECK(spec) 0 |
---|
69 | #endif |
---|
70 | #if CHECK_LEVEL > 0 && TRACK_LEVEL > 0 |
---|
71 | #define DO_TRACK(spec) (spec & (((unsigned long) 1) << 24)) |
---|
72 | #define GET_TRACK(spec) (((spec & ((((unsigned long) 1) << 27) | (((unsigned long) 1) << 26) | (((unsigned long) 1) << 25))) >> 25) % 5) + TRACK_LEVEL |
---|
73 | // #define DO_TRACK(spec) TRACK_LEVEL |
---|
74 | // #define GET_TRACK(spec) TRACK_LEVEL |
---|
75 | #else |
---|
76 | #define DO_TRACK(spec) 0 |
---|
77 | #define GET_TRACK(spec) 0 |
---|
78 | #endif |
---|
79 | #if KEEP_LEVEL > 0 |
---|
80 | #define DO_KEEP(spec) (DO_CHECK(spec) && (spec % KEEP_LEVEL == 0)) |
---|
81 | #define DO_FREE_KEEP(spec) (DO_FREE_CHECK(spec) && (spec % KEEP_LEVEL == 0)) |
---|
82 | #else |
---|
83 | #define DO_KEEP(spec) 0 |
---|
84 | #define DO_FREE_KEEP(spec) 0 |
---|
85 | #endif |
---|
86 | |
---|
87 | #define IS_FREE_SIZE(spec) (spec & (((unsigned long) 1) << 28)) |
---|
88 | #define IS_FREE_BIN(spec) (spec & (((unsigned long) 1) << 29)) |
---|
89 | #define IS_SLOPPY(spec) (spec & (((unsigned long) 1) << 30)) |
---|
90 | #define IS_FREE_BINADDR(spec) (spec & (((unsigned long) 1) << 31)) |
---|
91 | |
---|
92 | |
---|
93 | #define SPEC_MAX ULONG_MAX |
---|
94 | #define SIZE_MAX ((((unsigned long) 1) << 14) -1) |
---|
95 | #define RANGE_MIN (((unsigned long) 1) << 6) |
---|
96 | #define RANGE_MAX (((unsigned long) 1) << 14) |
---|
97 | |
---|
98 | #define PAGES_PER_REGION 128 |
---|
99 | |
---|
100 | void omtTestAlloc(omMemCell cell, unsigned long spec); |
---|
101 | void omtTestRealloc(omMemCell cell, unsigned long spec); |
---|
102 | void omtTestDup(omMemCell cell, unsigned long spec); |
---|
103 | void omtTestFree(omMemCell cell); |
---|
104 | |
---|
105 | void omtTestAllocDebug(omMemCell cell, unsigned long spec); |
---|
106 | void omtTestReallocDebug(omMemCell cell, unsigned long spec); |
---|
107 | void omtTestDupDebug(omMemCell cell, unsigned long spec); |
---|
108 | void omtTestFreeDebug(omMemCell cell); |
---|
109 | |
---|
110 | void omtTestAllocKeep(omMemCell cell, unsigned long spec); |
---|
111 | void omtTestReallocKeep(omMemCell cell, unsigned long spec); |
---|
112 | void omtTestDupKeep(omMemCell cell, unsigned long spec); |
---|
113 | void omtTestFreeKeep(omMemCell cell); |
---|
114 | |
---|
115 | void InitCellAddrContent(omMemCell cell); |
---|
116 | int omtTestErrors(); |
---|
117 | omBin omtGetStickyBin(omBin bin); |
---|
118 | |
---|
119 | #if CHECK_LEVEL > 0 |
---|
120 | void omtTestDebug(omMemCell cell); |
---|
121 | void TestAddrContent(void* addr, unsigned long value, size_t size); |
---|
122 | void TestAddrContentEqual(void* s1, void* s2, size_t size); |
---|
123 | #else |
---|
124 | #define omtTestDebug(cell) do {} while (0) |
---|
125 | #define TestAddrContent(a,v,s) do {} while (0) |
---|
126 | #define TestAddrContentEqual(s1, s2, s) do {} while (0) |
---|
127 | #endif |
---|