new version with internal buffering

This commit is contained in:
ceriel 1989-11-16 15:52:08 +00:00
parent e93e256512
commit 42a7f3d8e7
3 changed files with 225 additions and 137 deletions

View file

@ -4,6 +4,8 @@
* See the copyright notice in the ACK home directory, in the file "Copyright". * See the copyright notice in the ACK home directory, in the file "Copyright".
*/ */
#include "byte_order.h" #include "byte_order.h"
#include <local.h>
#include <stdio.h>
#if CHAR_UNSIGNED #if CHAR_UNSIGNED
#define Xchar(ch) (ch) #define Xchar(ch) (ch)
@ -33,3 +35,37 @@
#define get4(c) (* ((long *) (c))) #define get4(c) (* ((long *) (c)))
#define put4(l, c) (* ((long *) (c)) = (l)) #define put4(l, c) (* ((long *) (c)) = (l))
#endif #endif
#if BIGMACHINE
#define WBUFSIZ (8*BUFSIZ)
#else
#define WBUFSIZ BUFSIZ
#endif
struct fil {
int cnt;
char *pnow;
char *pbegin;
long currpos;
#ifndef OUTSEEK
int fd;
#endif
char pbuf[WBUFSIZ];
};
extern struct fil __parts[];
#define OUTBYTE(p, b) {if (__parts[p].cnt == 0) __wr_flush(&__parts[p]);\
__parts[p].cnt--; *__parts[p].pnow++ = (b);}
#define PARTEMIT 0
#define PARTRELO 1
#define PARTNAME 2
#define PARTCHAR 3
#ifdef SYMDBUG
#define PARTDBUG 4
#else
#define PARTDBUG 3
#endif
#define NPARTS (PARTDBUG + 1)

View file

@ -19,63 +19,118 @@ extern long lseek();
/* /*
* Parts of the output file. * Parts of the output file.
*/ */
#define PARTEMIT 0
#define PARTRELO 1
#define PARTNAME 2
#define PARTCHAR 3
#ifdef SYMDBUG
#define PARTDBUG 4
#else
#define PARTDBUG 3
#endif
#define NPARTS (PARTDBUG + 1)
static long offset[MAXSECT]; static long offset[MAXSECT];
struct fil __parts[NPARTS];
#ifdef OUTSEEK #ifdef OUTSEEK
static int outfile; static int outfile;
static long outseek[NPARTS];
static long currpos; static long currpos;
#define OUTSECT(i) \ #endif
(outseek[PARTEMIT] = offset[i]) static int sectionnr;
static static int offcnt;
OUTWRITE(p, b, n) {
char *b;
long n;
{
register long l = outseek[p];
if (currpos != l) { __wr_flush(ptr)
lseek(outfile, l, 0); register struct fil *ptr;
{
if (ptr->pnow > ptr->pbegin) {
#ifdef OUTSEEK
if (currpos != ptr->currpos) {
lseek(ptr->fd, ptr->currpos, 0);
}
#endif
wr_bytes(ptr->fd, ptr->pbegin, (long)(ptr->pnow - ptr->pbegin));
ptr->currpos += ptr->pnow - ptr->pbegin;
if (ptr == &__parts[PARTEMIT]) {
offset[sectionnr] = ptr->currpos;
}
} }
wr_bytes(outfile, b, n); ptr->cnt = WBUFSIZ;
currpos = l + n; ptr->pnow = ptr->pbuf;
outseek[p] = currpos; ptr->pbegin = ptr->pbuf;
#ifdef OUTSEEK
currpos = ptr->currpos;
#endif
} }
#define BEGINSEEK(p, o) \ static OUTWRITE(p, b, n)
(outseek[(p)] = (o)) int p; /* part number */
register char *b; /* buffer pointer */
long n; /* write count */
{
register struct fil *ptr = &__parts[p];
register char *pn = ptr->pnow;
register int i;
long m;
#else OUTSEEK i = ptr->cnt;
if (i < WBUFSIZ) {
if (n > i) {
__wr_flush(ptr);
wr_bytes(ptr->fd, b, (long) i);
n -= i;
b += i;
ptr->currpos += i;
if (ptr == &__parts[PARTEMIT]) {
offset[sectionnr] = ptr->currpos;
}
}
else {
i = n;
ptr->cnt -= i;
n = 0;
while (i > 0) {
*pn++ = *b++;
i--;
}
ptr->pnow = pn;
}
}
if (ptr->cnt == 0 || ptr->cnt == WBUFSIZ) {
__wr_flush(ptr);
m = n & ~(WBUFSIZ - 1);
if (m != 0) {
wr_bytes(ptr->fd, b, m);
b += m;
n &= WBUFSIZ - 1;
ptr->currpos += m;
if (ptr == &__parts[PARTEMIT]) {
offset[sectionnr] = ptr->currpos;
}
}
i = n;
if (i != 0) {
n = 0;
pn = ptr->pnow;
ptr->cnt -= i;
while (i > 0) {
*pn++ = *b++;
i--;
}
ptr->pnow = pn;
}
#ifdef OUTSEEK
currpos = ptr->currpos;
#endif
}
}
static int outfile[NPARTS]; static BEGINSEEK(p, o)
static long currpos[NPARTS]; int p; /* part number */
#define OUTSECT(i) \ long o; /* offset in file */
(currpos[PARTEMIT] == offset[(i)] ?\ {
0 :\ register struct fil *ptr = &__parts[p];
(currpos[PARTEMIT] = offset[(i)],\
lseek(outfile[PARTEMIT], currpos[PARTEMIT], 0)))
#define OUTWRITE(p, b, n) \
(wr_bytes(outfile[(p)], (b), (n)), currpos[(p)] += (n))
#define BEGINSEEK(p, o) \
(currpos[(p)] = lseek(outfile[(p)], (o), 0))
#endif OUTSEEK #ifdef OUTSEEK
ptr->fd = outfile;
ptr->currpos = o;
#else
ptr->currpos = lseek(ptr->fd, o, 0);
#endif
ptr->cnt = WBUFSIZ - ((int)o & (WBUFSIZ-1));
ptr->pbegin = ptr->pbuf + (WBUFSIZ - ptr->cnt);
ptr->pnow = ptr->pbegin;
}
int _ocnt;
char *_pbuf;
static int sectionnr;
static int offcnt;
/* /*
* Open the output file according to the chosen strategy. * Open the output file according to the chosen strategy.
@ -84,42 +139,40 @@ int
wr_open(f) wr_open(f)
char *f; char *f;
{ {
#ifndef OUTSEEK register struct fil *fdp;
register int *fdp;
#endif OUTSEEK
close(creat(f, 0666)); close(creat(f, 0666));
#ifdef OUTSEEK #ifdef OUTSEEK
if ((outfile = open(f, 1)) < 0) if ((outfile = open(f, 1)) < 0)
return 0; return 0;
currpos = 0; currpos = 0;
#else OUTSEEK #else /* not OUTSEEK */
for (fdp = &outfile[PARTEMIT]; fdp < &outfile[NPARTS]; fdp++) for (fdp = &__parts[PARTEMIT]; fdp < &__parts[NPARTS]; fdp++)
if ((*fdp = open(f, 1)) < 0) if ((fdp->fd = open(f, 1)) < 0)
return 0; return 0;
#endif OUTSEEK #endif /* not OUTSEEK */
offcnt = 0; offcnt = 0;
return 1; return 1;
} }
wr_close() wr_close()
{ {
register struct fil *ptr;
#ifndef OUTSEEK #ifndef OUTSEEK
register int *fdp; register int *fdp;
#endif not OUTSEEK #endif /* not OUTSEEK */
if (_ocnt) { for (ptr = &__parts[PARTEMIT]; ptr < &__parts[NPARTS]; ptr++) {
wr_flush(); __wr_flush(ptr);
#ifndef OUTSEEK
close(ptr->fd);
#endif /* not OUTSEEK */
ptr->fd = -1;
} }
#ifdef OUTSEEK #ifdef OUTSEEK
close(outfile); close(outfile);
outfile = -1; outfile = -1;
#else not OUTSEEK #endif /* OUTSEEK */
for (fdp = &outfile[PARTEMIT]; fdp < &outfile[NPARTS]; fdp++) {
close(*fdp);
*fdp = -1;
}
#endif not OUTSEEK
} }
wr_fd() wr_fd()
@ -127,25 +180,27 @@ wr_fd()
#ifdef OUTSEEK #ifdef OUTSEEK
return outfile; return outfile;
#else #else
return outfile[PARTEMIT]; return __parts[PARTEMIT].fd;
#endif #endif
} }
wr_ohead(head) wr_ohead(head)
register struct outhead *head; register struct outhead *head;
{ {
register long off = OFF_RELO(*head); {
register long off = OFF_RELO(*head);
BEGINSEEK(PARTEMIT, 0L); BEGINSEEK(PARTEMIT, 0L);
BEGINSEEK(PARTRELO, off); BEGINSEEK(PARTRELO, off);
off += (long) head->oh_nrelo * SZ_RELO; off += (long) head->oh_nrelo * SZ_RELO;
BEGINSEEK(PARTNAME, off); BEGINSEEK(PARTNAME, off);
off += (long) head->oh_nname * SZ_NAME; off += (long) head->oh_nname * SZ_NAME;
BEGINSEEK(PARTCHAR, off); BEGINSEEK(PARTCHAR, off);
#ifdef SYMDBUG #ifdef SYMDBUG
off += head->oh_nchar; off += head->oh_nchar;
BEGINSEEK(PARTDBUG, off); BEGINSEEK(PARTDBUG, off);
#endif #endif
}
#if ! (BYTES_REVERSED || WORDS_REVERSED) #if ! (BYTES_REVERSED || WORDS_REVERSED)
if (sizeof(struct outhead) != SZ_HEAD) if (sizeof(struct outhead) != SZ_HEAD)
#endif #endif
@ -164,7 +219,9 @@ wr_ohead(head)
OUTWRITE(PARTEMIT, buf, (long) SZ_HEAD); OUTWRITE(PARTEMIT, buf, (long) SZ_HEAD);
} }
#if ! (BYTES_REVERSED || WORDS_REVERSED) #if ! (BYTES_REVERSED || WORDS_REVERSED)
else OUTWRITE(PARTEMIT, (char *) head, (long) SZ_HEAD); else {
OUTWRITE(PARTEMIT, (char *)head, (long)SZ_HEAD);
}
#endif #endif
} }
@ -172,26 +229,26 @@ wr_sect(sect, cnt1)
register struct outsect *sect; register struct outsect *sect;
unsigned int cnt1; unsigned int cnt1;
{ {
register unsigned int cnt = cnt1;
char buf[MAXSECT * SZ_SECT]; char buf[MAXSECT * SZ_SECT];
register char *c = buf; register char *c = buf;
register unsigned int cnt = cnt1;
while (cnt--) { while (cnt--) {
#if ! (BYTES_REVERSED || WORDS_REVERSED) #if ! (BYTES_REVERSED || WORDS_REVERSED)
if (sizeof(struct outsect) != SZ_SECT) if (sizeof(struct outsect) != SZ_SECT)
#endif #endif
{ {
put4(sect->os_base, c); c += 4; put4(sect->os_base, c); c += 4;
put4(sect->os_size, c); c += 4; put4(sect->os_size, c); c += 4;
put4(sect->os_foff, c); c += 4; put4(sect->os_foff, c); c += 4;
} }
offset[offcnt++] = sect->os_foff; offset[offcnt++] = sect->os_foff;
#if ! (BYTES_REVERSED || WORDS_REVERSED) #if ! (BYTES_REVERSED || WORDS_REVERSED)
if (sizeof(struct outsect) != SZ_SECT) if (sizeof(struct outsect) != SZ_SECT)
#endif #endif
{ {
put4(sect->os_flen, c); c += 4; put4(sect->os_flen, c); c += 4;
put4(sect->os_lign, c); c += 4; put4(sect->os_lign, c); c += 4;
} }
sect++; sect++;
} }
@ -205,22 +262,29 @@ wr_sect(sect, cnt1)
#endif #endif
} }
wr_flush()
{
OUTWRITE(PARTEMIT, _pbuf, (long) _ocnt);
offset[sectionnr] += _ocnt;
_ocnt = 0;
}
wr_outsect(s) wr_outsect(s)
int s; /* section number */
{ {
if (s != sectionnr) { register struct fil *ptr = &__parts[PARTEMIT];
if (_ocnt) {
wr_flush(); if (s != sectionnr &&
} offset[s] != ptr->currpos + (ptr->pnow - ptr->pbegin)) {
sectionnr = s; #ifdef OUTSEEK
OUTSECT(s); if (currpos != ptr->currpos)
currpos = lseek(ptr->fd, ptr->currpos, 0);
#endif
wr_bytes(ptr->fd, ptr->pbegin, (long)(ptr->pnow - ptr->pbegin));
#ifdef OUTSEEK
currpos += ptr->pnow - ptr->pbegin;
#endif
ptr->currpos += ptr->pnow - ptr->pbegin;
offset[sectionnr] = ptr->currpos;
ptr->currpos = lseek(ptr->fd, offset[s], 0);
ptr->cnt = WBUFSIZ - ((int)offset[s] & (WBUFSIZ-1));
ptr->pbegin = ptr->pbuf + (WBUFSIZ - ptr->cnt);
ptr->pnow = ptr->pbegin;
} }
sectionnr = s;
} }
/* /*
@ -230,81 +294,78 @@ wr_emit(emit, cnt)
char *emit; char *emit;
long cnt; long cnt;
{ {
if (_ocnt) wr_flush();
OUTWRITE(PARTEMIT, emit, cnt); OUTWRITE(PARTEMIT, emit, cnt);
offset[sectionnr] += cnt;
} }
wr_relo(relo, cnt) wr_relo(relo, cnt)
register struct outrelo *relo; register struct outrelo *relo;
register unsigned int cnt; unsigned int cnt;
{ {
long l;
#if ! (BYTES_REVERSED || WORDS_REVERSED) #if ! (BYTES_REVERSED || WORDS_REVERSED)
if (sizeof(struct outrelo) != SZ_RELO) if (sizeof(struct outrelo) != SZ_RELO)
#endif #endif
while (cnt)
{ {
char buf[100 * SZ_RELO]; register char *c;
register char *c = buf; register unsigned int i;
register int i = 0;
while (cnt--) { i = __parts[PARTRELO].cnt/SZ_RELO;
c = __parts[PARTRELO].pnow;
if (i > cnt) i = cnt;
cnt -= i;
__parts[PARTRELO].cnt -= (i*SZ_RELO);
while (i--) {
*c++ = relo->or_type; *c++ = relo->or_type;
*c++ = relo->or_sect; *c++ = relo->or_sect;
put2(relo->or_nami, c); c += 2; put2(relo->or_nami, c); c += 2;
put4(relo->or_addr, c); c += 4; put4(relo->or_addr, c); c += 4;
relo++; relo++;
i++; }
if (i == 100 || cnt == 0) { __parts[PARTRELO].pnow = c;
c = buf; if (cnt) {
l = (long) (i * SZ_RELO); __wr_flush(&__parts[PARTRELO]);
OUTWRITE(PARTRELO, c, l);
i = 0;
}
} }
} }
#if ! (BYTES_REVERSED || WORDS_REVERSED) #if ! (BYTES_REVERSED || WORDS_REVERSED)
else { else {
l = (long) cnt * SZ_RELO; OUTWRITE(PARTRELO, (char *) relo, (long) cnt * SZ_RELO);
OUTWRITE(PARTRELO, (char *) relo, l);
} }
#endif #endif
} }
wr_name(name, cnt) wr_name(name, cnt)
register struct outname *name; register struct outname *name;
register unsigned int cnt; unsigned int cnt;
{ {
long l; register unsigned int i = cnt;
#if ! (BYTES_REVERSED || WORDS_REVERSED) #if ! (BYTES_REVERSED || WORDS_REVERSED)
if (sizeof(struct outname) != SZ_NAME) if (sizeof(struct outname) != SZ_NAME)
#endif #endif
while (cnt)
{ {
char buf[100 * SZ_NAME]; register char *c;
register char *c = buf; register unsigned int i;
register int i = 0;
while (cnt--) { i = __parts[PARTNAME].cnt/SZ_NAME;
put4(name->on_foff,c); c += 4; c = __parts[PARTNAME].pnow;
put2(name->on_type,c); c += 2; if (i > cnt) i = cnt;
put2(name->on_desc,c); c += 2; cnt -= i;
put4(name->on_valu,c); c += 4; __parts[PARTNAME].cnt -= (i*SZ_NAME);
while (i--) {
put4(name->on_foff, c); c += 4;
put2(name->on_type, c); c += 2;
put2(name->on_desc, c); c += 2;
put4(name->on_valu, c); c += 4;
name++; name++;
i++;
if (i == 100 || !cnt) {
c = buf;
l = (long) (i * SZ_NAME);
OUTWRITE(PARTNAME, c, l);
i = 0;
}
} }
__parts[PARTNAME].pnow = c;
if (cnt) __wr_flush(&__parts[PARTNAME]);
} }
#if ! (BYTES_REVERSED || WORDS_REVERSED) #if ! (BYTES_REVERSED || WORDS_REVERSED)
else { else {
l = (long)cnt * SZ_NAME; OUTWRITE(PARTNAME, (char *) name, (long)cnt * SZ_NAME);
OUTWRITE(PARTNAME, (char *) name, l);
} }
#endif #endif

View file

@ -3,18 +3,9 @@
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
* See the copyright notice in the ACK home directory, in the file "Copyright". * See the copyright notice in the ACK home directory, in the file "Copyright".
*/ */
#include <stdio.h> #include "object.h"
static char buf[BUFSIZ];
extern char *_pbuf;
extern int _ocnt;
wr_putc(ch) wr_putc(ch)
{ {
_pbuf = buf; OUTBYTE(PARTEMIT, ch);
buf[_ocnt++] = ch;
if (_ocnt == BUFSIZ) {
wr_flush();
}
} }