compiler is fast now

This commit is contained in:
Borja Ferrer
2006-07-16 02:25:32 +00:00
parent 65513c6037
commit fb2e0c5f6c
3 changed files with 140 additions and 173 deletions

View File

@ -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)