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 */
177 EXTERN Data *startPtr128;
178 EXTERN Size regMemSize;
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
207 PUBLIC U8 *cmMemcpy(tgt, src, len)
213 /*cm_lib_c_001.main_14 : Fix for TRACE5 feature crash due to missing TRC MACRO*/
216 #ifdef MS_MBUF_CORRUPTION /* checking for valid memory address */
217 if ((tgt > startPtr128) && (tgt < (startPtr128+regMemSize)))
219 if ((*((U32 *)(tgt + 4)) == 0xDEADDEAD) || (*((U32 *)(tgt + 24)) == 0xDEADDEAD) ||
220 (*((U32 *)(tgt + 44)) == 0xDEADDEAD) || (*((U32 *)(tgt + 80)) == 0xDEADDEAD))
222 Data *crashPtr=NULLP;
226 if ((src > startPtr128) && (src < (startPtr128+regMemSize)))
228 if ((*((U32 *)(src + 4)) == 0xDEADDEAD) || (*((U32 *)(src + 24)) == 0xDEADDEAD) ||
229 (*((U32 *)(src + 44)) == 0xDEADDEAD) || (*((U32 *)(src + 80)) == 0xDEADDEAD))
231 Data *crashPtr=NULLP;
236 #if (MEMCPY_AVAIL) /* memcpy is available */
237 RETVALUE((U8 *) memcpy((Void *)tgt, (CONSTANT Void *)src, (size_t)len));
243 #endif /* MEMCPY_AVAIL */
245 } /* end of cmMemcpy */
252 * Desc: common primitive to compare a contiguous string of bytes
253 * optimized for when memcmp is available. It uses memcmp
254 * when available. Otherwise, compares in a 'for' loop.
256 * Ret: < 0 => s1 < s2
273 PUBLIC S16 cmMemcmp (s1, s2, len)
279 /*cm_lib_c_001.main_14 : Fix for TRACE5 feature crash due to missing TRC MACRO*/
281 #if MEMCMP_AVAIL /* memcmp is available */
282 RETVALUE((S16) memcmp((CONSTANT Void *)s1, (CONSTANT Void *)s2, (size_t)len));
283 #else /* MEMCMP_AVAIL: memcmp is not available */
287 RETVALUE((S16) (*s1 - *s2));
292 #endif /* MEMCMP_AVAIL */
293 } /* end of cmMemcmp */
300 * Desc: common primitive to set a contiguous string of bytes
301 * with a specified value optimized for when memset is available.
302 * It uses memset when available. Otherwise, uses a 'for' loop.
304 * sets "len" memory locations starting from "str" to the value
307 * Ret: pointer to string
322 PUBLIC U8 *cmMemset(str, val, len)
328 /*cm_lib_c_001.main_14 : Fix for TRACE5 feature crash due to missing TRC MACRO*/
330 #if MS_MBUF_CORRUPTION /* checking for valid memory address */
331 if ((str > startPtr128) && (str < (startPtr128+regMemSize)))
333 if ((*((U32 *)(str + 4)) == 0xDEADDEAD) || (*((U32 *)(str + 24)) == 0xDEADDEAD) ||
334 (*((U32 *)(str + 44)) == 0xDEADDEAD) || (*((U32 *)(str + 80)) == 0xDEADDEAD))
336 Data *crashPtr=NULLP;
341 #if (MEMSET_AVAIL) /* memset is available */
344 bzero((void *)str,(size_t)len);
348 memset((void *)str,val,(size_t) len);
350 #else /* MEMSET_AVAIL: memset is not available */
354 #endif /* MEMSET_AVAIL */
356 } /* end of cmMemset */
363 * Desc: common primitive to compare a contiguous string of characters
364 * terminated by the '\0' character.
366 * when strcmp is available, it uses that. otherwise, it
367 * compares the strings using a for loop.
369 * The following is the "strcmp" description from the SunOS 5.4
370 * man-page. cmStrcmp follows this.
372 * strcmp() compares two strings byte-by-byte, according to the
373 * ordering of your machine's character set. The function
374 * returns an integer greater than, equal to, or less than 0,
375 * if the string pointed to by s1 is greater than, equal to, or
376 * less than the string pointed to by s2 respectively. The sign
377 * of a non-zero return value is determined by the sign of the
378 * difference between the values of the first pair of bytes
379 * that differ in the strings being compared.
381 * Bytes following a null byte are not compared.
384 * Ret: < 0 => s1 < s2
400 PUBLIC S16 cmStrcmp (s1, s2)
405 /*cm_lib_c_001.main_14 : Fix for TRACE5 feature crash due to missing TRC MACRO*/
408 RETVALUE(strcmp((CONSTANT S8 *)s1, (CONSTANT S8 *)s2));
409 #else /* STRCMP_AVAIL */
419 #endif /* strcmp is not available */
421 } /* end of cmStrcmp */
429 * Desc: common primitive to compare a contiguous string of characters
430 * terminated by the '\0' character.
432 * when strncmp is available, it uses that. otherwise, it
433 * compares the strings using a for loop.
435 * The following is the "strncmp" description from the SunOS 5.4
436 * man-page. cmStrncmp follows this.
438 * strcmp() compares two strings byte-by-byte, according to the
439 * ordering of your machine's character set. The function
440 * returns an integer greater than, equal to, or less than 0,
441 * if the string pointed to by s1 is greater than, equal to, or
442 * less than the string pointed to by s2 respectively. The sign
443 * of a non-zero return value is determined by the sign of the
444 * difference between the values of the first pair of bytes
445 * that differ in the strings being compared. strncmp() makes
446 * the same comparison but looks at a maximum of n bytes.
447 * Bytes following a null byte are not compared.
449 * Ret: < 0 => s1 < s2
463 MsgLen len /* cm_lib_c_001.main_12: Changing from S16 to MsgLen.*/
466 PUBLIC S16 cmStrncmp (s1, s2, len)
472 /*cm_lib_c_001.main_14 : Fix for TRACE5 feature crash due to missing TRC MACRO*/
475 RETVALUE(strncmp((CONSTANT S8 *)s1, (CONSTANT S8 *)s2, (size_t) len));
476 #else /* STRNCMP_AVAIL */
478 while (*s1 && *s2 && len--)
486 #endif /* strncmp is not available */
487 } /* end of cmStrncmp */
494 * Desc: common primitive to compute the length of a NULL terminated
497 * when strlen is available, it uses that. otherwise, it
498 * inspects the string using a for loop.
500 * The following is the "strlen" description from the SunOS 5.4
501 * man-page. cmStrlen follows this.
503 * strlen() returns the number of bytes in s, not including the
504 * terminating null character.
506 * Ret: length of string
514 PUBLIC MsgLen cmStrlen
519 /* cm_lib_c_001.main_12: Changing from S16 to MsgLen.*/
520 PUBLIC MsgLen cmStrlen (s)
525 /*cm_lib_c_001.main_15 : Fix for warning due to mixed declation*/
527 RETVALUE((MsgLen)strlen((CONSTANT S8 *)s));
528 #else /* STRLEN_AVAIL */
531 /*cm_lib_c_001.main_15 : Fix for warning due to mixed declation*/
534 for (i = 0; *s; i++, s++);
536 #endif /* strlen is not available */
537 } /* end of cmStrlen */
539 /**********************************************************************
541 **********************************************************************/