Merge pull request #1526 from elmart/mem-func-impr

Memory functions: Improve style.
This commit is contained in:
Justin M. Keyes 2014-11-24 14:18:45 -05:00
commit 657601a04c

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');