2 * Copyright 2017 Advanced Micro Devices, Inc.
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:
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
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.
26 #include "u_async_debug.h"
31 #include "util/u_debug.h"
32 #include "util/u_string.h"
35 u_async_debug_message(void *data
, unsigned *id
, enum pipe_debug_type type
,
36 const char *fmt
, va_list args
)
38 struct util_async_debug_callback
*adbg
= data
;
39 struct util_debug_message
*msg
;
43 r
= util_vasprintf(&text
, fmt
, args
);
47 simple_mtx_lock(&adbg
->lock
);
48 if (adbg
->count
>= adbg
->max
) {
49 unsigned new_max
= MAX2(16, adbg
->max
* 2);
51 if (new_max
< adbg
->max
||
52 new_max
> SIZE_MAX
/ sizeof(*adbg
->messages
)) {
57 struct util_debug_message
*new_msg
=
58 realloc(adbg
->messages
, new_max
* sizeof(*adbg
->messages
));
65 adbg
->messages
= new_msg
;
68 msg
= &adbg
->messages
[adbg
->count
++];
74 simple_mtx_unlock(&adbg
->lock
);
78 u_async_debug_init(struct util_async_debug_callback
*adbg
)
80 memset(adbg
, 0, sizeof(*adbg
));
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
;
89 u_async_debug_cleanup(struct util_async_debug_callback
*adbg
)
91 simple_mtx_destroy(&adbg
->lock
);
93 for (unsigned i
= 0; i
< adbg
->count
; ++i
)
94 free(adbg
->messages
[i
].msg
);
99 _u_async_debug_drain(struct util_async_debug_callback
*adbg
,
100 struct pipe_debug_callback
*dst
)
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
];
106 _pipe_debug_message(dst
, msg
->id
, msg
->type
, "%s", msg
->msg
);
112 simple_mtx_unlock(&adbg
->lock
);