home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / glibc-1.06 / malloc / ralloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-22  |  12.6 KB  |  507 lines

  1. /* Block-relocating memory allocator. 
  2.    Copyright (C) 1993 Free Software Foundation, Inc.
  3.  
  4.  
  5. This file is part of the GNU C Library.
  6.  
  7. The GNU C Library is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU Library General Public License as
  9. published by the Free Software Foundation; either version 2 of the
  10. License, or (at your option) any later version.
  11.  
  12. The GNU C Library is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15. Library General Public License for more details.
  16.  
  17. You should have received a copy of the GNU Library General Public
  18. License along with the GNU C Library; see the file COPYING.LIB.  If
  19. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  20. Cambridge, MA 02139, USA.  */
  21.  
  22. /* NOTES:
  23.  
  24.    Only relocate the blocs neccessary for SIZE in r_alloc_sbrk,
  25.    rather than all of them.  This means allowing for a possible
  26.    hole between the first bloc and the end of malloc storage. */
  27.  
  28. #ifdef emacs
  29.  
  30. #include "config.h"
  31. #include "lisp.h"        /* Needed for VALBITS.  */
  32.  
  33. #undef NULL
  34.  
  35. /* The important properties of this type are that 1) it's a pointer, and
  36.    2) arithmetic on it should work as if the size of the object pointed
  37.    to has a size of 1.  */
  38. #ifdef __STDC__
  39. typedef void *POINTER;
  40. #else
  41.  
  42. #ifdef    HAVE_CONFIG_H
  43. #include "config.h"
  44. #endif
  45.  
  46. typedef char *POINTER;
  47.  
  48. #endif
  49.  
  50. typedef unsigned long SIZE;
  51.  
  52. /* Declared in dispnew.c, this version doesn't screw up if regions
  53.    overlap.  */
  54. extern void safe_bcopy ();
  55.  
  56. #include "getpagesize.h"
  57.  
  58. #else    /* Not emacs.  */
  59.  
  60. #include <stddef.h>
  61.  
  62. typedef size_t SIZE;
  63. typedef void *POINTER;
  64.  
  65. #include <unistd.h>
  66. #include <malloc.h>
  67. #include <string.h>
  68.  
  69. #define safe_bcopy(x, y, z) memmove (y, x, z)
  70.  
  71. #endif    /* emacs.  */
  72.  
  73. #define NIL ((POINTER) 0)
  74.  
  75. /* A flag to indicate whether we have initialized ralloc yet.  For
  76.    Emacs's sake, please do not make this local to malloc_init; on some
  77.    machines, the dumping procedure makes all static variables
  78.    read-only.  On these machines, the word static is #defined to be
  79.    the empty string, meaning that r_alloc_initialized becomes an
  80.    automatic variable, and loses its value each time Emacs is started up.  */
  81. static int r_alloc_initialized = 0;
  82.  
  83. static void r_alloc_init ();
  84.  
  85. /* Declarations for working with the malloc, ralloc, and system breaks.  */
  86.  
  87. /* Function to set the real break value. */
  88. static POINTER (*real_morecore) ();
  89.  
  90. /* The break value, as seen by malloc (). */
  91. static POINTER virtual_break_value;
  92.  
  93. /* The break value, viewed by the relocatable blocs. */
  94. static POINTER break_value;
  95.  
  96. /* The REAL (i.e., page aligned) break value of the process. */
  97. static POINTER page_break_value;
  98.  
  99. /* This is the size of a page.  We round memory requests to this boundary.  */
  100. static int page_size;
  101.  
  102. /* Whenever we get memory from the system, get this many extra bytes.  This 
  103.    must be a multiple of page_size.  */
  104. static int extra_bytes;
  105.  
  106. /* Macros for rounding.  Note that rounding to any value is possible
  107.    by changing the definition of PAGE. */
  108. #define PAGE (getpagesize ())
  109. #define ALIGNED(addr) (((unsigned int) (addr) & (page_size - 1)) == 0)
  110. #define ROUNDUP(size) (((unsigned int) (size) + page_size - 1) & ~(page_size - 1))
  111. #define ROUND_TO_PAGE(addr) (addr & (~(page_size - 1)))
  112.  
  113. /* Functions to get and return memory from the system.  */
  114.  
  115. /* Obtain SIZE bytes of space.  If enough space is not presently available
  116.    in our process reserve, (i.e., (page_break_value - break_value)),
  117.    this means getting more page-aligned space from the system.
  118.  
  119.    Return non-zero if all went well, or zero if we couldn't allocate
  120.    the memory.  */
  121. static int
  122. obtain (size)
  123.      SIZE size;
  124. {
  125.   SIZE already_available = page_break_value - break_value;
  126.  
  127.   if (already_available < size)
  128.     {
  129.       SIZE get = ROUNDUP (size - already_available);
  130.       /* Get some extra, so we can come here less often.  */
  131.       get += extra_bytes;
  132.  
  133.       if ((*real_morecore) (get) == 0)
  134.     return 0;
  135.  
  136.       page_break_value += get;
  137.     }
  138.  
  139.   break_value += size;
  140.  
  141.   return 1;
  142. }
  143.  
  144. /* Obtain SIZE bytes of space and return a pointer to the new area.
  145.    If we could not allocate the space, return zero.  */
  146.  
  147. static POINTER
  148. get_more_space (size)
  149.      SIZE size;
  150. {
  151.   POINTER ptr = break_value;
  152.   if (obtain (size))
  153.     return ptr;
  154.   else
  155.     return 0;
  156. }
  157.  
  158. /* Note that SIZE bytes of space have been relinquished by the process.
  159.    If SIZE is more than a page, return the space to the system. */
  160.  
  161. static void
  162. relinquish (size)
  163.      SIZE size;
  164. {
  165.   POINTER new_page_break;
  166.   int excess;
  167.  
  168.   break_value -= size;
  169.   new_page_break = (POINTER) ROUNDUP (break_value);
  170.   excess = (char *) page_break_value - (char *) new_page_break;
  171.   
  172.   if (excess > extra_bytes * 2)
  173.     {
  174.       /* Keep extra_bytes worth of empty space.
  175.      And don't free anything unless we can free at least extra_bytes.  */
  176.       if ((*real_morecore) (extra_bytes - excess) == 0)
  177.     abort ();
  178.  
  179.       page_break_value += extra_bytes - excess;
  180.     }
  181.  
  182.   /* Zero the space from the end of the "official" break to the actual
  183.      break, so that bugs show up faster.  */
  184.   bzero (break_value, ((char *) page_break_value - (char *) break_value));
  185. }
  186.  
  187. /* The meat - allocating, freeing, and relocating blocs.  */
  188.  
  189. /* These structures are allocated in the malloc arena.
  190.    The linked list is kept in order of increasing '.data' members.
  191.    The data blocks abut each other; if b->next is non-nil, then
  192.    b->data + b->size == b->next->data.  */
  193. typedef struct bp
  194. {
  195.   struct bp *next;
  196.   struct bp *prev;
  197.   POINTER *variable;
  198.   POINTER data;
  199.   SIZE size;
  200. } *bloc_ptr;
  201.  
  202. #define NIL_BLOC ((bloc_ptr) 0)
  203. #define BLOC_PTR_SIZE (sizeof (struct bp))
  204.  
  205. /* Head and tail of the list of relocatable blocs. */
  206. static bloc_ptr first_bloc, last_bloc;
  207.  
  208. /* Find the bloc referenced by the address in PTR.  Returns a pointer
  209.    to that block. */
  210.  
  211. static bloc_ptr
  212. find_bloc (ptr)
  213.      POINTER *ptr;
  214. {
  215.   register bloc_ptr p = first_bloc;
  216.  
  217.   while (p != NIL_BLOC)
  218.     {
  219.       if (p->variable == ptr && p->data == *ptr)
  220.     return p;
  221.  
  222.       p = p->next;
  223.     }
  224.  
  225.   return p;
  226. }
  227.  
  228. /* Allocate a bloc of SIZE bytes and append it to the chain of blocs.
  229.    Returns a pointer to the new bloc, or zero if we couldn't allocate
  230.    memory for the new block.  */
  231.  
  232. static bloc_ptr
  233. get_bloc (size)
  234.      SIZE size;
  235. {
  236.   register bloc_ptr new_bloc;
  237.  
  238.   if (! (new_bloc = (bloc_ptr) malloc (BLOC_PTR_SIZE))
  239.       || ! (new_bloc->data = get_more_space (size)))
  240.     {
  241.       if (new_bloc)
  242.     free (new_bloc);
  243.  
  244.       return 0;
  245.     }
  246.  
  247.   new_bloc->size = size;
  248.   new_bloc->next = NIL_BLOC;
  249.   new_bloc->variable = (POINTER *) NIL;
  250.  
  251.   if (first_bloc)
  252.     {
  253.       new_bloc->prev = last_bloc;
  254.       last_bloc->next = new_bloc;
  255.       last_bloc = new_bloc;
  256.     }
  257.   else
  258.     {
  259.       first_bloc = last_bloc = new_bloc;
  260.       new_bloc->prev = NIL_BLOC;
  261.     }
  262.  
  263.   return new_bloc;
  264. }
  265.  
  266. /* Relocate all blocs from BLOC on upward in the list to the zone
  267.    indicated by ADDRESS.  Direction of relocation is determined by
  268.    the position of ADDRESS relative to BLOC->data.
  269.  
  270.    If BLOC is NIL_BLOC, nothing is done.
  271.  
  272.    Note that ordering of blocs is not affected by this function. */
  273.  
  274. static void
  275. relocate_some_blocs (bloc, address)
  276.      bloc_ptr bloc;
  277.      POINTER address;
  278. {
  279.   if (bloc != NIL_BLOC)
  280.     {
  281.       register SIZE offset = address - bloc->data;
  282.       register SIZE data_size = 0;
  283.       register bloc_ptr b;
  284.       
  285.       for (b = bloc; b != NIL_BLOC; b = b->next)
  286.     {
  287.       data_size += b->size;
  288.       b->data += offset;
  289.       *b->variable = b->data;
  290.     }
  291.  
  292.       safe_bcopy (address - offset, address, data_size);
  293.     }
  294. }
  295.  
  296.  
  297. /* Free BLOC from the chain of blocs, relocating any blocs above it
  298.    and returning BLOC->size bytes to the free area. */
  299.  
  300. static void
  301. free_bloc (bloc)
  302.      bloc_ptr bloc;
  303. {
  304.   if (bloc == first_bloc && bloc == last_bloc)
  305.     {
  306.       first_bloc = last_bloc = NIL_BLOC;
  307.     }
  308.   else if (bloc == last_bloc)
  309.     {
  310.       last_bloc = bloc->prev;
  311.       last_bloc->next = NIL_BLOC;
  312.     }
  313.   else if (bloc == first_bloc)
  314.     {
  315.       first_bloc = bloc->next;
  316.       first_bloc->prev = NIL_BLOC;
  317.     }
  318.   else
  319.     {
  320.       bloc->next->prev = bloc->prev;
  321.       bloc->prev->next = bloc->next;
  322.     }
  323.  
  324.   relocate_some_blocs (bloc->next, bloc->data);
  325.   relinquish (bloc->size);
  326.   free (bloc);
  327. }
  328.  
  329. /* Interface routines.  */
  330.  
  331. static int use_relocatable_buffers;
  332.  
  333. /* Obtain SIZE bytes of storage from the free pool, or the system, as
  334.    necessary.  If relocatable blocs are in use, this means relocating
  335.    them.  This function gets plugged into the GNU malloc's __morecore
  336.    hook.
  337.  
  338.    We provide hysteresis, never relocating by less than extra_bytes.
  339.  
  340.    If we're out of memory, we should return zero, to imitate the other
  341.    __morecore hook values - in particular, __default_morecore in the
  342.    GNU malloc package.  */
  343.  
  344. POINTER 
  345. r_alloc_sbrk (size)
  346.      long size;
  347. {
  348.   /* This is the first address not currently available for the heap.  */
  349.   POINTER top;
  350.   /* Amount of empty space below that.  */
  351.   SIZE already_available;
  352.   POINTER ptr;
  353.  
  354.   if (! use_relocatable_buffers)
  355.     return (*real_morecore) (size);
  356.  
  357.   top = first_bloc ? first_bloc->data : page_break_value;
  358.   already_available = (char *) top - (char *) virtual_break_value;
  359.  
  360.   /* Do we not have enough gap already?  */
  361.   if (size > 0 && already_available < size)
  362.     {
  363.       /* Get what we need, plus some extra so we can come here less often.  */
  364.       SIZE get = size - already_available + extra_bytes;
  365.  
  366.       if (! obtain (get))
  367.     return 0;
  368.  
  369.       if (first_bloc)
  370.     relocate_some_blocs (first_bloc, first_bloc->data + get);
  371.  
  372.       /* Zero out the space we just allocated, to help catch bugs
  373.      quickly.  */
  374.       bzero (virtual_break_value, get);
  375.     }
  376.   /* Can we keep extra_bytes of gap while freeing at least extra_bytes?  */
  377.   else if (size < 0 && already_available - size > 2 * extra_bytes)
  378.     {
  379.       /* Ok, do so.  This is how many to free.  */
  380.       SIZE give_back = already_available - size - extra_bytes;
  381.  
  382.       if (first_bloc)
  383.     relocate_some_blocs (first_bloc, first_bloc->data - give_back);
  384.       relinquish (give_back);
  385.     }
  386.  
  387.   ptr = virtual_break_value;
  388.   virtual_break_value += size;
  389.  
  390.   return ptr;
  391. }
  392.  
  393. /* Allocate a relocatable bloc of storage of size SIZE.  A pointer to
  394.    the data is returned in *PTR.  PTR is thus the address of some variable
  395.    which will use the data area.
  396.  
  397.    If we can't allocate the necessary memory, set *PTR to zero, and
  398.    return zero.  */
  399.  
  400. POINTER
  401. r_alloc (ptr, size)
  402.      POINTER *ptr;
  403.      SIZE size;
  404. {
  405.   register bloc_ptr new_bloc;
  406.  
  407.   if (! r_alloc_initialized)
  408.     r_alloc_init ();
  409.  
  410.   new_bloc = get_bloc (size);
  411.   if (new_bloc)
  412.     {
  413.       new_bloc->variable = ptr;
  414.       *ptr = new_bloc->data;
  415.     }
  416.   else
  417.     *ptr = 0;
  418.  
  419.   return *ptr;
  420. }
  421.  
  422. /* Free a bloc of relocatable storage whose data is pointed to by PTR.
  423.    Store 0 in *PTR to show there's no block allocated.  */
  424.  
  425. void
  426. r_alloc_free (ptr)
  427.      register POINTER *ptr;
  428. {
  429.   register bloc_ptr dead_bloc;
  430.  
  431.   dead_bloc = find_bloc (ptr);
  432.   if (dead_bloc == NIL_BLOC)
  433.     abort ();
  434.  
  435.   free_bloc (dead_bloc);
  436.   *ptr = 0;
  437. }
  438.  
  439. /* Given a pointer at address PTR to relocatable data, resize it to SIZE.
  440.    Do this by shifting all blocks above this one up in memory, unless
  441.    SIZE is less than or equal to the current bloc size, in which case
  442.    do nothing.
  443.  
  444.    Change *PTR to reflect the new bloc, and return this value.
  445.  
  446.    If more memory cannot be allocated, then leave *PTR unchanged, and
  447.    return zero.  */
  448.  
  449. POINTER
  450. r_re_alloc (ptr, size)
  451.      POINTER *ptr;
  452.      SIZE size;
  453. {
  454.   register bloc_ptr bloc;
  455.  
  456.   bloc = find_bloc (ptr);
  457.   if (bloc == NIL_BLOC)
  458.     abort ();
  459.  
  460.   if (size <= bloc->size)
  461.     /* Wouldn't it be useful to actually resize the bloc here?  */
  462.     return *ptr;
  463.  
  464.   if (! obtain (size - bloc->size))
  465.     return 0;
  466.  
  467.   relocate_some_blocs (bloc->next, bloc->data + size);
  468.  
  469.   /* Zero out the new space in the bloc, to help catch bugs faster.  */
  470.   bzero (bloc->data + bloc->size, size - bloc->size);
  471.  
  472.   /* Indicate that this block has a new size.  */
  473.   bloc->size = size;
  474.  
  475.   return *ptr;
  476. }
  477.  
  478. /* The hook `malloc' uses for the function which gets more space
  479.    from the system.  */
  480. extern POINTER (*__morecore) ();
  481.  
  482. /* Intialize various things for memory allocation. */
  483.  
  484. static void
  485. r_alloc_init ()
  486. {
  487.   if (r_alloc_initialized)
  488.     return;
  489.  
  490.   r_alloc_initialized = 1;
  491.   real_morecore = __morecore;
  492.   __morecore = r_alloc_sbrk;
  493.  
  494.   virtual_break_value = break_value = (*real_morecore) (0);
  495.   if (break_value == NIL)
  496.     abort ();
  497.  
  498.   page_size = PAGE;
  499.   extra_bytes = ROUNDUP (50000);
  500.  
  501.   page_break_value = (POINTER) ROUNDUP (break_value);
  502.   /* Clear the rest of the last page; this memory is in our address space
  503.      even though it is after the sbrk value.  */
  504.   bzero (break_value, (page_break_value - break_value));
  505.   use_relocatable_buffers = 1;
  506. }
  507.