Mercurial > repos > rhope
annotate runtime/fixed_alloc.c @ 101:f4fc0a98088a
Fixed some bugs that were preventing compiled compiler from working correctly. Need to address memory usage
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Mon, 09 Aug 2010 23:53:20 -0400 |
parents | e73a93fb5de1 |
children | 43cc42df26cc |
rev | line source |
---|---|
41
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1 #include "fixed_alloc.h" |
67 | 2 #include <stdlib.h> |
48
a24eb366195c
Fixed some bugs introduced in previous commit and moved definition of integer methods out of runtime and into the compiler
Mike Pavone <pavone@retrodev.com>
parents:
41
diff
changeset
|
3 #include <string.h> |
89
5a195ee08eac
Fix memory leak and bug that was preventing First@Dictionary from working properly
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
4 #include <stdio.h> |
41
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
5 |
92
e73a93fb5de1
Beginning of port of compiler to itself, some bugfixes and a refcount optimization
Mike Pavone <pavone@retrodev.com>
parents:
89
diff
changeset
|
6 uint16_t max_free[(MAX_SIZE-MIN_SIZE)/STRIDE+1]; |
41
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
7 |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
8 void fixed_alloc_init() |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
9 { |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
10 int i; |
92
e73a93fb5de1
Beginning of port of compiler to itself, some bugfixes and a refcount optimization
Mike Pavone <pavone@retrodev.com>
parents:
89
diff
changeset
|
11 for(i = 0; i < ((MAX_SIZE-MIN_SIZE)/STRIDE+1); ++i) |
41
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
12 max_free[i] = (BLOCK_SIZE - sizeof(mem_block)+1)*8/((i*STRIDE+MIN_SIZE)*8+1); |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
13 } |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
14 |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
15 mem_manager * new_mem_manager() |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
16 { |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
17 int i; |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
18 mem_manager * ret = malloc(sizeof(mem_manager)); |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
19 memset(ret, 0, sizeof(mem_manager)); |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
20 return ret; |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
21 } |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
22 |
89
5a195ee08eac
Fix memory leak and bug that was preventing First@Dictionary from working properly
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
23 void print_mem_info(mem_manager * manager) |
5a195ee08eac
Fix memory leak and bug that was preventing First@Dictionary from working properly
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
24 { |
5a195ee08eac
Fix memory leak and bug that was preventing First@Dictionary from working properly
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
25 int i,count,freeobjs; |
5a195ee08eac
Fix memory leak and bug that was preventing First@Dictionary from working properly
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
26 mem_block * cur; |
5a195ee08eac
Fix memory leak and bug that was preventing First@Dictionary from working properly
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
27 printf("Free blocks: %d\n", manager->freecount); |
5a195ee08eac
Fix memory leak and bug that was preventing First@Dictionary from working properly
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
28 printf("Full Blocks: %d\n", manager->fullcount); |
5a195ee08eac
Fix memory leak and bug that was preventing First@Dictionary from working properly
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
29 for (i = 0; i < (MAX_SIZE-MIN_SIZE)/STRIDE; i++) |
5a195ee08eac
Fix memory leak and bug that was preventing First@Dictionary from working properly
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
30 { |
5a195ee08eac
Fix memory leak and bug that was preventing First@Dictionary from working properly
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
31 count = 0; |
5a195ee08eac
Fix memory leak and bug that was preventing First@Dictionary from working properly
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
32 freeobjs = 0; |
5a195ee08eac
Fix memory leak and bug that was preventing First@Dictionary from working properly
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
33 cur = manager->inuse[i]; |
5a195ee08eac
Fix memory leak and bug that was preventing First@Dictionary from working properly
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
34 while(cur) |
5a195ee08eac
Fix memory leak and bug that was preventing First@Dictionary from working properly
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
35 { |
5a195ee08eac
Fix memory leak and bug that was preventing First@Dictionary from working properly
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
36 count++; |
5a195ee08eac
Fix memory leak and bug that was preventing First@Dictionary from working properly
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
37 freeobjs += ((int)cur->numfree); |
5a195ee08eac
Fix memory leak and bug that was preventing First@Dictionary from working properly
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
38 cur = cur->next; |
5a195ee08eac
Fix memory leak and bug that was preventing First@Dictionary from working properly
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
39 } |
5a195ee08eac
Fix memory leak and bug that was preventing First@Dictionary from working properly
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
40 printf("Bucket %d(size: %d) has %d blocks in use with %d total free slots\n", i, i*STRIDE+MIN_SIZE,count, freeobjs); |
5a195ee08eac
Fix memory leak and bug that was preventing First@Dictionary from working properly
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
41 } |
5a195ee08eac
Fix memory leak and bug that was preventing First@Dictionary from working properly
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
42 fflush(stdout); |
5a195ee08eac
Fix memory leak and bug that was preventing First@Dictionary from working properly
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
43 } |
5a195ee08eac
Fix memory leak and bug that was preventing First@Dictionary from working properly
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
44 |
41
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
45 void * falloc(size_t size, mem_manager * manager) |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
46 { |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
47 uint16_t i,bit; |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
48 uint16_t bucket; |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
49 mem_block * block,*temp; |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
50 if(size > MAX_SIZE) |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
51 return malloc(size); |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
52 //puts("falloc"); |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
53 size = ADJUST_SIZE(size); |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
54 bucket = (size-MIN_SIZE)/STRIDE; |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
55 block = manager->inuse[bucket]; |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
56 if(!block) |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
57 { |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
58 block = manager->freelist; |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
59 if(block) |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
60 { |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
61 --manager->freecount; |
67 | 62 manager->freelist = block->next; |
63 //memset(block, 0xCD, BLOCK_SIZE); | |
41
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
64 } |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
65 else |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
66 { |
67 | 67 block = block_alloc(BLOCK_SIZE); |
68 //memset(block, 0xAB, BLOCK_SIZE); | |
41
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
69 } |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
70 manager->inuse[bucket] = block; |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
71 block->next = NULL; |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
72 block->last = NULL; |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
73 block->numfree = max_free[bucket]; |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
74 block->firstfree = 0; |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
75 memset(block->bitmap, 0xFF, max_free[bucket]/8+1); |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
76 } |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
77 //printf("block: %X,numfree: %d, firstfree: %d, maxfree: %d\n", block, block->numfree, block->firstfree, max_free[bucket]); |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
78 /*if(block->numfree > max_free[bucket]) |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
79 { |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
80 puts("uh oh!"); |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
81 exit(1); |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
82 }*/ |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
83 //find first free |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
84 i = block->firstfree; |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
85 while(!block->bitmap[i]) |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
86 ++i; |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
87 //printf("i:%d,bitmap:%X\n", i, block->bitmap[i]); |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
88 bit = 0; |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
89 while(!((1 << bit) & block->bitmap[i])) |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
90 ++bit; |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
91 //update free bitmask |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
92 block->bitmap[i] ^= 1 << bit; |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
93 //If current bitmap has no more free elements, set firstfree to the next bitmap |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
94 if(!block->bitmap[i]) |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
95 block->firstfree = i+1; |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
96 else |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
97 block->firstfree = i; |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
98 --block->numfree; |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
99 if(!block->numfree) |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
100 { |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
101 //Remove from linked list if there are no more free elements |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
102 manager->inuse[bucket] = block->next; |
89
5a195ee08eac
Fix memory leak and bug that was preventing First@Dictionary from working properly
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
103 manager->fullcount++; |
41
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
104 if(block->next) |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
105 block->next->last = block->last; |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
106 } |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
107 i = i*8+bit; |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
108 return (void *)(((char *)block)+BLOCK_SIZE-((i+1)*size)); |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
109 } |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
110 |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
111 void ffree(void * ptr, size_t size, mem_manager * manager) |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
112 { |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
113 mem_block * block,*temp; |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
114 uint16_t i,bit,bucket; |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
115 if(size > MAX_SIZE) |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
116 { |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
117 free(ptr); |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
118 return; |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
119 } |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
120 //puts("ffree"); |
67 | 121 size = ADJUST_SIZE(size); |
122 //memset(ptr, 0xEF, size); | |
41
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
123 block = GET_BLOCK(ptr); |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
124 i = (((((char *)block) + BLOCK_SIZE) - ((char *)ptr))/size)-1; |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
125 bit = i & 0x7; |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
126 i = (i&0xFFFFFFF8) >> 3; |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
127 //printf("ptr:%X,block:%X,i:%d,bit:%d\n", ptr,block,bit,i); |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
128 block->bitmap[i] |= 1 << bit; |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
129 if(i < block->firstfree) |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
130 block->firstfree = i; |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
131 ++block->numfree; |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
132 bucket = (size-MIN_SIZE)/STRIDE; |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
133 //printf("numfree:%d,max_free:%d,last:%X,next:%X\n", block->numfree, max_free[bucket], block->last, block->next); |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
134 /*if(block->numfree > max_free[bucket]) |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
135 { |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
136 puts("uh oh!"); |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
137 exit(1); |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
138 }*/ |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
139 if(block->numfree == max_free[bucket]) |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
140 { |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
141 //Block is now unused, remove it from the inuse list |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
142 if(block->last) |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
143 block->last->next = block->next; |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
144 else |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
145 manager->inuse[bucket] = block->next; |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
146 if(block->next) |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
147 block->next->last = block->last; |
67 | 148 if(manager->freecount == MAX_FREE) |
49
3e20ed8959c4
Added initial FFI implementation, Array type and 64-bit integers
Mike Pavone <pavone@retrodev.com>
parents:
48
diff
changeset
|
149 { |
67 | 150 block_free(block, BLOCK_SIZE); |
49
3e20ed8959c4
Added initial FFI implementation, Array type and 64-bit integers
Mike Pavone <pavone@retrodev.com>
parents:
48
diff
changeset
|
151 } |
41
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
152 else |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
153 { |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
154 block->next = manager->freelist; |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
155 manager->freelist = block; |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
156 ++manager->freecount; |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
157 } |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
158 } else if(block->numfree == 1) { |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
159 //Block was previously full, add it to the inuse list |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
160 block->next = manager->inuse[bucket]; |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
161 block->last = NULL; |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
162 manager->inuse[bucket] = block; |
89
5a195ee08eac
Fix memory leak and bug that was preventing First@Dictionary from working properly
Mike Pavone <pavone@retrodev.com>
parents:
67
diff
changeset
|
163 manager->fullcount--; |
41
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
164 if(block->next) |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
165 block->next->last = block; |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
166 } else if(block->next && block->next->numfree < block->numfree) { |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
167 //We want to use more filled blockes before less filled ones |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
168 //so we can return empty ones to the OS more often |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
169 //so if we now have more free nodes in this block than the |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
170 //next one swap them |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
171 temp = block->next; |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
172 block->next = temp->next; |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
173 temp->last = block->last; |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
174 block->last = temp; |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
175 temp->next = block; |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
176 if(block->next) |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
177 block->next->last = block; |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
178 if(temp->last) |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
179 temp->last->next = temp; |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
180 else |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
181 manager->inuse[bucket] = temp; |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
182 } |
1b86a1ee500a
Added faster allocator for small objects
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
183 } |