1.change inline usage in code and mvec in makefile to adapt to GCC version
[o-du/phy.git] / fapi_5g / source / utils / nr5g_fapi_memory.c
1 /******************************************************************************
2 *
3 *   Copyright (c) 2019 Intel.
4 *
5 *   Licensed under the Apache License, Version 2.0 (the "License");
6 *   you may not use this file except in compliance with the License.
7 *   You may obtain a copy of the License at
8 *
9 *       http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *   Unless required by applicable law or agreed to in writing, software
12 *   distributed under the License is distributed on an "AS IS" BASIS,
13 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *   See the License for the specific language governing permissions and
15 *   limitations under the License.
16 *
17 *******************************************************************************/
18
19 /**
20  * @file This file defines all the functions used for 
21  * memory operations. 
22  *
23  **/
24
25 #include <rte_memcpy.h>
26 #include <string.h>
27 #include "nr5g_fapi_wls.h"
28
29 uint8_t nr5g_fapi_memcpy_bound_check(
30     void *d,
31     size_t x,
32     const void *s,
33     size_t n)
34 {
35     // Memory block size and destination/source boundary check
36     if ((n >= (MSG_MAXSIZE)) || (n > x)) {
37         printf
38             ("[MEMCPY FAILED] %s : %d : max block size: %d, memcpy dst size: %ld, memcpy src size: %ld\n",
39             __func__, __LINE__, MSG_MAXSIZE, x, n);
40         return FAILURE;
41     }
42     // Overlap check: source pointer overruns destination pointer
43     if ((char *)s < (char *)d) {
44         if ((((char *)s + x) >= (char *)d) || (((char *)s + n) >= (char *)d)) {
45             printf("[MEMCPY FAILED] %s : %d : max block size: %d, \
46                     memcpy dst size: %ld, strcpy src size: %ld, \
47                     Source pointer %p overlaps into destination pointer %p\n", __func__, __LINE__, MSG_MAXSIZE, x, n, s, d);
48             return FAILURE;
49         }
50     }
51     // Overlap check: destination pointer overruns source pointer
52     if ((char *)d < (char *)s) {
53         if ((((char *)d + x) >= (char *)s) || (((char *)d + n) >= (char *)s)) {
54             printf("[MEMCPY FAILED] %s : %d : max block size: %d, \
55                     memcpy dst size: %ld, strcpy src size: %ld, \
56                     Destination pointer %p overlaps into source pointer %p\n", __func__, __LINE__, MSG_MAXSIZE, x, n, d, s);
57             return FAILURE;
58         }
59     }
60     rte_memcpy(d, s, n);
61     return SUCCESS;
62 }
63
64 uint8_t nr5g_fapi_memset_bound_check(
65     void *s,
66     size_t x,
67     int32_t c,
68     size_t n)
69 {
70     // Memory block size and destination/source boundary check
71     if ((n >= MSG_MAXSIZE) || (n > x)) {
72         printf
73             ("[MEMSET FAILED] %s : %d : max block size: %d, memset dst size: %ld, memset src size: %ld\n",
74             __func__, __LINE__, MSG_MAXSIZE, x, n);
75         return FAILURE;
76     }
77     memset(s, c, n);
78     return SUCCESS;
79 }
80
81 uint8_t nr5g_fapi_strcpy_bound_check(
82     char *d,
83     size_t x,
84     const char *s,
85     size_t n)
86 {
87     // Memory block size and destination/source boundary check
88     if ((n >= MSG_MAXSIZE) || (n > x)) {
89         printf("[STRNCPY FAILED] %s : %d : max block size: %d, \
90             strcpy dst size: %ld, strcpy src size: %ld\n", __func__, __LINE__, MSG_MAXSIZE, x, n);
91         return FAILURE;
92     }
93     // Overlap check: source pointer overruns destination pointer
94     if ((char *)s < (char *)d) {
95         if ((((char *)s + x) >= (char *)d) || (((char *)s + n) >= (char *)d)) {
96             printf("[STRNCPY FAILED] %s : %d : max block size: %d, \
97                     strcpy dst size: %ld, strcpy src size: %ld, \
98                     Source pointer %p overlaps into destination pointer %p\n", __func__, __LINE__, MSG_MAXSIZE, x, n, s, d);
99             return FAILURE;
100         }
101     }
102     // Overlap check: destination pointer overruns source pointer
103     if ((char *)d < (char *)s) {
104         if ((((char *)d + x) >= (char *)s) || (((char *)d + n) >= (char *)s)) {
105             printf("[STRNCPY FAILED] %s : %d : max block size: %d, \
106                     strcpy dst size: %ld, strcpy src size: %ld, \
107                     Destination pointer %p overlaps into source pointer %p\n", __func__, __LINE__, MSG_MAXSIZE, x, n, d, s);
108             return FAILURE;
109         }
110     }
111     strncpy(d, s, n);
112     return SUCCESS;
113 }