pb_validate.c revision aef455c4a7bbd7df97a6444ae332cb5fb976e627
1/**************************************************************************
2 *
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * 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
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28/**
29 * @file
30 * Buffer validation.
31 *
32 * @author Jose Fonseca <jrfonseca@tungstengraphics.com>
33 */
34
35
36#include "pipe/p_compiler.h"
37#include "pipe/p_error.h"
38#include "util/u_memory.h"
39#include "pipe/p_debug.h"
40
41#include "pb_buffer.h"
42#include "pb_buffer_fenced.h"
43#include "pb_validate.h"
44
45
46#define PB_VALIDATE_INITIAL_SIZE 1 /* 512 */
47
48
49struct pb_validate
50{
51   struct pb_buffer **buffers;
52   unsigned used;
53   unsigned size;
54};
55
56
57enum pipe_error
58pb_validate_add_buffer(struct pb_validate *vl,
59                       struct pb_buffer *buf)
60{
61   assert(buf);
62   if(!buf)
63      return PIPE_ERROR;
64
65   /* We only need to store one reference for each buffer, so avoid storing
66    * consecutive references for the same buffer. It might not be the more
67    * common pasttern, but it is easy to implement.
68    */
69   if(vl->used && vl->buffers[vl->used - 1] == buf) {
70      return PIPE_OK;
71   }
72
73   /* Grow the table */
74   if(vl->used == vl->size) {
75      unsigned new_size;
76      struct pb_buffer **new_buffers;
77
78      new_size = vl->size * 2;
79      if(!new_size)
80	 return PIPE_ERROR_OUT_OF_MEMORY;
81
82      new_buffers = (struct pb_buffer **)REALLOC(vl->buffers,
83                                                 vl->size*sizeof(struct pb_buffer *),
84                                                 new_size*sizeof(struct pb_buffer *));
85      if(!new_buffers)
86         return PIPE_ERROR_OUT_OF_MEMORY;
87
88      memset(new_buffers + vl->size, 0, (new_size - vl->size)*sizeof(struct pb_buffer *));
89
90      vl->size = new_size;
91      vl->buffers = new_buffers;
92   }
93
94   assert(!vl->buffers[vl->used]);
95   pb_reference(&vl->buffers[vl->used], buf);
96   ++vl->used;
97
98   return PIPE_OK;
99}
100
101
102enum pipe_error
103pb_validate_validate(struct pb_validate *vl)
104{
105   /* FIXME: go through each buffer, ensure its not mapped, its address is
106    * available -- requires a new pb_buffer interface */
107   return PIPE_OK;
108}
109
110
111void
112pb_validate_fence(struct pb_validate *vl,
113                  struct pipe_fence_handle *fence)
114{
115   unsigned i;
116   for(i = 0; i < vl->used; ++i) {
117      buffer_fence(vl->buffers[i], fence);
118      pb_reference(&vl->buffers[i], NULL);
119   }
120   vl->used = 0;
121}
122
123
124void
125pb_validate_destroy(struct pb_validate *vl)
126{
127   unsigned i;
128   for(i = 0; i < vl->used; ++i)
129      pb_reference(&vl->buffers[i], NULL);
130   FREE(vl->buffers);
131   FREE(vl);
132}
133
134
135struct pb_validate *
136pb_validate_create()
137{
138   struct pb_validate *vl;
139
140   vl = CALLOC_STRUCT(pb_validate);
141   if(!vl)
142      return NULL;
143
144   vl->size = PB_VALIDATE_INITIAL_SIZE;
145   vl->buffers = (struct pb_buffer **)CALLOC(vl->size, sizeof(struct pb_buffer *));
146   if(!vl->buffers) {
147      FREE(vl);
148      return NULL;
149   }
150
151   return vl;
152}
153
154