summaryrefslogtreecommitdiff
path: root/src/driver/amgc_unit_ctx.c
blob: d2268fddf79e151b828ca2441331982f120a149c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**********************************************************/
/*  apimagic: cparser-based API normalization utility     */
/*  Copyright (C) 2015--2016  Z. Gilboa                   */
/*  Released under GPLv2 and GPLv3; see COPYING.APIMAGIC. */
/**********************************************************/

#include <stdint.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>

#include <apimagic/apimagic.h>
#include "apimagic_driver_impl.h"

static int amgc_free_unit_ctx_impl(struct amgc_unit_ctx_impl * ctx, int status)
{
	if (ctx) {
		amgc_unmap_input(&ctx->map);
		free(ctx);
	}

	return status;
}

int amgc_get_unit_ctx(
	const struct amgc_driver_ctx *	dctx,
	const char *			path,
	struct amgc_unit_ctx **		pctx)
{
	struct amgc_unit_ctx_impl *	ctx;

	if (!dctx || !(ctx = calloc(sizeof(*ctx),1)))
		return -1;

	if (amgc_map_input(-1,path,PROT_READ,&ctx->map))
		return amgc_free_unit_ctx_impl(ctx,-1);

	memcpy(&ctx->cctx,dctx->cctx,
		sizeof(ctx->cctx));

	ctx->path	= path;

	ctx->uctx.path	= &ctx->path;
	ctx->uctx.map	= &ctx->map;
	ctx->uctx.cctx	= &ctx->cctx;

	*pctx = &ctx->uctx;
	return 0;
}

void amgc_free_unit_ctx(struct amgc_unit_ctx * ctx)
{
	struct amgc_unit_ctx_impl *	ictx;
	uintptr_t			addr;

	if (ctx) {
		addr = (uintptr_t)ctx - offsetof(struct amgc_unit_ctx_impl,uctx);
		ictx = (struct amgc_unit_ctx_impl *)addr;
		amgc_free_unit_ctx_impl(ictx,0);
	}
}