Replace string_shrink2fit by string_reserve (More generic).
This commit is contained in:
1 parent
f9a6a82af3
commit
e0aa57b845
2 files changed
+13
-5
No files matched your search
+12
-4
@@ -146,13 +146,21 @@ stringi_fit2size (string_t v, size_t size)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static inline void
|
static inline void
|
||||||
string_shrink2fit(string_t v)
|
string_reserve(string_t v, size_t alloc)
|
||||||
{
|
{
|
||||||
STRING_CONTRACT (v);
|
STRING_CONTRACT (v);
|
||||||
char *ptr = M_MEMORY_REALLOC (char, v->ptr, v->size+1);
|
/* NOTE: Reserve below needed size, perform a shrink to fit */
|
||||||
M_ASSUME (ptr != NULL);
|
if (v->size + 1 > alloc) {
|
||||||
|
alloc = v->size+1;
|
||||||
|
}
|
||||||
|
assert (alloc > 0);
|
||||||
|
char *ptr = M_MEMORY_REALLOC (char, v->ptr, alloc);
|
||||||
|
if (M_UNLIKELY (ptr == NULL) ) {
|
||||||
|
M_MEMORY_FULL(sizeof (char) * alloc);
|
||||||
|
return;
|
||||||
|
}
|
||||||
v->ptr = ptr; // Can be != ptr if v->ptr was NULL before.
|
v->ptr = ptr; // Can be != ptr if v->ptr was NULL before.
|
||||||
v->alloc = v->size+1;
|
v->alloc = alloc;
|
||||||
STRING_CONTRACT (v);
|
STRING_CONTRACT (v);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -221,7 +221,7 @@ int main(void)
|
|||||||
string_clear (s2);
|
string_clear (s2);
|
||||||
string_init_move(s2, s1);
|
string_init_move(s2, s1);
|
||||||
assert (string_equal_str_p(s2, "RESTART"));
|
assert (string_equal_str_p(s2, "RESTART"));
|
||||||
string_shrink2fit(s2);
|
string_reserve(s2, 0);
|
||||||
assert (string_equal_str_p(s2, "RESTART"));
|
assert (string_equal_str_p(s2, "RESTART"));
|
||||||
string_init(s1);
|
string_init(s1);
|
||||||
string_move(s1, s2);
|
string_move(s1, s2);
|
||||||
|
|||||||
Reference in new issue
Block a user