MALLOC Garbage Collector

If you have any questions on programming, this is the place to ask them, whether you're a newbie or an experienced programmer. Discussion on programming in general is also welcome. We will help you with programming homework, but we will not do your work for you! Any porting requests must be made in Developmental Ideas.
Post Reply
coldev
DCEmu Crazy Poster
DCEmu Crazy Poster
Posts: 26
Joined: Tue Nov 01, 2011 7:30 pm
Contact:

MALLOC Garbage Collector

Post by coldev » Wed Aug 23, 2017 10:35 am

Hello, i am using malloc and free..

exists a garbage collector to prevent mem fragmentation... ?

or a method to manage mem to best ?
User avatar
BlueCrab
The Crabby Overlord
The Crabby Overlord
Posts: 5264
Joined: Mon May 27, 2002 11:31 am
Location: Sailing the Skies of Arcadia
Contact:

Re: MALLOC Garbage Collector

Post by BlueCrab » Wed Aug 23, 2017 10:50 am

C and C++ do not have a garbage collector. Even if they did, this wouldn't solve fragmentation unless there was also some form of compaction, which is a very difficult task to accomplish in a language like C or C++ that runs on the bare metal (and not through something like a JVM or the .NET runtime on a PC).

The malloc implementation used by KallistiOS is generally pretty good about preventing large-scale fragmentation problems, but it (of course) is not perfect. Just make sure you actually free everything you allocate with malloc, and you should be fine (unless you request something that's too large, of course).
coldev
DCEmu Crazy Poster
DCEmu Crazy Poster
Posts: 26
Joined: Tue Nov 01, 2011 7:30 pm
Contact:

Re: MALLOC Garbage Collector

Post by coldev » Sun Oct 15, 2017 12:49 am

hello , i need free mem...

+ i have mem problems

i call this in a function but (DC free mem is the same)

KOS_INIT_FLAGS(INIT_DEFAULT | INIT_MALLOCSTATS );

i call mm_init ();

but i have malloc crash errors..

how restart malloc , mem global restart.. what function.. ? not restart console, only restart kos malloc o manager system..



+ other question is possible pass params to arch_exec() function ?
how this .. arch_exec("hola.bin", "param1 param2");

+ question.. How get current directory path in kos ? get executable directory?


Thanks for your time..
User avatar
BlueCrab
The Crabby Overlord
The Crabby Overlord
Posts: 5264
Joined: Mon May 27, 2002 11:31 am
Location: Sailing the Skies of Arcadia
Contact:

Re: MALLOC Garbage Collector

Post by BlueCrab » Sun Oct 15, 2017 1:30 am

You cannot reset the memory allocator once the program has started. Doing so would mess with all the allocations that the hardware-related code does on startup, for instance. No operating system that I'm aware of would ever allow such a thing during the execution of a program because it is a very bad idea.

KOS doesn't have any concept of arguments being passed to programs, nor does it have any concept of a path where the program is located. If you want to do something like that, you'll have to implement it on your own (by finding an unused place in memory to store them, or something).

Getting the current working directory can be done in the normal C way with the POSIX standard getcwd() function, or through the KOS-specific function fs_getwd().
coldev
DCEmu Crazy Poster
DCEmu Crazy Poster
Posts: 26
Joined: Tue Nov 01, 2011 7:30 pm
Contact:

Re: MALLOC Garbage Collector

Post by coldev » Mon Oct 16, 2017 11:59 am

thanks for reply...

i use function arch_exec, to free mem...

but i have a errror



===== the DC exec.elf process ======
Error: mapped file /cd/OTHER/1ST_READ.BIN .. DC exec call failed
fase 5
arch: shutting down kernel
Except in block A0B3A85 | CF8AF28
Except in block A0B3A85 | CF8AF28
maple: final stats -- device count = 2, vbl_cntr = 6, dma_cntr = 6
Except in block A0B6891 | CF8B6A8
Except in block A0B6891 | CF8B6A8
Except in block A0B6C22 | CF8B9A8
Except in block A0B6C22 | CF8B9A8
VREG = 03 ARMRST 01
vid_set_mode: 640x480IL NTSC
VREG = 03 ARMRST 01
TI , invalidating *TLB
not implemented opcode : 8C00 : unknown opcode @ 8C00E0A2
Press Any key to continue



executable requires any format to use? please help me...



my source code ....



//===========================================

const char *cadena= string_get( params[1] );

file_t f;
void *subelf;

/* Print a hello */
printf("===== the DC exec.elf process ====== \n");

/* Map the sub-elf */
f = fs_open(cadena, O_RDONLY);
if (f)
{
subelf = fs_mmap(f);
if (subelf)
{
/* Tell exec to replace us */
printf("sub.bin mapped at %08x, jumping to it! \n", subelf);
arch_exec(subelf, fs_total(f));
}
else
printf("Error: mapped file %s .. \n",cadena);
}
else
printf("File %s dnt exists \n",cadena);

/* Shouldn't get here */
printf( "DC exec call failed \n");

string_discard( params[1] ) ;
//===========================================


thanks for your help
User avatar
BlueCrab
The Crabby Overlord
The Crabby Overlord
Posts: 5264
Joined: Mon May 27, 2002 11:31 am
Location: Sailing the Skies of Arcadia
Contact:

Re: MALLOC Garbage Collector

Post by BlueCrab » Mon Oct 16, 2017 8:06 pm

fs_mmap() does not do what you're expecting it to do for most filesystems, including iso9660. You need to open the file, find the file size yourself (fs_total() will work here), read it in, and then you should be able to jump to it.

arch_exec() expects your binary you load to just be a simple raw binary and it cannot be scrambled (unless you unscramble it yourself before calling arch_exec()).
coldev
DCEmu Crazy Poster
DCEmu Crazy Poster
Posts: 26
Joined: Tue Nov 01, 2011 7:30 pm
Contact:

Re: MALLOC Garbage Collector

Post by coldev » Mon Oct 16, 2017 9:04 pm

Thank you again.. God Bless You..

1. compile elf...
2. use objcopy
sh-elf-objcopy -R .stack -O binary bennudreamcast.exe 1ST_READ.BIN


3. the code...

//=========================
void *blob;
ssize_t length = fs_load(cadena, &blob);
if (length > 0)
{
printf ("Executing binary %s \n",cadena);
arch_exec(blob, length);
}
else
printf ("Error: Execute binary %s \n",cadena);



//=========================
coldev
DCEmu Crazy Poster
DCEmu Crazy Poster
Posts: 26
Joined: Tue Nov 01, 2011 7:30 pm
Contact:

Re: MALLOC Garbage Collector

Post by coldev » Tue Oct 17, 2017 4:03 pm

Dnt work ... restart and load main executable again



Relocated dynarec blocks: 1235 KB before, 1231 KB new, 3915 bytes diff, 0.31%
Executing binary /cd/GAME4/1ST_READ.BIN
arch: shutting down kernel
Except in block C12F135 | F0FF248
Except in block C12F135 | F0FF248
maple: final stats -- device count = 2, vbl_cntr = 5609, dma_cntr = 5609
Except in block C131A6B | F100088
Except in block C131A6B | F100088
Except in block C131DFC | F100268
Except in block C131DFC | F100268
VREG = 03 ARMRST 01
vid_set_mode: 640x480IL NTSC
VREG = 03 ARMRST 01
i-cache invalidation requested! (11 total)
Reseting Dynarec Cache on ICI due to manual block count
Reseting Dynarec Cache...
Post Reply