2 static void print_region_list(void);
3 static int check_block_list(queue_entry * block_list, malloc_block * new_block);
7 void print_region_list(void) {
9 malloc_region * cur_region;
11 cur_region = (malloc_region *)&malloc_region_list;
12 printf("First region:\n");
13 printf("curr: 0x%8x prev: 0x%8x next: 0x%8x\n",
14 (unsigned int)cur_region,
15 (unsigned int)(cur_region->links.prev),
16 (unsigned int)(cur_region->links.next));
18 printf("Region list contents:\n");
21 queue_iterate(&malloc_region_list, cur_region, malloc_region *, links) {
22 if (i > num_regions) {
25 printf("curr: 0x%8x prev: 0x%8x next: 0x%8x\n",
26 (unsigned int)cur_region,
27 (unsigned int)(cur_region->links.prev),
28 (unsigned int)(cur_region->links.next));
34 void print_block_list(queue_entry * block_list) {
35 malloc_block * cur_block;
37 queue_iterate(block_list, cur_block, malloc_block *, links) {
38 printf("curr: 0x%8x prev: 0x%8x next: 0x%8x\n",
39 (unsigned int)cur_block,
40 (unsigned int)(cur_block->links.prev),
41 (unsigned int)(cur_block->links.next));
46 int break_here(void) {
51 int check_block_list(queue_entry * block_list, malloc_block * new_block) {
52 void * end_of_new_block;
53 malloc_block * cur_block;
56 end_of_new_block = new_block + sizeof(malloc_block);
58 queue_iterate(block_list, cur_block, malloc_block *, links) {
59 malloc_region * cur_region;
64 cur_region = cur_block->region;
65 end_of_region = cur_region + cur_region->region_size;
66 scratch_block = cur_block;
67 end_of_block = scratch_block + sizeof(malloc_block);
69 if ( ((void *)new_block >= scratch_block && (void *)new_block <= end_of_block) ||
70 (end_of_new_block >= scratch_block && end_of_new_block <= end_of_block) ||
71 (scratch_block >= (void *)new_block && scratch_block <= end_of_new_block) ||
72 (end_of_block >= (void *)new_block && end_of_block <= end_of_new_block) ) {
74 printf("New block %p overlaps existing block %p.\n",
75 new_block, scratch_block);
82 if (scratch_block < (void *)cur_region ||
83 end_of_block >= end_of_region) {
85 printf("Found invalid block link at block %d.\n", i);
86 printf("curr: 0x%8x prev: 0x%8x next: 0x%8x\n",
87 (unsigned int)cur_block,
88 (unsigned int)(cur_block->links.prev),
89 (unsigned int)(cur_block->links.next));
95 scratch_block = (malloc_block *)cur_block->links.prev;
96 end_of_block = scratch_block + sizeof(malloc_block);
98 if (scratch_block < (void *)cur_region ||
99 end_of_block >= end_of_region) {
101 printf("Found invalid block link at block %d.\n", i);
102 printf("curr: 0x%8x prev: 0x%8x next: 0x%8x\n",
103 (unsigned int)cur_block,
104 (unsigned int)(cur_block->links.prev),
105 (unsigned int)(cur_block->links.next));
111 scratch_block = (malloc_block *)cur_block->links.next;
112 end_of_block = scratch_block + sizeof(malloc_block);
114 if (scratch_block < (void *)cur_region ||
115 end_of_block >= end_of_region) {
116 printf("Found invalid block link at block %d.\n", i);
118 printf("curr: 0x%8x prev: 0x%8x next: 0x%8x\n",
119 (unsigned int)cur_block,
120 (unsigned int)(cur_block->links.prev),
121 (unsigned int)(cur_block->links.next));
133 int malloc_sanity_check(void) {
135 malloc_region * cur_region;
138 queue_iterate(&malloc_region_list, cur_region, malloc_region *, links) {
139 if (i > num_regions) {
142 if (cur_region->links.next != &malloc_region_list &&
143 cur_region->links.next < (queue_entry *)cur_region) {
144 printf("inconsistency detected\n");
153 /*********************************************************************
156 * Returns the maximum amount of memory ever reserved by this package.
157 *********************************************************************/
158 size_t malloc_hiwat() {
159 return malloc_hiwater_mark;
162 void malloc_clear_hiwat(void) {
163 malloc_hiwater_mark = 0;
167 size_t malloc_current_usage(void)
169 return current_block_total;
172 size_t malloc_region_usage(void) {
174 malloc_region * cur_region;
176 queue_iterate(&malloc_region_list, cur_region, malloc_region *, links) {
177 total += cur_region->region_size - sizeof(malloc_region);
184 double malloc_peak_usage(void)
189 double malloc_min_usage(void)
194 size_t malloc_unused(void) {
196 malloc_region * cur_region;
197 malloc_block * cur_block;
199 queue_iterate(&malloc_region_list, cur_region, malloc_region *, links) {
200 total += cur_region->free_size;
203 queue_iterate(&sorted_free_block_list, cur_block, malloc_block *, links) {
204 total += cur_block->block_size;
210 double malloc_current_efficiency(void)
212 double efficiency = 0.0;
213 double total_block_size = 0;
214 double total_request_size = 0;
215 unsigned long total_block_sizeL = 0;
216 unsigned long total_request_sizeL = 0;
218 size_t max_discrepancy = 0;
219 malloc_region * cur_region;
220 malloc_block * cur_block;
222 queue_iterate(&malloc_region_list, cur_region, malloc_region *, links) {
223 queue_iterate(&cur_region->block_list, cur_block, malloc_block *, links) {
224 size_t cur_block_size = cur_block->block_size - sizeof(malloc_block);
225 total_block_sizeL += cur_block_size;
226 total_request_sizeL += cur_block->request_size;
227 total_block_size += (double)cur_block_size;
228 total_request_size += (double)cur_block->request_size;
229 discrepancy = cur_block_size - cur_block->request_size;
230 if (discrepancy > max_discrepancy) {
231 max_discrepancy = discrepancy;
236 if (total_block_size > 0) {
237 efficiency = (double)total_request_size / (double)total_block_size;
242 printf("requested %.2f, actual %.2f\n", total_request_size, total_block_size);
243 printf("requested %ld, actual %ld\n", total_request_sizeL, total_block_sizeL);
244 printf("max discrepancy %ld\n", max_discrepancy);
250 /*********************************************************************
253 * Print stats on allocated regions and blocks.
254 *********************************************************************/
255 void malloc_report(void) {
256 malloc_region * cur_region;
257 malloc_block * cur_block;
258 size_t total_block_size;
260 queue_iterate(&malloc_region_list, cur_region, malloc_region *, links) {
262 printf("VM Region, size, free: ");
263 printf("%p, %d, %d\n", cur_region,
264 cur_region->region_size,
265 cur_region->free_size);
267 total_block_size = 0;
269 queue_iterate(&cur_region->block_list, cur_block, malloc_block *, links) {
271 total_block_size += cur_block->block_size;
272 printf(" Block address, size: %p, %ld (%ld)\n",
273 cur_block->buffer, cur_block->block_size,
274 cur_block->block_size - sizeof(malloc_block));
275 printf(" Block content: %s\n",
276 (char *)cur_block->buffer);
278 printf(" Total blocks size: %ld\n", total_block_size);
280 queue_iterate(&cur_region->free_list, cur_block, malloc_block *, links) {
282 total_block_size += cur_block->block_size;
283 printf(" Free block address, size: %p, %ld (%ld)\n",
284 cur_block->buffer, cur_block->block_size,
285 cur_block->block_size - sizeof(malloc_block));
290 printf("High water mark: %ld\n", malloc_hiwater_mark);
293 } /* malloc_report() */