提交 ab2258a4 编写于 作者: B Behdad Esfahbod

[vector] Use allocated = -1 to signify failure

上级 09fa536d
...@@ -44,7 +44,7 @@ struct hb_vector_t ...@@ -44,7 +44,7 @@ struct hb_vector_t
unsigned int length; unsigned int length;
private: private:
unsigned int allocated; /* == 0 means allocation failed. */ int allocated; /* == -1 means allocation failed. */
Type *arrayZ_; Type *arrayZ_;
Type static_array[PreallocedCount]; Type static_array[PreallocedCount];
public: public:
...@@ -60,8 +60,7 @@ struct hb_vector_t ...@@ -60,8 +60,7 @@ struct hb_vector_t
{ {
if (arrayZ_) if (arrayZ_)
free (arrayZ_); free (arrayZ_);
arrayZ_ = nullptr; init ();
allocated = length = 0;
} }
void fini_deep () void fini_deep ()
{ {
...@@ -141,12 +140,12 @@ struct hb_vector_t ...@@ -141,12 +140,12 @@ struct hb_vector_t
return p; return p;
} }
bool in_error () const { return allocated == 0; } bool in_error () const { return allocated < 0; }
/* Allocate for size but don't adjust length. */ /* Allocate for size but don't adjust length. */
bool alloc (unsigned int size) bool alloc (unsigned int size)
{ {
if (unlikely (!allocated)) if (unlikely (allocated < 0))
return false; return false;
if (likely (size <= allocated)) if (likely (size <= allocated))
...@@ -168,14 +167,17 @@ struct hb_vector_t ...@@ -168,14 +167,17 @@ struct hb_vector_t
} }
else else
{ {
bool overflows = (new_allocated < allocated) || hb_unsigned_mul_overflows (new_allocated, sizeof (Type)); bool overflows =
(int) new_allocated < 0 ||
(new_allocated < allocated) ||
hb_unsigned_mul_overflows (new_allocated, sizeof (Type));
if (likely (!overflows)) if (likely (!overflows))
new_array = (Type *) realloc (arrayZ_, new_allocated * sizeof (Type)); new_array = (Type *) realloc (arrayZ_, new_allocated * sizeof (Type));
} }
if (unlikely (!new_array)) if (unlikely (!new_array))
{ {
allocated = 0; allocated = -1;
return false; return false;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册