Blame src/driver/sfrt_unit_ctx.c

f46039
#include <stdint.h>
f46039
#include <stddef.h>
f46039
#include <stdlib.h>
f46039
#include <string.h>
f46039
#include <sys/mman.h>
f46039
f46039
#include <sofort/sofort.h>
f46039
#include "sofort_driver_impl.h"
f46039
f46039
static int sfrt_free_unit_ctx_impl(struct sfrt_unit_ctx_impl * ctx, int status)
f46039
{
f46039
	if (ctx) {
f46039
		sfrt_unmap_input(&ctx->map);
f46039
		free(ctx);
f46039
	}
f46039
f46039
	return status;
f46039
}
f46039
f46039
int sfrt_get_unit_ctx(
f46039
	const struct sfrt_driver_ctx *	dctx,
f46039
	const char *			path,
f46039
	struct sfrt_unit_ctx **		pctx)
f46039
{
f46039
	struct sfrt_unit_ctx_impl *	ctx;
f46039
a2826e
	if (!dctx || !(ctx = calloc(1,sizeof(*ctx))))
f46039
		return -1;
f46039
47a21b
	if (sfrt_map_input(dctx,-1,path,PROT_READ,&ctx->map))
f46039
		return sfrt_free_unit_ctx_impl(ctx,-1);
f46039
f46039
	memcpy(&ctx->cctx,dctx->cctx,
f46039
		sizeof(ctx->cctx));
f46039
f46039
	ctx->path	= path;
f46039
f46039
	ctx->uctx.path	= &ctx->path;
f46039
	ctx->uctx.map	= &ctx->map;
f46039
	ctx->uctx.cctx	= &ctx->cctx;
f46039
f46039
	*pctx = &ctx->uctx;
f46039
	return 0;
f46039
}
f46039
f46039
void sfrt_free_unit_ctx(struct sfrt_unit_ctx * ctx)
f46039
{
f46039
	struct sfrt_unit_ctx_impl *	ictx;
f46039
	uintptr_t			addr;
f46039
f46039
	if (ctx) {
f46039
		addr = (uintptr_t)ctx - offsetof(struct sfrt_unit_ctx_impl,uctx);
f46039
		ictx = (struct sfrt_unit_ctx_impl *)addr;
f46039
		sfrt_free_unit_ctx_impl(ictx,0);
f46039
	}
f46039
}