From: erickson Date: Fri, 24 Mar 2006 22:35:40 +0000 (+0000) Subject: added some macros for speeding up tight loops - taking advantage of those macros X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=f0e0f2864716b873367f04d95dd41bdf9b8f7d8c;p=working%2FOpenSRF.git added some macros for speeding up tight loops - taking advantage of those macros git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@667 9efc2488-bf62-4759-914b-345cdb29e865 --- diff --git a/src/utils/utils.c b/src/utils/utils.c index bfeafb9..1d347bb 100644 --- a/src/utils/utils.c +++ b/src/utils/utils.c @@ -138,11 +138,12 @@ growing_buffer* buffer_init(int num_initial_bytes) { size_t len = sizeof(growing_buffer); - growing_buffer* gb = (growing_buffer*) safe_malloc(len); + growing_buffer* gb; + OSRF_MALLOC(gb, len); gb->n_used = 0;/* nothing stored so far */ gb->size = num_initial_bytes; - gb->buf = (char *) safe_malloc(gb->size + 1); + OSRF_MALLOC(gb->buf, gb->size + 1); return gb; } @@ -172,31 +173,32 @@ int buffer_fadd(growing_buffer* gb, const char* format, ... ) { } -int buffer_add(growing_buffer* gb, char* data) { +int buffer_add(growing_buffer* gb, char* data) { + if(!(gb && data)) return 0; - if( ! gb || ! data ) { return 0; } int data_len = strlen( data ); - - if( data_len == 0 ) { return 0; } int total_len = data_len + gb->n_used; - while( total_len >= gb->size ) { - gb->size *= 2; - } - - if( gb->size > BUFFER_MAX_SIZE ) { - fprintf(stderr, "Buffer reached MAX_SIZE of %d", BUFFER_MAX_SIZE ); - buffer_free( gb ); - return 0; + if( total_len >= gb->size ) { + while( total_len >= gb->size ) { + gb->size *= 2; + } + + if( gb->size > BUFFER_MAX_SIZE ) { + fprintf(stderr, "Buffer reached MAX_SIZE of %d", BUFFER_MAX_SIZE ); + buffer_free( gb ); + return 0; + } + + char* new_data; + OSRF_MALLOC(new_data, gb->size ); + + strcpy( new_data, gb->buf ); + free( gb->buf ); + gb->buf = new_data; } - char* new_data = (char*) safe_malloc( gb->size ); - - strcpy( new_data, gb->buf ); - free( gb->buf ); - gb->buf = new_data; - strcat( gb->buf, data ); gb->n_used = total_len; return total_len; @@ -224,6 +226,18 @@ char* buffer_data( growing_buffer *gb) { } +/* +#define OSRF_BUFFER_ADD_CHAR(gb, c)\ + do {\ + if(gb) {\ + if(gb->n_used < gb->size - 1)\ + gb->buf[gb->n_used++] = c;\ + else\ + buffer_add_char(gb, c);\ + }\ + }while(0) + */ + int buffer_add_char(growing_buffer* gb, char c) { char buf[2]; buf[0] = c; diff --git a/src/utils/utils.h b/src/utils/utils.h index 01eb7aa..41aa488 100644 --- a/src/utils/utils.h +++ b/src/utils/utils.h @@ -31,6 +31,40 @@ GNU General Public License for more details. #include "md5.h" +#define OSRF_MALLOC(ptr, size) \ + ptr = (void*) malloc( size ); \ + if( ptr == NULL ) { \ + perror("OSRF_MALLOC(): Out of Memory" );\ + exit(99); \ + } \ + memset( ptr, 0, size ); + + +#define OSRF_BUFFER_ADD(gb, data) \ + do {\ + int __tl; \ + if(gb && data) {\ + __tl = strlen(data) + gb->n_used;\ + if( __tl < gb->size ) {\ + strcat(gb->buf, data);\ + gb->n_used = __tl; \ + } else { buffer_add(gb, data); }\ + }\ + } while(0) + +#define OSRF_BUFFER_ADD_CHAR(gb, c)\ + do {\ + if(gb) {\ + if(gb->n_used < gb->size - 1)\ + gb->buf[gb->n_used++] = c;\ + else\ + buffer_add_char(gb, c);\ + }\ + }while(0) + + + + /* turns a va_list into a string */ #define VA_LIST_TO_STRING(x) \ unsigned long __len = 0;\