Browse Source

Updated comment + starts ifs headers

Yann Weber 4 years ago
parent
commit
1e99abf3b8
6 changed files with 172 additions and 42 deletions
  1. 4
    1
      Makefile
  2. 1
    1
      python_rpnexpr.h
  3. 48
    15
      rpn_if.c
  4. 23
    9
      rpn_if.h
  5. 22
    16
      rpn_if_default.h
  6. 74
    0
      rpn_ifs.h

+ 4
- 1
Makefile View File

@@ -21,7 +21,7 @@ PYTHON_LDFLAGS=-shared  -fPIC `$(PYTHON_CONFIG) --libs` `$(PYTHON_CONFIG) --ldfl
21 21
 
22 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 25
 	$(LD) $(LDFLAGS) $(PYTHON_LDFLAGS) -o $@ $^
26 26
 
27 27
 python_pyrpn.o: python_pyrpn.c python_rpnexpr.h python_rpnexpr.o rpn_jit.o
@@ -45,6 +45,9 @@ rpn_if.o: rpn_if.c rpn_if.h rpn_jit.o
45 45
 rpn_if_default.o: rpn_if_default.c rpn_if_default.h rpn_if.o
46 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 51
 rpn_lib.o: rpn_lib.asm rpn_lib.h
49 52
 	$(NASM) $(NASMCFLAGS) -o $@ $<
50 53
 

+ 1
- 1
python_rpnexpr.h View File

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

+ 48
- 15
rpn_if.c View File

@@ -18,9 +18,11 @@
18 18
  */
19 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 24
 	rpn_if_t *res;
25
+	size_t i;
24 26
 	int err;
25 27
 
26 28
 	res = malloc(sizeof(rpn_if_t));
@@ -29,13 +31,23 @@ rpn_if_t* rpn_if_new(const rpn_if_param_t *params, rpn_expr_t *rpn)
29 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 53
 	res->rpn_res = malloc(sizeof(rpn_value_t) *
@@ -46,20 +58,38 @@ rpn_if_t* rpn_if_new(const rpn_if_param_t *params, rpn_expr_t *rpn)
46 58
 	}
47 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 62
 	if(!res->rpn)
51 63
 	{
52 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 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 84
 	rpn_expr_err:
58 85
 		err = errno;
59 86
 		free(res->rpn_res);
60 87
 	rpn_malloc_err:
61 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 93
 	mmap_err:
64 94
 		err = errno;
65 95
 		free(res);
@@ -72,13 +102,16 @@ rpn_if_t* rpn_if_new(const rpn_if_param_t *params, rpn_expr_t *rpn)
72 102
 void rpn_if_free(rpn_if_t* rif)
73 103
 {
74 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 107
 		rpn_expr_close(&(rif->rpn[i]));
78 108
 	}
79 109
 	free(rif->rpn);
80 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 115
 	free(rif);
83 116
 }
84 117
 
@@ -86,23 +119,23 @@ size_t rpn_if_step(rpn_if_t *rif, size_t pos)
86 119
 {
87 120
 	size_t i;
88 121
 	size_t newpos;
89
-	rif->params.arg_f(rif, pos, rif->rpn_args);
122
+	rif->params->arg_f(rif, pos, rif->rpn_args);
90 123
 	/* WRONG ! rif->rpn_args is in rif structure and do not have to be
91 124
 	   given as argument... */
92
-	for(i=0; i<rif->params.rpn_sz; i++)
125
+	for(i=0; i<rif->params->rpn_sz; i++)
93 126
 	{
94 127
 		rif->rpn_res[i] = rpn_expr_eval(&(rif->rpn[i]), rif->rpn_args);
95 128
 	}
96
-	//rif->params.res_f(rif, &newpos, rif->rpn_res);
129
+	//rif->params->res_f(rif, &newpos, rif->rpn_res);
97 130
 	/* MEGA WRONG ! rif->rpn_res is in rif structure and do not have to be
98 131
 	   given as argument... */
99
-	rif->params.res_f(rif, &newpos, NULL);
132
+	rif->params->res_f(rif, &newpos, NULL);
100 133
 	return newpos;
101 134
 }
102 135
 
103 136
 int rpn_if_rpn_upd(rpn_if_t *rif, rpn_tokenized_t *rpns)
104 137
 {
105
-	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);
106 139
 }
107 140
 
108 141
 int rpn_if_rpn_upd_rng(rpn_if_t *rif, rpn_tokenized_t *rpns, size_t sz,

+ 23
- 9
rpn_if.h View File

@@ -24,6 +24,7 @@
24 24
 #include "rpn_jit.h"
25 25
 
26 26
 /**@defgroup ifs_if Iterated function
27
+ * @ingroup ifs
27 28
  * @brief Iterated RPN expression
28 29
  *
29 30
  * A single Iterated Function implemented using an RPN expression.
@@ -51,6 +52,9 @@ struct rpn_if_param_s
51 52
 	/**@brief Number of arguments expected by RPN expressions */
52 53
 	size_t rpn_argc;
53 54
 
55
+	/**@brief Sizeof RPN expression stacks */
56
+	unsigned char rpn_stack_sz;
57
+
54 58
 	/**@brief Set RPN arguments given a position
55 59
 	 * @note transform position to arguments and fetch other arguments
56 60
 	 * from memory map*/
@@ -67,7 +71,7 @@ struct rpn_if_param_s
67 71
 struct rpn_if_s
68 72
 {
69 73
 	/**@brief IF parameters */
70
-	rpn_if_param_t params;
74
+	const rpn_if_param_t *params;
71 75
 	/**@brief RPN expression(s) pointer(s) */
72 76
 	rpn_expr_t *rpn;
73 77
 	/**@brief RPN expression(s) result(s) buffer */
@@ -76,21 +80,28 @@ struct rpn_if_s
76 80
 	rpn_value_t *rpn_args;
77 81
 	/**@brief Memory map in wich data are fetch & stored */
78 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;
79 86
 };
80 87
 
81 88
 /**@brief Macro fetching a memory pointer given a position
82 89
  * @return rpn_value_t* values
83 90
  */
84
-#define rpn_if_getitem(rif, pos) (rif->mem + (rif->params.value_sz * pos))
91
+#define rpn_if_getitem(rif, pos) (rif->mem + ((rif->params->value_sz) * pos))
85 92
 
86
-/**@brief Alloc a new @ref rpn_if_s from two transformation functions
93
+/**@brief Alloc a new @ref rpn_if_s using given parameters
87 94
  * @param params IF parameters
88 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
89 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
90 100
  */
91
-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);
92 103
 
93
-/**@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 
94 105
  * @ref rpn_expr_s
95 106
  * @param rif The IF to deallocate
96 107
  */
@@ -105,7 +116,7 @@ size_t rpn_if_step(rpn_if_t *rif, size_t pos);
105 116
 
106 117
 /**@brief Update all RPN expressions
107 118
  * @param rif The concerned IF
108
- * @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)
109 120
  * @return 0 if no error else -1
110 121
  * @note Shortcut for @ref rpn_if_rpn_upd_rng(rif, rpns, rif->rpn_sz, 0);
111 122
  */
@@ -113,7 +124,7 @@ int rpn_if_rpn_upd(rpn_if_t *rif, rpn_tokenized_t *rpns);
113 124
 
114 125
 /**@brief Update a range of RPN expressions
115 126
  * @param rif The concerned IF
116
- * @param rpn A list of tokenized expressions
127
+ * @param rpns A list of tokenized expressions
117 128
  * @param sz Number of rpn expression in rpn argument
118 129
  * @param offset Start updating expressions from this offset
119 130
  * @return 0 if no error else -1
@@ -123,13 +134,16 @@ int rpn_if_rpn_upd_rng(rpn_if_t *rif, rpn_tokenized_t *rpns, size_t sz,
123 134
 
124 135
 /**@brief New @ref rpn_if_s and partial initialisation
125 136
  * @param mem_sz memory size in bytes
126
- * @param argc number of arguments taken by @ref rpn_expr_s
137
+ * @param rpn_argc number of arguments taken by @ref rpn_expr_s
127 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
128 141
  */
129 142
 rpn_if_t* _rpn_if_new(size_t mem_sz, size_t rpn_argc, size_t rpn_count,
130 143
 	size_t value_sz);
131 144
 
132
-/**@page doc_ifs
145
+/**@page doc_ifs Iterated function system
146
+ * @brief Iterated function system using RPN
133 147
  *
134 148
  * Iterated functions system are a composed of Iterated function choosed
135 149
  * randomly.

+ 22
- 16
rpn_if_default.h View File

@@ -20,10 +20,16 @@
20 20
 #define __rpn_if_default__h__
21 21
 
22 22
 /**@file rpn_if_default.h Defines default IF
23
+ * @ingroup ifs_if_default
23 24
  * @brief Default IF definitions
24 25
  */
26
+
25 27
 /**@defgroup ifs_if_default Default functions
28
+ * @ingroup ifs_if
26 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 35
 #include "config.h"
@@ -64,7 +70,7 @@ struct rpn_if_default_data_s
64 70
 	size_t *size_lim;
65 71
 
66 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 74
 	 * - For @ref RPN_IF_RES_CONST_RGBA const_val points on 4 values
69 75
 	 */
70 76
 	rpn_value_t *const_val;
@@ -74,49 +80,49 @@ int rpn_if_argf_default(rpn_if_t *rif, size_t pos, rpn_value_t *args);
74 80
 int rpn_if_resf_default(rpn_if_t *rif, size_t *pos, rpn_value_t *data);
75 81
 
76 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 86
  * @note Other arguments are set using the generic @ref rpn_if_argf_default
81 87
  * function
82 88
  */
83 89
 int rpn_if_argf_linear(rpn_if_t *rif, size_t pos, rpn_value_t *args);
84 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 94
  * @note Data from position fecth is done by generic @ref rpn_if_resf_default
89 95
  * function
90 96
  */
91 97
 int rpn_if_resf_linear(rpn_if_t *rif, size_t *pos, rpn_value_t *data);
92 98
 
93 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 103
  * @note Other arguments are set using the generic @ref rpn_if_argf_default
98 104
  * function
99 105
  */
100 106
 int rpn_if_argf_xy(rpn_if_t *rif, size_t pos, rpn_value_t *args);
101 107
 /**@brief Transform 1st and 2nd result into a memory map's offset
102
- * @params rif Expressions
108
+ * @param rif Expressions
103 109
  * @param pos Memory map offset pointer
104
- * @params data Pointer on resulting data
110
+ * @param data Pointer on resulting data
105 111
  * @note Data from position fetch is done by generic @ref rpn_if_resf_default
106 112
  * function
107 113
  */
108 114
 int rpn_if_resf_xy(rpn_if_t *rif, size_t *pos, rpn_value_t *data);
109 115
 
110 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 120
  * @note Other arguments are set using the generic @ref rpn_if_argf_default
115 121
  * function
116 122
  */
117 123
 int rpn_if_argf_xdim(rpn_if_t *rif, size_t pos, rpn_value_t *args);
118 124
 /**@brief Transform X arguments into a memory map's offset
119
- * @params rif Expressions
125
+ * @param rif Expressions
120 126
  * @param pos Memory map offset pointer
121 127
  * @param data Pointer on resulting data
122 128
  * @note Data from position fetch is done by generic @ref rpn_if_resf_default

+ 74
- 0
rpn_ifs.h View File

@@ -22,6 +22,7 @@
22 22
 #include "config.h"
23 23
 
24 24
 #include "rpn_jit.h"
25
+#include "rpn_if.h"
25 26
 
26 27
 /**@defgroup ifs Iterated function system
27 28
  * @brief IFS implementation with RPN expressions
@@ -37,5 +38,78 @@
37 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 114
 #endif
41 115
 

Loading…
Cancel
Save