home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / lucid / lemacs-19.6 / src / ralloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-29  |  9.4 KB  |  420 lines

  1. /* Block-relocating memory allocator. 
  2.    Copyright (C) 1990 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Emacs.
  5.  
  6. GNU Emacs is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU Emacs is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Emacs; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* NOTES:
  21.  
  22.    Only relocate the blocs neccessary for SIZE in r_alloc_sbrk,
  23.    rather than all of them.  This means allowing for a possible
  24.    hole between the first bloc and the end of malloc storage. */
  25.  
  26. #include "config.h"
  27. #include "lisp.h"        /* Needed for xterm.h */
  28. #undef NULL
  29. #include "mem_limits.h"
  30.  
  31. #define NIL ((POINTER *) 0)
  32.  
  33.  
  34. /* System call to set the break value. */
  35. extern POINTER sbrk ();
  36.  
  37. /* The break value, as seen by malloc (). */
  38. static POINTER virtual_break_value;
  39.  
  40. /* The break value, viewed by the relocatable blocs. */
  41. static POINTER break_value;
  42.  
  43. /* The REAL (i.e., page aligned) break value of the process. */
  44. static POINTER page_break_value;
  45.  
  46. /* Macros for rounding.  Note that rounding to any value is possible
  47.    by changing the definition of PAGE. */
  48. #define PAGE (getpagesize ())
  49. #define ALIGNED(addr) (((unsigned int) (addr) & (PAGE - 1)) == 0)
  50. #define ROUNDUP(size) (((unsigned int) (size) + PAGE) & ~(PAGE - 1))
  51. #define ROUND_TO_PAGE(addr) (addr & (~(PAGE - 1)))
  52. #define EXCEEDS_ELISP_PTR(ptr) ((unsigned int) (ptr) >> VALBITS)
  53.  
  54. /* Level of warnings issued. */
  55. static int warnlevel;
  56.  
  57. /* Function to call to issue a warning;
  58.    0 means don't issue them.  */
  59. static void (*warnfunction) ();
  60.  
  61. static void
  62. check_memory_limits (address)
  63.      POINTER address;
  64. {
  65.   SIZE data_size = address - data_space_start;
  66.  
  67.   switch (warnlevel)
  68.     {
  69.     case 0: 
  70.       if (data_size > (lim_data / 4) * 3)
  71.     {
  72.       warnlevel++;
  73.       (*warnfunction) ("Warning: past 75% of memory limit");
  74.     }
  75.       break;
  76.  
  77.     case 1: 
  78.       if (data_size > (lim_data / 20) * 17)
  79.     {
  80.       warnlevel++;
  81.       (*warnfunction) ("Warning: past 85% of memory limit");
  82.     }
  83.       break;
  84.  
  85.     case 2: 
  86.       if (data_size > (lim_data / 20) * 19)
  87.     {
  88.       warnlevel++;
  89.       (*warnfunction) ("Warning: past 95% of memory limit");
  90.     }
  91.       break;
  92.  
  93.     default:
  94.       (*warnfunction) ("Warning: past acceptable memory limits");
  95.       break;
  96.     }
  97.  
  98.     if (EXCEEDS_ELISP_PTR (address))
  99.       (*warnfunction) ("Warning: memory in use exceeds lisp pointer size");
  100. }
  101.  
  102. /* Obtain SIZE bytes of space.  If enough space is not presently available
  103.    in our process reserve, (i.e., (page_break_value - break_value)),
  104.    this means getting more page-aligned space from the system. */
  105.  
  106. static void
  107. obtain (size)
  108.      SIZE size;
  109. {
  110.   SIZE already_available = page_break_value - break_value;
  111.  
  112.   if (already_available < size)
  113.     {
  114.       SIZE get = ROUNDUP (size);
  115.  
  116.       if (warnfunction)
  117.     check_memory_limits (page_break_value);
  118.  
  119.       if (((int) sbrk (get)) < 0)
  120.     abort ();
  121.  
  122.       page_break_value += get;
  123.     }
  124.  
  125.   break_value += size;
  126. }
  127.  
  128. /* Obtain SIZE bytes of space and return a pointer to the new area. */
  129.  
  130. static POINTER
  131. get_more_space (size)
  132.      SIZE size;
  133. {
  134.   POINTER ptr = break_value;
  135.   obtain (size);
  136.   return ptr;
  137. }
  138.  
  139. /* Note that SIZE bytes of space have been relinquished by the process.
  140.    If SIZE is more than a page, return the space the system. */
  141.  
  142. static void
  143. relinquish (size)
  144.      SIZE size;
  145. {
  146.   SIZE page_part = ROUND_TO_PAGE (size);
  147.  
  148.   if (page_part)
  149.     {
  150.       if (((int) (sbrk (- page_part))) < 0)
  151.     abort ();
  152.  
  153.       page_break_value -= page_part;
  154.     }
  155.  
  156.   break_value -= size;
  157.   bzero (break_value, (size - page_part));
  158. }
  159.  
  160. typedef struct bp
  161. {
  162.   struct bp *next;
  163.   struct bp *prev;
  164.   POINTER *variable;
  165.   POINTER data;
  166.   SIZE size;
  167. } *bloc_ptr;
  168.  
  169. #define NIL_BLOC ((bloc_ptr) 0)
  170. #define BLOC_PTR_SIZE (sizeof (struct bp))
  171.  
  172. /* Head and tail of the list of relocatable blocs. */
  173. static bloc_ptr first_bloc, last_bloc;
  174.  
  175. /* Declared in dispnew.c, this version dosen't fuck up if regions overlap. */
  176. extern void safe_bcopy ();
  177.  
  178. /* Find the bloc reference by the address in PTR.  Returns a pointer
  179.    to that block. */
  180.  
  181. static bloc_ptr
  182. find_bloc (ptr)
  183.      POINTER *ptr;
  184. {
  185.   register bloc_ptr p = first_bloc;
  186.  
  187.   while (p != NIL_BLOC)
  188.     {
  189.       if (p->variable == ptr && p->data == *ptr)
  190.     return p;
  191.  
  192.       p = p->next;
  193.     }
  194.  
  195.   return p;
  196. }
  197.  
  198. /* Allocate a bloc of SIZE bytes and append it to the chain of blocs.
  199.    Returns a pointer to the new bloc. */
  200.  
  201. static bloc_ptr
  202. get_bloc (size)
  203.      SIZE size;
  204. {
  205.   register bloc_ptr new_bloc = (bloc_ptr) malloc (BLOC_PTR_SIZE);
  206.  
  207.   new_bloc->data = get_more_space (size);
  208.   new_bloc->size = size;
  209.   new_bloc->next = NIL_BLOC;
  210.   new_bloc->variable = NIL;
  211.  
  212.   if (first_bloc)
  213.     {
  214.       new_bloc->prev = last_bloc;
  215.       last_bloc->next = new_bloc;
  216.       last_bloc = new_bloc;
  217.     }
  218.   else
  219.     {
  220.       first_bloc = last_bloc = new_bloc;
  221.       new_bloc->prev = NIL_BLOC;
  222.     }
  223.  
  224.   return new_bloc;
  225. }
  226.  
  227. /* Relocate all blocs from BLOC on upward in the list to the zone
  228.    indicated by ADDRESS.  Direction of relocation is determined by
  229.    the position of ADDRESS relative to BLOC->data.
  230.  
  231.    Note that ordering of blocs is not affected by this function. */
  232.  
  233. static void
  234. relocate_some_blocs (bloc, address)
  235.      bloc_ptr bloc;
  236.      POINTER address;
  237. {
  238.   register bloc_ptr b;
  239.   POINTER data_zone = bloc->data;
  240.   register SIZE data_zone_size = 0;
  241.   register SIZE offset = bloc->data - address;
  242.   POINTER new_data_zone = data_zone - offset;
  243.  
  244.   for (b = bloc; b != NIL_BLOC; b = b->next)
  245.     {
  246.       data_zone_size += b->size;
  247.       b->data -= offset;
  248.       *b->variable = b->data;
  249.     }
  250.  
  251.   safe_bcopy (data_zone, new_data_zone, data_zone_size);
  252. }
  253.  
  254. /* Free BLOC from the chain of blocs, relocating any blocs above it
  255.    and returning BLOC->size bytes to the free area. */
  256.  
  257. static void
  258. free_bloc (bloc)
  259.      bloc_ptr bloc;
  260. {
  261.   if (bloc == first_bloc && bloc == last_bloc)
  262.     {
  263.       first_bloc = last_bloc = NIL_BLOC;
  264.     }
  265.   else if (bloc == last_bloc)
  266.     {
  267.       last_bloc = bloc->prev;
  268.       last_bloc->next = NIL_BLOC;
  269.     }
  270.   else if (bloc == first_bloc)
  271.     {
  272.       first_bloc = bloc->next;
  273.       first_bloc->prev = NIL_BLOC;
  274.       relocate_some_blocs (bloc->next, bloc->data);
  275.     }
  276.   else
  277.     {
  278.       bloc->next->prev = bloc->prev;
  279.       bloc->prev->next = bloc->next;
  280.       relocate_some_blocs (bloc->next, bloc->data);
  281.     }
  282.  
  283.   relinquish (bloc->size);
  284.   free (bloc);
  285. }
  286.  
  287. static int use_relocatable_buffers;
  288.  
  289. /* Obtain SIZE bytes of storage from the free pool, or the system,
  290.    as neccessary.  If relocatable blocs are in use, this means
  291.    relocating them. */
  292.  
  293. POINTER 
  294. r_alloc_sbrk (size)
  295.      long size;
  296. {
  297.   POINTER ptr;
  298.  
  299.   if (! use_relocatable_buffers)
  300.     return sbrk (size);
  301.  
  302.   if (size > 0)
  303.     {
  304.       obtain (size);
  305.       if (first_bloc)
  306.     {
  307.       relocate_some_blocs (first_bloc, first_bloc->data + size);
  308.       bzero (virtual_break_value, size);
  309.     }
  310.     }
  311.   else if (size < 0)
  312.     {
  313.       if (first_bloc)
  314.         relocate_some_blocs (first_bloc, first_bloc->data + size);
  315.       relinquish (- size);
  316.     }
  317.  
  318.   ptr = virtual_break_value;
  319.   virtual_break_value += size;
  320.   return ptr;
  321. }
  322.  
  323. /* Allocate a relocatable bloc of storage of size SIZE.  A pointer to
  324.    the data is returned in *PTR.  PTR is thus the address of some variable
  325.    which will use the data area. */
  326.  
  327. POINTER
  328. r_alloc (ptr, size)
  329.      POINTER *ptr;
  330.      SIZE size;
  331. {
  332.   register bloc_ptr new_bloc;
  333.  
  334.   new_bloc = get_bloc (size);
  335.   new_bloc->variable = ptr;
  336.   *ptr = new_bloc->data;
  337.  
  338.   return *ptr;
  339. }
  340.  
  341. /* Free a bloc of relocatable storage whose data is pointed to by PTR. */
  342.  
  343. void
  344. r_alloc_free (ptr)
  345.      register POINTER *ptr;
  346. {
  347.   register bloc_ptr dead_bloc;
  348.  
  349.   dead_bloc = find_bloc (ptr);
  350.   if (dead_bloc == NIL_BLOC)
  351.     abort ();
  352.  
  353.   free_bloc (dead_bloc);
  354. }
  355.  
  356. /* Given a pointer at address PTR to relocatable data, resize it
  357.    to SIZE.  This is done by obtaining a new block and freeing the
  358.    old, unless SIZE is less than or equal to the current bloc size,
  359.    in which case nothing happens and the current value is returned.
  360.  
  361.    The contents of PTR is changed to reflect the new bloc, and this
  362.    value is returned. */
  363.  
  364. POINTER
  365. r_re_alloc (ptr, size)
  366.      POINTER *ptr;
  367.      SIZE size;
  368. {
  369.   register bloc_ptr old_bloc, new_bloc;
  370.  
  371.   old_bloc = find_bloc (ptr);
  372.   if (old_bloc == NIL_BLOC)
  373.     abort ();
  374.  
  375.   if (size <= old_bloc->size)
  376.     return *ptr;
  377.  
  378.   new_bloc = get_bloc (size);
  379.   new_bloc->variable = ptr;
  380.   safe_bcopy (old_bloc->data, new_bloc->data, old_bloc->size);
  381.   *ptr = new_bloc->data;
  382.  
  383.   free_bloc (old_bloc);
  384.  
  385.   return *ptr;
  386. }
  387.  
  388. /* The hook `malloc' uses for the function which gets more space
  389.    from the system.  */
  390. extern POINTER (*__morecore) ();
  391.  
  392. /* Intialize various things for memory allocation. */
  393.  
  394. void
  395. malloc_init (start, warn_func)
  396.      POINTER start;
  397.      void (*warn_func) ();
  398. {
  399.   static int malloc_initialized = 0;
  400.  
  401.   if (start)
  402.     data_space_start = start;
  403.  
  404.   if (malloc_initialized)
  405.     return;
  406.  
  407.   malloc_initialized = 1;
  408.   __morecore = r_alloc_sbrk;
  409.   virtual_break_value = break_value = sbrk (0);
  410.   page_break_value = (POINTER) ROUNDUP (break_value);
  411.   bzero (break_value, (page_break_value - break_value));
  412.   use_relocatable_buffers = 1;
  413.  
  414.   lim_data = 0;
  415.   warnlevel = 0;
  416.   warnfunction = warn_func;
  417.  
  418.   get_lim_data ();
  419. }
  420.