comparison runtime/fixed_alloc.c @ 89:5a195ee08eac

Fix memory leak and bug that was preventing First@Dictionary from working properly
author Mike Pavone <pavone@retrodev.com>
date Sat, 31 Jul 2010 00:19:15 -0400
parents d1569087348f
children e73a93fb5de1
comparison
equal deleted inserted replaced
88:f69987c58fa8 89:5a195ee08eac
1 #include "fixed_alloc.h" 1 #include "fixed_alloc.h"
2 #include <stdlib.h> 2 #include <stdlib.h>
3 #include <string.h> 3 #include <string.h>
4 #include <stdio.h>
4 5
5 uint16_t max_free[(MAX_SIZE-MIN_SIZE)/STRIDE]; 6 uint16_t max_free[(MAX_SIZE-MIN_SIZE)/STRIDE];
6 7
7 void fixed_alloc_init() 8 void fixed_alloc_init()
8 { 9 {
15 { 16 {
16 int i; 17 int i;
17 mem_manager * ret = malloc(sizeof(mem_manager)); 18 mem_manager * ret = malloc(sizeof(mem_manager));
18 memset(ret, 0, sizeof(mem_manager)); 19 memset(ret, 0, sizeof(mem_manager));
19 return ret; 20 return ret;
21 }
22
23 void print_mem_info(mem_manager * manager)
24 {
25 int i,count,freeobjs;
26 mem_block * cur;
27 printf("Free blocks: %d\n", manager->freecount);
28 printf("Full Blocks: %d\n", manager->fullcount);
29 for (i = 0; i < (MAX_SIZE-MIN_SIZE)/STRIDE; i++)
30 {
31 count = 0;
32 freeobjs = 0;
33 cur = manager->inuse[i];
34 while(cur)
35 {
36 count++;
37 freeobjs += ((int)cur->numfree);
38 cur = cur->next;
39 }
40 printf("Bucket %d(size: %d) has %d blocks in use with %d total free slots\n", i, i*STRIDE+MIN_SIZE,count, freeobjs);
41 }
42 fflush(stdout);
20 } 43 }
21 44
22 void * falloc(size_t size, mem_manager * manager) 45 void * falloc(size_t size, mem_manager * manager)
23 { 46 {
24 uint16_t i,bit; 47 uint16_t i,bit;
75 --block->numfree; 98 --block->numfree;
76 if(!block->numfree) 99 if(!block->numfree)
77 { 100 {
78 //Remove from linked list if there are no more free elements 101 //Remove from linked list if there are no more free elements
79 manager->inuse[bucket] = block->next; 102 manager->inuse[bucket] = block->next;
103 manager->fullcount++;
80 if(block->next) 104 if(block->next)
81 block->next->last = block->last; 105 block->next->last = block->last;
82 } 106 }
83 i = i*8+bit; 107 i = i*8+bit;
84 return (void *)(((char *)block)+BLOCK_SIZE-((i+1)*size)); 108 return (void *)(((char *)block)+BLOCK_SIZE-((i+1)*size));
134 } else if(block->numfree == 1) { 158 } else if(block->numfree == 1) {
135 //Block was previously full, add it to the inuse list 159 //Block was previously full, add it to the inuse list
136 block->next = manager->inuse[bucket]; 160 block->next = manager->inuse[bucket];
137 block->last = NULL; 161 block->last = NULL;
138 manager->inuse[bucket] = block; 162 manager->inuse[bucket] = block;
163 manager->fullcount--;
139 if(block->next) 164 if(block->next)
140 block->next->last = block; 165 block->next->last = block;
141 } else if(block->next && block->next->numfree < block->numfree) { 166 } else if(block->next && block->next->numfree < block->numfree) {
142 //We want to use more filled blockes before less filled ones 167 //We want to use more filled blockes before less filled ones
143 //so we can return empty ones to the OS more often 168 //so we can return empty ones to the OS more often