Moving all common header file into common_def.h file
[o-du/l2.git] / src / cm / cm_lib.c
1 /*******************************************************************************
2 ################################################################################
3 #   Copyright (c) [2017-2019] [Radisys]                                        #
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**
20
21      Name:     common library functions
22   
23      Type:     C source file
24   
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.
28
29      File:     cm_lib.c
30
31 *********************************************************************21*/
32 \f
33 /***********************************************************************
34  * This file provides memory and string library functions implemented in
35  * one of two ways:
36  *
37  *     1. portable: does not assume library support for any of the functions
38  *                  needed.
39  *
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!
43  *
44  *  The following functions are available in this file:
45  *
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
53  *          encountered.
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.
57  *
58  *  The following flags are used in this file:
59  *
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.
63  *
64  *  For the SOLARIS environment:
65  *
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.
69  *
70  *     If you want to take advantage of these library functions, all you need
71  *     to do is the following:
72  *
73  *     1. Do not turn on the DONT_USE_SYS_LIB flag.
74  *     2. Turn on the SUNOS51 flag.
75  *
76  *     This, in turn, turns on the following flags
77  *
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
84  *
85  *  For an environment different from SOLARIS:
86  *        
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.
89  *
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
95  *        primitive.
96  *     3. communicate your changes to Trillium so they can be incorporated
97  *        in the next release of this file
98  *
99  *  To add a new library primitive to this file:
100  *     1. it should be implemented in both a portable and environment specific
101  *        manner.
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>
106  *                    #else
107  *                     <portable implementation>
108  *                    #endif
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
112  *        management.
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
116  *        enhance this file.
117  ************************************************************************/
118 \f
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 */
123
124 #ifndef DONT_USE_SYS_LIB
125
126 #include <stdio.h>
127 #include <ctype.h>
128 #include <stdlib.h>
129 #include <string.h>
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
136
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 */
145
146
147  
148 #include "gen.h"           /* general layer */
149 #include "ssi.h"           /* system services */
150
151 /* header/extern include files (.x) */
152  
153 #include "gen.x"           /* general layer */
154 #include "ssi.x"           /* system services */
155 #include "cm_lib.x"        /* prototypes of primitives in this file */
156
157 #if (ERRCLASS & ERRCLS_DEBUG)
158
159 #define ECMLIBBASE     0
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
167
168 #define CMLIBERR(_eCode, _eVal, _eDesc) \
169     SLogError ((Ent) 0, (Inst)0, (ProcId)0, __FILE__, __LINE__, \
170                (ErrCls)ERRCLS_DEBUG, (ErrCode)_eCode, (ErrVal) _eVal, \
171                (Txt *) _eDesc)
172 #else
173 #define CMLIBERR(_eCode, _eVal, _eDesc)
174 #endif
175
176 #ifdef MS_MBUF_CORRUPTION /* Should be enabled when debugging mbuf corruption */
177 EXTERN Data *startPtr128;
178 EXTERN Size regMemSize;
179 #endif
180 \f
181 /*
182 *
183 *       Fun:   cmMemcpy
184 *
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.
188 *
189 *              sets "len" memory locations starting from "tgt" to the values
190 *              of corresponding memory locations starting from "src".
191 *
192 *       Ret:   pointer to target string
193 *
194 *       Notes: None
195 *
196 *       File:  cm_lib.c
197 *
198 */
199 #ifdef ANSI
200 PUBLIC U8 *cmMemcpy
201 (
202 U8           *tgt,
203 CONSTANT U8  *src,
204 PTR          len
205 )
206 #else
207 PUBLIC U8 *cmMemcpy(tgt, src, len)
208 U8           *tgt;
209 CONSTANT U8  *src;
210 PTR          len;
211 #endif
212 {
213    /*cm_lib_c_001.main_14 : Fix for TRACE5 feature crash due to missing TRC MACRO*/
214    TRC2(cmMemcpy)
215
216 #ifdef MS_MBUF_CORRUPTION /* checking for valid memory address */
217 if ((tgt > startPtr128) && (tgt < (startPtr128+regMemSize)))
218 {
219    if ((*((U32 *)(tgt + 4)) == 0xDEADDEAD) || (*((U32 *)(tgt + 24)) == 0xDEADDEAD) ||
220       (*((U32 *)(tgt + 44)) == 0xDEADDEAD) || (*((U32 *)(tgt + 80)) == 0xDEADDEAD))
221    {
222       Data *crashPtr=NULLP;
223       *crashPtr = 9;
224    }
225 }
226 if ((src > startPtr128) && (src < (startPtr128+regMemSize)))
227 {
228    if ((*((U32 *)(src + 4)) == 0xDEADDEAD) || (*((U32 *)(src + 24)) == 0xDEADDEAD) ||
229       (*((U32 *)(src + 44)) == 0xDEADDEAD) || (*((U32 *)(src + 80)) == 0xDEADDEAD))
230    {
231       Data *crashPtr=NULLP;
232       *crashPtr = 9;
233    }
234 }
235 #endif 
236 #if (MEMCPY_AVAIL) /* memcpy is available */
237    RETVALUE((U8 *) memcpy((Void *)tgt, (CONSTANT Void *)src, (size_t)len));
238 #else
239    while (len--)
240       *tgt++ = *src++;
241
242    RETVALUE(tgt);
243 #endif /* MEMCPY_AVAIL */
244
245 } /* end of cmMemcpy */
246
247 \f
248 /*
249 *
250 *       Fun:   cmMemcmp
251 *
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.
255 *
256 *       Ret:    < 0 => s1 < s2
257 *               > 0 => s1 > s2
258 *               = 0 => s1 = s2
259 *
260 *       Notes: None
261 *
262 *       File:  cm_lib.c
263 *
264 */
265 #ifdef ANSI
266 PUBLIC S16 cmMemcmp
267 (
268 CONSTANT U8     *s1,
269 CONSTANT U8     *s2,
270 PTR             len
271 )
272 #else
273 PUBLIC S16 cmMemcmp (s1, s2, len)
274 CONSTANT U8     *s1;
275 CONSTANT U8     *s2;
276 PTR             len;
277 #endif
278 {
279    /*cm_lib_c_001.main_14 : Fix for TRACE5 feature crash due to missing TRC MACRO*/
280    TRC2(cmMemcmp)
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 */
284    while (len--)
285    {
286       if (*s1 ^ *s2)
287          RETVALUE((S16) (*s1 - *s2));
288       s1++;
289       s2++;
290    }
291    RETVALUE(0);
292 #endif /* MEMCMP_AVAIL */
293 } /* end of cmMemcmp */
294
295 \f
296 /*
297 *
298 *       Fun:   cmMemset
299 *
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.
303 *
304 *              sets "len" memory locations starting from "str" to the value
305 *              "val".
306
307 *       Ret:   pointer to string
308 *
309 *       Notes: None
310 *
311 *       File:  cm_lib.c
312 *
313 */
314 #ifdef ANSI
315 PUBLIC U8 *cmMemset
316 (
317 U8           *str,
318 U8           val,
319 PTR          len
320 )
321 #else
322 PUBLIC U8 *cmMemset(str, val, len)
323 U8           *str;
324 U8           val;
325 PTR          len;
326 #endif
327 {
328    /*cm_lib_c_001.main_14 : Fix for TRACE5 feature crash due to missing TRC MACRO*/
329    TRC2(cmMemset)
330 #if MS_MBUF_CORRUPTION /* checking for valid memory address */
331 if ((str > startPtr128) && (str < (startPtr128+regMemSize)))
332 {
333    if ((*((U32 *)(str + 4)) == 0xDEADDEAD) || (*((U32 *)(str + 24)) == 0xDEADDEAD) ||
334       (*((U32 *)(str + 44)) == 0xDEADDEAD) || (*((U32 *)(str + 80)) == 0xDEADDEAD))
335    {
336       Data *crashPtr=NULLP;
337       *crashPtr = 9;
338    }
339 }
340 #endif
341 #if (MEMSET_AVAIL) /* memset is available */
342    if (val==0)
343    {  
344       bzero((void *)str,(size_t)len);
345    }
346    else
347    {
348       memset((void *)str,val,(size_t) len);
349    }
350 #else  /* MEMSET_AVAIL: memset is not available */
351    while (len --)
352       *str++ = val;
353
354 #endif /* MEMSET_AVAIL */
355    RETVALUE(str);
356 } /* end of cmMemset */
357
358 \f
359 /*
360 *
361 *       Fun:   cmStrcmp
362 *
363 *       Desc:  common primitive to compare a contiguous string of characters
364 *              terminated by the '\0' character.
365 *
366 *              when strcmp is available, it uses that. otherwise, it
367 *              compares the strings using a for loop.
368 *
369 *              The following is the "strcmp" description from the SunOS 5.4
370 *              man-page. cmStrcmp follows this.
371 *
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.
380 *
381 *             Bytes following a null byte are not compared.
382 *
383 *
384 *       Ret:    < 0 => s1 < s2
385 *               > 0 => s1 > s2
386 *               = 0 => s1 = s2
387 *
388 *       Notes: None
389 *
390 *       File:  cm_lib.c
391 *
392 */
393 #ifdef ANSI
394 PUBLIC S16 cmStrcmp
395 (
396 CONSTANT U8 *s1,
397 CONSTANT U8 *s2
398 )
399 #else
400 PUBLIC S16 cmStrcmp (s1, s2)
401 CONSTANT U8 *s1;
402 CONSTANT U8 *s2;
403 #endif
404 {
405    /*cm_lib_c_001.main_14 : Fix for TRACE5 feature crash due to missing TRC MACRO*/
406    TRC2(cmStrcmp)
407 #if (STRCMP_AVAIL)
408    RETVALUE(strcmp((CONSTANT S8 *)s1, (CONSTANT S8 *)s2));
409 #else   /* STRCMP_AVAIL */
410   
411    while (*s1 && *s2)
412    {
413       if (*s1 ^ *s2)
414          RETVALUE(*s1 - *s2);
415       s1++;
416       s2++;
417    }
418    RETVALUE(0);
419 #endif      /* strcmp is not available */
420
421 } /* end of cmStrcmp */
422
423
424 \f
425 /*
426 *
427 *       Fun:   cmStrncmp
428 *
429 *       Desc:  common primitive to compare a contiguous string of characters
430 *              terminated by the '\0' character.
431 *              
432 *              when strncmp is available, it uses that. otherwise, it
433 *              compares the strings using a for loop.
434 *              
435 *              The following is the "strncmp" description from the SunOS 5.4
436 *              man-page. cmStrncmp follows this.
437 *
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.
448 *
449 *       Ret:    < 0 => s1 < s2
450 *               > 0 => s1 > s2
451 *               = 0 => s1 = s2
452 *
453 *       Notes: None
454 *
455 *       File:  cm_lib.c
456 *
457 */
458 #ifdef ANSI
459 PUBLIC S16 cmStrncmp
460 (
461 CONSTANT U8  *s1,
462 CONSTANT U8  *s2,
463 MsgLen       len /* cm_lib_c_001.main_12: Changing from S16 to MsgLen.*/
464 )
465 #else
466 PUBLIC S16 cmStrncmp (s1, s2, len)
467 CONSTANT U8  *s1;
468 CONSTANT U8  *s2;
469 MsgLen       len;
470 #endif
471 {
472    /*cm_lib_c_001.main_14 : Fix for TRACE5 feature crash due to missing TRC MACRO*/
473    TRC2(cmStrncmp)
474 #if (STRNCMP_AVAIL)
475    RETVALUE(strncmp((CONSTANT S8 *)s1, (CONSTANT S8 *)s2, (size_t) len));
476 #else   /* STRNCMP_AVAIL */
477   
478    while (*s1 && *s2 && len--)
479    {
480       if (*s1 ^ *s2)
481          RETVALUE(*s1 - *s2);
482       s1++;
483       s2++;
484    }
485    RETVALUE(0);
486 #endif   /* strncmp is not available */
487 } /* end of cmStrncmp */
488
489 \f
490 /*
491 *
492 *       Fun:   cmStrlen
493 *
494 *       Desc:  common primitive to compute the length of a NULL terminated
495 *              string.
496 *              
497 *              when strlen is available, it uses that. otherwise, it
498 *              inspects the string using a for loop.
499 *              
500 *              The following is the "strlen" description from the SunOS 5.4
501 *              man-page. cmStrlen follows this.
502 *
503 *              strlen() returns the number of bytes in s, not including the
504 *              terminating null character.
505 *
506 *       Ret:   length of string
507 *
508 *       Notes: None
509 *
510 *       File:  cm_lib.c
511 *
512 */
513 #ifdef ANSI
514 PUBLIC MsgLen cmStrlen
515 (
516 CONSTANT U8 *s
517 )
518 #else
519 /* cm_lib_c_001.main_12: Changing from S16 to MsgLen.*/
520 PUBLIC MsgLen cmStrlen (s)
521 CONSTANT U8 *s;
522 #endif
523 {
524 #if (STRLEN_AVAIL)
525    /*cm_lib_c_001.main_15 : Fix for warning due to mixed declation*/
526    TRC2(cmStrlen)
527    RETVALUE((MsgLen)strlen((CONSTANT S8 *)s));
528 #else   /* STRLEN_AVAIL */
529    MsgLen i;
530   
531    /*cm_lib_c_001.main_15 : Fix for warning due to mixed declation*/
532    TRC2(cmStrlen)
533
534    for (i = 0; *s; i++, s++);
535    RETVALUE(i);
536 #endif   /* strlen is not available */
537 } /* end of cmStrlen */
538
539 /**********************************************************************
540          End of file
541 **********************************************************************/