Skip to content

Commit fdcc150

Browse files
committed
psxbios: some missed malloc merge behavior
1 parent 8fb79cd commit fdcc150

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

libpcsxcore/psxbios.c

+16-6
Original file line numberDiff line numberDiff line change
@@ -1320,7 +1320,7 @@ static int malloc_heap_grow(u32 size) {
13201320

13211321
heap_addr = loadRam32(A_HEAP_BASE);
13221322
heap_end = loadRam32(A_HEAP_END);
1323-
heap_addr_new = heap_addr + 4 + size;
1323+
heap_addr_new = heap_addr + size + 4;
13241324
if (heap_addr_new >= heap_end)
13251325
return -1;
13261326
storeRam32(A_HEAP_BASE, heap_addr_new);
@@ -1372,6 +1372,12 @@ static void psxBios_malloc() { // 0x33
13721372
break;
13731373
}
13741374
// chunk too small
1375+
if (next_chunk_hdr == ~1) {
1376+
// rm useless last free block
1377+
storeRam32(A_HEAP_BASE, chunk + 4);
1378+
storeRam32(chunk, ~1);
1379+
continue;
1380+
}
13751381
if (next_chunk_hdr & 1) {
13761382
// merge
13771383
u32 msize = (chunk_hdr & ~3) + 4 + (next_chunk_hdr & ~3);
@@ -1391,10 +1397,15 @@ static void psxBios_malloc() { // 0x33
13911397
}
13921398
}
13931399

1394-
if (i == limit)
1400+
if (i == limit) {
1401+
PSXBIOS_LOG("malloc: limit OOM\n");
13951402
ret = 0;
1396-
else if (tries == 0 && malloc_heap_grow(size))
1403+
}
1404+
else if (tries == 0 && malloc_heap_grow(size)) {
1405+
PSXBIOS_LOG("malloc: grow OOM s=%d end=%08x/%08x\n",
1406+
size, loadRam32(A_HEAP_BASE), loadRam32(A_HEAP_END));
13971407
ret = 0;
1408+
}
13981409
else {
13991410
u32 chunk = loadRam32(A_HEAP_CURCHNK);
14001411
storeRam32(chunk, loadRam32(chunk) & ~3);
@@ -1428,9 +1439,8 @@ static void psxBios_calloc() { // 0x37
14281439
void psxBios_realloc() { // 0x38
14291440
u32 block = a0;
14301441
u32 size = a1;
1431-
#ifdef PSXBIOS_LOG
1432-
PSXBIOS_LOG("psxBios_%s\n", biosA0n[0x38]);
1433-
#endif
1442+
1443+
PSXBIOS_LOG("psxBios_%s %08x %d\n", biosA0n[0x38], a0, a1);
14341444

14351445
a0 = block;
14361446
/* If "old_buf" is zero, executes malloc(new_size), and returns r2=new_buf (or 0=failed). */

0 commit comments

Comments
 (0)