fatal_signal_block();
if (n_files >= max_files) {
- max_files = max_files * 2 + 1;
- files = xrealloc(files, sizeof *files * max_files);
+ files = x2nrealloc(files, &max_files, sizeof *files);
}
files[n_files++] = xstrdup(file);
fatal_signal_unblock();
return s;
}
+void *
+x2nrealloc(void *p, size_t *n, size_t s)
+{
+ *n = *n == 0 ? 1 : 2 * *n;
+ return xrealloc(p, *n * s);
+}
+
char *
xasprintf(const char *format, ...)
{
char *xstrdup(const char *);
char *xasprintf(const char *format, ...) PRINTF_FORMAT(1, 2);
char *xvasprintf(const char *format, va_list) PRINTF_FORMAT(1, 0);
+void *x2nrealloc(void *p, size_t *n, size_t s);
#ifndef HAVE_STRLCPY
void strlcpy(char *dst, const char *src, size_t size);
/* Add certificate to array. */
if (*n_certs >= allocated_certs) {
- allocated_certs = 1 + 2 * allocated_certs;
- *certs = xrealloc(*certs, sizeof *certs * allocated_certs);
+ *certs = x2nrealloc(*certs, &allocated_certs, sizeof **certs);
}
(*certs)[(*n_certs)++] = certificate;
struct hook *hook;
if (secchan->n_hooks >= secchan->allocated_hooks) {
- secchan->allocated_hooks = secchan->allocated_hooks * 2 + 1;
- secchan->hooks = xrealloc(secchan->hooks,
- (sizeof *secchan->hooks
- * secchan->allocated_hooks));
+ secchan->hooks = x2nrealloc(secchan->hooks, &secchan->allocated_hooks,
+ sizeof *secchan->hooks);
}
hook = &secchan->hooks[secchan->n_hooks++];
hook->class = class;
const struct settings *s;
time_t booted;
struct switch_status_category *categories;
- int n_categories, allocated_categories;
+ size_t n_categories, allocated_categories;
};
struct status_reply {
{
struct switch_status_category *c;
if (ss->n_categories >= ss->allocated_categories) {
- ss->allocated_categories = 1 + ss->allocated_categories * 2;
- ss->categories = xrealloc(ss->categories,
- (sizeof *ss->categories
- * ss->allocated_categories));
+ ss->categories = x2nrealloc(ss->categories, &ss->allocated_categories,
+ sizeof *ss->categories);
}
c = &ss->categories[ss->n_categories++];
c->cb = cb;