mirror of https://github.com/xzeldon/htop.git
Add xReallocArrayZero() helper
Add helper function to reallocate an dynamic allocated array including zeroing the newly allocated memory.
This commit is contained in:
parent
8f259bc5e1
commit
bf395e10c5
16
XUtils.c
16
XUtils.c
|
@ -78,6 +78,22 @@ void* xReallocArray(void* ptr, size_t nmemb, size_t size) {
|
||||||
return xRealloc(ptr, nmemb * size);
|
return xRealloc(ptr, nmemb * size);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void* xReallocArrayZero(void* ptr, size_t prevmemb, size_t newmemb, size_t size) {
|
||||||
|
assert((ptr == NULL) == (prevmemb == 0));
|
||||||
|
|
||||||
|
if (prevmemb == newmemb) {
|
||||||
|
return ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void* ret = xReallocArray(ptr, newmemb, size);
|
||||||
|
|
||||||
|
if (newmemb > prevmemb) {
|
||||||
|
memset((unsigned char*)ret + prevmemb * size, '\0', (newmemb - prevmemb) * size);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
inline bool String_contains_i(const char* s1, const char* s2) {
|
inline bool String_contains_i(const char* s1, const char* s2) {
|
||||||
return strcasestr(s1, s2) != NULL;
|
return strcasestr(s1, s2) != NULL;
|
||||||
}
|
}
|
||||||
|
|
2
XUtils.h
2
XUtils.h
|
@ -30,6 +30,8 @@ void* xRealloc(void* ptr, size_t size) ATTR_ALLOC_SIZE1(2);
|
||||||
|
|
||||||
void* xReallocArray(void* ptr, size_t nmemb, size_t size) ATTR_ALLOC_SIZE2(2, 3);
|
void* xReallocArray(void* ptr, size_t nmemb, size_t size) ATTR_ALLOC_SIZE2(2, 3);
|
||||||
|
|
||||||
|
void* xReallocArrayZero(void* ptr, size_t prevmemb, size_t newmemb, size_t size) ATTR_ALLOC_SIZE2(3, 4);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* String_startsWith gives better performance if strlen(match) can be computed
|
* String_startsWith gives better performance if strlen(match) can be computed
|
||||||
* at compile time (e.g. when they are immutable string literals). :)
|
* at compile time (e.g. when they are immutable string literals). :)
|
||||||
|
|
Loading…
Reference in New Issue