@ -11,46 +11,6 @@
# include <sched.h>
# include <sched.h>
# endif // __linux__
# endif // __linux__
void * read_file ( const char * path , size_t * out_len ) {
FILE * f = fopen ( path , " r " ) ;
if ( ! f ) {
return NULL ;
}
fseek ( f , 0 , SEEK_END ) ;
long f_len = ftell ( f ) ;
rewind ( f ) ;
// malloc one extra byte so the file will always be NULL terminated
// cl_cached_program_from_file relies on this
char * buf = ( char * ) malloc ( f_len + 1 ) ;
assert ( buf ) ;
size_t num_read = fread ( buf , f_len , 1 , f ) ;
fclose ( f ) ;
if ( num_read ! = 1 ) {
free ( buf ) ;
return NULL ;
}
buf [ f_len ] = ' \0 ' ;
if ( out_len ) {
* out_len = f_len ;
}
return buf ;
}
int write_file ( const char * path , const void * data , size_t size , int flags , mode_t mode ) {
int fd = open ( path , flags , mode ) ;
if ( fd = = - 1 ) {
return - 1 ;
}
ssize_t n = write ( fd , data , size ) ;
close ( fd ) ;
return ( n > = 0 & & ( size_t ) n = = size ) ? 0 : - 1 ;
}
void set_thread_name ( const char * name ) {
void set_thread_name ( const char * name ) {
# ifdef __linux__
# ifdef __linux__
// pthread_setname_np is dumb (fails instead of truncates)
// pthread_setname_np is dumb (fails instead of truncates)
@ -84,3 +44,30 @@ int set_core_affinity(int core) {
return - 1 ;
return - 1 ;
# endif
# endif
}
}
namespace util {
std : : string read_file ( const std : : string & fn ) {
std : : ifstream ifs ( fn , std : : ios : : binary | std : : ios : : ate ) ;
if ( ifs ) {
std : : ifstream : : pos_type pos = ifs . tellg ( ) ;
std : : string result ;
result . resize ( pos ) ;
ifs . seekg ( 0 , std : : ios : : beg ) ;
ifs . read ( result . data ( ) , pos ) ;
if ( ifs ) return result ;
}
return " " ;
}
int write_file ( const char * path , const void * data , size_t size , int flags , mode_t mode ) {
int fd = open ( path , flags , mode ) ;
if ( fd = = - 1 ) {
return - 1 ;
}
ssize_t n = write ( fd , data , size ) ;
close ( fd ) ;
return ( n > = 0 & & ( size_t ) n = = size ) ? 0 : - 1 ;
}
} // namespace util