@@ -65,6 +65,11 @@ static inline int clzll(unsigned long long mask) {
65
65
66
66
template <typename T>
67
67
ATTRIBUTE_RETURNS_NOALIAS static inline T *allocate_nonzero (size_t count) {
68
+ #ifndef NDEBUG
69
+ // make behavior when size == 0 portable
70
+ if (count == 0 )
71
+ return nullptr ;
72
+ #endif
68
73
T *ptr = reinterpret_cast <T*>(malloc (count * sizeof (T)));
69
74
if (!ptr)
70
75
zig_panic (" allocation failed" );
@@ -73,6 +78,11 @@ ATTRIBUTE_RETURNS_NOALIAS static inline T *allocate_nonzero(size_t count) {
73
78
74
79
template <typename T>
75
80
ATTRIBUTE_RETURNS_NOALIAS static inline T *allocate (size_t count) {
81
+ #ifndef NDEBUG
82
+ // make behavior when size == 0 portable
83
+ if (count == 0 )
84
+ return nullptr ;
85
+ #endif
76
86
T *ptr = reinterpret_cast <T*>(calloc (count, sizeof (T)));
77
87
if (!ptr)
78
88
zig_panic (" allocation failed" );
@@ -93,9 +103,7 @@ static inline void safe_memcpy(T *dest, const T *src, size_t count) {
93
103
94
104
template <typename T>
95
105
static inline T *reallocate (T *old, size_t old_count, size_t new_count) {
96
- T *ptr = reinterpret_cast <T*>(realloc (old, new_count * sizeof (T)));
97
- if (!ptr)
98
- zig_panic (" allocation failed" );
106
+ T *ptr = reallocate_nonzero (old, old_count, new_count);
99
107
if (new_count > old_count) {
100
108
memset (&ptr[old_count], 0 , (new_count - old_count) * sizeof (T));
101
109
}
@@ -104,6 +112,11 @@ static inline T *reallocate(T *old, size_t old_count, size_t new_count) {
104
112
105
113
template <typename T>
106
114
static inline T *reallocate_nonzero (T *old, size_t old_count, size_t new_count) {
115
+ #ifndef NDEBUG
116
+ // make behavior when size == 0 portable
117
+ if (new_count == 0 && old == nullptr )
118
+ return nullptr ;
119
+ #endif
107
120
T *ptr = reinterpret_cast <T*>(realloc (old, new_count * sizeof (T)));
108
121
if (!ptr)
109
122
zig_panic (" allocation failed" );
0 commit comments