10

In common lisp, what is the preferred way to manage external resources (sockets, filesystem handles, etc)?

I'm trying to make a simple opengl 2d platformer in common lisp. The problem is I'm not quite sure how to track OpenGL textures (must be deleted with glDeleteTextures when they are no longer needed).

In C++ I preferred to use following scheme:

  1. Make texture class
  2. Make smart/weak pointers for that texture class
  3. Store textures in map (dictionary/hashtable) that maps file names to weak pointers to textures.
  4. When new textures are requested, look into map, and see if there's a non-null (nil) weak pointer available. If it is available, return existing object, otherwise load new texture.

However, I'm not quite sure how to port this scheme to common lisp, because:

  1. There are no destructors.
  2. There's garbage collector, and it seems my implementation (clozureCL on windows platform) supports finalizers, but as far as I can tell it is not recommended to use finalizers in common lisp because they're not deterministic.
  3. The preferred way of managing resources using (with-* doesn't look suitable there, because resources can be shared, and loaded/unloaded in the middle of function call.

As far as I can tell, there are several approaches available:

  1. Give up on automatic resource managment, and do it manually.
  2. Implement something similar to C++ RAII, weakpointer and smartpointer using macros (this code probably doesn't work):

    (defclass destructible () ())
    
    (defmethod destroy ((obj destructible)) (print (format nil "destroy: ~A" obj)))
    
    (defparameter *destructible-classes-list* nil)
    
    (defmethod initialize-instance :after ((obj destructible) &key)
      (progn
          (push *destructible-classes-list* obj)
          (print (format nil "init-instance: ~A" obj))))
    
    (defmacro with-cleanup (&rest body)
      `(let ((*destructible-classes-list* nil))
        (progn ,@body (mapcar (lambda (x) (destroy x)) *destructible-classes-list*))))
    
    (defclass refdata (destructible)
      ((value :accessor refdata-value :initform nil)
       (ref :accessor refdata-ref :initform 0)
       (weakrefcount :accessor refdata-weakref :initform 0)))
    
    (defmethod incref ((ref refdata))
      (if ref (incf (refdata-ref ref))))
    
    (defmethod decref ((ref refdata))
      (if ref
        (progn (decf (refdata-ref ref))
         (if (<= (refdata-ref ref) 0) 
           (progn (destroy (refdata-value ref))
              (setf (refdata-value ref) nil))))))
    
    (defmethod incweakref ((ref refdata))
      (if ref (incf (refdata-weakref ref))))
    
    (defmethod decweakref ((ref refdata))
      (if ref (decf (refdata-weakref ref))))
    
    (defclass refbase (destructible) ((data :accessor refbase-data :initform nil)))
    
    (defmethod deref ((ref refbase))
      (if (and (refbase-data ref) (refdata-value (refbase-data ref)))
        (refdata-value (refbase-data ref))
        nil))
    
    (defclass strongref (refbase) ())
    
    (defclass weakref (refbase) ())
    
    (defmethod destroy :before ((obj strongref))
      (decref (refbase-data obj)))
    
    (defmethod destroy :before ((obj weakref))
      (decweakref (refbase-data obj)))
    
    (defmethod initialize-instance :after ((obj strongref) &key)
      (incref (refbase-data obj)))
    
    (defmethod initialize-instance :after ((obj weakref) &key)
      (incweakref (refbase-data obj)))
    

Is there a better way to do it?

C++ Concepts Explanation: What is a smart pointer and when should I use one?

Community
  • 1
  • 1
SigTerm
  • 24,947
  • 5
  • 61
  • 109
  • I'm not familiar enough with C++ to answer your question, but this project: http://common-lisp.net/project/trivial-garbage/ includes weak pointers and weak hash-tables, which looks like it may help. This may help as well: http://code.google.com/p/lispbuilder/source/browse/trunk/lispbuilder-sdl/sdl/cffi-finalizers.lisp?r=426 (though `finalize` is now in trivial-garbage, not cffi) – Lex Nov 10 '12 at 15:40
  • @Lex: Does it mean there's no other way to do it? I've seen trivial-garbage, and while it is useful, it does not allow fine control. Also see *"but as far as I can tell it is not recommended to use finalizers in common lisp because they're not deterministic."* – SigTerm Nov 10 '12 at 15:58
  • I don't know enough about C++ to answer one way or the other. Just thought the code might help in your implementation. – Lex Nov 10 '12 at 16:24
  • @wvxvw: "(print (format nil" I'm just getting used to lisp, you know... "which is idk... have you really tested" The problem is that I need to manage objects that GC can't handle automatically (OpenGL objects - they can be freed only under specific conditions, etc), and those objects should exist for prolonged time. And of course, I want it to be done automatically. Judging by google search results, it seems this is a common problem for all GC languages - when you want something unusual to be GCd but only under certain conditions. Anything that isn't GL-related can be handled by GC, of course. – SigTerm Nov 10 '12 at 19:57

1 Answers1

9

If you want to handle a dynamic extent scope use UNWIND-PROTECT. If the program leaves that scope - normally or on error - the clean-up form is called. Just deallocate there or do whatever you want.

Sometimes Lisps use some kind of 'resource' mechanism to keep track of used and unused objects. Such a library provides fast allocation from a pool, allocating, initialization, deallocation, mapping of resourced object. CLIM defines a primitive version: Resources. CCL has a similar primitive version of it.

In a Lisp with 'timers', you can run a function periodically which looks for 'objects' to deallocate. In CCL you can use a thread which sleeps for a certain time using PROCESS-WAIT.

A bit about your coding style:

  • FORMAT can directly output to a stream, no PRINT needed
  • (defmethod foo ((e bar)) (if e ...)) : the IF makes no sense. e will always be an object.
  • many PROGN are not needed. Where needed, it could be removed when IF is replaced by WHEN.
Rainer Joswig
  • 127,693
  • 10
  • 201
  • 325
  • That's pretty close to what I've been looking for, thank you. – SigTerm Nov 10 '12 at 21:32
  • "A bit about your coding style" Thanks for the tips, but I"m new to this (I think this is either 2nd or 3rd lisp program I've written?) so I guess coding style will improve evenetually - when I get more experience... – SigTerm Nov 10 '12 at 22:38
  • 1
    @SigTerm checkout Clozure Common Lisp's 'with-autorelease-pool' macro as a concrete example. It uses the unwind-protect technique Rainer mentions here. – Clayton Stanley Nov 11 '12 at 05:47