4 Commits

Author SHA1 Message Date
  Yann Weber 680b4ee1ff Implementing RPN expression re-initialization 4 years ago
  Yann Weber 61e74e7ab2 Adds ifs function implementations (begining) 4 years ago
  Yann Weber 1e99abf3b8 Updated comment + starts ifs headers 4 years ago
  Yann Weber 8bbee65ff8 Add default rpn_if resf function implementation 4 years ago
10 changed files with 400 additions and 57 deletions
  1. 4
    1
      Makefile
  2. 1
    1
      python_rpnexpr.h
  3. 52
    14
      rpn_if.c
  4. 29
    10
      rpn_if.h
  5. 86
    10
      rpn_if_default.c
  6. 22
    16
      rpn_if_default.h
  7. 97
    0
      rpn_ifs.c
  8. 74
    0
      rpn_ifs.h
  9. 30
    5
      rpn_jit.c
  10. 5
    0
      rpn_jit.h

+ 4
- 1
Makefile View File

21
 
21
 
22
 all: .deps pyrpn.so
22
 all: .deps pyrpn.so
23
 
23
 
24
-pyrpn.so: python_pyrpn.o python_rpnexpr.o rpn_lib.o rpn_jit.o rpn_parse.o rpn_mutation.o rpn_if.o rpn_if_default.o
24
+pyrpn.so: python_pyrpn.o python_rpnexpr.o rpn_lib.o rpn_jit.o rpn_parse.o rpn_mutation.o rpn_if.o rpn_if_default.o rpn_ifs.o
25
 	$(LD) $(LDFLAGS) $(PYTHON_LDFLAGS) -o $@ $^
25
 	$(LD) $(LDFLAGS) $(PYTHON_LDFLAGS) -o $@ $^
26
 
26
 
27
 python_pyrpn.o: python_pyrpn.c python_rpnexpr.h python_rpnexpr.o rpn_jit.o
27
 python_pyrpn.o: python_pyrpn.c python_rpnexpr.h python_rpnexpr.o rpn_jit.o
45
 rpn_if_default.o: rpn_if_default.c rpn_if_default.h rpn_if.o
45
 rpn_if_default.o: rpn_if_default.c rpn_if_default.h rpn_if.o
46
 	$(CC) $(CFLAGS) -c $<
46
 	$(CC) $(CFLAGS) -c $<
47
 
47
 
48
+rpn_ifs.o: rpn_ifs.c rpn_ifs.h rpn_if.o
49
+	$(CC) $(CFLAGS) -c $<
50
+
48
 rpn_lib.o: rpn_lib.asm rpn_lib.h
51
 rpn_lib.o: rpn_lib.asm rpn_lib.h
49
 	$(NASM) $(NASMCFLAGS) -o $@ $<
52
 	$(NASM) $(NASMCFLAGS) -o $@ $<
50
 
53
 

+ 1
- 1
python_rpnexpr.h View File

31
 
31
 
32
 /**@defgroup python_type RPNExpr Python class
32
 /**@defgroup python_type RPNExpr Python class
33
  * @brief Exposed Python class : RPNExpr
33
  * @brief Exposed Python class : RPNExpr
34
- * @ingroup python_ext
34
+ * @ingroup python_module
35
  */
35
  */
36
 
36
 
37
 /**@file python_rpnexpr.h
37
 /**@file python_rpnexpr.h

+ 52
- 14
rpn_if.c View File

18
  */
18
  */
19
 #include "rpn_if.h"
19
 #include "rpn_if.h"
20
 
20
 
21
-rpn_if_t* rpn_if_new(const rpn_if_param_t *params, rpn_expr_t *rpn)
21
+rpn_if_t* rpn_if_new(const rpn_if_param_t *params, rpn_expr_t *rpn,
22
+	rpn_value_t *memmap)
22
 {
23
 {
23
 	rpn_if_t *res;
24
 	rpn_if_t *res;
25
+	size_t i;
24
 	int err;
26
 	int err;
25
 
27
 
26
 	res = malloc(sizeof(rpn_if_t));
28
 	res = malloc(sizeof(rpn_if_t));
29
 		goto error;
31
 		goto error;
30
 	}
32
 	}
31
 	
33
 	
32
-	memcpy(&res->params, params, sizeof(rpn_if_param_t));
34
+	//memcpy(&res->params, params, sizeof(rpn_if_param_t));
35
+	res->params = params;
33
 
36
 
34
-	res->mem = mmap(NULL, params->mem_sz, PROT_READ|PROT_WRITE, MAP_ANON,
35
-		-1, 0);
36
-	if(res->mem == (void*)-1)
37
+	if(memmap)
38
+	{
39
+		res->self_mem = 0;
40
+		res->mem = memmap;
41
+	}
42
+	else
37
 	{
43
 	{
38
-		goto mmap_err;
44
+		res->self_mem = 1;
45
+		res->mem = mmap(NULL, params->mem_sz, PROT_READ|PROT_WRITE, MAP_ANON,
46
+			-1, 0);
47
+		if(res->mem == (void*)-1)
48
+		{
49
+			goto mmap_err;
50
+		}
39
 	}
51
 	}
40
 
52
 
41
 	res->rpn_res = malloc(sizeof(rpn_value_t) *
53
 	res->rpn_res = malloc(sizeof(rpn_value_t) *
46
 	}
58
 	}
47
 	res->rpn_args = &(res->rpn_res[params->rpn_sz]);
59
 	res->rpn_args = &(res->rpn_res[params->rpn_sz]);
48
 
60
 
49
-	res->rpn = malloc(sizeof(rpn_expr_t) * params->rpn_sz);
61
+	res->rpn = malloc(sizeof(rpn_expr_t*) * params->rpn_sz);
50
 	if(!res->rpn)
62
 	if(!res->rpn)
51
 	{
63
 	{
52
 		goto rpn_expr_err;
64
 		goto rpn_expr_err;
53
 	}
65
 	}
66
+	for(i=0; i<params->rpn_sz; i++)
67
+	{
68
+		if(rpn_expr_init(&(res->rpn[i]), params->rpn_stack_sz,
69
+				params->rpn_argc) < 0)
70
+		{
71
+			goto rpn_init_error;
72
+		}
73
+	}
54
 
74
 
55
 	return res;
75
 	return res;
56
 
76
 
77
+	rpn_init_error:
78
+		err = errno;
79
+		while(i)
80
+		{
81
+			rpn_expr_close(&(res->rpn[i]));
82
+			i--;
83
+		}
57
 	rpn_expr_err:
84
 	rpn_expr_err:
58
 		err = errno;
85
 		err = errno;
59
 		free(res->rpn_res);
86
 		free(res->rpn_res);
60
 	rpn_malloc_err:
87
 	rpn_malloc_err:
61
 		err = errno;
88
 		err = errno;
62
-		munmap(res->mem, params->mem_sz);
89
+		if(res->self_mem)
90
+		{
91
+			munmap(res->mem, params->mem_sz);
92
+		}
63
 	mmap_err:
93
 	mmap_err:
64
 		err = errno;
94
 		err = errno;
65
 		free(res);
95
 		free(res);
72
 void rpn_if_free(rpn_if_t* rif)
102
 void rpn_if_free(rpn_if_t* rif)
73
 {
103
 {
74
 	size_t i;
104
 	size_t i;
75
-	for(i=0; i<rif->params.rpn_sz; i++)
105
+	for(i=0; i<rif->params->rpn_sz; i++)
76
 	{
106
 	{
77
 		rpn_expr_close(&(rif->rpn[i]));
107
 		rpn_expr_close(&(rif->rpn[i]));
78
 	}
108
 	}
79
 	free(rif->rpn);
109
 	free(rif->rpn);
80
 	free(rif->rpn_res);
110
 	free(rif->rpn_res);
81
-	munmap(rif->mem, rif->params.mem_sz);
111
+	if(rif->self_mem)
112
+	{
113
+		munmap(rif->mem, rif->params->mem_sz);
114
+	}
82
 	free(rif);
115
 	free(rif);
83
 }
116
 }
84
 
117
 
86
 {
119
 {
87
 	size_t i;
120
 	size_t i;
88
 	size_t newpos;
121
 	size_t newpos;
89
-	rif->params.arg_f(rif, pos, rif->rpn_args);
90
-	for(i=0; i<rif->params.rpn_sz; i++)
122
+	rif->params->arg_f(rif, pos, rif->rpn_args);
123
+	/* WRONG ! rif->rpn_args is in rif structure and do not have to be
124
+	   given as argument... */
125
+	for(i=0; i<rif->params->rpn_sz; i++)
91
 	{
126
 	{
92
 		rif->rpn_res[i] = rpn_expr_eval(&(rif->rpn[i]), rif->rpn_args);
127
 		rif->rpn_res[i] = rpn_expr_eval(&(rif->rpn[i]), rif->rpn_args);
93
 	}
128
 	}
94
-	rif->params.res_f(rif, &newpos, rif->rpn_res);
129
+	//rif->params->res_f(rif, &newpos, rif->rpn_res);
130
+	/* MEGA WRONG ! rif->rpn_res is in rif structure and do not have to be
131
+	   given as argument... */
132
+	rif->params->res_f(rif, &newpos, NULL);
95
 	return newpos;
133
 	return newpos;
96
 }
134
 }
97
 
135
 
98
 int rpn_if_rpn_upd(rpn_if_t *rif, rpn_tokenized_t *rpns)
136
 int rpn_if_rpn_upd(rpn_if_t *rif, rpn_tokenized_t *rpns)
99
 {
137
 {
100
-	return rpn_if_rpn_upd_rng(rif, rpns, rif->params.rpn_sz, 0);
138
+	return rpn_if_rpn_upd_rng(rif, rpns, rif->params->rpn_sz, 0);
101
 }
139
 }
102
 
140
 
103
 int rpn_if_rpn_upd_rng(rpn_if_t *rif, rpn_tokenized_t *rpns, size_t sz,
141
 int rpn_if_rpn_upd_rng(rpn_if_t *rif, rpn_tokenized_t *rpns, size_t sz,

+ 29
- 10
rpn_if.h View File

24
 #include "rpn_jit.h"
24
 #include "rpn_jit.h"
25
 
25
 
26
 /**@defgroup ifs_if Iterated function
26
 /**@defgroup ifs_if Iterated function
27
+ * @ingroup ifs
27
  * @brief Iterated RPN expression
28
  * @brief Iterated RPN expression
28
  *
29
  *
29
  * A single Iterated Function implemented using an RPN expression.
30
  * A single Iterated Function implemented using an RPN expression.
51
 	/**@brief Number of arguments expected by RPN expressions */
52
 	/**@brief Number of arguments expected by RPN expressions */
52
 	size_t rpn_argc;
53
 	size_t rpn_argc;
53
 
54
 
54
-	/**@brief Position to RPN argument transformation */
55
+	/**@brief Sizeof RPN expression stacks */
56
+	unsigned char rpn_stack_sz;
57
+
58
+	/**@brief Set RPN arguments given a position
59
+	 * @note transform position to arguments and fetch other arguments
60
+	 * from memory map*/
55
 	int (*arg_f)(rpn_if_t *rif, size_t pos, rpn_value_t *args);
61
 	int (*arg_f)(rpn_if_t *rif, size_t pos, rpn_value_t *args);
56
 	/**@brief RPN results to data and position transformation
62
 	/**@brief RPN results to data and position transformation
57
 	 * @note set memory maps with converted data */
63
 	 * @note set memory maps with converted data */
65
 struct rpn_if_s
71
 struct rpn_if_s
66
 {
72
 {
67
 	/**@brief IF parameters */
73
 	/**@brief IF parameters */
68
-	rpn_if_param_t params;
74
+	const rpn_if_param_t *params;
69
 	/**@brief RPN expression(s) pointer(s) */
75
 	/**@brief RPN expression(s) pointer(s) */
70
 	rpn_expr_t *rpn;
76
 	rpn_expr_t *rpn;
71
 	/**@brief RPN expression(s) result(s) buffer */
77
 	/**@brief RPN expression(s) result(s) buffer */
74
 	rpn_value_t *rpn_args;
80
 	rpn_value_t *rpn_args;
75
 	/**@brief Memory map in wich data are fetch & stored */
81
 	/**@brief Memory map in wich data are fetch & stored */
76
 	rpn_value_t *mem;
82
 	rpn_value_t *mem;
83
+	/**@brief If 1 the mmap is called at initialization time, munmap
84
+	 * should be called by @ref rpn_if_free */
85
+	short self_mem;
77
 };
86
 };
78
 
87
 
79
-#define rpn_if_getitem(rif, pos) (rif->mem + (rif->params.value_sz * pos))
88
+/**@brief Macro fetching a memory pointer given a position
89
+ * @return rpn_value_t* values
90
+ */
91
+#define rpn_if_getitem(rif, pos) (rif->mem + ((rif->params->value_sz) * pos))
80
 
92
 
81
-/**@brief Alloc a new @ref rpn_if_s from two transformation functions
93
+/**@brief Alloc a new @ref rpn_if_s using given parameters
82
  * @param params IF parameters
94
  * @param params IF parameters
83
  * @param rpn list of RPN expresions of params->rpn_sz size
95
  * @param rpn list of RPN expresions of params->rpn_sz size
96
+ * @param memmap A suitable memory map. If NULL given, a new mmap is used
84
  * @return A pointer on an allocated @ref rpn_if_s
97
  * @return A pointer on an allocated @ref rpn_if_s
98
+ * @todo Delete rpn argument : rpn expressions are initialized and
99
+ * have to be compiled later
85
  */
100
  */
86
-rpn_if_t* rpn_if_new(const rpn_if_param_t *params, rpn_expr_t *rpn);
101
+rpn_if_t* rpn_if_new(const rpn_if_param_t *params, rpn_expr_t *rpn,
102
+	rpn_value_t *memmap);
87
 
103
 
88
-/**@brief Deallocate an @ref rpn_ifs_s and its ressources and close associated 
104
+/**@brief Deallocate an @ref rpn_if_s and its ressources and close associated 
89
  * @ref rpn_expr_s
105
  * @ref rpn_expr_s
90
  * @param rif The IF to deallocate
106
  * @param rif The IF to deallocate
91
  */
107
  */
100
 
116
 
101
 /**@brief Update all RPN expressions
117
 /**@brief Update all RPN expressions
102
  * @param rif The concerned IF
118
  * @param rif The concerned IF
103
- * @param rpn A list of tokenized expressions (must be of rif->rpn_sz size)
119
+ * @param rpns A list of tokenized expressions (must be of rif->rpn_sz size)
104
  * @return 0 if no error else -1
120
  * @return 0 if no error else -1
105
  * @note Shortcut for @ref rpn_if_rpn_upd_rng(rif, rpns, rif->rpn_sz, 0);
121
  * @note Shortcut for @ref rpn_if_rpn_upd_rng(rif, rpns, rif->rpn_sz, 0);
106
  */
122
  */
108
 
124
 
109
 /**@brief Update a range of RPN expressions
125
 /**@brief Update a range of RPN expressions
110
  * @param rif The concerned IF
126
  * @param rif The concerned IF
111
- * @param rpn A list of tokenized expressions
127
+ * @param rpns A list of tokenized expressions
112
  * @param sz Number of rpn expression in rpn argument
128
  * @param sz Number of rpn expression in rpn argument
113
  * @param offset Start updating expressions from this offset
129
  * @param offset Start updating expressions from this offset
114
  * @return 0 if no error else -1
130
  * @return 0 if no error else -1
118
 
134
 
119
 /**@brief New @ref rpn_if_s and partial initialisation
135
 /**@brief New @ref rpn_if_s and partial initialisation
120
  * @param mem_sz memory size in bytes
136
  * @param mem_sz memory size in bytes
121
- * @param argc number of arguments taken by @ref rpn_expr_s
137
+ * @param rpn_argc number of arguments taken by @ref rpn_expr_s
122
  * @param rpn_count number of @ref rpn_expr_s
138
  * @param rpn_count number of @ref rpn_expr_s
139
+ * @param value_sz the count of rpn_value_t in one memory map value
140
+ * @return a pointer on the initialized rif
123
  */
141
  */
124
 rpn_if_t* _rpn_if_new(size_t mem_sz, size_t rpn_argc, size_t rpn_count,
142
 rpn_if_t* _rpn_if_new(size_t mem_sz, size_t rpn_argc, size_t rpn_count,
125
 	size_t value_sz);
143
 	size_t value_sz);
126
 
144
 
127
-/**@page doc_ifs
145
+/**@page doc_ifs Iterated function system
146
+ * @brief Iterated function system using RPN
128
  *
147
  *
129
  * Iterated functions system are a composed of Iterated function choosed
148
  * Iterated functions system are a composed of Iterated function choosed
130
  * randomly.
149
  * randomly.

+ 86
- 10
rpn_if_default.c View File

7
 	rpn_if_default_data_t *data;
7
 	rpn_if_default_data_t *data;
8
 	rpn_value_t *values;
8
 	rpn_value_t *values;
9
 
9
 
10
-	data = (rpn_if_default_data_t*)rif->params.data;
10
+	data = (rpn_if_default_data_t*)rif->params->data;
11
 
11
 
12
 	switch(data->pos_flag)
12
 	switch(data->pos_flag)
13
 	{
13
 	{
24
 			cur_arg = *(data->size_lim);
24
 			cur_arg = *(data->size_lim);
25
 			break;
25
 			break;
26
 	}
26
 	}
27
-	if(cur_arg >= rif->params.rpn_argc)
27
+	if(cur_arg > rif->params->rpn_argc)
28
 	{
28
 	{
29
 		/* Too many arguments for given rif !! */
29
 		/* Too many arguments for given rif !! */
30
 		return -1;
30
 		return -1;
50
 			}
50
 			}
51
 			break;
51
 			break;
52
 		case RPN_IF_RES_XFUN:
52
 		case RPN_IF_RES_XFUN:
53
-			for(i=0; i < rif->params.rpn_sz - cur_arg; i++)
53
+			for(i=0; i < rif->params->rpn_sz - cur_arg; i++)
54
 			{
54
 			{
55
 				args[cur_arg+i] = values[i];
55
 				args[cur_arg+i] = values[i];
56
 			}
56
 			}
57
 			break;
57
 			break;
58
 		default:
58
 		default:
59
+			/* LOG ERROR */
59
 			return -1;
60
 			return -1;
60
 	}
61
 	}
61
 	return -1;
62
 	return -1;
62
 }
63
 }
63
 
64
 
64
-int rpn_if_resf_default(rpn_if_t *rif, size_t *pos, rpn_value_t *data);
65
+int rpn_if_resf_default(rpn_if_t *rif, size_t *pos, rpn_value_t *res)
66
+{
67
+	rpn_if_default_data_t *data;
68
+	size_t cur_arg, i, rgb_imax;
69
+	rpn_value_t *values;
70
+
71
+	data = (rpn_if_default_data_t*)rif->params->data;
72
+
73
+	switch(data->pos_flag)
74
+	{
75
+		case RPN_IF_POSITION_LINEAR:
76
+			rpn_if_resf_linear(rif, pos, res);
77
+			cur_arg = 1;
78
+			break;
79
+		case RPN_IF_POSITION_XY:
80
+			rpn_if_resf_xy(rif, pos, res);
81
+			cur_arg = 2;
82
+			break;
83
+		case RPN_IF_POSITION_XDIM:
84
+			rpn_if_resf_xdim(rif, pos, res);
85
+			cur_arg = *(data->size_lim);
86
+			break;
87
+	}
88
+	if(cur_arg > rif->params->rpn_argc)
89
+	{
90
+		/** LOG ERROR ! should never append... */
91
+		return -1;
92
+	}
93
+	rgb_imax = 3; /* rgba */
94
+	values = rpn_if_getitem(rif, *pos);
95
+	/**@todo if(res) set the values in res too ! */
96
+	switch(data->res_flag)
97
+	{
98
+		case RPN_IF_RES_BOOL:
99
+			*values = 1;
100
+			break;
101
+
102
+		case RPN_IF_RES_CONST:
103
+			*values = *(data->const_val);
104
+			break;
105
+
106
+		case RPN_IF_RES_COUNT:
107
+			(*values)++;
108
+			break;
109
+
110
+		case RPN_IF_RES_CONST_RGBA:
111
+			rgb_imax = 4;
112
+		//case RPN_IF_RES_CONST_RGB:
113
+			for(i=0;i<rgb_imax;i++)
114
+			{
115
+				values[i] = data->const_val[i];
116
+			}
117
+			break;
118
+
119
+		case RPN_IF_RES_RGBA:
120
+			rgb_imax = 4;
121
+		case RPN_IF_RES_RGB:
122
+			for(i=0;i<rgb_imax;i++)
123
+			{
124
+				values[i] = rif->rpn_res[cur_arg+i];
125
+			}
126
+			break;
127
+
128
+		case RPN_IF_RES_XFUN:
129
+			for(i=0; i<rif->params->rpn_sz - cur_arg; i++)
130
+			{
131
+				values[i] = rif->rpn_res[cur_arg+i];
132
+			}
133
+			break;
134
+
135
+		default:
136
+			/* LOG ERROR */
137
+			return -1;
138
+	}
139
+	return 0;
140
+}
65
 
141
 
66
 int rpn_if_argf_linear(rpn_if_t *rif, size_t pos, rpn_value_t *args)
142
 int rpn_if_argf_linear(rpn_if_t *rif, size_t pos, rpn_value_t *args)
67
 {
143
 {
68
 	rpn_if_default_data_t *data;
144
 	rpn_if_default_data_t *data;
69
-	data = (rpn_if_default_data_t*)rif->params.data;
145
+	data = (rpn_if_default_data_t*)rif->params->data;
70
 	if(data->size_lim && pos >= *data->size_lim)
146
 	if(data->size_lim && pos >= *data->size_lim)
71
 	{
147
 	{
72
 		if(data->pos_flag & RPN_IF_POSITION_OF_ERR)
148
 		if(data->pos_flag & RPN_IF_POSITION_OF_ERR)
83
 {
159
 {
84
 	rpn_if_default_data_t *data;
160
 	rpn_if_default_data_t *data;
85
 	size_t res;
161
 	size_t res;
86
-	data = (rpn_if_default_data_t*)rif->params.data;
162
+	data = (rpn_if_default_data_t*)rif->params->data;
87
 	res = rif->rpn_res[0];
163
 	res = rif->rpn_res[0];
88
 	if(data->size_lim && res >= *data->size_lim)
164
 	if(data->size_lim && res >= *data->size_lim)
89
 	{
165
 	{
101
 {
177
 {
102
 	rpn_if_default_data_t *data;
178
 	rpn_if_default_data_t *data;
103
 
179
 
104
-	data = (rpn_if_default_data_t*)rif->params.data;
180
+	data = (rpn_if_default_data_t*)rif->params->data;
105
 	if(!data->size_lim)
181
 	if(!data->size_lim)
106
 	{
182
 	{
107
 		return -1;
183
 		return -1;
124
 	rpn_if_default_data_t *data;
200
 	rpn_if_default_data_t *data;
125
 	size_t xy[2];
201
 	size_t xy[2];
126
 
202
 
127
-	data = (rpn_if_default_data_t*)rif->params.data;
203
+	data = (rpn_if_default_data_t*)rif->params->data;
128
 	
204
 	
129
 	xy[0] = rif->rpn_res[0];
205
 	xy[0] = rif->rpn_res[0];
130
 	xy[1] = rif->rpn_res[1];
206
 	xy[1] = rif->rpn_res[1];
152
 	rpn_if_default_data_t *data;
228
 	rpn_if_default_data_t *data;
153
 	size_t i, curdim_sz, curpos;
229
 	size_t i, curdim_sz, curpos;
154
 
230
 
155
-	data = (rpn_if_default_data_t*)rif->params.data;
231
+	data = (rpn_if_default_data_t*)rif->params->data;
156
 
232
 
157
 	if(!data->size_lim)
233
 	if(!data->size_lim)
158
 	{
234
 	{
189
 	rpn_if_default_data_t *data;
265
 	rpn_if_default_data_t *data;
190
 	size_t i, res, cur, curlim, prevlim;
266
 	size_t i, res, cur, curlim, prevlim;
191
 
267
 
192
-	data = (rpn_if_default_data_t*)rif->params.data;
268
+	data = (rpn_if_default_data_t*)rif->params->data;
193
 	res = 0;
269
 	res = 0;
194
 	
270
 	
195
 	if(!data->size_lim)
271
 	if(!data->size_lim)

+ 22
- 16
rpn_if_default.h View File

20
 #define __rpn_if_default__h__
20
 #define __rpn_if_default__h__
21
 
21
 
22
 /**@file rpn_if_default.h Defines default IF
22
 /**@file rpn_if_default.h Defines default IF
23
+ * @ingroup ifs_if_default
23
  * @brief Default IF definitions
24
  * @brief Default IF definitions
24
  */
25
  */
26
+
25
 /**@defgroup ifs_if_default Default functions
27
 /**@defgroup ifs_if_default Default functions
28
+ * @ingroup ifs_if
26
  * @brief Simple iterated functions functions
29
  * @brief Simple iterated functions functions
30
+ *
31
+ * Defines default @ref rpn_if_param_s.res_f and @ref rpn_if_param_s.arg_f
32
+ * functions.
27
  */
33
  */
28
 
34
 
29
 #include "config.h"
35
 #include "config.h"
64
 	size_t *size_lim;
70
 	size_t *size_lim;
65
 
71
 
66
 	/**@brief Store constant values to set mem giver res_flag
72
 	/**@brief Store constant values to set mem giver res_flag
67
-	 * - For @ref RPN_IF_CONST_RGBA const_val points on a single value
73
+	 * - For @ref RPN_IF_RES_CONST_RGBA const_val points on a single value
68
 	 * - For @ref RPN_IF_RES_CONST_RGBA const_val points on 4 values
74
 	 * - For @ref RPN_IF_RES_CONST_RGBA const_val points on 4 values
69
 	 */
75
 	 */
70
 	rpn_value_t *const_val;
76
 	rpn_value_t *const_val;
74
 int rpn_if_resf_default(rpn_if_t *rif, size_t *pos, rpn_value_t *data);
80
 int rpn_if_resf_default(rpn_if_t *rif, size_t *pos, rpn_value_t *data);
75
 
81
 
76
 /**@brief Set the first expression argument from position
82
 /**@brief Set the first expression argument from position
77
- * @params rif Expressions
78
- * @params pos Memory map offset
79
- * @params args pointer on expressions arguments
83
+ * @param rif Expressions
84
+ * @param pos Memory map offset
85
+ * @param args pointer on expressions arguments
80
  * @note Other arguments are set using the generic @ref rpn_if_argf_default
86
  * @note Other arguments are set using the generic @ref rpn_if_argf_default
81
  * function
87
  * function
82
  */
88
  */
83
 int rpn_if_argf_linear(rpn_if_t *rif, size_t pos, rpn_value_t *args);
89
 int rpn_if_argf_linear(rpn_if_t *rif, size_t pos, rpn_value_t *args);
84
 /**@brief Transform 1st expression result to position 
90
 /**@brief Transform 1st expression result to position 
85
- * @params rif Expressions
86
- * @params pos Pointer on resulting position in memory map
87
- * @params data Pointer on resulting data
91
+ * @param rif Expressions
92
+ * @param pos Pointer on resulting position in memory map
93
+ * @param data Pointer on resulting data
88
  * @note Data from position fecth is done by generic @ref rpn_if_resf_default
94
  * @note Data from position fecth is done by generic @ref rpn_if_resf_default
89
  * function
95
  * function
90
  */
96
  */
91
 int rpn_if_resf_linear(rpn_if_t *rif, size_t *pos, rpn_value_t *data);
97
 int rpn_if_resf_linear(rpn_if_t *rif, size_t *pos, rpn_value_t *data);
92
 
98
 
93
 /**@brief Set the 1st & 2nd argument from position
99
 /**@brief Set the 1st & 2nd argument from position
94
- * @params rif Expressions
95
- * @params pos Memory map offset
96
- * @params args pointer on expression arguments
100
+ * @param rif Expressions
101
+ * @param pos Memory map offset
102
+ * @param args pointer on expression arguments
97
  * @note Other arguments are set using the generic @ref rpn_if_argf_default
103
  * @note Other arguments are set using the generic @ref rpn_if_argf_default
98
  * function
104
  * function
99
  */
105
  */
100
 int rpn_if_argf_xy(rpn_if_t *rif, size_t pos, rpn_value_t *args);
106
 int rpn_if_argf_xy(rpn_if_t *rif, size_t pos, rpn_value_t *args);
101
 /**@brief Transform 1st and 2nd result into a memory map's offset
107
 /**@brief Transform 1st and 2nd result into a memory map's offset
102
- * @params rif Expressions
108
+ * @param rif Expressions
103
  * @param pos Memory map offset pointer
109
  * @param pos Memory map offset pointer
104
- * @params data Pointer on resulting data
110
+ * @param data Pointer on resulting data
105
  * @note Data from position fetch is done by generic @ref rpn_if_resf_default
111
  * @note Data from position fetch is done by generic @ref rpn_if_resf_default
106
  * function
112
  * function
107
  */
113
  */
108
 int rpn_if_resf_xy(rpn_if_t *rif, size_t *pos, rpn_value_t *data);
114
 int rpn_if_resf_xy(rpn_if_t *rif, size_t *pos, rpn_value_t *data);
109
 
115
 
110
 /**@brief Set X first arguments from position
116
 /**@brief Set X first arguments from position
111
- * @params rif Expressions
112
- * @params pos Memory map offset
113
- * @params args Pointer on expression arguments
117
+ * @param rif Expressions
118
+ * @param pos Memory map offset
119
+ * @param args Pointer on expression arguments
114
  * @note Other arguments are set using the generic @ref rpn_if_argf_default
120
  * @note Other arguments are set using the generic @ref rpn_if_argf_default
115
  * function
121
  * function
116
  */
122
  */
117
 int rpn_if_argf_xdim(rpn_if_t *rif, size_t pos, rpn_value_t *args);
123
 int rpn_if_argf_xdim(rpn_if_t *rif, size_t pos, rpn_value_t *args);
118
 /**@brief Transform X arguments into a memory map's offset
124
 /**@brief Transform X arguments into a memory map's offset
119
- * @params rif Expressions
125
+ * @param rif Expressions
120
  * @param pos Memory map offset pointer
126
  * @param pos Memory map offset pointer
121
  * @param data Pointer on resulting data
127
  * @param data Pointer on resulting data
122
  * @note Data from position fetch is done by generic @ref rpn_if_resf_default
128
  * @note Data from position fetch is done by generic @ref rpn_if_resf_default

+ 97
- 0
rpn_ifs.c View File

1
+#include "rpn_ifs.h"
2
+
3
+rpn_ifs_t* rpn_ifs_new(rpn_if_param_t *params, rpn_value_t *memmap)
4
+{
5
+	rpn_ifs_t *res;
6
+	int err;
7
+
8
+	if(!(res = malloc(sizeof(rpn_ifs_t))))
9
+	{
10
+		goto error;
11
+	}
12
+	bzero(res, sizeof(rpn_ifs_t));
13
+
14
+	memcpy(&(res->params), params, sizeof(rpn_if_param_t));
15
+
16
+	if(memmap)
17
+	{
18
+		res->self_mem = 0;
19
+		res->mem = memmap;
20
+	}
21
+	else
22
+	{
23
+		res->self_mem = 1;
24
+		res->mem = mmap(NULL, params->mem_sz, PROT_READ|PROT_WRITE, MAP_ANON,
25
+			-1, 0);
26
+		if(res->mem == (void*)-1)
27
+		{
28
+			goto mmap_err;
29
+		}
30
+	}
31
+
32
+	return res;
33
+
34
+	mmap_err:
35
+		err = errno;
36
+		free(res);
37
+	error:
38
+		err = errno;
39
+		errno = err;
40
+		return NULL;
41
+}
42
+
43
+void rpn_ifs_free(rpn_ifs_t *rifs)
44
+{
45
+	while(rifs->if_sz)
46
+	{
47
+		rpn_if_free(rifs->rpn_if[--(rifs->if_sz)]);
48
+	}
49
+	if(rifs->self_mem)
50
+	{
51
+		munmap(rifs->mem, rifs->params.mem_sz);
52
+	}
53
+	if(rifs->rpn_if)
54
+	{
55
+		free(rifs->rpn_if);
56
+	}
57
+	if(rifs->weight)
58
+	{
59
+		free(rifs->weight);
60
+	}
61
+}
62
+
63
+size_t rpn_ifs_add_if(rpn_ifs_t *rifs, rpn_expr_t **exprs, unsigned int weight)
64
+{
65
+	size_t res;
66
+	void *tmp;
67
+
68
+	res = rifs->if_sz + 1;
69
+
70
+	if(!(tmp = realloc(rifs->rpn_if, sizeof(rpn_if_t*) * res)))
71
+	{
72
+		return 0;
73
+	}
74
+	rifs->rpn_if = tmp;
75
+
76
+	if(!(tmp = realloc(rifs->weight, sizeof(unsigned int) * res)))
77
+	{
78
+		return 0;
79
+	}
80
+	rifs->weight = tmp;
81
+
82
+	rifs->weight[rifs->if_sz] = weight;
83
+	//WRONG expr ARGUMENT !!!
84
+	rifs->rpn_if[rifs->if_sz] = rpn_if_new(&(rifs->params), *exprs, rifs->mem);
85
+	if(!rifs->rpn_if[rifs->if_sz])
86
+	{
87
+		return 0;
88
+	}
89
+
90
+	rifs->if_sz++;
91
+	if(rpn_ifs_weight_update(rifs) < 0)
92
+	{
93
+		rpn_ifs_del_if(rifs, res); // don't attempt to ceck for errors..
94
+		return 0;
95
+	}
96
+	return res;
97
+}

+ 74
- 0
rpn_ifs.h View File

22
 #include "config.h"
22
 #include "config.h"
23
 
23
 
24
 #include "rpn_jit.h"
24
 #include "rpn_jit.h"
25
+#include "rpn_if.h"
25
 
26
 
26
 /**@defgroup ifs Iterated function system
27
 /**@defgroup ifs Iterated function system
27
  * @brief IFS implementation with RPN expressions
28
  * @brief IFS implementation with RPN expressions
37
  * be able to use both IFS and IF almost tansparently via Python API.
38
  * be able to use both IFS and IF almost tansparently via Python API.
38
  */
39
  */
39
 
40
 
41
+/**@brief struct @ref rpn_ifs_s shortcut */
42
+typedef struct rpn_ifs_s rpn_ifs_t;
43
+
44
+/**@brief Stores IFS informations */
45
+struct rpn_ifs_s
46
+{
47
+	/**@brief Parameters shared by all IF of the system */
48
+	rpn_if_param_t params;
49
+
50
+	/**@brief Memory map shared by all iterated functions */
51
+	rpn_value_t *mem;
52
+	/**@brief If 1 the mem has to be munmap at free time */
53
+	short self_mem;
54
+
55
+	/**@brief Pointers on iterated function structures */
56
+	rpn_if_t **rpn_if;
57
+	/**@brief Number of iterated functions in the system */
58
+	size_t if_sz;
59
+
60
+	/**@brief Current position in memory map */
61
+	size_t pos;
62
+
63
+	/**@brief Stores the original chance of choosing corresponding IF */
64
+	unsigned int *weight;
65
+	/** @brief Stores an array of 255 pointers on IF allowing fast
66
+	 * random choice */
67
+	rpn_if_t *if_proba[255];
68
+};
69
+
70
+/**@brief Initialize a new IFS struct given params
71
+ * @param params Individual, but shared, parameters for iteraed functions
72
+ * in the system
73
+ * @param memmap A suitable memory map or NULL
74
+ * @return NULL if error and set errno
75
+ * @note Initialized system is empty. Use @ref rpn_ifs_add_if to add an iterated
76
+ * function to the system
77
+ */
78
+rpn_ifs_t* rpn_ifs_new(rpn_if_param_t *params, rpn_value_t *memmap);
79
+
80
+/**@brief Free ressources of an iterated function system
81
+ * @param rifs The iterated function system to free
82
+ */
83
+void rpn_ifs_free(rpn_ifs_t *rifs);
84
+
85
+/**@brief Add a new iterated function to the system
86
+ * @param rifs The iterated function system
87
+ * @param exprs Optionnal strings representing RPN expressions for
88
+ * the new @ref rpn_if_s
89
+ * @return 0 on error, else returns the new @ref rpn_if_s index
90
+ * @note if epxrs is NULL empty RPN expressions are used
91
+ * @todo change the exprs argument when if init will be updated
92
+ */
93
+size_t rpn_ifs_add_if(rpn_ifs_t *rifs, rpn_expr_t **exprs, unsigned int weight);
94
+
95
+/**@brief Delete an iterated function from the system given its index
96
+ * @param rifs The iterated function system
97
+ * @param if_idx The iterated function index in the system
98
+ * @return -1 if error else 0
99
+ */
100
+int rpn_ifs_del_if(rpn_ifs_t *rifs, size_t if_idx);
101
+
102
+/**@brief Updates the @ref rpn_ifs_s.if_proba array using
103
+ * @ref rpn_ifs_s.if_proba values
104
+ * @param rifs The iterated function system
105
+ * @return -1 if error else 0
106
+ */
107
+int rpn_ifs_weight_update(rpn_ifs_t *rifs);
108
+
109
+/**@brief Randomly choose an IF and make a step updating ifs current posisition
110
+ * @return -1 on error else 0
111
+ */
112
+int rpn_ifs_step(rpn_ifs_t *rifs);
113
+
40
 #endif
114
 #endif
41
 
115
 

+ 30
- 5
rpn_jit.c View File

58
 	return 0;
58
 	return 0;
59
 }
59
 }
60
 
60
 
61
+int rpn_expr_reinit(rpn_expr_t* expr)
62
+{
63
+#ifdef DEBUG
64
+	if(!expr)
65
+	{
66
+		dprintf(2, "Error, NULL ptr given as expression to rpn_expr_compile");
67
+		errno = EINVAL;
68
+		return -1;
69
+	}
70
+#endif
71
+	bzero(expr->code_map, expr->code_map_sz);
72
+	bzero(expr->stack, sizeof(unsigned long) * stack_sz);
73
+	if(_rpn_expr_init_map(expr) < 0)
74
+	{
75
+		snprintf(expr->err_reason, 128, 
76
+			"Unable to re-init code map : %s", strerror(errno));
77
+		free(expr->expr);
78
+		expr->state = RPN_ERROR;
79
+		return -1;
80
+	}
81
+}
82
+
61
 int rpn_expr_compile(rpn_expr_t *expr, const char *code)
83
 int rpn_expr_compile(rpn_expr_t *expr, const char *code)
62
 {
84
 {
63
 #ifdef DEBUG
85
 #ifdef DEBUG
445
 
467
 
446
 int _rpn_expr_init_map(rpn_expr_t* expr)
468
 int _rpn_expr_init_map(rpn_expr_t* expr)
447
 {
469
 {
448
-	expr->code_map_sz = RPN_MAP_CHUNK;
449
-	expr->code_map = mmap(NULL, expr->code_map_sz, PROT_READ | PROT_WRITE,
450
-		MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
451
-	if(!expr->code_map)
470
+	if(!expr->code_map_sz)
452
 	{
471
 	{
453
-		return -1;
472
+		expr->code_map_sz = RPN_MAP_CHUNK;
473
+		expr->code_map = mmap(NULL, expr->code_map_sz, PROT_READ | PROT_WRITE,
474
+			MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
475
+		if(!expr->code_map)
476
+		{
477
+			return -1;
478
+		}
454
 	}
479
 	}
455
 	expr->code_map_ptr = expr->code_map;
480
 	expr->code_map_ptr = expr->code_map;
456
 	if(CODE_PART_CPY(expr, rpn_exec))
481
 	if(CODE_PART_CPY(expr, rpn_exec))

+ 5
- 0
rpn_jit.h View File

146
 int rpn_expr_init(rpn_expr_t* expr, const unsigned char stack_sz,
146
 int rpn_expr_init(rpn_expr_t* expr, const unsigned char stack_sz,
147
 	  const size_t args_count);
147
 	  const size_t args_count);
148
 
148
 
149
+/**@brief Reinit an existing @ref rpn_expr_s
150
+ * @param expr
151
+ */
152
+int rpn_expr_reinit(rpn_expr_t* expr);
153
+
149
 /**@brief Starts a new initialized expression from an expression string
154
 /**@brief Starts a new initialized expression from an expression string
150
  * @param expr Pointer on an intialized expression ( see @ref rpn_expr_init )
155
  * @param expr Pointer on an intialized expression ( see @ref rpn_expr_init )
151
  * @param code '\0' terminated string representing the RPN expr
156
  * @param code '\0' terminated string representing the RPN expr

Loading…
Cancel
Save