xref: /OK3568_Linux_fs/kernel/drivers/gpu/arm/midgard/tests/kutf/kutf_utils.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  *
3  * (C) COPYRIGHT 2014, 2017 ARM Limited. All rights reserved.
4  *
5  * This program is free software and is provided to you under the terms of the
6  * GNU General Public License version 2 as published by the Free Software
7  * Foundation, and any use by you of this program is subject to the terms
8  * of such GNU licence.
9  *
10  * A copy of the licence is included with the program, and can also be obtained
11  * from Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
12  * Boston, MA  02110-1301, USA.
13  *
14  */
15 
16 
17 
18 /* Kernel UTF utility functions */
19 
20 #include <linux/mutex.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/printk.h>
24 
25 #include <kutf/kutf_utils.h>
26 #include <kutf/kutf_mem.h>
27 
28 static char tmp_buffer[KUTF_MAX_DSPRINTF_LEN];
29 
30 DEFINE_MUTEX(buffer_lock);
31 
kutf_dsprintf(struct kutf_mempool * pool,const char * fmt,...)32 const char *kutf_dsprintf(struct kutf_mempool *pool,
33 		const char *fmt, ...)
34 {
35 	va_list args;
36 	int len;
37 	int size;
38 	void *buffer;
39 
40 	mutex_lock(&buffer_lock);
41 	va_start(args, fmt);
42 	len = vsnprintf(tmp_buffer, sizeof(tmp_buffer), fmt, args);
43 	va_end(args);
44 
45 	if (len < 0) {
46 		pr_err("kutf_dsprintf: Bad format dsprintf format %s\n", fmt);
47 		goto fail_format;
48 	}
49 
50 	if (len >= sizeof(tmp_buffer)) {
51 		pr_warn("kutf_dsprintf: Truncated dsprintf message %s\n", fmt);
52 		size = sizeof(tmp_buffer);
53 	} else {
54 		size = len + 1;
55 	}
56 
57 	buffer = kutf_mempool_alloc(pool, size);
58 	if (!buffer)
59 		goto fail_alloc;
60 
61 	memcpy(buffer, tmp_buffer, size);
62 	mutex_unlock(&buffer_lock);
63 
64 	return buffer;
65 
66 fail_alloc:
67 fail_format:
68 	mutex_unlock(&buffer_lock);
69 	return NULL;
70 }
71 EXPORT_SYMBOL(kutf_dsprintf);
72