Переглянути джерело

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

full-length PKB allocation.
tags/v0.9.3
Michael Brown 18 роки тому
джерело
коміт
35776f481c
2 змінених файлів з 23 додано та 6 видалено
  1. 17
    2
      src/core/malloc.c
  2. 6
    4
      src/include/stdlib.h

+ 17
- 2
src/core/malloc.c Переглянути файл

@@ -134,8 +134,6 @@ void * alloc_memblock ( size_t size, size_t align ) {
134 134
 			 */
135 135
 			if ( pre_size < MIN_MEMBLOCK_SIZE )
136 136
 				list_del ( &pre->list );
137
-			/* Zero allocated memory, for calloc() */
138
-			memset ( block, 0, size );
139 137
 			DBG ( "Allocated [%p,%p)\n", block,
140 138
 			      ( ( ( void * ) block ) + size ) );
141 139
 			return block;
@@ -297,6 +295,23 @@ void free ( void *ptr ) {
297 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 316
  * Add memory to allocation pool
302 317
  *

+ 6
- 4
src/include/stdlib.h Переглянути файл

@@ -8,6 +8,8 @@ extern void free ( void *ptr );
8 8
 extern int system ( const char *command );
9 9
 extern long int random ( void );
10 10
 
11
+extern void * _calloc ( size_t len );
12
+
11 13
 /**
12 14
  * Allocate cleared memory
13 15
  *
@@ -17,12 +19,12 @@ extern long int random ( void );
17 19
  *
18 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 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 30
 #endif /* STDLIB_H */

Завантаження…
Відмінити
Зберегти