43 |
return vp; |
return vp; |
44 |
} |
} |
45 |
|
|
46 |
/* Allocates a block of SIZE bytes, fill it with all-bits-0, and |
|
47 |
returns it. |
/* Allocates a continous block of N_MEMB by SIZE elements, with all |
48 |
If SIZE is 0, returns a null pointer. |
bits set to 0. |
49 |
Aborts if unsuccessful. */ |
Aborts if unsuccessful. |
50 |
|
*/ |
51 |
void * |
void * |
52 |
xcalloc (size_t size) |
xcalloc (size_t n_memb, size_t size) |
53 |
{ |
{ |
54 |
void *vp = xmalloc (size); |
const size_t prod = size * n_memb; |
55 |
memset (vp, 0, size); |
void *vp = 0; |
56 |
|
|
57 |
|
if (prod == 0) |
58 |
|
return NULL; |
59 |
|
|
60 |
|
/* Trap overflow errors */ |
61 |
|
assert ( prod >= size ); |
62 |
|
assert ( prod >= n_memb ) ; |
63 |
|
|
64 |
|
vp = xmalloc ( prod ); |
65 |
|
memset (vp, 0, prod); |
66 |
return vp; |
return vp; |
67 |
} |
} |
68 |
|
|
69 |
|
|
70 |
|
|
71 |
/* If SIZE is 0, then block PTR is freed and a null pointer is |
/* If SIZE is 0, then block PTR is freed and a null pointer is |
72 |
returned. |
returned. |
73 |
Otherwise, if PTR is a null pointer, then a new block is allocated |
Otherwise, if PTR is a null pointer, then a new block is allocated |