summaryrefslogtreecommitdiff
path: root/src/driver/amgc_unit_ctx.c
diff options
context:
space:
mode:
authormidipix <writeonce@midipix.org>2015-12-29 04:48:00 -0500
committermidipix <writeonce@midipix.org>2016-01-01 22:50:23 -0500
commit383aa6559ee06236b7600df8df17d7f3ab254ca3 (patch)
tree1745e2888f440a7f56d54e29ce4a37a486d9d272 /src/driver/amgc_unit_ctx.c
parentd764f2cab0ed73a95f3574f7974ec53a516f3c0d (diff)
downloadapimagic-383aa6559ee06236b7600df8df17d7f3ab254ca3.tar.bz2
apimagic-383aa6559ee06236b7600df8df17d7f3ab254ca3.tar.xz
created skeleton.
Diffstat (limited to 'src/driver/amgc_unit_ctx.c')
-rw-r--r--src/driver/amgc_unit_ctx.c62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/driver/amgc_unit_ctx.c b/src/driver/amgc_unit_ctx.c
new file mode 100644
index 0000000..d2268fd
--- /dev/null
+++ b/src/driver/amgc_unit_ctx.c
@@ -0,0 +1,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);
+ }
+}