kanoi 10 years ago
parent
commit
fad4a6fb55
  1. 2
      src/ckpool.c
  2. 48
      src/libckpool.c
  3. 1
      src/libckpool.h

2
src/ckpool.c

@ -810,6 +810,7 @@ static void launch_process(proc_instance_t *pi)
struct sigaction handler; struct sigaction handler;
int ret; int ret;
json_set_alloc_funcs(json_ckalloc, free);
launch_logger(pi); launch_logger(pi);
handler.sa_handler = &childsighandler; handler.sa_handler = &childsighandler;
handler.sa_flags = 0; handler.sa_flags = 0;
@ -1260,6 +1261,7 @@ int main(int argc, char **argv)
/* Make significant floating point errors fatal to avoid subtle bugs being missed */ /* Make significant floating point errors fatal to avoid subtle bugs being missed */
feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW ); feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW );
json_set_alloc_funcs(json_ckalloc, free);
global_ckp = &ckp; global_ckp = &ckp;
memset(&ckp, 0, sizeof(ckp)); memset(&ckp, 0, sizeof(ckp));

48
src/libckpool.c

@ -1203,9 +1203,13 @@ void align_len(size_t *len)
*len += 4 - (*len % 4); *len += 4 - (*len % 4);
} }
/* Malloc failure should be fatal but keep backing off and retrying as the OS
* will kill us eventually if it can't recover. */
void realloc_strcat(char **ptr, const char *s) void realloc_strcat(char **ptr, const char *s)
{ {
size_t old, new, len; size_t old, new, len;
int backoff = 1;
void *new_ptr;
char *ofs; char *ofs;
if (unlikely(!*s)) { if (unlikely(!*s)) {
@ -1223,9 +1227,16 @@ void realloc_strcat(char **ptr, const char *s)
old = strlen(*ptr); old = strlen(*ptr);
len = old + new + 1; len = old + new + 1;
align_len(&len); align_len(&len);
*ptr = realloc(*ptr, len); while (42) {
if (!*ptr) new_ptr = realloc(*ptr, len);
quit(1, "Failed to realloc ptr of size %d in realloc_strcat", (int)len); if (likely(new_ptr))
break;
if (backoff == 1)
fprintf(stderr, "Failed to realloc %d, retrying\n", (int)len);
cksleep_ms(backoff);
backoff <<= 1;
}
*ptr = new_ptr;
ofs = *ptr + old; ofs = *ptr + old;
sprintf(ofs, "%s", s); sprintf(ofs, "%s", s);
} }
@ -1241,23 +1252,46 @@ void trail_slash(char **buf)
void *_ckalloc(size_t len, const char *file, const char *func, const int line) void *_ckalloc(size_t len, const char *file, const char *func, const int line)
{ {
int backoff = 1;
void *ptr; void *ptr;
align_len(&len); align_len(&len);
while (42) {
ptr = malloc(len); ptr = malloc(len);
if (unlikely(!ptr)) if (likely(ptr))
quitfrom(1, file, func, line, "Failed to ckalloc!"); break;
if (backoff == 1) {
fprintf(stderr, "Failed to ckalloc %d, retrying from %s %s:%d\n",
(int)len, file, func, line);
}
cksleep_ms(backoff);
backoff <<= 1;
}
return ptr; return ptr;
} }
void *json_ckalloc(size_t size)
{
return _ckalloc(size, __FILE__, __func__, __LINE__);
}
void *_ckzalloc(size_t len, const char *file, const char *func, const int line) void *_ckzalloc(size_t len, const char *file, const char *func, const int line)
{ {
int backoff = 1;
void *ptr; void *ptr;
align_len(&len); align_len(&len);
while (42) {
ptr = calloc(len, 1); ptr = calloc(len, 1);
if (unlikely(!ptr)) if (likely(ptr))
quitfrom(1, file, func, line, "Failed to ckalloc!"); break;
if (backoff == 1) {
fprintf(stderr, "Failed to ckzalloc %d, retrying from %s %s:%d\n",
(int)len, file, func, line);
}
cksleep_ms(backoff);
backoff <<= 1;
}
return ptr; return ptr;
} }

1
src/libckpool.h

@ -489,6 +489,7 @@ void align_len(size_t *len);
void realloc_strcat(char **ptr, const char *s); void realloc_strcat(char **ptr, const char *s);
void trail_slash(char **buf); void trail_slash(char **buf);
void *_ckalloc(size_t len, const char *file, const char *func, const int line); void *_ckalloc(size_t len, const char *file, const char *func, const int line);
void *json_ckalloc(size_t size);
void *_ckzalloc(size_t len, const char *file, const char *func, const int line); void *_ckzalloc(size_t len, const char *file, const char *func, const int line);
void _dealloc(void **ptr); void _dealloc(void **ptr);
extern const int hex2bin_tbl[]; extern const int hex2bin_tbl[];

Loading…
Cancel
Save