Assorted casts to silence g++ warnings.
[mesa.git] / src / mesa / tnl / t_imm_alloc.c
1
2 /*
3 * Mesa 3-D graphics library
4 * Version: 5.1
5 *
6 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 * Authors:
26 * Keith Whitwell <keith@tungstengraphics.com>
27 */
28
29 #include "glheader.h"
30 #include "imports.h"
31 #include "mtypes.h"
32
33 #include "t_imm_alloc.h"
34
35
36 static int id = 0; /* give each struct immediate a unique ID number */
37
38
39 static struct immediate *
40 real_alloc_immediate( GLcontext *ctx )
41 {
42 struct immediate *immed = ALIGN_CALLOC_STRUCT( immediate, 32 );
43
44 if (!immed)
45 return NULL;
46
47 immed->id = id++;
48 immed->ref_count = 0;
49 immed->FlushElt = 0;
50 immed->LastPrimitive = IMM_MAX_COPIED_VERTS;
51 immed->Count = IMM_MAX_COPIED_VERTS;
52 immed->Start = IMM_MAX_COPIED_VERTS;
53 immed->Material = 0;
54 immed->MaterialMask = 0;
55 immed->MaxTextureUnits = ctx->Const.MaxTextureCoordUnits;
56 immed->TexSize = 0;
57 immed->NormalLengthPtr = 0;
58
59 /* Only allocate space for vertex positions right now. Color, texcoord,
60 * etc storage will be allocated as needed.
61 */
62 immed->Attrib[VERT_ATTRIB_POS] = (GLfloat (*)[4]) _mesa_malloc(IMM_SIZE * 4 * sizeof(GLfloat));
63
64 /* Enable this to allocate all attribute arrays up front */
65 if (0)
66 {
67 int i;
68 for (i = 1; i < VERT_ATTRIB_MAX; i++) {
69 immed->Attrib[i] = (GLfloat (*)[4]) _mesa_malloc(IMM_SIZE * 4 * sizeof(GLfloat));
70 }
71 }
72
73 immed->CopyTexSize = 0;
74 immed->CopyStart = immed->Start;
75
76 return immed;
77 }
78
79
80 static void
81 real_free_immediate( struct immediate *immed )
82 {
83 static int freed = 0;
84 GLuint i;
85
86 for (i =0; i < VERT_ATTRIB_MAX; i++) {
87 if (immed->Attrib[i])
88 _mesa_free(immed->Attrib[i]);
89 immed->Attrib[i] = NULL;
90 }
91
92 if (immed->Material) {
93 FREE( immed->Material );
94 FREE( immed->MaterialMask );
95 immed->Material = 0;
96 immed->MaterialMask = 0;
97 }
98
99 if (immed->NormalLengthPtr)
100 ALIGN_FREE( immed->NormalLengthPtr );
101
102 ALIGN_FREE( immed );
103 freed++;
104 /* printf("outstanding %d\n", id - freed); */
105 }
106
107
108 /**
109 * Return a pointer to a new 'struct immediate' object.
110 * We actually keep a spare/cached one to reduce malloc calls.
111 */
112 struct immediate *
113 _tnl_alloc_immediate( GLcontext *ctx )
114 {
115 TNLcontext *tnl = TNL_CONTEXT(ctx);
116 struct immediate *tmp = tnl->freed_immediate;
117
118 if (tmp) {
119 tnl->freed_immediate = 0;
120 return tmp;
121 }
122 else
123 return real_alloc_immediate( ctx );
124 }
125
126 /**
127 * Free a 'struct immediate' object.
128 * May be called after tnl is destroyed.
129 */
130 void
131 _tnl_free_immediate( GLcontext *ctx, struct immediate *immed )
132 {
133 TNLcontext *tnl = TNL_CONTEXT(ctx);
134
135 ASSERT(immed->ref_count == 0);
136
137 if (immed->NormalLengthPtr) {
138 ALIGN_FREE(immed->NormalLengthPtr);
139 immed->NormalLengthPtr = NULL;
140 }
141
142 if (!tnl) {
143 real_free_immediate( immed );
144 }
145 else {
146 if (tnl->freed_immediate)
147 real_free_immediate( tnl->freed_immediate );
148
149 tnl->freed_immediate = immed;
150 }
151 }