Branch: Tag:

2019-11-14

2019-11-14 09:46:57 by Henrik Grubbström (Grubba) <grubba@grubba.org>

Compiler: Move node_allocator to struct compilation.

Allocated nodes may need to live during an entire translation unit.

This moves the allocator from struct Pike_compiler (ie compilation.h)
to struct compilation (aka CompilationEnvironment.PikeCompiler).

Fixes use of already freed nodes after certain syntax errors.

465:   void init_node_s_blocks() { }      void really_free_node_s(node * n) { -  ba_free(&Pike_compiler->node_allocator, n); +  ba_free(&Pike_compiler->compiler->node_allocator, n);   }      MALLOC_FUNCTION   node * alloc_node_s() { -  return ba_alloc(&Pike_compiler->node_allocator); +  return ba_alloc(&Pike_compiler->compiler->node_allocator);   }      void count_memory_in_node_ss(size_t * num, size_t * size) {
481:       while (state) {    size_t _num, _size; -  ba_count_all(&state->node_allocator, &_num, &_size); +  ba_count_all(&state->compiler->node_allocator, &_num, &_size);    *num += _num;    *size += _size;    state = state->previous;    }   }    - void node_walker(struct ba_iterator * it, void * UNUSED(data)) { -  do { -  node * tmp = ba_it_val(it); -  -  /* -  * since we free nodes from here, we might iterate over them again. -  * to avoid that we check for the free mark. -  */ -  if (tmp->token == USHRT_MAX) continue; -  - #ifdef PIKE_DEBUG -  if(!cumulative_parse_error) -  { -  fprintf(stderr,"Free node at %p, (%s:%ld) (token=%d).\n", -  (void *)tmp, -  tmp->current_file->str, (long)tmp->line_number, -  tmp->token); -  -  debug_malloc_dump_references(tmp,0,2,0); -  -  if(tmp->token==F_CONSTANT) -  print_tree(tmp); -  } -  /* else */ - #endif -  { -  /* Free the node and be happy */ -  /* Make sure we don't free any nodes twice */ -  if(car_is_node(tmp)) _CAR(tmp)=0; -  if(cdr_is_node(tmp)) _CDR(tmp)=0; - #ifdef PIKE_DEBUG -  if (l_flag > 3) { -  fprintf(stderr, "Freeing node that had %d refs.\n", -  tmp->refs); -  } - #endif /* PIKE_DEBUG */ -  /* Force the node to be freed. */ -  tmp->refs = 1; -  debug_malloc_touch(tmp->type); -  free_node(tmp); -  } -  } while (ba_it_step(it)); - } -  - void free_all_nodes(void) - { -  node *tmp; -  - #ifndef PIKE_DEBUG -  if(cumulative_parse_error) { - #endif -  ba_walk(&Pike_compiler->node_allocator, &node_walker, NULL); - #ifdef PIKE_DEBUG -  if(!cumulative_parse_error) { -  size_t n, s; -  ba_count_all(&Pike_compiler->node_allocator, &n, &s); -  if (n) -  Pike_fatal("Failed to free %"PRINTSIZET"d nodes when compiling!\n",n); -  } - #else -  } - #endif -  cumulative_parse_error=0; - } -  +    void debug_free_node(node *n)   {    if(!n) return;