Skip to content

Commit

Permalink
mx_util: Add mx_calloc*()
Browse files Browse the repository at this point in the history
  • Loading branch information
mariux committed May 15, 2015
1 parent d220d6a commit e51a023
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
17 changes: 17 additions & 0 deletions mx_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -607,3 +607,20 @@ int mx_sleep_nofail(unsigned int seconds)
mx_sleep(seconds);
return 1;
}

void *mx_calloc_forever_sec(size_t nmemb, size_t size, unsigned int time)
{
void *ptr;

while (1) {
ptr = calloc(nmemb, size);
if (ptr)
break;

mx_log_debug("calloc() failed: %m - retrying (forever) in %d second(s).", time);
if (time)
mx_sleep(time);
}

return ptr;
}
8 changes: 8 additions & 0 deletions mx_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,12 @@ int mx_open_newfile(char *fname);
int mx_sleep(unsigned int seconds);
int mx_sleep_nofail(unsigned int seconds);

#ifndef MX_CALLOC_FAIL_WAIT_DEFAULT
# define MX_CALLOC_FAIL_WAIT_DEFAULT 1
#endif

#define mx_calloc_forever(n, s) mx_calloc_forever_sec((n), (s), MX_CALLOC_FAIL_WAIT_DEFAULT)

void *mx_calloc_forever_sec(size_t nmemb, size_t size, unsigned int time);

#endif

0 comments on commit e51a023

Please sign in to comment.