caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Re: Weak pointers
@ 1997-03-17 19:19 Damien Doligez
  1997-03-18 10:09 ` Kohler Markus
  0 siblings, 1 reply; 15+ messages in thread
From: Damien Doligez @ 1997-03-17 19:19 UTC (permalink / raw)
  To: caml-list

>From: Frank Christoph <christo@nextsolution.co.jp>
>  Does this mean that even a full weak pointer can be erased?

Yes.  An empty pointer doesn't contain anything, so talking about
erasure is only meaningful for full pointers.

> What does "at any time" mean --- even if the pointer is still
>accessible from the root set, its contents can be erased?

Yes, but if that object is still reachable through some normal
(non-weak) pointer (i.e. any object except a weak array), then the GC
will not erase that weak pointer.  In other words, the GC will only
erase a weak pointer if this erasure makes the object unreachable,
thus deallocatable.

>  Is this intended to contrast with a usual reference, which must
>always be initialized?

I think initialisation is irrelevant.

>  Are weak pointers intended to model C pointers?

No.  Weak pointers are essentially a GC feature: pointers that are not
considered as making the pointed object reachable.

>  Could someone post an example of their use?

You can use them to implement hash-consing.  Assume you have some tree
data structure.  If you want to share the nodes wherever possible, you
can put every created node into a hash table.  Then when you are about
to create a new node, you first look in the hash table to see if the
same node has already been created.  If this is the case, you return
the old one from the hash table instead of creating a new one.

But the above prevents the GC from collecting the nodes when they
become unused, because they will always be reachable from the hash
table.  If you use weak pointers in your hash table, the GC will be
able to deallocate the dead nodes, but you still can have access to
the live ones through the weak pointers.

I don't have the weak hash table code yet, but it will certainly
be in the library at some point in the future.

Hope this helps.  If it's not clear, please ask more questions.

-- Damien





^ permalink raw reply	[flat|nested] 15+ messages in thread
* Weak pointers
@ 1997-03-30 17:13 Daniel de Rauglaudre
  1997-04-01 10:57 ` Pierre Weis
  0 siblings, 1 reply; 15+ messages in thread
From: Daniel de Rauglaudre @ 1997-03-30 17:13 UTC (permalink / raw)
  To: caml-list

I would like to use weak pointers in my code, and I wrote a little test
to check its behavior... It does not work. Why? Maybe I did not
understand them...

$ cat >foo.ml
let print_weak w i =
  begin match Weak.get w i with
    Some _ -> Printf.printf "Some ..."
  | None -> Printf.printf "None"
  end;
  Printf.printf "\n";
  flush stdout
;;

let w = Weak.create 1;;
let r = ref "hello";;

Weak.set w 0 (Some !r);;
print_weak w 0;;

Gc.full_major ();;
print_weak w 0;;

r := "world";;
print_weak w 0;;

Gc.full_major ();;
print_weak w 0;;

$ ocamlc -v
The Objective Caml compiler, version 1.05-2
Standard library directory: ...

$ ocamlc foo.ml

$ ./a.out
Some ...
Some ...
Some ...
Some ...

  I do not understand why the very last print is "Some ..." and not "None".

--------------------------------------------------------------------------
 Daniel de RAUGLAUDRE

 Projet Cristal - INRIA Rocquencourt
 Tel: +33 (01) 39 63 53 51
 Email: daniel.de_rauglaudre@inria.fr
 Web: http://pauillac.inria.fr/~ddr/
--------------------------------------------------------------------------





^ permalink raw reply	[flat|nested] 15+ messages in thread
* Re:  Weak pointers
@ 1997-03-21 17:26 Damien Doligez
  0 siblings, 0 replies; 15+ messages in thread
From: Damien Doligez @ 1997-03-21 17:26 UTC (permalink / raw)
  To: caml-list

>I wonder when Weaks pointers are erased by the GC.

With the incremental garbage collector, it's hard to say.

At the soonest, at the end of the first marking phase that ends after
all (strong) pointers to the object have disappeared; at the latest at
the end of the next marking phase, unless you have created more strong
pointers in the meantime by using Weak.get on the weak pointer.

In any case, integers and constant constructors (which are represented
by integers) are never erased by the GC (there is no memory to be
gained by erasing them).  But there's of course no guarantee on this
point.


>As soon as it can be or when there is no more memory ?

The answer is: as soon as it can be, for some meaning of "can".
Caml is not SML/NJ.  You don't need to assume that it will use all
your memory, and then some.


>Is it a good idea to use weak pointers to implement more
>or less a cache ?

If the objects are only in your cache (and not also in some other data
structure used by your program), no because the GC will erase them as
soon as it can.

-- Damien





^ permalink raw reply	[flat|nested] 15+ messages in thread
* Weak pointers
@ 1997-03-20 12:12 Emmanuel Engel
  1997-03-20 16:09 ` Kohler Markus
  0 siblings, 1 reply; 15+ messages in thread
From: Emmanuel Engel @ 1997-03-20 12:12 UTC (permalink / raw)
  To: caml-list

I wonder when Weaks pointers are erased by the GC. As soon
as it can be or when there is no more memory ? In the second
case, does all the weaks pointers that can be erased are erased
or just enough to allocate new memory. In this case, which are 
erased, the older or the younger ?


Is it a good idea to use weak pointers to implement more
or less a cache ?

Something like 

type ('a,'b) cache =             (* In fact a binary tree *)
    Node of 'a * 'b * ('a,'b)cache Weak.t
   |Leaf

and to use the usual fonctions over binary tree.


-- 

- Emmanuel Engel





^ permalink raw reply	[flat|nested] 15+ messages in thread
[parent not found: <199703181607.RAA13208@tobago.inria.fr>]
[parent not found: <199703181434.PAA12806@tobago.inria.fr>]
* Re: Weak pointers
@ 1997-03-18  7:43 Kohler Markus
  0 siblings, 0 replies; 15+ messages in thread
From: Kohler Markus @ 1997-03-18  7:43 UTC (permalink / raw)
  To: caml-list

I didn't take a look at Java's weak pointers, but I assume it's same as in 
Smalltalk. 

In Smalltalk a weak pointer is not counted when the garbage checks if the 
object is still referenced. That means if you have only weak pointers to an 
object and the application needs memory it will be garbage collected. 

In Smalltalk theres is a correspinding concept called "finalization". 
If a weak object is garbage collected a finalize message is called. 
This is used in Smalltalk to free external resources like windows. 


Markus
-- 
+----------------------------------------------------------------------------+
| Markus Kohler                          Hewlett-Packard GmbH                |
| Software Engineer                      Network & System Management Division| 
|                                        IT/E Success Team                   |
+----------------------------------------------------------------------------+







^ permalink raw reply	[flat|nested] 15+ messages in thread
* Objective Caml 1.04 released
@ 1997-03-11 13:28 Xavier Leroy
  1997-03-14  8:40 ` Frank Christoph
  0 siblings, 1 reply; 15+ messages in thread
From: Xavier Leroy @ 1997-03-11 13:28 UTC (permalink / raw)
  To: caml-list, comp-lang-ml

Release 1.04 of Objective Caml is now available.

The main novelty in this release is the port of the Caml Light replay
debugger. Also, the native-code compiler now works on Silicon
Graphics, weak pointers are supported, and the foreign interface
was enriched to make calling Caml from C easier.

A detailed list of changes follows at the end of this message.

The sources and Windows binaries are available by anonymous FTP
at ftp://ftp.inria.fr/lang/caml-light. Diffs are not available due to
bootstrapping difficulties.

See http://pauillac.inria.fr/ocaml/ for documentation and general info
about Objective Caml.

- Xavier Leroy

Objective Caml 1.04:
--------------------

* Replay debugger ported from Caml Light; added debugger support in
  compiler (option -g) and runtime system. Debugger is alpha-quality
  and needs testing.

* Parsing:
  - Support for "# linenum" directives.
  - At toplevel, allow several phrases without intermediate ";;".

* Typing:
  - Allow constraints on datatype parameters, e.g. 
    type 'a foo = ... constraint 'a = 'b * 'c.
  - Fixed bug in signature matching in presence of free type variables '_a.
  - Extensive cleanup of internals of type inference.

* Native-code compilation:
  - Inlining of small functions at point of call (fairly conservative).
  - MIPS code generator ported to SGI IRIX 6.
  - Better code generated for large integer constants.
  - Check for urgent GC when allocating large objects in major heap.
  - PowerPC port: better scheduling, reduced TOC consumption.
  - HPPA port: handle long conditional branches gracefully,
    several span-dependent bugs fixed.

* Standard library:
  - More floating-point functions (all ANSI C float functions now available).
  - Hashtbl: added functorial interface (allow providing own equality
    and hash functions); rehash when resizing, avoid memory leak on
    Hashtbl.remove.
  - Added Char.uppercase, Char.lowercase, String.uppercase, String.lowercase,
    String.capitalize, String.uncapitalize.
  - New module Weak for manipulating weak pointers.
  - New module Callback for registering closures and exceptions to be
    used from C.

* Foreign interface:
  - Better support for callbacks (C calling Caml), exception raising
    from C, and main() in C. Added function to remove a global root.
  - Option -output-obj to package Caml code as a C library.

* Thread library: fixed bug in timed_read and timed_write operations;
  Lexing.from_function and Lexing.from_channel now reentrant.

* Unix interface: renamed EACCESS to EACCES (the POSIX name); added setsid;
  fixed bug in inet_addr_of_string for 64-bit platforms.

* Ocamlyacc: default error function no longer prevents error recovery.

* Ocamllex: fixed reentrancy problem w.r.t. exceptions during refill;
  fixed output problem (\r\r\n) under Win32.

* Macintosh port:
  - The makefiles are provided for compiling and installing O'Caml on
    a Macintosh with MPW 3.4.1.
  - An application with the toplevel in a window is forthcoming.

* Windows NT/95 port: updated toplevel GUI to that of Caml Light 0.73.

* Emacs editing mode and debugger interface included in distribution.





^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~1997-04-01 12:43 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-03-17 19:19 Weak pointers Damien Doligez
1997-03-18 10:09 ` Kohler Markus
1997-03-18 15:54   ` Wolfgang Lux
1997-03-18 16:37     ` Kohler Markus
1997-03-18 17:32       ` Wolfgang Lux
  -- strict thread matches above, loose matches on Subject: below --
1997-03-30 17:13 Daniel de Rauglaudre
1997-04-01 10:57 ` Pierre Weis
1997-04-01 12:03   ` Daniel de Rauglaudre
1997-03-21 17:26 Damien Doligez
1997-03-20 12:12 Emmanuel Engel
1997-03-20 16:09 ` Kohler Markus
     [not found] <199703181607.RAA13208@tobago.inria.fr>
1997-03-18 17:08 ` Kohler Markus
     [not found] <199703181434.PAA12806@tobago.inria.fr>
1997-03-18 15:51 ` Kohler Markus
1997-03-18  7:43 Kohler Markus
1997-03-11 13:28 Objective Caml 1.04 released Xavier Leroy
1997-03-14  8:40 ` Frank Christoph
1997-03-17  9:13   ` Weak pointers Frank Christoph

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).