comparison src/ralloc.c @ 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 6bf2c4766d4c
children 4b61400a5195
comparison
equal deleted inserted replaced
1086:273918bf0a95 1087:6c410cc87574
374 abort (); 374 abort ();
375 375
376 free_bloc (dead_bloc); 376 free_bloc (dead_bloc);
377 } 377 }
378 378
379 /* Given a pointer at address PTR to relocatable data, resize it 379 /* Given a pointer at address PTR to relocatable data, resize it to SIZE.
380 to SIZE. This is done by obtaining a new block and freeing the 380 This is done by shifting all blocks above this one up in memory,
381 old, unless SIZE is less than or equal to the current bloc size, 381 unless SIZE is less than or equal to the current bloc size, in
382 in which case nothing happens and the current value is returned. 382 which case nothing happens and the current value is returned.
383 383
384 The contents of PTR is changed to reflect the new bloc, and this 384 The contents of PTR is changed to reflect the new bloc, and this
385 value is returned. */ 385 value is returned. */
386 386
387 POINTER 387 POINTER
388 r_re_alloc (ptr, size) 388 r_re_alloc (ptr, size)
389 POINTER *ptr; 389 POINTER *ptr;
390 SIZE size; 390 SIZE size;
391 { 391 {
392 register bloc_ptr old_bloc, new_bloc; 392 register bloc_ptr bloc;
393 393
394 old_bloc = find_bloc (ptr); 394 bloc = find_bloc (ptr);
395 if (old_bloc == NIL_BLOC) 395 if (bloc == NIL_BLOC)
396 abort (); 396 abort ();
397 397
398 if (size <= old_bloc->size) 398 if (size <= bloc->size)
399 /* Wouldn't it be useful to actually resize the bloc here? */ 399 /* Wouldn't it be useful to actually resize the bloc here? */
400 return *ptr; 400 return *ptr;
401 401
402 new_bloc = get_bloc (size); 402 obtain (size - bloc->size);
403 new_bloc->variable = ptr; 403 relocate_some_blocs (bloc->next, bloc->data + size);
404 safe_bcopy (old_bloc->data, new_bloc->data, old_bloc->size); 404
405 *ptr = new_bloc->data; 405 /* Zero out the new space in the bloc, to help catch bugs faster. */
406 406 bzero (bloc->data + bloc->size, size - bloc->size);
407 free_bloc (old_bloc); 407
408 /* Indicate that this block has a new size. */
409 bloc->size = size;
408 410
409 return *ptr; 411 return *ptr;
410 } 412 }
411 413
412 /* The hook `malloc' uses for the function which gets more space 414 /* The hook `malloc' uses for the function which gets more space