1 /*
2 * Copyright (c) 2022 Samsung Electronics Co., Ltd.
3 * All Rights Reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * - Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * - Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * - Neither the name of the copyright owner, nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <stdarg.h>
33 #include "oapv_port.h"
34
oapv_malloc_align32(int size)35 void *oapv_malloc_align32(int size)
36 {
37 void *p = NULL;
38
39 // p variable is covered under ap. It's user rosponsibility to free it using funcion oapv_mfree_align32.
40 p = oapv_malloc(size + 32 + sizeof(void *));
41 if(p) {
42
43 #ifdef _IS64BIT // for 64bit CPU
44 void **ap = (void **)(((u64)(p) + 32 + sizeof(void *) - 1) & (~0x1F));
45 #else // for 32bit CPU
46 void **ap = (void **)(((u32)(p) + 32 + sizeof(void *) - 1) & (~0x1F));
47 #endif
48 ap[-1] = (void *)p;
49 return (void *)ap;
50 }
51 return NULL;
52 }
53
oapv_mfree_align32(void * p)54 void oapv_mfree_align32(void *p)
55 {
56 if(p) {
57 oapv_mfree(((void **)p)[-1]);
58 }
59 }
60
oapv_trace0(char * filename,int line,const char * fmt,...)61 void oapv_trace0(char *filename, int line, const char *fmt, ...)
62 {
63 char str[1024] = { '\0' };
64
65 if(filename != NULL && line >= 0) {
66 sprintf(str, "[%s:%d] ", filename, line);
67 }
68 va_list args;
69 va_start(args, fmt);
70 vsprintf(str + strlen(str), fmt, args);
71 va_end(args);
72 printf("%s", str);
73 }
74
oapv_trace_line(char * pre)75 void oapv_trace_line(char *pre)
76 {
77 const int chars = 80;
78 char str[128] = { '\0' };
79 int len = (pre == NULL) ? 0 : (int)strlen(pre);
80
81 if(len > 0) {
82 sprintf(str, "%s ", pre);
83 len = (int)strlen(str);
84 }
85 for(int i = len; i < chars; i++) {
86 str[i] = '=';
87 }
88 str[chars] = '\0';
89 printf("%s\n", str);
90 }