changeset 1087:6c410cc87574

* ralloc.c (r_re_alloc): Instead of allocating a new bloc at the end of the heap, copying the data to it, and then freeing the original bloc, just expand the original block. This saves a copy and a call to sbrk, and also removes the large spike in memory allocation that would occur when resizing large buffers. And it's less code.
author Jim Blandy <jimb@redhat.com>
date Wed, 09 Sep 1992 00:05:42 +0000
parents 273918bf0a95
children c4400ec7bee0
files src/ralloc.c
diffstat 1 files changed, 15 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/src/ralloc.c	Sat Sep 05 23:19:22 1992 +0000
+++ b/src/ralloc.c	Wed Sep 09 00:05:42 1992 +0000
@@ -376,10 +376,10 @@
   free_bloc (dead_bloc);
 }
 
-/* Given a pointer at address PTR to relocatable data, resize it
-   to SIZE.  This is done by obtaining a new block and freeing the
-   old, unless SIZE is less than or equal to the current bloc size,
-   in which case nothing happens and the current value is returned.
+/* Given a pointer at address PTR to relocatable data, resize it to SIZE.
+   This is done by shifting all blocks above this one up in memory,
+   unless SIZE is less than or equal to the current bloc size, in
+   which case nothing happens and the current value is returned.
 
    The contents of PTR is changed to reflect the new bloc, and this
    value is returned. */
@@ -389,22 +389,24 @@
      POINTER *ptr;
      SIZE size;
 {
-  register bloc_ptr old_bloc, new_bloc;
+  register bloc_ptr bloc;
 
-  old_bloc = find_bloc (ptr);
-  if (old_bloc == NIL_BLOC)
+  bloc = find_bloc (ptr);
+  if (bloc == NIL_BLOC)
     abort ();
 
-  if (size <= old_bloc->size)
+  if (size <= bloc->size)
     /* Wouldn't it be useful to actually resize the bloc here?  */
     return *ptr;
 
-  new_bloc = get_bloc (size);
-  new_bloc->variable = ptr;
-  safe_bcopy (old_bloc->data, new_bloc->data, old_bloc->size);
-  *ptr = new_bloc->data;
+  obtain (size - bloc->size);
+  relocate_some_blocs (bloc->next, bloc->data + size);
 
-  free_bloc (old_bloc);
+  /* Zero out the new space in the bloc, to help catch bugs faster.  */
+  bzero (bloc->data + bloc->size, size - bloc->size);
+  
+  /* Indicate that this block has a new size.  */
+  bloc->size = size;
 
   return *ptr;
 }