1987-03-09 19:15:41 +00:00
|
|
|
/*
|
|
|
|
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
|
|
|
|
* See the copyright notice in the ACK home directory, in the file "Copyright".
|
|
|
|
*/
|
1985-01-08 11:54:57 +00:00
|
|
|
#ifndef lint
|
1994-06-24 11:31:16 +00:00
|
|
|
static char rcsid[] = "$Id$";
|
1985-01-08 11:54:57 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Memory manager. Memory is divided into NMEMS pieces. There is a struct
|
|
|
|
* for each piece telling where it is, how many bytes are used, and how may
|
|
|
|
* are left. If a request for core doesn't fit in the left bytes, an sbrk()
|
|
|
|
* is done and pieces after the one that requested the growth are moved up.
|
1986-12-02 16:16:08 +00:00
|
|
|
*
|
|
|
|
* Unfortunately, we cannot use sbrk to request more memory, because its
|
|
|
|
* result cannot be trusted. More specifically, it does not work properly
|
|
|
|
* on 2.9 BSD, and probably does not work properly on 2.8 BSD and V7 either.
|
|
|
|
* The problem is that "sbrk" adds the increment to the current "break"
|
|
|
|
* WITHOUT testing the carry bit. So, if your break is at 40000, and
|
|
|
|
* you "sbrk(30000)", it will succeed, but your break will be at 4464
|
|
|
|
* (70000 - 65536).
|
1985-01-08 11:54:57 +00:00
|
|
|
*/
|
|
|
|
|
2018-11-13 23:05:16 +00:00
|
|
|
/*
|
|
|
|
* USEMALLOC tells the allocator to use malloc() and realloc(), not brk().
|
|
|
|
* This might help systems where brk() doesn't work, or where malloc() can
|
|
|
|
* allocate outside the brk area.
|
|
|
|
*/
|
|
|
|
#define USEMALLOC
|
|
|
|
|
2017-11-10 03:22:13 +00:00
|
|
|
#include <assert.h>
|
1987-08-11 15:31:09 +00:00
|
|
|
#include <stdio.h>
|
2017-01-18 18:55:56 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
2017-08-06 13:57:49 +00:00
|
|
|
#include <unistd.h>
|
1986-10-20 10:17:57 +00:00
|
|
|
#include <out.h>
|
1985-01-08 11:54:57 +00:00
|
|
|
#include "const.h"
|
|
|
|
#include "debug.h"
|
|
|
|
#include "memory.h"
|
2017-08-06 13:57:49 +00:00
|
|
|
#include "object.h"
|
2018-03-11 11:37:23 +00:00
|
|
|
#include "sym.h"
|
2019-03-24 09:08:45 +00:00
|
|
|
#include "finish.h"
|
|
|
|
#include "write.h"
|
1985-01-08 11:54:57 +00:00
|
|
|
|
2018-11-13 23:05:16 +00:00
|
|
|
#ifndef USEMALLOC
|
2017-08-06 13:57:49 +00:00
|
|
|
static void copy_down(struct memory* mem, ind_t dist);
|
|
|
|
static void copy_up(struct memory* mem, ind_t dist);
|
2018-11-13 23:05:16 +00:00
|
|
|
#endif
|
2017-08-06 13:57:49 +00:00
|
|
|
static void free_saved_moduls(void);
|
1985-06-10 10:48:49 +00:00
|
|
|
|
1985-01-08 11:54:57 +00:00
|
|
|
struct memory mems[NMEMS];
|
|
|
|
|
|
|
|
bool incore = TRUE; /* TRUE while everything can be kept in core. */
|
1985-01-10 13:35:39 +00:00
|
|
|
ind_t core_position = (ind_t)0; /* Index of current module. */
|
1985-01-08 11:54:57 +00:00
|
|
|
|
2018-11-13 23:05:16 +00:00
|
|
|
#ifdef USEMALLOC
|
|
|
|
static size_t modl_initial_size;
|
|
|
|
static bool frozen = FALSE; /* TRUE after freeze_core(). */
|
|
|
|
|
|
|
|
#else /* ifndef USEMALLOC */
|
1986-12-02 16:16:08 +00:00
|
|
|
#define GRANULE 64 /* power of 2 */
|
|
|
|
|
|
|
|
static char *BASE;
|
|
|
|
static ind_t refused;
|
|
|
|
|
2017-08-06 13:57:49 +00:00
|
|
|
static int sbreak(ind_t incr)
|
1986-12-02 16:16:08 +00:00
|
|
|
{
|
|
|
|
incr = (incr + (GRANULE - 1)) & ~(GRANULE - 1);
|
|
|
|
|
|
|
|
if ((refused && refused < incr) ||
|
2018-11-12 03:51:17 +00:00
|
|
|
BASE + incr < BASE ||
|
1990-12-11 11:10:46 +00:00
|
|
|
brk(BASE + incr) == -1) {
|
1987-05-21 10:06:14 +00:00
|
|
|
if (!refused || refused > incr)
|
|
|
|
refused = incr;
|
1986-12-02 16:16:08 +00:00
|
|
|
return -1;
|
|
|
|
}
|
1987-09-02 18:42:21 +00:00
|
|
|
BASE += incr;
|
1986-12-02 16:16:08 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2018-11-13 23:05:16 +00:00
|
|
|
#endif /* ndef USEMALLOC */
|
1985-01-08 11:54:57 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize some pieces of core. We hope that this will be our last
|
|
|
|
* real allocation, meaning we've made the right choices.
|
|
|
|
*/
|
2017-08-06 13:57:49 +00:00
|
|
|
void init_core(void)
|
1985-01-08 11:54:57 +00:00
|
|
|
{
|
2018-11-13 23:05:16 +00:00
|
|
|
#ifdef USEMALLOC
|
|
|
|
struct memory *failed_mem = NULL;
|
|
|
|
struct memory *mem;
|
|
|
|
bool string_area;
|
|
|
|
|
|
|
|
#include "mach.h"
|
|
|
|
modl_initial_size = mems[ALLOMODL].mem_left;
|
|
|
|
|
|
|
|
for (mem = mems; mem < &mems[NMEMS]; mem++) {
|
|
|
|
string_area = mem == &mems[ALLOLCHR] || mem == &mems[ALLOGCHR];
|
|
|
|
/* String areas need at least one byte. */
|
|
|
|
if (string_area && mem->mem_left == 0)
|
|
|
|
mem->mem_left++;
|
|
|
|
/* Don't malloc() size zero. */
|
|
|
|
if (mem->mem_left > 0) {
|
|
|
|
mem->mem_base = malloc(mem->mem_left);
|
|
|
|
if (mem->mem_base == NULL) {
|
|
|
|
failed_mem = mem;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mem->mem_full = 0;
|
|
|
|
if (string_area) {
|
|
|
|
mem->mem_left--;
|
|
|
|
mem->mem_full++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (failed_mem != NULL) {
|
|
|
|
incore = FALSE; /* In core strategy failed. */
|
|
|
|
/* Undo allocations. */
|
|
|
|
for (mem = mems; mem != failed_mem; mem++)
|
|
|
|
free(mem->mem_base);
|
|
|
|
/* Allocate only the string areas. */
|
|
|
|
for (mem = mems; mem < &mems[NMEMS]; mem++) {
|
|
|
|
if (mem == &mems[ALLOLCHR] || mem == &mems[ALLOGCHR]) {
|
|
|
|
mem->mem_base = malloc(1);
|
|
|
|
if (mem->mem_base == NULL)
|
|
|
|
fatal("no core at all");
|
|
|
|
mem->mem_left = 0;
|
|
|
|
mem->mem_full = 1;
|
|
|
|
} else {
|
|
|
|
mem->mem_base = NULL;
|
|
|
|
mem->mem_left = mem->mem_full = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else /* ifndef USEMALLOC */
|
1985-01-08 11:54:57 +00:00
|
|
|
register char *base;
|
1985-01-10 13:35:39 +00:00
|
|
|
register ind_t total_size;
|
1985-01-08 11:54:57 +00:00
|
|
|
register struct memory *mem;
|
|
|
|
|
2016-08-14 12:40:01 +00:00
|
|
|
#include "mach.h"
|
1987-04-08 17:15:30 +00:00
|
|
|
#define ALIGN 8 /* minimum alignment for pieces */
|
|
|
|
#define AT_LEAST (ind_t)2*ALIGN /* See comment about string areas. */
|
1985-01-08 11:54:57 +00:00
|
|
|
|
1985-01-10 13:35:39 +00:00
|
|
|
total_size = (ind_t)0; /* Will accumulate the sizes. */
|
1985-06-10 10:48:49 +00:00
|
|
|
BASE = base = sbrk(0); /* First free. */
|
1987-04-08 17:15:30 +00:00
|
|
|
if ((int)base % ALIGN) {
|
|
|
|
base = sbrk(ALIGN - (int)base % ALIGN);
|
|
|
|
BASE = base = sbrk(0);
|
1985-01-08 11:54:57 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* String areas are special-cased. The first byte is unused as a way to
|
|
|
|
* distinguish a name without string from a name which has the first
|
|
|
|
* string in the string area.
|
|
|
|
*/
|
1987-04-08 17:15:30 +00:00
|
|
|
for (mem = mems; mem < &mems[NMEMS]; mem++) {
|
|
|
|
mem->mem_base = base;
|
|
|
|
mem->mem_full = (ind_t)0;
|
|
|
|
if (mem == &mems[ALLOLCHR] || mem == &mems[ALLOGCHR]) {
|
|
|
|
if (mem->mem_left == 0) {
|
|
|
|
mem->mem_left = ALIGN;
|
|
|
|
total_size += ALIGN;
|
|
|
|
base += ALIGN;
|
|
|
|
}
|
|
|
|
base += mem->mem_left;
|
|
|
|
total_size += mem->mem_left;
|
|
|
|
mem->mem_left--;
|
|
|
|
mem->mem_full++;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
base += mem->mem_left; /* Each piece will start after prev. */
|
|
|
|
total_size += mem->mem_left;
|
|
|
|
}
|
|
|
|
}
|
1985-01-08 11:54:57 +00:00
|
|
|
|
1986-12-02 16:16:08 +00:00
|
|
|
if (sbreak(total_size) == -1) {
|
1985-01-08 11:54:57 +00:00
|
|
|
incore = FALSE; /* In core strategy failed. */
|
1986-12-02 16:16:08 +00:00
|
|
|
if (sbreak(AT_LEAST) == -1)
|
1985-01-08 11:54:57 +00:00
|
|
|
fatal("no core at all");
|
2017-01-18 18:55:56 +00:00
|
|
|
|
1986-12-02 16:16:08 +00:00
|
|
|
base = BASE;
|
|
|
|
for (mem = mems; mem < &mems[NMEMS]; mem++) {
|
|
|
|
mem->mem_base = base;
|
1987-04-08 17:15:30 +00:00
|
|
|
if (mem == &mems[ALLOLCHR] || mem == &mems[ALLOGCHR]) {
|
|
|
|
base += ALIGN;
|
|
|
|
mem->mem_left = ALIGN - 1;
|
|
|
|
mem->mem_full = 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mem->mem_full = (ind_t)0;
|
|
|
|
mem->mem_left = 0;
|
|
|
|
}
|
1986-12-02 16:16:08 +00:00
|
|
|
}
|
1985-01-08 11:54:57 +00:00
|
|
|
}
|
2018-11-13 23:05:16 +00:00
|
|
|
#endif /* ndef USEMALLOC */
|
1985-01-08 11:54:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate an extra block of `incr' bytes and move all pieces with index
|
1987-05-21 10:06:14 +00:00
|
|
|
* higher than `piece' up with the size of the block.
|
|
|
|
* Move up as much as possible, if "incr" fails.
|
1985-01-08 11:54:57 +00:00
|
|
|
*/
|
1987-05-21 10:06:14 +00:00
|
|
|
static ind_t
|
1985-01-08 11:54:57 +00:00
|
|
|
move_up(piece, incr)
|
|
|
|
register int piece;
|
1985-01-10 13:35:39 +00:00
|
|
|
register ind_t incr;
|
1985-01-08 11:54:57 +00:00
|
|
|
{
|
2018-11-13 23:05:16 +00:00
|
|
|
#ifdef USEMALLOC
|
|
|
|
size_t oldsize = mems[piece].mem_full + mems[piece].mem_left;
|
|
|
|
size_t newsize;
|
|
|
|
char *newbase;
|
|
|
|
|
|
|
|
if (frozen)
|
|
|
|
return 0; /* Can't realloc() frozen core. */
|
|
|
|
|
|
|
|
/* We realloc() this piece without moving the other pieces. */
|
|
|
|
while (incr > 0) {
|
|
|
|
newsize = oldsize + incr;
|
|
|
|
if (newsize > oldsize) {
|
|
|
|
newbase = realloc(mems[piece].mem_base, newsize);
|
|
|
|
if (newbase != NULL) {
|
|
|
|
mems[piece].mem_base = newbase;
|
|
|
|
mems[piece].mem_left += incr;
|
|
|
|
return incr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
incr -= INCRSIZE < incr ? INCRSIZE : incr;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
#else /* ifndef USEMALLOC */
|
1985-01-08 11:54:57 +00:00
|
|
|
register struct memory *mem;
|
1987-08-11 15:31:09 +00:00
|
|
|
#ifndef NOSTATISTICS
|
|
|
|
extern int statistics;
|
|
|
|
#endif
|
1985-01-08 11:54:57 +00:00
|
|
|
|
|
|
|
debug("move_up(%d, %d)\n", piece, (int)incr, 0, 0);
|
1987-05-21 10:06:14 +00:00
|
|
|
while (incr > 0 && sbreak(incr) == -1)
|
2018-11-12 03:51:17 +00:00
|
|
|
incr -= INCRSIZE < incr ? INCRSIZE : incr;
|
1985-01-08 11:54:57 +00:00
|
|
|
|
2018-11-12 03:51:17 +00:00
|
|
|
if (incr == 0)
|
1987-05-21 10:06:14 +00:00
|
|
|
return (ind_t) 0;
|
1987-08-11 15:31:09 +00:00
|
|
|
#ifndef NOSTATISTICS
|
1990-12-14 13:57:46 +00:00
|
|
|
if (statistics) fprintf(stderr,"moving up %lx\n", (long) incr);
|
1987-08-11 15:31:09 +00:00
|
|
|
#endif
|
1985-01-08 11:54:57 +00:00
|
|
|
for (mem = &mems[NMEMS - 1]; mem > &mems[piece]; mem--)
|
|
|
|
copy_up(mem, incr);
|
|
|
|
|
|
|
|
mems[piece].mem_left += incr;
|
1987-05-21 10:06:14 +00:00
|
|
|
return incr;
|
2018-11-13 23:05:16 +00:00
|
|
|
#endif /* ndef USEMALLOC */
|
1985-01-08 11:54:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
extern int passnumber;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This routine is called if `piece' needs `incr' bytes and the system won't
|
|
|
|
* give them. We first steal the free bytes of all lower pieces and move them
|
|
|
|
* and `piece' down. If that doesn't give us enough bytes, we steal the free
|
|
|
|
* bytes of all higher pieces and move them up. We return whether we have
|
|
|
|
* enough bytes, the first or the second time.
|
|
|
|
*/
|
|
|
|
static bool
|
2017-08-06 13:57:49 +00:00
|
|
|
compact(int piece, ind_t incr, int flag)
|
1987-04-01 08:20:39 +00:00
|
|
|
#define NORMAL 0
|
|
|
|
#define FREEZE 1
|
|
|
|
#define FORCED 2
|
1985-01-08 11:54:57 +00:00
|
|
|
{
|
2018-11-13 23:05:16 +00:00
|
|
|
#ifdef USEMALLOC
|
|
|
|
struct memory *mem;
|
|
|
|
size_t newsize, oldsize;
|
|
|
|
char *newbase;
|
|
|
|
|
|
|
|
if (frozen)
|
|
|
|
return incr == 0; /* Can't realloc() frozen core. */
|
|
|
|
/*
|
|
|
|
* We realloc() to shrink most pieces.
|
|
|
|
* We can't control how realloc() moves the pieces.
|
|
|
|
*/
|
|
|
|
for (mem = mems; mem < &mems[NMEMS]; mem++) {
|
|
|
|
if (mem == &mems[piece])
|
|
|
|
continue;
|
|
|
|
if (flag == FREEZE && mem == &mems[ALLOMODL])
|
|
|
|
continue;
|
|
|
|
if (mem->mem_full == 0) {
|
|
|
|
/* Don't try to realloc() to size zero. */
|
|
|
|
free(mem->mem_base);
|
|
|
|
mem->mem_base = NULL;
|
|
|
|
mem->mem_left = 0;
|
|
|
|
} else {
|
|
|
|
newbase = realloc(mem->mem_base, mem->mem_full);
|
|
|
|
if (newbase != NULL) {
|
|
|
|
mem->mem_base = newbase;
|
|
|
|
mem->mem_left = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* After FREEZE, we must be able to grow ALLOMODL without
|
|
|
|
* moving it. The old allocator allowed ALLOMODL to grow in
|
|
|
|
* the brk area, but we can't call realloc() later, so we must
|
|
|
|
* leave some extra space in ALLOMODL now.
|
|
|
|
*/
|
|
|
|
if (flag == FREEZE) {
|
|
|
|
mem = &mems[ALLOMODL];
|
|
|
|
oldsize = mem->mem_full + mem->mem_left;
|
|
|
|
newsize = mem->mem_full + modl_initial_size / 2;
|
|
|
|
/* Don't shrink ALLOMODL. */
|
|
|
|
while (newsize > oldsize) {
|
|
|
|
newbase = realloc(mem->mem_base, newsize);
|
|
|
|
if (newbase != NULL) {
|
|
|
|
mem->mem_base = newbase;
|
|
|
|
mem->mem_left = newsize - mem->mem_full;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
newsize -= INCRSIZE < newsize ? INCRSIZE : newsize;
|
|
|
|
}
|
|
|
|
frozen = TRUE; /* Prevent later realloc(). */
|
|
|
|
}
|
|
|
|
/* Now grow our piece. */
|
|
|
|
if (incr == 0)
|
|
|
|
return TRUE;
|
|
|
|
mem = &mems[piece];
|
|
|
|
oldsize = mem->mem_full + mem->mem_left;
|
|
|
|
newsize = oldsize + incr;
|
|
|
|
if (newsize < mem->mem_full)
|
|
|
|
return FALSE; /* The size overflowed. */
|
|
|
|
newbase = realloc(mem->mem_base, newsize);
|
|
|
|
if (newbase == NULL)
|
|
|
|
return FALSE;
|
|
|
|
mem->mem_base = newbase;
|
|
|
|
mem->mem_left += incr;
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
#else /* ifndef USEMALLOC */
|
1986-12-03 13:06:48 +00:00
|
|
|
register ind_t gain, size;
|
1985-01-08 11:54:57 +00:00
|
|
|
register struct memory *mem;
|
1987-04-08 17:15:30 +00:00
|
|
|
int min = piece, max = piece;
|
1986-12-03 13:06:48 +00:00
|
|
|
#define SHIFT_COUNT 2 /* let pieces only contribute if their free
|
|
|
|
memory is more than 1/2**SHIFT_COUNT * 100 %
|
|
|
|
of its occupied memory
|
|
|
|
*/
|
1985-01-08 11:54:57 +00:00
|
|
|
|
1987-04-01 08:20:39 +00:00
|
|
|
debug("compact(%d, %d, %d)\n", piece, (int)incr, flag, 0);
|
1986-12-03 13:06:48 +00:00
|
|
|
for (mem = &mems[0]; mem < &mems[NMEMS - 1]; mem++) {
|
|
|
|
assert(mem->mem_base + mem->mem_full + mem->mem_left == (mem+1)->mem_base);
|
|
|
|
}
|
1987-04-08 17:15:30 +00:00
|
|
|
|
|
|
|
mem = &mems[piece];
|
|
|
|
if (flag == NORMAL) {
|
|
|
|
/* try and gain a bit more than needed */
|
|
|
|
gain = (mem->mem_full + incr) >> SHIFT_COUNT;
|
|
|
|
if (incr < gain) incr = gain;
|
|
|
|
}
|
2017-01-18 18:55:56 +00:00
|
|
|
|
1986-12-03 13:06:48 +00:00
|
|
|
/*
|
|
|
|
* First, check that moving will result in enough space
|
|
|
|
*/
|
1987-04-01 08:20:39 +00:00
|
|
|
if (flag != FREEZE) {
|
1987-04-08 17:15:30 +00:00
|
|
|
gain = mem->mem_left;
|
|
|
|
for (mem = &mems[piece-1]; mem >= &mems[0]; mem--) {
|
2017-01-18 18:55:56 +00:00
|
|
|
/*
|
|
|
|
* Don't give it all away!
|
1986-12-03 13:06:48 +00:00
|
|
|
* If this does not give us enough, bad luck
|
|
|
|
*/
|
1987-04-01 08:20:39 +00:00
|
|
|
if (flag == FORCED)
|
|
|
|
size = 0;
|
1987-04-08 17:15:30 +00:00
|
|
|
else {
|
|
|
|
size = mem->mem_full >> SHIFT_COUNT;
|
|
|
|
if (size == 0) size = mem->mem_left >> 1;
|
|
|
|
}
|
|
|
|
if (mem->mem_left >= size)
|
1986-12-03 13:06:48 +00:00
|
|
|
gain += (mem->mem_left - size) & ~(ALIGN - 1);
|
1987-04-08 17:15:30 +00:00
|
|
|
if (gain >= incr) {
|
|
|
|
min = mem - &mems[0];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (min == piece)
|
|
|
|
for (mem = &mems[piece+1]; mem <= &mems[NMEMS - 1]; mem++) {
|
2017-01-18 18:55:56 +00:00
|
|
|
/*
|
|
|
|
* Don't give it all away!
|
1987-04-08 17:15:30 +00:00
|
|
|
* If this does not give us enough, bad luck
|
|
|
|
*/
|
|
|
|
if (flag == FORCED)
|
|
|
|
size = 0;
|
|
|
|
else {
|
|
|
|
size = mem->mem_full >> SHIFT_COUNT;
|
|
|
|
if (size == 0) size = mem->mem_left >> 1;
|
|
|
|
}
|
|
|
|
if (mem->mem_left >= size)
|
|
|
|
gain += (mem->mem_left - size) & ~(ALIGN - 1);
|
|
|
|
if (gain >= incr) {
|
|
|
|
max = mem - &mems[0];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (min == piece) {
|
|
|
|
min = 0;
|
|
|
|
if (max == piece) max = 0;
|
1986-12-03 13:06:48 +00:00
|
|
|
}
|
|
|
|
if (gain < incr) return 0;
|
|
|
|
}
|
1987-04-08 17:15:30 +00:00
|
|
|
else {
|
|
|
|
min = 0;
|
|
|
|
max = NMEMS - 1;
|
|
|
|
}
|
1986-12-03 13:06:48 +00:00
|
|
|
|
|
|
|
gain = 0;
|
1987-04-08 17:15:30 +00:00
|
|
|
for (mem = &mems[min]; mem != &mems[piece]; mem++) {
|
1985-01-08 11:54:57 +00:00
|
|
|
/* Here memory is inserted before a piece. */
|
1985-01-10 13:35:39 +00:00
|
|
|
assert(passnumber == FIRST || gain == (ind_t)0);
|
1987-04-08 17:15:30 +00:00
|
|
|
if (gain) copy_down(mem, gain);
|
1987-04-01 08:20:39 +00:00
|
|
|
if (flag == FREEZE || gain < incr) {
|
|
|
|
if (flag != NORMAL) size = 0;
|
1987-04-08 17:15:30 +00:00
|
|
|
else {
|
|
|
|
size = mem->mem_full >> SHIFT_COUNT;
|
|
|
|
if (size == 0) size = mem->mem_left >> 1;
|
|
|
|
}
|
|
|
|
if (mem->mem_left >= size) {
|
1986-12-03 13:06:48 +00:00
|
|
|
size = (mem->mem_left - size) & ~(ALIGN - 1);
|
|
|
|
gain += size;
|
|
|
|
mem->mem_left -= size;
|
|
|
|
}
|
|
|
|
}
|
1985-01-08 11:54:57 +00:00
|
|
|
}
|
|
|
|
/*
|
1986-12-03 13:06:48 +00:00
|
|
|
* Now mems[piece]:
|
1985-01-08 11:54:57 +00:00
|
|
|
*/
|
1987-04-08 17:15:30 +00:00
|
|
|
if (gain) copy_down(mem, gain);
|
|
|
|
gain += mem->mem_left;
|
|
|
|
mem->mem_left = 0;
|
1986-12-03 13:06:48 +00:00
|
|
|
|
1985-01-08 11:54:57 +00:00
|
|
|
if (gain < incr) {
|
1985-01-10 13:35:39 +00:00
|
|
|
register ind_t up = (ind_t)0;
|
1985-01-08 11:54:57 +00:00
|
|
|
|
1987-04-08 17:15:30 +00:00
|
|
|
for (mem = &mems[max]; mem > &mems[piece]; mem--) {
|
1985-01-08 11:54:57 +00:00
|
|
|
/* Here memory is appended after a piece. */
|
1987-04-01 08:20:39 +00:00
|
|
|
if (flag == FREEZE || gain + up < incr) {
|
1987-04-08 17:15:30 +00:00
|
|
|
if (flag != NORMAL) size = 0;
|
|
|
|
else {
|
|
|
|
size = mem->mem_full >> SHIFT_COUNT;
|
|
|
|
if (size == 0) size = mem->mem_left >> 1;
|
|
|
|
}
|
|
|
|
if (mem->mem_left >= size) {
|
1986-12-03 13:06:48 +00:00
|
|
|
size = (mem->mem_left - size) & ~(ALIGN - 1);
|
|
|
|
up += size;
|
|
|
|
mem->mem_left -= size;
|
|
|
|
}
|
|
|
|
}
|
1987-04-08 17:15:30 +00:00
|
|
|
if (up) copy_up(mem, up);
|
1985-01-08 11:54:57 +00:00
|
|
|
}
|
|
|
|
gain += up;
|
|
|
|
}
|
1986-12-02 16:16:08 +00:00
|
|
|
mems[piece].mem_left += gain;
|
1987-04-01 08:20:39 +00:00
|
|
|
assert(flag == FREEZE || gain >= incr);
|
1986-12-03 13:06:48 +00:00
|
|
|
for (mem = &mems[0]; mem < &mems[NMEMS - 1]; mem++) {
|
|
|
|
assert(mem->mem_base + mem->mem_full + mem->mem_left == (mem+1)->mem_base);
|
|
|
|
}
|
1985-01-08 11:54:57 +00:00
|
|
|
return gain >= incr;
|
2018-11-13 23:05:16 +00:00
|
|
|
#endif /* ndef USEMALLOC */
|
1985-01-08 11:54:57 +00:00
|
|
|
}
|
|
|
|
|
2018-11-13 23:05:16 +00:00
|
|
|
#ifndef USEMALLOC
|
1985-01-08 11:54:57 +00:00
|
|
|
/*
|
|
|
|
* The bytes of `mem' must be moved `dist' down in the address space.
|
|
|
|
* We copy the bytes from low to high, because the tail of the new area may
|
|
|
|
* overlap with the old area, but we do not want to overwrite them before they
|
|
|
|
* are copied.
|
|
|
|
*/
|
2017-08-06 13:57:49 +00:00
|
|
|
static void
|
|
|
|
copy_down(struct memory* mem, ind_t dist)
|
1985-01-08 11:54:57 +00:00
|
|
|
{
|
|
|
|
register char *old;
|
|
|
|
register char *new;
|
1985-01-10 13:35:39 +00:00
|
|
|
register ind_t size;
|
1985-01-08 11:54:57 +00:00
|
|
|
|
|
|
|
size = mem->mem_full;
|
|
|
|
old = mem->mem_base;
|
|
|
|
new = old - dist;
|
|
|
|
mem->mem_base = new;
|
|
|
|
while (size--)
|
|
|
|
*new++ = *old++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The bytes of `mem' must be moved `dist' up in the address space.
|
|
|
|
* We copy the bytes from high to low, because the tail of the new area may
|
|
|
|
* overlap with the old area, but we do not want to overwrite them before they
|
|
|
|
* are copied.
|
|
|
|
*/
|
2017-08-06 13:57:49 +00:00
|
|
|
static void copy_up(struct memory* mem, ind_t dist)
|
1985-01-08 11:54:57 +00:00
|
|
|
{
|
|
|
|
register char *old;
|
|
|
|
register char *new;
|
1985-01-10 13:35:39 +00:00
|
|
|
register ind_t size;
|
1985-01-08 11:54:57 +00:00
|
|
|
|
|
|
|
size = mem->mem_full;
|
|
|
|
old = mem->mem_base + size;
|
|
|
|
new = old + dist;
|
|
|
|
while (size--)
|
|
|
|
*--new = *--old;
|
|
|
|
mem->mem_base = new;
|
|
|
|
}
|
2018-11-13 23:05:16 +00:00
|
|
|
#endif /* ndef USEMALLOC */
|
1985-01-08 11:54:57 +00:00
|
|
|
|
1987-04-01 08:20:39 +00:00
|
|
|
static int alloctype = NORMAL;
|
|
|
|
|
1985-01-08 11:54:57 +00:00
|
|
|
/*
|
|
|
|
* Add `size' bytes to the bytes already allocated for `piece'. If it has no
|
|
|
|
* free bytes left, ask them from memory or, if that fails, from the free
|
|
|
|
* bytes of other pieces. The offset of the new area is returned. No matter
|
|
|
|
* how many times the area is moved, because of another allocate, this offset
|
|
|
|
* remains valid.
|
|
|
|
*/
|
2018-11-12 03:51:17 +00:00
|
|
|
ind_t alloc(int piece, size_t size)
|
1985-01-08 11:54:57 +00:00
|
|
|
{
|
1985-01-10 13:35:39 +00:00
|
|
|
register ind_t incr = 0;
|
1986-10-20 10:17:57 +00:00
|
|
|
ind_t left = mems[piece].mem_left;
|
1985-01-10 13:35:39 +00:00
|
|
|
register ind_t full = mems[piece].mem_full;
|
1985-01-08 11:54:57 +00:00
|
|
|
|
|
|
|
assert(passnumber == FIRST || (!incore && piece == ALLOMODL));
|
2018-11-12 03:51:17 +00:00
|
|
|
if (size == 0)
|
1985-01-08 11:54:57 +00:00
|
|
|
return full;
|
1985-01-10 13:35:39 +00:00
|
|
|
if (size != (ind_t)size)
|
|
|
|
return BADOFF;
|
1988-04-21 18:53:31 +00:00
|
|
|
switch(piece) {
|
|
|
|
case ALLOMODL:
|
|
|
|
case ALLORANL:
|
|
|
|
size = int_align(size);
|
2018-11-12 03:51:17 +00:00
|
|
|
if (size == 0)
|
|
|
|
return BADOFF;
|
1988-04-21 18:53:31 +00:00
|
|
|
}
|
1985-01-08 11:54:57 +00:00
|
|
|
|
2018-11-12 03:51:17 +00:00
|
|
|
if (size > left) {
|
1987-05-21 10:06:14 +00:00
|
|
|
incr = ((size - left + (INCRSIZE - 1)) / INCRSIZE) * INCRSIZE;
|
2018-11-12 03:51:17 +00:00
|
|
|
if (incr == 0)
|
|
|
|
return BADOFF;
|
|
|
|
}
|
1987-05-21 10:06:14 +00:00
|
|
|
|
1987-04-01 08:20:39 +00:00
|
|
|
if (incr == 0 ||
|
2018-11-12 03:51:17 +00:00
|
|
|
(incr < left + full && move_up(piece, left + full) >= incr) ||
|
1987-05-21 10:06:14 +00:00
|
|
|
move_up(piece, incr) == incr ||
|
1987-04-01 08:20:39 +00:00
|
|
|
compact(piece, size, alloctype)) {
|
1985-01-08 11:54:57 +00:00
|
|
|
mems[piece].mem_full += size;
|
|
|
|
mems[piece].mem_left -= size;
|
|
|
|
return full;
|
|
|
|
} else {
|
|
|
|
incore = FALSE;
|
|
|
|
return BADOFF;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Same as alloc() but for a piece which really needs it. If the first
|
|
|
|
* attempt fails, release the space occupied by other pieces and try again.
|
|
|
|
*/
|
1985-01-10 13:35:39 +00:00
|
|
|
ind_t
|
2018-11-12 03:51:17 +00:00
|
|
|
hard_alloc(int piece, size_t size)
|
1985-01-08 11:54:57 +00:00
|
|
|
{
|
1985-01-10 13:35:39 +00:00
|
|
|
register ind_t ret;
|
1985-01-08 11:54:57 +00:00
|
|
|
register int i;
|
|
|
|
|
1985-01-10 13:35:39 +00:00
|
|
|
if (size != (ind_t)size)
|
|
|
|
return BADOFF;
|
1987-04-01 08:20:39 +00:00
|
|
|
if ((ret = alloc(piece, size)) != BADOFF) {
|
1985-01-08 11:54:57 +00:00
|
|
|
return ret;
|
1987-04-01 08:20:39 +00:00
|
|
|
}
|
1985-01-08 11:54:57 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Deallocate what we don't need.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < NMEMS; i++) {
|
|
|
|
switch (i) {
|
|
|
|
case ALLOGLOB:
|
|
|
|
case ALLOGCHR:
|
|
|
|
case ALLOSYMB:
|
|
|
|
case ALLOARCH:
|
|
|
|
case ALLOMODL:
|
1988-01-11 18:24:34 +00:00
|
|
|
case ALLORANL:
|
1985-01-08 11:54:57 +00:00
|
|
|
break; /* Do not try to deallocate this. */
|
|
|
|
default:
|
|
|
|
dealloc(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free_saved_moduls();
|
|
|
|
|
1987-04-08 17:15:30 +00:00
|
|
|
if ((ret = alloc(piece, size)) != BADOFF) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
alloctype = FORCED;
|
1987-04-01 08:20:39 +00:00
|
|
|
ret = alloc(piece, size);
|
|
|
|
alloctype = NORMAL;
|
|
|
|
return ret;
|
1985-01-08 11:54:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We don't need the previous modules, so we put the current module
|
|
|
|
* at the start of the piece allocated for module contents, thereby
|
|
|
|
* overwriting the saved modules, and release its space.
|
|
|
|
*/
|
2017-08-06 13:57:49 +00:00
|
|
|
static void
|
|
|
|
free_saved_moduls(void)
|
1985-01-08 11:54:57 +00:00
|
|
|
{
|
1985-01-10 13:35:39 +00:00
|
|
|
register ind_t size;
|
1985-01-08 11:54:57 +00:00
|
|
|
register char *old, *new;
|
|
|
|
register struct memory *mem = &mems[ALLOMODL];
|
|
|
|
|
|
|
|
size = mem->mem_full - core_position;
|
|
|
|
new = mem->mem_base;
|
|
|
|
old = new + core_position;
|
|
|
|
while (size--)
|
|
|
|
*new++ = *old++;
|
|
|
|
mem->mem_full -= core_position;
|
|
|
|
mem->mem_left += core_position;
|
1985-01-10 13:35:39 +00:00
|
|
|
core_position = (ind_t)0;
|
1985-01-08 11:54:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The piece of memory with index `piece' is no longer needed.
|
|
|
|
* We take care that it can be used by compact() later, if needed.
|
|
|
|
*/
|
2017-08-06 09:58:36 +00:00
|
|
|
void
|
|
|
|
dealloc(int piece)
|
1985-01-08 11:54:57 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Some pieces need their memory throughout the program.
|
|
|
|
*/
|
|
|
|
assert(piece != ALLOGLOB);
|
|
|
|
assert(piece != ALLOGCHR);
|
|
|
|
assert(piece != ALLOSYMB);
|
|
|
|
assert(piece != ALLOARCH);
|
|
|
|
mems[piece].mem_left += mems[piece].mem_full;
|
1985-01-10 13:35:39 +00:00
|
|
|
mems[piece].mem_full = (ind_t)0;
|
1985-01-08 11:54:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
2018-11-12 03:51:17 +00:00
|
|
|
core_alloc(int piece, size_t size)
|
1985-01-08 11:54:57 +00:00
|
|
|
{
|
1985-01-10 13:35:39 +00:00
|
|
|
register ind_t off;
|
1985-01-08 11:54:57 +00:00
|
|
|
|
1987-05-21 10:10:27 +00:00
|
|
|
if ((off = alloc(piece, size)) == BADOFF)
|
|
|
|
return (char *)0;
|
1985-01-08 11:54:57 +00:00
|
|
|
return address(piece, off);
|
|
|
|
}
|
|
|
|
|
2017-08-06 09:58:36 +00:00
|
|
|
void core_free(int piece, char* p)
|
1987-05-21 10:06:14 +00:00
|
|
|
{
|
|
|
|
char *q = address(piece, mems[piece].mem_full);
|
|
|
|
|
|
|
|
assert(p < q);
|
2018-11-12 03:51:17 +00:00
|
|
|
mems[piece].mem_full -= (ind_t) q - (ind_t) p;
|
|
|
|
mems[piece].mem_left += (ind_t) q - (ind_t) p;
|
1987-05-21 10:06:14 +00:00
|
|
|
}
|
|
|
|
|
1985-01-08 11:54:57 +00:00
|
|
|
/*
|
|
|
|
* Reset index into piece of memory for modules and
|
|
|
|
* take care that the allocated pieces will not be moved.
|
|
|
|
*/
|
2019-03-24 09:08:45 +00:00
|
|
|
void freeze_core(void)
|
1985-01-08 11:54:57 +00:00
|
|
|
{
|
|
|
|
register int i;
|
|
|
|
|
1985-01-10 13:35:39 +00:00
|
|
|
core_position = (ind_t)0;
|
1985-01-08 11:54:57 +00:00
|
|
|
|
|
|
|
if (incore)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (i = 0; i < NMEMS; i++) {
|
|
|
|
switch (i) {
|
|
|
|
case ALLOGLOB:
|
|
|
|
case ALLOGCHR:
|
|
|
|
case ALLOSYMB:
|
|
|
|
case ALLOARCH:
|
|
|
|
break; /* Do not try to deallocate this. */
|
|
|
|
default:
|
|
|
|
dealloc(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
1987-04-01 08:20:39 +00:00
|
|
|
compact(NMEMS - 1, (ind_t)0, FREEZE);
|
1985-01-08 11:54:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* To transform the various pieces of the output in core to the file format,
|
1991-12-17 15:28:58 +00:00
|
|
|
* we must order the bytes in the unsigned shorts and longs as ACK prescribes.
|
1985-01-08 11:54:57 +00:00
|
|
|
*/
|
2017-08-06 13:57:49 +00:00
|
|
|
void write_bytes(void)
|
1985-01-08 11:54:57 +00:00
|
|
|
{
|
1991-12-17 15:28:58 +00:00
|
|
|
unsigned short nsect;
|
1985-01-10 13:35:39 +00:00
|
|
|
long offchar;
|
|
|
|
register struct memory *mem;
|
|
|
|
extern long NLChars, NGChars;
|
|
|
|
extern int flagword;
|
|
|
|
extern struct outhead outhead;
|
|
|
|
extern struct outsect outsect[];
|
|
|
|
extern char *outputname;
|
1986-10-20 10:17:57 +00:00
|
|
|
int sectionno = 0;
|
1985-01-08 11:54:57 +00:00
|
|
|
|
1985-01-10 13:35:39 +00:00
|
|
|
nsect = outhead.oh_nsect;
|
|
|
|
offchar = OFF_CHAR(outhead);
|
1985-01-08 11:54:57 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We allocated two areas: one for local and one for global names.
|
|
|
|
* Also, we used another kind of on_foff than on file.
|
|
|
|
* At the end of the global area we have put the section names.
|
|
|
|
*/
|
|
|
|
if (!(flagword & SFLAG)) {
|
1990-07-30 11:56:28 +00:00
|
|
|
do_crs((struct outname *)mems[ALLOLOCL].mem_base, NLocals);
|
1985-01-08 11:54:57 +00:00
|
|
|
namecpy((struct outname *)mems[ALLOLOCL].mem_base,
|
|
|
|
NLocals,
|
|
|
|
offchar
|
|
|
|
);
|
|
|
|
namecpy((struct outname *)mems[ALLOGLOB].mem_base,
|
|
|
|
NGlobals + nsect,
|
|
|
|
offchar + NLChars
|
|
|
|
);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* These pieces must always be written.
|
|
|
|
*/
|
1986-10-20 10:17:57 +00:00
|
|
|
wr_ohead(&outhead);
|
|
|
|
wr_sect(outsect, nsect);
|
1985-01-10 13:35:39 +00:00
|
|
|
for (mem = &mems[ALLOEMIT]; mem < &mems[ALLORELO]; mem++)
|
1986-10-20 10:17:57 +00:00
|
|
|
wrt_emit(mem->mem_base, sectionno++, mem->mem_full);
|
1985-01-08 11:54:57 +00:00
|
|
|
/*
|
|
|
|
* The rest depends on the flags.
|
|
|
|
*/
|
1987-08-26 13:22:44 +00:00
|
|
|
if (flagword & (RFLAG|CFLAG))
|
1986-10-20 10:17:57 +00:00
|
|
|
wr_relo((struct outrelo *) mems[ALLORELO].mem_base,
|
|
|
|
outhead.oh_nrelo);
|
1985-01-08 11:54:57 +00:00
|
|
|
if (!(flagword & SFLAG)) {
|
1986-10-20 10:17:57 +00:00
|
|
|
wr_name((struct outname *) mems[ALLOLOCL].mem_base,
|
|
|
|
NLocals);
|
|
|
|
wr_name((struct outname *) mems[ALLOGLOB].mem_base,
|
|
|
|
NGlobals+nsect);
|
|
|
|
wr_string(mems[ALLOLCHR].mem_base + 1, (long)NLChars);
|
|
|
|
wr_string(mems[ALLOGCHR].mem_base + 1, (long)NGChars);
|
1985-01-08 11:54:57 +00:00
|
|
|
#ifdef SYMDBUG
|
1986-10-20 10:17:57 +00:00
|
|
|
wr_dbug(mems[ALLODBUG].mem_base, mems[ALLODBUG].mem_full);
|
1991-12-17 15:28:58 +00:00
|
|
|
#endif /* SYMDBUG */
|
1985-01-08 11:54:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-06 13:57:49 +00:00
|
|
|
void namecpy(struct outname* name, unsigned nname, long offchar)
|
1985-01-08 11:54:57 +00:00
|
|
|
{
|
|
|
|
while (nname--) {
|
|
|
|
if (name->on_foff)
|
|
|
|
name->on_foff += offchar - 1;
|
|
|
|
name++;
|
|
|
|
}
|
|
|
|
}
|