Path: blob/21.2-virgl/src/gallium/auxiliary/pipebuffer/pb_validate.c
4565 views
/**************************************************************************1*2* Copyright 2008 VMware, Inc.3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sub license, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* The above copyright notice and this permission notice (including the14* next paragraph) shall be included in all copies or substantial portions15* of the Software.16*17* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS18* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.20* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR21* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,22* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE23* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.24*25**************************************************************************/2627/**28* @file29* Buffer validation.30*31* @author Jose Fonseca <[email protected]>32*/333435#include "pipe/p_compiler.h"36#include "pipe/p_defines.h"37#include "util/u_memory.h"38#include "util/u_debug.h"39#include "util/u_hash_table.h"4041#include "pb_buffer.h"42#include "pb_validate.h"434445#define PB_VALIDATE_INITIAL_SIZE 1 /* 512 */464748struct pb_validate_entry49{50struct pb_buffer *buf;51unsigned flags;52};535455struct pb_validate56{57struct pb_validate_entry *entries;58unsigned used;59unsigned size;60};616263enum pipe_error64pb_validate_add_buffer(struct pb_validate *vl,65struct pb_buffer *buf,66enum pb_usage_flags flags,67struct hash_table *ht,68boolean *already_present)69{70assert(buf);71*already_present = FALSE;72if (!buf)73return PIPE_ERROR;7475assert(flags & PB_USAGE_GPU_READ_WRITE);76assert(!(flags & ~PB_USAGE_GPU_READ_WRITE));77flags &= PB_USAGE_GPU_READ_WRITE;7879if (ht) {80unsigned entry_idx = (unsigned)(uintptr_t)util_hash_table_get(ht, buf);8182if (entry_idx) {83struct pb_validate_entry *entry = &vl->entries[entry_idx - 1];8485assert(entry->buf == buf);86entry->flags |= flags;87*already_present = TRUE;8889return PIPE_OK;90}91}9293/* Grow the table */94if(vl->used == vl->size) {95unsigned new_size;96struct pb_validate_entry *new_entries;9798new_size = vl->size * 2;99if(!new_size)100return PIPE_ERROR_OUT_OF_MEMORY;101102new_entries = (struct pb_validate_entry *)REALLOC(vl->entries,103vl->size*sizeof(struct pb_validate_entry),104new_size*sizeof(struct pb_validate_entry));105if (!new_entries)106return PIPE_ERROR_OUT_OF_MEMORY;107108memset(new_entries + vl->size, 0, (new_size - vl->size)*sizeof(struct pb_validate_entry));109110vl->size = new_size;111vl->entries = new_entries;112}113114assert(!vl->entries[vl->used].buf);115pb_reference(&vl->entries[vl->used].buf, buf);116vl->entries[vl->used].flags = flags;117++vl->used;118119if (ht)120_mesa_hash_table_insert(ht, buf, (void *) (uintptr_t) vl->used);121122return PIPE_OK;123}124125126enum pipe_error127pb_validate_foreach(struct pb_validate *vl,128enum pipe_error (*callback)(struct pb_buffer *buf, void *data),129void *data)130{131unsigned i;132for(i = 0; i < vl->used; ++i) {133enum pipe_error ret;134ret = callback(vl->entries[i].buf, data);135if(ret != PIPE_OK)136return ret;137}138return PIPE_OK;139}140141142enum pipe_error143pb_validate_validate(struct pb_validate *vl)144{145unsigned i;146147for(i = 0; i < vl->used; ++i) {148enum pipe_error ret;149ret = pb_validate(vl->entries[i].buf, vl, vl->entries[i].flags);150if(ret != PIPE_OK) {151while(i--)152pb_validate(vl->entries[i].buf, NULL, 0);153return ret;154}155}156157return PIPE_OK;158}159160161void162pb_validate_fence(struct pb_validate *vl,163struct pipe_fence_handle *fence)164{165unsigned i;166for(i = 0; i < vl->used; ++i) {167pb_fence(vl->entries[i].buf, fence);168pb_reference(&vl->entries[i].buf, NULL);169}170vl->used = 0;171}172173174void175pb_validate_destroy(struct pb_validate *vl)176{177unsigned i;178for(i = 0; i < vl->used; ++i)179pb_reference(&vl->entries[i].buf, NULL);180FREE(vl->entries);181FREE(vl);182}183184185struct pb_validate *186pb_validate_create()187{188struct pb_validate *vl;189190vl = CALLOC_STRUCT(pb_validate);191if (!vl)192return NULL;193194vl->size = PB_VALIDATE_INITIAL_SIZE;195vl->entries = (struct pb_validate_entry *)CALLOC(vl->size, sizeof(struct pb_validate_entry));196if(!vl->entries) {197FREE(vl);198return NULL;199}200201return vl;202}203204205206