Blame src/driver/amgc_unit_ctx.c

383aa6
/**********************************************************/
383aa6
/*  apimagic: cparser-based API normalization utility     */
383aa6
/*  Copyright (C) 2015--2016  Z. Gilboa                   */
383aa6
/*  Released under GPLv2 and GPLv3; see COPYING.APIMAGIC. */
383aa6
/**********************************************************/
383aa6
383aa6
#include <stdint.h>
383aa6
#include <stddef.h>
383aa6
#include <stdlib.h>
383aa6
#include <string.h>
383aa6
#include <sys/mman.h>
383aa6
383aa6
#include <apimagic/apimagic.h>
383aa6
#include "apimagic_driver_impl.h"
383aa6
383aa6
static int amgc_free_unit_ctx_impl(struct amgc_unit_ctx_impl * ctx, int status)
383aa6
{
383aa6
	if (ctx) {
383aa6
		amgc_unmap_input(&ctx->map);
383aa6
		free(ctx);
383aa6
	}
383aa6
383aa6
	return status;
383aa6
}
383aa6
383aa6
int amgc_get_unit_ctx(
383aa6
	const struct amgc_driver_ctx *	dctx,
383aa6
	const char *			path,
383aa6
	struct amgc_unit_ctx **		pctx)
383aa6
{
383aa6
	struct amgc_unit_ctx_impl *	ctx;
383aa6
383aa6
	if (!dctx || !(ctx = calloc(sizeof(*ctx),1)))
383aa6
		return -1;
383aa6
383aa6
	if (amgc_map_input(-1,path,PROT_READ,&ctx->map))
383aa6
		return amgc_free_unit_ctx_impl(ctx,-1);
383aa6
383aa6
	memcpy(&ctx->cctx,dctx->cctx,
383aa6
		sizeof(ctx->cctx));
383aa6
383aa6
	ctx->path	= path;
383aa6
383aa6
	ctx->uctx.path	= &ctx->path;
383aa6
	ctx->uctx.map	= &ctx->map;
383aa6
	ctx->uctx.cctx	= &ctx->cctx;
383aa6
383aa6
	*pctx = &ctx->uctx;
383aa6
	return 0;
383aa6
}
383aa6
383aa6
void amgc_free_unit_ctx(struct amgc_unit_ctx * ctx)
383aa6
{
383aa6
	struct amgc_unit_ctx_impl *	ictx;
383aa6
	uintptr_t			addr;
383aa6
383aa6
	if (ctx) {
383aa6
		addr = (uintptr_t)ctx - offsetof(struct amgc_unit_ctx_impl,uctx);
383aa6
		ictx = (struct amgc_unit_ctx_impl *)addr;
383aa6
		amgc_free_unit_ctx_impl(ictx,0);
383aa6
	}
383aa6
}