Tor 0.4.9.2-alpha-dev
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
context.c
1/* Copyright (c) 2020 tevador <tevador@gmail.com> */
2/* See LICENSE for licensing information */
3
4#include <stdlib.h>
5#include <equix.h>
6#include <virtual_memory.h>
7#include "context.h"
8#include "solver_heap.h"
9
10equix_ctx* equix_alloc(equix_ctx_flags flags) {
11 equix_ctx* ctx = malloc(sizeof(equix_ctx));
12 if (ctx == NULL) {
13 goto failure;
14 }
15 ctx->flags = (equix_ctx_flags)0;
16
17 if (flags & EQUIX_CTX_MUST_COMPILE) {
18 ctx->hash_func = hashx_alloc(HASHX_TYPE_COMPILED);
19 } else if (flags & EQUIX_CTX_TRY_COMPILE) {
20 ctx->hash_func = hashx_alloc(HASHX_TRY_COMPILE);
21 } else {
22 ctx->hash_func = hashx_alloc(HASHX_TYPE_INTERPRETED);
23 }
24 if (ctx->hash_func == NULL) {
25 goto failure;
26 }
27
28 if (flags & EQUIX_CTX_SOLVE) {
29 if (flags & EQUIX_CTX_HUGEPAGES) {
30#ifdef EQUIX_SUPPORT_HUGEPAGES
31 ctx->heap = hashx_vm_alloc_huge(sizeof(solver_heap));
32#else
33 ctx->heap = NULL;
34#endif
35 }
36 else {
37 ctx->heap = malloc(sizeof(solver_heap));
38 }
39 if (ctx->heap == NULL) {
40 goto failure;
41 }
42 } else {
43 ctx->heap = NULL;
44 }
45
46 ctx->flags = flags;
47 return ctx;
48failure:
49 equix_free(ctx);
50 return NULL;
51}
52
53void equix_free(equix_ctx* ctx) {
54 if (ctx != NULL) {
55 if (ctx->flags & EQUIX_CTX_SOLVE) {
56 if (ctx->flags & EQUIX_CTX_HUGEPAGES) {
57 hashx_vm_free(ctx->heap, sizeof(solver_heap));
58 }
59 else {
60 free(ctx->heap);
61 }
62 }
63 hashx_free(ctx->hash_func);
64 free(ctx);
65 }
66}