Blame src/driver/mdso_unit_ctx.c

cde03b
/****************************************************************/
cde03b
/*  mdso: midipix dso scavenger                                 */
cde03b
/*  Copyright (C) 2015  Z. Gilboa                               */
cde03b
/*  Released under GPLv2 and GPLv3; see COPYING.MDSO.           */
cde03b
/****************************************************************/
cde03b
cde03b
#include <stdint.h>
cde03b
#include <stddef.h>
cde03b
#include <stdlib.h>
cde03b
#include <string.h>
cde03b
#include <sys/mman.h>
cde03b
cde03b
#include <mdso/mdso.h>
cde03b
#include "mdso_driver_impl.h"
cde03b
cde03b
static int mdso_free_unit_ctx_impl(struct mdso_unit_ctx_impl * ctx, int status)
cde03b
{
cde03b
	if (ctx) {
cde03b
		mdso_unmap_input(&ctx->map);
cde03b
		free(ctx);
cde03b
	}
cde03b
cde03b
	return status;
cde03b
}
cde03b
cde03b
int mdso_get_unit_ctx(
cde03b
	const struct mdso_driver_ctx *	dctx,
cde03b
	const char *			path,
cde03b
	struct mdso_unit_ctx **		pctx)
cde03b
{
cde03b
	struct mdso_unit_ctx_impl *	ctx;
cde03b
cde03b
	if (!dctx || !(ctx = calloc(sizeof(*ctx),1)))
cde03b
		return -1;
cde03b
cde03b
	if (mdso_map_input(-1,path,PROT_READ,&ctx->map))
cde03b
		return mdso_free_unit_ctx_impl(ctx,-1);
cde03b
cde03b
	memcpy(&ctx->cctx,dctx->cctx,
cde03b
		sizeof(ctx->cctx));
cde03b
cde03b
	ctx->path	= path;
cde03b
cde03b
	ctx->uctx.path	= &ctx->path;
cde03b
	ctx->uctx.map	= &ctx->map;
cde03b
	ctx->uctx.cctx	= &ctx->cctx;
cde03b
cde03b
	*pctx = &ctx->uctx;
cde03b
	return 0;
cde03b
}
cde03b
cde03b
void mdso_free_unit_ctx(struct mdso_unit_ctx * ctx)
cde03b
{
cde03b
	struct mdso_unit_ctx_impl *	ictx;
cde03b
	uintptr_t			addr;
cde03b
cde03b
	if (ctx) {
cde03b
		addr = (uintptr_t)ctx - offsetof(struct mdso_unit_ctx_impl,uctx);
cde03b
		ictx = (struct mdso_unit_ctx_impl *)addr;
cde03b
		mdso_free_unit_ctx_impl(ictx,0);
cde03b
	}
cde03b
}