File tree Expand file tree Collapse file tree 3 files changed +30
-11
lines changed Expand file tree Collapse file tree 3 files changed +30
-11
lines changed Original file line number Diff line number Diff line change 2
2
* Exercise 8-6. The standard library function calloc(n, size) returns a
3
3
* pointer to n objects of size size, with the storage initialized to zero.
4
4
* Write calloc, by calling malloc or by modifying it.
5
+ *
6
+ * Note: to clear memory, you could use memset function in my_calloc instead,
7
+ * but without measures faster.
8
+ *
5
9
* By Faisal Saadatmand
6
10
*/
7
11
@@ -62,17 +66,22 @@ void *knr_malloc(unsigned nbytes)
62
66
prevp = p ; /* save current pointer's address */
63
67
}
64
68
}
65
- #include <string.h>
69
+
66
70
/* my_calloc: general-purpose storage allocator. Initialize memory to zeros */
67
71
void * my_calloc (unsigned n , unsigned size )
68
72
{
69
73
unsigned char * p ; /* char is exactly 1 byte */
74
+ Header * hp ;
75
+ unsigned bsize ; /* actual block size in bytes */
70
76
unsigned i ;
71
77
72
- if ((p = (unsigned char * ) knr_malloc (n * size )) != NULL )
73
- for (i = 0 ; i < n * size ; i ++ )
78
+ if ((p = (unsigned char * ) knr_malloc (n * size )) != NULL ) {
79
+ hp = (Header * ) p - 1 ;
80
+ bsize = (hp -> s .size - 1 ) * sizeof (Header );
81
+ for (i = 0 ; i < bsize - 1 ; i ++ )
74
82
p [i ] &= 0x0u ; /* clear each byte */
75
- return (void * ) p ;
83
+ }
84
+ return (void * ) p ;
76
85
}
77
86
78
87
/* morecore: ask system for more memory */
Original file line number Diff line number Diff line change @@ -70,13 +70,18 @@ void *knr_malloc(unsigned nbytes)
70
70
/* my_calloc: general-purpose storage allocator. Initialize memory to zeros */
71
71
void * my_calloc (unsigned n , unsigned size )
72
72
{
73
- unsigned char * p ; /* char is exactly 1 byte */
73
+ unsigned char * p ; /* char is exactly 1 byte */
74
+ Header * hp ;
75
+ unsigned bsize ; /* actual block size in bytes */
74
76
unsigned i ;
75
77
76
- if ((p = (unsigned char * ) knr_malloc (n * size )) != NULL )
77
- for (i = 0 ; i < n * size ; i ++ )
78
+ if ((p = (unsigned char * ) knr_malloc (n * size )) != NULL ) {
79
+ hp = (Header * ) p - 1 ;
80
+ bsize = (hp -> s .size - 1 ) * sizeof (Header );
81
+ for (i = 0 ; i < bsize - 1 ; i ++ )
78
82
p [i ] &= 0x0u ; /* clear each byte */
79
- return (void * ) p ;
83
+ }
84
+ return (void * ) p ;
80
85
}
81
86
82
87
/* morecore: ask system for more memory */
Original file line number Diff line number Diff line change @@ -71,12 +71,17 @@ void *knr_malloc(unsigned nbytes)
71
71
void * my_calloc (unsigned n , unsigned size )
72
72
{
73
73
unsigned char * p ; /* char is exactly 1 byte */
74
+ Header * hp ;
75
+ unsigned bsize ; /* actual block size in bytes */
74
76
unsigned i ;
75
77
76
- if ((p = (unsigned char * ) knr_malloc (n * size )) != NULL )
77
- for (i = 0 ; i < n * size ; i ++ )
78
+ if ((p = (unsigned char * ) knr_malloc (n * size )) != NULL ) {
79
+ hp = (Header * ) p - 1 ;
80
+ bsize = (hp -> s .size - 1 ) * sizeof (Header );
81
+ for (i = 0 ; i < bsize - 1 ; i ++ )
78
82
p [i ] &= 0x0u ; /* clear each byte */
79
- return (void * ) p ;
83
+ }
84
+ return (void * ) p ;
80
85
}
81
86
82
87
/* morecore: ask system for more memory */
You can’t perform that action at this time.
0 commit comments