1/*
2 * Copyright 2017 Advanced Micro Devices, Inc.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 */
25
26#include "u_async_debug.h"
27
28#include <stdio.h>
29#include <stdarg.h>
30
31#include "util/u_debug.h"
32#include "util/u_string.h"
33
34static void
35u_async_debug_message(void *data, unsigned *id, enum util_debug_type type,
36                      const char *fmt, va_list args)
37{
38   struct util_async_debug_callback *adbg = data;
39   struct util_debug_message *msg;
40   char *text;
41   int r;
42
43   r = vasprintf(&text, fmt, args);
44   if (r < 0)
45      return;
46
47   simple_mtx_lock(&adbg->lock);
48   if (adbg->count >= adbg->max) {
49      size_t new_max = MAX2(16, adbg->max * 2);
50
51      if (new_max < adbg->max ||
52          new_max > SIZE_MAX / sizeof(*adbg->messages)) {
53         free(text);
54         goto out;
55      }
56
57      struct util_debug_message *new_msg =
58         realloc(adbg->messages, new_max * sizeof(*adbg->messages));
59      if (!new_msg) {
60         free(text);
61         goto out;
62      }
63
64      adbg->max = new_max;
65      adbg->messages = new_msg;
66   }
67
68   msg = &adbg->messages[adbg->count++];
69   msg->id = id;
70   msg->type = type;
71   msg->msg = text;
72
73out:
74   simple_mtx_unlock(&adbg->lock);
75}
76
77void
78u_async_debug_init(struct util_async_debug_callback *adbg)
79{
80   memset(adbg, 0, sizeof(*adbg));
81
82   simple_mtx_init(&adbg->lock, mtx_plain);
83   adbg->base.async = true;
84   adbg->base.debug_message = u_async_debug_message;
85   adbg->base.data = adbg;
86}
87
88void
89u_async_debug_cleanup(struct util_async_debug_callback *adbg)
90{
91   simple_mtx_destroy(&adbg->lock);
92
93   for (unsigned i = 0; i < adbg->count; ++i)
94      free(adbg->messages[i].msg);
95   free(adbg->messages);
96}
97
98void
99_u_async_debug_drain(struct util_async_debug_callback *adbg,
100                     struct util_debug_callback *dst)
101{
102   simple_mtx_lock(&adbg->lock);
103   for (unsigned i = 0; i < adbg->count; ++i) {
104      const struct util_debug_message *msg = &adbg->messages[i];
105
106      _util_debug_message(dst, msg->id, msg->type, "%s", msg->msg);
107
108      free(msg->msg);
109   }
110
111   adbg->count = 0;
112   simple_mtx_unlock(&adbg->lock);
113}
114
115