committing scott's safer MALLOC macro and a new buffer_release function patches
authorerickson <erickson@9efc2488-bf62-4759-914b-345cdb29e865>
Fri, 18 May 2007 16:53:29 +0000 (16:53 +0000)
committererickson <erickson@9efc2488-bf62-4759-914b-345cdb29e865>
Fri, 18 May 2007 16:53:29 +0000 (16:53 +0000)
git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@898 9efc2488-bf62-4759-914b-345cdb29e865

src/utils/utils.c
src/utils/utils.h

index bdeaa96..6949344 100644 (file)
@@ -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;
index 308e3b7..7bbade0 100644 (file)
@@ -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);