ralloc.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  1. /* Block-relocating memory allocator.
  2. Copyright (C) 1993, 1995 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library. Its master source is NOT part of
  4. the C library, however. The master source lives in /gd/gnu/lib.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public License as
  7. published by the Free Software Foundation; either version 2 of the
  8. License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Library General Public License for more details.
  13. You should have received a copy of the GNU Library General Public
  14. License along with the GNU C Library; see the file COPYING.LIB. If
  15. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  16. Cambridge, MA 02139, USA. */
  17. /* NOTES:
  18. Only relocate the blocs necessary for SIZE in r_alloc_sbrk,
  19. rather than all of them. This means allowing for a possible
  20. hole between the first bloc and the end of malloc storage. */
  21. #ifdef emacs
  22. #include <config.h>
  23. #include "lisp.h" /* Needed for VALBITS. */
  24. #undef NULL
  25. /* The important properties of this type are that 1) it's a pointer, and
  26. 2) arithmetic on it should work as if the size of the object pointed
  27. to has a size of 1. */
  28. #if 0 /* Arithmetic on void* is a GCC extension. */
  29. #ifdef __STDC__
  30. typedef void *POINTER;
  31. #else
  32. #ifdef HAVE_CONFIG_H
  33. #include "config.h"
  34. #endif
  35. typedef char *POINTER;
  36. #endif
  37. #endif /* 0 */
  38. /* Unconditionally use char * for this. */
  39. typedef char *POINTER;
  40. typedef unsigned long SIZE;
  41. /* Declared in dispnew.c, this version doesn't screw up if regions
  42. overlap. */
  43. extern void safe_bcopy ();
  44. #include "getpagesize.h"
  45. #else /* Not emacs. */
  46. #include <stddef.h>
  47. typedef size_t SIZE;
  48. typedef void *POINTER;
  49. #include <unistd.h>
  50. #include <malloc.h>
  51. #include <string.h>
  52. #define safe_bcopy(x, y, z) memmove (y, x, z)
  53. #endif /* emacs. */
  54. #define NIL ((POINTER) 0)
  55. /* A flag to indicate whether we have initialized ralloc yet. For
  56. Emacs's sake, please do not make this local to malloc_init; on some
  57. machines, the dumping procedure makes all static variables
  58. read-only. On these machines, the word static is #defined to be
  59. the empty string, meaning that r_alloc_initialized becomes an
  60. automatic variable, and loses its value each time Emacs is started up. */
  61. static int r_alloc_initialized = 0;
  62. static void r_alloc_init ();
  63. /* Declarations for working with the malloc, ralloc, and system breaks. */
  64. /* Function to set the real break value. */
  65. static POINTER (*real_morecore) ();
  66. /* The break value, as seen by malloc. */
  67. static POINTER virtual_break_value;
  68. /* The address of the end of the last data in use by ralloc,
  69. including relocatable blocs as well as malloc data. */
  70. static POINTER break_value;
  71. /* This is the size of a page. We round memory requests to this boundary. */
  72. static int page_size;
  73. /* Whenever we get memory from the system, get this many extra bytes. This
  74. must be a multiple of page_size. */
  75. static int extra_bytes;
  76. /* Macros for rounding. Note that rounding to any value is possible
  77. by changing the definition of PAGE. */
  78. #define PAGE (getpagesize ())
  79. #define ALIGNED(addr) (((unsigned long int) (addr) & (page_size - 1)) == 0)
  80. #define ROUNDUP(size) (((unsigned long int) (size) + page_size - 1) \
  81. & ~(page_size - 1))
  82. #define ROUND_TO_PAGE(addr) (addr & (~(page_size - 1)))
  83. #define MEM_ALIGN sizeof(double)
  84. #define MEM_ROUNDUP(addr) (((unsigned long int)(addr) + MEM_ALIGN - 1) \
  85. & ~(MEM_ALIGN - 1))
  86. /* Data structures of heaps and blocs. */
  87. /* The relocatable objects, or blocs, and the malloc data
  88. both reside within one or more heaps.
  89. Each heap contains malloc data, running from `start' to `bloc_start',
  90. and relocatable objects, running from `bloc_start' to `free'.
  91. Relocatable objects may relocate within the same heap
  92. or may move into another heap; the heaps themselves may grow
  93. but they never move.
  94. We try to make just one heap and make it larger as necessary.
  95. But sometimes we can't do that, because we can't get continguous
  96. space to add onto the heap. When that happens, we start a new heap. */
  97. typedef struct heap
  98. {
  99. struct heap *next;
  100. struct heap *prev;
  101. /* Start of memory range of this heap. */
  102. POINTER start;
  103. /* End of memory range of this heap. */
  104. POINTER end;
  105. /* Start of relocatable data in this heap. */
  106. POINTER bloc_start;
  107. /* Start of unused space in this heap. */
  108. POINTER free;
  109. /* First bloc in this heap. */
  110. struct bp *first_bloc;
  111. /* Last bloc in this heap. */
  112. struct bp *last_bloc;
  113. } *heap_ptr;
  114. #define NIL_HEAP ((heap_ptr) 0)
  115. #define HEAP_PTR_SIZE (sizeof (struct heap))
  116. /* This is the first heap object.
  117. If we need additional heap objects, each one resides at the beginning of
  118. the space it covers. */
  119. static struct heap heap_base;
  120. /* Head and tail of the list of heaps. */
  121. static heap_ptr first_heap, last_heap;
  122. /* These structures are allocated in the malloc arena.
  123. The linked list is kept in order of increasing '.data' members.
  124. The data blocks abut each other; if b->next is non-nil, then
  125. b->data + b->size == b->next->data. */
  126. typedef struct bp
  127. {
  128. struct bp *next;
  129. struct bp *prev;
  130. POINTER *variable;
  131. POINTER data;
  132. SIZE size;
  133. POINTER new_data; /* tmporarily used for relocation */
  134. /* Heap this bloc is in. */
  135. struct heap *heap;
  136. } *bloc_ptr;
  137. #define NIL_BLOC ((bloc_ptr) 0)
  138. #define BLOC_PTR_SIZE (sizeof (struct bp))
  139. /* Head and tail of the list of relocatable blocs. */
  140. static bloc_ptr first_bloc, last_bloc;
  141. /* Functions to get and return memory from the system. */
  142. /* Find the heap that ADDRESS falls within. */
  143. static heap_ptr
  144. find_heap (address)
  145. POINTER address;
  146. {
  147. heap_ptr heap;
  148. for (heap = last_heap; heap; heap = heap->prev)
  149. {
  150. if (heap->start <= address && address <= heap->end)
  151. return heap;
  152. }
  153. return NIL_HEAP;
  154. }
  155. /* Find SIZE bytes of space in a heap.
  156. Try to get them at ADDRESS (which must fall within some heap's range)
  157. if we can get that many within one heap.
  158. If enough space is not presently available in our reserve, this means
  159. getting more page-aligned space from the system. If the retuned space
  160. is not contiguos to the last heap, allocate a new heap, and append it
  161. obtain does not try to keep track of whether space is in use
  162. or not in use. It just returns the address of SIZE bytes that
  163. fall within a single heap. If you call obtain twice in a row
  164. with the same arguments, you typically get the same value.
  165. to the heap list. It's the caller's responsibility to keep
  166. track of what space is in use.
  167. Return the address of the space if all went well, or zero if we couldn't
  168. allocate the memory. */
  169. static POINTER
  170. obtain (address, size)
  171. POINTER address;
  172. SIZE size;
  173. {
  174. heap_ptr heap;
  175. SIZE already_available;
  176. /* Find the heap that ADDRESS falls within. */
  177. for (heap = last_heap; heap; heap = heap->prev)
  178. {
  179. if (heap->start <= address && address <= heap->end)
  180. break;
  181. }
  182. if (! heap)
  183. abort ();
  184. /* If we can't fit SIZE bytes in that heap,
  185. try successive later heaps. */
  186. while (heap && address + size > heap->end)
  187. {
  188. heap = heap->next;
  189. if (heap == NIL_HEAP)
  190. break;
  191. address = heap->bloc_start;
  192. }
  193. /* If we can't fit them within any existing heap,
  194. get more space. */
  195. if (heap == NIL_HEAP)
  196. {
  197. POINTER new = (*real_morecore)(0);
  198. SIZE get;
  199. already_available = (char *)last_heap->end - (char *)address;
  200. if (new != last_heap->end)
  201. {
  202. /* Someone else called sbrk. Make a new heap. */
  203. heap_ptr new_heap = (heap_ptr) MEM_ROUNDUP (new);
  204. POINTER bloc_start = (POINTER) MEM_ROUNDUP ((POINTER)(new_heap + 1));
  205. if ((*real_morecore) (bloc_start - new) != new)
  206. return 0;
  207. new_heap->start = new;
  208. new_heap->end = bloc_start;
  209. new_heap->bloc_start = bloc_start;
  210. new_heap->free = bloc_start;
  211. new_heap->next = NIL_HEAP;
  212. new_heap->prev = last_heap;
  213. new_heap->first_bloc = NIL_BLOC;
  214. new_heap->last_bloc = NIL_BLOC;
  215. last_heap->next = new_heap;
  216. last_heap = new_heap;
  217. address = bloc_start;
  218. already_available = 0;
  219. }
  220. /* Add space to the last heap (which we may have just created).
  221. Get some extra, so we can come here less often. */
  222. get = size + extra_bytes - already_available;
  223. get = (char *) ROUNDUP ((char *)last_heap->end + get)
  224. - (char *) last_heap->end;
  225. if ((*real_morecore) (get) != last_heap->end)
  226. return 0;
  227. last_heap->end += get;
  228. }
  229. return address;
  230. }
  231. /* Return unused heap space to the system
  232. if there is a lot of unused space now.
  233. This can make the last heap smaller;
  234. it can also eliminate the last heap entirely. */
  235. static void
  236. relinquish ()
  237. {
  238. register heap_ptr h;
  239. int excess = 0;
  240. /* Add the amount of space beyond break_value
  241. in all heaps which have extend beyond break_value at all. */
  242. for (h = last_heap; h && break_value < h->end; h = h->prev)
  243. {
  244. excess += (char *) h->end - (char *) ((break_value < h->bloc_start)
  245. ? h->bloc_start : break_value);
  246. }
  247. if (excess > extra_bytes * 2 && (*real_morecore) (0) == last_heap->end)
  248. {
  249. /* Keep extra_bytes worth of empty space.
  250. And don't free anything unless we can free at least extra_bytes. */
  251. excess -= extra_bytes;
  252. if ((char *)last_heap->end - (char *)last_heap->bloc_start <= excess)
  253. {
  254. /* This heap should have no blocs in it. */
  255. if (last_heap->first_bloc != NIL_BLOC
  256. || last_heap->last_bloc != NIL_BLOC)
  257. abort ();
  258. /* Return the last heap, with its header, to the system. */
  259. excess = (char *)last_heap->end - (char *)last_heap->start;
  260. last_heap = last_heap->prev;
  261. last_heap->next = NIL_HEAP;
  262. }
  263. else
  264. {
  265. excess = (char *) last_heap->end
  266. - (char *) ROUNDUP ((char *)last_heap->end - excess);
  267. last_heap->end -= excess;
  268. }
  269. if ((*real_morecore) (- excess) == 0)
  270. abort ();
  271. }
  272. }
  273. /* The meat - allocating, freeing, and relocating blocs. */
  274. /* Find the bloc referenced by the address in PTR. Returns a pointer
  275. to that block. */
  276. static bloc_ptr
  277. find_bloc (ptr)
  278. POINTER *ptr;
  279. {
  280. register bloc_ptr p = first_bloc;
  281. while (p != NIL_BLOC)
  282. {
  283. if (p->variable == ptr && p->data == *ptr)
  284. return p;
  285. p = p->next;
  286. }
  287. return p;
  288. }
  289. /* Allocate a bloc of SIZE bytes and append it to the chain of blocs.
  290. Returns a pointer to the new bloc, or zero if we couldn't allocate
  291. memory for the new block. */
  292. static bloc_ptr
  293. get_bloc (size)
  294. SIZE size;
  295. {
  296. register bloc_ptr new_bloc;
  297. register heap_ptr heap;
  298. if (! (new_bloc = (bloc_ptr) malloc (BLOC_PTR_SIZE))
  299. || ! (new_bloc->data = obtain (break_value, size)))
  300. {
  301. if (new_bloc)
  302. free (new_bloc);
  303. return 0;
  304. }
  305. break_value = new_bloc->data + size;
  306. new_bloc->size = size;
  307. new_bloc->next = NIL_BLOC;
  308. new_bloc->variable = (POINTER *) NIL;
  309. new_bloc->new_data = 0;
  310. /* Record in the heap that this space is in use. */
  311. heap = find_heap (new_bloc->data);
  312. heap->free = break_value;
  313. /* Maintain the correspondence between heaps and blocs. */
  314. new_bloc->heap = heap;
  315. heap->last_bloc = new_bloc;
  316. if (heap->first_bloc == NIL_BLOC)
  317. heap->first_bloc = new_bloc;
  318. /* Put this bloc on the doubly-linked list of blocs. */
  319. if (first_bloc)
  320. {
  321. new_bloc->prev = last_bloc;
  322. last_bloc->next = new_bloc;
  323. last_bloc = new_bloc;
  324. }
  325. else
  326. {
  327. first_bloc = last_bloc = new_bloc;
  328. new_bloc->prev = NIL_BLOC;
  329. }
  330. return new_bloc;
  331. }
  332. /* Calculate new locations of blocs in the list beginning with BLOC,
  333. relocating it to start at ADDRESS, in heap HEAP. If enough space is
  334. not presently available in our reserve, call obtain for
  335. more space.
  336. Store the new location of each bloc in its new_data field.
  337. Do not touch the contents of blocs or break_value. */
  338. static int
  339. relocate_blocs (bloc, heap, address)
  340. bloc_ptr bloc;
  341. heap_ptr heap;
  342. POINTER address;
  343. {
  344. register bloc_ptr b = bloc;
  345. while (b)
  346. {
  347. /* If bloc B won't fit within HEAP,
  348. move to the next heap and try again. */
  349. while (heap && address + b->size > heap->end)
  350. {
  351. heap = heap->next;
  352. if (heap == NIL_HEAP)
  353. break;
  354. address = heap->bloc_start;
  355. }
  356. /* If BLOC won't fit in any heap,
  357. get enough new space to hold BLOC and all following blocs. */
  358. if (heap == NIL_HEAP)
  359. {
  360. register bloc_ptr tb = b;
  361. register SIZE s = 0;
  362. /* Add up the size of all the following blocs. */
  363. while (tb != NIL_BLOC)
  364. {
  365. s += tb->size;
  366. tb = tb->next;
  367. }
  368. /* Get that space. */
  369. address = obtain (address, s);
  370. if (address == 0)
  371. return 0;
  372. heap = last_heap;
  373. }
  374. /* Record the new address of this bloc
  375. and update where the next bloc can start. */
  376. b->new_data = address;
  377. address += b->size;
  378. b = b->next;
  379. }
  380. return 1;
  381. }
  382. /* Reorder the bloc BLOC to go before bloc BEFORE in the doubly linked list.
  383. This is necessary if we put the memory of space of BLOC
  384. before that of BEFORE. */
  385. static void
  386. reorder_bloc (bloc, before)
  387. bloc_ptr bloc, before;
  388. {
  389. bloc_ptr prev, next;
  390. /* Splice BLOC out from where it is. */
  391. prev = bloc->prev;
  392. next = bloc->next;
  393. if (prev)
  394. prev->next = next;
  395. if (next)
  396. next->prev = prev;
  397. /* Splice it in before BEFORE. */
  398. prev = before->prev;
  399. if (prev)
  400. prev->next = bloc;
  401. bloc->prev = prev;
  402. before->prev = bloc;
  403. bloc->next = before;
  404. }
  405. /* Update the records of which heaps contain which blocs, starting
  406. with heap HEAP and bloc BLOC. */
  407. static void
  408. update_heap_bloc_correspondence (bloc, heap)
  409. bloc_ptr bloc;
  410. heap_ptr heap;
  411. {
  412. register bloc_ptr b;
  413. /* Initialize HEAP's status to reflect blocs before BLOC. */
  414. if (bloc != NIL_BLOC && bloc->prev != NIL_BLOC && bloc->prev->heap == heap)
  415. {
  416. /* The previous bloc is in HEAP. */
  417. heap->last_bloc = bloc->prev;
  418. heap->free = bloc->prev->data + bloc->prev->size;
  419. }
  420. else
  421. {
  422. /* HEAP contains no blocs before BLOC. */
  423. heap->first_bloc = NIL_BLOC;
  424. heap->last_bloc = NIL_BLOC;
  425. heap->free = heap->bloc_start;
  426. }
  427. /* Advance through blocs one by one. */
  428. for (b = bloc; b != NIL_BLOC; b = b->next)
  429. {
  430. /* Advance through heaps, marking them empty,
  431. till we get to the one that B is in. */
  432. while (heap)
  433. {
  434. if (heap->bloc_start <= b->data && b->data <= heap->end)
  435. break;
  436. heap = heap->next;
  437. /* We know HEAP is not null now,
  438. because there has to be space for bloc B. */
  439. heap->first_bloc = NIL_BLOC;
  440. heap->last_bloc = NIL_BLOC;
  441. heap->free = heap->bloc_start;
  442. }
  443. /* Update HEAP's status for bloc B. */
  444. heap->free = b->data + b->size;
  445. heap->last_bloc = b;
  446. if (heap->first_bloc == NIL_BLOC)
  447. heap->first_bloc = b;
  448. /* Record that B is in HEAP. */
  449. b->heap = heap;
  450. }
  451. /* If there are any remaining heaps and no blocs left,
  452. mark those heaps as empty. */
  453. heap = heap->next;
  454. while (heap)
  455. {
  456. heap->first_bloc = NIL_BLOC;
  457. heap->last_bloc = NIL_BLOC;
  458. heap->free = heap->bloc_start;
  459. heap = heap->next;
  460. }
  461. }
  462. /* Resize BLOC to SIZE bytes. This relocates the blocs
  463. that come after BLOC in memory. */
  464. static int
  465. resize_bloc (bloc, size)
  466. bloc_ptr bloc;
  467. SIZE size;
  468. {
  469. register bloc_ptr b;
  470. heap_ptr heap;
  471. POINTER address;
  472. SIZE old_size;
  473. if (bloc == NIL_BLOC || size == bloc->size)
  474. return 1;
  475. for (heap = first_heap; heap != NIL_HEAP; heap = heap->next)
  476. {
  477. if (heap->bloc_start <= bloc->data && bloc->data <= heap->end)
  478. break;
  479. }
  480. if (heap == NIL_HEAP)
  481. abort ();
  482. old_size = bloc->size;
  483. bloc->size = size;
  484. /* Note that bloc could be moved into the previous heap. */
  485. address = (bloc->prev ? bloc->prev->data + bloc->prev->size
  486. : first_heap->bloc_start);
  487. while (heap)
  488. {
  489. if (heap->bloc_start <= address && address <= heap->end)
  490. break;
  491. heap = heap->prev;
  492. }
  493. if (! relocate_blocs (bloc, heap, address))
  494. {
  495. bloc->size = old_size;
  496. return 0;
  497. }
  498. if (size > old_size)
  499. {
  500. for (b = last_bloc; b != bloc; b = b->prev)
  501. {
  502. safe_bcopy (b->data, b->new_data, b->size);
  503. *b->variable = b->data = b->new_data;
  504. }
  505. safe_bcopy (bloc->data, bloc->new_data, old_size);
  506. bzero (bloc->new_data + old_size, size - old_size);
  507. *bloc->variable = bloc->data = bloc->new_data;
  508. }
  509. else
  510. {
  511. for (b = bloc; b != NIL_BLOC; b = b->next)
  512. {
  513. safe_bcopy (b->data, b->new_data, b->size);
  514. *b->variable = b->data = b->new_data;
  515. }
  516. }
  517. update_heap_bloc_correspondence (bloc, heap);
  518. break_value = (last_bloc ? last_bloc->data + last_bloc->size
  519. : first_heap->bloc_start);
  520. return 1;
  521. }
  522. /* Free BLOC from the chain of blocs, relocating any blocs above it.
  523. This may return space to the system. */
  524. static void
  525. free_bloc (bloc)
  526. bloc_ptr bloc;
  527. {
  528. heap_ptr heap = bloc->heap;
  529. resize_bloc (bloc, 0);
  530. if (bloc == first_bloc && bloc == last_bloc)
  531. {
  532. first_bloc = last_bloc = NIL_BLOC;
  533. }
  534. else if (bloc == last_bloc)
  535. {
  536. last_bloc = bloc->prev;
  537. last_bloc->next = NIL_BLOC;
  538. }
  539. else if (bloc == first_bloc)
  540. {
  541. first_bloc = bloc->next;
  542. first_bloc->prev = NIL_BLOC;
  543. }
  544. else
  545. {
  546. bloc->next->prev = bloc->prev;
  547. bloc->prev->next = bloc->next;
  548. }
  549. /* Update the records of which blocs are in HEAP. */
  550. if (heap->first_bloc == bloc)
  551. {
  552. if (bloc->next->heap == heap)
  553. heap->first_bloc = bloc->next;
  554. else
  555. heap->first_bloc = heap->last_bloc = NIL_BLOC;
  556. }
  557. if (heap->last_bloc == bloc)
  558. {
  559. if (bloc->prev->heap == heap)
  560. heap->last_bloc = bloc->prev;
  561. else
  562. heap->first_bloc = heap->last_bloc = NIL_BLOC;
  563. }
  564. relinquish ();
  565. free (bloc);
  566. }
  567. /* Interface routines. */
  568. static int use_relocatable_buffers;
  569. static int r_alloc_freeze_level;
  570. /* Obtain SIZE bytes of storage from the free pool, or the system, as
  571. necessary. If relocatable blocs are in use, this means relocating
  572. them. This function gets plugged into the GNU malloc's __morecore
  573. hook.
  574. We provide hysteresis, never relocating by less than extra_bytes.
  575. If we're out of memory, we should return zero, to imitate the other
  576. __morecore hook values - in particular, __default_morecore in the
  577. GNU malloc package. */
  578. POINTER
  579. r_alloc_sbrk (size)
  580. long size;
  581. {
  582. register bloc_ptr b;
  583. POINTER address;
  584. if (! use_relocatable_buffers)
  585. return (*real_morecore) (size);
  586. if (size == 0)
  587. return virtual_break_value;
  588. if (size > 0)
  589. {
  590. /* Allocate a page-aligned space. GNU malloc would reclaim an
  591. extra space if we passed an unaligned one. But we could
  592. not always find a space which is contiguos to the previous. */
  593. POINTER new_bloc_start;
  594. heap_ptr h = first_heap;
  595. SIZE get = ROUNDUP (size);
  596. address = (POINTER) ROUNDUP (virtual_break_value);
  597. /* Search the list upward for a heap which is large enough. */
  598. while ((char *) h->end < (char *) MEM_ROUNDUP ((char *)address + get))
  599. {
  600. h = h->next;
  601. if (h == NIL_HEAP)
  602. break;
  603. address = (POINTER) ROUNDUP (h->start);
  604. }
  605. /* If not found, obtain more space. */
  606. if (h == NIL_HEAP)
  607. {
  608. get += extra_bytes + page_size;
  609. if (r_alloc_freeze_level > 0 || ! obtain (address, get))
  610. return 0;
  611. if (first_heap == last_heap)
  612. address = (POINTER) ROUNDUP (virtual_break_value);
  613. else
  614. address = (POINTER) ROUNDUP (last_heap->start);
  615. h = last_heap;
  616. }
  617. new_bloc_start = (POINTER) MEM_ROUNDUP ((char *)address + get);
  618. if (first_heap->bloc_start < new_bloc_start)
  619. {
  620. /* Move all blocs upward. */
  621. if (r_alloc_freeze_level > 0
  622. || ! relocate_blocs (first_bloc, h, new_bloc_start))
  623. return 0;
  624. /* Note that (POINTER)(h+1) <= new_bloc_start since
  625. get >= page_size, so the following does not destroy the heap
  626. header. */
  627. for (b = last_bloc; b != NIL_BLOC; b = b->prev)
  628. {
  629. safe_bcopy (b->data, b->new_data, b->size);
  630. *b->variable = b->data = b->new_data;
  631. }
  632. h->bloc_start = new_bloc_start;
  633. update_heap_bloc_correspondence (first_bloc, h);
  634. }
  635. if (h != first_heap)
  636. {
  637. /* Give up managing heaps below the one the new
  638. virtual_break_value points to. */
  639. first_heap->prev = NIL_HEAP;
  640. first_heap->next = h->next;
  641. first_heap->start = h->start;
  642. first_heap->end = h->end;
  643. first_heap->free = h->free;
  644. first_heap->first_bloc = h->first_bloc;
  645. first_heap->last_bloc = h->last_bloc;
  646. first_heap->bloc_start = h->bloc_start;
  647. if (first_heap->next)
  648. first_heap->next->prev = first_heap;
  649. else
  650. last_heap = first_heap;
  651. }
  652. bzero (address, size);
  653. }
  654. else /* size < 0 */
  655. {
  656. SIZE excess = (char *)first_heap->bloc_start
  657. - ((char *)virtual_break_value + size);
  658. address = virtual_break_value;
  659. if (r_alloc_freeze_level == 0 && excess > 2 * extra_bytes)
  660. {
  661. excess -= extra_bytes;
  662. first_heap->bloc_start
  663. = (POINTER) MEM_ROUNDUP ((char *)first_heap->bloc_start - excess);
  664. relocate_blocs (first_bloc, first_heap, first_heap->bloc_start);
  665. for (b = first_bloc; b != NIL_BLOC; b = b->next)
  666. {
  667. safe_bcopy (b->data, b->new_data, b->size);
  668. *b->variable = b->data = b->new_data;
  669. }
  670. }
  671. if ((char *)virtual_break_value + size < (char *)first_heap->start)
  672. {
  673. /* We found an additional space below the first heap */
  674. first_heap->start = (POINTER) ((char *)virtual_break_value + size);
  675. }
  676. }
  677. virtual_break_value = (POINTER) ((char *)address + size);
  678. break_value = (last_bloc
  679. ? last_bloc->data + last_bloc->size
  680. : first_heap->bloc_start);
  681. if (size < 0)
  682. relinquish ();
  683. return address;
  684. }
  685. /* Allocate a relocatable bloc of storage of size SIZE. A pointer to
  686. the data is returned in *PTR. PTR is thus the address of some variable
  687. which will use the data area.
  688. If we can't allocate the necessary memory, set *PTR to zero, and
  689. return zero. */
  690. POINTER
  691. r_alloc (ptr, size)
  692. POINTER *ptr;
  693. SIZE size;
  694. {
  695. register bloc_ptr new_bloc;
  696. if (! r_alloc_initialized)
  697. r_alloc_init ();
  698. new_bloc = get_bloc (MEM_ROUNDUP (size));
  699. if (new_bloc)
  700. {
  701. new_bloc->variable = ptr;
  702. *ptr = new_bloc->data;
  703. }
  704. else
  705. *ptr = 0;
  706. return *ptr;
  707. }
  708. /* Free a bloc of relocatable storage whose data is pointed to by PTR.
  709. Store 0 in *PTR to show there's no block allocated. */
  710. void
  711. r_alloc_free (ptr)
  712. register POINTER *ptr;
  713. {
  714. register bloc_ptr dead_bloc;
  715. dead_bloc = find_bloc (ptr);
  716. if (dead_bloc == NIL_BLOC)
  717. abort ();
  718. free_bloc (dead_bloc);
  719. *ptr = 0;
  720. }
  721. /* Given a pointer at address PTR to relocatable data, resize it to SIZE.
  722. Do this by shifting all blocks above this one up in memory, unless
  723. SIZE is less than or equal to the current bloc size, in which case
  724. do nothing.
  725. Change *PTR to reflect the new bloc, and return this value.
  726. If more memory cannot be allocated, then leave *PTR unchanged, and
  727. return zero. */
  728. POINTER
  729. r_re_alloc (ptr, size)
  730. POINTER *ptr;
  731. SIZE size;
  732. {
  733. register bloc_ptr bloc;
  734. bloc = find_bloc (ptr);
  735. if (bloc == NIL_BLOC)
  736. abort ();
  737. if (size <= bloc->size)
  738. /* Wouldn't it be useful to actually resize the bloc here? */
  739. return *ptr;
  740. if (! resize_bloc (bloc, MEM_ROUNDUP (size)))
  741. return 0;
  742. return *ptr;
  743. }
  744. /* Disable relocations, after making room for at least SIZE bytes
  745. of non-relocatable heap if possible. The relocatable blocs are
  746. guaranteed to hold still until thawed, even if this means that
  747. malloc must return a null pointer. */
  748. void
  749. r_alloc_freeze (size)
  750. long size;
  751. {
  752. /* If already frozen, we can't make any more room, so don't try. */
  753. if (r_alloc_freeze_level > 0)
  754. size = 0;
  755. /* If we can't get the amount requested, half is better than nothing. */
  756. while (size > 0 && r_alloc_sbrk (size) == 0)
  757. size /= 2;
  758. ++r_alloc_freeze_level;
  759. if (size > 0)
  760. r_alloc_sbrk (-size);
  761. }
  762. void
  763. r_alloc_thaw ()
  764. {
  765. if (--r_alloc_freeze_level < 0)
  766. abort ();
  767. }
  768. /* The hook `malloc' uses for the function which gets more space
  769. from the system. */
  770. extern POINTER (*__morecore) ();
  771. /* Initialize various things for memory allocation. */
  772. static void
  773. r_alloc_init ()
  774. {
  775. if (r_alloc_initialized)
  776. return;
  777. r_alloc_initialized = 1;
  778. real_morecore = __morecore;
  779. __morecore = r_alloc_sbrk;
  780. first_heap = last_heap = &heap_base;
  781. first_heap->next = first_heap->prev = NIL_HEAP;
  782. first_heap->start = first_heap->bloc_start
  783. = virtual_break_value = break_value = (*real_morecore) (0);
  784. if (break_value == NIL)
  785. abort ();
  786. page_size = PAGE;
  787. extra_bytes = ROUNDUP (50000);
  788. first_heap->end = (POINTER) ROUNDUP (first_heap->start);
  789. /* The extra call to real_morecore guarantees that the end of the
  790. address space is a multiple of page_size, even if page_size is
  791. not really the page size of the system running the binary in
  792. which page_size is stored. This allows a binary to be built on a
  793. system with one page size and run on a system with a smaller page
  794. size. */
  795. (*real_morecore) (first_heap->end - first_heap->start);
  796. /* Clear the rest of the last page; this memory is in our address space
  797. even though it is after the sbrk value. */
  798. /* Doubly true, with the additional call that explicitly adds the
  799. rest of that page to the address space. */
  800. bzero (first_heap->start, first_heap->end - first_heap->start);
  801. virtual_break_value = break_value = first_heap->bloc_start = first_heap->end;
  802. use_relocatable_buffers = 1;
  803. }
  804. #ifdef DEBUG
  805. #include <assert.h>
  806. int
  807. r_alloc_check ()
  808. {
  809. int found = 0;
  810. heap_ptr h, ph = 0;
  811. bloc_ptr b, pb = 0;
  812. if (!r_alloc_initialized)
  813. return;
  814. assert (first_heap);
  815. assert (last_heap->end <= (POINTER) sbrk (0));
  816. assert ((POINTER) first_heap < first_heap->start);
  817. assert (first_heap->start <= virtual_break_value);
  818. assert (virtual_break_value <= first_heap->end);
  819. for (h = first_heap; h; h = h->next)
  820. {
  821. assert (h->prev == ph);
  822. assert ((POINTER) ROUNDUP (h->end) == h->end);
  823. assert ((POINTER) MEM_ROUNDUP (h->start) == h->start);
  824. assert ((POINTER) MEM_ROUNDUP (h->bloc_start) == h->bloc_start);
  825. assert (h->start <= h->bloc_start && h->bloc_start <= h->end);
  826. if (ph)
  827. {
  828. assert (ph->end < h->start);
  829. assert (h->start <= (POINTER)h && (POINTER)(h+1) <= h->bloc_start);
  830. }
  831. if (h->bloc_start <= break_value && break_value <= h->end)
  832. found = 1;
  833. ph = h;
  834. }
  835. assert (found);
  836. assert (last_heap == ph);
  837. for (b = first_bloc; b; b = b->next)
  838. {
  839. assert (b->prev == pb);
  840. assert ((POINTER) MEM_ROUNDUP (b->data) == b->data);
  841. assert ((SIZE) MEM_ROUNDUP (b->size) == b->size);
  842. ph = 0;
  843. for (h = first_heap; h; h = h->next)
  844. {
  845. if (h->bloc_start <= b->data && b->data + b->size <= h->end)
  846. break;
  847. ph = h;
  848. }
  849. assert (h);
  850. if (pb && pb->data + pb->size != b->data)
  851. {
  852. assert (ph && b->data == h->bloc_start);
  853. while (ph)
  854. {
  855. if (ph->bloc_start <= pb->data
  856. && pb->data + pb->size <= ph->end)
  857. {
  858. assert (pb->data + pb->size + b->size > ph->end);
  859. break;
  860. }
  861. else
  862. {
  863. assert (ph->bloc_start + b->size > ph->end);
  864. }
  865. ph = ph->prev;
  866. }
  867. }
  868. pb = b;
  869. }
  870. assert (last_bloc == pb);
  871. if (last_bloc)
  872. assert (last_bloc->data + last_bloc->size == break_value);
  873. else
  874. assert (first_heap->bloc_start == break_value);
  875. }
  876. #endif /* DEBUG */