Bennene posix.c zu hunk.c um

This commit is contained in:
Yamagi Burmeister 2010-10-18 13:19:17 +00:00
parent 1ad221b667
commit 9a750b4e4e
4 changed files with 345 additions and 341 deletions

View File

@ -254,7 +254,7 @@ SERVER_OBJS = \
# POSIX platform objects
POSIX_OBJS = \
build/posix/network.o \
build/posix/posix.o \
build/posix/hunk.o \
build/posix/system.o \
build/posix/glob/glob.o \
build/posix/vid/menu.o \
@ -314,7 +314,7 @@ DEDICATED_SERVER_COMMON_OBJS = \
DEDICATED_SERVER_POSIX_OBJS = \
build/dedicated_server_posix/glob/glob.o \
build/dedicated_server_posix/network.o \
build/dedicated_server_posix/posix.o \
build/dedicated_server_posix/hunk.o \
build/dedicated_server_posix/system.o
# ----------
@ -343,7 +343,7 @@ OPENGL_GAME_OBJS = \
OPENGL_POSIX_OBJS = \
build/ref_gl_posix/abi.o \
build/ref_gl_posix/glob.o \
build/ref_gl_posix/posix.o \
build/ref_gl_posix/hunk.o \
build/ref_gl_posix/qgl.o \
build/ref_gl_posix/refresh.o
@ -620,7 +620,7 @@ build/server/sv_world.o : src/server/sv_world.c
build/posix/network.o : src/posix/network.c
$(CC) $(CFLAGS_CLIENT) -o $@ -c $<
build/posix/posix.o : src/posix/posix.c
build/posix/hunk.o : src/posix/hunk.c
$(CC) $(CFLAGS_CLIENT) -o $@ -c $<
build/posix/system.o : src/posix/system.c
@ -750,7 +750,7 @@ build/dedicated_server_posix/glob/glob.o : src/posix/glob/glob.c
build/dedicated_server_posix/network.o : src/posix/network.c
$(CC) $(CFLAGS_DEDICATED_SERVER) -o $@ -c $<
build/dedicated_server_posix/posix.o : src/posix/posix.c
build/dedicated_server_posix/hunk.o : src/posix/hunk.c
$(CC) $(CFLAGS_DEDICATED_SERVER) -o $@ -c $<
build/dedicated_server_posix/system.o : src/posix/system.c
@ -800,7 +800,7 @@ build/ref_gl_posix/abi.o: src/posix/refresh/abi.c
build/ref_gl_posix/glob.o: src/posix/glob/glob.c
$(CC) $(CFLAGS_OPENGL) -o $@ -c $<
build/ref_gl_posix/posix.o: src/posix/posix.c
build/ref_gl_posix/hunk.o: src/posix/hunk.c
$(CC) $(CFLAGS_OPENGL) -o $@ -c $<
build/ref_gl_posix/qgl.o: src/posix/refresh/qgl.c

142
src/posix/hunk.c Normal file
View File

@ -0,0 +1,142 @@
/*
* Copyright (C) 1997-2001 Id Software, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*
* =======================================================================
*
* This file implements the low level part of the Hunk_* memory system
*
* =======================================================================
*/
#include <errno.h>
#include <sys/mman.h>
#include <sys/time.h>
#include "../common/header/common.h"
#if defined( __FreeBSD__ )
#include <machine/param.h>
#define MAP_ANONYMOUS MAP_ANON
#endif
/* For mremap() */
#if defined( __linux__ )
#define __USE_GNU
#endif
byte *membase;
int maxhunksize;
int curhunksize;
void *
Hunk_Begin ( int maxsize )
{
/* reserve a huge chunk of memory, but don't commit any yet */
maxhunksize = maxsize + sizeof ( int );
curhunksize = 0;
membase = mmap( 0, maxhunksize, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0 );
if ( ( membase == NULL ) || ( membase == (byte *) -1 ) )
{
Sys_Error( "unable to virtual allocate %d bytes", maxsize );
}
*( (int *) membase ) = curhunksize;
return ( membase + sizeof ( int ) );
}
void *
Hunk_Alloc ( int size )
{
byte *buf;
/* round to cacheline */
size = ( size + 31 ) & ~31;
if ( curhunksize + size > maxhunksize )
{
Sys_Error( "Hunk_Alloc overflow" );
}
buf = membase + sizeof ( int ) + curhunksize;
curhunksize += size;
return ( buf );
}
int
Hunk_End ( void )
{
byte *n = NULL;
#if defined( __FreeBSD__ )
size_t old_size = maxhunksize;
size_t new_size = curhunksize + sizeof ( int );
void *unmap_base;
size_t unmap_len;
new_size = round_page( new_size );
old_size = round_page( old_size );
if ( new_size > old_size )
{
n = 0; /* error */
}
else if ( new_size < old_size )
{
unmap_base = (caddr_t) ( membase + new_size );
unmap_len = old_size - new_size;
n = munmap( unmap_base, unmap_len ) + membase;
}
#endif
#if defined( __linux__ )
n = mremap( membase, maxhunksize, curhunksize + sizeof ( int ), 0 );
#endif
if ( n != membase )
{
Sys_Error( "Hunk_End: Could not remap virtual block (%d)", errno );
}
*( (int *) membase ) = curhunksize + sizeof ( int );
return ( curhunksize );
}
void
Hunk_Free ( void *base )
{
byte *m;
if ( base )
{
m = ( (byte *) base ) - sizeof ( int );
if ( munmap( m, *( (int *) m ) ) )
{
Sys_Error( "Hunk_Free: munmap failed (%d)", errno );
}
}
}

View File

@ -1,325 +0,0 @@
/*
* Copyright (C) 1997-2001 Id Software, Inc.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdio.h>
#include <ctype.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/time.h>
#include "glob/glob.h"
#include "../common/header/common.h"
#if defined( __FreeBSD__ )
#include <machine/param.h>
#define MAP_ANONYMOUS MAP_ANON
#endif
/* For mremap() */
#if defined( __linux__ )
#define __USE_GNU
#endif
byte *membase;
int maxhunksize;
int curhunksize;
void *
Hunk_Begin ( int maxsize )
{
/* reserve a huge chunk of memory, but don't commit any yet */
maxhunksize = maxsize + sizeof ( int );
curhunksize = 0;
membase = mmap( 0, maxhunksize, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0 );
if ( ( membase == NULL ) || ( membase == (byte *) -1 ) )
{
Sys_Error( "unable to virtual allocate %d bytes", maxsize );
}
*( (int *) membase ) = curhunksize;
return ( membase + sizeof ( int ) );
}
void *
Hunk_Alloc ( int size )
{
byte *buf;
/* round to cacheline */
size = ( size + 31 ) & ~31;
if ( curhunksize + size > maxhunksize )
{
Sys_Error( "Hunk_Alloc overflow" );
}
buf = membase + sizeof ( int ) + curhunksize;
curhunksize += size;
return ( buf );
}
int
Hunk_End ( void )
{
byte *n = NULL;
#if defined( __FreeBSD__ )
size_t old_size = maxhunksize;
size_t new_size = curhunksize + sizeof ( int );
void *unmap_base;
size_t unmap_len;
new_size = round_page( new_size );
old_size = round_page( old_size );
if ( new_size > old_size )
{
n = 0; /* error */
}
else if ( new_size < old_size )
{
unmap_base = (caddr_t) ( membase + new_size );
unmap_len = old_size - new_size;
n = munmap( unmap_base, unmap_len ) + membase;
}
#endif
#if defined( __linux__ )
n = mremap( membase, maxhunksize, curhunksize + sizeof ( int ), 0 );
#endif
if ( n != membase )
{
Sys_Error( "Hunk_End: Could not remap virtual block (%d)", errno );
}
*( (int *) membase ) = curhunksize + sizeof ( int );
return ( curhunksize );
}
void
Hunk_Free ( void *base )
{
byte *m;
if ( base )
{
m = ( (byte *) base ) - sizeof ( int );
if ( munmap( m, *( (int *) m ) ) )
{
Sys_Error( "Hunk_Free: munmap failed (%d)", errno );
}
}
}
int curtime;
int
Sys_Milliseconds ( void )
{
struct timeval tp;
struct timezone tzp;
static int secbase;
gettimeofday( &tp, &tzp );
if ( !secbase )
{
secbase = tp.tv_sec;
return ( tp.tv_usec / 1000 );
}
curtime = ( tp.tv_sec - secbase ) * 1000 + tp.tv_usec / 1000;
return ( curtime );
}
void
Sys_Mkdir ( char *path )
{
mkdir( path, 0755 );
}
void
Sys_Rmdir ( char *path )
{
rmdir( path );
}
char *
Sys_GetCurrentDirectory ( void )
{
static char dir [ MAX_OSPATH ];
if ( !getcwd( dir, sizeof ( dir ) ) )
{
Sys_Error( "Couldn't get current working directory" );
}
return ( dir );
}
char *
strlwr ( char *s )
{
char *p = s;
while ( *s )
{
*s = tolower( *s );
s++;
}
return ( p );
}
static char findbase [ MAX_OSPATH ];
static char findpath [ MAX_OSPATH ];
static char findpattern [ MAX_OSPATH ];
static DIR *fdir;
static qboolean
CompareAttributes ( char *path, char *name, unsigned musthave, unsigned canthave )
{
struct stat st;
char fn [ MAX_OSPATH ];
/* . and .. never match */
if ( ( strcmp( name, "." ) == 0 ) || ( strcmp( name, ".." ) == 0 ) )
{
return ( false );
}
return ( true );
if ( stat( fn, &st ) == -1 )
{
return ( false ); /* shouldn't happen */
}
if ( ( st.st_mode & S_IFDIR ) && ( canthave & SFF_SUBDIR ) )
{
return ( false );
}
if ( ( musthave & SFF_SUBDIR ) && !( st.st_mode & S_IFDIR ) )
{
return ( false );
}
return ( true );
}
char *
Sys_FindFirst ( char *path, unsigned musthave, unsigned canhave )
{
struct dirent *d;
char *p;
if ( fdir )
{
Sys_Error( "Sys_BeginFind without close" );
}
strcpy( findbase, path );
if ( ( p = strrchr( findbase, '/' ) ) != NULL )
{
*p = 0;
strcpy( findpattern, p + 1 );
}
else
{
strcpy( findpattern, "*" );
}
if ( strcmp( findpattern, "*.*" ) == 0 )
{
strcpy( findpattern, "*" );
}
if ( ( fdir = opendir( findbase ) ) == NULL )
{
return ( NULL );
}
while ( ( d = readdir( fdir ) ) != NULL )
{
if ( !*findpattern || glob_match( findpattern, d->d_name ) )
{
if ( CompareAttributes( findbase, d->d_name, musthave, canhave ) )
{
sprintf( findpath, "%s/%s", findbase, d->d_name );
return ( findpath );
}
}
}
return ( NULL );
}
char *
Sys_FindNext ( unsigned musthave, unsigned canhave )
{
struct dirent *d;
if ( fdir == NULL )
{
return ( NULL );
}
while ( ( d = readdir( fdir ) ) != NULL )
{
if ( !*findpattern || glob_match( findpattern, d->d_name ) )
{
if ( CompareAttributes( findbase, d->d_name, musthave, canhave ) )
{
sprintf( findpath, "%s/%s", findbase, d->d_name );
return ( findpath );
}
}
}
return ( NULL );
}
void
Sys_FindClose ( void )
{
if ( fdir != NULL )
{
closedir( fdir );
}
fdir = NULL;
}

View File

@ -1,22 +1,29 @@
/*
* Copyright (C) 1997-2001 Id Software, Inc.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*
* =======================================================================
*
* This file implements all system dependend generic funktions
*
* =======================================================================
*/
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
@ -36,18 +43,201 @@
#include <sys/mman.h>
#include <errno.h>
#include <dlfcn.h>
#include <dirent.h>
#include "glob/glob.h"
#include "../common/header/common.h"
#include "posix.h"
unsigned sys_frame_time;
int curtime;
static void *game_library;
static char findbase [ MAX_OSPATH ];
static char findpath [ MAX_OSPATH ];
static char findpattern [ MAX_OSPATH ];
static DIR *fdir;
cvar_t *nostdout;
uid_t saved_euid;
qboolean stdin_active = true;
int
Sys_Milliseconds ( void )
{
struct timeval tp;
struct timezone tzp;
static int secbase;
gettimeofday( &tp, &tzp );
if ( !secbase )
{
secbase = tp.tv_sec;
return ( tp.tv_usec / 1000 );
}
curtime = ( tp.tv_sec - secbase ) * 1000 + tp.tv_usec / 1000;
return ( curtime );
}
void
Sys_Mkdir ( char *path )
{
mkdir( path, 0755 );
}
void
Sys_Rmdir ( char *path )
{
rmdir( path );
}
char *
Sys_GetCurrentDirectory ( void )
{
static char dir [ MAX_OSPATH ];
if ( !getcwd( dir, sizeof ( dir ) ) )
{
Sys_Error( "Couldn't get current working directory" );
}
return ( dir );
}
char *
strlwr ( char *s )
{
char *p = s;
while ( *s )
{
*s = tolower( *s );
s++;
}
return ( p );
}
static qboolean
CompareAttributes ( char *path, char *name, unsigned musthave, unsigned canthave )
{
struct stat st;
char fn [ MAX_OSPATH ];
/* . and .. never match */
if ( ( strcmp( name, "." ) == 0 ) || ( strcmp( name, ".." ) == 0 ) )
{
return ( false );
}
return ( true );
if ( stat( fn, &st ) == -1 )
{
return ( false ); /* shouldn't happen */
}
if ( ( st.st_mode & S_IFDIR ) && ( canthave & SFF_SUBDIR ) )
{
return ( false );
}
if ( ( musthave & SFF_SUBDIR ) && !( st.st_mode & S_IFDIR ) )
{
return ( false );
}
return ( true );
}
char *
Sys_FindFirst ( char *path, unsigned musthave, unsigned canhave )
{
struct dirent *d;
char *p;
if ( fdir )
{
Sys_Error( "Sys_BeginFind without close" );
}
strcpy( findbase, path );
if ( ( p = strrchr( findbase, '/' ) ) != NULL )
{
*p = 0;
strcpy( findpattern, p + 1 );
}
else
{
strcpy( findpattern, "*" );
}
if ( strcmp( findpattern, "*.*" ) == 0 )
{
strcpy( findpattern, "*" );
}
if ( ( fdir = opendir( findbase ) ) == NULL )
{
return ( NULL );
}
while ( ( d = readdir( fdir ) ) != NULL )
{
if ( !*findpattern || glob_match( findpattern, d->d_name ) )
{
if ( CompareAttributes( findbase, d->d_name, musthave, canhave ) )
{
sprintf( findpath, "%s/%s", findbase, d->d_name );
return ( findpath );
}
}
}
return ( NULL );
}
char *
Sys_FindNext ( unsigned musthave, unsigned canhave )
{
struct dirent *d;
if ( fdir == NULL )
{
return ( NULL );
}
while ( ( d = readdir( fdir ) ) != NULL )
{
if ( !*findpattern || glob_match( findpattern, d->d_name ) )
{
if ( CompareAttributes( findbase, d->d_name, musthave, canhave ) )
{
sprintf( findpath, "%s/%s", findbase, d->d_name );
return ( findpath );
}
}
}
return ( NULL );
}
void
Sys_FindClose ( void )
{
if ( fdir != NULL )
{
closedir( fdir );
}
fdir = NULL;
}
void
Sys_ConsoleOutput ( char *string )
{
@ -203,7 +393,6 @@ Sys_ConsoleInput ( void )
return ( text );
}
void
Sys_UnloadGame ( void )
{
@ -319,8 +508,6 @@ Sys_SendKeyEvents ( void )
sys_frame_time = Sys_Milliseconds();
}
/*****************************************************************************/
int
main ( int argc, char **argv )
{