From: erickson Date: Fri, 18 May 2007 16:53:29 +0000 (+0000) Subject: committing scott's safer MALLOC macro and a new buffer_release function patches X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=a9ed2bb6214ce705cb8b4127b77559e9553334b1;p=opensrf%2Fbjwebb.git committing scott's safer MALLOC macro and a new buffer_release function patches git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@898 9efc2488-bf62-4759-914b-345cdb29e865 --- diff --git a/src/utils/utils.c b/src/utils/utils.c index bdeaa96..6949344 100644 --- a/src/utils/utils.c +++ b/src/utils/utils.c @@ -214,6 +214,18 @@ int buffer_reset( growing_buffer *gb){ return 1; } +/* Return a pointer to the text within a growing_buffer, */ +/* while destroying the growing_buffer itself. */ + +char* buffer_release( growing_buffer* gb) { + char* s = gb->buf; + s[gb->n_used] = '\0'; + free( gb ); + return s; +} + +/* Destroy a growing_buffer and the text it contains */ + int buffer_free( growing_buffer* gb ) { if( gb == NULL ) return 0; diff --git a/src/utils/utils.h b/src/utils/utils.h index 308e3b7..7bbade0 100644 --- a/src/utils/utils.h +++ b/src/utils/utils.h @@ -32,12 +32,14 @@ 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 ); + do {\ + ptr = (void*) malloc( size ); \ + if( ptr == NULL ) { \ + perror("OSRF_MALLOC(): Out of Memory" );\ + exit(99); \ + } \ + memset( ptr, 0, size );\ + } while(0) #define OSRF_BUFFER_ADD(gb, data) \ @@ -173,6 +175,7 @@ int buffer_add(growing_buffer* gb, char* c); int buffer_fadd(growing_buffer* gb, const char* format, ... ); int buffer_reset( growing_buffer* gb); char* buffer_data( growing_buffer* gb); +char* buffer_release( growing_buffer* gb ); int buffer_free( growing_buffer* gb ); int buffer_add_char(growing_buffer* gb, char c);