22
33import myworld .hummingbird .HummingbirdVM ;
44
5+ import java .util .Arrays ;
6+
57import static myworld .hummingbird .HummingbirdVM .NULL ;
68
79/**
@@ -137,7 +139,6 @@ public Allocator(HummingbirdVM vm, int baseAddress, int initialSize){
137139
138140 hvm = vm ;
139141 head = baseAddress ;
140- tail = head ;
141142 Header = new HeaderStruct (vm );
142143
143144 this .initialSize = initialSize ;
@@ -207,13 +208,18 @@ public synchronized int malloc(int nbytes){
207208
208209 for (var block = Header .nextBlock (head ); block != NULL ; previousBlock = block , block = Header .nextBlock (block )){
209210
210- if (Header .size (block ) == nbytes && !(previousBlock == head && Header . nextBlock ( block ) == NULL )){
211+ if (Header .size (block ) == nbytes && !(previousBlock == head && block == tail )){
211212 // Exact match - don't resize, just join the preceding and trailing blocks.
212213 // Can't do this on the head block because there is no prior block to work with.
213214
214215 Header .nextBlock (previousBlock , Header .nextBlock (block ));
215216 Header .nextBlock (block , NULL );
216217
218+ System .out .println ("Exact malloc(%d): %s (tail is %s (%d))" .formatted (nbytes , Pointer .toString (block ), Pointer .toString (tail ), Header .size (tail )));
219+ if (block == tail ){
220+ System .out .println ("Did exact allocation on tail. Prior block is %s, prior's new next is %s" .formatted (Pointer .toString (previousBlock ), Pointer .toString (Header .nextBlock (previousBlock ))));
221+ }
222+
217223 return block + Header .sizeOf ();
218224 }else if (Header .size (block ) >= nbytes + Header .sizeOf ()){
219225
@@ -232,9 +238,14 @@ public synchronized int malloc(int nbytes){
232238 if (block == tail ){
233239 // We just snagged the first bit of the tail block, so update
234240 // the tail to the new block
241+ if (newBlock == 452984836 ){
242+ System .out .println ("Found it" );
243+ new Exception ().printStackTrace ();
244+ }
235245 tail = newBlock ;
236246 Header .nextBlock (newBlock , NULL );
237247 }
248+ System .out .println ("Split malloc(%d): %s" .formatted (nbytes , Pointer .toString (block )));
238249 return block + Header .sizeOf ();
239250 }
240251
@@ -244,6 +255,7 @@ public synchronized int malloc(int nbytes){
244255 // to be large enough.
245256 if (block == tail ){
246257 block = morecore (nbytes );
258+ //System.out.println("New core ptr: " + Pointer.toString(block));
247259 if (block == NULL ){
248260 return NULL ;
249261 }
@@ -261,6 +273,10 @@ protected int freeInternal(int ptr){
261273 }
262274
263275 int hPtr = ptr - Header .sizeOf ();
276+ System .out .println ("free(%s)" .formatted (Pointer .toString (hPtr )));
277+ System .out .println ("Free blocks: " + Arrays .toString (Arrays .stream (freeBlocks ()).mapToObj (Pointer ::toString ).toArray ()));
278+ System .out .println ("Free block sizes: " + Arrays .toString (blockSizes ()));
279+ //new Exception().printStackTrace(System.out);
264280
265281 int block = head ;
266282 if (hPtr > tail ){
@@ -278,23 +294,38 @@ protected int freeInternal(int ptr){
278294 block = next ;
279295 }
280296 }
297+ System .out .println ("block: " + Pointer .toString (block ));
281298
282299 // Insert hPtr into the list
283300 // Note that when block == tail, nextBlock is NULL.
301+ if (block == tail ){
302+ System .out .println ("Freeing from tail (%s): next is (%s)" .formatted (Pointer .toString (tail ), Pointer .toString (Header .nextBlock (tail ))));
303+ }
284304 int nextBlock = Header .nextBlock (block );
285305
286306 Header .nextBlock (block , hPtr );
287307 Header .nextBlock (hPtr , nextBlock );
288308
289309 // Now merge (if able) block, hPtr, and the trailing block
290310 var mergedOrNext = mergeIfAble (block , hPtr );
311+ //System.out.println("Merge 1: %s (%d), next %s (%d)".formatted(Pointer.toString(mergedOrNext), Header.size(mergedOrNext),
312+ // Pointer.toString(nextBlock), Header.size(nextBlock)));
291313 if (nextBlock != NULL ){
292314 mergedOrNext = mergeIfAble (mergedOrNext , nextBlock );
315+ // System.out.println("Merge 2: %s (%d), next %s (%d)".formatted(Pointer.toString(mergedOrNext), Header.size(mergedOrNext),
316+ // Pointer.toString(Header.nextBlock(mergedOrNext)), Header.size(Header.nextBlock(nextBlock))));
293317 }
318+ //System.out.println("Finished applicable merges");
294319
295320 if (block == tail ){
296321 // Sets the tail to the merged block (if merge happened),
297322 // or leaves it pointing to the old tail (if merge didn't happen).
323+ //if(mergedOrNext == 452984832){
324+ //System.out.println("Setting new tail: Block: %s (%d), hPtr: %s (%d), tail: %s (%d)".formatted(Pointer.toString(block), Header.size(block), Pointer.toString(hPtr), Header.size(hPtr), Pointer.toString(tail), Header.size(tail)));
325+ //System.out.println("Merged ptr: " + Pointer.toString(mergedOrNext));
326+ //System.out.println("Tail next block: " + Pointer.toString(Header.nextBlock(tail)));
327+ //System.out.println("Head size: " + Header.size(head));
328+ //}
298329 tail = mergedOrNext ;
299330 Header .nextBlock (tail , NULL );
300331 }
@@ -330,16 +361,46 @@ public synchronized int[] blockSizes(){
330361 return sizes ;
331362 }
332363
364+ public synchronized int [] freeBlocks (){
365+ var blocks = countFreeBlocks ();
366+ var pointers = new int [blocks ];
367+ for (int i = 0 , block = head ; block != NULL ; block = Header .nextBlock (block ), i ++){
368+ pointers [i ] = block ;
369+ }
370+ return pointers ;
371+ }
372+
333373 private int mergeIfAble (int preceding , int trailing ){
374+ //if(preceding == 0x7E4 || trailing == 0x7E4 || preceding == 0x7EE || trailing == 0x7EE){
375+ //System.out.println("Merge: Preceding %s (%d), trailing %s (%d)".formatted(
376+ // Pointer.toString(preceding), Header.size(preceding),
377+ // Pointer.toString(trailing), Header.size(trailing)
378+ //));
379+ //}
334380 if (preceding == head ){
381+ System .out .println ("Preceding is head, not merging" );
335382 return trailing ; // Can't merge head
336383 }
337384 if (preceding + Header .sizeOf () + Header .size (preceding ) == trailing ){
338385 // Include the size of the header in the block being merged
339386 Header .size (preceding , Header .size (preceding ) + Header .size (trailing ) + Header .sizeOf ());
340387 Header .nextBlock (preceding , Header .nextBlock (trailing ));
388+ //if(preceding == 0x7E4 || trailing == 0x7E4){
389+ // System.out.println("Merged block: %s (%d), next %s (%d)".formatted(
390+ // Pointer.toString(preceding), Header.size(preceding),
391+ // Pointer.toString(Header.nextBlock(preceding)), Header.size(Header.nextBlock(preceding))
392+ // ));
393+ //}
394+ System .out .println ("Merged %s (%d)" .formatted (Pointer .toString (preceding ), Header .size (preceding )));
341395 return preceding ;
342396 }
397+ //if(preceding == 0x7E4 || trailing == 0x7E4){
398+ //System.out.println("Tail: " + Pointer.toString(tail));
399+ //System.out.println("Didn't merge, trailing block: %s (%d), next %s".formatted(
400+ // Pointer.toString(trailing), Header.size(trailing),
401+ // Pointer.toString(Header.nextBlock(trailing)))
402+ //);
403+ //}
343404 return trailing ;
344405 }
345406
@@ -364,13 +425,18 @@ public HeaderStruct(HummingbirdVM vm){
364425 }
365426
366427 public int nextBlock (int hPtr ){
367- return acc .readInt (hPtr , nextBlock );
428+ var ptr = acc .readInt (hPtr , nextBlock );
429+ if (ptr == 0x1B000000 ){
430+ System .out .println ("Found bad next read at nextBlock(%s)" .formatted (Pointer .toString (hPtr )));
431+ new Exception ().printStackTrace (System .out );
432+ }
433+ return ptr ;
368434 }
369435
370436 public void nextBlock (int hPtr , int ptr ){
371- if (hPtr == 8 && ptr == 0 ){
372- new Exception (). printStackTrace ( );
373- System . exit ( 0 );
437+ if (/* hPtr == 0x7EE &&*/ ptr == 0x1B000000 ){
438+ System . out . println ( "Found bad next write: " + ptr );
439+ new Exception (). printStackTrace ( System . out );
374440 }
375441 acc .writeInt (hPtr , Allocator .HeaderStruct .nextBlock , ptr );
376442 }
@@ -380,6 +446,13 @@ public int size(int hPtr){
380446 }
381447
382448 public void size (int hPtr , int size ){
449+ if (hPtr == 0x08 + 4 ){
450+ System .err .println ("Found bad write to head size: " + size );
451+ }
452+ /*if(hPtr == 0x7E4){
453+ System.err.println("Found write to tail: " + size);
454+ new Exception().printStackTrace();
455+ }*/
383456 acc .writeInt (hPtr , Allocator .HeaderStruct .size , size );
384457 }
385458
0 commit comments