📄 ralloc.c
字号:
/* Block-relocating memory allocator. Copyright (C) 1993, 1995 Free Software Foundation, Inc.This file is part of the GNU C Library. Its master source is NOT part ofthe C library, however. The master source lives in /gd/gnu/lib.The GNU C Library is free software; you can redistribute it and/ormodify it under the terms of the GNU Library General Public License aspublished by the Free Software Foundation; either version 2 of theLicense, or (at your option) any later version.The GNU C Library is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNULibrary General Public License for more details.You should have received a copy of the GNU Library General PublicLicense along with the GNU C Library; see the file COPYING.LIB. Ifnot, write to the Free Software Foundation, Inc., 675 Mass Ave,Cambridge, MA 02139, USA. *//* NOTES: Only relocate the blocs necessary for SIZE in r_alloc_sbrk, rather than all of them. This means allowing for a possible hole between the first bloc and the end of malloc storage. */#ifdef emacs#include <config.h>#include "lisp.h" /* Needed for VALBITS. */#undef NULL/* The important properties of this type are that 1) it's a pointer, and 2) arithmetic on it should work as if the size of the object pointed to has a size of 1. */#if 0 /* Arithmetic on void* is a GCC extension. */#ifdef __STDC__typedef void *POINTER;#else#ifdef HAVE_CONFIG_H#include "config.h"#endiftypedef char *POINTER;#endif#endif /* 0 *//* Unconditionally use char * for this. */typedef char *POINTER;typedef unsigned long SIZE;/* Declared in dispnew.c, this version doesn't screw up if regions overlap. */extern void safe_bcopy ();#include "getpagesize.h"#else /* Not emacs. */#include <stddef.h>typedef size_t SIZE;typedef void *POINTER;#include <unistd.h>#include <malloc.h>#include <string.h>#define safe_bcopy(x, y, z) memmove (y, x, z)#endif /* emacs. */#define NIL ((POINTER) 0)/* A flag to indicate whether we have initialized ralloc yet. For Emacs's sake, please do not make this local to malloc_init; on some machines, the dumping procedure makes all static variables read-only. On these machines, the word static is #defined to be the empty string, meaning that r_alloc_initialized becomes an automatic variable, and loses its value each time Emacs is started up. */static int r_alloc_initialized = 0;static void r_alloc_init ();/* Declarations for working with the malloc, ralloc, and system breaks. *//* Function to set the real break value. */static POINTER (*real_morecore) ();/* The break value, as seen by malloc. */static POINTER virtual_break_value;/* The address of the end of the last data in use by ralloc, including relocatable blocs as well as malloc data. */static POINTER break_value;/* This is the size of a page. We round memory requests to this boundary. */static int page_size;/* Whenever we get memory from the system, get this many extra bytes. This must be a multiple of page_size. */static int extra_bytes;/* Macros for rounding. Note that rounding to any value is possible by changing the definition of PAGE. */#define PAGE (getpagesize ())#define ALIGNED(addr) (((unsigned long int) (addr) & (page_size - 1)) == 0)#define ROUNDUP(size) (((unsigned long int) (size) + page_size - 1) \ & ~(page_size - 1))#define ROUND_TO_PAGE(addr) (addr & (~(page_size - 1)))#define MEM_ALIGN sizeof(double)#define MEM_ROUNDUP(addr) (((unsigned long int)(addr) + MEM_ALIGN - 1) \ & ~(MEM_ALIGN - 1))/* Data structures of heaps and blocs. *//* The relocatable objects, or blocs, and the malloc data both reside within one or more heaps. Each heap contains malloc data, running from `start' to `bloc_start', and relocatable objects, running from `bloc_start' to `free'. Relocatable objects may relocate within the same heap or may move into another heap; the heaps themselves may grow but they never move. We try to make just one heap and make it larger as necessary. But sometimes we can't do that, because we can't get continguous space to add onto the heap. When that happens, we start a new heap. */ typedef struct heap{ struct heap *next; struct heap *prev; /* Start of memory range of this heap. */ POINTER start; /* End of memory range of this heap. */ POINTER end; /* Start of relocatable data in this heap. */ POINTER bloc_start; /* Start of unused space in this heap. */ POINTER free; /* First bloc in this heap. */ struct bp *first_bloc; /* Last bloc in this heap. */ struct bp *last_bloc;} *heap_ptr;#define NIL_HEAP ((heap_ptr) 0)#define HEAP_PTR_SIZE (sizeof (struct heap))/* This is the first heap object. If we need additional heap objects, each one resides at the beginning of the space it covers. */static struct heap heap_base;/* Head and tail of the list of heaps. */static heap_ptr first_heap, last_heap;/* These structures are allocated in the malloc arena. The linked list is kept in order of increasing '.data' members. The data blocks abut each other; if b->next is non-nil, then b->data + b->size == b->next->data. */typedef struct bp{ struct bp *next; struct bp *prev; POINTER *variable; POINTER data; SIZE size; POINTER new_data; /* tmporarily used for relocation */ /* Heap this bloc is in. */ struct heap *heap;} *bloc_ptr;#define NIL_BLOC ((bloc_ptr) 0)#define BLOC_PTR_SIZE (sizeof (struct bp))/* Head and tail of the list of relocatable blocs. */static bloc_ptr first_bloc, last_bloc;/* Functions to get and return memory from the system. *//* Find the heap that ADDRESS falls within. */static heap_ptrfind_heap (address) POINTER address;{ heap_ptr heap; for (heap = last_heap; heap; heap = heap->prev) { if (heap->start <= address && address <= heap->end) return heap; } return NIL_HEAP;}/* Find SIZE bytes of space in a heap. Try to get them at ADDRESS (which must fall within some heap's range) if we can get that many within one heap. If enough space is not presently available in our reserve, this means getting more page-aligned space from the system. If the retuned space is not contiguos to the last heap, allocate a new heap, and append it obtain does not try to keep track of whether space is in use or not in use. It just returns the address of SIZE bytes that fall within a single heap. If you call obtain twice in a row with the same arguments, you typically get the same value. to the heap list. It's the caller's responsibility to keep track of what space is in use. Return the address of the space if all went well, or zero if we couldn't allocate the memory. */static POINTERobtain (address, size) POINTER address; SIZE size;{ heap_ptr heap; SIZE already_available; /* Find the heap that ADDRESS falls within. */ for (heap = last_heap; heap; heap = heap->prev) { if (heap->start <= address && address <= heap->end) break; } if (! heap) abort (); /* If we can't fit SIZE bytes in that heap, try successive later heaps. */ while (heap && address + size > heap->end) { heap = heap->next; if (heap == NIL_HEAP) break; address = heap->bloc_start; } /* If we can't fit them within any existing heap, get more space. */ if (heap == NIL_HEAP) { POINTER new = (*real_morecore)(0); SIZE get; already_available = (char *)last_heap->end - (char *)address; if (new != last_heap->end) { /* Someone else called sbrk. Make a new heap. */ heap_ptr new_heap = (heap_ptr) MEM_ROUNDUP (new); POINTER bloc_start = (POINTER) MEM_ROUNDUP ((POINTER)(new_heap + 1)); if ((*real_morecore) (bloc_start - new) != new) return 0; new_heap->start = new; new_heap->end = bloc_start; new_heap->bloc_start = bloc_start; new_heap->free = bloc_start; new_heap->next = NIL_HEAP; new_heap->prev = last_heap; new_heap->first_bloc = NIL_BLOC; new_heap->last_bloc = NIL_BLOC; last_heap->next = new_heap; last_heap = new_heap; address = bloc_start; already_available = 0; } /* Add space to the last heap (which we may have just created). Get some extra, so we can come here less often. */ get = size + extra_bytes - already_available; get = (char *) ROUNDUP ((char *)last_heap->end + get) - (char *) last_heap->end; if ((*real_morecore) (get) != last_heap->end) return 0; last_heap->end += get; } return address;}/* Return unused heap space to the system if there is a lot of unused space now. This can make the last heap smaller; it can also eliminate the last heap entirely. */static voidrelinquish (){ register heap_ptr h; int excess = 0; /* Add the amount of space beyond break_value in all heaps which have extend beyond break_value at all. */ for (h = last_heap; h && break_value < h->end; h = h->prev) { excess += (char *) h->end - (char *) ((break_value < h->bloc_start) ? h->bloc_start : break_value); } if (excess > extra_bytes * 2 && (*real_morecore) (0) == last_heap->end) { /* Keep extra_bytes worth of empty space. And don't free anything unless we can free at least extra_bytes. */ excess -= extra_bytes; if ((char *)last_heap->end - (char *)last_heap->bloc_start <= excess) { /* This heap should have no blocs in it. */ if (last_heap->first_bloc != NIL_BLOC || last_heap->last_bloc != NIL_BLOC) abort (); /* Return the last heap, with its header, to the system. */ excess = (char *)last_heap->end - (char *)last_heap->start; last_heap = last_heap->prev; last_heap->next = NIL_HEAP; } else { excess = (char *) last_heap->end - (char *) ROUNDUP ((char *)last_heap->end - excess); last_heap->end -= excess; } if ((*real_morecore) (- excess) == 0) abort (); }}/* The meat - allocating, freeing, and relocating blocs. *//* Find the bloc referenced by the address in PTR. Returns a pointer to that block. */static bloc_ptrfind_bloc (ptr) POINTER *ptr;{ register bloc_ptr p = first_bloc; while (p != NIL_BLOC) { if (p->variable == ptr && p->data == *ptr) return p; p = p->next; } return p;}/* Allocate a bloc of SIZE bytes and append it to the chain of blocs. Returns a pointer to the new bloc, or zero if we couldn't allocate memory for the new block. */static bloc_ptrget_bloc (size) SIZE size;{ register bloc_ptr new_bloc; register heap_ptr heap; if (! (new_bloc = (bloc_ptr) malloc (BLOC_PTR_SIZE)) || ! (new_bloc->data = obtain (break_value, size))) { if (new_bloc) free (new_bloc); return 0; } break_value = new_bloc->data + size; new_bloc->size = size; new_bloc->next = NIL_BLOC; new_bloc->variable = (POINTER *) NIL; new_bloc->new_data = 0; /* Record in the heap that this space is in use. */ heap = find_heap (new_bloc->data); heap->free = break_value; /* Maintain the correspondence between heaps and blocs. */ new_bloc->heap = heap; heap->last_bloc = new_bloc; if (heap->first_bloc == NIL_BLOC) heap->first_bloc = new_bloc; /* Put this bloc on the doubly-linked list of blocs. */ if (first_bloc) { new_bloc->prev = last_bloc; last_bloc->next = new_bloc; last_bloc = new_bloc; } else { first_bloc = last_bloc = new_bloc; new_bloc->prev = NIL_BLOC; } return new_bloc;}/* Calculate new locations of blocs in the list beginning with BLOC, relocating it to start at ADDRESS, in heap HEAP. If enough space is not presently available in our reserve, call obtain for more space. Store the new location of each bloc in its new_data field. Do not touch the contents of blocs or break_value. */static intrelocate_blocs (bloc, heap, address) bloc_ptr bloc; heap_ptr heap; POINTER address;{ register bloc_ptr b = bloc; while (b) { /* If bloc B won't fit within HEAP, move to the next heap and try again. */ while (heap && address + b->size > heap->end) { heap = heap->next; if (heap == NIL_HEAP) break; address = heap->bloc_start; } /* If BLOC won't fit in any heap, get enough new space to hold BLOC and all following blocs. */ if (heap == NIL_HEAP) { register bloc_ptr tb = b; register SIZE s = 0; /* Add up the size of all the following blocs. */ while (tb != NIL_BLOC) { s += tb->size; tb = tb->next; } /* Get that space. */ address = obtain (address, s); if (address == 0) return 0; heap = last_heap; } /* Record the new address of this bloc and update where the next bloc can start. */ b->new_data = address; address += b->size; b = b->next; } return 1;}/* Reorder the bloc BLOC to go before bloc BEFORE in the doubly linked list. This is necessary if we put the memory of space of BLOC before that of BEFORE. */static voidreorder_bloc (bloc, before) bloc_ptr bloc, before;{ bloc_ptr prev, next; /* Splice BLOC out from where it is. */ prev = bloc->prev; next = bloc->next; if (prev) prev->next = next; if (next) next->prev = prev; /* Splice it in before BEFORE. */ prev = before->prev; if (prev) prev->next = bloc; bloc->prev = prev; before->prev = bloc; bloc->next = before;}/* Update the records of which heaps contain which blocs, starting with heap HEAP and bloc BLOC. */static voidupdate_heap_bloc_correspondence (bloc, heap) bloc_ptr bloc; heap_ptr heap;{ register bloc_ptr b; /* Initialize HEAP's status to reflect blocs before BLOC. */ if (bloc != NIL_BLOC && bloc->prev != NIL_BLOC && bloc->prev->heap == heap) { /* The previous bloc is in HEAP. */ heap->last_bloc = bloc->prev; heap->free = bloc->prev->data + bloc->prev->size; } else { /* HEAP contains no blocs before BLOC. */ heap->first_bloc = NIL_BLOC; heap->last_bloc = NIL_BLOC; heap->free = heap->bloc_start; } /* Advance through blocs one by one. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -