Blame src/driver/tpax_unit_ctx.c

88751e
/******************************************************/
88751e
/*  tpax: a topological pax implementation            */
88751e
/*  Copyright (C) 2020  Z. Gilboa                     */
88751e
/*  Released under GPLv2 and GPLv3; see COPYING.TPAX. */
88751e
/******************************************************/
88751e
88751e
#include <stdint.h>
88751e
#include <stddef.h>
88751e
#include <stdlib.h>
88751e
#include <string.h>
88751e
#include <sys/mman.h>
88751e
88751e
#include <tpax/tpax.h>
88751e
#include "tpax_driver_impl.h"
88751e
#include "tpax_errinfo_impl.h"
88751e
88751e
static int tpax_free_unit_ctx_impl(struct tpax_unit_ctx_impl * ctx, int ret)
88751e
{
88751e
	if (ctx) {
88751e
		free(ctx);
88751e
	}
88751e
88751e
	return ret;
88751e
}
88751e
88751e
int tpax_get_unit_ctx(
88751e
	const struct tpax_driver_ctx *	dctx,
88751e
	const char *			path,
88751e
	struct tpax_unit_ctx **		pctx)
88751e
{
88751e
	struct tpax_unit_ctx_impl *	ctx;
88751e
88751e
	if (!dctx)
88751e
		return TPAX_CUSTOM_ERROR(
88751e
			dctx,TPAX_ERR_NULL_CONTEXT);
88751e
88751e
	else if (!(ctx = calloc(1,sizeof(*ctx))))
88751e
		return TPAX_BUFFER_ERROR(dctx);
88751e
88751e
	tpax_driver_set_ectx(
88751e
		dctx,0,path);
88751e
88751e
	ctx->path	= path;
88751e
	ctx->uctx.path	= &ctx->path;
88751e
88751e
	*pctx = &ctx->uctx;
88751e
88751e
	return 0;
88751e
}
88751e
88751e
void tpax_free_unit_ctx(struct tpax_unit_ctx * ctx)
88751e
{
88751e
	struct tpax_unit_ctx_impl *	ictx;
88751e
	uintptr_t			addr;
88751e
88751e
	if (ctx) {
88751e
		addr = (uintptr_t)ctx - offsetof(struct tpax_unit_ctx_impl,uctx);
88751e
		ictx = (struct tpax_unit_ctx_impl *)addr;
88751e
		tpax_free_unit_ctx_impl(ictx,0);
88751e
	}
88751e
}