libjit-sys 0.2.1

Just-In-Time Compilation in Rust using LibJIT bindings
Documentation
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
/*
 * jit-cache.c - Translated function cache implementation.
 *
 * Copyright (C) 2002, 2003, 2008  Southern Storm Software, Pty Ltd.
 * Copyright (C) 2012  Aleksey Demakov
 *
 * This file is part of the libjit library.
 *
 * The libjit library is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation, either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * The libjit library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the libjit library.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

/*
See the bottom of this file for documentation on the cache system.
*/

#include "jit-internal.h"
#include "jit-apply-func.h"

#include <stddef.h> /* for offsetof */

#ifdef	__cplusplus
extern	"C" {
#endif

/*
 * Tune the default size of a cache page.  Memory is allocated from
 * the system in chunks of this size.
 */
#ifndef	JIT_CACHE_PAGE_SIZE
#define	JIT_CACHE_PAGE_SIZE		(64 * 1024)
#endif

/*
 * Tune the maximum size of a cache page.  The size of a page might be
 * up to (JIT_CACHE_PAGE_SIZE * JIT_CACHE_MAX_PAGE_FACTOR).  This will
 * also determine the maximum method size that can be translated.
 */
#ifndef JIT_CACHE_MAX_PAGE_FACTOR
#define JIT_CACHE_MAX_PAGE_FACTOR	1024
#endif

/*
 * Method information block, organised as a red-black tree node.
 * There may be more than one such block associated with a method
 * if the method contains exception regions.
 */
typedef struct jit_cache_node *jit_cache_node_t;
struct jit_cache_node
{
	jit_cache_node_t	left;		/* Left sub-tree and red/black bit */
	jit_cache_node_t	right;		/* Right sub-tree */
	unsigned char		*start;		/* Start of the cache region */
	unsigned char		*end;		/* End of the cache region */
	jit_function_t		func;		/* Function info block slot */
};

/*
 * Structure of the page list entry.
 */
struct jit_cache_page
{
	void			*page;		/* Page memory */
	long			factor;		/* Page size factor */
};

/*
 * Structure of the method cache.
 */
typedef struct jit_cache *jit_cache_t;
struct jit_cache
{
	struct jit_cache_page	*pages;		/* List of pages currently in the cache */
	unsigned long		numPages;	/* Number of pages currently in the cache */
	unsigned long		maxNumPages;	/* Maximum number of pages that could be in the list */
	unsigned long		pageSize;	/* Default size of a page for allocation */
	unsigned int		maxPageFactor;	/* Maximum page size factor */
	long			pagesLeft;	/* Number of pages left to allocate */
	unsigned char		*free_start;	/* Current start of the free region */
	unsigned char		*free_end;	/* Current end of the free region */
	unsigned char		*prev_start;	/* Previous start of the free region */
	unsigned char		*prev_end;	/* Previous end of the free region */
	jit_cache_node_t	node;		/* Information for the current function */
	struct jit_cache_node	head;		/* Head of the lookup tree */
	struct jit_cache_node	nil;		/* Nil pointer for the lookup tree */
};

/*
 * Get or set the sub-trees of a node.
 */
#define	GetLeft(node)	\
	((jit_cache_node_t)(((jit_nuint)(node)->left) & ~((jit_nuint)1)))
#define	GetRight(node)	\
	((node)->right)
#define	SetLeft(node,value)	\
	((node)->left = (jit_cache_node_t)(((jit_nuint)value) | \
						(((jit_nuint)(node)->left) & ((jit_nuint)1))))
#define	SetRight(node,value)	\
	((node)->right = (value))

/*
 * Get or set the red/black state of a node.
 */
#define	GetRed(node)	\
	((((jit_nuint)((node)->left)) & ((jit_nuint)1)) != 0)
#define	SetRed(node)	\
	((node)->left = (jit_cache_node_t)(((jit_nuint)(node)->left) | ((jit_nuint)1)))
#define	SetBlack(node)	\
	((node)->left = (jit_cache_node_t)(((jit_nuint)(node)->left) & ~((jit_nuint)1)))

void _jit_cache_destroy(jit_cache_t cache);
void * _jit_cache_alloc_data(jit_cache_t cache, unsigned long size, unsigned long align);

/*
 * Allocate a cache page and add it to the cache.
 */
static void
AllocCachePage(jit_cache_t cache, int factor)
{
	long num;
	unsigned char *ptr;
	struct jit_cache_page *list;

	/* The minimum page factor is 1 */
	if(factor <= 0)
	{
		factor = 1;
	}

	/* If too big a page is requested, then bail out */
	if(((unsigned int) factor) > cache->maxPageFactor)
	{
		goto failAlloc;
	}

	/* If the page limit is hit, then bail out */
	if(cache->pagesLeft >= 0 && cache->pagesLeft < factor)
	{
		goto failAlloc;
	}

	/* Try to allocate a physical page */
	ptr = (unsigned char *) _jit_malloc_exec((unsigned int) cache->pageSize * factor);
	if(!ptr)
	{
		goto failAlloc;
	}

	/* Add the page to the page list.  We keep this in an array
	   that is separate from the pages themselves so that we don't
	   have to "touch" the pages to free them.  Touching the pages
	   may cause them to be swapped in if they are currently out.
	   There's no point doing that if we are trying to free them */
	if(cache->numPages == cache->maxNumPages)
	{
		if(cache->numPages == 0)
		{
			num = 16;
		}
		else
		{
			num = cache->numPages * 2;
		}
		if(cache->pagesLeft > 0 && num > (cache->numPages + cache->pagesLeft - factor + 1))
		{
			num = cache->numPages + cache->pagesLeft - factor + 1;
		}

		list = (struct jit_cache_page *) jit_realloc(cache->pages,
							     sizeof(struct jit_cache_page) * num);
		if(!list)
		{
			_jit_free_exec(ptr, cache->pageSize * factor);
		failAlloc:
			cache->free_start = 0;
			cache->free_end = 0;
			return;
		}

		cache->maxNumPages = num;
		cache->pages = list;
	}
	cache->pages[cache->numPages].page = ptr;
	cache->pages[cache->numPages].factor = factor;
	++(cache->numPages);

	/* Adjust te number of pages left before we hit the limit */
	if(cache->pagesLeft > 0)
	{
		cache->pagesLeft -= factor;
	}

	/* Set up the working region within the new page */
	cache->free_start = ptr;
	cache->free_end = ptr + (int) cache->pageSize * factor;
}

/*
 * Compare a key against a node, being careful of sentinel nodes.
 */
static int
CacheCompare(jit_cache_t cache, unsigned char *key, jit_cache_node_t node)
{
	if(node == &cache->nil || node == &cache->head)
	{
		/* Every key is greater than the sentinel nodes */
		return 1;
	}
	else
	{
		/* Compare a regular node */
		if(key < node->start)
		{
			return -1;
		}
		else if(key > node->start)
		{
			return 1;
		}
		else
		{
			return 0;
		}
	}
}

/*
 * Rotate a sub-tree around a specific node.
 */
static jit_cache_node_t
CacheRotate(jit_cache_t cache, unsigned char *key, jit_cache_node_t around)
{
	jit_cache_node_t child, grandChild;
	int setOnLeft;
	if(CacheCompare(cache, key, around) < 0)
	{
		child = GetLeft(around);
		setOnLeft = 1;
	}
	else
	{
		child = GetRight(around);
		setOnLeft = 0;
	}
	if(CacheCompare(cache, key, child) < 0)
	{
		grandChild = GetLeft(child);
		SetLeft(child, GetRight(grandChild));
		SetRight(grandChild, child);
	}
	else
	{
		grandChild = GetRight(child);
		SetRight(child, GetLeft(grandChild));
		SetLeft(grandChild, child);
	}
	if(setOnLeft)
	{
		SetLeft(around, grandChild);
	}
	else
	{
		SetRight(around, grandChild);
	}
	return grandChild;
}

/*
 * Split a red-black tree at the current position.
 */
#define	Split()		\
			do { \
				SetRed(temp); \
				SetBlack(GetLeft(temp)); \
				SetBlack(GetRight(temp)); \
				if(GetRed(parent)) \
				{ \
					SetRed(grandParent); \
					if((CacheCompare(cache, key, grandParent) < 0) != \
							(CacheCompare(cache, key, parent) < 0)) \
					{ \
						parent = CacheRotate(cache, key, grandParent); \
					} \
					temp = CacheRotate(cache, key, greatGrandParent); \
					SetBlack(temp); \
				} \
			} while (0)

/*
 * Add a method region block to the red-black lookup tree
 * that is associated with a method cache.
 */
static void
AddToLookupTree(jit_cache_t cache, jit_cache_node_t method)
{
	unsigned char *key = method->start;
	jit_cache_node_t temp;
	jit_cache_node_t greatGrandParent;
	jit_cache_node_t grandParent;
	jit_cache_node_t parent;
	jit_cache_node_t nil = &(cache->nil);
	int cmp;

	/* Search for the insert position */
	temp = &(cache->head);
	greatGrandParent = temp;
	grandParent = temp;
	parent = temp;
	while(temp != nil)
	{
		/* Adjust our ancestor pointers */
		greatGrandParent = grandParent;
		grandParent = parent;
		parent = temp;

		/* Compare the key against the current node */
		cmp = CacheCompare(cache, key, temp);
		if(cmp == 0)
		{
			/* This is a duplicate, which normally shouldn't happen.
			   If it does happen, then ignore the node and bail out */
			return;
		}
		else if(cmp < 0)
		{
			temp = GetLeft(temp);
		}
		else
		{
			temp = GetRight(temp);
		}

		/* Do we need to split this node? */
		if(GetRed(GetLeft(temp)) && GetRed(GetRight(temp)))
		{
			Split();
		}
	}

	/* Insert the new node into the current position */
	method->left = (jit_cache_node_t)(((jit_nuint)nil) | ((jit_nuint)1));
	method->right = nil;
	if(CacheCompare(cache, key, parent) < 0)
	{
		SetLeft(parent, method);
	}
	else
	{
		SetRight(parent, method);
	}
	Split();
	SetBlack(cache->head.right);
}

jit_cache_t
_jit_cache_create(jit_context_t context)
{
	jit_cache_t cache;
	long limit, cache_page_size;
	int max_page_factor;
	unsigned long exec_page_size;

	limit = (long)
		jit_context_get_meta_numeric(context, JIT_OPTION_CACHE_LIMIT);
	cache_page_size = (long)
		jit_context_get_meta_numeric(context, JIT_OPTION_CACHE_PAGE_SIZE);
	max_page_factor = (int)
		jit_context_get_meta_numeric(context, JIT_OPTION_CACHE_MAX_PAGE_FACTOR);

	/* Allocate space for the cache control structure */
	if((cache = (jit_cache_t )jit_malloc(sizeof(struct jit_cache))) == 0)
	{
		return 0;
	}

	/* determine the default cache page size */
	exec_page_size = jit_vmem_page_size();
	if(cache_page_size <= 0)
	{
		cache_page_size = JIT_CACHE_PAGE_SIZE;
	}
	if(cache_page_size < exec_page_size)
	{
		cache_page_size = exec_page_size;
	}
	else
	{
		cache_page_size = (cache_page_size / exec_page_size) * exec_page_size;
	}

	/* determine the maximum page size factor */
	if(max_page_factor <= 0)
	{
		max_page_factor = JIT_CACHE_MAX_PAGE_FACTOR;
	}

	/* Initialize the rest of the cache fields */
	cache->pages = 0;
	cache->numPages = 0;
	cache->maxNumPages = 0;
	cache->pageSize = cache_page_size;
	cache->maxPageFactor = max_page_factor;
	cache->free_start = 0;
	cache->free_end = 0;
	if(limit > 0)
	{
		cache->pagesLeft = limit / cache_page_size;
		if(cache->pagesLeft < 1)
		{
			cache->pagesLeft = 1;
		}
	}
	else
	{
		cache->pagesLeft = -1;
	}
	cache->node = 0;
	cache->nil.left = &(cache->nil);
	cache->nil.right = &(cache->nil);
	cache->nil.func = 0;
	cache->head.left = 0;
	cache->head.right = &(cache->nil);
	cache->head.func = 0;

	/* Allocate the initial cache page */
	AllocCachePage(cache, 0);
	if(!cache->free_start)
	{
		_jit_cache_destroy(cache);
		return 0;
	}

	/* Ready to go */
	return cache;
}

void
_jit_cache_destroy(jit_cache_t cache)
{
	unsigned long page;

	/* Free all of the cache pages */
	for(page = 0; page < cache->numPages; ++page)
	{
		_jit_free_exec(cache->pages[page].page,
			       cache->pageSize * cache->pages[page].factor);
	}
	if(cache->pages)
	{
		jit_free(cache->pages);
	}

	/* Free the cache object itself */
	jit_free(cache);
}

int
_jit_cache_extend(jit_cache_t cache, int count)
{
	/* Compute the page size factor */
	int factor = 1 << count;

	/* Bail out if there is a started function */
	if(cache->node)
	{
		return JIT_MEMORY_ERROR;
	}

	/* If we had a newly allocated page then it has to be freed
	   to let allocate another new page of appropriate size. */
	struct jit_cache_page *p = &cache->pages[cache->numPages - 1];
	if((cache->free_start == ((unsigned char *)p->page))
	   && (cache->free_end == (cache->free_start + cache->pageSize * p->factor)))
	{
		_jit_free_exec(p->page, cache->pageSize * p->factor);

		--(cache->numPages);
		if(cache->pagesLeft >= 0)
		{
			cache->pagesLeft += p->factor;
		}
		cache->free_start = 0;
		cache->free_end = 0;

		if(factor <= p->factor)
		{
			factor = p->factor << 1;
		}
	}

	/* Allocate a new page now */
	AllocCachePage(cache, factor);
	if(!cache->free_start)
	{
		return JIT_MEMORY_TOO_BIG;
	}
	return JIT_MEMORY_OK;
}

jit_function_t
_jit_cache_alloc_function(jit_cache_t cache)
{
	return jit_cnew(struct _jit_function);
}

void
_jit_cache_free_function(jit_cache_t cache, jit_function_t func)
{
	jit_free(func);
}

int
_jit_cache_start_function(jit_cache_t cache, jit_function_t func)
{
	/* Bail out if there is a started function already */
	if(cache->node)
	{
		return JIT_MEMORY_ERROR;
	}

	/* Bail out if the cache is already full */
	if(!cache->free_start)
	{
		return JIT_MEMORY_TOO_BIG;
	}

	/* Save the cache position */
	cache->prev_start = cache->free_start;
	cache->prev_end = cache->free_end;

	/* Allocate a new cache node */
	cache->node = _jit_cache_alloc_data(
		cache, sizeof(struct jit_cache_node), sizeof(void *));
	if(!cache->node)
	{
		return JIT_MEMORY_RESTART;
	}
	cache->node->func = func;

	/* Initialize the function information */
	cache->node->start = cache->free_start;
	cache->node->end = 0;
	cache->node->left = 0;
	cache->node->right = 0;

	return JIT_MEMORY_OK;
}

int
_jit_cache_end_function(jit_cache_t cache, int result)
{
	/* Bail out if there is no started function */
	if(!cache->node)
	{
		return JIT_MEMORY_ERROR;
	}

	/* Determine if we ran out of space while writing the function */
	if(result != JIT_MEMORY_OK)
	{
		/* Restore the saved cache position */
		cache->free_start = cache->prev_start;
		cache->free_end = cache->prev_end;
		cache->node = 0;

		return JIT_MEMORY_RESTART;
	}

	/* Update the method region block and then add it to the lookup tree */
	cache->node->end = cache->free_start;
	AddToLookupTree(cache, cache->node);
	cache->node = 0;

	/* The method is ready to go */
	return JIT_MEMORY_OK;
}

void *
_jit_cache_get_code_break(jit_cache_t cache)
{
	/* Bail out if there is no started function */
	if(!cache->node)
	{
		return 0;
	}

	/* Return the address of the available code area */
	return cache->free_start;
}

void
_jit_cache_set_code_break(jit_cache_t cache, void *ptr)
{
	/* Bail out if there is no started function */
	if(!cache->node)
	{
		return;
	}
	/* Sanity checks */
	if((unsigned char *) ptr < cache->free_start)
	{
		return;
	}
	if((unsigned char *) ptr > cache->free_end)
	{
		return;
	}

	/* Update the address of the available code area */
	cache->free_start = ptr;
}

void *
_jit_cache_get_code_limit(jit_cache_t cache)
{
	/* Bail out if there is no started function */
	if(!cache->node)
	{
		return 0;
	}

	/* Return the end address of the available code area */
	return cache->free_end;
}

void *
_jit_cache_alloc_data(jit_cache_t cache, unsigned long size, unsigned long align)
{
	unsigned char *ptr;

	/* Get memory from the top of the free region, so that it does not
	   overlap with the function code possibly being written at the bottom
	   of the free region */
	ptr = cache->free_end - size;
	ptr = (unsigned char *) (((jit_nuint) ptr) & ~(align - 1));
	if(ptr < cache->free_start)
	{
		/* When we aligned the block, it caused an overflow */
		return 0;
	}

	/* Allocate the block and return it */
	cache->free_end = ptr;
	return ptr;
}

static void *
alloc_code(jit_cache_t cache, unsigned int size, unsigned int align)
{
	unsigned char *ptr;

	/* Bail out if there is a started function */
	if(cache->node)
	{
		return 0;
	}
	/* Bail out if there is no cache available */
	if(!cache->free_start)
	{
		return 0;
	}

	/* Allocate aligned memory. */
	ptr = cache->free_start;
	if(align > 1)
	{
		jit_nuint p = ((jit_nuint) ptr + align - 1) & ~(align - 1);
		ptr = (unsigned char *) p;
	}

	/* Do we need to allocate a new cache page? */
	if((ptr + size) > cache->free_end)
	{
		/* Allocate a new page */
		AllocCachePage(cache, 0);

		/* Bail out if the cache is full */
		if(!cache->free_start)
		{
			return 0;
		}

		/* Allocate memory from the new page */
		ptr = cache->free_start;
		if(align > 1)
		{
			jit_nuint p = ((jit_nuint) ptr + align - 1) & ~(align - 1);
			ptr = (unsigned char *) p;
		}
	}

	/* Allocate the block and return it */
	cache->free_start = ptr + size;
	return (void *) ptr;
}

void *
_jit_cache_alloc_trampoline(jit_cache_t cache)
{
	return alloc_code(cache,
			  jit_get_trampoline_size(),
			  jit_get_trampoline_alignment());
}

void
_jit_cache_free_trampoline(jit_cache_t cache, void *trampoline)
{
	/* not supported yet */
}

void *
_jit_cache_alloc_closure(jit_cache_t cache)
{
	return alloc_code(cache,
			  jit_get_closure_size(),
			  jit_get_closure_alignment());
}

void
_jit_cache_free_closure(jit_cache_t cache, void *closure)
{
	/* not supported yet */
}

#if 0
void *
_jit_cache_alloc_no_method(jit_cache_t cache, unsigned long size, unsigned long align)
{
	unsigned char *ptr;

	/* Bail out if there is a started function */
	if(cache->method)
	{
		return 0;
	}
	/* Bail out if there is no cache available */
	if(!cache->free_start)
	{
		return 0;
	}
	/* Bail out if the request is too big to ever be satisfiable */
	if ((size + align - 1) > (cache->pageSize * cache->maxPageFactor))
	{
		return 0;
	}

	/* Allocate memory from the top of the current free region, so
	 * that it does not overlap with the method code being written
	 * at the bottom of the free region */
	ptr = cache->free_end - size;
	ptr = (unsigned char *) (((jit_nuint) ptr) & ~((jit_nuint) align - 1));

	/* Do we need to allocate a new cache page? */
	if(ptr < cache->free_start)
	{
		/* Find the appropriate page size */
		int factor = 1;
		while((size + align - 1) > (factor * cache->pageSize)) {
			factor <<= 1;
		}

		/* Try to allocate it */
		AllocCachePage(cache, factor);

		/* Bail out if the cache is full */
		if(!cache->free_start)
		{
			return 0;
		}

		/* Allocate memory from the new page */
		ptr = cache->free_end - size;
		ptr = (unsigned char *) (((jit_nuint) ptr) & ~((jit_nuint) align - 1));
	}

	/* Allocate the block and return it */
	cache->free_end = ptr;
	return (void *)ptr;
}
#endif

void *
_jit_cache_find_function_info(jit_cache_t cache, void *pc)
{
	jit_cache_node_t node = cache->head.right;
	while(node != &(cache->nil))
	{
		if(((unsigned char *)pc) < node->start)
		{
			node = GetLeft(node);
		}
		else if(((unsigned char *)pc) >= node->end)
		{
			node = GetRight(node);
		}
		else
		{
			return node;
		}
	}
	return 0;
}

jit_function_t
_jit_cache_get_function(jit_cache_t cache, void *func_info)
{
	if (func_info)
	{
		jit_cache_node_t node = (jit_cache_node_t) func_info;
		return node->func;
	}
	return 0;
}

void *
_jit_cache_get_function_start(jit_memory_context_t memctx, void *func_info)
{
	if (func_info)
	{
		jit_cache_node_t node = (jit_cache_node_t) func_info;
		return node->start;
	}
	return 0;
}

void *
_jit_cache_get_function_end(jit_memory_context_t memctx, void *func_info)
{
	if (func_info)
	{
		jit_cache_node_t node = (jit_cache_node_t) func_info;
		return node->end;
	}
	return 0;
}

jit_memory_manager_t
jit_default_memory_manager(void)
{
	static const struct jit_memory_manager mm = {

		(jit_memory_context_t (*)(jit_context_t))
		&_jit_cache_create,

		(void (*)(jit_memory_context_t))
		&_jit_cache_destroy,

		(jit_function_info_t (*)(jit_memory_context_t, void *))
		&_jit_cache_find_function_info,

		(jit_function_t (*)(jit_memory_context_t, jit_function_info_t))
		&_jit_cache_get_function,

		(void * (*)(jit_memory_context_t, jit_function_info_t))
		&_jit_cache_get_function_start,

		(void * (*)(jit_memory_context_t, jit_function_info_t))
		&_jit_cache_get_function_end,

		(jit_function_t (*)(jit_memory_context_t))
		&_jit_cache_alloc_function,

		(void (*)(jit_memory_context_t, jit_function_t))
		&_jit_cache_free_function,

		(int (*)(jit_memory_context_t, jit_function_t))
		&_jit_cache_start_function,

		(int (*)(jit_memory_context_t, int))
		&_jit_cache_end_function,

		(int (*)(jit_memory_context_t, int))
		&_jit_cache_extend,

		(void * (*)(jit_memory_context_t))
		&_jit_cache_get_code_limit,

		(void * (*)(jit_memory_context_t))
		&_jit_cache_get_code_break,

		(void (*)(jit_memory_context_t, void *))
		&_jit_cache_set_code_break,

		(void * (*)(jit_memory_context_t))
		&_jit_cache_alloc_trampoline,

		(void (*)(jit_memory_context_t, void *))
		&_jit_cache_free_trampoline,

		(void * (*)(jit_memory_context_t))
		&_jit_cache_alloc_closure,

		(void (*)(jit_memory_context_t, void *))
		&_jit_cache_free_closure,

		(void * (*)(jit_memory_context_t, jit_size_t, jit_size_t))
		&_jit_cache_alloc_data
	};
	return &mm;
}

/*

Using the cache
---------------

To output the code for a method, first call _jit_cache_start_method:

	jit_cache_posn posn;
	int result;

	result = _jit_cache_start_method(cache, &posn, factor,
					 METHOD_ALIGNMENT, method);

"factor" is used to control cache space allocation for the method.
The cache space is allocated by pages.  The value 0 indicates that
the method has to use the space left after the last allocation.
The value 1 or more indicates that the method has to start on a
newly allocated space that must contain the specified number of
consecutive pages.

"METHOD_ALIGNMENT" is used to align the start of the method on an
appropriate boundary for the target CPU.  Use the value 1 if no
special alignment is required.  Note: this value is a hint to the
cache - it may alter the alignment value.

"method" is a value that uniquely identifies the method that is being
translated.  Usually this is the "jit_function_t" pointer.

The function initializes the "posn" structure to point to the start
and end of the space available for the method output.  The function
returns one of three result codes:

	JIT_CACHE_OK       The function call was successful.
	JIT_CACHE_RESTART  The cache does not currently have enough
	                   space to fit any method.  This code may
			   only be returned if the "factor" value
			   was 0.  In this case it is necessary to
			   restart the method output process by
			   calling _jit_cache_start_method again
			   with a bigger "factor" value.
	JIT_CACHE_TOO_BIG  The cache does not have any space left
	                   for allocation.  In this case a restart
			   won't help.

Some CPU optimization guides recommend that labels should be aligned.
This can be achieved using _jit_cache_align.

Once the method code has been output, call _jit_cache_end_method to finalize
the process.  This function returns one of two result codes:

	JIT_CACHE_OK       The method output process was successful.
	JIT_CACHE_RESTART  The cache space overflowed. It is necessary
	                   to restart the method output process by
			   calling _jit_cache_start_method again
			   with a bigger "factor" value.

The caller should repeatedly translate the method while _jit_cache_end_method
continues to return JIT_CACHE_END_RESTART.  Normally there will be no
more than a single request to restart, but the caller should not rely
upon this.  The cache algorithm guarantees that the restart loop will
eventually terminate.

Cache data structure
--------------------

The cache consists of one or more "cache pages", which contain method
code and auxiliary data.  The default size for a cache page is 64k
(JIT_CACHE_PAGE_SIZE).  The size is adjusted to be a multiple
of the system page size (usually 4k), and then stored in "pageSize".

Method code is written into a cache page starting at the bottom of the
page, and growing upwards.  Auxiliary data is written into a cache page
starting at the top of the page, and growing downwards.  When the two
regions meet, a new cache page is allocated and the process restarts.

To allow methods bigger than a single cache page it is possible to
allocate a block of consecutive pages as a single unit. The method
code and auxiliary data is written to such a multiple-page block in
the same manner as into an ordinary page.

Each method has one or more jit_cache_method auxiliary data blocks associated
with it.  These blocks indicate the start and end of regions within the
method.  Normally these regions correspond to exception "try" blocks, or
regular code between "try" blocks.

The jit_cache_method blocks are organised into a red-black tree, which
is used to perform fast lookups by address (_jit_cache_get_method).  These
lookups are used when walking the stack during exceptions or security
processing.

Each method can also have offset information associated with it, to map
between native code addresses and offsets within the original bytecode.
This is typically used to support debugging.  Offset information is stored
as auxiliary data, attached to the jit_cache_method block.

Threading issues
----------------

Writing a method to the cache, querying a method by address, or querying
offset information for a method, are not thread-safe.  The caller should
arrange for a cache lock to be acquired prior to performing these
operations.

Executing methods from the cache is thread-safe, as the method code is
fixed in place once it has been written.

Note: some CPU's require that a special cache flush instruction be
performed before executing method code that has just been written.
This is especially important in SMP environments.  It is the caller's
responsibility to perform this flush operation.

We do not provide locking or CPU flush capabilities in the cache
implementation itself, because the caller may need to perform other
duties before flushing the CPU cache or releasing the lock.

The following is the recommended way to map an "jit_function_t" pointer
to a starting address for execution:

	Look in "jit_function_t" to see if we already have a starting address.
		If so, then bail out.
	Acquire the cache lock.
	Check again to see if we already have a starting address, just
		in case another thread got here first.  If so, then release
		the cache lock and bail out.
	Translate the method.
	Update the "jit_function_t" structure to contain the starting address.
	Force a CPU cache line flush.
	Release the cache lock.

Why aren't methods flushed when the cache fills up?
---------------------------------------------------

In this cache implementation, methods are never "flushed" when the
cache becomes full.  Instead, all translation stops.  This is not a bug.
It is a feature.

In a multi-threaded environment, it is impossible to know if some
other thread is executing the code of a method that may be a candidate
for flushing.  Impossible that is unless one introduces a huge number
of read-write locks, one per method, to prevent a method from being
flushed.  The read locks must be acquired on entry to a method, and
released on exit.  The write locks are acquired prior to translation.

The overhead of introducing all of these locks and the associated cache
data structures is very high.  The only safe thing to do is to assume
that once a method has been translated, its code must be fixed in place
for all time.

We've looked at the code for other Free Software and Open Source JIT's,
and they all use a constantly-growing method cache.  No one has found
a solution to this problem, it seems.  Suggestions are welcome.

To prevent the cache from chewing up all of system memory, it is possible
to set a limit on how far it will grow.  Once the limit is reached, out
of memory will be reported and there is no way to recover.

*/

#ifdef	__cplusplus
};
#endif