@@ -118,7 +118,8 @@ struct jit_cache
118118/*
119119 * Allocate a cache page and add it to the cache.
120120 */
121- static void AllocCachePage (jit_cache_t cache , int factor )
121+ static void
122+ AllocCachePage (jit_cache_t cache , int factor )
122123{
123124 long num ;
124125 unsigned char * ptr ;
@@ -230,8 +231,8 @@ CacheCompare(jit_cache_t cache, unsigned char *key, jit_cache_method_t node)
230231/*
231232 * Rotate a sub-tree around a specific node.
232233 */
233- static jit_cache_method_t CacheRotate ( jit_cache_t cache , unsigned char * key ,
234- jit_cache_method_t around )
234+ static jit_cache_method_t
235+ CacheRotate ( jit_cache_t cache , unsigned char * key , jit_cache_method_t around )
235236{
236237 jit_cache_method_t child , grandChild ;
237238 int setOnLeft ;
@@ -293,7 +294,8 @@ static jit_cache_method_t CacheRotate(jit_cache_t cache, unsigned char *key,
293294 * Add a method region block to the red-black lookup tree
294295 * that is associated with a method cache.
295296 */
296- static void AddToLookupTree (jit_cache_t cache , jit_cache_method_t method )
297+ static void
298+ AddToLookupTree (jit_cache_t cache , jit_cache_method_t method )
297299{
298300 unsigned char * key = method -> func -> code_start ;
299301 jit_cache_method_t temp ;
@@ -354,27 +356,6 @@ static void AddToLookupTree(jit_cache_t cache, jit_cache_method_t method)
354356 SetBlack (cache -> head .right );
355357}
356358
357- static unsigned char *
358- cache_alloc_data (jit_cache_t cache , unsigned long size , unsigned long align )
359- {
360- unsigned char * ptr ;
361-
362- /* Allocate memory from the top of the free region, so that it
363- does not overlap with the method code being written at the
364- bottom of the free region */
365- ptr = cache -> free_end - size ;
366- ptr = (unsigned char * ) (((jit_nuint ) ptr ) & ~((jit_nuint ) align - 1 ));
367- if (ptr < cache -> free_start )
368- {
369- /* When we aligned the block, it caused an overflow */
370- return 0 ;
371- }
372-
373- /* Allocate the block and return it */
374- cache -> free_end = ptr ;
375- return ptr ;
376- }
377-
378359jit_cache_t
379360_jit_cache_create (long limit , long cache_page_size , int max_page_factor )
380361{
@@ -468,45 +449,51 @@ _jit_cache_destroy(jit_cache_t cache)
468449 jit_free (cache );
469450}
470451
471- int
472- _jit_cache_start_function (jit_cache_t cache , jit_function_t func , int restart_count )
452+ void
453+ _jit_cache_extend (jit_cache_t cache , int count )
473454{
474- /* Bail out if there is a started function already */
455+ /* Compute the page size factor */
456+ int factor = 1 << count ;
457+
458+ /* Bail out if there is a started function */
475459 if (cache -> method )
476460 {
477- return JIT_CACHE_ERROR ;
461+ return ;
478462 }
479463
480- /* Do we need to allocate a new cache page? */
481- if (restart_count > 0 )
464+ /* If we had a newly allocated page then it has to be freed
465+ to let allocate another new page of appropriate size. */
466+ struct jit_cache_page * p = & cache -> pages [cache -> numPages - 1 ];
467+ if ((cache -> free_start == ((unsigned char * )p -> page ))
468+ && (cache -> free_end == (cache -> free_start + cache -> pageSize * p -> factor )))
482469 {
483- /* Compute the page size factor */
484- int factor = 1 << (restart_count - 1 );
470+ jit_free_exec (p -> page , cache -> pageSize * p -> factor );
485471
486- /* If we had a newly allocated page then it has to be freed
487- to let allocate another new page of appropriate size. */
488- struct jit_cache_page * p = & cache -> pages [cache -> numPages - 1 ];
489- if ((cache -> free_start == ((unsigned char * )p -> page ))
490- && (cache -> free_end == (cache -> free_start + cache -> pageSize * p -> factor )))
472+ -- (cache -> numPages );
473+ if (cache -> pagesLeft >= 0 )
491474 {
492- jit_free_exec (p -> page , cache -> pageSize * p -> factor );
493-
494- -- (cache -> numPages );
495- if (cache -> pagesLeft >= 0 )
496- {
497- cache -> pagesLeft += p -> factor ;
498- }
499- cache -> free_start = 0 ;
500- cache -> free_end = 0 ;
475+ cache -> pagesLeft += p -> factor ;
476+ }
477+ cache -> free_start = 0 ;
478+ cache -> free_end = 0 ;
501479
502- if (factor <= p -> factor )
503- {
504- factor = p -> factor << 1 ;
505- }
480+ if (factor <= p -> factor )
481+ {
482+ factor = p -> factor << 1 ;
506483 }
484+ }
507485
508- /* Allocate a new page now */
509- AllocCachePage (cache , factor );
486+ /* Allocate a new page now */
487+ AllocCachePage (cache , factor );
488+ }
489+
490+ int
491+ _jit_cache_start_function (jit_cache_t cache , jit_function_t func )
492+ {
493+ /* Bail out if there is a started function already */
494+ if (cache -> method )
495+ {
496+ return JIT_CACHE_ERROR ;
510497 }
511498
512499 /* Bail out if the cache is already full */
@@ -519,15 +506,18 @@ _jit_cache_start_function(jit_cache_t cache, jit_function_t func, int restart_co
519506 cache -> prev_start = cache -> free_start ;
520507 cache -> prev_end = cache -> free_end ;
521508
522- /* Allocate memory for the method information block */
523- cache -> method = (jit_cache_method_t ) cache_alloc_data (cache ,
524- sizeof (struct jit_cache_method ),
525- JIT_BEST_ALIGNMENT );
509+ /* Allocate memory for the function information block */
510+ cache -> method = (jit_cache_method_t )
511+ _jit_cache_alloc_data (cache ,
512+ sizeof (struct jit_cache_method ),
513+ JIT_BEST_ALIGNMENT );
526514 if (!cache -> method )
527515 {
528516 /* There is insufficient space in this page */
529517 return JIT_CACHE_RESTART ;
530518 }
519+
520+ /* Initialize the function information */
531521 cache -> method -> func = func ;
532522 cache -> method -> func -> code_start = cache -> free_start ;
533523 cache -> method -> func -> code_end = cache -> free_start ;
@@ -591,7 +581,8 @@ _jit_cache_set_code_break(jit_cache_t cache, void *ptr)
591581 {
592582 return ;
593583 }
594- if ((unsigned char * ) ptr > cache -> free_end ) {
584+ if ((unsigned char * ) ptr > cache -> free_end )
585+ {
595586 return ;
596587 }
597588
@@ -615,14 +606,22 @@ _jit_cache_get_code_limit(jit_cache_t cache)
615606void *
616607_jit_cache_alloc_data (jit_cache_t cache , unsigned long size , unsigned long align )
617608{
618- /* Bail out if there is no started function */
619- if (!cache -> method )
609+ unsigned char * ptr ;
610+
611+ /* Get memory from the top of the free region, so that it does not
612+ overlap with the function code possibly being written at the bottom
613+ of the free region */
614+ ptr = cache -> free_end - size ;
615+ ptr = (unsigned char * ) (((jit_nuint ) ptr ) & ~(align - 1 ));
616+ if (ptr < cache -> free_start )
620617 {
618+ /* When we aligned the block, it caused an overflow */
621619 return 0 ;
622620 }
623621
624622 /* Allocate the block and return it */
625- return cache_alloc_data (cache , size , align );
623+ cache -> free_end = ptr ;
624+ return ptr ;
626625}
627626
628627void *
0 commit comments