gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Coding > Garbage Collector in C for GBA

#10567 - Gordon - Tue Sep 09, 2003 5:17 am

Any one has written Garbage Collector in C?
I just want to get some ideas from it.

#10569 - tepples - Tue Sep 09, 2003 5:33 am

The best-known garbage-collecting allocator for C is the Boehm garbage collector for C and C++. I'm not sure if it's well suited for 256 KB arenas though...
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#10570 - Gordon - Tue Sep 09, 2003 5:59 am

tepples wrote:
The best-known garbage-collecting allocator for C is the Boehm garbage collector for C and C++. I'm not sure if it's well suited for 256 KB arenas though...

The collector is not completely portable, but the distribution includes ports to most standard PC and UNIX platforms. Win32, win32s, OS/2, and UNIX environments are supported on Intel-based PCs, as are all common UNIX workstations, MacOS, and AmigaDOS. Some ports are more polished than others

#10578 - Lupin - Tue Sep 09, 2003 2:39 pm

why'd I need an garbage collector?

#10586 - Cyberman - Tue Sep 09, 2003 9:55 pm

Lupin wrote:
why'd I need an garbage collector?


It's pretty simple.. it's called memory management. When you allocate a lot of objects and you wish to keep them persistant as long as necessary then 'collecting' the ones you don't need at the moment for new objects to be created is needed. They become garbage and need collected to free space for new objects.

That's about it :)

Cyb
_________________
If at first you don't succeed parachuting is NOT for you.

#10599 - Gordon - Wed Sep 10, 2003 4:32 am

Cyberman wrote:
Lupin wrote:
why'd I need an garbage collector?

keep them persistant as long as necessary then 'collecting' the ones you don't need

Since a object can be referenced everywhere. do you have any simple method to indicate which object is free to collect! and How to malloc a new object?

#10600 - tepples - Wed Sep 10, 2003 4:48 am

For answers, look up any fundamental reference on memory allocation that includes garbage collecting. PM me if you need help forming a Google query that returns relevant results.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#10614 - Lupin - Wed Sep 10, 2003 4:36 pm

Im a little confused, you're talking of objects, but as long as we're talking about C you could just mean arrays or variables...

Why don't you just manually allocate an region in memory instead of using such an garbage collector? I think this would be faster too though... but I'm just an C n00b :)

#10620 - sajiimori - Wed Sep 10, 2003 7:33 pm

The word "object" is often used in a generic way to denote anything that uses memory.

Ever used malloc/free or new/delete? Then you know how important it is to free memory that you've allocated at the appropriate time, to not lose your last pointer to an allocated block, and to not use a pointer after the block has been freed.

A garbage collector allows you to forget about these details by freeing blocks automatically when appropriate, at the expense of some speed. Most new languages (and some old ones) support GC.

#10628 - Gordon - Thu Sep 11, 2003 4:52 am

sajiimori wrote:
The word "object" is often used in a generic way to denote anything that uses memory.

Ever used malloc/free or new/delete? Then you know how important it is to free memory that you've allocated at the appropriate time, to not lose your last pointer to an allocated block, and to not use a pointer after the block has been freed.

A garbage collector allows you to forget about these details by freeing blocks automatically when appropriate, at the expense of some speed. Most new languages (and some old ones) support GC.

Thx for your explaination! Beening a good developer is not that easy!

Message for ALL:
We only interested "Garbage Collector"(GC) here, We don't care how we are going to use it!, It may be used in VM, OOP in C! Developing a GC in a Platform is easy, but it doesn't imply for GBA.

Sine, I love to write OO in C, instead of C++! Don't you know how to OOP in C? We may talk about it!

#10643 - Lupin - Thu Sep 11, 2003 2:41 pm

I took an look at malloc (oh, I should really learn more C) and I noticed that I missed such an important function! What does Malloc actually do? I mean I know what it's doing in general, but what does it do in code, is it possible to code my own malloc in C or ASM? I know it should of course be possible to do it, but how?

Sorry for using your thread for my dumb questions, but these questions at least keep the thread up ;P

#10649 - tepples - Thu Sep 11, 2003 3:36 pm

Google search: malloc algorithm
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#10655 - sajiimori - Thu Sep 11, 2003 5:28 pm

It always suprises me how long it takes for many beginning C programmers to discover malloc/free.

Not to sound like a broken record, but if you want to really understand C, get the C Primer. It's a pretty easy read, and fun when you're working along with it. You can also read it out of order.

If you want to understand how OS internals like memory allocation work, you can grab Lions' Commentary On Unix 6th Edition. Also, the character RAM allocation class I posted for hnager is very much like a simple malloc/free.