1 /*
2 * Copyright (C) 1997-2005, R3vis Corporation.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17 * USA, or visit http://www.gnu.org/copyleft/lgpl.html.
18 *
19 * Original Contributor:
20 * Wes Bethel, R3vis Corporation, Marin County, California
21 * Additional Contributor(s):
22 *
23 * The OpenRM project is located at http://openrm.sourceforge.net/.
24 */
25 /*
26 * $Id: rmmalloc.c,v 1.6 2005/02/27 19:34:04 wes Exp $
27 * Version: $Name: OpenRM-1-6-0-2-RC2 $
28 * $Revision: 1.6 $
29 * $Log: rmmalloc.c,v $
30 * Revision 1.6 2005/02/27 19:34:04 wes
31 * Added support for application supplied texture object IDs and display lists.
32 *
33 * Revision 1.5 2005/02/19 16:22:50 wes
34 * Distro sync and consolidation.
35 *
36 * Revision 1.4 2005/01/23 17:04:03 wes
37 * Copyright update to 2005.
38 *
39 * Revision 1.3 2004/01/16 16:46:09 wes
40 * Updated copyright line for 2004.
41 *
42 * Revision 1.2 2003/02/02 02:07:15 wes
43 * Updated copyright to 2003.
44 *
45 * Revision 1.1.1.1 2003/01/28 02:15:23 wes
46 * Manual rebuild of rm150 repository.
47 *
48 * Revision 1.6 2003/01/16 22:21:17 wes
49 * Updated all source files to reflect new organization of header files:
50 * all header files formerly located in include/rmaux, include/rmi, include/rmv
51 * are now located in include/rm.
52 *
53 * Revision 1.5 2002/09/17 14:20:25 wes
54 * Sanitized some comments.
55 *
56 * Revision 1.4 2002/04/30 19:32:22 wes
57 * Updated copyright dates.
58 *
59 * Revision 1.3 2001/03/31 17:12:38 wes
60 * v1.4.0-alpha-2 checkin.
61 *
62 * Revision 1.2 2000/04/20 16:29:47 wes
63 * Documentation additions/enhancements, some code rearragement.
64 *
65 * Revision 1.1.1.1 2000/02/28 21:29:40 wes
66 * OpenRM 1.2 Checkin
67 *
68 * Revision 1.1.1.1 2000/02/28 17:18:48 wes
69 * Initial entry - pre-RM120 release, source base for OpenRM 1.2.
70 *
71 */
72
73 #include <rm/rm.h>
74 #include "rmprivat.h"
75
76 /*
77 * ----------------------------------------------------
78 * @Name rmFloatNew
79 @pstart
80 float * rmFloatNew (int n)
81 @pend
82
83 @astart
84 int n - number of floats to create (input).
85 @aend
86
87 @dstart
88
89 Return a handle to the requested number of floats.
90
91 @dend
92 * ----------------------------------------------------
93 */
94 float *
rmFloatNew(int n)95 rmFloatNew (int n)
96 {
97 return((float *)(malloc(sizeof(float) * n)));
98 }
99
100
101 /*
102 * ----------------------------------------------------
103 * @Name rmFloatDelete
104 @pstart
105 void rmFloatDelete (float *t)
106 @pend
107
108 @astart
109 float *t - a handle to float(s) to delete (modified).
110 @aend
111
112 @dstart
113
114 Free the float resources pointed to by t.
115
116 @dend
117 * ----------------------------------------------------
118 */
119 void
rmFloatDelete(float * t)120 rmFloatDelete (float *t)
121 {
122 free((void *)t);
123 }
124
125
126 /*
127 * ----------------------------------------------------
128 * @Name rmVertex2DNew
129 @pstart
130 RMvertex2D * rmVertex2DNew (int n)
131 @pend
132
133 @astart
134 int n - number of RMvertex2D objects to create (input).
135 @aend
136
137 @dstart
138
139 Return a handle to the requested number of RMvertex2D.
140
141 @dend
142 * ----------------------------------------------------
143 */
144 RMvertex2D *
rmVertex2DNew(int n)145 rmVertex2DNew (int n)
146 {
147 RMvertex2D *v;
148
149 v = (RMvertex2D *)malloc(sizeof(RMvertex2D) * n);
150
151 return(v);
152 }
153
154
155 /*
156 * ----------------------------------------------------
157 * @Name rmVertex2DDelete
158 @pstart
159 void rmVertex2DDelete (RMvertex2D *v)
160 @pend
161
162 @astart
163 RMvertex2D *v - a handle to RMvertex2D object(s) to delete
164 (modified).
165 @aend
166
167 @dstart
168
169 Free the RMvertex2D resources pointed to by v. It is assumed that v
170 points to a valid RMvertex2D previously created with rmVertex2DNew.
171
172 @dend
173 * ----------------------------------------------------
174 */
175 void
rmVertex2DDelete(RMvertex2D * v)176 rmVertex2DDelete (RMvertex2D *v)
177 {
178 if (RM_ASSERT(v, "rmVertex2DDelete() error: input RMvertex2D pointer is NULL.") == RM_WHACKED)
179 return;
180
181 free((void *)v);
182 }
183
184
185 /*
186 * ----------------------------------------------------
187 * @Name rmVertex3DNew
188 @pstart
189 RMvertex3D * rmVertex3DNew (int n)
190 @pend
191
192 @astart
193 int n - number of RMvertex3D objects to create (input).
194 @aend
195
196 @dstart
197
198 Return a handle to the requested number of RMvertex3D.
199
200 @dend
201 * ----------------------------------------------------
202 */
203 RMvertex3D *
rmVertex3DNew(int n)204 rmVertex3DNew (int n)
205 {
206 RMvertex3D *v;
207 v = (RMvertex3D *)malloc(sizeof(RMvertex3D) * n);
208
209 return(v);
210 }
211
212
213 /*
214 * ----------------------------------------------------
215 * @Name rmVertex3DDelete
216 @pstart
217 void rmVertex3DDelete (RMvertex3D *v)
218 @pend
219
220 @astart
221
222 RMvertex3D *v - a handle to RMvertex3D object(s) to delete
223 (modified).
224 @aend
225
226 @dstart
227
228 Free the RMvertex3D resources pointed to by v. It is assumed that v
229 points to a valid RMvertex3D previously created with rmVertex3DNew.
230
231 @dend
232 * ----------------------------------------------------
233 */
234 void
rmVertex3DDelete(RMvertex3D * v)235 rmVertex3DDelete (RMvertex3D *v)
236 {
237 if (RM_ASSERT(v, "rmVertex3DDelete() error: input RMvertex3D pointer is NULL.") == RM_WHACKED)
238 return;
239
240 free(v);
241 }
242
243
244 /*
245 * ----------------------------------------------------
246 * @Name rmColor4DNew
247 @pstart
248 RMcolor4D * rmColor4DNew (int n)
249 @pend
250
251 @astart
252 int n - number of RMcolor4D objects to create (input).
253 @aend
254
255 @dstart
256
257 Return a handle to the requested number of RMcolor4D.
258
259 @dend
260 * ----------------------------------------------------
261 */
262 RMcolor4D *
rmColor4DNew(int n)263 rmColor4DNew (int n)
264 {
265 return(malloc(sizeof(RMcolor4D) * n));
266 }
267
268
269 /*
270 * ----------------------------------------------------
271 * @Name rmColor4DDelete
272 @pstart
273 void rmColor4DDelete (RMcolor4D *c)
274 @pend
275
276 @astart
277 RMcolor4D *c - a handle to RMcolor4D object(s) to delete (modified).
278 @aend
279
280 @dstart
281
282 Free the RMcolor4D resources pointed to by c. It is assumed
283 that c points to a valid RMcolor4D previously created with
284 rmColor4DNew().
285
286 @dend
287 * ----------------------------------------------------
288 */
289 void
rmColor4DDelete(RMcolor4D * c)290 rmColor4DDelete (RMcolor4D *c)
291 {
292 if (c != NULL)
293 free((void *)c);
294 }
295
296
297 /*
298 * ----------------------------------------------------
299 * @Name rmInternalMarker2DNew
300 @pstart
301 RMinternalMarker2D * rmInternalMarker2DNew (int nverts,
302 int begin_flag,
303 RMvertex2D *dverts)
304 @pend
305
306 @astart
307 int nverts - number of vertices in the RMinternalMarker2D list.
308
309 int begin_flag - ?
310
311 RMvertex2D *dverts - vertex data.
312 @aend
313
314 @dstart
315
316 This routine may be deprecated. Need to add checks to see if this is
317 actually being used by any contemporary applications.
318
319 Create a new RMinternalMarker2D from the given parameters and return
320 a handle to the caller.
321
322 @dend
323 * ----------------------------------------------------
324 */
325 RMinternalMarker2D *
rmInternalMarker2DNew(int nverts,int begin_flag,RMvertex2D * dverts)326 rmInternalMarker2DNew (int nverts,
327 int begin_flag,
328 RMvertex2D *dverts)
329 {
330 RMinternalMarker2D *t;
331
332 t = (RMinternalMarker2D *)malloc(sizeof(RMinternalMarker2D));
333 memset(t, 0, sizeof(RMinternalMarker2D));
334
335 t->vlist = rmVertex2DNew(nverts);
336 memcpy(t->vlist, dverts, sizeof(RMvertex2D)*nverts);
337 rmInternalMarker2DSetNpts(t, nverts);
338 rmInternalMarker2DSetBFlag(t, begin_flag);
339
340 return(t);
341 }
342
343
344 /*
345 * ----------------------------------------------------
346 * @Name rmInternalMarker2DDelete
347 @pstart
348 void rmInternalMarker2DDelete (RMinternalMarker2D *t)
349 @pend
350
351 @astart
352 RMinternalMarker2D *t - a handle to RMinternalMarker2D object to
353 delete (modified).
354 @aend
355
356 @dstart
357
358 Free the RMinternalMarker2D resources pointed to by t. It is assumed
359 that t points to a valid RMinternalMarker2D previously created with
360 rmInternalMarker2DNew().
361
362 @dend
363 * ----------------------------------------------------
364 */
365 void
rmInternalMarker2DDelete(RMinternalMarker2D * t)366 rmInternalMarker2DDelete (RMinternalMarker2D *t)
367 {
368 free(t->vlist);
369 free(t);
370 }
371
372
373 /*
374 * ----------------------------------------------------
375 * @Name rmMalloc2DByteBuffer
376 @pstart
377 unsigned char ** rmMalloc2DByteBuffer (int width,
378 int height)
379 @pend
380
381 @astart
382 int width - buffer width in bytes (input).
383
384 int height - buffer height in bytes (input).
385 @aend
386
387 @dstart
388
389 Creates a 2D byte buffer of the requested dimensions and returns a
390 handle to the caller. The resulting buffer is row-major order, so
391 indexing it as an array of unsigned char means that the first index
392 is the row (height) and the second index is the column (width).
393
394 @dend
395 * ----------------------------------------------------
396 */
397 unsigned char **
rmMalloc2DByteBuffer(int width,int height)398 rmMalloc2DByteBuffer (int width,
399 int height)
400 {
401 int i;
402 unsigned char **c2;
403 unsigned char *c;
404
405 c = (unsigned char *)malloc(sizeof(unsigned char) * width * height);
406 c2 = (unsigned char **)malloc(sizeof(unsigned char *) * height);
407
408 for (i = 0; i < height; i++)
409 c2[i] = c + (i * width);
410
411 memset(c, 0, sizeof(unsigned char) * width * height);
412
413 return(c2);
414 }
415
416
417 /*
418 * ----------------------------------------------------
419 * @Name rmFree2DByteBuffer
420 @pstart
421 void rmFree2DByteBuffer (unsigned char **c)
422 @pend
423
424 @astart
425 unsigned char **c - a handle to a 2D byte buffer (modified).
426 @aend
427
428 @dstart
429
430 Free the 2D byte buffer resources pointed to by c. It is assumed
431 that c points to a valid 2D byte buffer previously created with
432 rmMalloc2DByteBuffer().
433
434 @dend
435 * ----------------------------------------------------
436 */
437 void
rmFree2DByteBuffer(unsigned char ** c)438 rmFree2DByteBuffer (unsigned char **c)
439 {
440 free(c[0]);
441 free(c);
442 }
443
444
445 /*
446 * ----------------------------------------------------
447 * @Name rmMalloc2DFloatBuffer
448 @pstart
449 float ** rmMalloc2DFloatBuffer (int width,
450 int height)
451 @pend
452
453 @astart
454 int width - buffer width in floats (input).
455
456 int height - buffer height in floats (input).
457 @aend
458
459 @dstart
460
461 Creates a 2D float buffer of the requested dimensions and returns a
462 handle to the caller. The resulting buffer is row-major order, so
463 indexing it as an array of unsigned char means that the first index
464 is the row (height) and the second index is the column (width).
465
466 @dend
467 * ----------------------------------------------------
468 */
469 float **
rmMalloc2DFloatBuffer(int width,int height)470 rmMalloc2DFloatBuffer (int width,
471 int height)
472 {
473 int i;
474 float **c2;
475 float *c;
476
477 c = (float *)malloc(sizeof(float) * width * height);
478 c2 = (float **)malloc(sizeof(float *) * height);
479
480 for (i = 0; i < height; i++)
481 c2[i] = c + (i * width);
482
483 return(c2);
484 }
485
486
487 /*
488 * ----------------------------------------------------
489 * @Name rmFree2DFloatBuffer
490 @pstart
491 void rmFree2DFloatBuffer (float **c)
492 @pend
493
494 @astart
495 float **c - a handle to a 2D byte buffer (modified).
496 @aend
497
498 @dstart
499
500 Free the 2D float buffer resources pointed to by c. It is assumed
501 that c points to a valid 2D float buffer previously created with
502 rmMalloc2DFloatBuffer().
503
504 @dend
505 * ----------------------------------------------------
506 */
507 void
rmFree2DFloatBuffer(float ** c)508 rmFree2DFloatBuffer (float **c)
509 {
510 free(c[0]);
511 free(c);
512 }
513
514
515 /*
516 * ----------------------------------------------------
517 * @Name rmMalloc3DByteBuffer
518 @pstart
519 unsigned char *** rmMalloc3DByteBuffer (int width,
520 int height,
521 int depth)
522 @pend
523
524 @astart
525 int width - buffer width in bytes (input).
526
527 int height - buffer height in bytes (input).
528
529 int depth - buffer depth in bytes (input).
530 @aend
531
532 @dstart
533
534 Creates a 3D byte buffer of the requested dimensions and returns a
535 handle to the caller. The resulting buffer is row-major order, so
536 indexing it as an array of unsigned char means that the first index
537 is the row (height) and the second index is the column (width).
538
539 @dend
540 * ----------------------------------------------------
541 */
542 unsigned char ***
rmMalloc3DByteBuffer(int width,int height,int depth)543 rmMalloc3DByteBuffer (int width,
544 int height,
545 int depth)
546 {
547 int i;
548 unsigned char ***c3;
549 unsigned char **c2;
550 unsigned char *c;
551
552 c = (unsigned char *)malloc(sizeof(unsigned char) * width * height * depth);
553 c2 = (unsigned char **)malloc(sizeof(unsigned char *) * height * depth);
554 c3 = (unsigned char ***)malloc(sizeof(unsigned char **) * depth);
555
556 for (i = 0; i < (height * depth); i++)
557 c2[i] = c + (i * width);
558
559 for (i = 0; i < depth; i++)
560 c3[i] = c2 + (depth * i);
561
562 memset(c, 0, sizeof(unsigned char) * width * height * depth);
563
564 return(c3);
565 }
566
567
568 /*
569 * ----------------------------------------------------
570 * @Name rmFree3DByteBuffer
571 @pstart
572 void rmFree3DByteBuffer (unsigned char ***c)
573 @pend
574
575 @astart
576 unsigned char ***c - a handle to a 3D byte buffer (modified).
577 @aend
578
579 @dstart
580
581 Free the 3D byte buffer resources pointed to by c. It is assumed
582 that c points to a valid 3D byte buffer previously created with
583 rmMalloc3DByteBuffer().
584
585 @dend
586 * ----------------------------------------------------
587 */
588 void
rmFree3DByteBuffer(unsigned char *** c)589 rmFree3DByteBuffer (unsigned char ***c)
590 {
591 free(c[0][0]);
592 free(c[0]);
593 free(c);
594 }
595
596
597 /*
598 * ----------------------------------------------------
599 * @Name rmMalloc3DFloatBuffer
600 @pstart
601 float *** rmMalloc3DFloatBuffer (int width,
602 int height,
603 int depth)
604 @pend
605
606 @astart
607 int width - buffer width in floats (input).
608
609 int height - buffer height in floats (input).
610
611 int depth - buffer depth in floats (input).
612 @aend
613
614 @dstart
615
616 Creates a 3D float buffer of the requested dimensions and returns a
617 handle to the caller. The resulting buffer is row-major order, so
618 indexing it as an array of unsigned char means that the first index
619 is the row (height) and the second index is the column (width).
620
621 @dend
622 * ----------------------------------------------------
623 */
624 float ***
rmMalloc3DFloatBuffer(int width,int height,int depth)625 rmMalloc3DFloatBuffer (int width,
626 int height,
627 int depth)
628 {
629 int i;
630 float ***c3;
631 float **c2;
632 float *c;
633
634 c = (float *)malloc(sizeof(float) * width * height * depth);
635 c2 = (float **)malloc(sizeof(float *) * height * depth);
636 c3 = (float ***)malloc(sizeof(float **) * depth);
637
638 for (i = 0; i < (height * depth); i++)
639 c2[i] = c + (i * width);
640
641 for (i = 0; i < depth; i++)
642 c3[i] = c2 + (depth * i);
643
644 memset(c, 0, sizeof(float) * width * height * depth);
645
646 return(c3);
647 }
648
649
650 /*
651 * ----------------------------------------------------
652 * @Name rmFree3DFloatBuffer
653 @pstart
654 void rmFree3DFloatBuffer (float ***c)
655 @pend
656
657 @astart
658 float ***c - a handle to a 3D float buffer (modified).
659 @aend
660
661 @dstart
662
663 Free the 3D float buffer resources pointed to by c. It is assumed
664 that c points to a valid 3D float buffer previously created with
665 rmMalloc3DFloatBuffer().
666
667 @dend
668 * ----------------------------------------------------
669 */
670 void
rmFree3DFloatBuffer(float *** c)671 rmFree3DFloatBuffer (float ***c)
672 {
673 free(c[0][0]);
674 free(c[0]);
675 free(c);
676 }
677
678
679 /* PRIVATE
680 *
681 * private_rmAssert is called by the macro RM_ASSERT()
682 */
683 int
private_rmAssert(const void * a,const char * s)684 private_rmAssert (const void *a,
685 const char *s)
686 {
687 if (a == NULL)
688 {
689 rmError(s);
690 return(RM_WHACKED);
691 }
692 return(RM_CHILL);
693 }
694
695 void *
private_rmMemDup(void * src,int sizeOfInput)696 private_rmMemDup(void *src,
697 int sizeOfInput)
698 {
699 void *dst;
700 dst = (void *)malloc(sizeOfInput);
701
702 if (dst == NULL)
703 {
704 char buf[256];
705 sprintf(buf, "private_rmMemDup() error - unable to duplicate %d bytes of memory.", sizeOfInput);
706 return NULL;
707 }
708
709 memcpy(dst, src, sizeOfInput);
710 return dst;
711 }
712 /* EOF */
713