src/libale: Macros for mapping and unmapping to the host address space.
[libale.git] / src / libale.h
blob6b7801f1cb861de0bae3de32a1328e0aeaecbc7c
1 /*
2 * Copyright 2008, 2009 David Hilvert <dhilvert@gmail.com>
4 * This file is part of libale.
6 * libale is free software: you can redistribute it and/or modify it under the
7 * terms of the GNU Affero General Public License as published by the Free
8 * Software Foundation, either version 3 of the License, or (at your option)
9 * any later version.
11 * libale is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
14 * more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with libale. If not, see <http://www.gnu.org/licenses/>.
20 #include <ale.h>
22 #include <stdlib.h>
25 * Abstract types used internally by host code (cf. ale_context_get_*_type for
26 * preferred computational types).
29 typedef double ale_pos;
30 typedef double ale_accum;
31 typedef double ale_real;
33 typedef struct { ale_pos x[3]; } point;
35 static point point2(ale_pos x0, ale_pos x1) {
36 point p;
38 p.x[0] = x0;
39 p.x[1] = x1;
40 p.x[2] = 0;
43 static point point3(ale_pos x0, ale_pos x1, ale_pos x2) {
44 point p;
46 p.x[0] = x0;
47 p.x[1] = x1;
48 p.x[2] = x2;
51 static point point_posinf(int dim) {
52 int d;
53 point p;
55 /* Division logic from ALE d2::point::posinf */
57 ale_pos a = +1;
58 ale_pos z = +0;
60 a = a / z;
62 for (d = 0; d < 3; d++)
63 p.x[d] = (d < dim) ? a : 0;
65 return p;
68 static point point_neginf(int dim) {
69 int d;
70 point p = point_posinf(dim);
72 for (d = 0; d < dim && d < 3; d++)
73 p.x[d] = -p.x[d];
75 return p;
79 * Macro for obtaining a pointer from ALE_POINTER type.
82 #define P(x) x.p
85 * Macro for obtaining a pointer to host-mapped memory from an ALE_POINTER
86 * type to device memory.
89 #warning map macro not yet defined
90 #define Q(x) /* not yet defined */
93 * Macro for unmapping a previously mapped ALE_POINTER region
96 #warning unmap macro not yet defined
97 #define R(x) /* not yet defined */
100 * Macro for NULL ALE_POINTER.
103 #define N(TYPEP) TYPEP ## _NULL()
106 * Macros for common API type elements and functions.
110 * We do our own reference counting, since there's no obvious
111 * way to perform destruction operations in the case of CL
112 * reference counts. (E.g., the spec explicitly states that
113 * obtained reference count values are unsuitable for uses
114 * other than debugging.
117 #define TYPE_COMMON_ELEMENTS \
118 unsigned int refcount;
120 #define TYPE_STRUCTURE(TYPENAME, TYPE_ELEMENTS) \
121 struct _ ## TYPENAME { \
122 TYPE_COMMON_ELEMENTS \
123 TYPE_ELEMENTS \
126 #define TYPE_HOST_COMMON_FUNCTIONS(TYPENAME, DESTRUCTOR) \
127 int TYPENAME ## _valid(TYPENAME xarg) { \
128 if (P(xarg) == NULL || P(xarg)->refcount == 0) \
129 return 0; \
130 return 1; \
133 int TYPENAME ## _retain(TYPENAME xarg) { \
134 if (!TYPENAME ## _valid(xarg)) \
135 return ALE_UNSPECIFIED_FAILURE; \
136 if (!(P(xarg)->refcount + 1 > 0)) \
137 return ALE_UNSPECIFIED_FAILURE; \
138 P(xarg)->refcount++; \
139 return ALE_SUCCESS; \
142 static TYPENAME TYPENAME ## _alloc() { \
143 TYPENAME retval; \
144 P(retval) = (struct _ ## TYPENAME *) malloc(sizeof(struct _ ## TYPENAME)); \
145 if (P(retval)) \
146 P(retval)->refcount = 1; \
147 return retval; \
150 static void TYPENAME ## _free(TYPENAME this) { \
151 DESTRUCTOR \
154 int TYPENAME ## _release(TYPENAME xarg) { \
155 if (!TYPENAME ## _valid(xarg)) \
156 return ALE_UNSPECIFIED_FAILURE; \
157 P(xarg)->refcount--; \
158 if (P(xarg)->refcount == 0) {\
159 TYPENAME ## _free(xarg); \
160 free(P(xarg)); \
162 return ALE_SUCCESS; \
165 #warning TYPE_COMMON_FUNCTIONS not yet differentiated from TYPE_HOST_COMMON_FUNCTIONS
166 #define TYPE_COMMON_FUNCTIONS(TYPENAME, DESTRUCTOR) \
167 int TYPENAME ## _valid(TYPENAME xarg) { \
168 if (P(xarg) == NULL || P(xarg)->refcount == 0) \
169 return 0; \
170 return 1; \
173 int TYPENAME ## _retain(TYPENAME xarg) { \
174 if (!TYPENAME ## _valid(xarg)) \
175 return ALE_UNSPECIFIED_FAILURE; \
176 if (!(P(xarg)->refcount + 1 > 0)) \
177 return ALE_UNSPECIFIED_FAILURE; \
178 P(xarg)->refcount++; \
179 return ALE_SUCCESS; \
182 static TYPENAME TYPENAME ## _alloc() { \
183 TYPENAME retval; \
184 P(retval) = (struct _ ## TYPENAME *) malloc(sizeof(struct _ ## TYPENAME)); \
185 if (P(retval)) \
186 P(retval)->refcount = 1; \
187 return retval; \
190 static void TYPENAME ## _free(TYPENAME this) { \
191 DESTRUCTOR \
194 int TYPENAME ## _release(TYPENAME xarg) { \
195 if (!TYPENAME ## _valid(xarg)) \
196 return ALE_UNSPECIFIED_FAILURE; \
197 P(xarg)->refcount--; \
198 if (P(xarg)->refcount == 0) {\
199 TYPENAME ## _free(xarg); \
200 free(P(xarg)); \
202 return ALE_SUCCESS; \
205 #define TYPE_HOST(TYPENAME, TYPE_ELEMENTS, DESTRUCTOR) \
206 TYPE_STRUCTURE(TYPENAME, TYPE_ELEMENTS) \
207 TYPE_HOST_COMMON_FUNCTIONS(TYPENAME, DESTRUCTOR)
209 #define TYPE(TYPENAME, TYPE_ELEMENTS, DESTRUCTOR) \
210 ALE_DATA_TYPE(_ ## TYPENAME ## _def, TYPE_STRUCTURE(TYPENAME, TYPE_ELEMENTS) ) \
211 TYPE_COMMON_FUNCTIONS(TYPENAME, DESTRUCTOR)
214 * Macros for API parameter operations.
217 #define PARAMETER_R(OBJECT_TYPE, PARAMETER_NAME, PARAMETER_TYPE) \
218 PARAMETER_TYPE OBJECT_TYPE ## _get_ ## PARAMETER_NAME (OBJECT_TYPE object) { \
219 if (!OBJECT_TYPE ## _valid(object)) \
220 return ((PARAMETER_TYPE) 0); \
221 return object->PARAMETER_NAME; \
224 #define PARAMETER_W(OBJECT_TYPE, PARAMETER_NAME, PARAMETER_TYPE) \
225 int OBJECT_TYPE ## _set_ ## PARAMETER_NAME (OBJECT_TYPE object, PARAMETER_TYPE value){ \
226 if (!OBJECT_TYPE ## _valid(object)) \
227 return ALE_UNSPECIFIED_FAILURE; \
228 object->PARAMETER_NAME = value; \
229 return ALE_SUCCESS; \
232 #define PARAMETER_RW(OBJECT_TYPE, PARAMETER_NAME, PARAMETER_TYPE) \
233 PARAMETER_R(OBJECT_TYPE, PARAMETER_NAME, PARAMETER_TYPE) \
234 PARAMETER_W(OBJECT_TYPE, PARAMETER_NAME, PARAMETER_TYPE)
237 * Detemine type size.
240 static int type_size(int type) {
241 if (type == ALE_TYPE_UINT_8)
242 return 1;
243 if (type == ALE_TYPE_UINT_16)
244 return 2;
245 if (type == ALE_TYPE_FLOAT_32 || type == ALE_TYPE_UINT_32)
246 return 4;
247 if (type == ALE_TYPE_FLOAT_64 || type == ALE_TYPE_UINT_64)
248 return 8;