BarryServer : Git

All the code for all my projects
// BarryServer : Git / libBLOC / blob / master / include / BLOC / object.h

// Related

libBLOC

Barry Restructuring object core + portable locking 22e0342 (2 years, 11 months ago)
/*
 * Copyright (C) 2023 Barry
 *
 * This file is part of Barry's Little Objects in C Library (libBLOC).
 *
 * libBLOC is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * libBLOC is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTIBILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * libBLOC.  If not, see <https://www.gnu.org/licenses/>.
 */

#ifndef _BLOC_OBJECT_H
#define _BLOC_OBJECT_H

#include <stddef.h>

typedef _Atomic unsigned int refcount_t;

/* Object Type */
struct ObjectType {
	refcount_t usage;
	const char *name;
	size_t size;
	void (*new)(void *);
	void (*delete)(void *);
};

void *obj_get(void *addr);
void obj_put(void *addr);
void *obj_new(struct ObjectType *type);

#define obj_drop(v) ({ obj_put(v); v = NULL; })
#define obj_swap(v,o) ({ obj_put(v); v = obj_get(o); })

int obj_verify(void *addr);
unsigned int obj_usage(void *addr);
struct ObjectType *obj_type(void *addr);

void obj_lock(void *addr);
void obj_unlock(void *addr);

#endif