Changeset 558


Ignore:
Timestamp:
Apr 30, 2011, 12:24:47 AM (7 years ago)
Author:
Sam Hocevar
Message:

lisp: some refactoring and code removal in the Lisp GC.

Location:
abuse/trunk/src/lisp
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • abuse/trunk/src/lisp/lisp.cpp

    r555 r558  
    190190    {
    191191        if (which_space == PERM_SPACE || which_space == TMP_SPACE)
    192             collect_space(which_space, 0);
     192            LispGC::CollectSpace(which_space, 0);
    193193
    194194        if (size > get_free_size(which_space))
    195             collect_space(which_space, 1);
     195            LispGC::CollectSpace(which_space, 1);
    196196
    197197        if (size > get_free_size(which_space))
     
    28232823    }
    28242824    case SYS_FUNC_GC:
    2825         collect_space(current_space, 0);
     2825        LispGC::CollectSpace(current_space, 0);
    28262826        break;
    28272827    case SYS_FUNC_SCHAR:
     
    31433143
    31443144/*  l_user_stack.push(ret);
    3145   collect_space(PERM_SPACE);
     3145  LispGC::CollectSpace(PERM_SPACE);
    31463146  ret=l_user_stack.pop(1);  */
    31473147
  • abuse/trunk/src/lisp/lisp_gc.cpp

    r557 r558  
    1818#include "lisp.h"
    1919#include "lisp_gc.h"
    20 #ifdef NO_LIBS
    21 #include "fakelib.h"
    22 #endif
    2320
    2421#include "stack.h"
     
    3532// Stack where user programs can push data and have it GCed
    3633grow_stack<void> l_user_stack(150);
     34
    3735// Stack of user pointers
    3836grow_stack<void *> PtrRef::stack(1500);
    3937
    40 size_t reg_ptr_total = 0;
    41 size_t reg_ptr_list_size = 0;
    42 void ***reg_ptr_list = NULL;
     38static size_t reg_ptr_total = 0;
     39static void ***reg_ptr_list = NULL;
    4340
    4441static uint8_t *cstart, *cend, *collected_start, *collected_end;
    4542
    46 void register_pointer(void **addr)
    47 {
    48   if (reg_ptr_total >= reg_ptr_list_size)
    49   {
    50     reg_ptr_list_size += 0x100;
    51     reg_ptr_list = (void ***)realloc(reg_ptr_list, sizeof(void **) * reg_ptr_list_size);
    52   }
    53   reg_ptr_list[reg_ptr_total++] = addr;
    54 }
    55 
    56 void unregister_pointer(void **addr)
    57 {
    58   void ***reg_on = reg_ptr_list;
    59   for (size_t i = 0; i < reg_ptr_total; i++, reg_on++)
    60   {
    61     if (*reg_on == addr)
    62     {
    63       reg_ptr_total--;
    64       for (size_t j = i; j < reg_ptr_total; j++, reg_on++)
    65         reg_on[0] = reg_on[1];
    66       return ;
    67     }
    68   }
    69   fprintf(stderr, "Unable to locate ptr to unregister");
    70 }
    71 
    72 static LObject *CollectObject(LObject *x);
    73 
    74 static LArray *CollectArray(LArray *x)
     43LArray *LispGC::CollectArray(LArray *x)
    7544{
    7645    size_t s = x->len;
     
    8453}
    8554
    86 inline LList *CollectList(LList *x)
     55LList *LispGC::CollectList(LList *x)
    8756{
    8857    LList *last = NULL, *first = NULL;
     
    11281}
    11382
    114 static LObject *CollectObject(LObject *x)
     83LObject *LispGC::CollectObject(LObject *x)
    11584{
    11685    LObject *ret = x;
     
    204173}
    205174
    206 static void collect_symbols(LSymbol *root)
     175void LispGC::CollectSymbols(LSymbol *root)
    207176{
    208177    if (!root)
     
    212181    root->function = CollectObject(root->function);
    213182    root->name = (LString *)CollectObject(root->name);
    214     collect_symbols(root->left);
    215     collect_symbols(root->right);
    216 }
    217 
    218 static void collect_stacks()
    219 {
    220   long t = l_user_stack.son;
    221 
    222   void **d = l_user_stack.sdata;
    223   for (int i = 0; i < t; i++, d++)
    224     *d = CollectObject((LObject *)*d);
    225 
    226   t = PtrRef::stack.son;
    227   void ***d2 = PtrRef::stack.sdata;
    228   for (int i = 0; i < t; i++, d2++)
    229   {
    230     void **ptr = *d2;
    231     *ptr = CollectObject((LObject *)*ptr);
    232   }
    233 
    234   d2 = reg_ptr_list;
    235   for (size_t i = 0; i < reg_ptr_total; i++, d2++)
    236   {
    237     void **ptr = *d2;
    238     *ptr = CollectObject((LObject *)*ptr);
    239   }
    240 }
    241 
    242 void collect_space(int which_space, int grow) // should be tmp or permanent
    243 {
    244   int old_space = current_space;
    245   cstart = space[which_space];
    246   cend = free_space[which_space];
    247 
    248   space_size[GC_SPACE] = space_size[which_space];
    249   if (grow)
    250   {
    251     space_size[GC_SPACE] += space_size[which_space] >> 1;
    252     space_size[GC_SPACE] -= (space_size[GC_SPACE] & 7);
    253   }
    254   uint8_t *new_space = (uint8_t *)malloc(space_size[GC_SPACE]);
    255   current_space = GC_SPACE;
    256   free_space[GC_SPACE] = space[GC_SPACE] = new_space;
    257 
    258   collected_start = new_space;
    259   collected_end = new_space + space_size[GC_SPACE];
    260 
    261   collect_symbols(LSymbol::root);
    262   collect_stacks();
    263 
    264   // for debuging clear it out
    265   memset(space[which_space], 0, space_size[which_space]);
    266   free(space[which_space]);
    267 
    268   space[which_space] = new_space;
    269   space_size[which_space] = space_size[GC_SPACE];
    270   free_space[which_space] = new_space
    271                           + (free_space[GC_SPACE] - space[GC_SPACE]);
    272   current_space = old_space;
    273 }
    274 
     183    CollectSymbols(root->left);
     184    CollectSymbols(root->right);
     185}
     186
     187void LispGC::CollectStacks()
     188{
     189    size_t t = l_user_stack.son;
     190
     191    void **d = l_user_stack.sdata;
     192    for (size_t i = 0; i < t; i++, d++)
     193        *d = CollectObject((LObject *)*d);
     194
     195    t = PtrRef::stack.son;
     196    void ***d2 = PtrRef::stack.sdata;
     197    for (size_t i = 0; i < t; i++, d2++)
     198    {
     199        void **ptr = *d2;
     200        *ptr = CollectObject((LObject *)*ptr);
     201    }
     202
     203    d2 = reg_ptr_list;
     204    for (size_t i = 0; i < reg_ptr_total; i++, d2++)
     205    {
     206        void **ptr = *d2;
     207        *ptr = CollectObject((LObject *)*ptr);
     208    }
     209}
     210
     211void LispGC::CollectSpace(int which_space, int grow)
     212{
     213    int old_space = current_space;
     214    cstart = space[which_space];
     215    cend = free_space[which_space];
     216
     217    space_size[GC_SPACE] = space_size[which_space];
     218    if (grow)
     219    {
     220        space_size[GC_SPACE] += space_size[which_space] >> 1;
     221        space_size[GC_SPACE] -= (space_size[GC_SPACE] & 7);
     222    }
     223    uint8_t *new_space = (uint8_t *)malloc(space_size[GC_SPACE]);
     224    current_space = GC_SPACE;
     225    free_space[GC_SPACE] = space[GC_SPACE] = new_space;
     226
     227    collected_start = new_space;
     228    collected_end = new_space + space_size[GC_SPACE];
     229
     230    CollectSymbols(LSymbol::root);
     231    CollectStacks();
     232
     233    // for debuging clear it out
     234    memset(space[which_space], 0, space_size[which_space]);
     235    free(space[which_space]);
     236
     237    space[which_space] = new_space;
     238    space_size[which_space] = space_size[GC_SPACE];
     239    free_space[which_space] = new_space
     240                            + (free_space[GC_SPACE] - space[GC_SPACE]);
     241    current_space = old_space;
     242}
     243
  • abuse/trunk/src/lisp/lisp_gc.h

    r555 r558  
    1313#define __LISP_GC_HPP_
    1414
    15 extern grow_stack<void> l_user_stack;       // stack user progs can push data and have it GCed
     15// Stack user progs can push data and have it GCed
     16extern grow_stack<void> l_user_stack;
    1617
    17 void collect_space(int which_space, int grow); // should be tmp or permenant
     18class LispGC
     19{
     20public:
     21    // Collect temporary or permanent spaces
     22    static void CollectSpace(int which_space, int grow);
    1823
    19 void register_pointer(void *&addr);
    20 void unregister_pointer(void *&addr);
     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};
    2131
    2232// This pointer reference stack lists all pointers to temporary lisp
     
    4151    }
    4252
    43     // stack of user pointers, user pointers get remapped on GC
    44     static grow_stack<void *>stack;
     53    // Stack of user pointers, user pointers get remapped on GC
     54    static grow_stack<void *> stack;
    4555};
    4656
Note: See TracChangeset for help on using the changeset viewer.