Ignore:
Timestamp:
Apr 17, 2011, 10:27:59 AM (12 years ago)
Author:
Sam Hocevar
Message:

lisp: start refactoring the core engine and garbage collector.

File:
1 edited

Legend:

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

    r162 r480  
    3636/* To bypass the whole garbage collection issue of lisp I am going to have
    3737 * separate spaces where lisp objects can reside.  Compiled code and gloabal
    38  * varibles will reside in permanant space.  Eveything else will reside in
     38 * variables will reside in permanant space.  Eveything else will reside in
    3939 * tmp space which gets thrown away after completion of eval.  system
    4040 * functions reside in permant space. */
     
    4646
    4747
    48 char *space[4], *free_space[4];
     48uint8_t *space[4], *free_space[4];
    4949int space_size[4], print_level=0, trace_level=0, trace_print_level=1000;
    5050int total_user_functions;
     
    167167void restore_heap(void *val, int heap)
    168168{
    169   free_space[heap]=(char *)val;
     169  free_space[heap] = (uint8_t *)val;
     170}
     171
     172static int get_free_size(int which_space)
     173{
     174    return space_size[which_space]
     175            - (free_space[which_space] - space[which_space]);
    170176}
    171177
    172178void *lmalloc(int size, int which_space)
    173179{
    174   return malloc(size); /* XXX FIXME: do we want to fix this one day? */
     180  return malloc(size);
    175181
    176182#ifdef WORD_ALIGN
     
    178184#endif
    179185
    180   if ((char *)free_space[which_space]-(char *)space[which_space]+size>space_size[which_space])
    181   {
    182     int fart=1;
    183     if (which_space==PERM_SPACE)
    184     {
    185       collect_space(PERM_SPACE);
    186       if ((char *)free_space[which_space]-(char *)space[which_space]+size<=space_size[which_space])
    187         fart=0;
    188     } else if (which_space==TMP_SPACE)
    189     {
    190       collect_space(TMP_SPACE);
    191       if ((char *)free_space[which_space]-(char *)space[which_space]+size<=space_size[which_space])
    192         fart=0;
    193     }
     186  if (size > get_free_size(which_space))
     187  {
     188    int fart = 1;
     189    fprintf(stderr, "%i > %i !!!\n", size, get_free_size(which_space));
     190
     191    if (which_space == PERM_SPACE || which_space == TMP_SPACE)
     192    {
     193      collect_space(which_space);
     194      if (size <= get_free_size(which_space))
     195        fart = 0;
     196    }
     197
    194198    if (fart)
    195199    {
    196       lbreak("lisp : cannot malloc %d bytes in space #%d\n", size, which_space);
     200      lbreak("lisp: cannot malloc %d bytes in space #%d\n", size, which_space);
    197201      exit(0);
    198202    }
    199   }
    200   void *ret=(void *)free_space[which_space];
    201   free_space[which_space]+=size;
     203    fprintf(stderr, "%i <= %i\n", size, get_free_size(which_space));
     204  }
     205  void *ret = (void *)free_space[which_space];
     206  free_space[which_space] += size;
    202207  return ret;
    203208}
     
    218223{
    219224  p_ref r11(rest);
    220   long s=sizeof(lisp_1d_array)+size*sizeof(void *);
    221   if (s<8) s=8;
     225  size_t s=sizeof(lisp_1d_array)+size*sizeof(void *);
     226  if (s<8 + sizeof(intptr_t)) s=8 + sizeof(intptr_t);
    222227  void *p=(lisp_1d_array *)lmalloc(s, current_space);
    223228  ((lisp_1d_array *)p)->type=L_1D_ARRAY;
     
    301306struct lisp_string *new_lisp_string(char const *string)
    302307{
    303   int size=sizeof(lisp_string)+strlen(string)+1;
    304   if (size<8) size=8;
     308  size_t size=sizeof(lisp_string)+strlen(string)+1;
     309  if (size<8 + sizeof(intptr_t)) size=8 + sizeof(intptr_t);
    305310
    306311  lisp_string *s=(lisp_string *)lmalloc(size, current_space);
     
    313318struct lisp_string *new_lisp_string(char const *string, int length)
    314319{
    315   int size=sizeof(lisp_string)+length+1;
    316   if (size<8) size=8;
     320  size_t size=sizeof(lisp_string)+length+1;
     321  if (size<8 + sizeof(intptr_t)) size=8 + sizeof(intptr_t);
    317322  lisp_string *s=(lisp_string *)lmalloc(size, current_space);
    318323  s->type=L_STRING;
     
    325330struct lisp_string *new_lisp_string(int length)
    326331{
    327   int size=sizeof(lisp_string)+length;
    328   if (size<8) size=8;
     332  size_t size=sizeof(lisp_string)+length;
     333  if (size<8 + sizeof(intptr_t)) size=8 + sizeof(intptr_t);
    329334  lisp_string *s=(lisp_string *)lmalloc(size, current_space);
    330335  s->type=L_STRING;
     
    28872892void use_user_space(void *addr, long size)
    28882893{
    2889   current_space=2;
    2890   free_space[USER_SPACE]=space[USER_SPACE]=(char *)addr;
    2891   space_size[USER_SPACE]=size;
     2894  current_space = 2;
     2895  free_space[USER_SPACE] = space[USER_SPACE] = (uint8_t *)addr;
     2896  space_size[USER_SPACE] = size;
    28922897}
    28932898
     
    31063111  } else if (free_space[TMP_SPACE]==space[TMP_SPACE])
    31073112  {
    3108     free_space[TMP_SPACE]=space[TMP_SPACE]=(char *)realloc(space[TMP_SPACE], new_size);
    3109     space_size[TMP_SPACE]=new_size;
     3113    free_space[TMP_SPACE] = space[TMP_SPACE] = (uint8_t *)realloc(space[TMP_SPACE], new_size);
     3114    space_size[TMP_SPACE] = new_size;
    31103115    dprintf("Lisp : tmp space resized to %d\n", new_size);
    31113116  } else dprintf("Lisp :tmp not empty, cannot resize\n");
     
    31163121{
    31173122  unsigned int i;
    3118   lsym_root=NULL;
    3119   total_user_functions=0;
    3120   free_space[0]=space[0]=(char *)malloc(perm_size);
    3121   space_size[0]=perm_size;
    3122 
    3123 
    3124   free_space[1]=space[1]=(char *)malloc(tmp_size);
    3125   space_size[1]=tmp_size;
     3123  lsym_root = NULL;
     3124  total_user_functions = 0;
     3125
     3126  free_space[0] = space[0] = (uint8_t *)malloc(perm_size);
     3127  space_size[0] = perm_size;
     3128
     3129  free_space[1] = space[1] = (uint8_t *)malloc(tmp_size);
     3130  space_size[1] = tmp_size;
    31263131
    31273132
Note: See TracChangeset for help on using the changeset viewer.