Hacker News new | ask | show | jobs
by massysett 831 days ago
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.