Browse Source

Don't always zero memory in malloc(). This saves around 2us on a

full-length PKB allocation.
tags/v0.9.3
Michael Brown 18 years ago
parent
commit
35776f481c
2 changed files with 23 additions and 6 deletions
  1. 17
    2
      src/core/malloc.c
  2. 6
    4
      src/include/stdlib.h

+ 17
- 2
src/core/malloc.c View File

134
 			 */
134
 			 */
135
 			if ( pre_size < MIN_MEMBLOCK_SIZE )
135
 			if ( pre_size < MIN_MEMBLOCK_SIZE )
136
 				list_del ( &pre->list );
136
 				list_del ( &pre->list );
137
-			/* Zero allocated memory, for calloc() */
138
-			memset ( block, 0, size );
139
 			DBG ( "Allocated [%p,%p)\n", block,
137
 			DBG ( "Allocated [%p,%p)\n", block,
140
 			      ( ( ( void * ) block ) + size ) );
138
 			      ( ( ( void * ) block ) + size ) );
141
 			return block;
139
 			return block;
297
 	realloc ( ptr, 0 );
295
 	realloc ( ptr, 0 );
298
 }
296
 }
299
 
297
 
298
+/**
299
+ * Allocate cleared memory
300
+ *
301
+ * @v size		Requested size
302
+ * @ret ptr		Allocated memory
303
+ *
304
+ * Allocate memory as per malloc(), and zero it.
305
+ */
306
+void * _calloc ( size_t size ) {
307
+	void *data;
308
+
309
+	data = malloc ( size );
310
+	if ( data )
311
+		memset ( data, 0, size );
312
+	return data;
313
+}
314
+
300
 /**
315
 /**
301
  * Add memory to allocation pool
316
  * Add memory to allocation pool
302
  *
317
  *

+ 6
- 4
src/include/stdlib.h View File

8
 extern int system ( const char *command );
8
 extern int system ( const char *command );
9
 extern long int random ( void );
9
 extern long int random ( void );
10
 
10
 
11
+extern void * _calloc ( size_t len );
12
+
11
 /**
13
 /**
12
  * Allocate cleared memory
14
  * Allocate cleared memory
13
  *
15
  *
17
  *
19
  *
18
  * Allocate memory as per malloc(), and zero it.
20
  * Allocate memory as per malloc(), and zero it.
19
  *
21
  *
20
- * Note that malloc() and calloc() are identical, in the interests of
21
- * reducing code size.  Callers should not, however, rely on malloc()
22
- * clearing memory, since this behaviour may change in future.
22
+ * This is implemented as a static inline, with the body of the
23
+ * function in _calloc(), since in most cases @c nmemb will be 1 and
24
+ * doing the multiply is just wasteful.
23
  */
25
  */
24
 static inline void * calloc ( size_t nmemb, size_t size ) {
26
 static inline void * calloc ( size_t nmemb, size_t size ) {
25
-	return malloc ( nmemb * size );
27
+	return _calloc ( nmemb * size );
26
 }
28
 }
27
 
29
 
28
 #endif /* STDLIB_H */
30
 #endif /* STDLIB_H */

Loading…
Cancel
Save