Subversion Repositories shark

Rev

Rev 56 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
56 pj 1
/* $Id: t_imm_alloc.c,v 1.1 2003-02-28 11:48:06 pj Exp $ */
2
 
3
/*
4
 * Mesa 3-D graphics library
5
 * Version:  4.1
6
 *
7
 * Copyright (C) 1999-2002  Brian Paul   All Rights Reserved.
8
 *
9
 * Permission is hereby granted, free of charge, to any person obtaining a
10
 * copy of this software and associated documentation files (the "Software"),
11
 * to deal in the Software without restriction, including without limitation
12
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13
 * and/or sell copies of the Software, and to permit persons to whom the
14
 * Software is furnished to do so, subject to the following conditions:
15
 *
16
 * The above copyright notice and this permission notice shall be included
17
 * in all copies or substantial portions of the Software.
18
 *
19
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
22
 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23
 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
 *
26
 * Authors:
27
 *    Keith Whitwell <keith@tungstengraphics.com>
28
 */
29
 
30
#include "glheader.h"
31
#include "imports.h"
32
#include "mtypes.h"
33
 
34
#include "t_imm_alloc.h"
35
 
36
 
37
static int id = 0;  /* give each struct immediate a unique ID number */
38
 
39
static struct immediate *real_alloc_immediate( GLcontext *ctx )
40
{
41
   struct immediate *IM = ALIGN_CALLOC_STRUCT( immediate, 32 );
42
 
43
   if (!IM)
44
      return 0;
45
 
46
/*     memset(IM, 0, sizeof(*IM)); */
47
 
48
   IM->id = id++;
49
   IM->ref_count = 0;
50
   IM->FlushElt = 0;
51
   IM->LastPrimitive = IMM_MAX_COPIED_VERTS;
52
   IM->Count = IMM_MAX_COPIED_VERTS;
53
   IM->Start = IMM_MAX_COPIED_VERTS;
54
   IM->Material = 0;
55
   IM->MaterialMask = 0;
56
   IM->MaxTextureUnits = ctx->Const.MaxTextureUnits;
57
   IM->TexSize = 0;
58
   IM->NormalLengthPtr = 0;
59
 
60
   IM->CopyTexSize = 0;
61
   IM->CopyStart = IM->Start;
62
 
63
   return IM;
64
}
65
 
66
 
67
static void real_free_immediate( struct immediate *IM )
68
{
69
   static int freed = 0;
70
 
71
   if (IM->Material) {
72
      FREE( IM->Material );
73
      FREE( IM->MaterialMask );
74
      IM->Material = 0;
75
      IM->MaterialMask = 0;
76
   }
77
 
78
   if (IM->NormalLengthPtr)
79
      ALIGN_FREE( IM->NormalLengthPtr );
80
 
81
   ALIGN_FREE( IM );
82
   freed++;
83
/*     printf("outstanding %d\n", id - freed);    */
84
}
85
 
86
 
87
/* Cache a single allocated immediate struct.
88
 */
89
struct immediate *_tnl_alloc_immediate( GLcontext *ctx )
90
{
91
   TNLcontext *tnl = TNL_CONTEXT(ctx);
92
   struct immediate *tmp = tnl->freed_immediate;
93
 
94
   if (tmp) {
95
      tnl->freed_immediate = 0;
96
      return tmp;
97
   }
98
   else
99
      return real_alloc_immediate( ctx );
100
}
101
 
102
/* May be called after tnl is destroyed.
103
 */
104
void _tnl_free_immediate( GLcontext *ctx, struct immediate *IM )
105
{
106
   TNLcontext *tnl = TNL_CONTEXT(ctx);
107
 
108
   ASSERT(IM->ref_count == 0);
109
 
110
   if (IM->NormalLengthPtr) {
111
      ALIGN_FREE(IM->NormalLengthPtr);
112
      IM->NormalLengthPtr = NULL;
113
   }
114
 
115
   if (!tnl) {
116
      real_free_immediate( IM );
117
   }
118
   else {
119
      if (tnl->freed_immediate)
120
         real_free_immediate( tnl->freed_immediate );
121
 
122
      tnl->freed_immediate = IM;
123
   }
124
}