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"
162601
#include "sofort_errinfo_impl.h"
f46039
162601
static int sfrt_free_unit_ctx_impl(struct sfrt_unit_ctx_impl * ctx, int ret)
f46039
{
f46039
	if (ctx) {
f46039
		sfrt_unmap_input(&ctx->map);
f46039
		free(ctx);
f46039
	}
f46039
162601
	return ret;
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
162601
	if (!dctx)
162601
		return SFRT_CUSTOM_ERROR(
162601
			dctx,SFRT_ERR_NULL_CONTEXT);
162601
162601
	else if (!(ctx = calloc(1,sizeof(*ctx))))
162601
		return SFRT_BUFFER_ERROR(dctx);
162601
162601
	sfrt_driver_set_ectx(
162601
		dctx,0,path);
f46039
47a21b
	if (sfrt_map_input(dctx,-1,path,PROT_READ,&ctx->map))
162601
		return sfrt_free_unit_ctx_impl(ctx,
162601
			SFRT_NESTED_ERROR(dctx));
f46039
f46039
	ctx->path	= path;
f46039
f46039
	ctx->uctx.path	= &ctx->path;
f46039
	ctx->uctx.map	= &ctx->map;
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
}