compiler is fast now
This commit is contained in:
parent
65513c6037
commit
fb2e0c5f6c
105
compiler/libpc300/memfile.c
Normal file
105
compiler/libpc300/memfile.c
Normal file
@ -0,0 +1,105 @@
|
||||
#include "memfile.h"
|
||||
#include <string.h>
|
||||
#include "osdefs.h"
|
||||
|
||||
memfile_t *memfile_creat(const char *name, size_t init)
|
||||
{
|
||||
memfile_t mf;
|
||||
memfile_t *pmf;
|
||||
|
||||
mf.size = init;
|
||||
mf.base = (char *)malloc(init);
|
||||
mf.usedoffs = 0;
|
||||
if (!mf.base)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
mf.offs = 0;
|
||||
mf._static = 0;
|
||||
|
||||
pmf = (memfile_t *)malloc(sizeof(memfile_t));
|
||||
memcpy(pmf, &mf, sizeof(memfile_t));
|
||||
|
||||
pmf->name = strdup(name);
|
||||
|
||||
return pmf;
|
||||
}
|
||||
|
||||
void memfile_destroy(memfile_t *mf)
|
||||
{
|
||||
if (!mf->_static)
|
||||
{
|
||||
free(mf->name);
|
||||
free(mf->base);
|
||||
free(mf);
|
||||
}
|
||||
}
|
||||
|
||||
void memfile_seek(memfile_t *mf, long seek)
|
||||
{
|
||||
mf->offs = seek;
|
||||
}
|
||||
|
||||
long memfile_tell(memfile_t *mf)
|
||||
{
|
||||
return mf->offs;
|
||||
}
|
||||
|
||||
size_t memfile_read(memfile_t *mf, void *buffer, size_t maxsize)
|
||||
{
|
||||
if (!maxsize || mf->offs >= mf->usedoffs)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (mf->usedoffs - mf->offs < (long)maxsize)
|
||||
{
|
||||
maxsize = mf->usedoffs - mf->offs;
|
||||
if (!maxsize)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
memcpy(buffer, mf->base + mf->offs, maxsize);
|
||||
|
||||
mf->offs += maxsize;
|
||||
|
||||
return maxsize;
|
||||
}
|
||||
|
||||
int memfile_write(memfile_t *mf, void *buffer, size_t size)
|
||||
{
|
||||
if (mf->offs + size > mf->size)
|
||||
{
|
||||
size_t newsize = (mf->size + size) * 2;
|
||||
if (mf->_static)
|
||||
{
|
||||
char *oldbase = mf->base;
|
||||
mf->base = (char *)malloc(newsize);
|
||||
if (!mf->base)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
memcpy(mf->base, oldbase, mf->size);
|
||||
} else {
|
||||
mf->base = (char *)realloc(mf->base, newsize);
|
||||
if (!mf->base)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
mf->_static = 0;
|
||||
mf->size = newsize;
|
||||
}
|
||||
memcpy(mf->base + mf->offs, buffer, size);
|
||||
mf->offs += size;
|
||||
|
||||
if (mf->offs > mf->usedoffs)
|
||||
{
|
||||
mf->usedoffs = mf->offs;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
23
compiler/libpc300/memfile.h
Normal file
23
compiler/libpc300/memfile.h
Normal file
@ -0,0 +1,23 @@
|
||||
#ifndef _INCLUDE_MEMFILE_H
|
||||
#define _INCLUDE_MEMFILE_H
|
||||
|
||||
#include <malloc.h>
|
||||
|
||||
typedef struct memfile_s
|
||||
{
|
||||
char *name;
|
||||
char *base;
|
||||
long offs;
|
||||
long usedoffs;
|
||||
size_t size;
|
||||
int _static;
|
||||
} memfile_t;
|
||||
|
||||
memfile_t *memfile_creat(const char *name, size_t init);
|
||||
void memfile_destroy(memfile_t *mf);
|
||||
void memfile_seek(memfile_t *mf, long seek);
|
||||
int memfile_write(memfile_t *mf, void *buffer, size_t size);
|
||||
size_t memfile_read(memfile_t *mf, void *buffer, size_t maxsize);
|
||||
long memfile_tell(memfile_t *mf);
|
||||
|
||||
#endif //_INCLUDE_MEMFILE_H
|
@ -29,6 +29,7 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "memfile.h"
|
||||
|
||||
#if defined FORTIFY
|
||||
#include "fortify.h"
|
||||
@ -44,11 +45,7 @@
|
||||
* buffer points to the "file name"
|
||||
* bufpos is the current "file pointer"
|
||||
*/
|
||||
typedef struct tagMEMFILE {
|
||||
struct tagMEMFILE *next;
|
||||
unsigned char *buffer;
|
||||
long bufpos;
|
||||
} MEMFILE;
|
||||
typedef memfile_t MEMFILE;
|
||||
#define tMEMFILE 1
|
||||
|
||||
#include "sc.h"
|
||||
@ -56,33 +53,12 @@ typedef struct tagMEMFILE {
|
||||
|
||||
MEMFILE *mfcreate(char *filename)
|
||||
{
|
||||
MEMFILE *mf;
|
||||
|
||||
/* create a first block that only holds the name */
|
||||
mf=(MEMFILE*)malloc(sizeof(MEMFILE));
|
||||
if (mf==NULL)
|
||||
return NULL;
|
||||
memset(mf,0,sizeof(MEMFILE));
|
||||
mf->buffer=(unsigned char*)strdup(filename);
|
||||
if (mf->buffer==NULL) {
|
||||
free(mf);
|
||||
return NULL;
|
||||
} /* if */
|
||||
return mf;
|
||||
return memfile_creat(filename, 4096);
|
||||
}
|
||||
|
||||
void mfclose(MEMFILE *mf)
|
||||
{
|
||||
MEMFILE *next;
|
||||
|
||||
assert(mf!=NULL);
|
||||
while (mf!=NULL) {
|
||||
next=mf->next;
|
||||
assert(mf->buffer!=NULL);
|
||||
free(mf->buffer);
|
||||
free(mf);
|
||||
mf=next;
|
||||
} /* while */
|
||||
memfile_destroy(mf);
|
||||
}
|
||||
|
||||
int mfdump(MEMFILE *mf)
|
||||
@ -92,19 +68,12 @@ int mfdump(MEMFILE *mf)
|
||||
|
||||
assert(mf!=NULL);
|
||||
/* create the file */
|
||||
fp=fopen((char*)mf->buffer,"wb");
|
||||
fp=fopen(mf->name, "wb");
|
||||
if (fp==NULL)
|
||||
return 0;
|
||||
|
||||
okay=1;
|
||||
mf=mf->next;
|
||||
while (mf!=NULL) {
|
||||
assert(mf->buffer!=NULL);
|
||||
/* all blocks except the last should be fully filled */
|
||||
assert(mf->next==NULL || (unsigned long)mf->bufpos==BUFFERSIZE);
|
||||
okay=okay && fwrite(mf->buffer,1,(size_t)mf->bufpos,fp)==(size_t)mf->bufpos;
|
||||
mf=mf->next;
|
||||
} /* while */
|
||||
okay = okay & (fwrite(mf->base, mf->usedoffs, 1, fp)==(size_t)mf->usedoffs);
|
||||
|
||||
fclose(fp);
|
||||
return okay;
|
||||
@ -112,19 +81,7 @@ int mfdump(MEMFILE *mf)
|
||||
|
||||
long mflength(MEMFILE *mf)
|
||||
{
|
||||
long length;
|
||||
|
||||
assert(mf!=NULL);
|
||||
/* find the size of the memory file */
|
||||
length=0L;
|
||||
mf=mf->next; /* skip initial block */
|
||||
while (mf!=NULL) {
|
||||
assert(mf->next==NULL || (unsigned long)mf->bufpos==BUFFERSIZE);
|
||||
length+=mf->bufpos;
|
||||
mf=mf->next;
|
||||
} /* while */
|
||||
|
||||
return length;
|
||||
return mf->usedoffs;
|
||||
}
|
||||
|
||||
long mfseek(MEMFILE *mf,long offset,int whence)
|
||||
@ -132,7 +89,7 @@ long mfseek(MEMFILE *mf,long offset,int whence)
|
||||
long length;
|
||||
|
||||
assert(mf!=NULL);
|
||||
if (mf->next==NULL)
|
||||
if (mf->usedoffs == 0)
|
||||
return 0L; /* early exit: not a single byte in the file */
|
||||
|
||||
/* find the size of the memory file */
|
||||
@ -143,7 +100,7 @@ long mfseek(MEMFILE *mf,long offset,int whence)
|
||||
case SEEK_SET:
|
||||
break;
|
||||
case SEEK_CUR:
|
||||
offset+=mf->bufpos;
|
||||
offset+=mf->offs;
|
||||
break;
|
||||
case SEEK_END:
|
||||
assert(offset<=0);
|
||||
@ -158,136 +115,18 @@ long mfseek(MEMFILE *mf,long offset,int whence)
|
||||
offset=length;
|
||||
|
||||
/* set new position and return it */
|
||||
mf->bufpos=offset;
|
||||
memfile_seek(mf, offset);
|
||||
return offset;
|
||||
}
|
||||
|
||||
unsigned int mfwrite(MEMFILE *mf,unsigned char *buffer,unsigned int size)
|
||||
{
|
||||
long length;
|
||||
long numblocks;
|
||||
int blockpos,blocksize;
|
||||
unsigned int bytes;
|
||||
MEMFILE *block;
|
||||
|
||||
assert(mf!=NULL);
|
||||
|
||||
/* see whether more memory must be allocated */
|
||||
length=mflength(mf);
|
||||
assert(mf->bufpos>=0 && mf->bufpos<=length);
|
||||
numblocks=(length+BUFFERSIZE-1)/BUFFERSIZE; /* # allocated blocks */
|
||||
while (mf->bufpos+size>numblocks*BUFFERSIZE) {
|
||||
/* append a block */
|
||||
MEMFILE *last;
|
||||
block=(MEMFILE*)malloc(sizeof(MEMFILE));
|
||||
if (block==NULL)
|
||||
return 0;
|
||||
memset(block,0,sizeof(MEMFILE));
|
||||
block->buffer=(unsigned char*)malloc(BUFFERSIZE);
|
||||
if (block->buffer==NULL) {
|
||||
free(block);
|
||||
return 0;
|
||||
} /* if */
|
||||
for (last=mf; last->next!=NULL; last=last->next)
|
||||
/* nothing */;
|
||||
assert(last!=NULL);
|
||||
assert(last->next==NULL);
|
||||
last->next=block;
|
||||
numblocks++;
|
||||
} /* while */
|
||||
|
||||
if (size==0)
|
||||
return 0;
|
||||
|
||||
/* find the block to start writing to */
|
||||
numblocks=mf->bufpos/BUFFERSIZE; /* # blocks to skip */
|
||||
block=mf->next;
|
||||
while (numblocks-->0) {
|
||||
assert(block!=NULL);
|
||||
block=block->next;
|
||||
} /* while */
|
||||
assert(block!=NULL);
|
||||
|
||||
/* copy into memory */
|
||||
bytes=0;
|
||||
blockpos=(int)(mf->bufpos % BUFFERSIZE);
|
||||
do {
|
||||
blocksize=BUFFERSIZE-blockpos;
|
||||
assert(blocksize>=0);
|
||||
if ((unsigned int)blocksize>size)
|
||||
blocksize=size;
|
||||
|
||||
assert(block!=NULL);
|
||||
memcpy(block->buffer+blockpos,buffer,blocksize);
|
||||
buffer+=blocksize;
|
||||
size-=blocksize;
|
||||
bytes+=blocksize;
|
||||
|
||||
if (blockpos+blocksize>block->bufpos)
|
||||
block->bufpos=blockpos+blocksize;
|
||||
assert(block->bufpos>=0 && (unsigned long)block->bufpos<=BUFFERSIZE);
|
||||
block=block->next;
|
||||
blockpos=0;
|
||||
} while (size>0);
|
||||
|
||||
/* adjust file pointer */
|
||||
mf->bufpos+=bytes;
|
||||
|
||||
return bytes;
|
||||
return (memfile_write(mf, buffer, size) ? size : 0);
|
||||
}
|
||||
|
||||
unsigned int mfread(MEMFILE *mf,unsigned char *buffer,unsigned int size)
|
||||
{
|
||||
long length;
|
||||
long numblocks;
|
||||
int blockpos,blocksize;
|
||||
unsigned int bytes;
|
||||
MEMFILE *block;
|
||||
|
||||
assert(mf!=NULL);
|
||||
|
||||
/* adjust the size to read */
|
||||
length=mflength(mf);
|
||||
assert(mf->bufpos>=0 && mf->bufpos<=length);
|
||||
if (mf->bufpos+size>(unsigned long)length)
|
||||
size=(int)(length-mf->bufpos);
|
||||
assert(mf->bufpos+size<=(unsigned long)length);
|
||||
if (size==0)
|
||||
return 0;
|
||||
|
||||
/* find the block to start reading from */
|
||||
numblocks=mf->bufpos/BUFFERSIZE; /* # blocks to skip */
|
||||
block=mf->next;
|
||||
while (numblocks-->0) {
|
||||
assert(block!=NULL);
|
||||
block=block->next;
|
||||
} /* while */
|
||||
assert(block!=NULL);
|
||||
|
||||
/* copy out of memory */
|
||||
bytes=0;
|
||||
blockpos=(int)(mf->bufpos % BUFFERSIZE);
|
||||
do {
|
||||
blocksize=BUFFERSIZE-blockpos;
|
||||
if ((unsigned int)blocksize>size)
|
||||
blocksize=size;
|
||||
|
||||
assert(block!=NULL);
|
||||
assert(block->bufpos>=0 && (unsigned long)block->bufpos<=BUFFERSIZE);
|
||||
assert(blockpos+blocksize<=block->bufpos);
|
||||
memcpy(buffer,block->buffer+blockpos,blocksize);
|
||||
buffer+=blocksize;
|
||||
size-=blocksize;
|
||||
bytes+=blocksize;
|
||||
|
||||
block=block->next;
|
||||
blockpos=0;
|
||||
} while (size>0);
|
||||
|
||||
/* adjust file pointer */
|
||||
mf->bufpos+=bytes;
|
||||
|
||||
return bytes;
|
||||
return memfile_read(mf, buffer, size);
|
||||
}
|
||||
|
||||
char *mfgets(MEMFILE *mf,char *string,unsigned int size)
|
||||
|
Loading…
Reference in New Issue
Block a user