[go: up one dir, main page]

File: test.c

package info (click to toggle)
duktape 2.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 20,496 kB
  • sloc: ansic: 203,676; python: 5,856; makefile: 476; cpp: 205
file content (114 lines) | stat: -rw-r--r-- 2,922 bytes parent folder | download | duplicates (4)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <stdio.h>
#include <stdlib.h>
#include "duktape.h"
#include "duk_alloc_pool.h"

void my_fatal(const char *msg) {
	fprintf(stderr, "*** FATAL: %s\n", msg ? msg : "no message");
	fflush(stderr);
	abort();
}

static duk_ret_t my_print(duk_context *ctx) {
	duk_push_string(ctx, " ");
	duk_insert(ctx, 0);
	duk_join(ctx, duk_get_top(ctx) - 1);
	printf("%s\n", duk_safe_to_string(ctx, -1));
	return 1;
}

static void dump_pool_state(duk_pool_global *g) {
	int i;
	long total_size = 0;
	long total_used = 0;

	for (i = 0; i < g->num_pools; i++) {
		duk_pool_state *st = g->states + i;
		int free, used;
		duk_pool_free *f;

		for (free = 0, f = st->first; f; f = f->next) {
			free++;
		}
		used = st->count - free;
		printf("Pool %2d: block size %5d, count %4d/%4d, bytes %6d/%6d\n",
		       i, (int) st->size, used, (int) st->count,
		       (int) st->size * used, (int) st->size * (int) st->count);

		total_size += (long) st->size * (long) st->count;
		total_used += (long) st->size * (long) used;
	}
	printf("=== Total: %ld/%ld, free %ld\n",
	       (long) total_used, (long) total_size, (long) (total_size - total_used));
}

int main(int argc, char *argv[]) {
	duk_context *ctx;
	int i;
	int exitcode = 0;

	/* NOTE! This pool configuration is NOT a good pool configuration
	 * for practical use (and is not intended to be one).  A production
	 * pool configuration should be created using measurements.
	 */
	const duk_pool_config pool_configs[15] = {
		{ 16, 20, 200 },
		{ 20, 40, 100 },
		{ 24, 40, 100 },
		{ 32, 60, 50 },
		{ 40, 60, 50 },
		{ 48, 60, 50 },
		{ 56, 60, 50 },
		{ 64, 60, 50 },
		{ 80, 60, 50 },
		{ 256, 100, 10 },
		{ 1024, 20, 2 },
		{ 2048, 20, 2 },
		{ 4096, 100, 2 },
		{ 6144, 60, 2 },
		{ 8192, 100, 2 },
	};
	duk_pool_state pool_states[15];  /* Count must match pool_configs[]. */
	duk_pool_global pool_global;

	char buffer[200000];
	void *pool_udata;

	pool_udata = duk_alloc_pool_init(buffer, sizeof(buffer), pool_configs, pool_states, sizeof(pool_configs) / sizeof(duk_pool_config), &pool_global);
	if (!pool_udata) {
		return 1;
	}

	printf("Pool after pool init:\n");
	dump_pool_state(&pool_global);

	ctx = duk_create_heap(duk_alloc_pool, duk_realloc_pool, duk_free_pool, pool_udata, NULL);
	if (!ctx) {
		return 1;
	}

	printf("Pool after Duktape heap creation:\n");
	dump_pool_state(&pool_global);

	duk_push_c_function(ctx, my_print, DUK_VARARGS);
	duk_put_global_string(ctx, "print");
	duk_push_c_function(ctx, my_print, DUK_VARARGS);
	duk_put_global_string(ctx, "alert");
	printf("top after init: %ld\n", (long) duk_get_top(ctx));

	for (i = 1; i < argc; i++) {
		printf("Evaling: %s\n", argv[i]);
		if (duk_peval_string(ctx, argv[i]) != 0) {
			exitcode = 1;
		}
		printf("--> %s\n", duk_safe_to_string(ctx, -1));
		duk_pop(ctx);
	}

	printf("Pool after evaling code:\n");
	dump_pool_state(&pool_global);

	printf("Done\n");
	duk_destroy_heap(ctx);
	return exitcode;
}