Browse Source

Reallocate memory for bitmaps only when necessary.

tags/v0.9.3
Michael Brown 17 years ago
parent
commit
1de705e30f
1 changed files with 9 additions and 7 deletions
  1. 9
    7
      src/core/bitmap.c

+ 9
- 7
src/core/bitmap.c View File

@@ -41,14 +41,16 @@ int bitmap_resize ( struct bitmap *bitmap, unsigned int new_length ) {
41 41
 	old_num_blocks = BITMAP_INDEX ( bitmap->length + BITMAP_BLKSIZE - 1 );
42 42
 	new_num_blocks = BITMAP_INDEX ( new_length + BITMAP_BLKSIZE - 1 );
43 43
 
44
-	new_size = ( new_num_blocks * sizeof ( bitmap->blocks[0] ) );
45
-	new_blocks = realloc ( bitmap->blocks, new_size );
46
-	if ( ! new_blocks ) {
47
-		DBGC ( bitmap, "Bitmap %p could not resize to %d bits\n",
48
-		       bitmap, new_length );
49
-		return -ENOMEM;
44
+	if ( old_num_blocks != new_num_blocks ) {
45
+		new_size = ( new_num_blocks * sizeof ( bitmap->blocks[0] ) );
46
+		new_blocks = realloc ( bitmap->blocks, new_size );
47
+		if ( ! new_blocks ) {
48
+			DBGC ( bitmap, "Bitmap %p could not resize to %d "
49
+			       "bits\n", bitmap, new_length );
50
+			return -ENOMEM;
51
+		}
52
+		bitmap->blocks = new_blocks;
50 53
 	}
51
-	bitmap->blocks = new_blocks;
52 54
 	bitmap->length = new_length;
53 55
 
54 56
 	while ( old_num_blocks < new_num_blocks ) {

Loading…
Cancel
Save