1 /*******************************************************************************
2 ################################################################################
3 # Copyright (c) [2017-2019] [Radisys] #
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 #
9 # http://www.apache.org/licenses/LICENSE-2.0 #
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 *******************************************************************************/
19 /********************************************************************20**
21 Name: common library functions
25 Desc: Common functions that are implemented in
26 both a portable and a performance-efficient manner. These
27 functions are selected based on the operating system.
31 *********************************************************************21*/
33 /***********************************************************************
34 * This file provides memory and string library functions implemented in
37 * 1. portable: does not assume library support for any of the functions
40 * 2. using environment specific: this uses existing library
41 * functions and is therefore, for an environment where such
42 * functionality is available and , a more desirable choice!
44 * The following functions are available in this file:
46 * 1. cmMemcpy: copies specified number of octets from one memory
47 * location to another.
48 * 2. cmMemcmp: compares specified number of octets at one memory
49 * locaton with the same number from another.
50 * 3. cmMemset: sets specified number of octets starting at given
51 * memory location with the given value.
52 * 4. cmStrCmp: compares two strings, until the '\0' character is
54 * 5. cmStrncmp: compares two strings, until \0 is encountered or
55 * until the specified length is exceeded.
56 * 6. cmStrlen: finds length of a string until the \0 character.
58 * The following flags are used in this file:
60 * 1. DONT_USE_SYS_LIB: If defined, this will compile in the portable
61 * versions of each function in this file. This overrides all other
62 * flags defined in this file.
64 * For the SOLARIS environment:
66 * The functionality defined in this file is generally available as library
67 * functions in the native operating systems' libraries. We have provided
68 * the functionality using the SOLARIS libraries, in this file.
70 * If you want to take advantage of these library functions, all you need
71 * to do is the following:
73 * 1. Do not turn on the DONT_USE_SYS_LIB flag.
74 * 2. Turn on the SUNOS51 flag.
76 * This, in turn, turns on the following flags
78 * 1. MEMCPY_AVAIL : maps cmMemcpy to C library function memcpy
79 * 2. MEMCMP_AVAIL : maps cmMemcmp to C library function memcmp
80 * 3. MEMSET_AVAIL : maps cmMemset to C library function memset
81 * 4. STRCMP_AVAIL : maps cmStrcmp to Strings library function strcmp
82 * 5. STRNCMP_AVAIL : maps cmStrncmp to Strings library function strncmp
83 * 5. STRLEN_AVAIL : maps cmStrlen to Strings library function strlen
85 * For an environment different from SOLARIS:
87 * You will need to modify this file to do more or less the same stuff
88 * as has been done for Solaris. i.e.
90 * 1. create a section inside the #ifndef DONT_USE_SYS_LIB section
91 * similar to the #ifdef SUNOS51 section that:
92 * 1. includes system header files.
93 * 2. defines MEMCPY_AVAIL etc. as needed.
94 * 2. modify code inside functions to make use of the system library
96 * 3. communicate your changes to Trillium so they can be incorporated
97 * in the next release of this file
99 * To add a new library primitive to this file:
100 * 1. it should be implemented in both a portable and environment specific
102 * 2. the portable implementation will not be the default
103 * 3. the portable version and the environment specif versions must be
104 * enclosed in #ifdef XXXXXX_AVAIL
105 * <environment specific implementation>
107 * <portable implementation>
109 * 4. It must be communicated to Trillium so it will be included in the
110 * next release of the file.
111 * 5. Trillium engineering must have all changes approved by engineering
113 * 6. Trillium reserves the right to not incorporate any changes submitted
114 * by customers, if not approved by Trillium engineering management.
115 * 7. Trillium reserves the right to modify code submitted by customers to
117 ************************************************************************/
119 /*cm_lib_c_001.main_13 - Moved env files inclusion to the top*/
120 #include "envopt.h" /* environment options */
121 #include "envind.h" /* environment options */
122 #include "envdep.h" /* environment options */
124 #ifndef DONT_USE_SYS_LIB
130 #define MEMCPY_AVAIL 1
131 #define MEMCMP_AVAIL 1
132 #define MEMSET_AVAIL 1
133 #define STRCMP_AVAIL 1
134 #define STRNCMP_AVAIL 1
135 #define STRLEN_AVAIL 1
137 #else /* DONT_USE_SYS_LIB */
138 #define MEMCPY_AVAIL 0
139 #define MEMCMP_AVAIL 0
140 #define MEMSET_AVAIL 0
141 #define STRCMP_AVAIL 0
142 #define STRNCMP_AVAIL 0
143 #define STRLEN_AVAIL 0
144 #endif /* not DONT_USE_SYS_LIB */
148 #include "gen.h" /* general layer */
149 #include "ssi.h" /* system services */
151 /* header/extern include files (.x) */
153 #include "gen.x" /* general layer */
154 #include "ssi.x" /* system services */
155 #include "cm_lib.x" /* prototypes of primitives in this file */
157 #if (ERRCLASS & ERRCLS_DEBUG)
160 #define ECMLIB001 ECMLIBBASE + 1
161 #define ECMLIB002 ECMLIBBASE + 2
162 #define ECMLIB003 ECMLIBBASE + 3
163 #define ECMLIB004 ECMLIBBASE + 4
164 #define ECMLIB005 ECMLIBBASE + 5
165 #define ECMLIB006 ECMLIBBASE + 6
166 #define ECMLIB007 ECMLIBBASE + 7
168 #define CMLIBERR(_eCode, _eVal, _eDesc) \
169 SLogError ((Ent) 0, (Inst)0, (ProcId)0, __FILE__, __LINE__, \
170 (ErrCls)ERRCLS_DEBUG, (ErrCode)_eCode, (ErrVal) _eVal, \
173 #define CMLIBERR(_eCode, _eVal, _eDesc)
176 #ifdef MS_MBUF_CORRUPTION /* Should be enabled when debugging mbuf corruption */
185 * Desc: common primitive to copy a contiguous string of bytes
186 * optimized for when memcpy is available. It uses memcpy
187 * when available. Otherwise, copies in a 'for' loop.
189 * sets "len" memory locations starting from "tgt" to the values
190 * of corresponding memory locations starting from "src".
192 * Ret: pointer to target string
199 uint8_t *cmMemcpy(uint8_t *tgt,const uint8_t *src,PTR len)
201 /*cm_lib_c_001.main_14 : Fix for TRACE5 feature crash due to missing TRC MACRO*/
203 #ifdef MS_MBUF_CORRUPTION /* checking for valid memory address */
204 if ((tgt > startPtr128) && (tgt < (startPtr128+regMemSize)))
206 if ((*((uint32_t *)(tgt + 4)) == 0xDEADDEAD) || (*((uint32_t *)(tgt + 24)) == 0xDEADDEAD) ||
207 (*((uint32_t *)(tgt + 44)) == 0xDEADDEAD) || (*((uint32_t *)(tgt + 80)) == 0xDEADDEAD))
209 Data *crashPtr=NULLP;
213 if ((src > startPtr128) && (src < (startPtr128+regMemSize)))
215 if ((*((uint32_t *)(src + 4)) == 0xDEADDEAD) || (*((uint32_t *)(src + 24)) == 0xDEADDEAD) ||
216 (*((uint32_t *)(src + 44)) == 0xDEADDEAD) || (*((uint32_t *)(src + 80)) == 0xDEADDEAD))
218 Data *crashPtr=NULLP;
223 #if (MEMCPY_AVAIL) /* memcpy is available */
224 return ( memcpy(tgt, src, len));
230 #endif /* MEMCPY_AVAIL */
232 } /* end of cmMemcpy */
239 * Desc: common primitive to compare a contiguous string of bytes
240 * optimized for when memcmp is available. It uses memcmp
241 * when available. Otherwise, compares in a 'for' loop.
243 * Ret: < 0 => s1 < s2
252 S16 cmMemcmp(const uint8_t *s1,const uint8_t *s2,PTR len)
254 /*cm_lib_c_001.main_14 : Fix for TRACE5 feature crash due to missing TRC MACRO*/
255 #if MEMCMP_AVAIL /* memcmp is available */
256 return ((S16) memcmp((const Void *)s1, (const Void *)s2, (size_t)len));
257 #else /* MEMCMP_AVAIL: memcmp is not available */
261 return ((S16) (*s1 - *s2));
266 #endif /* MEMCMP_AVAIL */
267 } /* end of cmMemcmp */
274 * Desc: common primitive to set a contiguous string of bytes
275 * with a specified value optimized for when memset is available.
276 * It uses memset when available. Otherwise, uses a 'for' loop.
278 * sets "len" memory locations starting from "str" to the value
281 * Ret: pointer to string
288 uint8_t *cmMemset(uint8_t *str,uint8_t val,PTR len)
290 /*cm_lib_c_001.main_14 : Fix for TRACE5 feature crash due to missing TRC MACRO*/
291 #if MS_MBUF_CORRUPTION /* checking for valid memory address */
292 if ((str > startPtr128) && (str < (startPtr128+regMemSize)))
294 if ((*((uint32_t *)(str + 4)) == 0xDEADDEAD) || (*((uint32_t *)(str + 24)) == 0xDEADDEAD) ||
295 (*((uint32_t *)(str + 44)) == 0xDEADDEAD) || (*((uint32_t *)(str + 80)) == 0xDEADDEAD))
297 Data *crashPtr=NULLP;
302 #if (MEMSET_AVAIL) /* memset is available */
305 bzero((void *)str,(size_t)len);
309 memset(str,val, len);
311 #else /* MEMSET_AVAIL: memset is not available */
315 #endif /* MEMSET_AVAIL */
317 } /* end of cmMemset */
324 * Desc: common primitive to compare a contiguous string of characters
325 * terminated by the '\0' character.
327 * when strcmp is available, it uses that. otherwise, it
328 * compares the strings using a for loop.
330 * The following is the "strcmp" description from the SunOS 5.4
331 * man-page. cmStrcmp follows this.
333 * strcmp() compares two strings byte-by-byte, according to the
334 * ordering of your machine's character set. The function
335 * returns an integer greater than, equal to, or less than 0,
336 * if the string pointed to by s1 is greater than, equal to, or
337 * less than the string pointed to by s2 respectively. The sign
338 * of a non-zero return value is determined by the sign of the
339 * difference between the values of the first pair of bytes
340 * that differ in the strings being compared.
342 * Bytes following a null byte are not compared.
345 * Ret: < 0 => s1 < s2
354 S16 cmStrcmp(const uint8_t *s1,const uint8_t *s2)
356 /*cm_lib_c_001.main_14 : Fix for TRACE5 feature crash due to missing TRC MACRO*/
358 return (strcmp((const char *)s1, (const char *)s2));
359 #else /* STRCMP_AVAIL */
369 #endif /* strcmp is not available */
371 } /* end of cmStrcmp */
379 * Desc: common primitive to compare a contiguous string of characters
380 * terminated by the '\0' character.
382 * when strncmp is available, it uses that. otherwise, it
383 * compares the strings using a for loop.
385 * The following is the "strncmp" description from the SunOS 5.4
386 * man-page. cmStrncmp follows this.
388 * strcmp() compares two strings byte-by-byte, according to the
389 * ordering of your machine's character set. The function
390 * returns an integer greater than, equal to, or less than 0,
391 * if the string pointed to by s1 is greater than, equal to, or
392 * less than the string pointed to by s2 respectively. The sign
393 * of a non-zero return value is determined by the sign of the
394 * difference between the values of the first pair of bytes
395 * that differ in the strings being compared. strncmp() makes
396 * the same comparison but looks at a maximum of n bytes.
397 * Bytes following a null byte are not compared.
399 * Ret: < 0 => s1 < s2
412 MsgLen len /* cm_lib_c_001.main_12: Changing from S16 to MsgLen.*/
415 /*cm_lib_c_001.main_14 : Fix for TRACE5 feature crash due to missing TRC MACRO*/
417 return (strncmp((const char *)s1, (const char *)s2, (size_t) len));
418 #else /* STRNCMP_AVAIL */
420 while (*s1 && *s2 && len--)
428 #endif /* strncmp is not available */
429 } /* end of cmStrncmp */
436 * Desc: common primitive to compute the length of a NULL terminated
439 * when strlen is available, it uses that. otherwise, it
440 * inspects the string using a for loop.
442 * The following is the "strlen" description from the SunOS 5.4
443 * man-page. cmStrlen follows this.
445 * strlen() returns the number of bytes in s, not including the
446 * terminating null character.
448 * Ret: length of string
455 MsgLen cmStrlen(const uint8_t *s)
458 /*cm_lib_c_001.main_15 : Fix for warning due to mixed declation*/
459 return ((MsgLen)strlen((const char *)s));
460 #else /* STRLEN_AVAIL */
463 /*cm_lib_c_001.main_15 : Fix for warning due to mixed declation*/
465 for (i = 0; *s; i++, s++);
467 #endif /* strlen is not available */
468 } /* end of cmStrlen */
470 /**********************************************************************
472 **********************************************************************/