From 2cf25b98257ca93512c816a4f5d17cb4bdbb2125 Mon Sep 17 00:00:00 2001 From: Alex Lin Date: Tue, 19 Apr 2016 09:02:57 -0500 Subject: [PATCH] trick comm byteswap functions can't handle sizes above 65536 bytes Added a pthread specific swap buffer in both tc_read and tc_write. The buffer is resized if a thread tries to send a buffer larger than the current swap size. refs #187 --- include/trick/trick_byteswap.h | 5 +++ .../trick_utils/comm/src/tc_read_byteswap.c | 30 ++++++++++++++--- .../trick_utils/comm/src/tc_write_byteswap.c | 32 ++++++++++++++++--- 3 files changed, 58 insertions(+), 9 deletions(-) diff --git a/include/trick/trick_byteswap.h b/include/trick/trick_byteswap.h index 469630c0..29d753c8 100644 --- a/include/trick/trick_byteswap.h +++ b/include/trick/trick_byteswap.h @@ -25,6 +25,11 @@ short trick_byteswap_short(short input); } #endif +struct SwapBuffer { + unsigned int size ; + char * swap_space ; +} ; + #define TRICK_GET_BYTE_ORDER(IND) \ { \ union { \ diff --git a/trick_source/trick_utils/comm/src/tc_read_byteswap.c b/trick_source/trick_utils/comm/src/tc_read_byteswap.c index 85205764..3a54eb97 100644 --- a/trick_source/trick_utils/comm/src/tc_read_byteswap.c +++ b/trick_source/trick_utils/comm/src/tc_read_byteswap.c @@ -4,15 +4,23 @@ * byte orders are different. */ +#include + #include "trick/tc.h" #include "trick/tc_proto.h" #include "trick/trick_byteswap.h" +static pthread_key_t key ; +static pthread_once_t key_once = PTHREAD_ONCE_INIT ; +static void make_key() { + pthread_key_create(&key, NULL) ; +} + int tc_read_byteswap(TCDevice * device, char *buffer, int size, ATTRIBUTES * attr) { char local_byteorder; int ret = 0; - static char swap[65536]; + struct SwapBuffer * swap_buffer ; if (!device) { TrickErrorHndlr *temp_error_hndlr = NULL; @@ -21,12 +29,26 @@ int tc_read_byteswap(TCDevice * device, char *buffer, int size, ATTRIBUTES * att return (-1); } + pthread_once(&key_once, make_key) ; + if (( swap_buffer = pthread_getspecific(key)) == NULL ) { + swap_buffer = malloc(sizeof(struct SwapBuffer)) ; + swap_buffer->size = 0 ; + } + if ( (unsigned int)size > swap_buffer->size ) { + unsigned int new_size = (unsigned int)size ; + if ( new_size % 4 ) { + new_size = new_size - ( new_size % 4 ) + 4 ; + } + swap_buffer->swap_space = malloc(new_size) ; + swap_buffer->size = new_size ; + } + TRICK_GET_BYTE_ORDER(local_byteorder) if (device->byte_info[TC_BYTE_ORDER_NDX] != local_byteorder) { - memset(swap, 0, (size_t) size); - ret = tc_read(device, (char *) swap, size); - trick_bswap_buffer(buffer, swap, attr, 0); + memset(swap_buffer->swap_space, 0, (size_t) size); + ret = tc_read(device, (char *) swap_buffer->swap_space, size); + trick_bswap_buffer(buffer, swap_buffer->swap_space, attr, 0); return (ret); } else { return (tc_read(device, (char *) buffer, size)); diff --git a/trick_source/trick_utils/comm/src/tc_write_byteswap.c b/trick_source/trick_utils/comm/src/tc_write_byteswap.c index f5f443cc..ccef4af3 100644 --- a/trick_source/trick_utils/comm/src/tc_write_byteswap.c +++ b/trick_source/trick_utils/comm/src/tc_write_byteswap.c @@ -4,15 +4,23 @@ * byte orders are different. */ +#include + #include "trick/tc.h" #include "trick/tc_proto.h" #include "trick/trick_byteswap.h" +static pthread_key_t key ; +static pthread_once_t key_once = PTHREAD_ONCE_INIT ; +static void make_key() { + pthread_key_create(&key, NULL) ; +} + int tc_write_byteswap(TCDevice * device, char *buffer, int size, ATTRIBUTES * attr) { char local_byteorder; int ret = 0; - static char swap[65536]; + struct SwapBuffer * swap_buffer ; if (!device) { TrickErrorHndlr *temp_error_hndlr = NULL; @@ -21,12 +29,26 @@ int tc_write_byteswap(TCDevice * device, char *buffer, int size, ATTRIBUTES * at return (-1); } + pthread_once(&key_once, make_key) ; + if (( swap_buffer = pthread_getspecific(key)) == NULL ) { + swap_buffer = malloc(sizeof(struct SwapBuffer)) ; + swap_buffer->size = 0 ; + } + if ( (unsigned int)size > swap_buffer->size ) { + unsigned int new_size = (unsigned int)size ; + if ( new_size % 4 ) { + new_size = new_size - ( new_size % 4 ) + 4 ; + } + swap_buffer->swap_space = malloc(new_size) ; + swap_buffer->size = new_size ; + } + TRICK_GET_BYTE_ORDER(local_byteorder) - if (device->byte_info[TC_BYTE_ORDER_NDX] != local_byteorder) { - memset(swap, 0, (size_t) size); - trick_bswap_buffer(swap, buffer, attr, 1); - ret = tc_write(device, (char *) swap, size); + if (device->byte_info[TC_BYTE_ORDER_NDX] != local_byteorder) { + memset(swap_buffer->swap_space, 0, (size_t) size); + trick_bswap_buffer(swap_buffer->swap_space, buffer, attr, 1); + ret = tc_write(device, (char *) swap_buffer->swap_space, size); return (ret); } else { return (tc_write(device, (char *) buffer, size));