*** empty log message ***

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@1010 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Dave Beazley 2001-01-22 13:38:48 +00:00
commit cd2f18170d
12 changed files with 111 additions and 207 deletions

View file

@ -14,173 +14,84 @@
#include "wad.h"
#include <ar.h>
/* Maximum number of files that can be loaded at once */
#define WAD_MAX_FILE 32
typedef struct WadFile {
int refcnt;
void *addr;
int size;
char path[MAX_PATH];
void *addr; /* Base address of the file */
int size; /* Size in bytes */
char *path; /* Path name */
struct WadFile *next; /* Next file */
} WadFile;
static WadFile wad_files[WAD_MAX_FILE]; /* Array of file objects */
static WadFile *wad_files = 0; /* Linked list of loaded files */
/* private function to manage the loading of raw files into memory */
static WadFile *
load_file(const char *path) {
int i;
int fd;
WadFile *firstfree = 0;
WadFile *firstreplace = 0;
WadFile *wf = wad_files;
if (wad_debug_mode & DEBUG_FILE) {
printf("wad: Loading file '%s' ... ", path);
wad_printf("wad: Loading file '%s' ... ", path);
}
/* Walk through the file list to see if we already know about the file */
for (i = 0; i < WAD_MAX_FILE; i++, wf++) {
if (strcmp(wf->path, path) == 0) {
wf->refcnt++;
if (wad_debug_mode & DEBUG_FILE) {
printf("cached.\n");
}
while (wf) {
if (strcmp(wf->path,path) == 0) {
if (wad_debug_mode & DEBUG_FILE) wad_printf("cached.\n");
return wf;
}
if (wf->refcnt <= 0) {
if (wf->path[0] == 0) firstfree = wf;
else firstreplace = wf;
}
wf = wf->next;
}
if (!firstfree && !firstreplace) {
if (wad_debug_mode & DEBUG_FILE) printf("out of memory!\n");
return 0; /* Out of space */
}
if (!firstfree) firstfree = firstreplace;
fd = open(path, O_RDONLY);
if (fd < 0) {
if (wad_debug_mode & DEBUG_FILE) printf("not found!\n");
if (wad_debug_mode & DEBUG_FILE) wad_printf("not found!\n");
return 0; /* Doesn't exist. Oh well */
}
if (wad_debug_mode & DEBUG_FILE) printf("loaded.\n");
/* If already mapped, unmap the file */
if (firstfree->addr) {
if (wad_debug_mode & DEBUG_FILE)
printf("wad: Unloading file '%s'\n", firstfree->path);
munmap(firstfree->addr,firstfree->size);
}
firstfree->refcnt = 1;
strncpy(firstfree->path,path,MAX_PATH);
if (wad_debug_mode & DEBUG_FILE) wad_printf("loaded.\n");
wf = (WadFile *) wad_malloc(sizeof(WadFile));
wf->path = wad_strdup(path);
/* Get file length */
firstfree->size = lseek(fd,0,SEEK_END);
wf->size = lseek(fd,0,SEEK_END);
lseek(fd,0,SEEK_SET);
/* Try to mmap the file */
firstfree->addr = mmap(NULL,firstfree->size, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE, fd, 0);
wf->addr = mmap(NULL,wf->size, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE, fd, 0);
close(fd);
if (firstfree->addr == MAP_FAILED) {
if (wad_debug_mode & DEBUG_FILE) printf("wad: Couldn't mmap '%s'\n", path);
firstfree->refcnt = 0;
firstfree->path[0] = 0;
firstfree->addr = 0;
firstfree->size = 0;
if (wf->addr == MAP_FAILED) {
if (wad_debug_mode & DEBUG_FILE) wad_printf("wad: Couldn't mmap '%s'\n", path);
return 0;
}
return firstfree;
wf->next = wad_files;
wad_files = wf;
return wf;
}
static void
release_file(WadFile *f) {
if (wad_debug_mode & DEBUG_FILE) printf("wad: Releasing file '%s'\n", f->path);
f->refcnt--;
}
#define WAD_MAX_OBJECT 32
static WadObjectFile wad_objects[WAD_MAX_OBJECT]; /* Object file descriptor table */
static int wad_obj_free[WAD_MAX_OBJECT]; /* Free object stack */
static int wad_obj_nfree = 0; /* Num free object descriptors */
/* -----------------------------------------------------------------------------
* wad_object_init()
*
* Initialize the object file system
* ----------------------------------------------------------------------------- */
void wad_object_init() {
int i;
for (i = 0; i < WAD_MAX_FILE; i++) {
wad_files[i].refcnt = 0;
wad_files[i].addr = 0;
wad_files[i].size = 0;
wad_files[i].path[0] = 0;
}
wad_obj_nfree = 0;
for (i = 0; i < WAD_MAX_OBJECT; i++) {
wad_objects[i].ptr = 0;
wad_objects[i].len = 0;
wad_objects[i].refcnt = 0;
wad_objects[i].path[0] = 0;
wad_obj_free[i] = i;
wad_obj_nfree++;
}
}
static WadObjectFile *wad_objects = 0; /* Linked list of object files */
/* -----------------------------------------------------------------------------
* wad_object_cleanup()
*
* Release all files loaded during the debugging
* Reset the object file loader
* ----------------------------------------------------------------------------- */
void
wad_object_cleanup() {
wad_object_reset() {
int i;
WadFile *f = wad_files;
if (wad_debug_mode & DEBUG_OBJECT) {
printf("wad: Releasing all files.\n");
wad_printf("wad: Releasing all files.\n");
}
for (i = 0; i < WAD_MAX_FILE; i++, f++) {
/* Unmap all of the loaded files */
while (f) {
if (f->addr) {
munmap(f->addr, f->size);
}
f->addr = 0;
f->size = 0;
f->path[0] = 0;
f->refcnt = 0;
f = f->next;
}
}
/* -----------------------------------------------------------------------------
* wad_object_release()
*
* Done with the object.
* ----------------------------------------------------------------------------- */
void
wad_object_release(WadObjectFile *wo) {
int n;
if (!wo) return;
wo->refcnt--;
if (wo->refcnt > 0) return;
if (wad_debug_mode & DEBUG_OBJECT) {
printf("wad: Releasing object '%s'\n", wo->path);
}
release_file(wo->file);
wo->file = 0;
wo->ptr = 0;
wo->len = 0;
wo->path[0] = 0;
n = wo - wad_objects;
wad_obj_free[wad_obj_nfree++] = n;
/* Reset the linked lists */
wad_files = 0;
wad_objects = 0;
}
/* -----------------------------------------------------------------------------
@ -198,21 +109,12 @@ wad_object_load(const char *path) {
WadObjectFile *wad_arobject_load(const char *path, const char *name);
if (wad_debug_mode & DEBUG_OBJECT) {
printf("wad: Loading object '%s'\n", path);
wad_printf("wad: Loading object '%s'\n", path);
}
/* See if the path has already been loaded */
for (i = 0; i < WAD_MAX_OBJECT; i++) {
if (strcmp(wad_objects[i].path,path) == 0) {
wo = &wad_objects[i];
wo->refcnt++;
return wo;
}
}
if (wad_obj_nfree == 0) {
if (wad_debug_mode & DEBUG_OBJECT)
printf("wad: No more space in wad_object_load()\n");
return 0;
for (wo = wad_objects; wo; wo=wo->next) {
if (strcmp(wo->path,path) == 0) return wo;
}
/* Didn't find it. Now we need to go load some files */
/* If this is an archive reference like /path/libfoo.a(blah.o), we need to
split up the name a little bit */
@ -232,7 +134,8 @@ wad_object_load(const char *path) {
/* Okay, I'm going to attempt to map this as a library file */
wo = wad_arobject_load(realfile,objfile);
if (wo) {
strncpy(wo->path, path, MAX_PATH);
/* Reset the path */
wo->path = wad_strdup(path);
return wo;
}
}
@ -240,12 +143,9 @@ wad_object_load(const char *path) {
wf = load_file(path);
if (!wf) return 0;
wo = wad_objects + wad_obj_free[wad_obj_nfree-1];
wad_obj_nfree--;
wo = (WadObjectFile *) wad_malloc(sizeof(WadObjectFile));
wo->file = wf;
wo->refcnt = 1;
strncpy(wo->path,path, MAX_PATH);
wo->path = wad_strdup(path);
wo->ptr = wf->addr;
wo->len = wf->size;
return wo;
@ -283,18 +183,10 @@ wad_arobject_load(const char *arpath, const char *robjname) {
arptr = (char *) wf->addr;
arlen = wf->size;
nf = wad_obj_free[wad_obj_nfree-1];
wo = wad_objects + nf;
wad_obj_nfree--;
wo->refcnt = 1;
wo->ptr = 0;
/* Now take a look at the archive */
if (strncmp(arptr,ARMAG,SARMAG) == 0) {
/* printf("Got an archive\n"); */
} else {
/* Not an archive file */
release_file(wf);
return 0;
}
@ -318,7 +210,6 @@ wad_arobject_load(const char *arpath, const char *robjname) {
soff = atoi(ah->ar_name+1);
if (!strtab) {
/* No offset table */
release_file(wf);
return 0;
}
e = strchr(strtab+soff,'\n');
@ -336,18 +227,15 @@ wad_arobject_load(const char *arpath, const char *robjname) {
/* Compare the names */
if (strncmp(mname,objname,sobjname) == 0) {
/* Found the archive */
wo = wad_objects + wad_obj_free[wad_obj_nfree-1];
wad_obj_nfree--;
wo = (WadObjectFile *) wad_malloc(sizeof(WadObjectFile));
wo->file = wf;
wo->refcnt = 1;
wo->ptr = (void *) (arptr + offset);
wo->len = msize;
wo->path = 0;
return wo;
}
offset += msize;
}
release_file(wf);
return 0;
}