This is the mail archive of the guile@cygnus.com mailing list for the Guile project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: New shared substring implementation


Mikael Djurfeldt <mdj@nada.kth.se> writes:

> forcer <forcer@mindless.com> writes:
>
> > Well, my thoughts about the copy-on-write idea stumbled on one
> > problem.
> 
> (BTW, have you noticed the macro SCM_COERCE_SUBSTR (which for some
> strange reason is defined in symbols.h)?  Generally, all operations
> which mutate strings should call this macro.)
> 
> > (define str1 "foo")		   ; a string is allocated as usual
> > (define str2 (substring str1 0 1)) ; str2 is now a shared
> > 			           ; substring of str1
> > (string-set! str1 0 #\b)	   ; since str1 is not a shared
> > 				   ; substring, it's not copied,
> > 				   ; and thus str2 => "b"
> >
> > A solution to this problem would be to have (substring) mark the
> > string it's applied to to be a "substring" as well. The problem
> > then is that:
> > (define str1 "foo")
> > (define str2 (substring str1 2 3)) ; str1 and str2 are marked as
> > 			           ; shared substrings
> > (string-set! str1 0 #\b)	   ; str1 now points to a newly
> > 				   ; malloc'd copy of the string,
> > 				   ; and str2 points to the
> > 				   ; original string[2]
> > (string-set! str2 0 #\b)	   ; str2 is now a copy of the
> > 				   ; string as well, and there's
> > 				   ; no pointer to the original
> > 				   ; string left :]
> 
> Hmm... Isn't this exactly what you want?
> 
> After the second string-set!, there's no reason to keep the original
> string, so it should be freed.

Yes. The problem is, how to decide when it got to be freed. We
need something like a reference count here :]
	-forcer

-- 
((email . "forcer@mindless.com")       (www . "http://forcix.cx/")
 (irc   . "forcer@#StarWars (IRCnet)") (gpg . "/other/forcer.gpg"))

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]