Blame src/util/array.c

Packit 5e46da
/*
Packit 5e46da
 * This file is part of libbluray
Packit 5e46da
 * Copyright (C) 2014  Petri Hintukainen <phintuka@users.sourceforge.net>
Packit 5e46da
 *
Packit 5e46da
 * This library is free software; you can redistribute it and/or
Packit 5e46da
 * modify it under the terms of the GNU Lesser General Public
Packit 5e46da
 * License as published by the Free Software Foundation; either
Packit 5e46da
 * version 2.1 of the License, or (at your option) any later version.
Packit 5e46da
 *
Packit 5e46da
 * This library is distributed in the hope that it will be useful,
Packit 5e46da
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 5e46da
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 5e46da
 * Lesser General Public License for more details.
Packit 5e46da
 *
Packit 5e46da
 * You should have received a copy of the GNU Lesser General Public
Packit 5e46da
 * License along with this library. If not, see
Packit 5e46da
 * <http://www.gnu.org/licenses/>.
Packit 5e46da
 */
Packit 5e46da
Packit 5e46da
#if HAVE_CONFIG_H
Packit 5e46da
#include "config.h"
Packit 5e46da
#endif
Packit 5e46da
Packit 5e46da
#include "array.h"
Packit 5e46da
Packit 5e46da
#include "macro.h"
Packit 5e46da
Packit 5e46da
#include <stdlib.h>
Packit 5e46da
Packit 5e46da
Packit 5e46da
void *array_alloc(size_t n, size_t sz)
Packit 5e46da
{
Packit 5e46da
    size_t size = sizeof(void *) + sz;
Packit 5e46da
    if (size < sz) {
Packit 5e46da
        return NULL;
Packit 5e46da
    }
Packit 5e46da
Packit 5e46da
    unsigned char *p = (unsigned char *)calloc(n, size);
Packit 5e46da
    if (!p) {
Packit 5e46da
        return NULL;
Packit 5e46da
    }
Packit 5e46da
Packit 5e46da
    void **array = (void **)p;
Packit 5e46da
    p += n * sizeof(void *);
Packit 5e46da
    size_t i;
Packit 5e46da
    for (i = 0; i < n; i++, p += sz) {
Packit 5e46da
        array[i] = p;
Packit 5e46da
    }
Packit 5e46da
Packit 5e46da
    return array;
Packit 5e46da
}
Packit 5e46da
Packit 5e46da
void array_free(void **p)
Packit 5e46da
{
Packit 5e46da
    if (p && *p) {
Packit 5e46da
        X_FREE(*p);
Packit 5e46da
    }
Packit 5e46da
}