3 * Mesa 3-D graphics library
6 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
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:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
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.
26 * Keith Whitwell <keith@tungstengraphics.com>
33 #include "t_imm_alloc.h"
36 static int id
= 0; /* give each struct immediate a unique ID number */
39 static struct immediate
*
40 real_alloc_immediate( GLcontext
*ctx
)
42 struct immediate
*immed
= ALIGN_CALLOC_STRUCT( immediate
, 32 );
50 immed
->LastPrimitive
= IMM_MAX_COPIED_VERTS
;
51 immed
->Count
= IMM_MAX_COPIED_VERTS
;
52 immed
->Start
= IMM_MAX_COPIED_VERTS
;
54 immed
->MaterialMask
= 0;
55 immed
->MaxTextureUnits
= ctx
->Const
.MaxTextureCoordUnits
;
57 immed
->NormalLengthPtr
= 0;
59 /* Only allocate space for vertex positions right now. Color, texcoord,
60 * etc storage will be allocated as needed.
62 immed
->Attrib
[VERT_ATTRIB_POS
] = (GLfloat (*)[4]) _mesa_malloc(IMM_SIZE
* 4 * sizeof(GLfloat
));
64 /* Enable this to allocate all attribute arrays up front */
68 for (i
= 1; i
< VERT_ATTRIB_MAX
; i
++) {
69 immed
->Attrib
[i
] = (GLfloat (*)[4]) _mesa_malloc(IMM_SIZE
* 4 * sizeof(GLfloat
));
73 immed
->CopyTexSize
= 0;
74 immed
->CopyStart
= immed
->Start
;
81 real_free_immediate( struct immediate
*immed
)
86 for (i
=0; i
< VERT_ATTRIB_MAX
; i
++) {
88 _mesa_free(immed
->Attrib
[i
]);
89 immed
->Attrib
[i
] = NULL
;
92 if (immed
->Material
) {
93 FREE( immed
->Material
);
94 FREE( immed
->MaterialMask
);
96 immed
->MaterialMask
= 0;
99 if (immed
->NormalLengthPtr
)
100 ALIGN_FREE( immed
->NormalLengthPtr
);
104 /* printf("outstanding %d\n", id - freed); */
109 * Return a pointer to a new 'struct immediate' object.
110 * We actually keep a spare/cached one to reduce malloc calls.
113 _tnl_alloc_immediate( GLcontext
*ctx
)
115 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
116 struct immediate
*tmp
= tnl
->freed_immediate
;
119 tnl
->freed_immediate
= 0;
123 return real_alloc_immediate( ctx
);
127 * Free a 'struct immediate' object.
128 * May be called after tnl is destroyed.
131 _tnl_free_immediate( GLcontext
*ctx
, struct immediate
*immed
)
133 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
135 ASSERT(immed
->ref_count
== 0);
137 if (immed
->NormalLengthPtr
) {
138 ALIGN_FREE(immed
->NormalLengthPtr
);
139 immed
->NormalLengthPtr
= NULL
;
143 real_free_immediate( immed
);
146 if (tnl
->freed_immediate
)
147 real_free_immediate( tnl
->freed_immediate
);
149 tnl
->freed_immediate
= immed
;