Blame src/util/refcnt.c

Packit 5e46da
/*
Packit 5e46da
 * This file is part of libbluray
Packit 5e46da
 * Copyright (C) 2013  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 "refcnt.h"
Packit 5e46da
Packit 5e46da
#include "logging.h"
Packit 5e46da
#include "mutex.h"
Packit 5e46da
Packit 5e46da
#include <stdlib.h>
Packit 5e46da
#include <string.h>
Packit 5e46da
Packit 5e46da
/*
Packit 5e46da
 *
Packit 5e46da
 */
Packit 5e46da
Packit 5e46da
typedef struct {
Packit 5e46da
  BD_MUTEX mutex;   /* initialized only if counted == 1 */
Packit 5e46da
  int      count;   /* reference count */
Packit 5e46da
  unsigned counted; /* 1 if this object is ref-counted */
Packit 5e46da
} BD_REFCNT;
Packit 5e46da
Packit 5e46da
/*
Packit 5e46da
 *
Packit 5e46da
 */
Packit 5e46da
Packit 5e46da
void bd_refcnt_inc(const void *obj)
Packit 5e46da
{
Packit 5e46da
    if (!obj) {
Packit 5e46da
        return;
Packit 5e46da
    }
Packit 5e46da
Packit 5e46da
    BD_REFCNT *ref = &(((BD_REFCNT *)(intptr_t)obj)[-1]);
Packit 5e46da
Packit 5e46da
    if (!ref->counted) {
Packit 5e46da
        bd_mutex_init(&ref->mutex);
Packit 5e46da
        ref->counted = 1;
Packit 5e46da
        ref->count = 2;
Packit 5e46da
        return;
Packit 5e46da
    }
Packit 5e46da
Packit 5e46da
    bd_mutex_lock(&ref->mutex);
Packit 5e46da
    ++ref->count;
Packit 5e46da
    bd_mutex_unlock(&ref->mutex);
Packit 5e46da
}
Packit 5e46da
Packit 5e46da
void bd_refcnt_dec(const void *obj)
Packit 5e46da
{
Packit 5e46da
    if (!obj) {
Packit 5e46da
        return;
Packit 5e46da
    }
Packit 5e46da
Packit 5e46da
    BD_REFCNT *ref = &((BD_REFCNT *)(intptr_t)obj)[-1];
Packit 5e46da
Packit 5e46da
    if (ref->counted) {
Packit 5e46da
        int count;
Packit 5e46da
Packit 5e46da
        bd_mutex_lock(&ref->mutex);
Packit 5e46da
        count = --ref->count;
Packit 5e46da
        bd_mutex_unlock(&ref->mutex);
Packit 5e46da
Packit 5e46da
        if (count > 0) {
Packit 5e46da
            return;
Packit 5e46da
        }
Packit 5e46da
Packit 5e46da
        bd_mutex_destroy(&ref->mutex);
Packit 5e46da
    }
Packit 5e46da
Packit 5e46da
    free(ref);
Packit 5e46da
}
Packit 5e46da
Packit 5e46da
void *refcnt_realloc(void *obj, size_t sz)
Packit 5e46da
{
Packit 5e46da
    sz += sizeof(BD_REFCNT);
Packit 5e46da
Packit 5e46da
    if (obj) {
Packit 5e46da
        if (((BD_REFCNT *)obj)[-1].counted) {
Packit 5e46da
            bd_refcnt_dec(obj);
Packit 5e46da
            BD_DEBUG(DBG_CRIT, "refcnt_realloc(): realloc locked object !\n");
Packit 5e46da
            obj = NULL;
Packit 5e46da
        }
Packit 5e46da
    }
Packit 5e46da
Packit 5e46da
    if (obj) {
Packit 5e46da
        obj = realloc(&((BD_REFCNT *)obj)[-1], sz);
Packit 5e46da
        if (!obj) {
Packit 5e46da
            return NULL;
Packit 5e46da
        }
Packit 5e46da
    } else {
Packit 5e46da
        obj = realloc(NULL, sz);
Packit 5e46da
        if (!obj) {
Packit 5e46da
            return NULL;
Packit 5e46da
        }
Packit 5e46da
        memset(obj, 0, sizeof(BD_REFCNT));
Packit 5e46da
    }
Packit 5e46da
Packit 5e46da
    return &((BD_REFCNT *)obj)[1];
Packit 5e46da
}
Packit 5e46da
Packit 5e46da
Packit 5e46da