1 | |
---|
2 | /* |
---|
3 | ----------------------------------------------------------------------------- |
---|
4 | This source file is part of GIMPACT Library. |
---|
5 | |
---|
6 | For the latest info, see http://gimpact.sourceforge.net/ |
---|
7 | |
---|
8 | Copyright (c) 2006 Francisco Leon. C.C. 80087371. |
---|
9 | email: projectileman@yahoo.com |
---|
10 | |
---|
11 | This library is free software; you can redistribute it and/or |
---|
12 | modify it under the terms of EITHER: |
---|
13 | (1) The GNU Lesser General Public License as published by the Free |
---|
14 | Software Foundation; either version 2.1 of the License, or (at |
---|
15 | your option) any later version. The text of the GNU Lesser |
---|
16 | General Public License is included with this library in the |
---|
17 | file GIMPACT-LICENSE-LGPL.TXT. |
---|
18 | (2) The BSD-style license that is included with this library in |
---|
19 | the file GIMPACT-LICENSE-BSD.TXT. |
---|
20 | |
---|
21 | This library is distributed in the hope that it will be useful, |
---|
22 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
23 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files |
---|
24 | GIMPACT-LICENSE-LGPL.TXT and GIMPACT-LICENSE-BSD.TXT for more details. |
---|
25 | |
---|
26 | ----------------------------------------------------------------------------- |
---|
27 | */ |
---|
28 | |
---|
29 | |
---|
30 | #include "GIMPACT/gim_memory.h" |
---|
31 | #include "stdlib.h" |
---|
32 | #include "malloc.h" |
---|
33 | //#include "mm_malloc.h" |
---|
34 | |
---|
35 | static gim_alloc_function *g_allocfn = 0; |
---|
36 | static gim_alloca_function *g_allocafn = 0; |
---|
37 | static gim_realloc_function *g_reallocfn = 0; |
---|
38 | static gim_free_function *g_freefn = 0; |
---|
39 | |
---|
40 | // buffer managers |
---|
41 | #define MAX_BUFFER_MANAGERS 16 |
---|
42 | static GBUFFER_MANAGER_DATA g_buffer_managers[MAX_BUFFER_MANAGERS]; |
---|
43 | static GUINT g_buffer_managers_count = 0; |
---|
44 | |
---|
45 | #define VALIDATE_BUFFER_MANAGER(buffer_manager_id)\ |
---|
46 | if(buffer_manager_id>=MAX_BUFFER_MANAGERS) return G_BUFFER_OP_INVALID;\ |
---|
47 | GBUFFER_MANAGER_DATA * bm_data;\ |
---|
48 | gim_get_buffer_manager_data(buffer_manager_id,&bm_data);\ |
---|
49 | if(bm_data == 0) return G_BUFFER_OP_INVALID;\ |
---|
50 | |
---|
51 | #define VALIDATE_BUFFER_ID_PT(buffer_id)\ |
---|
52 | VALIDATE_BUFFER_MANAGER(buffer_id->m_buffer_manager_id)\ |
---|
53 | if(buffer_id->m_buffer_id>=bm_data->m_buffer_array.m_size) return G_BUFFER_OP_INVALID;\ |
---|
54 | GBUFFER_DATA * pbuffer = GIM_DYNARRAY_POINTER(GBUFFER_DATA,bm_data->m_buffer_array);\ |
---|
55 | pbuffer += buffer_id->m_buffer_id;\ |
---|
56 | if(pbuffer->m_buffer_handle==0) return G_BUFFER_OP_INVALID;\ |
---|
57 | |
---|
58 | |
---|
59 | void GIM_BUFFER_ARRAY_DESTROY(GBUFFER_ARRAY & array_data) |
---|
60 | { |
---|
61 | gim_buffer_array_unlock(&array_data); |
---|
62 | gim_buffer_free(&(array_data).m_buffer_id); |
---|
63 | } |
---|
64 | |
---|
65 | void GIM_DYNARRAY_DESTROY(GDYNAMIC_ARRAY & array_data) |
---|
66 | { |
---|
67 | if(array_data.m_pdata != 0) |
---|
68 | { |
---|
69 | gim_free(array_data.m_pdata,0); |
---|
70 | array_data.m_reserve_size = 0; |
---|
71 | array_data.m_size = 0; |
---|
72 | array_data.m_pdata = 0; |
---|
73 | } |
---|
74 | } |
---|
75 | |
---|
76 | void gim_set_alloc_handler (gim_alloc_function *fn) |
---|
77 | { |
---|
78 | g_allocfn = fn; |
---|
79 | } |
---|
80 | |
---|
81 | void gim_set_alloca_handler (gim_alloca_function *fn) |
---|
82 | { |
---|
83 | g_allocafn = fn; |
---|
84 | } |
---|
85 | |
---|
86 | void gim_set_realloc_handler (gim_realloc_function *fn) |
---|
87 | { |
---|
88 | g_reallocfn = fn; |
---|
89 | } |
---|
90 | |
---|
91 | void gim_set_free_handler (gim_free_function *fn) |
---|
92 | { |
---|
93 | g_freefn = fn; |
---|
94 | } |
---|
95 | |
---|
96 | gim_alloc_function *gim_get_alloc_handler() |
---|
97 | { |
---|
98 | return g_allocfn; |
---|
99 | } |
---|
100 | |
---|
101 | gim_alloca_function *gim_get_alloca_handler() |
---|
102 | { |
---|
103 | return g_allocafn; |
---|
104 | } |
---|
105 | |
---|
106 | |
---|
107 | gim_realloc_function *gim_get_realloc_handler () |
---|
108 | { |
---|
109 | return g_reallocfn; |
---|
110 | } |
---|
111 | |
---|
112 | |
---|
113 | gim_free_function *gim_get_free_handler () |
---|
114 | { |
---|
115 | return g_freefn; |
---|
116 | } |
---|
117 | |
---|
118 | |
---|
119 | void * gim_alloc(size_t size) |
---|
120 | { |
---|
121 | void * ptr = 0; |
---|
122 | ptr = malloc(size); |
---|
123 | /*if (g_allocfn) ptr = g_allocfn(size); else ptr = malloc(size);//_mm_malloc(size,0);*/ |
---|
124 | if(ptr==0) |
---|
125 | { |
---|
126 | float * fp = 0; |
---|
127 | *fp = 0.0f; |
---|
128 | } |
---|
129 | return ptr; |
---|
130 | } |
---|
131 | |
---|
132 | void * gim_alloca(size_t size) |
---|
133 | { |
---|
134 | if (g_allocafn) return g_allocafn(size); else return alloca(size); |
---|
135 | } |
---|
136 | |
---|
137 | |
---|
138 | void * gim_realloc(void *ptr, size_t oldsize, size_t newsize) |
---|
139 | { |
---|
140 | /*if (g_reallocfn) return g_reallocfn(ptr,oldsize,newsize); |
---|
141 | else return realloc(ptr,newsize);*/ |
---|
142 | //return realloc(ptr,newsize); |
---|
143 | void * newptr = gim_alloc(newsize); |
---|
144 | size_t copysize = newsize> oldsize? oldsize: newsize; |
---|
145 | memcpy(newptr,ptr,copysize); |
---|
146 | gim_free(ptr,oldsize); |
---|
147 | return newptr; |
---|
148 | } |
---|
149 | |
---|
150 | void gim_free(void *ptr, size_t size) |
---|
151 | { |
---|
152 | if (!ptr) return; |
---|
153 | if (g_freefn) |
---|
154 | { |
---|
155 | g_freefn(ptr,size); |
---|
156 | } |
---|
157 | else |
---|
158 | { |
---|
159 | free(ptr);//_mm_free(ptr); |
---|
160 | } |
---|
161 | } |
---|
162 | |
---|
163 | ///******************************* BUFFER MANAGERS ******************************/// |
---|
164 | |
---|
165 | //!** Basic buffer prototyoe functions |
---|
166 | |
---|
167 | GUINT _system_buffer_alloc_function(GUINT size,int usage) |
---|
168 | { |
---|
169 | void * newdata = gim_alloc(size); |
---|
170 | memset(newdata,0,size); |
---|
171 | return (GUINT)(newdata); |
---|
172 | } |
---|
173 | |
---|
174 | GUINT _system_buffer_alloc_data_function(const void * pdata,GUINT size,int usage) |
---|
175 | { |
---|
176 | void * newdata = gim_alloc(size); |
---|
177 | memcpy(newdata,pdata,size); |
---|
178 | return (GUINT)(newdata); |
---|
179 | } |
---|
180 | |
---|
181 | GUINT _system_buffer_realloc_function(GUINT buffer_handle,GUINT oldsize,int old_usage,GUINT newsize,int new_usage) |
---|
182 | { |
---|
183 | void * newdata = gim_realloc((void *)buffer_handle,oldsize,newsize); |
---|
184 | return (GUINT)(newdata); |
---|
185 | } |
---|
186 | |
---|
187 | void _system_buffer_free_function(GUINT buffer_handle,GUINT size) |
---|
188 | { |
---|
189 | gim_free((void*)buffer_handle,size); |
---|
190 | } |
---|
191 | |
---|
192 | char * _system_lock_buffer_function(GUINT buffer_handle,int access) |
---|
193 | { |
---|
194 | return (char * )(buffer_handle); |
---|
195 | } |
---|
196 | |
---|
197 | |
---|
198 | void _system_unlock_buffer_function(GUINT buffer_handle) |
---|
199 | { |
---|
200 | } |
---|
201 | |
---|
202 | void _system_download_from_buffer_function( |
---|
203 | GUINT source_buffer_handle, |
---|
204 | GUINT source_pos, |
---|
205 | void * destdata, |
---|
206 | GUINT copysize) |
---|
207 | { |
---|
208 | char * pdata; |
---|
209 | pdata = (char *)source_buffer_handle; |
---|
210 | memcpy(destdata,pdata+source_pos,copysize); |
---|
211 | } |
---|
212 | |
---|
213 | void _system_upload_to_buffer_function( |
---|
214 | GUINT dest_buffer_handle, |
---|
215 | GUINT dest_pos, |
---|
216 | void * sourcedata, |
---|
217 | GUINT copysize) |
---|
218 | { |
---|
219 | char * pdata; |
---|
220 | pdata = (char * )dest_buffer_handle; |
---|
221 | memcpy(pdata+dest_pos,sourcedata,copysize); |
---|
222 | } |
---|
223 | |
---|
224 | void _system_copy_buffers_function( |
---|
225 | GUINT source_buffer_handle, |
---|
226 | GUINT source_pos, |
---|
227 | GUINT dest_buffer_handle, |
---|
228 | GUINT dest_pos, |
---|
229 | GUINT copysize) |
---|
230 | { |
---|
231 | char * pdata1,*pdata2; |
---|
232 | pdata1 = (char *)source_buffer_handle; |
---|
233 | pdata2 = (char *)dest_buffer_handle; |
---|
234 | memcpy(pdata2+dest_pos,pdata1+source_pos,copysize); |
---|
235 | } |
---|
236 | |
---|
237 | GUINT _shared_buffer_alloc_function(GUINT size,int usage) |
---|
238 | { |
---|
239 | return 0; |
---|
240 | } |
---|
241 | |
---|
242 | GUINT _shared_buffer_alloc_data_function(const void * pdata,GUINT size,int usage) |
---|
243 | { |
---|
244 | return (GUINT)pdata; |
---|
245 | } |
---|
246 | |
---|
247 | GUINT _shared_buffer_realloc_function(GUINT buffer_handle,GUINT oldsize,int old_usage,GUINT newsize,int new_usage) |
---|
248 | { |
---|
249 | return 0; |
---|
250 | } |
---|
251 | |
---|
252 | void _shared_buffer_free_function(GUINT buffer_handle,GUINT size) |
---|
253 | { |
---|
254 | } |
---|
255 | |
---|
256 | //!** Buffer manager operations |
---|
257 | void gim_create_buffer_manager(GBUFFER_MANAGER_PROTOTYPE * prototype,GUINT buffer_manager_id) |
---|
258 | { |
---|
259 | GBUFFER_MANAGER_DATA * bm_data; |
---|
260 | bm_data = &g_buffer_managers[buffer_manager_id]; |
---|
261 | |
---|
262 | if(bm_data->m_active==0) |
---|
263 | { |
---|
264 | if(g_buffer_managers_count<=buffer_manager_id) |
---|
265 | { |
---|
266 | g_buffer_managers_count = buffer_manager_id+1; |
---|
267 | } |
---|
268 | } |
---|
269 | else |
---|
270 | { |
---|
271 | gim_destroy_buffer_manager(buffer_manager_id); |
---|
272 | } |
---|
273 | bm_data->m_active = 1; |
---|
274 | //CREATE ARRAYS |
---|
275 | GIM_DYNARRAY_CREATE(GBUFFER_DATA,bm_data->m_buffer_array,G_ARRAY_GROW_SIZE); |
---|
276 | GIM_DYNARRAY_CREATE(GUINT,bm_data->m_free_positions,G_ARRAY_GROW_SIZE); |
---|
277 | //INIT PROTOTYPE |
---|
278 | bm_data->m_prototype.alloc_data_fn = prototype->alloc_data_fn; |
---|
279 | bm_data->m_prototype.alloc_fn = prototype->alloc_fn; |
---|
280 | bm_data->m_prototype.copy_buffers_fn = prototype->copy_buffers_fn; |
---|
281 | bm_data->m_prototype.download_from_buffer_fn = prototype->download_from_buffer_fn; |
---|
282 | bm_data->m_prototype.free_fn = prototype->free_fn; |
---|
283 | bm_data->m_prototype.lock_buffer_fn = prototype->lock_buffer_fn; |
---|
284 | bm_data->m_prototype.realloc_fn = prototype->realloc_fn; |
---|
285 | bm_data->m_prototype.unlock_buffer_fn = prototype->unlock_buffer_fn; |
---|
286 | bm_data->m_prototype.upload_to_buffer_fn = prototype->upload_to_buffer_fn; |
---|
287 | } |
---|
288 | |
---|
289 | GUINT gim_get_buffer_manager_count() |
---|
290 | { |
---|
291 | return g_buffer_managers_count; |
---|
292 | } |
---|
293 | void gim_destroy_buffer_manager(GUINT buffer_manager_id) |
---|
294 | { |
---|
295 | GBUFFER_MANAGER_DATA * bm_data; |
---|
296 | gim_get_buffer_manager_data(buffer_manager_id,&bm_data); |
---|
297 | if(bm_data == 0) return; |
---|
298 | //Destroy all buffers |
---|
299 | |
---|
300 | GBUFFER_DATA * buffers = GIM_DYNARRAY_POINTER(GBUFFER_DATA,bm_data->m_buffer_array); |
---|
301 | GUINT i, buffer_count = bm_data->m_buffer_array.m_size; |
---|
302 | for (i=0;i<buffer_count ;i++ ) |
---|
303 | { |
---|
304 | if(buffers[i].m_buffer_handle!=0) //Is active |
---|
305 | { |
---|
306 | // free handle |
---|
307 | bm_data->m_prototype.free_fn(buffers[i].m_buffer_handle,buffers[i].m_size); |
---|
308 | } |
---|
309 | } |
---|
310 | |
---|
311 | //destroy buffer array |
---|
312 | GIM_DYNARRAY_DESTROY(bm_data->m_buffer_array); |
---|
313 | //destroy free positions |
---|
314 | GIM_DYNARRAY_DESTROY(bm_data->m_free_positions); |
---|
315 | //Mark as innactive |
---|
316 | bm_data->m_active = 0; |
---|
317 | } |
---|
318 | void gim_get_buffer_manager_data(GUINT buffer_manager_id,GBUFFER_MANAGER_DATA ** pbm_data) |
---|
319 | { |
---|
320 | GBUFFER_MANAGER_DATA * bm_data; |
---|
321 | bm_data = &g_buffer_managers[buffer_manager_id]; |
---|
322 | |
---|
323 | if(bm_data->m_active==0) |
---|
324 | { |
---|
325 | *pbm_data = 0; |
---|
326 | } |
---|
327 | else |
---|
328 | { |
---|
329 | *pbm_data = bm_data; |
---|
330 | } |
---|
331 | } |
---|
332 | |
---|
333 | void gim_init_buffer_managers() |
---|
334 | { |
---|
335 | GUINT i; |
---|
336 | for (i=0;i<MAX_BUFFER_MANAGERS;i++) |
---|
337 | { |
---|
338 | g_buffer_managers[i].m_active = 0; |
---|
339 | g_buffer_managers[i].m_buffer_array.m_pdata = 0; |
---|
340 | g_buffer_managers[i].m_buffer_array.m_reserve_size = 0; |
---|
341 | g_buffer_managers[i].m_buffer_array.m_size = 0; |
---|
342 | g_buffer_managers[i].m_free_positions.m_pdata = 0; |
---|
343 | g_buffer_managers[i].m_free_positions.m_reserve_size = 0; |
---|
344 | g_buffer_managers[i].m_free_positions.m_size = 0; |
---|
345 | } |
---|
346 | g_buffer_managers_count = 0; |
---|
347 | // Add the two most important buffer managers |
---|
348 | GBUFFER_MANAGER_PROTOTYPE prototype; |
---|
349 | |
---|
350 | //add system buffer manager |
---|
351 | prototype.alloc_data_fn = _system_buffer_alloc_data_function; |
---|
352 | prototype.alloc_fn = _system_buffer_alloc_function; |
---|
353 | prototype.copy_buffers_fn = _system_copy_buffers_function; |
---|
354 | prototype.download_from_buffer_fn = _system_download_from_buffer_function; |
---|
355 | prototype.free_fn = _system_buffer_free_function; |
---|
356 | prototype.lock_buffer_fn = _system_lock_buffer_function; |
---|
357 | prototype.realloc_fn = _system_buffer_realloc_function; |
---|
358 | prototype.unlock_buffer_fn = _system_unlock_buffer_function; |
---|
359 | prototype.upload_to_buffer_fn = _system_upload_to_buffer_function; |
---|
360 | |
---|
361 | gim_create_buffer_manager(&prototype,G_BUFFER_MANAGER_SYSTEM ); |
---|
362 | |
---|
363 | //add zhared buffer manager |
---|
364 | prototype.alloc_data_fn = _shared_buffer_alloc_data_function; |
---|
365 | prototype.alloc_fn = _shared_buffer_alloc_function; |
---|
366 | prototype.free_fn = _shared_buffer_free_function; |
---|
367 | gim_create_buffer_manager(&prototype,G_BUFFER_MANAGER_SHARED); |
---|
368 | } |
---|
369 | |
---|
370 | void gim_terminate_buffer_managers() |
---|
371 | { |
---|
372 | GUINT i; |
---|
373 | for (i=0;i<g_buffer_managers_count;i++) |
---|
374 | { |
---|
375 | gim_destroy_buffer_manager(i); |
---|
376 | } |
---|
377 | g_buffer_managers_count = 0; |
---|
378 | } |
---|
379 | |
---|
380 | //!** Nuffer operations |
---|
381 | |
---|
382 | void GET_AVALIABLE_BUFFER_ID(GBUFFER_MANAGER_DATA * buffer_manager, GUINT & buffer_id) |
---|
383 | { |
---|
384 | if(buffer_manager->m_free_positions.m_size>0)\ |
---|
385 | { |
---|
386 | GUINT * _pointer = GIM_DYNARRAY_POINTER(GUINT,buffer_manager->m_free_positions); |
---|
387 | buffer_id = _pointer[buffer_manager->m_free_positions.m_size-1]; |
---|
388 | GIM_DYNARRAY_POP_ITEM(buffer_manager->m_free_positions); |
---|
389 | } |
---|
390 | else |
---|
391 | { |
---|
392 | buffer_id = buffer_manager->m_buffer_array.m_size; |
---|
393 | GIM_DYNARRAY_PUSH_EMPTY(GBUFFER_DATA,buffer_manager->m_buffer_array); |
---|
394 | } |
---|
395 | } |
---|
396 | |
---|
397 | GINT _validate_buffer_id(GBUFFER_ID * buffer_id,GBUFFER_DATA ** ppbuffer,GBUFFER_MANAGER_DATA ** pbm_data) |
---|
398 | { |
---|
399 | VALIDATE_BUFFER_ID_PT(buffer_id) |
---|
400 | *ppbuffer = pbuffer; |
---|
401 | *pbm_data = bm_data; |
---|
402 | return G_BUFFER_OP_SUCCESS; |
---|
403 | } |
---|
404 | |
---|
405 | GUINT gim_create_buffer( |
---|
406 | GUINT buffer_manager_id, |
---|
407 | GUINT buffer_size, |
---|
408 | int usage, |
---|
409 | GBUFFER_ID * buffer_id) |
---|
410 | { |
---|
411 | VALIDATE_BUFFER_MANAGER(buffer_manager_id) |
---|
412 | |
---|
413 | GUINT newbufferhandle = bm_data->m_prototype.alloc_fn(buffer_size,usage); |
---|
414 | if(newbufferhandle==0) return G_BUFFER_OP_INVALID; |
---|
415 | |
---|
416 | GET_AVALIABLE_BUFFER_ID(bm_data,buffer_id->m_buffer_id); |
---|
417 | buffer_id->m_buffer_manager_id = buffer_manager_id; |
---|
418 | |
---|
419 | GBUFFER_DATA * pbuffer = GIM_DYNARRAY_POINTER(GBUFFER_DATA,bm_data->m_buffer_array); |
---|
420 | pbuffer += buffer_id->m_buffer_id ; |
---|
421 | pbuffer->m_buffer_handle = newbufferhandle; |
---|
422 | pbuffer->m_size = buffer_size; |
---|
423 | pbuffer->m_usage = usage; |
---|
424 | pbuffer->m_lock_count = 0; |
---|
425 | pbuffer->m_refcount = 0; |
---|
426 | pbuffer->m_mapped_pointer = 0; |
---|
427 | |
---|
428 | //set shadow buffer if needed |
---|
429 | |
---|
430 | if(usage == G_MU_STATIC_READ || |
---|
431 | usage == G_MU_STATIC_READ_DYNAMIC_WRITE|| |
---|
432 | usage == G_MU_STATIC_READ_DYNAMIC_WRITE_COPY) |
---|
433 | { |
---|
434 | gim_create_common_buffer(buffer_size,&pbuffer->m_shadow_buffer); |
---|
435 | } |
---|
436 | else |
---|
437 | { |
---|
438 | pbuffer->m_shadow_buffer.m_buffer_id = G_UINT_INFINITY; |
---|
439 | pbuffer->m_shadow_buffer.m_buffer_manager_id = G_UINT_INFINITY; |
---|
440 | } |
---|
441 | return G_BUFFER_OP_SUCCESS; |
---|
442 | } |
---|
443 | |
---|
444 | |
---|
445 | GUINT gim_create_buffer_from_data( |
---|
446 | GUINT buffer_manager_id, |
---|
447 | const void * pdata, |
---|
448 | GUINT buffer_size, |
---|
449 | int usage, |
---|
450 | GBUFFER_ID * buffer_id) |
---|
451 | { |
---|
452 | VALIDATE_BUFFER_MANAGER(buffer_manager_id) |
---|
453 | |
---|
454 | GUINT newbufferhandle = bm_data->m_prototype.alloc_data_fn(pdata,buffer_size,usage); |
---|
455 | if(newbufferhandle==0) return G_BUFFER_OP_INVALID; |
---|
456 | |
---|
457 | GET_AVALIABLE_BUFFER_ID(bm_data,buffer_id->m_buffer_id); |
---|
458 | buffer_id->m_buffer_manager_id = buffer_manager_id; |
---|
459 | |
---|
460 | GBUFFER_DATA * pbuffer = GIM_DYNARRAY_POINTER(GBUFFER_DATA,bm_data->m_buffer_array); |
---|
461 | pbuffer += buffer_id->m_buffer_id ; |
---|
462 | pbuffer->m_buffer_handle = newbufferhandle; |
---|
463 | pbuffer->m_size = buffer_size; |
---|
464 | pbuffer->m_usage = usage; |
---|
465 | pbuffer->m_lock_count = 0; |
---|
466 | pbuffer->m_mapped_pointer = 0; |
---|
467 | pbuffer->m_refcount = 0; |
---|
468 | |
---|
469 | //set shadow buffer if needed |
---|
470 | |
---|
471 | if(usage == G_MU_STATIC_READ || |
---|
472 | usage == G_MU_STATIC_READ_DYNAMIC_WRITE|| |
---|
473 | usage == G_MU_STATIC_READ_DYNAMIC_WRITE_COPY) |
---|
474 | { |
---|
475 | gim_create_common_buffer_from_data(pdata,buffer_size,&pbuffer->m_shadow_buffer); |
---|
476 | } |
---|
477 | else |
---|
478 | { |
---|
479 | pbuffer->m_shadow_buffer.m_buffer_id = G_UINT_INFINITY; |
---|
480 | pbuffer->m_shadow_buffer.m_buffer_manager_id = G_UINT_INFINITY; |
---|
481 | } |
---|
482 | return G_BUFFER_OP_SUCCESS; |
---|
483 | } |
---|
484 | |
---|
485 | GUINT gim_create_common_buffer(GUINT buffer_size, GBUFFER_ID * buffer_id) |
---|
486 | { |
---|
487 | return gim_create_buffer(G_BUFFER_MANAGER_SYSTEM,buffer_size,G_MU_DYNAMIC_READ_WRITE,buffer_id); |
---|
488 | } |
---|
489 | |
---|
490 | GUINT gim_create_common_buffer_from_data( |
---|
491 | const void * pdata, GUINT buffer_size, GBUFFER_ID * buffer_id) |
---|
492 | { |
---|
493 | return gim_create_buffer_from_data(G_BUFFER_MANAGER_SYSTEM,pdata,buffer_size,G_MU_DYNAMIC_READ_WRITE,buffer_id); |
---|
494 | } |
---|
495 | |
---|
496 | GUINT gim_create_shared_buffer_from_data( |
---|
497 | const void * pdata, GUINT buffer_size, GBUFFER_ID * buffer_id) |
---|
498 | { |
---|
499 | return gim_create_buffer_from_data(G_BUFFER_MANAGER_SHARED,pdata,buffer_size,G_MU_DYNAMIC_READ_WRITE,buffer_id); |
---|
500 | } |
---|
501 | |
---|
502 | GINT gim_buffer_realloc(GBUFFER_ID * buffer_id,GUINT newsize) |
---|
503 | { |
---|
504 | VALIDATE_BUFFER_ID_PT(buffer_id) |
---|
505 | if(pbuffer->m_lock_count>0) return G_BUFFER_OP_INVALID; |
---|
506 | GUINT newhandle = bm_data->m_prototype.realloc_fn(pbuffer->m_buffer_handle,pbuffer->m_size,pbuffer->m_usage,newsize,pbuffer->m_usage); |
---|
507 | if(newhandle==0) return G_BUFFER_OP_INVALID; |
---|
508 | pbuffer->m_buffer_handle = newhandle; |
---|
509 | //realloc shadow buffer if any |
---|
510 | gim_buffer_realloc(&pbuffer->m_shadow_buffer,newsize); |
---|
511 | return G_BUFFER_OP_SUCCESS; |
---|
512 | } |
---|
513 | |
---|
514 | GINT gim_buffer_add_ref(GBUFFER_ID * buffer_id) |
---|
515 | { |
---|
516 | VALIDATE_BUFFER_ID_PT(buffer_id) |
---|
517 | pbuffer->m_refcount++; |
---|
518 | return G_BUFFER_OP_SUCCESS; |
---|
519 | } |
---|
520 | |
---|
521 | GINT gim_buffer_free(GBUFFER_ID * buffer_id) |
---|
522 | { |
---|
523 | VALIDATE_BUFFER_ID_PT(buffer_id) |
---|
524 | if(pbuffer->m_lock_count>0) return G_BUFFER_OP_INVALID; |
---|
525 | if(pbuffer->m_refcount>0) pbuffer->m_refcount--; |
---|
526 | if(pbuffer->m_refcount>0) return G_BUFFER_OP_STILLREFCOUNTED; |
---|
527 | |
---|
528 | bm_data->m_prototype.free_fn(pbuffer->m_buffer_handle,pbuffer->m_size); |
---|
529 | //destroy shadow buffer if needed |
---|
530 | gim_buffer_free(&pbuffer->m_shadow_buffer); |
---|
531 | // Obtain a free slot index for a new buffer |
---|
532 | GIM_DYNARRAY_PUSH_ITEM(GUINT,bm_data->m_free_positions,buffer_id->m_buffer_id); |
---|
533 | pbuffer->m_buffer_handle = 0; |
---|
534 | pbuffer->m_size = 0; |
---|
535 | pbuffer->m_shadow_buffer.m_buffer_id = G_UINT_INFINITY; |
---|
536 | pbuffer->m_shadow_buffer.m_buffer_manager_id = G_UINT_INFINITY; |
---|
537 | return G_BUFFER_OP_SUCCESS; |
---|
538 | } |
---|
539 | |
---|
540 | GINT gim_lock_buffer(GBUFFER_ID * buffer_id,int access,char ** map_pointer) |
---|
541 | { |
---|
542 | VALIDATE_BUFFER_ID_PT(buffer_id) |
---|
543 | if(pbuffer->m_lock_count>0) |
---|
544 | { |
---|
545 | if(pbuffer->m_access!=access) return G_BUFFER_OP_INVALID; |
---|
546 | pbuffer->m_lock_count++; |
---|
547 | *map_pointer = pbuffer->m_mapped_pointer; |
---|
548 | return G_BUFFER_OP_SUCCESS; |
---|
549 | } |
---|
550 | |
---|
551 | pbuffer->m_access = access; |
---|
552 | |
---|
553 | GUINT result; |
---|
554 | if(pbuffer->m_usage==G_MU_STATIC_WRITE) |
---|
555 | { |
---|
556 | *map_pointer = 0;///no access |
---|
557 | return G_BUFFER_OP_INVALID; |
---|
558 | } |
---|
559 | else if(pbuffer->m_usage==G_MU_STATIC_READ) |
---|
560 | { |
---|
561 | if(pbuffer->m_access == G_MA_READ_ONLY) |
---|
562 | { |
---|
563 | result = gim_lock_buffer(&pbuffer->m_shadow_buffer,access,map_pointer); |
---|
564 | if(result!= G_BUFFER_OP_SUCCESS) return G_BUFFER_OP_INVALID; |
---|
565 | pbuffer->m_mapped_pointer = *map_pointer; |
---|
566 | pbuffer->m_lock_count++; |
---|
567 | } |
---|
568 | else |
---|
569 | { |
---|
570 | *map_pointer = 0; |
---|
571 | return G_BUFFER_OP_INVALID; |
---|
572 | } |
---|
573 | } |
---|
574 | else if(pbuffer->m_usage==G_MU_STATIC_READ_DYNAMIC_WRITE) |
---|
575 | { |
---|
576 | if(pbuffer->m_access == G_MA_READ_ONLY) |
---|
577 | { |
---|
578 | result = gim_lock_buffer(&pbuffer->m_shadow_buffer,access,map_pointer); |
---|
579 | if(result!= G_BUFFER_OP_SUCCESS) return G_BUFFER_OP_INVALID; |
---|
580 | pbuffer->m_mapped_pointer = *map_pointer; |
---|
581 | pbuffer->m_lock_count++; |
---|
582 | } |
---|
583 | else if(pbuffer->m_access == G_MA_WRITE_ONLY) |
---|
584 | { |
---|
585 | pbuffer->m_mapped_pointer = bm_data->m_prototype.lock_buffer_fn(pbuffer->m_buffer_handle,access); |
---|
586 | *map_pointer = pbuffer->m_mapped_pointer; |
---|
587 | pbuffer->m_lock_count++; |
---|
588 | } |
---|
589 | else if(pbuffer->m_access == G_MA_READ_WRITE) |
---|
590 | { |
---|
591 | *map_pointer = 0; |
---|
592 | return G_BUFFER_OP_INVALID; |
---|
593 | } |
---|
594 | } |
---|
595 | else if(pbuffer->m_usage==G_MU_STATIC_READ_DYNAMIC_WRITE_COPY) |
---|
596 | { |
---|
597 | result = gim_lock_buffer(&pbuffer->m_shadow_buffer,access,map_pointer); |
---|
598 | if(result!= G_BUFFER_OP_SUCCESS) return G_BUFFER_OP_INVALID; |
---|
599 | pbuffer->m_mapped_pointer = *map_pointer; |
---|
600 | pbuffer->m_lock_count++; |
---|
601 | } |
---|
602 | else if(pbuffer->m_usage==G_MU_STATIC_WRITE_DYNAMIC_READ) |
---|
603 | { |
---|
604 | if(pbuffer->m_access == G_MA_READ_ONLY) |
---|
605 | { |
---|
606 | pbuffer->m_mapped_pointer = bm_data->m_prototype.lock_buffer_fn(pbuffer->m_buffer_handle,access); |
---|
607 | *map_pointer = pbuffer->m_mapped_pointer; |
---|
608 | pbuffer->m_lock_count++; |
---|
609 | } |
---|
610 | else |
---|
611 | { |
---|
612 | *map_pointer = 0; |
---|
613 | return G_BUFFER_OP_INVALID; |
---|
614 | } |
---|
615 | } |
---|
616 | else if(pbuffer->m_usage==G_MU_DYNAMIC_READ_WRITE) |
---|
617 | { |
---|
618 | pbuffer->m_mapped_pointer = bm_data->m_prototype.lock_buffer_fn(pbuffer->m_buffer_handle,access); |
---|
619 | *map_pointer = pbuffer->m_mapped_pointer; |
---|
620 | pbuffer->m_lock_count++; |
---|
621 | } |
---|
622 | return G_BUFFER_OP_SUCCESS; |
---|
623 | } |
---|
624 | |
---|
625 | GINT gim_unlock_buffer(GBUFFER_ID * buffer_id) |
---|
626 | { |
---|
627 | VALIDATE_BUFFER_ID_PT(buffer_id) |
---|
628 | if(pbuffer->m_lock_count==0) return G_BUFFER_OP_INVALID; |
---|
629 | |
---|
630 | if(pbuffer->m_lock_count>1) |
---|
631 | { |
---|
632 | pbuffer->m_lock_count--; |
---|
633 | return G_BUFFER_OP_SUCCESS; |
---|
634 | } |
---|
635 | |
---|
636 | |
---|
637 | GUINT result; |
---|
638 | if(pbuffer->m_usage==G_MU_STATIC_WRITE) |
---|
639 | { |
---|
640 | pbuffer->m_mapped_pointer = 0; |
---|
641 | pbuffer->m_lock_count=0; |
---|
642 | return G_BUFFER_OP_INVALID; |
---|
643 | } |
---|
644 | else if(pbuffer->m_usage==G_MU_STATIC_READ) |
---|
645 | { |
---|
646 | if(pbuffer->m_access == G_MA_READ_ONLY) |
---|
647 | { |
---|
648 | result = gim_unlock_buffer(&pbuffer->m_shadow_buffer); |
---|
649 | if(result!= G_BUFFER_OP_SUCCESS) return G_BUFFER_OP_INVALID; |
---|
650 | pbuffer->m_mapped_pointer = 0; |
---|
651 | pbuffer->m_lock_count=0; |
---|
652 | } |
---|
653 | else |
---|
654 | { |
---|
655 | pbuffer->m_mapped_pointer = 0; |
---|
656 | pbuffer->m_lock_count=0; |
---|
657 | return G_BUFFER_OP_INVALID; |
---|
658 | } |
---|
659 | } |
---|
660 | else if(pbuffer->m_usage==G_MU_STATIC_READ_DYNAMIC_WRITE) |
---|
661 | { |
---|
662 | if(pbuffer->m_access == G_MA_READ_ONLY) |
---|
663 | { |
---|
664 | result = gim_unlock_buffer(&pbuffer->m_shadow_buffer); |
---|
665 | if(result!= G_BUFFER_OP_SUCCESS) return G_BUFFER_OP_INVALID; |
---|
666 | pbuffer->m_mapped_pointer = 0; |
---|
667 | pbuffer->m_lock_count=0; |
---|
668 | } |
---|
669 | else if(pbuffer->m_access == G_MA_WRITE_ONLY) |
---|
670 | { |
---|
671 | bm_data->m_prototype.unlock_buffer_fn(pbuffer->m_buffer_handle); |
---|
672 | pbuffer->m_mapped_pointer = 0; |
---|
673 | pbuffer->m_lock_count=0; |
---|
674 | } |
---|
675 | else if(pbuffer->m_access == G_MA_READ_WRITE) |
---|
676 | { |
---|
677 | pbuffer->m_mapped_pointer = 0; |
---|
678 | pbuffer->m_lock_count=0; |
---|
679 | return G_BUFFER_OP_INVALID; |
---|
680 | } |
---|
681 | } |
---|
682 | else if(pbuffer->m_usage==G_MU_STATIC_READ_DYNAMIC_WRITE_COPY) |
---|
683 | { |
---|
684 | result = gim_unlock_buffer(&pbuffer->m_shadow_buffer); |
---|
685 | if(result!= G_BUFFER_OP_SUCCESS) return G_BUFFER_OP_INVALID; |
---|
686 | pbuffer->m_mapped_pointer = 0; |
---|
687 | pbuffer->m_lock_count=0; |
---|
688 | if(pbuffer->m_access == G_MA_WRITE_ONLY||pbuffer->m_access == G_MA_READ_WRITE) |
---|
689 | { |
---|
690 | gim_copy_buffers(&pbuffer->m_shadow_buffer,0,buffer_id,0,pbuffer->m_size); |
---|
691 | } |
---|
692 | } |
---|
693 | else if(pbuffer->m_usage==G_MU_STATIC_WRITE_DYNAMIC_READ) |
---|
694 | { |
---|
695 | if(pbuffer->m_access == G_MA_READ_ONLY) |
---|
696 | { |
---|
697 | bm_data->m_prototype.unlock_buffer_fn(pbuffer->m_buffer_handle); |
---|
698 | pbuffer->m_mapped_pointer = 0; |
---|
699 | pbuffer->m_lock_count=0; |
---|
700 | } |
---|
701 | else |
---|
702 | { |
---|
703 | pbuffer->m_mapped_pointer = 0; |
---|
704 | pbuffer->m_lock_count=0; |
---|
705 | return G_BUFFER_OP_INVALID; |
---|
706 | } |
---|
707 | } |
---|
708 | else if(pbuffer->m_usage==G_MU_DYNAMIC_READ_WRITE) |
---|
709 | { |
---|
710 | bm_data->m_prototype.unlock_buffer_fn(pbuffer->m_buffer_handle); |
---|
711 | pbuffer->m_mapped_pointer = 0; |
---|
712 | pbuffer->m_lock_count=0; |
---|
713 | } |
---|
714 | return G_BUFFER_OP_SUCCESS; |
---|
715 | } |
---|
716 | |
---|
717 | GINT gim_get_buffer_size(GBUFFER_ID * buffer_id,GUINT * buffer_size) |
---|
718 | { |
---|
719 | VALIDATE_BUFFER_ID_PT(buffer_id) |
---|
720 | *buffer_size = pbuffer->m_size; |
---|
721 | return G_BUFFER_OP_SUCCESS; |
---|
722 | } |
---|
723 | |
---|
724 | GINT gim_get_buffer_is_locked(GBUFFER_ID * buffer_id,GUINT * lock_count) |
---|
725 | { |
---|
726 | VALIDATE_BUFFER_ID_PT(buffer_id) |
---|
727 | *lock_count = pbuffer->m_lock_count; |
---|
728 | return G_BUFFER_OP_SUCCESS; |
---|
729 | } |
---|
730 | |
---|
731 | |
---|
732 | GINT gim_download_from_buffer( |
---|
733 | GBUFFER_ID * buffer_id, |
---|
734 | GUINT source_pos, |
---|
735 | void * destdata, |
---|
736 | GUINT copysize) |
---|
737 | { |
---|
738 | VALIDATE_BUFFER_ID_PT(buffer_id) |
---|
739 | bm_data->m_prototype.download_from_buffer_fn( |
---|
740 | pbuffer->m_buffer_handle,source_pos,destdata,copysize); |
---|
741 | return G_BUFFER_OP_SUCCESS; |
---|
742 | } |
---|
743 | |
---|
744 | GINT gim_upload_to_buffer( |
---|
745 | GBUFFER_ID * buffer_id, |
---|
746 | GUINT dest_pos, |
---|
747 | void * sourcedata, |
---|
748 | GUINT copysize) |
---|
749 | { |
---|
750 | VALIDATE_BUFFER_ID_PT(buffer_id) |
---|
751 | bm_data->m_prototype.upload_to_buffer_fn( |
---|
752 | pbuffer->m_buffer_handle,dest_pos,sourcedata,copysize); |
---|
753 | return G_BUFFER_OP_SUCCESS; |
---|
754 | } |
---|
755 | |
---|
756 | GINT gim_copy_buffers( |
---|
757 | GBUFFER_ID * source_buffer_id, |
---|
758 | GUINT source_pos, |
---|
759 | GBUFFER_ID * dest_buffer_id, |
---|
760 | GUINT dest_pos, |
---|
761 | GUINT copysize) |
---|
762 | { |
---|
763 | GBUFFER_MANAGER_DATA * bm_data1,* bm_data2; |
---|
764 | GBUFFER_DATA * pbuffer1, * pbuffer2; |
---|
765 | void * tempdata; |
---|
766 | if(_validate_buffer_id(source_buffer_id,&pbuffer1,&bm_data1)!= G_BUFFER_OP_SUCCESS) return G_BUFFER_OP_INVALID; |
---|
767 | |
---|
768 | if(_validate_buffer_id(dest_buffer_id,&pbuffer2,&bm_data2)!= G_BUFFER_OP_SUCCESS) return G_BUFFER_OP_INVALID; |
---|
769 | |
---|
770 | if((source_buffer_id->m_buffer_manager_id == dest_buffer_id->m_buffer_manager_id)|| |
---|
771 | (source_buffer_id->m_buffer_manager_id == G_BUFFER_MANAGER_SYSTEM && dest_buffer_id->m_buffer_manager_id == G_BUFFER_MANAGER_SHARED)|| |
---|
772 | (source_buffer_id->m_buffer_manager_id == G_BUFFER_MANAGER_SHARED && dest_buffer_id->m_buffer_manager_id == G_BUFFER_MANAGER_SYSTEM) |
---|
773 | ) |
---|
774 | {//smooth copy |
---|
775 | bm_data1->m_prototype.copy_buffers_fn(pbuffer1->m_buffer_handle,source_pos,pbuffer2->m_buffer_handle,dest_pos,copysize); |
---|
776 | } |
---|
777 | else if(source_buffer_id->m_buffer_manager_id == G_BUFFER_MANAGER_SYSTEM || source_buffer_id->m_buffer_manager_id == G_BUFFER_MANAGER_SHARED) |
---|
778 | { |
---|
779 | //hard copy |
---|
780 | tempdata = (void *)pbuffer1->m_buffer_handle; |
---|
781 | //upload data |
---|
782 | bm_data2->m_prototype.upload_to_buffer_fn(pbuffer2->m_buffer_handle,dest_pos, |
---|
783 | tempdata, |
---|
784 | copysize); |
---|
785 | } |
---|
786 | else |
---|
787 | { |
---|
788 | //very hard copy |
---|
789 | void * tempdata = gim_alloc(copysize); |
---|
790 | //download data |
---|
791 | bm_data1->m_prototype.download_from_buffer_fn(pbuffer1->m_buffer_handle,source_pos, |
---|
792 | tempdata, |
---|
793 | copysize); |
---|
794 | |
---|
795 | //upload data |
---|
796 | bm_data2->m_prototype.upload_to_buffer_fn(pbuffer2->m_buffer_handle,dest_pos, |
---|
797 | tempdata, |
---|
798 | copysize); |
---|
799 | //delete temp buffer |
---|
800 | gim_free(tempdata,copysize); |
---|
801 | } |
---|
802 | return G_BUFFER_OP_SUCCESS; |
---|
803 | } |
---|
804 | |
---|
805 | GINT gim_buffer_array_lock(GBUFFER_ARRAY * array_data, int access) |
---|
806 | { |
---|
807 | if(array_data->m_buffer_data != 0) return G_BUFFER_OP_SUCCESS; |
---|
808 | GINT result = gim_lock_buffer(&array_data->m_buffer_id,access,&array_data->m_buffer_data); |
---|
809 | if(result!= G_BUFFER_OP_SUCCESS) return result; |
---|
810 | array_data->m_buffer_data += array_data->m_byte_offset; |
---|
811 | return result; |
---|
812 | } |
---|
813 | |
---|
814 | GINT gim_buffer_array_unlock(GBUFFER_ARRAY * array_data) |
---|
815 | { |
---|
816 | if(array_data->m_buffer_data == 0) return G_BUFFER_OP_SUCCESS; |
---|
817 | GINT result = gim_unlock_buffer(&array_data->m_buffer_id); |
---|
818 | if(result!= G_BUFFER_OP_SUCCESS) return result; |
---|
819 | array_data->m_buffer_data = 0; |
---|
820 | return result; |
---|
821 | } |
---|
822 | |
---|
823 | void gim_buffer_array_copy_ref(GBUFFER_ARRAY * source_data,GBUFFER_ARRAY * dest_data) |
---|
824 | { |
---|
825 | dest_data->m_buffer_id.m_buffer_id = source_data->m_buffer_id.m_buffer_id; |
---|
826 | dest_data->m_buffer_id.m_buffer_manager_id = source_data->m_buffer_id.m_buffer_manager_id; |
---|
827 | dest_data->m_buffer_data = 0; |
---|
828 | dest_data->m_byte_stride = source_data->m_byte_stride; |
---|
829 | dest_data->m_byte_offset = source_data->m_byte_offset; |
---|
830 | dest_data->m_element_count = source_data->m_element_count; |
---|
831 | gim_buffer_add_ref(&dest_data->m_buffer_id); |
---|
832 | } |
---|
833 | |
---|
834 | void gim_buffer_array_copy_value(GBUFFER_ARRAY * source_data,GBUFFER_ARRAY * dest_data, GUINT buffer_manager_id,int usage) |
---|
835 | { |
---|
836 | //Create new buffer |
---|
837 | GUINT buffsize = source_data->m_element_count*source_data->m_byte_stride; |
---|
838 | gim_create_buffer(buffer_manager_id,buffsize,usage,&dest_data->m_buffer_id); |
---|
839 | |
---|
840 | //copy ref data |
---|
841 | dest_data->m_buffer_data = 0; |
---|
842 | dest_data->m_byte_stride = source_data->m_byte_stride; |
---|
843 | dest_data->m_byte_offset = 0; |
---|
844 | dest_data->m_element_count = source_data->m_element_count; |
---|
845 | gim_buffer_add_ref(&dest_data->m_buffer_id); |
---|
846 | //copy buffers |
---|
847 | gim_copy_buffers(&source_data->m_buffer_id,source_data->m_byte_offset,&dest_data->m_buffer_id,0,buffsize); |
---|
848 | } |
---|