473,396 Members | 2,030 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Gerbage collection as a library

I've tried to implement some garbage collection library for see. Here is
an example of it's usage. Do you think that it is usefull at all and
deserve development?

#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include "virlib.h"

void fclose_p (void *p);
void print (fp);
char *readline (fp);

int
main (int argc, char **argv)
{
FILE *fp;
begin_func ();

fp = fopen ("style.txt", "r");
add_destructor (fp, fclose_p);

print (fp);

end_func ();
return 0;
}

void
fclose_p (void *p)
{
fclose (p);
}

void
print (fp)
{
char *s;
int n;

begin_func ();
n = 0;
while ((s = readline (fp)) != NULL)
fprintf ("%4d%s", n++, s);
end_func ();
}

char *
readline (fp)
{
char *s, *t, *p;
int alen, llen;

begin_func ();
alen = 10;
s = malloc (alen);
if (s == NULL)
fail ("allocating buffer to read");
add_destructor (s, free);

p = s;
for (;;) {
t = fgets (p, alen, fp);
if (t == NULL) {
end_func ();
return NULL;
}

llen = strlen (p);
if (p[llen - 1] == '\n') {
mark_result (s);
end_func ();
return s;
}

llen = p - s;
t = realloc (s, alen * 2);
if (t == NULL)
fail ("allocating buffer to read");
mark_global (s); /* No need to destruct s */
s = t;
add_destructor (s, free);
p = s + llen;
}
}
Jan 25 '06 #1
2 1808
In article <dr***********@news.comcor-tv.ru>,
Victor Nazarov <vi*@comtv.ru> wrote:
I've tried to implement some garbage collection library for see. Here is
an example of it's usage. Do you think that it is usefull at all and
deserve development?
[code snipped]

I would suggest that you describe each of the library operations and
their purpose.

It appears from your code example that the user needs to manually
add_destructor) each item which is to be garbage collected,
and to mark_global() each item which is not to be garbage collected.
The user also appears to need to begin_func(), and appears to need
to end_func() at the end before returning from your routine.

The operation of mark_result() is not clear from your example --
does it sort of move the pointer's destruction time into the frame
above? But if it does then if you are returning a pointer through
multiple layers, you would have to know to mark_result() the pointer
at each layer. What is the user to do if the user wants to
return a structure or array that mixes items which are to be have
destructors run and items which are not to have destructors run?
It is not apparent from your example why it is that you do not
have two operations:

1) add_destructor() to record that an item will need to be garbage collected.
return value: an internal marker of the current garbage collection stack.

2) gc() to trigger a garbage collection. The argument to this would
be one of the values returned by add_destructor(), and gc() would
do the destruction of everything back to that marker.

There is no obvious need in your scheme for mark_global() since if
something is not marked for destruction then it simply wouldn't be
destroyed. (I do not have advice at the moment as to how to
handle values that are going to be returned.)
In my -opinion-, your present interface has too much manual work and
coding overhead, and would not become popular.

char *
readline (fp)
{
char *s, *t, *p;
int alen, llen;

begin_func ();
alen = 10;
s = malloc (alen);
if (s == NULL)
fail ("allocating buffer to read");
add_destructor (s, free);

p = s;
for (;;) {
t = fgets (p, alen, fp);
if (t == NULL) {
end_func ();
return NULL;
}

llen = strlen (p);
if (p[llen - 1] == '\n') {
mark_result (s);
end_func ();
return s;
}

llen = p - s;
t = realloc (s, alen * 2);
After the realloc(), the former value of s is not certain
to be valid anymore. This implies that you would need an operation
which was "transfer the attributes associated with this pointer
to instead be associated with this other pointer".
if (t == NULL)
fail ("allocating buffer to read");
mark_global (s); /* No need to destruct s */
But the value of s is not valid anymore?? Unless this -is- your
way of saying, "remove the attributes that were associated with this
pointer" -- and then requiring the user to know what the attributes
were and to set them on the new pointer. It would be easier on
the user to have a realloc() wrapper that took care of these details.
s = t;
add_destructor (s, free);
p = s + llen;
}
}


--
"It is important to remember that when it comes to law, computers
never make copies, only human beings make copies. Computers are given
commands, not permission. Only people can be given permission."
-- Brad Templeton
Jan 25 '06 #2
Walter Roberson wrote:
In article <dr***********@news.comcor-tv.ru>,
It appears from your code example that the user needs to manually
add_destructor) each item which is to be garbage collected,
and to mark_global() each item which is not to be garbage collected. yes
The user also appears to need to begin_func(), and appears to need
to end_func() at the end before returning from your routine. yes
The operation of mark_result() is not clear from your example --
does it sort of move the pointer's destruction time into the frame
above? yes
But if it does then if you are returning a pointer through
multiple layers, you would have to know to mark_result() the pointer
at each layer. User must mark_result() every "object" that function returns. I think
it's not so difficult to know what are you returning.
What is the user to do if the user wants to
return a structure or array that mixes items which are to be have
destructors run and items which are not to have destructors run? That's not solved yet. I think some routings should be written for
containers to implement their own resource managment. So if object is
added to an array it should be deleted when the array is deleted.
May be you can suggest something?
It is not apparent from your example why it is that you do not
have two operations:

1) add_destructor() to record that an item will need to be garbage collected.
return value: an internal marker of the current garbage collection stack.

2) gc() to trigger a garbage collection. The argument to this would
be one of the values returned by add_destructor(), and gc() would
do the destruction of everything back to that marker. Hmm, it's impossible to implement it from user level, only compiler
level, isn't it? I'm just trying to add resource management to C, not
developing new language. There are Java and Caml after all.
There is no obvious need in your scheme for mark_global() since if
something is not marked for destruction then it simply wouldn't be
destroyed. So it's used in the realloc example, i. e. "cancel managment of this
object, I'll do this manually".
(I do not have advice at the moment as to how to
handle values that are going to be returned.)
In my -opinion-, your present interface has too much manual work and
coding overhead, and would not become popular. My interface shouldn't be used every where. That was the part of the
design. The question is "are there cases when this library whould be
much better then manual resource managment? and how often is this?".
After the realloc(), the former value of s is not certain
to be valid anymore. This implies that you would need an operation
which was "transfer the attributes associated with this pointer
to instead be associated with this other pointer".

if (t == NULL)
fail ("allocating buffer to read");
mark_global (s); /* No need to destruct s */

But the value of s is not valid anymore?? Unless this -is- your
way of saying, "remove the attributes that were associated with this
pointer" -- and then requiring the user to know what the attributes
were and to set them on the new pointer.

Yes, that's what I've said.
It would be easier on
the user to have a realloc() wrapper that took care of these details.

Yes, it would.
Jan 25 '06 #3

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

Similar topics

2
by: Peter Kwan | last post by:
Hi, I believe I have discovered a bug in Python 2.3. Could anyone suggest a get around? When I tested my existing Python code with the newly released Python 2.3, I get the following warning: ...
4
by: Wandy Tang | last post by:
Dear All, How to pass a collection from a VB Component to an ASP page? I have no clue the syntax of all. Many Thanks. Regards, Wandy Tang
11
by: Rick | last post by:
Hi, My question is.. if Lisp, a 40 year old language supports garbage collection, why didn't the authors of C++ choose garbage collection for this language? Are there fundamental reasons behind...
34
by: Ville Voipio | last post by:
I would need to make some high-reliability software running on Linux in an embedded system. Performance (or lack of it) is not an issue, reliability is. The piece of software is rather simple,...
5
by: Kurt Bauer | last post by:
I have an ASP group calendar application which pulls calendar data from Exchange via webdav into an XML string. I then loop the XML nodes to populate a collection of appointments. Finally I use...
6
by: Andy | last post by:
Along with many others I've noticed the large amount of memory that can be taken up by the aspnet_wp.exe. I've found that I can better control and limit this memory consumption by including a...
7
by: Michael | last post by:
Hi all : Can I Inherits GridColumnStylesCollection if yes, and how to write it ? Thanks
6
by: Scott M. Lyon | last post by:
As I mentioned in my other post, I'm attempting to, using COM Interop so I can update existing VB6 code to (for several specific functions) return a Hashtable from a .NET library. I've had...
0
by: JosAH | last post by:
Greetings, Introduction Before we start designing and implementing our text builder class(es), I'd like to mention a reply by Prometheuzz: he had a Dutch version of the entire bible ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.