metadata.count = rv;
PARA_NOTICE_LOG("%d metadata item(s) found\n", rv);
- metadata.tags = alloc((metadata.count + 5) * sizeof(mp4ff_tag_t));
+ metadata.tags = arr_alloc(metadata.count + 5, sizeof(mp4ff_tag_t));
for (i = 0; i < metadata.count; i++) {
mp4ff_tag_t *tag = metadata.tags + i;
consumed += frame_info.bytesconsumed;
if (!frame_info.samples)
goto success;
- btrbuf = alloc(2 * frame_info.samples);
+ btrbuf = arr_alloc(2, frame_info.samples);
for (i = 0; i < frame_info.samples; i++) {
short sh = ((short *)outbuf)[i];
write_int16_host_endian(btrbuf + loaded, sh);
int (*compar)(const void *, const void *);
int i;
- options->data_ptr = alloc(nmemb * sizeof(*options->data_ptr));
+ options->data_ptr = arr_alloc(nmemb, sizeof(*options->data_ptr));
for (i = 0; i < nmemb; i++)
options->data_ptr[i] = options->data + i;
n = OPT_GIVEN(USER_ALLOW);
if (n == 0)
return;
- uid_whitelist = alloc(n * sizeof(uid_t));
+ uid_whitelist = arr_alloc(n, sizeof(uid_t));
for (i = 0; i < n; i++) {
const char *arg = lls_string_val(i, OPT_RESULT(USER_ALLOW));
int32_t val;
goto free_moods;
num_pl = ret;
n = num_moods + num_pl;
- mops = alloc((n + 1) * sizeof(char *));
+ mops = arr_alloc(n + 1, sizeof(char *));
for (i = 0; i < num_moods; i++)
mops[i] = make_message("m/%s", moods[i]);
for (i = 0; i < num_pl; i++)
for (i = 0; p < end; i++)
p += strlen(p) + 1;
argc = i;
- argv = alloc((argc + 1) * sizeof(char *));
+ argv = arr_alloc(argc + 1, sizeof(char *));
for (i = 0, p = iov->iov_base; p < end; i++) {
argv[i] = para_strdup(p);
p += strlen(p) + 1;
static unsigned char *alloc_matrix(int rows, int cols)
{
- return alloc(rows * cols);
+ return arr_alloc(rows, cols);
}
/*
static int invert_mat(unsigned char *src, int k)
{
int irow, icol, row, col, ix, error;
- int *indxc = alloc(k * sizeof(int));
- int *indxr = alloc(k * sizeof(int));
- int *ipiv = alloc(k * sizeof(int)); /* elements used as pivots */
+ int *indxc = arr_alloc(k, sizeof(int));
+ int *indxr = arr_alloc(k, sizeof(int));
+ int *ipiv = arr_alloc(k, sizeof(int)); /* elements used as pivots */
unsigned char c, *p, *id_row = alloc_matrix(1, k),
*temp_row = alloc_matrix(1, k);
if (ret < 0)
return ret;
/* do the actual decoding */
- slice = alloc(k * sizeof(unsigned char *));
+ slice = arr_alloc(k, sizeof(unsigned char *));
for (row = 0; row < k; row++) {
if (idx[row] >= k) {
slice[row] = zalloc(sz);
}
if (fg->num_slices == 0) {
fg->num_slices = fg->h.slices_per_group;
- fg->idx = alloc(fg->num_slices * sizeof(int));
+ fg->idx = arr_alloc(fg->num_slices, sizeof(int));
fg->data = zalloc(fg->num_slices * sizeof(unsigned char *));
}
r = fg->num_received_slices;
EMBRACE(.name = "stdin"));
stdin_task_register(sit, &s);
- fns = alloc(OPT_GIVEN(FILTER) * sizeof(*fns));
+ fns = arr_alloc(OPT_GIVEN(FILTER), sizeof(*fns));
for (i = 0, parent = sit->btrn; i < OPT_GIVEN(FILTER); i++) {
const char *fa = lls_string_val(i, OPT_RESULT(FILTER));
const char *name;
struct btr_node *btrn = fn->btrn;
size_t k, n = frame->header.blocksize;
unsigned channels = FLAC__stream_decoder_get_channels(decoder);
- char *outbuffer = alloc(n * channels * 2);
+ char *outbuffer = arr_alloc(n, channels * 2);
if (channels == 1) {
for (k = 0; k < n; k++) {
s->nbits = nbits;
n = 1 << nbits;
- s->revtab = alloc(n * sizeof(uint16_t));
+ s->revtab = arr_alloc(n, sizeof(uint16_t));
for (j = 4; j <= nbits; j++) {
int k = 1 << j;
double freq = 2 * M_PI / k;
s->nbits = nbits;
s->n = n;
n4 = n >> 2;
- s->tcos = alloc(n4 * sizeof(fftsample_t));
- s->tsin = alloc(n4 * sizeof(fftsample_t));
+ s->tcos = arr_alloc(n4, sizeof(fftsample_t));
+ s->tsin = arr_alloc(n4, sizeof(fftsample_t));
for (i = 0; i < n4; i++) {
alpha = 2 * M_PI * (i + 1.0 / 8.0) / n;
if (is_prefix(word, cmd, len))
match_count++;
}
- matches = alloc((match_count + 1) * sizeof(*matches));
+ matches = arr_alloc(match_count + 1, sizeof(*matches));
for (i = 0, match_count = 0; (cmd = completers[i].name); i++)
if (is_prefix(word, cmd, len))
matches[match_count++] = para_strdup(cmd);
const char *tag_versions[] = {"no", "id3v1", "id3v2", "id3v1+id3v2"};
afhi->chunks_total = 0;
- afhi->chunk_table = alloc(chunk_table_size * sizeof(uint32_t));
+ afhi->chunk_table = arr_alloc(chunk_table_size, sizeof(uint32_t));
while (1) {
int freq, br;
struct timeval tmp, cct; /* current chunk time */
}
fn->min_iqs = 0;
mad_synth_frame(&pmd->synth, &pmd->frame);
- outbuffer = alloc(pmd->synth.pcm.length * 2 * pmd->channels);
+ outbuffer = arr_alloc(pmd->synth.pcm.length, 2 * pmd->channels);
loaded = 0;
for (i = 0; i < pmd->synth.pcm.length; i++) {
int sample = MAD_TO_SHORT(pmd->synth.pcm.samples[0][i]);
PARA_INFO_LOG("%" PRIu32 " seconds, %d frames/chunk\n",
afhi->seconds_total, frames_per_chunk);
ct_size = 250;
- afhi->chunk_table = alloc(ct_size * sizeof(uint32_t));
+ afhi->chunk_table = arr_alloc(ct_size, sizeof(uint32_t));
afhi->chunk_table[0] = 0;
afhi->chunk_table[1] = afhi->header_len;
oss.returned = afhi->header_len;
/* don't care for anything except opus eos */
if (op->e_o_s && ctx->os.serialno == ctx->opus_serialno)
ctx->eos = true;
- output = alloc(sizeof(short) * MAX_FRAME_SIZE * ctx->channels);
+ output = arr_alloc(sizeof(short) * ctx->channels, MAX_FRAME_SIZE);
ret = opus_multistream_decode(ctx->st, (unsigned char *)op->packet,
op->bytes, output, MAX_FRAME_SIZE, 0);
if (ret < 0) {
typeof(x) _x = (x); \
_x > 0? _x : -_x; })
-
extern __printf_2_3 void (*para_log)(int, const char*, ...);
/**
* Define a standard log function that always writes to stderr.
static void init_shuffle_map(void)
{
unsigned n, num_inputs = lls_num_inputs(play_lpr);
- shuffle_map = alloc(num_inputs * sizeof(unsigned));
+ shuffle_map = arr_alloc(num_inputs, sizeof(unsigned));
for (n = 0; n < num_inputs; n++)
shuffle_map[n] = n;
if (!OPT_GIVEN(RANDOMIZE))
char **result;
int i;
- result = alloc((NUM_MAPPED_KEYS + 1) * sizeof(char *));
+ result = arr_alloc(NUM_MAPPED_KEYS + 1, sizeof(char *));
FOR_EACH_MAPPED_KEY(i) {
char *seq = get_key_map_seq(i);
result[i] = seq;
data.output_frames = num_frames * ctx->ratio + 1;
out_samples = data.output_frames * ctx->channels;
- in_float = alloc(num_samples * sizeof(float));
+ in_float = arr_alloc(num_samples, sizeof(float));
src_short_to_float_array(in, in_float, num_samples);
data.data_in = in_float;
- data.data_out = alloc(out_samples * sizeof(float));
+ data.data_out = arr_alloc(out_samples, sizeof(float));
ret = src_process(ctx->src_state, &data);
free(in_float);
if (ret != 0) {
return -E_LIBSAMPLERATE;
}
out_samples = data.output_frames_gen * ctx->channels;
- out = alloc(out_samples * sizeof(short));
+ out = arr_alloc(out_samples, sizeof(short));
src_float_to_short_array(data.data_out, out, out_samples);
free(data.data_out);
*result = out;
if (new_frame_size <= 0)
continue;
samples = new_frame_size * psd->shi.channels;
- btr_output = alloc(2 * samples);
+ btr_output = arr_alloc(samples, 2);
for (i = 0; i < samples; i++)
btr_output[i] = read_u16(output + i + skip_idx);
btr_add_output((char *)btr_output, samples * 2, btrn);
return ptr;
}
+/**
+ * Allocate an array, abort on failure or bugs.
+ *
+ * \param nmemb See \ref arr_realloc().
+ * \param size See \ref arr_realloc().
+ *
+ * Like \ref arr_realloc(), this aborts on invalid arguments, integer overflow
+ * and allocation errors.
+ *
+ * \return A pointer to newly allocated memory which is suitably aligned for
+ * any kind of variable.
+ *
+ * \sa See \ref arr_realloc().
+ */
+__must_check __malloc void *arr_alloc(size_t nmemb, size_t size)
+{
+ return arr_realloc(NULL, nmemb, size);
+}
+
/**
* Paraslash's version of realloc().
*
*/
__must_check __malloc void *alloc(size_t size)
{
- void *p;
-
- assert(size);
- p = malloc(size);
- if (!p) {
- PARA_EMERG_LOG("malloc failed (size = %zu), aborting\n",
- size);
- exit(EXIT_FAILURE);
- }
- return p;
+ return arr_alloc(1, size);
}
/**
static int create_argv_offset(int offset, const char *buf, const char *delim,
char ***result)
{
- char *word, **argv = alloc((offset + 1) * sizeof(char *));
+ char *word, **argv = arr_alloc(offset + 1, sizeof(char *));
const char *p;
int i, ret;
return -ERRNO_TO_PARA_ERROR(errno);
if (num_wchars == 0)
return 0;
- dest = alloc((num_wchars + 1) * sizeof(*dest));
+ dest = arr_alloc(num_wchars + 1, sizeof(*dest));
src = s;
memset(&state, 0, sizeof(state));
num_wchars = mbsrtowcs(dest, &src, num_wchars, &state);
num_wchars = mbsrtowcs(NULL, &src, 0, &state);
if (num_wchars == (size_t)-1)
return -ERRNO_TO_PARA_ERROR(errno);
- wcs = alloc((num_wchars + 1) * sizeof(*wcs));
+ wcs = arr_alloc(num_wchars + 1, sizeof(*wcs));
memset(&state, 0, sizeof(state));
num_wchars = mbsrtowcs(wcs, &src, num_wchars + 1, &state);
assert(num_wchars != (size_t)-1);
__must_check void *para_realloc(void *p, size_t size);
__must_check __malloc void *alloc(size_t size);
__must_check __malloc void *zalloc(size_t size);
+__must_check __malloc void *arr_alloc(size_t nmemb, size_t size);
__must_check __malloc char *para_strdup(const char *s);
__printf_2_0 unsigned xvasprintf(char **result, const char *fmt, va_list ap);
r_b = FILTER_CMD_OPT_RESULT(SYNC, BUDDY, lpr);
n = lls_opt_given(r_b);
- sbi = alloc(n * sizeof(*sbi));
+ sbi = arr_alloc(n, sizeof(*sbi));
PARA_INFO_LOG("initializing buddy info array of length %u\n", n);
for (i = 0; i < n; i++) {
const char *url = lls_string_val(i, r_b);
ret = fec_new(k, n, &fc->parms);
if (ret < 0)
return ret;
- fc->src_data = alloc(k * sizeof(char *));
+ fc->src_data = arr_alloc(k, sizeof(char *));
for (i = 0; i < k; i++)
fc->src_data[i] = alloc(fc->mps);
fc->enc_buf = alloc(fc->mps);
size_t ct_size = 250;
int ret, count = 0, num_frames, num_superframes;
- afhi->chunk_table = alloc(ct_size * sizeof(uint32_t));
+ afhi->chunk_table = arr_alloc(ct_size, sizeof(uint32_t));
afhi->chunk_table[0] = 0;
afhi->chunk_table[1] = afhi->header_len;
if (top.reserved2 != 2)
return -E_NO_WMA;
p++; /* objects start at p */
- top.objects = alloc(top.num_objects * sizeof(struct asf_object));
+ top.objects = arr_alloc(top.num_objects, sizeof(struct asf_object));
ret = read_asf_objects(p, top.size - (p - map), top.num_objects,
top.objects, &ton);
if (ret < 0)
int i, l, j, k, level, n = src->n;
init_vlc(dst, VLCBITS, n, src->huffbits, src->huffcodes, 4);
- pwd->run_table[didx] = alloc(n * sizeof(uint16_t));
- pwd->level_table[didx] = alloc(n * sizeof(uint16_t));
+ pwd->run_table[didx] = arr_alloc(n, sizeof(uint16_t));
+ pwd->level_table[didx] = arr_alloc(n, sizeof(uint16_t));
i = 2;
level = 1;
k = 0;
{
struct mp_ast_node *node = ast_node_raw(id);
node->num_children = 2;
- node->children = alloc(2 * sizeof(struct mp_ast_node *));
+ node->children = arr_alloc(2, sizeof(struct mp_ast_node *));
node->children[0] = left;
node->children[1] = right;
return node;