Path: blob/21.2-virgl/src/gallium/auxiliary/util/u_async_debug.c
4561 views
/*1* Copyright 2017 Advanced Micro Devices, Inc.2* All Rights Reserved.3*4* Permission is hereby granted, free of charge, to any person obtaining a5* copy of this software and associated documentation files (the "Software"),6* to deal in the Software without restriction, including without limitation7* on the rights to use, copy, modify, merge, publish, distribute, sub8* license, and/or sell copies of the Software, and to permit persons to whom9* the Software is furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice (including the next12* paragraph) shall be included in all copies or substantial portions of the13* Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL18* THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,19* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR20* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE21* USE OR OTHER DEALINGS IN THE SOFTWARE.22*23*/2425#include "u_async_debug.h"2627#include <stdio.h>28#include <stdarg.h>2930#include "util/u_debug.h"31#include "util/u_string.h"3233static void34u_async_debug_message(void *data, unsigned *id, enum pipe_debug_type type,35const char *fmt, va_list args)36{37struct util_async_debug_callback *adbg = data;38struct util_debug_message *msg;39char *text;40int r;4142r = vasprintf(&text, fmt, args);43if (r < 0)44return;4546simple_mtx_lock(&adbg->lock);47if (adbg->count >= adbg->max) {48size_t new_max = MAX2(16, adbg->max * 2);4950if (new_max < adbg->max ||51new_max > SIZE_MAX / sizeof(*adbg->messages)) {52free(text);53goto out;54}5556struct util_debug_message *new_msg =57realloc(adbg->messages, new_max * sizeof(*adbg->messages));58if (!new_msg) {59free(text);60goto out;61}6263adbg->max = new_max;64adbg->messages = new_msg;65}6667msg = &adbg->messages[adbg->count++];68msg->id = id;69msg->type = type;70msg->msg = text;7172out:73simple_mtx_unlock(&adbg->lock);74}7576void77u_async_debug_init(struct util_async_debug_callback *adbg)78{79memset(adbg, 0, sizeof(*adbg));8081simple_mtx_init(&adbg->lock, mtx_plain);82adbg->base.async = true;83adbg->base.debug_message = u_async_debug_message;84adbg->base.data = adbg;85}8687void88u_async_debug_cleanup(struct util_async_debug_callback *adbg)89{90simple_mtx_destroy(&adbg->lock);9192for (unsigned i = 0; i < adbg->count; ++i)93free(adbg->messages[i].msg);94free(adbg->messages);95}9697void98_u_async_debug_drain(struct util_async_debug_callback *adbg,99struct pipe_debug_callback *dst)100{101simple_mtx_lock(&adbg->lock);102for (unsigned i = 0; i < adbg->count; ++i) {103const struct util_debug_message *msg = &adbg->messages[i];104105_pipe_debug_message(dst, msg->id, msg->type, "%s", msg->msg);106107free(msg->msg);108}109110adbg->count = 0;111simple_mtx_unlock(&adbg->lock);112}113114115116