comparison src/ralloc.c @ 8951:b628561b185b

(r_alloc_freeze_level): New variable. (r_alloc_freeze, r_alloc_thaw): New functions. (r_alloc_sbrk): Refuse to move blocs, if frozen.
author Karl Heuer <kwzh@gnu.org>
date Tue, 20 Sep 1994 05:51:50 +0000
parents 9ab921a16318
children a1569f00a6a6
comparison
equal deleted inserted replaced
8950:68ad2f08d735 8951:b628561b185b
331 } 331 }
332 332
333 /* Interface routines. */ 333 /* Interface routines. */
334 334
335 static int use_relocatable_buffers; 335 static int use_relocatable_buffers;
336 static int r_alloc_freeze_level;
336 337
337 /* Obtain SIZE bytes of storage from the free pool, or the system, as 338 /* Obtain SIZE bytes of storage from the free pool, or the system, as
338 necessary. If relocatable blocs are in use, this means relocating 339 necessary. If relocatable blocs are in use, this means relocating
339 them. This function gets plugged into the GNU malloc's __morecore 340 them. This function gets plugged into the GNU malloc's __morecore
340 hook. 341 hook.
368 if (size > 0 && already_available < size) 369 if (size > 0 && already_available < size)
369 { 370 {
370 /* Get what we need, plus some extra so we can come here less often. */ 371 /* Get what we need, plus some extra so we can come here less often. */
371 SIZE get = size - already_available + extra_bytes; 372 SIZE get = size - already_available + extra_bytes;
372 373
373 if (! obtain (get)) 374 if (r_alloc_freeze_level > 0 || ! obtain (get))
374 return 0; 375 return 0;
375 376
376 if (first_bloc) 377 if (first_bloc)
377 relocate_some_blocs (first_bloc, first_bloc->data + get); 378 relocate_some_blocs (first_bloc, first_bloc->data + get);
378 379
379 /* Zero out the space we just allocated, to help catch bugs 380 /* Zero out the space we just allocated, to help catch bugs
380 quickly. */ 381 quickly. */
381 bzero (virtual_break_value, get); 382 bzero (virtual_break_value, get);
382 } 383 }
383 /* Can we keep extra_bytes of gap while freeing at least extra_bytes? */ 384 /* Can we keep extra_bytes of gap while freeing at least extra_bytes? */
384 else if (size < 0 && already_available - size > 2 * extra_bytes) 385 else if (size < 0 && already_available - size > 2 * extra_bytes
386 && r_alloc_freeze_level == 0)
385 { 387 {
386 /* Ok, do so. This is how many to free. */ 388 /* Ok, do so. This is how many to free. */
387 SIZE give_back = already_available - size - extra_bytes; 389 SIZE give_back = already_available - size - extra_bytes;
388 390
389 if (first_bloc) 391 if (first_bloc)
478 480
479 /* Indicate that this block has a new size. */ 481 /* Indicate that this block has a new size. */
480 bloc->size = size; 482 bloc->size = size;
481 483
482 return *ptr; 484 return *ptr;
485 }
486
487 /* Disable relocations, after making room for at least SIZE bytes
488 of non-relocatable heap if possible. The relocatable blocs are
489 guaranteed to hold still until thawed, even if this means that
490 malloc must return a null pointer. */
491 void
492 r_alloc_freeze (size)
493 long size;
494 {
495 /* If already frozen, we can't make any more room, so don't try. */
496 if (r_alloc_freeze_level > 0)
497 size = 0;
498 /* If we can't get the amount requested, half is better than nothing. */
499 while (size > 0 && r_alloc_sbrk (size) == 0)
500 size /= 2;
501 ++r_alloc_freeze_level;
502 if (size > 0)
503 r_alloc_sbrk (-size);
504 }
505
506 void
507 r_alloc_thaw ()
508 {
509 if (--r_alloc_freeze_level < 0)
510 abort ();
483 } 511 }
484 512
485 /* The hook `malloc' uses for the function which gets more space 513 /* The hook `malloc' uses for the function which gets more space
486 from the system. */ 514 from the system. */
487 extern POINTER (*__morecore) (); 515 extern POINTER (*__morecore) ();