Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

This might sound crazy or stupid but I really want to know if there is some Lisp with manual memory management? I love Lisp syntax. People complain about the parentheses but for me they are a blessing. I like how extremely uniform and regular they look.

But all Lisps I've seen have garbage collectors. If I could find a Lisp with manual memory management, I could ditch C++ in favor of that Lisp. Is there one?



You can use a regular common lisp distribution like SBCL and just stick to foreign types https://www.sbcl.org/manual/#Foreign-Types this way you still have a GC for lighter tasks but with the option of manual memory management


You can also use the DYNAMIC-EXTENT declaration to (in general) tell your implementation to stack-allocate a value.

http://clhs.lisp.se/Body/d_dynami.htm#dynamic-extent



You could do this in Common Lisp with global variables. They are not garbage collected. You'd have to decide how manual you want this to be. The ultimate manual setup would be something like

    (defvar *heap* (make-array 1024 :element-type '(unsigned-byte 8)))
which is just a 1k bag of bytes. Then write some functions to allocate from it, keep track of it, etc. A typical Common Lisp implementation will make a "specialized array" when it does this - that is, the array is not filled with a bunch of pointers to individually-allocated bytes.

There are other ways you could do it - for example (I don't know if this would be efficient):

    (defvar *heap* (make-array 0 :element-type '(vector (unsigned-byte 8))
                                 :adjustable t :fill-pointer t))
    (defun malloc (bytes)
        (let ((result (make-array bytes :element-type '(unsigned-byte 8))))
          (vector-push-extend result *heap*)
          result))
and there are ways you can write a FREE function to release these allocations or mark them for re-use.

These methods would not really release memory back to the OS. However, C malloc and free typically do not really free memory back to the OS either. You can easily do something in Common Lisp that's on equal footing with C in that regard.

Edi Weitz in "Common Lisp Recipes" points out that when you're doing something like this you're "pooling" memory, and in effect you're replacing automatic memory management with your own hand-rolled memory manager, and chances are it's not going to be as good as an automatic memory manager. But certainly it's doable.


I bet you could go quite far writing a straightforward compiler using an S-expr syntax.

Any by a "compiler" I'm talking doing something simply like taking Pascal, converting it to an s-expr grammar, and going to town.

Scheme can be a simple algol style language. Using s-expr syntax just made your lexer/parser much simpler. And (simple) compilation is really not that hard (just compile it to C for your first crack if you want).

I bet you could get quite far quite quickly.


If it exists, it's probably among the ones listed at https://www.softwarepreservation.org/projects/LISP/

If it doesn't exist, you could try modifying one of the listed implementations.


Maybe carp or bone-lisp?


Carp! It's still experimental and I'd argue the affordances provided by a gc are a part of what makes lisp lisp, but it fits your criteria.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: