Ignore:
Timestamp:
May 11, 2011, 1:26:11 AM (8 years ago)
Author:
Sam Hocevar
Message:

lisp: refactor Lisp spaces so that they are real objects, and get rid
of the unused USER_SPACE.

File:
1 edited

Legend:

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

    r563 r635  
    4242 * tmp space which gets thrown away after completion of eval.  system
    4343 * functions reside in permant space. */
    44 
    45 bFILE *current_print_file=NULL;
     44LSpace LSpace::Tmp, LSpace::Perm, LSpace::Gc;
     45
     46/* Normally set to Tmp, unless compiling or other needs. */
     47LSpace *LSpace::Current;
     48
     49bFILE *current_print_file = NULL;
    4650
    4751LSymbol *LSymbol::root = NULL;
    4852size_t LSymbol::count = 0;
    4953
    50 
    51 uint8_t *space[4], *free_space[4];
    52 size_t space_size[4];
    5354int print_level = 0, trace_level = 0, trace_print_level = 1000;
    5455int total_user_functions;
    55 
    56 int current_space;  // normally set to TMP_SPACE, unless compiling or other needs
    5756
    5857int break_level=0;
     
    160159void need_perm_space(char const *why)
    161160{
    162   if (current_space!=PERM_SPACE && current_space!=GC_SPACE)
     161  if (LSpace::Current != &LSpace::Perm && LSpace::Current != &LSpace::Gc)
    163162  {
    164163    lbreak("%s : action requires permanant space\n", why);
     
    167166}
    168167
    169 void *mark_heap(int heap)
    170 {
    171   return free_space[heap];
    172 }
    173 
    174 void restore_heap(void *val, int heap)
    175 {
    176   free_space[heap] = (uint8_t *)val;
    177 }
    178 
    179 static size_t get_free_size(int which_space)
    180 {
    181     size_t used = free_space[which_space] - space[which_space];
    182     return space_size[which_space] > used ? space_size[which_space] - used : 0;
    183 }
    184 
    185 static void *lmalloc(size_t size, int which_space)
     168void *LSpace::Mark()
     169{
     170    return m_free;
     171}
     172
     173void LSpace::Restore(void *val)
     174{
     175    m_free = (uint8_t *)val;
     176}
     177
     178size_t LSpace::GetFree()
     179{
     180    size_t used = m_free - m_data;
     181    return m_size > used ? m_size - used : 0;
     182}
     183
     184void *LSpace::Alloc(size_t size)
    186185{
    187186    // Align allocation
     
    189188
    190189    // Collect garbage if necessary
    191     if (size > get_free_size(which_space))
    192     {
    193         if (which_space == PERM_SPACE || which_space == TMP_SPACE)
    194             LispGC::CollectSpace(which_space, 0);
    195 
    196         if (size > get_free_size(which_space))
    197             LispGC::CollectSpace(which_space, 1);
    198 
    199         if (size > get_free_size(which_space))
    200         {
    201             lbreak("lisp: cannot find %d bytes in space #%d\n",
    202                    size, which_space);
     190    if (size > GetFree())
     191    {
     192        if (this == &LSpace::Perm || this == &LSpace::Tmp)
     193            LispGC::CollectSpace(this, 0);
     194
     195        if (size > GetFree())
     196            LispGC::CollectSpace(this, 1);
     197
     198        if (size > GetFree())
     199        {
     200            lbreak("lisp: cannot find %d bytes in %s\n", size, m_name);
    203201            exit(0);
    204202        }
    205203    }
    206204
    207     void *ret = (void *)free_space[which_space];
    208     free_space[which_space] += size;
     205    void *ret = m_free;
     206    m_free += size;
    209207    return ret;
    210208}
     
    229227        size = sizeof(LRedirect);
    230228
    231     LArray *p = (LArray *)lmalloc(size, current_space);
     229    LArray *p = (LArray *)LSpace::Current->Alloc(size);
    232230    p->type = L_1D_ARRAY;
    233231    p->len = len;
     
    282280    size_t size = Max(sizeof(LFixedPoint), sizeof(LRedirect));
    283281
    284     LFixedPoint *p = (LFixedPoint *)lmalloc(size, current_space);
     282    LFixedPoint *p = (LFixedPoint *)LSpace::Current->Alloc(size);
    285283    p->type = L_FIXED_POINT;
    286284    p->x = x;
     
    292290    size_t size = Max(sizeof(LObjectVar), sizeof(LRedirect));
    293291
    294     LObjectVar *p = (LObjectVar *)lmalloc(size, current_space);
     292    LObjectVar *p = (LObjectVar *)LSpace::Current->Alloc(size);
    295293    p->type = L_OBJECT_VAR;
    296294    p->index = index;
     
    304302    size_t size = Max(sizeof(LPointer), sizeof(LRedirect));
    305303
    306     LPointer *p = (LPointer *)lmalloc(size, current_space);
     304    LPointer *p = (LPointer *)LSpace::Current->Alloc(size);
    307305    p->type = L_POINTER;
    308306    p->addr = addr;
     
    314312    size_t size = Max(sizeof(LChar), sizeof(LRedirect));
    315313
    316     LChar *c = (LChar *)lmalloc(size, current_space);
     314    LChar *c = (LChar *)LSpace::Current->Alloc(size);
    317315    c->type = L_CHARACTER;
    318316    c->ch = ch;
     
    339337    size_t size = Max(sizeof(LString) + length - 1, sizeof(LRedirect));
    340338
    341     LString *s = (LString *)lmalloc(size, current_space);
     339    LString *s = (LString *)LSpace::Current->Alloc(size);
    342340    s->type = L_STRING;
    343341    s->str[0] = '\0';
     
    351349    size_t size = Max(sizeof(LUserFunction), sizeof(LRedirect));
    352350
    353     LUserFunction *lu = (LUserFunction *)lmalloc(size, current_space);
     351    LUserFunction *lu = (LUserFunction *)LSpace::Current->Alloc(size);
    354352    lu->type = L_USER_FUNCTION;
    355353    lu->arg_list = arg_list;
     
    363361
    364362    // System functions should reside in permanant space
    365     int space = (current_space == GC_SPACE) ? GC_SPACE : PERM_SPACE;
    366     LSysFunction *ls = (LSysFunction *)lmalloc(size, space);
     363    LSysFunction *ls = LSpace::Current == &LSpace::Gc
     364                     ? (LSysFunction *)LSpace::Gc.Alloc(size)
     365                     : (LSysFunction *)LSpace::Perm.Alloc(size);
    367366    ls->type = L_SYS_FUNCTION;
    368367    ls->min_args = min_args;
     
    397396    size_t size = Max(sizeof(LSymbol), sizeof(LRedirect));
    398397
    399     LSymbol *s = (LSymbol *)lmalloc(size, current_space);
     398    LSymbol *s = (LSymbol *)LSpace::Current->Alloc(size);
    400399    PtrRef ref(s);
    401400
     
    414413    size_t size = Max(sizeof(LNumber), sizeof(LRedirect));
    415414
    416     LNumber *n = (LNumber *)lmalloc(size, current_space);
     415    LNumber *n = (LNumber *)LSpace::Current->Alloc(size);
    417416    n->type = L_NUMBER;
    418417    n->num = num;
     
    424423    size_t size = Max(sizeof(LList), sizeof(LRedirect));
    425424
    426     LList *c = (LList *)lmalloc(size, current_space);
     425    LList *c = (LList *)LSpace::Current->Alloc(size);
    427426    c->type = L_CONS_CELL;
    428427    c->car = NULL;
     
    772771  else
    773772  {
    774     int sp=current_space;
    775     if (current_space!=GC_SPACE)
    776       current_space=PERM_SPACE;       // make sure all symbols get defined in permanant space
     773    LSpace *sp = LSpace::Current;
     774    if (LSpace::Current != &LSpace::Gc)
     775      LSpace::Current = &LSpace::Perm;       // make sure all symbols get defined in permanant space
    777776    LList *cs;
    778777    cs=LList::Create();
     
    781780    cs->cdr=symbol_list;
    782781    symbol_list=cs;
    783     current_space=sp;
     782    LSpace::Current = sp;
    784783  }
    785784  return s;
     
    815814
    816815    // Make sure all symbols get defined in permanant space
    817     int sp = current_space;
    818     if (current_space != GC_SPACE)
    819        current_space = PERM_SPACE;
     816    LSpace *sp = LSpace::Current;
     817    if (LSpace::Current != &LSpace::Gc)
     818        LSpace::Current = &LSpace::Perm;
    820819
    821820    p = (LSymbol *)malloc(sizeof(LSymbol));
     
    833832    count++;
    834833
    835     current_space = sp;
     834    LSpace::Current = sp;
    836835    return p;
    837836}
     
    24012400                    stat_man->update((cs - s) * 100 / l);
    24022401#endif
    2403                 void *m = mark_heap(TMP_SPACE);
     2402                void *m = LSpace::Tmp.Mark();
    24042403                compiled_form = LObject::Compile(cs);
    24052404                compiled_form->Eval();
    24062405                compiled_form = NULL;
    2407                 restore_heap(m, TMP_SPACE);
     2406                LSpace::Tmp.Restore(m);
    24082407            }
    24092408#ifndef NO_LIBS
     
    24712470    case SYS_FUNC_ENUM:
    24722471    {
    2473         int sp = current_space;
    2474         current_space = PERM_SPACE;
     2472        LSpace *sp = LSpace::Current;
     2473        LSpace::Current = &LSpace::Perm;
    24752474        int32_t x = 0;
    24762475        while (arg_list)
     
    25112510            x++;
    25122511        }
    2513         current_space = sp;
     2512        LSpace::Current = sp;
    25142513        break;
    25152514    }
     
    28072806    }
    28082807    case SYS_FUNC_GC:
    2809         LispGC::CollectSpace(current_space, 0);
     2808        LispGC::CollectSpace(LSpace::Current, 0);
    28102809        break;
    28112810    case SYS_FUNC_SCHAR:
     
    29462945void tmp_space()
    29472946{
    2948     current_space = TMP_SPACE;
     2947    LSpace::Current = &LSpace::Tmp;
    29492948}
    29502949
    29512950void perm_space()
    29522951{
    2953     current_space = PERM_SPACE;
    2954 }
    2955 
    2956 void use_user_space(void *addr, long size)
    2957 {
    2958     current_space = USER_SPACE;
    2959     free_space[USER_SPACE] = space[USER_SPACE] = (uint8_t *)addr;
    2960     space_size[USER_SPACE] = size;
     2952    LSpace::Current = &LSpace::Perm;
    29612953}
    29622954
     
    30713063        {
    30723064            dprintf("%d (%d, %d, %d) TRACE : ", trace_level,
    3073                     get_free_size(PERM_SPACE), get_free_size(TMP_SPACE),
     3065                    LSpace::Perm.GetFree(), LSpace::Tmp.GetFree(),
    30743066                    PtrRef::stack.m_size);
    30753067            Print();
     
    31203112        if (trace_level <= trace_print_level)
    31213113            dprintf("%d (%d, %d, %d) TRACE ==> ", trace_level,
    3122                     get_free_size(PERM_SPACE), get_free_size(TMP_SPACE),
     3114                    LSpace::Perm.GetFree(), LSpace::Tmp.GetFree(),
    31233115                    PtrRef::stack.m_size);
    31243116        ret->Print();
     
    31273119
    31283120/*  l_user_stack.push(ret);
    3129   LispGC::CollectSpace(PERM_SPACE);
     3121  LispGC::CollectSpace(&LSpace::Perm);
    31303122  ret=l_user_stack.pop(1);  */
    31313123
     
    31353127void l_comp_init();
    31363128
    3137 void lisp_init()
     3129void Lisp::Init()
    31383130{
    31393131    LSymbol::root = NULL;
    31403132    total_user_functions = 0;
    31413133
    3142     free_space[0] = space[0] = (uint8_t *)malloc(0x1000);
    3143     space_size[0] = 0x1000;
    3144 
    3145     free_space[1] = space[1] = (uint8_t *)malloc(0x1000);
    3146     space_size[1] = 0x1000;
    3147 
    3148     current_space = PERM_SPACE;
     3134    LSpace::Tmp.m_free = LSpace::Tmp.m_data = (uint8_t *)malloc(0x1000);
     3135    LSpace::Tmp.m_size = 0x1000;
     3136    LSpace::Tmp.m_name = "temporary space";
     3137
     3138    LSpace::Perm.m_free = LSpace::Perm.m_data = (uint8_t *)malloc(0x1000);
     3139    LSpace::Perm.m_size = 0x1000;
     3140    LSpace::Perm.m_name = "permanent space";
     3141
     3142    LSpace::Gc.m_name = "garbage space";
     3143
     3144    LSpace::Current = &LSpace::Perm;
    31493145
    31503146    l_comp_init();
     
    31533149                         sys_funcs[i].min_args, sys_funcs[i].max_args, i);
    31543150    clisp_init();
    3155     current_space = TMP_SPACE;
     3151    LSpace::Current = &LSpace::Tmp;
    31563152    dprintf("Lisp: %d symbols defined, %d system functions, "
    31573153            "%d pre-compiled functions\n", LSymbol::count,
     
    31593155}
    31603156
    3161 void lisp_uninit()
    3162 {
    3163     free(space[0]);
    3164     free(space[1]);
     3157void Lisp::Uninit()
     3158{
     3159    free(LSpace::Tmp.m_data);
     3160    free(LSpace::Perm.m_data);
    31653161    DeleteAllSymbols(LSymbol::root);
    31663162    LSymbol::root = NULL;
     
    31683164}
    31693165
    3170 void clear_tmp()
    3171 {
    3172     free_space[TMP_SPACE] = space[TMP_SPACE];
     3166void LSpace::Clear()
     3167{
     3168    m_free = m_data;
    31733169}
    31743170
Note: See TracChangeset for help on using the changeset viewer.