source: abuse/trunk/src/lisp/lisp_gc.h @ 561

Last change on this file since 561 was 561, checked in by Sam Hocevar, 11 years ago

lisp: fix a memory leak in the grow stack objets and refactor the class.

  • Property svn:keywords set to Id
File size: 1.4 KB
RevLine 
[56]1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
[494]4 *  Copyright (c) 2005-2011 Sam Hocevar <sam@hocevar.net>
[56]5 *
6 *  This software was released into the Public Domain. As with most public
[555]7 *  domain software, no warranty is made or implied by Crack dot Com, by
8 *  Jonathan Clark, or by Sam Hocevar.
[56]9 */
10
[481]11#include "stack.h"
[2]12#ifndef __LISP_GC_HPP_
13#define __LISP_GC_HPP_
14
[558]15// Stack user progs can push data and have it GCed
[561]16extern GrowStack<void> l_user_stack;
[2]17
[558]18class LispGC
19{
20public:
21    // Collect temporary or permanent spaces
22    static void CollectSpace(int which_space, int grow);
[2]23
[558]24private:
25    static LArray *CollectArray(LArray *x);
26    static LList *CollectList(LList *x);
27    static LObject *CollectObject(LObject *x);
28    static void CollectSymbols(LSymbol *root);
29    static void CollectStacks();
30};
[2]31
[491]32// This pointer reference stack lists all pointers to temporary lisp
33// objects. This allows the pointers to be automatically modified if an
34// object allocation triggers a garbage collection.
35class PtrRef
36{
37public:
38    template<typename T> inline PtrRef(T *&ref)
39    {
40        stack.push((void **)&ref);
41    }
[2]42
[496]43    template<typename T> inline PtrRef(T * const &ref)
44    {
45        stack.push((void **)&ref);
46    }
47
[491]48    inline ~PtrRef()
49    {
50        stack.pop(1);
51    }
[2]52
[558]53    // Stack of user pointers, user pointers get remapped on GC
[561]54    static GrowStack<void *> stack;
[491]55};
56
[2]57#endif
[491]58
Note: See TracBrowser for help on using the repository browser.