Last active
August 28, 2017 15:12
-
-
Save swt02026/d6bafca53b375e62d807c33f33a0b694 to your computer and use it in GitHub Desktop.
example for diff void** void*
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <fcntl.h> | |
#include <unistd.h> | |
#include <errno.h> | |
#include <sys/ipc.h> | |
#include <sys/shm.h> | |
#include <memory.h> | |
#define IPC_PERMS 0664 | |
#define KEY_SHM 0x2000 | |
// use follow command create example binary file | |
// python -c 'print("\x55\xAA\x55\xAA")' > struct.bin | |
typedef struct { | |
short a; | |
short b; | |
char tail; | |
} TEST_STRUCT; | |
TEST_STRUCT *g_test_struct; | |
int infd = 0; | |
static int get_shm(const int shmsize, const int mode ) | |
{ | |
int shmid = 0; | |
if( ( shmid = shmget(KEY_SHM , shmsize, mode )) < 0 ) | |
return( -1 ); | |
if( ( g_test_struct = (TEST_STRUCT *) shmat( shmid, (char *) 0, 0 ) ) | |
== (TEST_STRUCT *) -1 ) | |
{ | |
shmctl( shmid, IPC_RMID, (struct shmid_ds *) 0 ); | |
return( -1 ); | |
} | |
return( shmid ); | |
} | |
static int open_shm_file(int *shmsize ) | |
{ | |
if( ( infd = open( "struct.bin" , O_RDONLY) ) == -1 ) | |
{ | |
close( infd ); | |
return( -1 ); | |
} | |
struct stat sbuf; | |
memset( &sbuf, 0, sizeof( struct stat ) ); | |
if( fstat( infd, &sbuf ) < 0 ) | |
{ | |
close( infd ); | |
return( -1 ); | |
} | |
*shmsize = sbuf.st_size; | |
return( 0 ); | |
} | |
static int load_shm_from_bin_voidpp(const int fd | |
, void** table | |
, const int shmsize) | |
{ | |
int len = 0; | |
if( ( len = read( fd, *table, shmsize ) ) != shmsize ) | |
{ | |
printf( " read(%d) error %d\n", len, errno ); | |
close( fd ); | |
return( -1 ); | |
} | |
close( fd ); | |
return( 0 ); | |
} | |
static int load_shm_from_bin_voidp(const int fd | |
, void* table | |
, const int shmsize) | |
{ | |
int len = 0; | |
if( ( len = read( fd, table, shmsize ) ) != shmsize ) | |
{ | |
printf( " read(%d) error %d\n", len, errno ); | |
close( fd ); | |
return( -1 ); | |
} | |
close( fd ); | |
return( 0 ); | |
} | |
static int load_table_from_shm_voidpp(void** table) | |
{ | |
int shmsize = 0; | |
if( open_shm_file( &shmsize ) < 0 ) | |
return( -1 ); | |
if( get_shm( shmsize, IPC_PERMS | IPC_CREAT ) < 0 ) | |
return( -1 ); | |
if( load_shm_from_bin_voidpp( infd, table, shmsize ) < 0 ) | |
return( -1 ); | |
return( 0 ); | |
} | |
static int load_table_from_shm_voidp(void* table) | |
{ | |
int shmsize = 0; | |
if( open_shm_file(&shmsize ) < 0 ) | |
return( -1 ); | |
if( get_shm( shmsize, IPC_PERMS | IPC_CREAT ) < 0 ) | |
return( -1 ); | |
if( load_shm_from_bin_voidp( infd, table, shmsize ) < 0 ) | |
return( -1 ); | |
return( 0 ); | |
} | |
int main(){ | |
//case for void** | |
// load_table_from_shm_voidpp((void**)&g_test_struct); | |
//case for void* | |
load_table_from_shm_voidp((void*)g_test_struct); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment