473,569 Members | 2,836 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

garbage collection questions

Hi,

I am trying to implement garbage collection for C/C++ mainly for
learning purpose. I dont know where to start.

For example -
Basic questions - How do I identify the variables that contain pointers
? Where do I look for these variables and how to tell if what is
assigned to them is a pointer ?

Do I need to implement my own API for malloc/free ( or new/delete ) or
can I create a library that can be linked with the given source code
that uses the standard malloc ? Are there any tradeoffs for either
approach ?

Appreciate any pointers to any info on the web or text books etc.

TIA

Jan 22 '06 #1
15 2374
vk02720 wrote:
Hi,

I am trying to implement garbage collection for C/C++ mainly for
learning purpose. I dont know where to start.

For example -
Basic questions - How do I identify the variables that contain pointers
? Where do I look for these variables and how to tell if what is
assigned to them is a pointer ?

Do I need to implement my own API for malloc/free ( or new/delete ) or
can I create a library that can be linked with the given source code
that uses the standard malloc ? Are there any tradeoffs for either
approach ?

Appreciate any pointers to any info on the web or text books etc.

TIA


You definitely need to be aware of this:

http://www.hpl.hp.com/personal/Hans_Boehm/gc/
Jan 22 '06 #2
vk02720 wrote:
I am trying to implement garbage collection for C/C++ mainly for
learning purpose. I dont know where to start.
Not to discourage you, but that's a plenty ambitious project. I humbly
suggest that if you have no idea where to start, your probably biting
off significantly more than you have a reasonable chance of being able
to chew.
For example -
Basic questions - How do I identify the variables that contain pointers
? Where do I look for these variables and how to tell if what is
assigned to them is a pointer ?

Do I need to implement my own API for malloc/free ( or new/delete ) or
can I create a library that can be linked with the given source code
that uses the standard malloc ? Are there any tradeoffs for either
approach ?
Garbage collection cannot simply be glued on to the standard memory
model. There's nothing about the contents of the memory which tells
you who's referencing it. A tool such as a profiler can do some
analysis of this sort, but with an overhead that would be unacceptable
outside of performance testing.

So, the place to start is to understand how the standard memory model
works, in excruciating detail, because what you're talking about is
stripping the whole thing out and replacing it. Still sound like a fun
project? (Okay, I'll confess, it sounds very fun, but I still think
you sound like you don't know what you're in for).
Appreciate any pointers to any info on the web or text books etc.


Andrei Alexandrescu's "Modern C++ Design" has a chapter on writing a
custom allocator. I saw him give a talk recently, though, and he now
disfavors the approach therein in favor of a policy-based design. It's
still instructive, of course. However, the first place I'd start is by
reading all of the relevant material in TC++PL (3rd or Special Ed.) by
Stroustrup.

Luke

Jan 22 '06 #3
vk02720 wrote:
Hi,

I am trying to implement garbage collection for C/C++ mainly for
learning purpose. I dont know where to start.

For example -
Basic questions - How do I identify the variables that contain pointers
? Where do I look for these variables and how to tell if what is
assigned to them is a pointer ?
What you can do is, provide functions so that the class implementer can
register the pointers to members. Provide also a virtual base class to
register the object itself.

Do I need to implement my own API for malloc/free ( or new/delete ) or
can I create a library that can be linked with the given source code
that uses the standard malloc ? Are there any tradeoffs for either
approach ?
You can but I doubt you have to...if you are doing heap compacting then yes.

Appreciate any pointers to any info on the web or text books etc.

TIA


Ben

Jan 23 '06 #4
"vk02720" <vk*****@my-deja.com> writes:
Hi,

I am trying to implement garbage collection for C/C++ mainly for
learning purpose. I dont know where to start.

For example -
Basic questions - How do I identify the variables that contain pointers
? Where do I look for these variables and how to tell if what is
assigned to them is a pointer ?
You have basically two ways:

- either you know them, that is, you are the compiler and you know
where you store the pointers, then you can output this information
in the object files. This is what is done with higher level
languages.

- or you don't know them. Then you cannot know them because stuff like:
{ char storage[100]; ((void*)storage )=malloc(4); } is perfectly valid in C.
So the only think you can do, is to be "conservati ve" (see Boehm GC),
that is, you scan all the allocated memory, and if you see a bit
pattern that looks like a pointer, then you assume it's a pointer.

What looks like a pointer? Any bitfield that points to a zone where
there is some memory allocated.
To find the variables you must identify the root set: all the
potential pointer in the stack and the global memory.

Do I need to implement my own API for malloc/free ( or new/delete ) or
can I create a library that can be linked with the given source code
that uses the standard malloc ? Are there any tradeoffs for either
approach ?


Well, at a minimum, you'd have to replace free by a version that does
nothing, and malloc by a version that calls the garbage collector from
time to time.
--
__Pascal Bourguignon__ http://www.informatimago.com/

This universe shipped by weight, not volume. Some expansion may have
occurred during shipment.
Jan 23 '06 #5
vk02720 wrote:
For example -
Basic questions - How do I identify the variables that contain pointers
? Where do I look for these variables and how to tell if what is
assigned to them is a pointer ?


In theory this is very hard because the internals of the implementation
are hidden. The language people may say: new and malloc allocate on the
heap, but the heap can have different meanings, depending on what data
structures your compiler uses to accomplish the functionality.

If you know what your implementation does for memory
allocation/deallocation (e.g. if you know everything about gcc
compiler) you can make such an attempt. If you just wanna change the
world, try writing a garbage collector for
http://fabrice.bellard.free.fr/otcc/ or
http://fabrice.bellard.free.fr/tcc/ to start with and it will hopefully
give you pointer to work with gcc.

Jan 23 '06 #6

"vk02720" <vk*****@my-deja.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
I am trying to implement garbage collection for C/C++ mainly for
learning purpose. I dont know where to start.


http://www.amazon.com/gp/product/047...lance&n=283155

Walter Bright
www.digitalmars.com C, C++, D programming language compilers
Jan 23 '06 #7
vk02720 wrote:

I am trying to implement garbage collection for C/C++ mainly for
learning purpose. I dont know where to start.


You have already made some fatal mistakes. There is no language
C/C++. There is C, and there is C++. Make up your mind. Do not
cross post between the two languages, because they are different
and have different rules and standards.

Never post with cross-posting without setting followups to the
single appropriate newsgroup.

Always post your efforts.

F'ups set.

--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell. org/google/>

Jan 23 '06 #8
On Mon, 23 Jan 2006 01:25:14 +0100,
Pascal Bourguignon <sp**@mouse-potato.com> wrote:
"vk02720" <vk*****@my-deja.com> writes:
I am trying to implement garbage collection for C/C++ mainly for
learning purpose. I dont know where to start.


You have basically two ways:

- either you know them, that is, you are the compiler and you know
where you store the pointers, then you can output this information
in the object files. This is what is done with higher level
languages.

- or you don't know them. Then you cannot know them because stuff like:
{ char storage[100]; ((void*)storage )=malloc(4); } is perfectly valid in C.


Not really, because storage is not an `lvalue', but garbage collection
in cases like this (when "you don't know all the details about all the
variables") is pretty hard. For instance, it cannot help in bugs like
the following:

void function (void)
{
char storage[4];
memset(storage, 0, 100);
}

But this is not really garbage collection related, I guess.

Jan 23 '06 #9
Giorgos Keramidas <ke******@ceid. upatras.gr> writes:
On Mon, 23 Jan 2006 01:25:14 +0100,
Pascal Bourguignon <sp**@mouse-potato.com> wrote:
"vk02720" <vk*****@my-deja.com> writes:
I am trying to implement garbage collection for C/C++ mainly for
learning purpose. I dont know where to start.


You have basically two ways:

- either you know them, that is, you are the compiler and you know
where you store the pointers, then you can output this information
in the object files. This is what is done with higher level
languages.

- or you don't know them. Then you cannot know them because stuff like:
{ char storage[100]; ((void*)storage )=malloc(4); } is perfectly valid in C.


Not really, because storage is not an `lvalue',


Indeed. I remember having done this kind of thing, but perhaps it has
always been with pointers instead of automatically allocated memory.
Anyways, you can just add a pair of stars to trick the compiler:

% cat m.c ; gcc -S -c -o m.s m.c ; cat m.s
#include <stdlib.h>
int main(void){ char storage[100]; (*(void**)stora ge)=malloc(4); return(0);}

.file "m.c"
.text
..globl main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
subl $120, %esp ; allocates char storage[100]
andl $-16, %esp
movl $0, %eax
subl %eax, %esp
subl $12, %esp
pushl $4
call malloc
addl $16, %esp
movl %eax, -120(%ebp) ; store the result of malloc to the
; first four bytes of storage.
movl $0, %eax
leave
ret
.size main, .-main
.ident "GCC: (GNU) 3.3 20030226 (prerelease) (SuSE Linux)"
--
__Pascal Bourguignon__ http://www.informatimago.com/

PLEASE NOTE: Some quantum physics theories suggest that when the
consumer is not directly observing this product, it may cease to
exist or will exist only in a vague and undetermined state.
Jan 23 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
947
by: JC | last post by:
How does garbage collection work in C# and VB.NET for data returned from COM object? For example, a COM object written in C++ returns a SAFEARRAY to C# or VB.NET, will this SAFEARRAY (mapped to type in C# or VB.NET) be garbage collected? Similarily, if C# or VB.NET pass SAFEARRAY to the COM object via COM method invocation, will this...
14
1519
by: Ömer KUL | last post by:
hi, i have a garbage collection problem. i have an application writen by vb.net, i am opening a form and in this form there is a big query. when i open the form, it takes 80 mb memory because of the data table. when i closed the form it is already taking 80 mb memory. where is garbage collection? i am waiting 30 minutes but memory usage is...
28
3326
by: joe | last post by:
I have a simple .NET application with two or three listViews which are filled with icons and when the user click on the proper item, they display the related images. I use "image = null ; " for all images that have been used and are going to be closed. This is how ever no way to reduce the memory consumption. I have noticed , using the task...
4
12319
by: Chris | last post by:
Hi, I think I'm having some problems here with garbage collection. Currently, I have the following code: public struct Event { public int timestamp;
6
1393
by: [Yosi] | last post by:
How can I make sure that a spicific function location in memory will not moved , by OS/Garbage collection ????? The problem is : I'm using a DLL this dll have an call backerror function, in my Application I made an error function which show the error if occurred (Message box). I send a reference of my Application error function to DLL call...
20
450
by: vk02720 | last post by:
Hi, I am trying to implement garbage collection for C/C++ mainly for learning purpose. I dont know where to start. For example - Basic questions - How do I identify the variables that contain pointers ? Where do I look for these variables and how to tell if what is assigned to them is a pointer ?
5
4768
by: R. MacDonald | last post by:
Hello, all, I am currently working on a .Net (VB) application that invokes routines in unmanaged (Fortran) DLLs. The unmanaged routines then communicate with the .Net application by means of a call-back mechanism. These calls pass a string that contains a "command" and a pointer to a SafeArray that (depending on the command) either...
2
1859
by: roger.dunham | last post by:
I am trying to identify whether a .NET 1.1 application that I have written has a memory leak. I thought I understood how .NET memory management worked, but it appears that there is more to it than I realised. I have created a simple app that creates a byte array which is stored as a member of the application's main form. The important...
4
2027
by: Carl Banks | last post by:
On Apr 12, 7:02 am, andreas.eis...@gmail.com wrote: Well, the garbage collector activates whenever allocations exceed deallocations by a certain amount, which for obvious reasons is the reason for the bottleneck wh My first stab at a suggestion would be to adjust the threshold depending on how successful it is. So if a garbage collection...
0
7612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8119
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7668
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5218
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3637
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2111
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.