]> git.donarmstrong.com Git - biopieces.git/blobdiff - code_c/Maasha/src/lib/mem.c
finished c fasta parser
[biopieces.git] / code_c / Maasha / src / lib / mem.c
index 72c02803a74688a1765430ce4345873d71f8fa62..03f49d5d50d1c94abcde44b5601576f2fc6dd742 100644 (file)
@@ -16,7 +16,7 @@ void *mem_get( size_t size )
 
     if ( ( pt = malloc( size ) ) == NULL )
     {
-        fprintf( stderr, "ERROR: Could not allocate %ld byte(s): %s\n", size, strerror( errno ) );
+        fprintf( stderr, "ERROR: Could not allocate %zu byte(s): %s\n", size, strerror( errno ) );
         abort();
     } 
 
@@ -48,14 +48,18 @@ void *mem_resize( void *pt, size_t size )
 {
     /* Martin A. Hansen, May 2008 */
 
+    /* Unit test done.*/
+
     /* Resize an allocated chunk of memory for a given pointer and new size. */
 
     void *pt_new;
 
-    if ( size == 0 ) {
-        die( "could not re-allocate 0 bytes of memory." );
-    } else if ( ( pt_new = realloc( pt, size ) ) == NULL ) {
-        die( "could not re-allocate memory." );
+    assert( size > 0 );
+
+    if ( ( pt_new = realloc( pt, size ) ) == NULL )
+    {
+        fprintf( stderr, "ERROR: Could not re-allocate %zu byte(s)\n", size );
+        abort();
     }
 
     return pt_new;
@@ -66,6 +70,8 @@ void *mem_resize_zero( void *pt, size_t old_size, size_t new_size )
 {
     /* Martin A. Hansen, May 2008 */
 
+    /* Unit test done.*/
+
     /* Resize an allocated chunk of memory for a given pointer and zero any extra memory. */
     
     void *pt_new;
@@ -84,10 +90,14 @@ void *mem_clone( void *old_pt, size_t size )
 {
     /* Martin A. Hansen, June 2008 */
 
+    /* Unit test done.*/
+
     /* Clone a structure in memory and return a pointer to the clone. */
 
     void *new_pt;
     
+    assert( size > 0 );
+
     new_pt = mem_get( size );
 
     memcpy( new_pt, old_pt, size );
@@ -96,34 +106,17 @@ void *mem_clone( void *old_pt, size_t size )
 }
 
 
-void mem_free( void *pt )
+void mem_free( void **ppt )
 {
     /* Martin A. Hansen, May 2008 */
 
-    /* Free memory from a given pointer. */
-
-    if ( pt != NULL )
-    {
-        free( pt );
-
-        pt = NULL;
-    }
-}
-
-
-void mem_free_zero( void *pt )
-{
-    /* Martin A. Hansen, July 2008 */
+    /* Unit test done.*/
 
-    /* Zero and then free memory from a given pointer. */
+    /* Free memory from a given pointer. */
 
-    if ( pt != NULL )
-    {
-        MEM_ZERO( pt );
-        free( pt );
+    free( *ppt );
 
-        pt = NULL;
-    }
+    *ppt = NULL;
 }