From 0bd8c2f09e7bcf4c7d84e846fd2bbd81d6d48fc9 Mon Sep 17 00:00:00 2001 From: Alex Lin Date: Tue, 19 Apr 2016 14:36:56 -0500 Subject: [PATCH] trick comm byteswap functions can't handle sizes above 65536 bytes Backporting fix that adds pthread specific swap buffer for both tc_read and tc_write. refs #187 --- .../trick_utils/comm/include/trick_byteswap.h | 5 +++ .../trick_utils/comm/src/tc_read_byteswap.c | 34 ++++++++++++++++--- .../trick_utils/comm/src/tc_write_byteswap.c | 34 ++++++++++++++++--- 3 files changed, 63 insertions(+), 10 deletions(-) diff --git a/trick_source/trick_utils/comm/include/trick_byteswap.h b/trick_source/trick_utils/comm/include/trick_byteswap.h index 469630c0..29d753c8 100644 --- a/trick_source/trick_utils/comm/include/trick_byteswap.h +++ b/trick_source/trick_utils/comm/include/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 d6ccaad8..20a990c3 100644 --- a/trick_source/trick_utils/comm/src/tc_read_byteswap.c +++ b/trick_source/trick_utils/comm/src/tc_read_byteswap.c @@ -4,14 +4,23 @@ * byte orders are different. */ +#include + #include "../include/tc.h" #include "../include/tc_proto.h" +#include "../include/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; @@ -20,12 +29,27 @@ 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 ; + pthread_setspecific(key, swap_buffer) ; + } + 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); + if (device->byte_info[TC_BYTE_ORDER_NDX] != local_byteorder) { + 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 8f27e3b3..affb8022 100644 --- a/trick_source/trick_utils/comm/src/tc_write_byteswap.c +++ b/trick_source/trick_utils/comm/src/tc_write_byteswap.c @@ -4,14 +4,23 @@ * byte orders are different. */ +#include + #include "../include/tc.h" #include "../include/tc_proto.h" +#include "../include/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; @@ -20,12 +29,27 @@ 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 ; + pthread_setspecific(key, swap_buffer) ; + } + 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));