Fix substitute newline: Memory functions: Improve style.

`try_malloc` was changed in 8bb2c2c074 to
avoid a warning when size is 0. Then, this improves some things on that:

- Use local vars instead of changing parameters.
- Homogenize style for other related functions.
This commit is contained in:
Eliseo Martínez
2014-11-22 21:06:22 +01:00
parent 1c25f1d947
commit 389470bd14

View File

@@ -49,11 +49,11 @@ static void try_to_free_memory(void)
/// @return pointer to allocated space. NULL if out of memory /// @return pointer to allocated space. NULL if out of memory
void *try_malloc(size_t size) FUNC_ATTR_MALLOC FUNC_ATTR_ALLOC_SIZE(1) void *try_malloc(size_t size) FUNC_ATTR_MALLOC FUNC_ATTR_ALLOC_SIZE(1)
{ {
size = size ? size : 1; size_t allocated_size = size ? size : 1;
void *ret = malloc(size); void *ret = malloc(allocated_size);
if (!ret) { if (!ret) {
try_to_free_memory(); try_to_free_memory();
ret = malloc(size); ret = malloc(allocated_size);
} }
return ret; return ret;
} }
@@ -102,10 +102,12 @@ void *xmalloc(size_t size)
void *xcalloc(size_t count, size_t size) void *xcalloc(size_t count, size_t size)
FUNC_ATTR_MALLOC FUNC_ATTR_ALLOC_SIZE_PROD(1, 2) FUNC_ATTR_NONNULL_RET FUNC_ATTR_MALLOC FUNC_ATTR_ALLOC_SIZE_PROD(1, 2) FUNC_ATTR_NONNULL_RET
{ {
void *ret = count && size ? calloc(count, size) : calloc(1, 1); size_t allocated_count = count && size ? count : 1;
size_t allocated_size = count && size ? size : 1;
void *ret = calloc(allocated_count, allocated_size);
if (!ret) { if (!ret) {
try_to_free_memory(); try_to_free_memory();
ret = count && size ? calloc(count, size) : calloc(1, 1); ret = calloc(allocated_count, allocated_size);
if (!ret) { if (!ret) {
OUT_STR(e_outofmem); OUT_STR(e_outofmem);
out_char('\n'); out_char('\n');
@@ -123,10 +125,11 @@ void *xcalloc(size_t count, size_t size)
void *xrealloc(void *ptr, size_t size) void *xrealloc(void *ptr, size_t size)
FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_ALLOC_SIZE(2) FUNC_ATTR_NONNULL_RET FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_ALLOC_SIZE(2) FUNC_ATTR_NONNULL_RET
{ {
void *ret = size ? realloc(ptr, size) : realloc(ptr, 1); size_t allocated_size = size ? size : 1;
void *ret = realloc(ptr, allocated_size);
if (!ret) { if (!ret) {
try_to_free_memory(); try_to_free_memory();
ret = size ? realloc(ptr, size) : realloc(ptr, 1); ret = realloc(ptr, allocated_size);
if (!ret) { if (!ret) {
OUT_STR(e_outofmem); OUT_STR(e_outofmem);
out_char('\n'); out_char('\n');