Skip to content

Commit adb0772

Browse files
committed
Add plugable memory manager interface.
1 parent ca56089 commit adb0772

18 files changed

+477
-359
lines changed

ChangeLog

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
2012-10-16 Aleksey Demakov <[email protected]>
2+
3+
* include/jit/jit-memory.h: add file that defines pluggable memory
4+
manager interface.
5+
* jit-internal.h, jit/jit-memory.c: add a number of _jit_memory_*
6+
pluggable memory wrapper functions. Replace with these wrappers
7+
all _jit_cache_* calls.
8+
* jit/jit-cache.c (jit_default_memory_manager): add function that
9+
gets memory mamnager plugin interface.
10+
* jit/jit-cache.h: remove file.
11+
* include/jit/jit-context.h:
12+
* jit/jit-context.c (jit_context_set_memory_manager): add function.
13+
114
2012-10-12 Aleksey Demakov <[email protected]>
215

316
* include/jit/jit-util.h, jit/jit-alloc.c (jit_malloc_exec)

include/jit/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ dist_libjitinclude_HEADERS = \
1919
jit-init.h \
2020
jit-insn.h \
2121
jit-intrinsic.h \
22+
jit-memory.h \
2223
jit-meta.h \
2324
jit-objmodel.h \
2425
jit-objmodel-private.h \

include/jit/jit-context.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,26 @@
2222
#define _JIT_CONTEXT_H
2323

2424
#include <jit/jit-common.h>
25+
#include <jit/jit-memory.h>
2526

2627
#ifdef __cplusplus
2728
extern "C" {
2829
#endif
2930

3031
jit_context_t jit_context_create(void) JIT_NOTHROW;
3132
void jit_context_destroy(jit_context_t context) JIT_NOTHROW;
33+
3234
void jit_context_build_start(jit_context_t context) JIT_NOTHROW;
3335
void jit_context_build_end(jit_context_t context) JIT_NOTHROW;
36+
3437
void jit_context_set_on_demand_driver(
3538
jit_context_t context,
3639
jit_on_demand_driver_func driver) JIT_NOTHROW;
40+
41+
void jit_context_set_memory_manager(
42+
jit_context_t context,
43+
jit_memory_manager_t manager) JIT_NOTHROW;
44+
3745
int jit_context_set_meta
3846
(jit_context_t context, int type, void *data,
3947
jit_meta_free_func free_data) JIT_NOTHROW;

include/jit/jit-memory.h

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* jit-memory.h - Memory management.
3+
*
4+
* Copyright (C) 2012 Southern Storm Software, Pty Ltd.
5+
*
6+
* The libjit library is free software: you can redistribute it and/or
7+
* modify it under the terms of the GNU Lesser General Public License
8+
* as published by the Free Software Foundation, either version 2.1 of
9+
* the License, or (at your option) any later version.
10+
*
11+
* The libjit library is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public
17+
* License along with the libjit library. If not, see
18+
* <http://www.gnu.org/licenses/>.
19+
*/
20+
21+
#ifndef _JIT_MEMORY_H
22+
#define _JIT_MEMORY_H
23+
24+
#include <jit/jit-defs.h>
25+
26+
#ifdef __cplusplus
27+
extern "C" {
28+
#endif
29+
30+
/*
31+
* Result values for "_jit_cache_start_function" and "_jit_cache_end_function".
32+
*/
33+
#define JIT_MEMORY_OK 0 /* Function is OK */
34+
#define JIT_MEMORY_RESTART 1 /* Restart is required */
35+
#define JIT_MEMORY_TOO_BIG 2 /* Function is too big for the cache */
36+
#define JIT_MEMORY_ERROR 3 /* Other error */
37+
38+
39+
/* TODO: the proper place for this is jit-def.h and it's going to depend on the platform. */
40+
typedef unsigned int jit_size_t;
41+
42+
typedef void *jit_memory_context_t;
43+
44+
typedef struct jit_memory_manager const* jit_memory_manager_t;
45+
46+
struct jit_memory_manager
47+
{
48+
jit_memory_context_t (*create)(jit_context_t context);
49+
void (*destroy)(jit_memory_context_t memctx);
50+
51+
jit_function_t (*find_function)(jit_memory_context_t memctx, void *pc);
52+
53+
jit_function_t (*alloc_function)(jit_memory_context_t memctx);
54+
void (*free_function)(jit_memory_context_t memctx, jit_function_t func);
55+
56+
int (*start_function)(jit_memory_context_t memctx, jit_function_t func);
57+
int (*end_function)(jit_memory_context_t memctx, int result);
58+
int (*extend_limit)(jit_memory_context_t memctx, int count);
59+
60+
void * (*get_limit)(jit_memory_context_t memctx);
61+
void * (*get_break)(jit_memory_context_t memctx);
62+
void (*set_break)(jit_memory_context_t memctx, void *brk);
63+
64+
void * (*alloc_trampoline)(jit_memory_context_t memctx);
65+
void (*free_trampoline)(jit_memory_context_t memctx, void *ptr);
66+
67+
void * (*alloc_closure)(jit_memory_context_t memctx);
68+
void (*free_closure)(jit_memory_context_t memctx, void *ptr);
69+
70+
void * (*alloc_data)(jit_memory_context_t memctx, jit_size_t size, jit_size_t align);
71+
};
72+
73+
jit_memory_manager_t jit_default_memory_manager(void) JIT_NOTHROW;
74+
75+
#ifdef __cplusplus
76+
}
77+
#endif
78+
79+
#endif /* _JIT_MEMORY_H */

jit/Makefile.am

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ libjit_la_SOURCES = \
1717
jit-bitset.h \
1818
jit-bitset.c \
1919
jit-block.c \
20-
jit-cache.h \
2120
jit-cache.c \
2221
jit-compile.c \
2322
jit-config.h \
@@ -45,6 +44,7 @@ libjit_la_SOURCES = \
4544
jit-interp-opcode.c \
4645
jit-intrinsic.c \
4746
jit-live.c \
47+
jit-memory.c \
4848
jit-meta.c \
4949
jit-opcode-apply.c \
5050
jit-objmodel.c \

jit/jit-apply.c

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#include "jit-internal.h"
2424
#include "jit-apply-rules.h"
2525
#include "jit-apply-func.h"
26-
#include "jit-cache.h"
2726
#if HAVE_STDLIB_H
2827
#include <stdlib.h>
2928
#endif
@@ -872,7 +871,6 @@ void *
872871
jit_closure_create(jit_context_t context, jit_type_t signature, jit_closure_func func, void *user_data)
873872
{
874873
#ifdef jit_closure_size
875-
jit_cache_t cache;
876874
jit_closure_t closure;
877875

878876
/* Validate the parameters */
@@ -881,21 +879,19 @@ jit_closure_create(jit_context_t context, jit_type_t signature, jit_closure_func
881879
return 0;
882880
}
883881

884-
/* Acquire the cache lock while we do this */
885-
jit_mutex_lock(&context->cache_lock);
886-
887-
/* Allocate space for the closure within the context's function cache */
888-
cache = _jit_context_get_cache(context);
889-
if(!cache)
882+
/* Acquire the memory context */
883+
_jit_memory_lock(context);
884+
if(!_jit_memory_ensure(context))
890885
{
891-
jit_mutex_unlock(&context->cache_lock);
886+
_jit_memory_unlock(context);
892887
return 0;
893888
}
894889

895-
closure = (jit_closure_t)_jit_cache_alloc_closure(cache);
890+
/* Allocate memory space for the closure */
891+
closure = (jit_closure_t) _jit_memory_alloc_closure(context);
896892
if(!closure)
897893
{
898-
jit_mutex_unlock(&context->cache_lock);
894+
_jit_memory_unlock(context);
899895
return 0;
900896
}
901897

@@ -905,12 +901,12 @@ jit_closure_create(jit_context_t context, jit_type_t signature, jit_closure_func
905901
closure->func = func;
906902
closure->user_data = user_data;
907903

904+
/* Release the memory context, as we are finished with it */
905+
_jit_memory_unlock(context);
906+
908907
/* Perform a cache flush on the closure's code */
909908
_jit_flush_exec(closure->buf, sizeof(closure->buf));
910909

911-
/* Unlock the cache, as we are finished with it */
912-
jit_mutex_unlock(&context->cache_lock);
913-
914910
/* Return the completed closure to the caller */
915911
return closure;
916912

jit/jit-cache.c

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ See the bottom of this file for documentation on the cache system.
2525
*/
2626

2727
#include "jit-internal.h"
28-
#include "jit-cache.h"
2928
#include "jit-apply-func.h"
3029

3130
#include <stddef.h> /* for offsetof */
@@ -76,7 +75,7 @@ struct jit_cache_page
7675
/*
7776
* Structure of the method cache.
7877
*/
79-
#define JIT_CACHE_DEBUG_SIZE 64
78+
typedef struct jit_cache *jit_cache_t;
8079
struct jit_cache
8180
{
8281
struct jit_cache_page *pages; /* List of pages currently in the cache */
@@ -117,6 +116,8 @@ struct jit_cache
117116
#define SetBlack(node) \
118117
((node)->left = (jit_cache_method_t)(((jit_nuint)(node)->left) & ~((jit_nuint)1)))
119118

119+
void _jit_cache_destroy(jit_cache_t cache);
120+
120121
/*
121122
* Allocate a cache page and add it to the cache.
122123
*/
@@ -517,13 +518,13 @@ _jit_cache_start_function(jit_cache_t cache, jit_function_t func)
517518
/* Bail out if there is a started function already */
518519
if(cache->method)
519520
{
520-
return JIT_CACHE_ERROR;
521+
return JIT_MEMORY_ERROR;
521522
}
522523

523524
/* Bail out if the cache is already full */
524525
if(!cache->free_start)
525526
{
526-
return JIT_CACHE_TOO_BIG;
527+
return JIT_MEMORY_TOO_BIG;
527528
}
528529

529530
/* Save the cache position */
@@ -540,7 +541,7 @@ _jit_cache_start_function(jit_cache_t cache, jit_function_t func)
540541
cache->method->left = 0;
541542
cache->method->right = 0;
542543

543-
return JIT_CACHE_OK;
544+
return JIT_MEMORY_OK;
544545
}
545546

546547
int
@@ -549,17 +550,17 @@ _jit_cache_end_function(jit_cache_t cache, int result)
549550
/* Bail out if there is no started function */
550551
if(!cache->method)
551552
{
552-
return JIT_CACHE_ERROR;
553+
return JIT_MEMORY_ERROR;
553554
}
554555

555556
/* Determine if we ran out of space while writing the function */
556-
if(result != JIT_CACHE_OK || cache->free_start >= cache->free_end)
557+
if(result != JIT_MEMORY_OK || cache->free_start >= cache->free_end)
557558
{
558559
/* Restore the saved cache position */
559560
cache->free_start = cache->prev_start;
560561
cache->free_end = cache->prev_end;
561562
cache->method = 0;
562-
return JIT_CACHE_RESTART;
563+
return JIT_MEMORY_RESTART;
563564
}
564565

565566
/* Update the method region block and then add it to the lookup tree */
@@ -568,7 +569,7 @@ _jit_cache_end_function(jit_cache_t cache, int result)
568569
cache->method = 0;
569570

570571
/* The method is ready to go */
571-
return JIT_CACHE_OK;
572+
return JIT_MEMORY_OK;
572573
}
573574

574575
void *
@@ -797,6 +798,30 @@ _jit_cache_get_function(jit_cache_t cache, void *pc)
797798
return 0;
798799
}
799800

801+
jit_memory_manager_t
802+
jit_default_memory_manager(void)
803+
{
804+
static const struct jit_memory_manager mm = {
805+
&_jit_cache_create,
806+
&_jit_cache_destroy,
807+
&_jit_cache_get_function,
808+
&_jit_cache_alloc_function,
809+
&_jit_cache_free_function,
810+
&_jit_cache_start_function,
811+
&_jit_cache_end_function,
812+
&_jit_cache_extend,
813+
&_jit_cache_get_code_limit,
814+
&_jit_cache_get_code_break,
815+
&_jit_cache_set_code_break,
816+
&_jit_cache_alloc_trampoline,
817+
&_jit_cache_free_trampoline,
818+
&_jit_cache_alloc_closure,
819+
&_jit_cache_free_closure,
820+
&_jit_cache_alloc_data
821+
};
822+
return &mm;
823+
}
824+
800825
/*
801826
802827
Using the cache

0 commit comments

Comments
 (0)