Branch: Tag:

2006-01-20

2006-01-20 17:35:16 by Jonas Wallden <jonasw@roxen.com>

Backport from 7.7: Rewrote mapping_rehash_backwards_{good,evil}() to not
eat stack. Fixes out of C-stack errors when resizing unbalanced mappings.

Rev: src/mapping.c:1.182

2:   || This file is part of Pike. For copyright information see COPYRIGHT.   || Pike is distributed under GPL, LGPL and MPL. See the file COPYING   || for more information. - || $Id: mapping.c,v 1.181 2004/07/02 17:36:22 grubba Exp $ + || $Id: mapping.c,v 1.182 2006/01/20 17:35:16 jonasw Exp $   */      #include "global.h" - RCSID("$Id: mapping.c,v 1.181 2004/07/02 17:36:22 grubba Exp $"); + RCSID("$Id: mapping.c,v 1.182 2006/01/20 17:35:16 jonasw Exp $");   #include "main.h"   #include "object.h"   #include "mapping.h"
273:    struct keypair *from)   {    unsigned INT32 h; -  struct keypair *k; +  struct keypair *k, *prev = NULL, *next;    -  if(!from) return; -  mapping_rehash_backwards_evil(md,from->next); +  if(!(k = from)) return;    -  +  /* Reverse the hash chain. */ +  while ((next = k->next)) { +  k->next = prev; +  prev = k; +  k = next; +  } +  k->next = prev; +  +  from = k; +  +  /* Rehash and reverse the hash chain. */ +  while (from) {    /* unlink */    k=md->free_list;   #ifndef PIKE_MAPPING_KEYPAIR_LOOP
302:    md->ind_types |= 1<< (k->ind.type);    md->val_types |= 1<< (k->val.type);    md->size++; +  +  /* Reverse */ +  prev = from->next; +  from->next = next; +  next = from; +  from = prev;    } -  + }      static void mapping_rehash_backwards_good(struct mapping_data *md,    struct keypair *from)   {    unsigned INT32 h; -  struct keypair *k; +  struct keypair *k, *prev = NULL, *next;    -  if(!from) return; -  mapping_rehash_backwards_good(md,from->next); +  if(!(k = from)) return;    -  +  /* Reverse the hash chain. */ +  while ((next = k->next)) { +  k->next = prev; +  prev = k; +  k = next; +  } +  k->next = prev; +  +  from = k; +  +  /* Rehash and reverse the hash chain. */ +  while (from) {    /* unlink */    k=md->free_list;   #ifndef PIKE_MAPPING_KEYPAIR_LOOP
339:    md->ind_types |= 1<< (k->ind.type);    md->val_types |= 1<< (k->val.type);    md->size++; +  +  /* Reverse */ +  prev = from->next; +  from->next = next; +  next = from; +  from = prev;    } -  + }      /* This function re-allocates a mapping. It adjusts the max no. of    * values can be fitted into the mapping. It takes a bit of time to