L1-L2 Integration fixes.[Issue-ID: ODUHIGH-252]
[o-du/l2.git] / src / mt / ss_msg.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 \f
19 /********************************************************************20**
20  
21      Name:     System Services -- Message manipulation functions
22  
23      Type:     C source file
24  
25      Desc:     Source Code for message related functions.
26  
27      File:     ss_msg.c
28  
29 *********************************************************************21*/
30  
31
32 \f
33 /* header include files (.h) */
34
35 #include "envopt.h"        /* environment options */
36 #include "envdep.h"        /* environment dependent */
37 #include "envind.h"        /* environment independent */
38   
39 #include "gen.h"           /* general layer */
40 #include "ssi.h"           /* system services */
41
42 #include "ss_err.h"        /* errors */
43 #include "ss_dep.h"        /* implementation-specific */
44 #include "ss_queue.h"      /* queues */
45 #include "ss_strm.h"       /* STREAMS */
46 #include "ss_msg.h"        /* messaging */
47 #include "ss_mem.h"        /* memory management interface */
48 #include "ss_gen.h"        /* general */
49 #include "cm_mem.h"
50 #include "cm_llist.h"
51
52 #ifdef RGL_SPECIFIC_CHANGES
53 #ifdef TENB_DPDK_BUF
54 #include "ntl_lib.h"
55 #endif
56 #endif
57
58
59 /* header/extern include files (.x) */
60
61 #include "gen.x"           /* general layer */
62 #include "ssi.x"           /* system services */
63
64 #include "ss_dep.x"        /* implementation-specific */
65 #include "ss_queue.x"      /* queues */
66 #include "ss_task.x"       /* tasking */
67 #include "ss_timer.x"      /* timers */
68 #include "ss_strm.x"       /* STREAMS */
69 #include "ss_msg.x"        /* messaging */
70 #include "ss_mem.x"        /* memory management interface */
71 #include "ss_drvr.x"       /* driver tasks */
72 #ifdef SS_LOCKLESS_MEMORY
73 #include "cm_llist.x"
74 #include "cm_hash.x"
75 #include "cm_mem_wl.x"        /* common memory manager */
76 #else
77 #include "cm_mem.x"        /* common memory manager */
78 #endif /* SS_LOCKLESS_MEMORY */
79 #include "ss_gen.x"        /* general */
80
81 /*ss004.301: Cavium changes */
82 #ifdef SS_SEUM_CAVIUM
83 /* cvmx includes files */
84 #include "cvmx-config.h"
85 #include "cvmx.h"
86 #include "cvmx-pow.h"
87 #include "cvmx-tim.h"
88 #include "cvmx-fpa.h"
89 #include "cvmx-helper-fpa.h"
90 #include "cvmx-malloc.h"
91 #endif /* SS_SEUM_CAVIUM */
92 #include <pthread.h>
93 #ifdef XEON_SPECIFIC_CHANGES
94 uint32_t startMemLeak=0; 
95 pthread_mutex_t  memLock;
96 #endif
97
98 #if (defined (MAC_FREE_RING_BUF) || defined (RLC_FREE_RING_BUF))
99 #include "ss_rbuf.h"
100 #include "ss_rbuf.x"
101 #endif
102 #ifdef L2_L3_SPLIT
103 #include "mt_plat_t33.h"
104 #include "mt_plat_t33.x"
105 #endif
106 /* forward declarations */
107 S16 ssGetDBufOfSize ARGS((Region region, Size size, Buffer **dBuf));
108 S16 SIncMsgRef(Buffer *srcBuf,Region dstRegion, Pool dstPool,Buffer **dstBuf);
109 #ifdef SSI_STATIC_MEM_LEAK_DETECTION
110 uint32_t GetNextFreeIdx ARGS((StaticMemAllocInfo * memAllocInfo));
111 void LogForStaticMemLeak ARGS((StaticMemAllocInfo* memAllocInfo, char* 
112          file, uint32_t line, uint32_t size, void* ptr, uint32_t idx));
113 void FreeIdx ARGS((uint8_t* ptr, uint32_t idx, StaticMemAllocInfo*
114          memAllocInfo,uint32_t size, char* file, uint32_t line));
115 #endif
116
117 #ifdef SS_USE_ZBC_MEMORY
118 #ifdef T2K_MEM_LEAK_DBG
119 static S16 SPutZbcDBufNew ARGS((Region region, Buffer *buf,char*,uint32_t));
120
121 #else
122 static S16 SPutZbcDBuf ARGS((Region region, Buffer *buf));
123 S16 SAttachPtrToBuf ARGS(( Region   region, Pool     pool, Data    *ptr,
124          MsgLen   totalLen, Buffer** mBuf));
125 #endif
126 #endif
127
128 /* ss012.13: Addition */
129 #ifdef SS_M_PROTO_REGION 
130 #ifdef T2K_MEM_LEAK_DBG
131 #define DupMsg(region,buffer) DupMsgNew(region,buffer,file,line)
132 static Buffer *DupMsgNew ARGS((Region region, Buffer *buffer,char*,uint32_t));
133 #else
134 #ifdef INTEL_WLS
135 #ifdef T2K_MEM_LEAK_DBG
136 S16 SAttachPtrToMBuf1 ARGS(( Region   region, Pool     pool, Data    *ptr, MsgLen   totalLen,
137                                    MsgLen   ptrLen, Buffer** mBuf, char* file, uint32_t line));
138 #else
139 S16 SAttachPtrToMBuf ARGS(( Region   region, Pool     pool, Data    *ptr, MsgLen   totalLen,
140                                    MsgLen   ptrLen, Buffer** mBuf));
141 #endif
142 #ifdef T2K_MEM_LEAK_DBG
143 S16 SAttachWlsPtrToMBuf1 ARGS(( Region   region, Pool     pool, Data    *ptr, 
144                                       Data    *readPtr,    MsgLen   totalLen,
145                                       MsgLen   ptrLen, Buffer** mBuf, char* file, uint32_t line));
146
147 S16 SAttachWlsPtrToMBuf ARGS(( Region   region, Pool     pool, Data    *ptr, 
148                                       Data    *readPtr,    MsgLen   totalLen,
149                                       MsgLen   ptrLen, Buffer** mBuf));
150 #endif
151 #ifdef TENB_DPDK_BUF
152 S16 SDetachDpdkPtrFrmMBuf ARGS((Buffer   *mBuf, Data     **ptr));
153 S16 SDetachDpdkPtrFrmDBuf ARGS((Buffer   *mBuf, Data     **ptr));
154 S16 SPutSBufDpdk          ARGS(( Data    *ptr));
155 #endif  /* TENB_DPDK_BUF */
156
157
158 #endif /* INTEL_WLS */
159
160 static Buffer *DupMsg ARGS((Region region, Buffer *buffer));
161 #endif
162 #endif /* SS_M_PROTO_REGION */
163
164 /* local defines */
165 #define MBSIZE   (sizeof(SsMblk))
166 #define MDBSIZE  (sizeof(SsDblk) + sizeof(SsMblk))
167 #define MSGSIZE  (MDBSIZE + sizeof(SsMsgInfo))
168
169 /*ss014.301 SSI-4GMX specific changes*/
170 #if (defined(SS_4GMX_LCORE) && defined (RB_HEAD_ROOM))
171 #define INITMBLK(mp, dp, dat)   {\
172    mp->b_next = NULLP;\
173    mp->b_prev = NULLP;\
174    mp->b_cont = NULLP;\
175    mp->b_rptr = (dat + RB_MSG_HEAD_ROOM);\
176    mp->b_wptr = (dat + RB_MSG_HEAD_ROOM);\
177    mp->b_datap = dp;\
178 }
179 #else
180 #define INITMBLK(mp, dp, dat)   {\
181    mp->b_next = NULLP;\
182    mp->b_prev = NULLP;\
183    mp->b_cont = NULLP;\
184    mp->b_rptr = dat;\
185    mp->b_wptr = dat;\
186    mp->b_datap = dp;\
187 }
188 #endif
189
190 /* ss008.301 */
191 #ifdef SS_DBLK_FREE_RTN
192 #define INITDBLK(dp, dat, size, frtn)   {\
193    dp->db_frtnp = frtn;\
194    dp->db_base = dat;\
195    dp->db_lim = (dat == NULLP ? dat : (dat + size));\
196    dp->db_ref = 1;\
197    dp->shared = FALSE; \
198    dp->db_type = SS_M_DATA;\
199 }
200 #else
201 #define INITDBLK(dp, dat, size, frtn)   {\
202    dp->db_base = dat;\
203    dp->db_lim = (dat == NULLP ? dat : (dat + size));\
204    dp->db_ref = 1;\
205    dp->shared = FALSE; \
206    dp->db_type = SS_M_DATA;\
207 }
208 #endif /* SS_DBLK_FREE_RTN */
209
210 #define INITB(mp, dp, dat, size, frtn)   {\
211    INITMBLK(mp, dp, dat)\
212    INITDBLK(dp, dat, size, frtn)\
213 }
214
215
216 pthread_t tmpRegTidMap[20];
217 #define CM_MEM_GET_REGION(_region)                        \
218 {                                                         \
219    uint8_t  _regCnt;                                           \
220    _region = 0xFF;                                        \
221                                                           \
222    for(_regCnt = 0; _regCnt < 12; _regCnt++)              \
223    {                                                      \
224       if(tmpRegTidMap[_regCnt] == pthread_self())         \
225       {                                                   \
226          _region = _regCnt;                               \
227          break;                                           \
228       }                                                   \
229    }                                                      \
230 }
231
232 #if (defined (MAC_FREE_RING_BUF) || defined (RLC_FREE_RING_BUF) || defined(L2_OPTMZ))
233 S32 clusterMode;
234 #endif
235 /*
236 *
237 *       Fun:   ssGetDBufOfSize
238 *
239 *       Desc:  This function gets a message buffer from specified region and
240 *              size
241 *
242 *       Ret:   ROK      - ok
243 *              RFAILED  - failed, general (optional)
244 *              ROUTRES  - failed, out of resources (optional)
245 *
246 *       Notes: message is created. message is returned via message buffer
247 *              pointer. Buffer type is SS_M_DATA.
248 *              return is ok.
249 *
250 *       File:  ss_msg.c
251 *
252 */
253 #ifdef T2K_MEM_LEAK_DBG
254 S16 ssGetDBufOfSizeNew
255 (
256 Region region,
257 Size size,
258 Buffer **dBuf,
259 char* file,
260 uint32_t line
261 )
262 #else
263 #ifdef ANSI
264 S16 ssGetDBufOfSize
265 (
266 Region region,
267 Size size,
268 Buffer **dBuf
269 )
270 #else
271 S16 ssGetDBufOfSize(region, size, dBuf)
272 Region region;
273 Size size;
274 Buffer **dBuf;
275 #endif
276 #endif
277 {
278    Size mdsize;
279    Data *data;
280    SsDblk *dptr;
281
282 #if (ERRCLASS & ERRCLS_INT_PAR)
283    /* check buffer pointer */
284    if (!dBuf)
285    {
286       SSLOGERROR(ERRCLS_INT_PAR, ESS047, ERRZERO, "ssGetDBufOfSize : Null\
287                  Buffer");
288       return RFAILED;
289    }
290    if (region >= SS_MAX_REGS)
291    {
292       SSLOGERROR(ERRCLS_INT_PAR, ESS048, ERRZERO, "ssGetDBufOfSize : Invalid\
293                 region id");
294       return RFAILED;
295    }
296  
297    if (size <= 0)
298    {
299       SSLOGERROR(ERRCLS_INT_PAR, ESS049, ERRZERO, "ssGetDBufOfSize : Invalid\
300                  size");
301       return RFAILED;
302    }
303 #endif
304
305    /* ss006.301 : optmized this function */
306    mdsize = MDBSIZE + size;
307 #ifdef SS_HISTOGRAM_SUPPORT 
308    if (SAlloc(region, &mdsize, 0, (Data **) dBuf, __LINE__, (uint8_t*) __FILE__, ENTNC) != ROK)
309 #else
310    if (SAlloc(region, &mdsize, 0, (Data **) dBuf) != ROK)
311 #endif /* SS_HISTOGRAM_SUPPORT */
312    {
313       return (ROUTRES);
314    }
315    data = (Data *) (*dBuf) + MDBSIZE;
316    size = mdsize - MDBSIZE;
317  
318    dptr = (SsDblk*) (((Data *) (*dBuf)) + MBSIZE);
319  
320    INITB((*dBuf), dptr, data, size, NULLP)
321 #ifndef SS_DBUF_REFLOCK_DISABLE
322    if((SInitLock (&(dptr->dBufLock), SS_LOCK_MUTEX)) != 0)
323    {
324       SSLOGERROR(ERRCLS_DEBUG, ESS056, ERRZERO,"Falied to initalize lock");
325    }
326 #endif
327    
328    return ROK;
329 } /* ssGetDBufOfSize */
330
331
332 /*
333 *
334 *       Fun:   SGetMsgNew
335 *
336 *       Desc:  This function gets a message
337 *
338 *       Ret:   ROK      - ok
339 *              RFAILED  - failed, general (optional)
340 *              ROUTRES  - failed, out of resources (optional)
341 *
342 *       Notes: message is created. message is set to type SS_M_PROTO.
343 *              message is returned via message buffer pointer.
344 *              return is ok.
345 *
346 *       File:  ss_msg.c
347 *
348 */
349 /* ss001.301: additions */
350 #ifdef SS_HISTOGRAM_SUPPORT 
351 #ifdef ANSI
352 S16 SGetMsgNew
353 (
354 Region region,              /* region id */
355 Pool pool,                  /* pool id */
356 Buffer **mBuf,              /* pointer to message buffer */
357 uint32_t    line,
358 uint8_t     *fileName
359 )
360 #else
361 S16 SGetMsgNew(region, pool, mBuf, line, fileName)
362 Region region;              /* region id */
363 Pool pool;                  /* pool id */
364 Buffer **mBuf;              /* pointer to message buffer */
365 uint32_t    line;
366 uint8_t     *fileName;
367 #endif
368 #else /* SS_HISTOGRAM_SUPPORT */
369 #ifdef T2K_MEM_LEAK_DBG
370 S16 SGetMsgNew
371 (
372 Region region,              /* region id */
373 Pool pool,                  /* pool id */
374 Buffer **mBuf,              /* pointer to message buffer */
375 char* file,
376 uint32_t line
377 )
378 #else
379 #ifdef ANSI
380 S16 SGetMsg
381 (
382 Region region,              /* region id */
383 Pool pool,                  /* pool id */
384 Buffer **mBuf              /* pointer to message buffer */
385 )
386 #else
387 S16 SGetMsg(region, pool, mBuf)
388 Region region;              /* region id */
389 Pool pool;                  /* pool id */
390 Buffer **mBuf;              /* pointer to message buffer */
391 #endif
392 #endif
393 #endif /* SS_HISTOGRAM_SUPPORT */
394 {
395    SsMsgInfo *minfo;
396    Size size = MSGSIZE;
397    SsDblk *dptr;
398    Data *data;
399 /* ss001.301: additions */
400 #ifdef SS_HISTOGRAM_SUPPORT
401         Ent entId = 0;
402 #endif /* SS_HISTOGRAM_SUPPORT */
403
404    /* ss021.103 - Addition of return value */
405 #ifndef SS_PERF
406 #if (ERRCLASS & ERRCLS_INT_PAR)
407    S16 ret;
408 #endif
409 #endif
410
411
412 #ifdef XEON_SPECIFIC_CHANGES
413    region = 0;
414    pool   = 0;
415 #endif
416
417 #if (ERRCLASS & ERRCLS_INT_PAR)
418    /* check buffer pointer */
419    if (!mBuf)
420    {
421       SSLOGERROR(ERRCLS_INT_PAR, ESS050, ERRZERO, "SGetMsg : Null Buffer");
422       return RFAILED;
423    }
424    if (region >= SS_MAX_REGS)
425    {
426       SSLOGERROR(ERRCLS_INT_PAR, ESS051, ERRZERO, "SGetMsg : Invalid region\
427                                                    id");
428       return RFAILED;
429    }
430  
431    if (pool >= SS_MAX_POOLS_PER_REG)
432    {
433       SSLOGERROR(ERRCLS_INT_PAR, ESS052, ERRZERO, "SGetMsg : Invalid pool id");
434       return RFAILED;
435    }
436
437 /* ss037.103 Removed the semaphore operation for performance enhancement */
438
439 #ifndef SS_PERF
440    /* ss021.103 - Addition to check if region is registered */
441    /* acquire one semaphore, to protect against deregistration */
442    SS_ACQUIRE_SEMA(&osCp.regionTblSem, ret);
443    if (ret != ROK)
444    {
445
446 #if (ERRCLASS & ERRCLS_DEBUG)
447       SSLOGERROR(ERRCLS_DEBUG, ESS053, (ErrVal) ret,
448                   "Could not lock region table");
449 #endif
450
451       return RFAILED;
452    }
453 #endif
454 #endif
455 #if (ERRCLASS & ERRCLS_INT_PAR)
456    /* verify that this region is present */
457    if (osCp.regionTbl[region].used == FALSE)
458    {
459 /* ss037.103 Removed the semaphore operation for performance enhancement */
460
461 #ifndef SS_PERF
462       if( SS_RELEASE_SEMA(&osCp.regionTblSem) != ROK)
463       {
464 #if (ERRCLASS & ERRCLS_DEBUG)
465          SSLOGERROR(ERRCLS_DEBUG, ESS054, ERRZERO,
466                   "Could not release semaphore");
467          return RFAILED;
468 #endif
469       }
470 #endif
471       SSLOGERROR(ERRCLS_INT_PAR, ESS055, region, "Region not registered");
472       return RFAILED;
473    }
474 /* ss037.103 Removed the semaphore operation for performance enhancement */
475
476 #ifndef SS_PERF
477    if( SS_RELEASE_SEMA(&osCp.regionTblSem) != ROK)
478    {
479 #if (ERRCLASS & ERRCLS_DEBUG)
480       SSLOGERROR(ERRCLS_DEBUG, ESS056, ERRZERO,
481                   "Could not release semaphore");
482       return RFAILED;
483 #endif
484    }
485 #endif
486 #endif
487
488 /* ss001.301: additions */
489 #ifdef SS_HISTOGRAM_SUPPORT 
490    SGetEntInd(&entId, fileName);
491 #endif /* SS_HISTOGRAM_SUPPORT */
492
493 /* ss012.13: Addition */
494 #ifdef SS_M_PROTO_REGION
495 /* ss001.301: additions */
496 #ifdef SS_HISTOGRAM_SUPPORT
497    if (SAlloc(region, &size, 0, (Data**) mBuf, line, fileName, entId) != ROK)
498 #else
499    if (SAlloc(region, &size, 0, (Data**) mBuf) != ROK)
500 #endif /* SS_HISTOGRAM_SUPPORT */
501    {
502       SSLOGERROR(ERRCLS_DEBUG, ESS058, ERRZERO, "SGetMsg:Failed in SAlloc");
503       return (ROUTRES);
504    }
505 #else /* SS_M_PROTO_REGION */  
506 /* ss001.301: additions */
507 #ifdef SS_HISTOGRAM_SUPPORT 
508    if (SAlloc(SS_DFLT_REGION, &size, 0, (Data**) mBuf, line, fileName, entId) != ROK)
509 #else
510    if (SAlloc(region, &size, 0, (Data**) mBuf) != ROK)
511 #endif /* SS_HISTOGRAM_SUPPORT */
512    {
513       SSLOGERROR(ERRCLS_DEBUG, ESS059, ERRZERO, "SGetMsg:Failed in SAlloc");
514       return (ROUTRES);
515    }
516 #endif /* SS_M_PROTO_REGION */
517
518    dptr = (SsDblk*) (((Data *) (*mBuf)) + MBSIZE);
519    data = (Data*) (((Data *) (*mBuf)) + MDBSIZE);
520  
521    /* INITB initialises and sets up the message blk */
522    INITB((*mBuf), dptr, data, sizeof(SsMsgInfo), NULLP)
523
524    (*mBuf)->b_datap->db_type = SS_M_PROTO;
525    (*mBuf)->b_wptr = (*mBuf)->b_rptr + sizeof(SsMsgInfo);
526
527    /* initialise message info of mBuf */
528    minfo = (SsMsgInfo*) (*mBuf)->b_rptr;
529
530    minfo->region = region;
531    minfo->pool = pool;
532    minfo->len = 0;
533    minfo->endptr = NULLP;
534    minfo->eventInfo.event = SS_EVNT_DATA;
535    /* ss021.103 - Addition to initialize next and route */
536    minfo->next = NULLP;
537 #ifdef L2_OPTMZ
538    {
539       if(clusterMode == RADIO_CLUSTER_MODE)
540       {
541          (*mBuf)->refCnt = 1;
542       }
543    }
544 #endif
545    return ROK;
546 }
547
548 #ifdef RGL_SPECIFIC_CHANGES
549 S16 SSetMBufPool(Buffer *mBuf, Pool pool)
550 {
551
552     SsMsgInfo *minfo;
553
554     minfo = (SsMsgInfo*) mBuf->b_rptr; 
555     minfo->pool   = pool;
556
557     return ROK;
558
559 #endif
560
561 /* #ifdef SS_LOCKLESS_MEMORY */
562
563 /*
564 *
565 *       Fun:   SPutMsgNew
566 *
567 *       Desc:  This function deallocates a message back.
568 *
569 *       Ret:   ROK      - ok
570 *              RFAILED  - failed, general (optional)
571 *
572 *       Notes: all data attached to message is returned to memory.
573 *              message is returned to memory. return is ok.
574 *
575 *       File:  ss_msg.c
576 *
577 */
578 /* ss001.301: additions */
579 #ifdef SS_HISTOGRAM_SUPPORT 
580 #ifdef ANSI
581 S16 SPutMsgNew
582 (
583 Buffer *mBuf,
584 uint32_t    line,
585 uint8_t     *fileName
586 )
587 #else
588 S16 SPutMsgNew(mBuf, line, fileName)
589 Buffer *mBuf;
590 uint32_t    line;
591 uint8_t     *fileName;
592 #endif
593 #else /* SS_HISTOGRAM_SUPPORT */
594 #ifdef T2K_MEM_LEAK_DBG
595 S16 SPutMsgNew
596 (
597 Buffer *mBuf,
598 char* file,
599 uint32_t line
600 )
601 #else
602 #ifdef ANSI
603 S16 SPutMsg
604 (
605 Buffer *mBuf
606 )
607 #else
608 S16 SPutMsg(mBuf)
609 Buffer *mBuf;
610 #endif
611 #endif
612 #endif /* SS_HISTOGRAM_SUPPORT */
613 {
614    Buffer *tmp;
615    SsMsgInfo *minfo;
616 #ifdef SS_MEM_WL_DEBUG
617    uint8_t     tmpThrReg;
618 #endif
619
620 /* ss001.301: additions */
621 #ifdef SS_HISTOGRAM_SUPPORT
622         Ent entId = 0;
623 #endif /* SS_HISTOGRAM_SUPPORT */
624
625 #ifdef L2_OPTMZ
626 //#ifdef RADIO_CLUSTER
627       if(clusterMode == RADIO_CLUSTER_MODE)
628       {
629          if(mBuf->refCnt)
630          {
631             mBuf->refCnt -= 1;
632          }
633          if (mBuf->refCnt > 0)
634          {
635             return ROK;
636          }
637       }
638 #endif
639 #if (defined (MAC_FREE_RING_BUF) || defined (RLC_FREE_RING_BUF))
640    {
641       pthread_t gMacTId,gRlcTId;
642       S32 clusterMode;
643       if(clusterMode == RADIO_CLUSTER_MODE)
644       {
645
646 #ifdef MAC_FREE_RING_BUF
647          if(pthread_self() == gMacTId)
648          //if(pthread_equal(pthread_self(),gMacTId))
649          {
650             if(ROK == mtAddBufToRing(SS_RNG_BUF_MAC_FREE_RING,(Void *)mBuf,0))
651             {
652                return ROK;
653             }
654          }
655 #endif
656 #ifdef RLC_FREE_RING_BUF
657          else if(pthread_self() == gRlcTId)
658          {
659             if(ROK == mtAddBufToRing(SS_RNG_BUF_RLC_FREE_RING,(Void *)mBuf,0))
660             {
661                return ROK;
662             }
663          }
664 #endif
665       }
666    }
667 #endif
668
669 #if (ERRCLASS & ERRCLS_INT_PAR)
670    if (mBuf == NULLP)
671    {
672       SSLOGERROR(ERRCLS_INT_PAR, ESS060, ERRZERO, "SPutMsg: Null Buffer");
673       return RFAILED;
674    }
675    if (mBuf->b_datap->db_type != SS_M_PROTO)
676    {
677       SSLOGERROR(ERRCLS_INT_PAR, ESS061, ERRZERO, "SPutMsg: Incorrect buffer\
678                                                    type");
679       return RFAILED;
680    }
681 #endif
682
683 #ifdef SS_MEM_WL_DEBUG
684    CM_MEM_GET_REGION(tmpThrReg)
685    if(tmpThrReg == 0xFF)
686    {
687       printf("\n Not able to get region \n");
688       return RFAILED;
689    }
690 #endif
691
692    /* get the message info */
693    minfo = (SsMsgInfo *) mBuf->b_rptr;
694
695    while ((tmp = mBuf->b_cont))
696    {
697       /* set b_cont of mBuf to point to the b_cont of tmp */
698       mBuf->b_cont = tmp->b_cont;
699 #ifdef SS_MEM_WL_DEBUG
700       (Void) SPutDBuf(tmpThrReg, minfo->pool, tmp);
701 #else
702       (Void) SPutDBuf(minfo->region, minfo->pool, tmp);
703 #endif
704    }
705
706 /* ss001.301: additions */
707 #ifdef SS_HISTOGRAM_SUPPORT 
708    SGetEntInd(&entId, fileName);
709 #endif /* SS_HISTOGRAM_SUPPORT */
710
711 /* ss012.13: Addition */
712 #ifdef SS_M_PROTO_REGION
713    /* ss021.103 - Addition to check return value of SFree */
714 #ifdef SS_HISTOGRAM_SUPPORT
715    if (SFree(minfo->region, (Data *) mBuf, MSGSIZE, line, fileName, entId) == RFAILED)
716 #else
717 #ifdef SS_MEM_WL_DEBUG
718    if (SFree(tmpThrReg, (Data *) mBuf, MSGSIZE) == RFAILED)
719 #else
720    if (SFree(minfo->region, (Data *) mBuf, MSGSIZE) == RFAILED)
721 #endif
722 #endif /* SS_HISTOGRAM_SUPPORT */
723       return RFAILED;
724 #else /* SS_M_PROTO_REGION */
725    /* ss021.103 - Addition to check return value of SFree */
726 #ifdef SS_HISTOGRAM_SUPPORT
727    if (SFree(SS_DFLT_REGION, (Data *) mBuf, MSGSIZE, line, fileName, entId) == RFAILED)
728 #else
729    if (SFree(minfo->region, (Data *) mBuf, MSGSIZE) == RFAILED)
730 #endif /* SS_HISTOGRAM_SUPPORT */
731       return RFAILED;
732 #endif /* SS_M_PROTO_REGION */
733
734    return ROK;
735 }
736
737 #ifdef SS_LOCKLESS_MEMORY
738 /*
739 *
740 *       Fun:   SPutStaticBuffer
741 *
742 *       Desc:  Returns a buffer to the specified static pool in the
743 *              specified memory region.
744 *
745 *       Ret:   ROK      - ok
746 *              RFAILED  - failed, general (optional)
747 *
748 *       Notes:
749 *
750 *       File:  ss_msg.c
751 *
752 */
753 /* ss001.301: additions */
754 #ifdef T2K_MEM_LEAK_DBG
755 S16 SPutStaticBufferNew
756 (
757 Region region,                  /* region ID */
758 Pool pool,                      /* pool ID */
759 Data *ptr,                      /* pointer to buffer */
760 Size size,                      /* size */
761 uint8_t   memType,                    /* memory type used if shareable or not */
762 char* file,
763 uint32_t line
764 )
765 #else
766 #ifdef ANSI
767 S16 SPutStaticBuffer
768 (
769 Region region,                  /* region ID */
770 Pool pool,                      /* pool ID */
771 Data *ptr,                      /* pointer to buffer */
772 Size size,                      /* size */
773 uint8_t   memType                    /* memory type used if shareable or not */
774 )
775 #else
776 S16 SPutStaticBuffer(region, pool, ptr, size)
777 Region region;                  /* region ID */
778 Pool pool;                      /* pool ID */
779 Data *ptr;                      /* pointer to buffer */
780 Size size;                      /* size */
781 uint8_t   memType;                   /* memory type used if shareable or not */
782 #endif
783 #endif
784 {
785    S16 ret = RFAILED;
786
787 #if (ERRCLASS & ERRCLS_INT_PAR)
788    /* validate region ID */
789    if (region >= SS_MAX_REGS)
790    {
791       SSLOGERROR(ERRCLS_INT_PAR, ESS220, region, "Invalid region");
792       return RFAILED;
793    }
794
795    /* validate pool ID */
796    if (pool >= SS_MAX_POOLS_PER_REG)
797    {
798       SSLOGERROR(ERRCLS_INT_PAR, ESS221, pool, "Invalid pool");
799       return RFAILED;
800    }
801    /* validate data pointer */
802    if (ptr == NULLP)
803    {
804       SSLOGERROR(ERRCLS_INT_PAR, ESS223, (ErrVal)0, "NULL data pointe");
805       return RFAILED;
806    }
807
808    /* validate size */
809    if (size <= 0)
810    {
811       SSLOGERROR(ERRCLS_INT_PAR, ESS224, size, "Invalid size");
812       return RFAILED;
813    }
814 #endif
815 #if (defined (MAC_FREE_RING_BUF) || defined (RLC_FREE_RING_BUF))
816    {
817       pthread_t gMacTId,gRlcTId;
818
819       if(clusterMode == RADIO_CLUSTER_MODE)
820       {
821 #ifdef MAC_FREE_RING_BUF
822          if(pthread_self() == gMacTId)
823          //if(pthread_equal(pthread_self(),gMacTId))
824          {
825             if(ROK == mtAddBufToRing(SS_RNG_BUF_MAC_FREE_RING,(Void *)ptr,1))
826             {
827                return ROK;
828             }
829          }
830 #endif
831 #ifdef RLC_FREE_RING_BUF
832          else if(pthread_self() == gRlcTId)
833          {
834             if(ROK == mtAddBufToRing(SS_RNG_BUF_RLC_FREE_RING,(Void *)ptr,1))
835             {
836                return ROK;
837             }
838          }
839       }
840    }
841 #endif
842 #endif
843    region = SS_GET_THREAD_MEM_REGION();
844    
845
846 #if (ERRCLASS & ERRCLS_INT_PAR)
847    /* verify that this region is present */
848    if (((memType == SS_NON_SHARABLE_MEMORY) && 
849        (osCp.regionTbl[region].used == FALSE)) ||
850        ((memType == SS_SHARABLE_MEMORY) && 
851        (osCp.dynRegionTbl[region].used == FALSE)))
852    {
853       SSLOGERROR(ERRCLS_INT_PAR, ESS218, region, "Region not registered");
854       return (ret);
855    }
856 #endif
857
858    /* call the memory manager, to allocate this memory */
859 #ifndef SS_DBUF_REFLOCK_DISABLE
860    if(memType == SS_NON_SHARABLE_MEMORY)
861    {
862     #ifndef T2K_MEM_LEAK_DBG
863     ret = (osCp.regionTbl[region].free)
864                  (osCp.regionTbl[region].regCb, ptr, size);
865     #else
866     ret = (osCp.regionTbl[region].free)
867                  (osCp.regionTbl[region].regCb, ptr, size, file, line);
868    
869     #endif
870    }
871    else if(memType == SS_SHARABLE_MEMORY)
872    {
873 #endif
874
875 #ifndef T2K_MEM_LEAK_DBG
876       ret = (osCp.dynRegionTbl[region].free)
877               (osCp.dynRegionTbl[region].regCb, ptr, size);
878 #else
879       ret = (osCp.dynRegionTbl[region].free)
880               (osCp.dynRegionTbl[region].regCb, ptr, size,file,line);
881 #endif
882 #ifndef SS_DBUF_REFLOCK_DISABLE
883    }
884 #endif
885
886    return (ret);
887 }
888
889 /*
890 *
891 *       Fun:   SGetStaticBuffer
892 *
893 *       Desc:  Allocates a buffer from the specified static memory pool
894 *              in the specified region. Depending on the type wheather 
895 *              the buffer is sharable or non-sharable indicated by the 
896 *              memType prameter allocation region is choosen
897 *              done
898 *
899 *       Ret:   ROK      - ok
900 *              RFAILED  - failed, general (optional)
901 *
902 *       Notes: 
903 *
904 *       File:  ss_msg.c
905 *
906 */
907 #ifdef T2K_MEM_LEAK_DBG
908 S16 SGetStaticBufferNew
909 (
910 Region region,                  /* region ID */
911 Pool pool,                      /* pool ID */
912 Data **ptr,                     /* pointer to buffer */
913 Size size,                      /* size requested */
914 uint8_t   memType,                    /* memory type used if shareable or not */
915 char* file,
916 uint32_t line
917 )
918 #else
919 #ifdef ANSI
920 S16 SGetStaticBuffer
921 (
922 Region region,                  /* region ID */
923 Pool pool,                      /* pool ID */
924 Data **ptr,                     /* pointer to buffer */
925 Size size,                      /* size requested */
926 uint8_t   memType                    /* memory type used if shareable or not */
927 )
928 #else
929 S16 SGetStaticBuffer(region, pool, ptr, size)
930 Region region;                  /* region ID */
931 Pool pool;                      /* pool ID */
932 Data **ptr;                     /* pointer to buffer */
933 Size size;                      /* size requested */
934 uint8_t   memType;                   /* memory type used if shareable or not */
935 #endif
936 #endif
937 {
938    S16 ret = RFAILED;
939    uint32_t flags;
940
941 #if (ERRCLASS & ERRCLS_INT_PAR)
942    /* validate region ID */
943    if (region >= SS_MAX_REGS)
944    {
945       SSLOGERROR(ERRCLS_INT_PAR, ESS211, region, "Invalid region");
946       return (ret);
947    }
948
949    /* validate pool ID */
950    if (pool >= SS_MAX_POOLS_PER_REG)
951    {
952       SSLOGERROR(ERRCLS_INT_PAR, ESS212, pool, "Invalid pool");
953       return (ret);
954    }
955    /* validate data pointer */
956    if (ptr == NULLP)
957    {
958       SSLOGERROR(ERRCLS_INT_PAR, ESS214, (ErrVal)0 , "NULL data pointer");
959       return (ret);
960    }
961
962    /* validate size */
963    if (size <= 0)
964    {
965       SSLOGERROR(ERRCLS_INT_PAR, ESS215, size, "Invalid size");
966       return (ret);
967    }
968 #endif
969
970 #if (ERRCLASS & ERRCLS_INT_PAR)
971    /* verify that this region is present */
972    if (((memType == SS_NON_SHARABLE_MEMORY) && 
973        (osCp.regionTbl[region].used == FALSE)) ||
974        ((memType == SS_SHARABLE_MEMORY) && 
975        (osCp.dynRegionTbl[region].used == FALSE)))
976    {
977       SSLOGERROR(ERRCLS_INT_PAR, ESS218, region, "Region not registered");
978       return (ret);
979    }
980 #endif
981
982    flags = 0;
983
984    region = SS_GET_THREAD_MEM_REGION();
985 #ifdef USE_MEMCAL
986    if (region == SS_STATIC_REGION)
987       flags = 1;
988 #endif
989    
990    /* call the memory manager, to allocate this memory */
991 #ifndef SS_DBUF_REFLOCK_DISABLE
992    if(memType == SS_NON_SHARABLE_MEMORY)
993    {
994 #ifndef T2K_MEM_LEAK_DBG
995       ret = (osCp.regionTbl[region].alloc)
996                  (osCp.regionTbl[region].regCb, &size, flags, ptr);
997 #else
998       ret = (osCp.regionTbl[region].alloc)
999                  (osCp.regionTbl[region].regCb, &size, flags, ptr, file,line);
1000 #endif
1001    }
1002    else if(memType == SS_SHARABLE_MEMORY)
1003    {
1004 #endif
1005 #ifndef T2K_MEM_LEAK_DBG
1006       ret = (osCp.dynRegionTbl[region].alloc)
1007               (osCp.dynRegionTbl[region].regCb, &size, flags, ptr);
1008 #else
1009       ret = (osCp.dynRegionTbl[region].alloc)
1010               (osCp.dynRegionTbl[region].regCb, &size, flags, ptr,file,line);
1011 #endif
1012 #ifndef SS_DBUF_REFLOCK_DISABLE
1013    }
1014 #endif
1015
1016    return (ret);
1017 }
1018 #endif /* SS_LOCKLESS_MEMORY */
1019
1020 #ifdef INTEL_WLS_MEM 
1021 #ifndef SS_LOCKLESS_MEMORY
1022 #ifdef ANSI
1023 S16 SGetStaticBuffer
1024 (
1025 Region region,                  /* region ID */
1026 Pool pool,                      /* pool ID */
1027 Data **ptr,                     /* pointer to buffer */
1028 Size size,                      /* size requested */
1029 uint8_t   memType                    /* memory type used if shareable or not */
1030 )
1031 #else
1032 S16 SGetStaticBuffer(region, pool, ptr, size)
1033 Region region;                  /* region ID */
1034 Pool pool;                      /* pool ID */
1035 Data **ptr;                     /* pointer to buffer */
1036 Size size;                      /* size requested */
1037 uint8_t   memType;                   /* memory type used if shareable or not */
1038 #endif
1039 {
1040     S16  ret;
1041
1042     ret = SGetSBuf(region, pool, ptr, size);
1043
1044     return (ret);
1045 }
1046
1047
1048 #ifdef ANSI
1049 S16 SPutStaticBuffer
1050 (
1051 Region region,                  /* region ID */
1052 Pool pool,                      /* pool ID */
1053 Data *ptr,                      /* pointer to buffer */
1054 Size size,                      /* size */
1055 uint8_t   memType                    /* memory type used if shareable or not */
1056 )
1057 #else
1058 S16 SPutStaticBuffer(region, pool, ptr, size)
1059 Region region;                  /* region ID */
1060 Pool pool;                      /* pool ID */
1061 Data *ptr;                      /* pointer to buffer */
1062 Size size;                      /* size */
1063 uint8_t   memType;                   /* memory type used if shareable or not */
1064 #endif
1065 {
1066
1067    S16   ret;
1068  
1069    ret = SPutSBuf(region, pool, ptr, size);
1070
1071    return (ret);
1072
1073 }
1074 #endif
1075 #ifdef T2K_MEM_LEAK_DBG
1076 S16 SGetSBufWls1
1077 (
1078 Region region,                  /* region ID */
1079 Pool pool,                      /* pool ID */
1080 Data **ptr,                     /* pointer to buffer */
1081 Size size,                       /* size requested */
1082 char* file,
1083 uint32_t line
1084 )
1085 #else
1086 S16 SGetSBufWls
1087 (
1088 Region region,                  /* region ID */
1089 Pool pool,                      /* pool ID */
1090 Data **ptr,                     /* pointer to buffer */
1091 Size size                       /* size requested */
1092 )
1093 #endif
1094 {
1095     S16   ret;
1096 #ifndef SS_LOCKLESS_MEMORY
1097     uint32_t   flags = 0;
1098 #endif
1099
1100 #ifdef SS_LOCKLESS_MEMORY
1101     region = SS_GET_THREAD_MEM_REGION();
1102     ret    = SAlloc(region, &size, 0, ptr);
1103 #else
1104     region = 0;
1105 #ifdef T2K_MEM_LEAK_DBG
1106     ret = (osCp.regionTbl[region].alloc)
1107                (osCp.regionTbl[region].regCb, &size, flags, ptr,file,line);
1108 #else
1109     ret = (osCp.regionTbl[region].alloc)
1110                (osCp.regionTbl[region].regCb, &size, flags, ptr);
1111 #endif
1112 #endif
1113
1114     return (ret);
1115 }
1116
1117 #ifdef T2K_MEM_LEAK_DBG
1118 S16 SPutSBufWls1
1119 (
1120 Region region,                  /* region ID */
1121 Pool pool,                      /* pool ID */
1122 Data *ptr,                      /* pointer to buffer */
1123 Size size,                      /* size */
1124 char* file,
1125 uint32_t line
1126 )
1127 #else
1128 S16 SPutSBufWls
1129 (
1130 Region region,                  /* region ID */
1131 Pool pool,                      /* pool ID */
1132 Data *ptr,                      /* pointer to buffer */
1133 Size size                      /* size */
1134 )
1135 #endif
1136 {
1137    S16   ret;
1138
1139 #ifdef SS_LOCKLESS_MEMORY
1140     region = SS_GET_THREAD_MEM_REGION();
1141     ret    = SFree(region, ptr, size);
1142 #else
1143    region = 0;
1144 #ifdef T2K_MEM_LEAK_DBG
1145    ret = (osCp.regionTbl[region].free)(osCp.regionTbl[region].regCb, ptr, size,file,line);
1146 #else
1147    ret = (osCp.regionTbl[region].free)(osCp.regionTbl[region].regCb, ptr, size);
1148 #endif
1149 #endif
1150
1151    return (ret);
1152 }
1153
1154 #endif  /* INTEL_WLS */
1155
1156 \f
1157 /*
1158 *
1159 *       Fun:   SGetSBufNew
1160 *
1161 *       Desc:  Allocates a buffer from the specified static memory pool
1162 *              in the specified region.
1163 *
1164 *       Ret:   ROK      - ok
1165 *              RFAILED  - failed, general (optional)
1166 *
1167 *       Notes: The new memory management scheme eliminates the concept
1168 *              of pools. This call maps directly to a call to the memory
1169 *              manager.
1170 *
1171 *       File:  ss_msg.c
1172 *
1173 */
1174 /* ss001.301: additions */
1175 #ifdef SS_HISTOGRAM_SUPPORT
1176 #ifdef ANSI
1177 S16 SGetSBufNew
1178 (
1179 Region region,                  /* region ID */
1180 Pool pool,                      /* pool ID */
1181 Data **ptr,                     /* pointer to buffer */
1182 Size size,                       /* size requested */
1183 uint32_t    line,
1184 uint8_t     *fileName
1185 )
1186 #else
1187 S16 SGetSBufNew(region, pool, ptr, size, line, fileName)
1188 Region region;                  /* region ID */
1189 Pool pool;                      /* pool ID */
1190 Data **ptr;                     /* pointer to buffer */
1191 Size size;                      /* size requested */
1192 uint32_t    line;
1193 uint8_t     *fileName;
1194 #endif
1195 #else /* SS_HISTOGRAM_SUPPORT */
1196 #if (defined(SSI_STATIC_MEM_LEAK_DETECTION) || defined(T2K_MEM_LEAK_DBG))
1197 #ifdef ANSI
1198 S16 SGetSBuf1
1199 (
1200 Region region,                  /* region ID */
1201 Pool pool,                      /* pool ID */
1202 Data **ptr,                     /* pointer to buffer */
1203 Size size,                       /* size requested */
1204 char* file,
1205 uint32_t line
1206 )
1207 #else
1208 S16 SGetSBuf1(region, pool, ptr, size, file, line)
1209 Region region;                  /* region ID */
1210 Pool pool;                      /* pool ID */
1211 Data **ptr;                     /* pointer to buffer */
1212 Size size;                      /* size requested */
1213 char* file;
1214 uint32_t line;
1215 #endif
1216 #else
1217 #ifdef ANSI
1218 S16 SGetSBuf
1219 (
1220 Region region,                  /* region ID */
1221 Pool pool,                      /* pool ID */
1222 Data **ptr,                     /* pointer to buffer */
1223 Size size                       /* size requested */
1224 )
1225 #else
1226 S16 SGetSBuf(region, pool, ptr, size)
1227 Region region;                  /* region ID */
1228 Pool pool;                      /* pool ID */
1229 Data **ptr;                     /* pointer to buffer */
1230 Size size;                      /* size requested */
1231 #endif
1232 #endif
1233 #endif /* SS_HISTOGRAM_SUPPORT */
1234 {
1235    S16 ret;
1236    uint32_t flags;
1237 #ifdef SSI_STATIC_MEM_LEAK_DETECTION
1238    Size tmpSize;
1239 #endif
1240
1241 #ifdef SS_HISTOGRAM_SUPPORT
1242         Ent entId = 0;
1243         Bool hstReg = FALSE;
1244 #endif /* SS_HISTOGRAM_SUPPORT */
1245
1246
1247    region = SS_GET_THREAD_MEM_REGION();
1248 #ifdef INTEL_WLS
1249    region = 1;
1250 #endif /* INTEL_WLS */
1251
1252 #if (ERRCLASS & ERRCLS_INT_PAR)
1253    /* validate region ID */
1254    if (region >= SS_MAX_REGS)
1255    {
1256       SSLOGERROR(ERRCLS_INT_PAR, ESS211, region, "Invalid region");
1257       return RFAILED;
1258    }
1259
1260    /* validate pool ID */
1261    if (pool >= SS_MAX_POOLS_PER_REG)
1262    {
1263       SSLOGERROR(ERRCLS_INT_PAR, ESS212, pool, "Invalid pool");
1264       return RFAILED;
1265    }
1266 /* ss008.13: addition */
1267    /* validate data pointer */
1268    if (ptr == NULLP)
1269    {
1270       SSLOGERROR(ERRCLS_INT_PAR, ESS214, (ErrVal)0 , "NULL data pointer");
1271       return RFAILED;
1272    }
1273
1274    /* validate size */
1275    if (size <= 0)
1276    {
1277       SSLOGERROR(ERRCLS_INT_PAR, ESS215, size, "Invalid size");
1278       return RFAILED;
1279    }
1280 #endif
1281 /* ss037.103 Removed the semaphore operation for performance enhancement */
1282
1283 #ifndef RGL_SPECIFIC_CHANGES
1284    region = SS_GET_THREAD_MEM_REGION();
1285 #endif
1286 #ifndef SS_PERF
1287    /* acquire one semaphore, to protect against deregistration */
1288    SS_ACQUIRE_SEMA(&osCp.regionTblSem, ret);
1289    if (ret != ROK)
1290    {
1291
1292 #if (ERRCLASS & ERRCLS_DEBUG)
1293       SSLOGERROR(ERRCLS_DEBUG, ESS216, (ErrVal) ret,
1294                   "Could not lock region table");
1295 #endif
1296
1297       return RFAILED;
1298    }
1299
1300 #endif
1301
1302 #if (ERRCLASS & ERRCLS_INT_PAR)
1303    /* verify that this region is present */
1304    if (osCp.regionTbl[region].used == FALSE)
1305    {
1306
1307 #ifndef SS_PERF
1308 /* ss006.13: addition */
1309       if( SS_RELEASE_SEMA(&osCp.regionTblSem) != ROK)
1310       {
1311 #if (ERRCLASS & ERRCLS_DEBUG)
1312          SSLOGERROR(ERRCLS_DEBUG, ESS217, ERRZERO,
1313                   "Could not release semaphore");
1314          return RFAILED;
1315 #endif
1316       }
1317
1318       SSLOGERROR(ERRCLS_INT_PAR, ESS218, region, "Region not registered");
1319       return RFAILED;
1320 #endif
1321    }
1322 #endif
1323
1324
1325    flags = 0;
1326
1327    /* ss024.103 - Addition for memory calculator tool */
1328 #ifdef USE_MEMCAL
1329    if (region == SS_STATIC_REGION)
1330       flags = 1;
1331 #endif
1332
1333 /* ss001.301: additions */
1334 #ifdef SS_HISTOGRAM_SUPPORT 
1335
1336       SGetEntInd(&entId, fileName);
1337       /* Get the Information from the oscp that the tapa task with the entity 
1338          id (entid) is registed for histogram or not */
1339       SGetHstGrmInfo(&entId, &hstReg);
1340    /* call the memory manager, to allocate this memory */
1341 #ifdef SSI_DEBUG_LEVEL1
1342    ret = (osCp.regionTbl[region].alloc)
1343                (osCp.regionTbl[region].regCb, &size, flags, ptr, 
1344                 SS_STATIC_MEM_FLAG, line, fileName, entId, hstReg);
1345 #else
1346    ret = (osCp.regionTbl[region].alloc)
1347                (osCp.regionTbl[region].regCb, &size, flags, ptr, 
1348                 line, fileName, entId, hstReg);
1349 #endif /* SSI_DEBUG_LEVEL1 */
1350
1351 #else 
1352    
1353    /* call the memory manager, to allocate this memory */
1354 /* ss036.103 - addition for passing additional parameter memType as static */
1355 #ifdef SSI_DEBUG_LEVEL1
1356    ret = (osCp.regionTbl[region].alloc)
1357                (osCp.regionTbl[region].regCb, &size, flags, ptr, SS_STATIC_MEM_FLAG);
1358 #else
1359 #ifdef SSI_STATIC_MEM_LEAK_DETECTION
1360    /* Static mem leak detection changes */
1361    tmpSize = size + 4;
1362 #ifdef T2K_MEM_LEAK_DBG
1363    ret = (osCp.regionTbl[region].alloc)
1364                (osCp.regionTbl[region].regCb, &tmpSize, flags, ptr, file, line);
1365 #else
1366    ret = (osCp.regionTbl[region].alloc)
1367                (osCp.regionTbl[region].regCb, &tmpSize, flags, ptr);
1368 #endif
1369    /*size = tmpSize - 4;*/
1370    {
1371       /*printf("ptr = %p *ptr = %p\n",ptr,*ptr);*/
1372 #ifdef XEON_SPECIFIC_CHANGES
1373       pthread_mutex_lock(&(memLock));
1374 #endif      
1375       uint32_t idx = GetNextFreeIdx(&SMemLeakInfo[region]);
1376       uint8_t* allocatedPtr = *ptr;
1377       void* actualPtr = allocatedPtr + 4;
1378       *ptr = actualPtr;
1379       /* store the index in the memory allocated itself */
1380       /**((uint32_t*)*((uint32_t*)allocatedPtr)) = idx;*/
1381       *((uint32_t*)allocatedPtr) = idx;
1382       /*printf("region = %d idx = %d ptr = %p *ptr = %p actual = %p allocated = %p content = %d\n",region, idx, ptr, *ptr, actualPtr, allocatedPtr, *((uint32_t*)allocatedPtr));*/
1383
1384       LogForStaticMemLeak(&SMemLeakInfo[region],
1385                           file,
1386                           line,
1387                           size,
1388                           *ptr,
1389                           idx);
1390 #ifdef XEON_SPECIFIC_CHANGES
1391       pthread_mutex_unlock(&(memLock));
1392 #endif      
1393    }
1394 #else
1395 #ifndef T2K_MEM_LEAK_DBG
1396    ret = (osCp.regionTbl[region].alloc)
1397                (osCp.regionTbl[region].regCb, &size, flags, ptr);
1398 #else
1399    ret = (osCp.regionTbl[region].alloc)
1400                (osCp.regionTbl[region].regCb, &size, flags, ptr, file, line);
1401 #endif
1402
1403 #endif
1404 #endif /* SSI_DEBUG_LEVEL1 */
1405
1406 #endif /* SS_HISTOGRAM_SUPPORT */
1407
1408    /* release the semaphore we took */
1409
1410 #ifndef SS_PERF
1411 /* ss006.13: addition */
1412    if( SS_RELEASE_SEMA(&osCp.regionTblSem) != ROK)
1413    {
1414 #if (ERRCLASS & ERRCLS_DEBUG)
1415       SSLOGERROR(ERRCLS_DEBUG, ESS219, ERRZERO,
1416                   "Could not release semaphore");
1417       return RFAILED;
1418 #endif
1419    }
1420 #endif
1421 /* ss036.103 - Addition to handle the memory trampling return value
1422 * This in turn might invoke SRegMemErrHdlr  
1423 */
1424 #ifdef SSI_DEBUG_LEVEL1
1425     if (ret == RTRAMPLINGNOK)
1426     {
1427        SRegMemErrHdlr( region, *ptr, ret);
1428     }
1429 #endif /* SSI_DEBUG_LEVEL1 */
1430
1431    return (ret);
1432 }
1433
1434 \f
1435 /*
1436 *
1437 *       Fun:   SPutSBufNew
1438 *
1439 *       Desc:  Returns a buffer to the specified static pool in the
1440 *              specified memory region.
1441 *
1442 *       Ret:   ROK      - ok
1443 *              RFAILED  - failed, general (optional)
1444 *
1445 *       Notes:
1446 *
1447 *       File:  ss_msg.c
1448 *
1449 */
1450 /* ss001.301: additions */
1451 #ifdef SS_HISTOGRAM_SUPPORT
1452 #ifdef ANSI
1453 S16 SPutSBufNew
1454 (
1455 Region region,                  /* region ID */
1456 Pool pool,                      /* pool ID */
1457 Data *ptr,                      /* pointer to buffer */
1458 Size size,                      /* size */
1459 uint32_t    line,
1460 uint8_t     *fileName
1461 )
1462 #else
1463 S16 SPutSBufNew(region, pool, ptr, size, line, fileName)
1464 Region region;                  /* region ID */
1465 Pool pool;                      /* pool ID */
1466 Data *ptr;                      /* pointer to buffer */
1467 Size size;                      /* size */
1468 uint32_t    line;
1469 uint8_t     *fileName;
1470 #endif
1471 #else  /* SS_HISTOGRAM_SUPPORT  */
1472 #if (defined(SSI_STATIC_MEM_LEAK_DETECTION) || defined(T2K_MEM_LEAK_DBG))
1473 #ifdef ANSI
1474 S16 SPutSBuf1
1475 (
1476 Region region,                  /* region ID */
1477 Pool pool,                      /* pool ID */
1478 Data *ptr,                      /* pointer to buffer */
1479 Size size,                      /* size */
1480 char* file,
1481 uint32_t line
1482 )
1483 #else
1484 S16 SPutSBuf1(region, pool, ptr, size, file, line)
1485 Region region;                  /* region ID */
1486 Pool pool;                      /* pool ID */
1487 Data *ptr;                      /* pointer to buffer */
1488 Size size;                      /* size */
1489 char* file;
1490 uint32_t line;
1491 #endif
1492 #else
1493 #ifdef ANSI
1494 S16 SPutSBuf
1495 (
1496 Region region,                  /* region ID */
1497 Pool pool,                      /* pool ID */
1498 Data *ptr,                      /* pointer to buffer */
1499 Size size                      /* size */
1500 )
1501 #else
1502 S16 SPutSBuf(region, pool, ptr, size)
1503 Region region;                  /* region ID */
1504 Pool pool;                      /* pool ID */
1505 Data *ptr;                      /* pointer to buffer */
1506 Size size;                      /* size */
1507 #endif
1508 #endif
1509 #endif /* SS_HISTOGRAM_SUPPORT */
1510 {
1511    S16 ret;
1512
1513 #ifdef SS_HISTOGRAM_SUPPORT
1514         Ent entId = 0;
1515         Bool hstReg = FALSE;
1516 #endif /* SS_HISTOGRAM_SUPPORT */
1517
1518    region = SS_GET_THREAD_MEM_REGION();
1519 #if (!defined(SS_LOCKLESS_MEMORY) && defined(INTEL_WLS))
1520    region = 1;
1521 #endif /* INTEL_WLS */
1522
1523 #if (ERRCLASS & ERRCLS_INT_PAR)
1524    /* validate region ID */
1525    if (region >= SS_MAX_REGS)
1526    {
1527       SSLOGERROR(ERRCLS_INT_PAR, ESS220, region, "Invalid region");
1528       return RFAILED;
1529    }
1530
1531    /* validate pool ID */
1532    if (pool >= SS_MAX_POOLS_PER_REG)
1533    {
1534       SSLOGERROR(ERRCLS_INT_PAR, ESS221, pool, "Invalid pool");
1535       return RFAILED;
1536    }
1537 /* ss008.13: addition */
1538    /* validate data pointer */
1539    if (ptr == NULLP)
1540    {
1541       SSLOGERROR(ERRCLS_INT_PAR, ESS223, (ErrVal)0, "NULL data pointe");
1542       return RFAILED;
1543    }
1544
1545    /* validate size */
1546    if (size <= 0)
1547    {
1548       SSLOGERROR(ERRCLS_INT_PAR, ESS224, size, "Invalid size");
1549       return RFAILED;
1550    }
1551 #endif
1552 /* ss037.103 Removed the semaphore operation for performance enhancement */
1553 #ifndef RGL_SPECIFIC_CHANGES
1554    region = SS_GET_THREAD_MEM_REGION();
1555 #endif
1556 #ifndef SS_PERF
1557    /* acquire one semaphore, to protect against deregistration */
1558    SS_ACQUIRE_SEMA(&osCp.regionTblSem, ret);
1559    if (ret != ROK)
1560    {
1561
1562 #if (ERRCLASS & ERRCLS_DEBUG)
1563       SSLOGERROR(ERRCLS_DEBUG, ESS225, (ErrVal) ret,
1564                   "Could not lock region table");
1565 #endif
1566
1567       return RFAILED;
1568    }
1569 #endif
1570
1571 #if (ERRCLASS & ERRCLS_INT_PAR)
1572    /* verify that this region is around */
1573    if (osCp.regionTbl[region].used == FALSE)
1574    {
1575 #ifndef SS_PERF
1576
1577 /* ss006.13: addition */
1578       if( SS_RELEASE_SEMA(&osCp.regionTblSem) != ROK)
1579       {
1580 #if (ERRCLASS & ERRCLS_DEBUG)
1581          SSLOGERROR(ERRCLS_DEBUG, ESS226, ERRZERO,
1582                   "Could not release semaphore");
1583          return RFAILED;
1584 #endif
1585       }
1586 #endif
1587       SSLOGERROR(ERRCLS_INT_PAR, ESS227, region, "Region not registered");
1588       return RFAILED;
1589    }
1590 #endif
1591
1592 #ifdef SSI_STATIC_MEM_LEAK_DETECTION
1593 /* Static mem leak detection changes */
1594    {
1595 #ifdef XEON_SPECIFIC_CHANGES
1596       pthread_mutex_lock(&(memLock));
1597 #endif      
1598       uint32_t idx = *((uint32_t*)((uint8_t *)ptr - 4));
1599       FreeIdx(ptr,idx,&SMemLeakInfo[region],size,file,line);
1600 #ifdef XEON_SPECIFIC_CHANGES
1601       pthread_mutex_unlock(&(memLock));
1602 #endif      
1603    }
1604 /* Static mem leak detection changes */
1605 #endif
1606
1607 /* ss001.301: additions */
1608 #ifdef SS_HISTOGRAM_SUPPORT 
1609      SGetEntInd(&entId, fileName);
1610     /* Get the Information from the oscp that the tapa task with the entity 
1611        id (entid) is registed for histogram or not */
1612       SGetHstGrmInfo(&entId, &hstReg);
1613
1614    /* call the memory manager to free this memory */
1615    ret = (osCp.regionTbl[region].free)(osCp.regionTbl[region].regCb, ptr, size,
1616                                                line, fileName, entId, hstReg);
1617 #else
1618    /* call the memory manager to free this memory */
1619 #ifdef SSI_STATIC_MEM_LEAK_DETECTION
1620 #ifdef T2K_MEM_LEAK_DBG
1621    ret = (osCp.regionTbl[region].free)(osCp.regionTbl[region].regCb, ptr - 4, size + 4, file, line);
1622 #else 
1623    ret = (osCp.regionTbl[region].free)(osCp.regionTbl[region].regCb, ptr - 4, size + 4);
1624 #endif
1625 #else
1626 #ifndef T2K_MEM_LEAK_DBG
1627    ret = (osCp.regionTbl[region].free)(osCp.regionTbl[region].regCb, ptr, size);
1628 #else
1629    ret = (osCp.regionTbl[region].free)(osCp.regionTbl[region].regCb, ptr, size, file, line);
1630 #endif
1631 #endif
1632 #endif /* SS_HISTOGRAM_SUPPORT */
1633 /* ss037.103 Removed the semaphore operation for performance enhancement */
1634
1635 #ifndef SS_PERF
1636    /* release the semaphore we took */
1637
1638 /* ss006.13: addition */
1639    if( SS_RELEASE_SEMA(&osCp.regionTblSem) != ROK)
1640    {
1641 #if (ERRCLASS & ERRCLS_DEBUG)
1642       SSLOGERROR(ERRCLS_DEBUG, ESS228, ERRZERO,
1643                   "Could not release semaphore");
1644       return RFAILED;
1645 #endif
1646    }
1647 #endif
1648 /* ss036.103 - addition to handle double free and trampling return values
1649 * This might invoke SRegMemErrHdlr 
1650 */
1651 #ifdef SSI_DEBUG_LEVEL1
1652     /* handle the double free error here by calling the OS specific error handling function */
1653     if ((ret == RDBLFREE) || (ret == RTRAMPLINGNOK))
1654     {
1655        SRegMemErrHdlr( region,  ptr,  ret);
1656     }
1657 #endif /* SSI_DEBUG_LEVEL1 */
1658
1659    return (ret);
1660 }
1661
1662
1663 /*
1664 *
1665 *       Fun:   SInitMsg
1666 *
1667 *       Desc:  This function deallocates a message back and then
1668 *              reinitializes the message.
1669 *
1670 *       Ret:   ROK      - ok
1671 *              RFAILED  - failed, general (optional)
1672 *
1673 *       Notes: all data attached to message is returned to memory.
1674 *              message is set to empty. message is not returned to
1675 *              memory. return is ok.
1676 *
1677 *       File:  ss_msg.c
1678 *
1679 */
1680
1681 #ifdef ANSI
1682 S16 SInitMsg
1683 (
1684 Buffer *mBuf
1685 )
1686 #else
1687 S16 SInitMsg(mBuf)
1688 Buffer *mBuf;
1689 #endif
1690 {
1691    SsMsgInfo *minfo;
1692    Buffer *tmp;
1693    uint8_t     tmpRegId;
1694
1695 #if (ERRCLASS & ERRCLS_INT_PAR)
1696    /* check message buffer */
1697    if (mBuf == NULLP)
1698    {
1699       SSLOGERROR(ERRCLS_INT_PAR, ESS062, ERRZERO, "SInitMsg: Null Buffer");
1700       return RFAILED;
1701    }
1702    if (mBuf->b_datap->db_type != SS_M_PROTO)
1703    {
1704       SSLOGERROR(ERRCLS_INT_PAR, ESS063, ERRZERO, "SInitMsg: Incorrect buffer\
1705                                                    type");
1706       return RFAILED;
1707    }
1708 #endif
1709
1710    /* get the message info */
1711 #ifdef YYYY
1712 #else
1713    CM_MEM_GET_REGION(tmpRegId);
1714    if(tmpRegId == 0xFF)
1715    {
1716       return RFAILED;
1717    }
1718 #endif
1719    minfo = (SsMsgInfo*) mBuf->b_rptr;
1720
1721    /* free all SS_M_DATA blks */
1722    while ((tmp = mBuf->b_cont))
1723    {
1724       mBuf->b_cont = tmp->b_cont;
1725 /* #ifdef YYYY */
1726 #if 1
1727 #ifdef T2K_MEM_LEAK_DBG
1728    char * file = __FILE__;
1729    uint32_t  line   = __LINE__;
1730 #endif
1731
1732       (Void) SPutDBuf(tmpRegId, minfo->pool, tmp);
1733 #else
1734       (Void) SPutDBuf(minfo->region, minfo->pool, tmp);
1735 #endif
1736    }
1737    /* initialise the length and endptr variables of mBuf */
1738
1739    minfo->len = 0;
1740    minfo->endptr = NULLP;
1741    minfo->next = NULLP;
1742
1743    return ROK;
1744 }
1745
1746 /* ss016.301 - Added a new API - SAddPreMsgMultInOrder. This preserves the
1747  * byte order while adding the data bytes to the beginning of the message.
1748  */
1749 /*
1750 *
1751 *       Fun:   SAddPreMsgMultInOrder
1752 *
1753 *       Desc:  This function copies consecutive bytes of data to the
1754 *              beginning of a message and keeps the bytes order preserved.
1755 *
1756 *       Ret:   ROK      - Appended the bytes to the beginning of the message.
1757 *              RFAILED  - Failed to append the bytes.
1758 *              ROUTRES  - Out of resources - Possibly insufficient memory.
1759 *
1760 *       Notes: If the message is empty,data is placed in the message. Message
1761 *              length is incremented. Return is ROK.
1762 *
1763 *              If the message is not empty,data is read by source pointer
1764 *              and appended at the beginning of the message.
1765 *              Message length is incremented. Return is ROK.
1766 *
1767 *       File:  ss_msg.c
1768 *
1769 */
1770 #ifdef ANSI
1771 S16 SAddPreMsgMultInOrder
1772 (
1773  Data *src,
1774  MsgLen cnt,
1775  Buffer *mBuf
1776  )
1777 #else
1778 S16 SAddPreMsgMultInOrder(src, cnt, mBuf)
1779     Data *src;
1780     MsgLen cnt;
1781     Buffer *mBuf;
1782 #endif
1783 {
1784    SsMsgInfo *minfo;   /* Message info */
1785    Buffer *tmp;
1786    Buffer *curblk=NULLP;
1787    Buffer *newblk;
1788    Buffer *prevblk;
1789    MsgLen numBytes;  /* no. of bytes to be copied */
1790    MsgLen len;
1791    MsgLen offset;
1792    Data *rptr;
1793    Data *revSrc;
1794
1795 #if (ERRCLASS & ERRCLS_INT_PAR)
1796       /* check message buffer */
1797    if (mBuf == NULLP)
1798    {
1799       SSLOGERROR(ERRCLS_INT_PAR, ESS070, ERRZERO, "SAddPreMsgMultInOrder:\
1800                                                    Null Buffer");
1801       return RFAILED;
1802    }
1803    /* check source */
1804    if (src == NULLP)
1805    {
1806       SSLOGERROR(ERRCLS_INT_PAR, ESS071, ERRZERO, "SAddPreMsgMultInOrder:\
1807                                                    Null Buffer");
1808       return RFAILED;
1809    }
1810    /* check count */
1811    if (cnt <= 0)
1812    {
1813       SSLOGERROR(ERRCLS_INT_PAR, ESS072, ERRZERO, "SAddPreMsgMultInOrder:\
1814                                                    Invalid count");
1815       return RFAILED;
1816    }
1817    if (mBuf->b_datap->db_type != SS_M_PROTO)
1818    {
1819       SSLOGERROR(ERRCLS_INT_PAR, ESS073, ERRZERO, "SAddPreMsgMultInOrder:\
1820                                                    Incorrect buffer type");
1821       return RFAILED;
1822    }
1823 #endif
1824
1825    /* get the SsMsgInfo of mBuf */
1826    minfo = (SsMsgInfo*) mBuf->b_rptr;
1827 #ifdef RGL_SPECIFIC_CHANGES
1828    minfo->region = 0;
1829 #endif
1830
1831    /* store cnt in length */
1832    len = cnt;
1833    offset = 0;
1834    revSrc = NULL;
1835
1836    /* point to the end of the source buffer */
1837    revSrc = src + cnt ;
1838
1839    if ((tmp = mBuf->b_cont) && (tmp->b_datap->db_ref == 1) &&
1840          (tmp->b_datap->db_base < tmp->b_rptr))
1841    {
1842       /* store the offset of the read pointer of tmp */
1843       offset = tmp->b_rptr - tmp->b_datap->db_base;
1844
1845       /* determine the number of bytes to copy */
1846       numBytes = MIN(cnt, offset);
1847
1848       /* traverse back from the end of the source buffer*/
1849       revSrc -= numBytes;
1850
1851       /* derement cnt */
1852       cnt -= numBytes;
1853
1854       /* move the read pointer towards the left */
1855       tmp->b_rptr -= numBytes ;
1856       SMemCpy((Void *)(tmp->b_rptr ), (Void *) revSrc, (size_t) numBytes);
1857
1858       if (!cnt)
1859       {
1860          minfo->len += len;
1861          return ROK;
1862       }
1863    }
1864    newblk = prevblk = NULLP;
1865    while (cnt)
1866    {
1867       /* allocate a message blk */
1868       if (SGetDBuf(minfo->region, minfo->pool, &curblk) != ROK)
1869       {
1870          while ((curblk = prevblk))
1871          {
1872             prevblk = prevblk->b_cont;
1873 #ifdef T2K_MEM_LEAK_DBG
1874    char * file = __FILE__;
1875    uint32_t  line   = __LINE__;
1876 #endif
1877
1878             (Void) SPutDBuf(minfo->region, minfo->pool, curblk);
1879          }
1880
1881          if (tmp)
1882          {
1883             tmp->b_rptr = tmp->b_datap->db_base + offset;
1884          }
1885
1886          return (ROUTRES);
1887       }
1888       /* attach curblk in the newblk chain */
1889       if (prevblk)
1890       { 
1891          curblk->b_cont = prevblk; /* stack them up */
1892       }
1893       else
1894       {
1895          newblk = curblk;
1896       }
1897       prevblk = curblk;
1898
1899       /* set the read and write pointers to the end of the data buffer */
1900       /* subsequent prepends have all the buffer to insert data into */
1901       curblk->b_wptr = curblk->b_datap->db_lim;
1902       rptr = curblk->b_datap->db_lim;
1903
1904       /* copy data */
1905       numBytes = MIN(cnt, (rptr - curblk->b_datap->db_base));
1906
1907       /* decrement cnt */
1908       cnt -= numBytes;
1909
1910       /* move the read pointer towards the left */
1911       rptr -= numBytes;
1912
1913       /* traverse back the source buffer */
1914       revSrc -= numBytes;
1915
1916       SMemCpy((Void *)(rptr), (Void *) revSrc, (size_t) numBytes);
1917
1918       curblk->b_rptr = rptr;
1919    }
1920
1921    /* attach the newblk chain into mBuf */
1922    newblk->b_cont = tmp;
1923    mBuf->b_cont = curblk;
1924
1925    /* update endptr of mBuf */
1926    if (!tmp)
1927    {
1928       minfo->endptr = newblk;
1929    }
1930    /* update length of message */
1931    minfo->len += len;
1932
1933    return ROK;
1934 }
1935 /*
1936 *
1937 *       Fun:   SAddPreMsg
1938 *
1939 *       Desc:  This function copies one byte of data to the
1940 *              beginning of a message.
1941 *
1942 *       Ret:   ROK      - ok
1943 *              RFAILED  - failed, general (optional)
1944 *              ROUTRES  - failed, out of resources (optional)
1945 *
1946 *       Notes: if message is empty: data is placed in the message. message
1947 *              length is incremented. return is ok.
1948 *
1949 *              if message is not empty: data is placed in front of all
1950 *              other data in message. message length is incremented.
1951 *              return is ok.
1952 *
1953 *       File:  ss_msg.c
1954 *
1955 */
1956
1957  
1958 #ifdef ANSI
1959 S16 SAddPreMsg
1960 (
1961 Data data,
1962 Buffer *mBuf
1963 )
1964 #else
1965 S16 SAddPreMsg (data, mBuf)
1966 Data data;
1967 Buffer *mBuf;
1968 #endif
1969 {
1970    SsMsgInfo *minfo;
1971    Buffer *tmp;
1972    Buffer *newb;
1973
1974 #if (ERRCLASS & ERRCLS_INT_PAR)
1975    /* check message buffer */
1976    if (!mBuf)
1977    {
1978       SSLOGERROR(ERRCLS_INT_PAR, ESS064, ERRZERO, "SAddPreMsg: Null Buffer");
1979       return RFAILED;
1980    }
1981    if (mBuf->b_datap->db_type != SS_M_PROTO)
1982    {
1983       SSLOGERROR(ERRCLS_INT_PAR, ESS065, ERRZERO, "SAddPreMsg: Incorrect\
1984                                                    buffer type");
1985       return RFAILED; 
1986    }
1987 #endif
1988
1989    minfo = (SsMsgInfo *) mBuf->b_rptr;
1990
1991    /* ss021.103 - Addition to check if exceeding maximum message length */
1992 #ifdef LONG_MSG
1993    if (minfo->len == 0x7FFFFFFF)
1994 #else
1995    if (minfo->len == 0x7FFF)
1996 #endif
1997    {
1998       return (ROUTRES);
1999    }
2000       
2001    /*
2002     * allocate a message blk using SGetDBuf(), if there are no data blks in the
2003     * message, mBuf, or if the reference count of the first data blk is greater
2004     * than 1, or if there is no space to append databytes in front of the read
2005     * pointer of the first data blk
2006    */
2007    if (!(tmp = mBuf->b_cont) || (tmp->b_datap->db_ref > 1) ||
2008         (tmp->b_rptr == tmp->b_datap->db_base))
2009    {
2010       if (SGetDBuf(minfo->region, minfo->pool, &newb) != ROK)
2011       {
2012          SSLOGERROR(ERRCLS_DEBUG, ESS066, ERRZERO, "SAddPreMsg:Failed in\
2013                     SGetDBuf");
2014          return (ROUTRES);
2015       }
2016       /* set the read and write pointers to end of data buffer */
2017       /* subsequent prepends have all the buffer to insert data into */
2018       newb->b_wptr = newb->b_datap->db_lim;
2019       newb->b_rptr = newb->b_datap->db_lim;
2020
2021       /* insert newb before tmp */
2022       newb -> b_cont = tmp;
2023       mBuf->b_cont = newb;
2024
2025       /* if endptr of mBuf is NULLP, set it to newb */
2026       if (tmp == NULLP)
2027          minfo->endptr = newb;
2028       tmp = newb;
2029    }
2030    /* insert data, increment length */
2031    *--tmp->b_rptr = data;
2032    minfo->len++;
2033
2034    return ROK;
2035 }
2036
2037 /*
2038 *
2039 *       Fun:   SAddPstMsg
2040 *
2041 *       Desc:  This function copies one byte of data to the
2042 *              end of a message.
2043 *
2044 *       Ret:   ROK      - ok
2045 *              RFAILED  - failed, general (optional)
2046 *              ROUTRES  - failed, out of resources (optional)
2047 *
2048 *       Notes: if message is empty: data is placed in the message. message
2049 *              length is incremented. return is ok.
2050 *
2051 *              if message is not empty: data is placed in back of all
2052 *              other data in message. message length is incremented.
2053 *              return is ok.
2054 *
2055 *       File:  ss_msg.c
2056 *
2057 */
2058
2059  
2060 #ifdef ANSI
2061 S16 SAddPstMsg
2062 (
2063 Data data,
2064 Buffer *mBuf
2065 )
2066 #else
2067 S16 SAddPstMsg (data, mBuf)
2068 Data data;
2069 Buffer *mBuf;
2070 #endif
2071 {
2072    SsMsgInfo *minfo;
2073    Buffer *tmp;
2074    Buffer *newb;
2075
2076 #if (ERRCLASS & ERRCLS_INT_PAR)
2077    /* check message buffer */
2078    if (!mBuf)
2079    {
2080       SSLOGERROR(ERRCLS_INT_PAR, ESS067, ERRZERO, "SAddPstMsg: Null Buffer");
2081       return RFAILED;
2082    }
2083    if (mBuf->b_datap->db_type != SS_M_PROTO)
2084    {
2085       SSLOGERROR(ERRCLS_INT_PAR, ESS068, ERRZERO, "SAddPstMsg: Incorrect\
2086                                                    buffer type");
2087       return RFAILED;
2088    }
2089 #endif
2090
2091    /* get the message info */
2092    minfo = (SsMsgInfo *) mBuf->b_rptr;
2093  
2094    if (!(tmp = minfo->endptr) || (tmp->b_datap->db_ref > 1) ||
2095         (tmp->b_wptr == tmp->b_datap->db_lim))
2096    {
2097       if (SGetDBuf(minfo->region, minfo->pool, &newb) != ROK)
2098       {
2099          SSLOGERROR(ERRCLS_DEBUG, ESS069, ERRZERO, "SAddPstMsg: Failed in\
2100                                                     SGetDBuf()");
2101          return (ROUTRES);
2102       }
2103
2104       /* append newb to the end of the mBuf chain */
2105       if (tmp)
2106          tmp->b_cont = newb;
2107       else
2108          mBuf->b_cont = newb;
2109
2110       /* set the endptr of mBuf to newb */
2111       minfo->endptr = newb;
2112
2113       tmp = newb;
2114    }
2115    /* insert data, increment length */
2116    *tmp->b_wptr++ = data;
2117    minfo->len++;
2118
2119    return ROK;
2120 }
2121
2122 /*
2123 *
2124 *       Fun:   SAddPreMsgMult
2125 *
2126 *       Desc:  This function copies consecutive bytes of data to the
2127 *              beginning of a message.
2128 *
2129 *       Ret:   ROK      - ok
2130 *              RFAILED  - failed, general (optional)
2131 *              ROUTRES  - failed, out of resources (optional)
2132 *
2133 *       Notes: if message is empty: data is placed in the message. message
2134 *              length is incremented. return is ok.
2135 *
2136 *              if message is not empty: data is read by source pointer,
2137 *              data is placed in front of all other data in message.
2138 *              message length is incremented. return is ok.
2139 *
2140 *              the first byte of data pointed to by the source pointer will
2141 *              be placed at the front of the message first, the last byte of
2142 *              data pointed to by the source pointer will be placed at the
2143 *              front of the message last (i.e. it will become the first
2144 *              byte of the message).
2145 *
2146 *       File:  ss_msg.c
2147 *
2148 */
2149  
2150 #ifdef T2K_MEM_LEAK_DBG
2151 S16 SAddPreMsgMult1
2152 (
2153 Data *src,
2154 MsgLen cnt,
2155 Buffer *mBuf,
2156 char   *file,
2157 uint32_t    line
2158 )
2159
2160 #else
2161 #ifdef ANSI
2162 S16 SAddPreMsgMult
2163 (
2164 Data *src,
2165 MsgLen cnt,
2166 Buffer *mBuf
2167 )
2168 #else
2169 S16 SAddPreMsgMult(src, cnt, mBuf)
2170 Data *src;
2171 MsgLen cnt;
2172 Buffer *mBuf;
2173 #endif
2174 #endif
2175 {
2176    SsMsgInfo *minfo;   /* Message info */
2177    Buffer *tmp;
2178    Buffer *curblk=NULLP;
2179    Buffer *newblk;
2180    Buffer *prevblk;
2181    MsgLen numBytes;  /* no. of bytes to be copied */
2182    MsgLen len;
2183    MsgLen offset;
2184    Data *rptr;
2185
2186 #if (ERRCLASS & ERRCLS_INT_PAR)
2187    /* check message buffer */
2188    if (mBuf == NULLP)
2189    {
2190       SSLOGERROR(ERRCLS_INT_PAR, ESS070, ERRZERO, "SAddPreMsgMult:Null Buffer");
2191       return RFAILED;
2192    }
2193    /* check source */
2194    if (src == NULLP)
2195    {
2196       SSLOGERROR(ERRCLS_INT_PAR, ESS071, ERRZERO, "SAddPreMsgMult:Null Buffer");
2197       return RFAILED;
2198    }
2199    /* check count */
2200    if (cnt <= 0)
2201    {
2202       SSLOGERROR(ERRCLS_INT_PAR, ESS072, ERRZERO, "SAddPreMsgMult: Invalid\
2203                                                    count");
2204       return RFAILED;
2205    }
2206    if (mBuf->b_datap->db_type != SS_M_PROTO)
2207    {
2208       SSLOGERROR(ERRCLS_INT_PAR, ESS073, ERRZERO, "SAddPreMsgMult: Incorrect\
2209                                                    buffer type");
2210       return RFAILED;
2211    }
2212 #endif
2213
2214    /* get the SsMsgInfo of mBuf */
2215    minfo = (SsMsgInfo*) mBuf->b_rptr;
2216
2217    /* store cnt in length */
2218    len = cnt;
2219 /* ss014.13: Addition */
2220    offset = 0;
2221
2222    if ((tmp = mBuf->b_cont) && (tmp->b_datap->db_ref == 1) &&
2223        (tmp->b_datap->db_base < tmp->b_rptr))
2224    {
2225       /* store the offset of the read pointer of tmp */
2226       offset = tmp->b_rptr - tmp->b_datap->db_base;
2227
2228       /* determine the number of bytes to copy */
2229       numBytes = MIN(cnt, offset);
2230
2231       /* decrement cnt */
2232       cnt -= numBytes;
2233
2234       /* copy data */
2235       while (numBytes--)
2236          *--tmp->b_rptr = *src++;
2237       if (!cnt)
2238       {
2239          minfo->len += len;
2240          return ROK;
2241       }
2242    }
2243    newblk = prevblk = NULLP;
2244    while (cnt)
2245    {
2246       /* allocate a message blk */
2247       if (SGetDBuf(minfo->region, minfo->pool, &curblk) != ROK)
2248       {
2249          while ((curblk = prevblk))
2250          {
2251             prevblk = prevblk->b_cont;
2252             (Void) SPutDBuf(minfo->region, minfo->pool, curblk);
2253          }
2254
2255          if (tmp)
2256          {
2257             tmp->b_rptr = tmp->b_datap->db_base + offset;
2258          }
2259
2260          return (ROUTRES);
2261       }
2262       /* attach curblk in the newblk chain */
2263       if (prevblk)
2264          curblk->b_cont = prevblk; /* stack them up */
2265       else
2266          newblk = curblk;
2267       prevblk = curblk;
2268
2269       /* set the read and write pointers to the end of the data buffer */
2270       /* subsequent prepends have all the buffer to insert data into */
2271       curblk->b_wptr = curblk->b_datap->db_lim;
2272       rptr = curblk->b_datap->db_lim;
2273
2274       /* copy data */
2275       numBytes = MIN(cnt, (rptr - curblk->b_datap->db_base));
2276
2277       /* decrement cnt */
2278       cnt -= numBytes;
2279
2280       while (numBytes--)
2281          *--rptr = *src++;
2282       curblk->b_rptr = rptr;
2283    }
2284
2285    /* attach the newblk chain into mBuf */
2286    newblk -> b_cont = tmp;
2287    mBuf -> b_cont = curblk;
2288
2289    /* update endptr of mBuf */
2290    if (!tmp)
2291       minfo -> endptr = newblk;
2292
2293    /* update length of message */
2294    minfo->len += len;
2295
2296    return ROK;
2297 }
2298
2299 /*
2300 *
2301 *       Fun:   SAddPstMsgMult
2302 *
2303 *       Desc:  This function copies consecutive bytes of data to the
2304 *              end of a message.
2305 *
2306 *       Ret:   ROK      - ok
2307 *              RFAILED  - failed, general (optional)
2308 *              ROUTRES  - failed, out of resources (optional)
2309 *
2310 *       Notes: if message is empty: data is placed in the message. message
2311 *              length is incremented. return is ok.
2312 *
2313 *              if message is not empty: data is read by source pointer,
2314 *              data is placed in back of all other data in message.
2315 *              message length is incremented. return is ok.
2316 *
2317 *              the first byte of data pointed to by the source pointer will
2318 *              be placed at the back of the message first, the last byte of
2319 *              data pointed to by the source pointer will be placed at the
2320 *              back of the message last (i.e. it will become the last
2321 *              byte of the message).
2322 *
2323 *       File:  ss_msg.c
2324 *
2325 */
2326
2327 #ifdef T2K_MEM_LEAK_DBG
2328 S16 SAddPstMsgMult1
2329 (
2330 Data *src,
2331 MsgLen cnt,
2332 Buffer *mBuf,
2333 char   *file,
2334 uint32_t    line
2335 )
2336 #else
2337 #ifdef ANSI
2338 S16 SAddPstMsgMult
2339 (
2340 Data *src,
2341 MsgLen cnt,
2342 Buffer *mBuf
2343 )
2344 #else
2345 S16 SAddPstMsgMult(src, cnt, mBuf)
2346 Data *src;
2347 MsgLen cnt;
2348 Buffer *mBuf;
2349 #endif
2350 #endif
2351 {
2352    SsMsgInfo *minfo;
2353    Buffer *tmp;
2354    Buffer *newblk;
2355    Buffer *curblk=NULLP;
2356    Buffer *prevblk;
2357    MsgLen len;
2358    MsgLen offset;
2359    MsgLen numBytes;
2360    Data *wptr;
2361
2362 #if (ERRCLASS & ERRCLS_INT_PAR)
2363    /* check message buffer */
2364    if (mBuf == NULLP)
2365    {
2366       SSLOGERROR(ERRCLS_INT_PAR, ESS074, ERRZERO, "SAddPstMsgMult:Null Buffer");
2367       return RFAILED;
2368    }
2369    /* check source */
2370    if (src == NULLP)
2371    {
2372       SSLOGERROR(ERRCLS_INT_PAR, ESS075, ERRZERO, "SAddPstMsgMult:Null Buffer");
2373       return RFAILED;
2374    }
2375    /* check count */
2376    if (cnt <= 0)
2377    {
2378       SSLOGERROR(ERRCLS_INT_PAR, ESS076, ERRZERO, "SAddPstMsgMult:Invalid\
2379                                                    count");
2380       return RFAILED;
2381    }
2382    if (mBuf->b_datap->db_type != SS_M_PROTO)
2383    {
2384       SSLOGERROR(ERRCLS_INT_PAR, ESS077, ERRZERO, "SAddPstMsgMult: Incorrect\
2385                                                    buffer type");
2386       return RFAILED;
2387    }
2388 #endif
2389  
2390    /* get the SsMsgInfo of mBuf */
2391    minfo = (SsMsgInfo*) mBuf->b_rptr;
2392
2393    /* store cnt in len */
2394    len = cnt;
2395
2396 /* ss014.13: Addition */
2397    offset = 0;
2398
2399    if ((tmp = minfo->endptr) && (tmp->b_datap->db_ref == 1) &&
2400        (tmp->b_datap->db_lim > tmp->b_wptr))
2401    {
2402       /* store offset of the write pointer */
2403       /* incase subsequent allocations fail, offset is read reset to original */
2404       offset = tmp->b_datap->db_lim - tmp->b_wptr;
2405
2406       /* determine the number of bytes to copy */
2407       numBytes = MIN(cnt, offset);
2408
2409       /* decrement cnt */
2410       cnt -= numBytes;
2411
2412
2413 /* ss002.13: addition */
2414
2415   /* ss004.13: addition */
2416       SMemCpy((Void *) tmp->b_wptr, (Void *) src, (size_t) numBytes);
2417
2418       tmp->b_wptr += numBytes;
2419       src += numBytes;
2420
2421       if (!cnt)
2422       {
2423          minfo->len += len;
2424          return ROK;
2425       }
2426    }
2427
2428    newblk = prevblk = NULLP;
2429
2430    while (cnt)
2431    {
2432       /* allocate a message blk */
2433       if (SGetDBuf(minfo->region, minfo->pool, &curblk) != ROK)
2434       {
2435          while ((curblk = newblk))
2436          {
2437             newblk = newblk->b_cont;
2438             (Void) SPutDBuf(minfo->region, minfo->pool, curblk);
2439          }
2440
2441          if (tmp)
2442          {
2443             tmp->b_wptr = tmp->b_datap->db_lim - offset;
2444          }
2445
2446          return (ROUTRES);
2447       }
2448
2449       /* insert curblk in the newblk chain */
2450       if (prevblk)
2451          prevblk->b_cont = curblk; /* stack them down */
2452       else
2453          newblk = curblk;
2454       prevblk = curblk;
2455
2456       /* copy data */
2457       wptr = curblk->b_wptr;
2458       numBytes = MIN(cnt, curblk->b_datap->db_lim - wptr);
2459
2460       /* decrement cnt */
2461       cnt -= numBytes;
2462
2463
2464 /* ss002.13: addition */
2465 /* ss003.13: addition */
2466       SMemCpy( (Void *) wptr, (Void *) src, (size_t) numBytes);
2467
2468       src = src + numBytes;
2469
2470       curblk->b_wptr +=  numBytes;  
2471  }
2472    /* insert newblk chain into mBuf */
2473    if (tmp)
2474       tmp->b_cont = newblk;
2475    else
2476       mBuf->b_cont = newblk;
2477    minfo->endptr = curblk;
2478
2479    /* update length */
2480    minfo->len += len;
2481
2482    return ROK;
2483 }
2484
2485 /* #ifdef SS_LOCKLESS_MEMORY */
2486
2487
2488
2489 /*
2490 *
2491 *       Fun:   SRemPreMsg
2492 *
2493 *       Desc:  This function copies and then removes one byte of
2494 *              data from the beginning of a message.
2495 *
2496 *       Ret:   ROK      - ok
2497 *              ROKDNA   - ok, data not available
2498 *              RFAILED  - failed, general (optional)
2499 *
2500 *       Notes: if message is empty: message is unchanged. return is ok,
2501 *              data not available.
2502 *
2503 *              if message is not empty: data is removed from front of
2504 *              message, data is returned via pointer to data. message
2505 *              length is decremented. return is ok.
2506 *
2507 *       File:  ss_msg.c
2508 *
2509 */
2510
2511  
2512 #ifdef ANSI
2513 S16 SRemPreMsg
2514 (
2515 Data *dataPtr,
2516 Buffer *mBuf
2517 )
2518 #else
2519 S16 SRemPreMsg(dataPtr, mBuf)
2520 Data *dataPtr;
2521 Buffer *mBuf;
2522 #endif
2523 {
2524    SsMsgInfo *minfo;
2525    Buffer *tmp;
2526 #ifdef T2K_MEM_LEAK_DBG
2527    char* file = __FILE__;
2528    uint32_t line = __LINE__;
2529 #endif
2530
2531 #if (ERRCLASS & ERRCLS_INT_PAR)
2532    /* check data pointer */
2533    if (!dataPtr)
2534    {
2535       SSLOGERROR(ERRCLS_INT_PAR, ESS078, ERRZERO, "SRemPreMsg : Null Buffer");
2536       return RFAILED;
2537    }
2538    /* check message buffer */
2539    if (!mBuf)
2540    {
2541       SSLOGERROR(ERRCLS_INT_PAR, ESS079, ERRZERO, "SRemPreMsg : Null Buffer");
2542       return RFAILED;
2543    }
2544    if (mBuf->b_datap->db_type != SS_M_PROTO)
2545    {
2546       SSLOGERROR(ERRCLS_INT_PAR, ESS080, ERRZERO, "SRemPreMsg : Incorrect\
2547                                                    buffer type");
2548       return RFAILED;
2549    }
2550 #endif
2551
2552    if (!(tmp = mBuf->b_cont))
2553       return (ROKDNA);
2554
2555    /* get SsMsgInfo of mBuf */
2556    minfo = (SsMsgInfo *) mBuf->b_rptr;
2557
2558    /* read databyte into dataPtr and incrment read ptr */
2559    *dataPtr = *tmp->b_rptr++;
2560
2561    /* if all data is exhausted, release the blk */
2562    if (tmp->b_rptr == tmp->b_wptr)
2563    {
2564       mBuf->b_cont = tmp->b_cont;
2565       (Void) SPutDBuf(minfo->region, minfo->pool, tmp);
2566    }
2567    /* update SsMsgInfo */
2568    if (!--minfo->len)
2569       minfo->endptr = NULLP;
2570
2571    return ROK;
2572 }
2573
2574 /*
2575 *
2576 *       Fun:   SRemPstMsg
2577 *
2578 *       Desc:  This function copies and then removes one byte of
2579 *              data from the end of a message.
2580 *
2581 *       Ret:   ROK      - ok
2582 *              ROKDNA   - ok, data not available
2583 *              RFAILED  - failed, general (optional)
2584 *
2585 *       Notes: if message is empty: message is unchanged. return is ok,
2586 *              data not available.
2587 *
2588 *              if message is not empty: data is removed from back of
2589 *              message, data is returned via pointer to data. message
2590 *              length is decremented. return is ok.
2591 *
2592 *       File:  ss_msg.c
2593 *
2594 */
2595
2596
2597 #ifdef ANSI
2598 S16 SRemPstMsg
2599 (
2600 Data *dataPtr,              /* pointer to data */
2601 Buffer *mBuf
2602 )
2603 #else
2604 S16 SRemPstMsg(dataPtr, mBuf)
2605 Data *dataPtr;              /* pointer to data */
2606 Buffer *mBuf;               /* message buffer */
2607 #endif
2608 {
2609    SsMsgInfo *minfo;
2610    Buffer *tmp;
2611    Buffer *last;
2612 #ifdef T2K_MEM_LEAK_DBG
2613    char* file = __FILE__;
2614    uint32_t line = __LINE__;
2615 #endif
2616
2617 #if (ERRCLASS & ERRCLS_INT_PAR)
2618    /* check data pointer */
2619    if (dataPtr == NULLP)
2620    {
2621       SSLOGERROR(ERRCLS_INT_PAR, ESS081, ERRZERO, "SRemPstMsg : Null Buffer");
2622       return RFAILED;
2623    }
2624    /* check message buffer */
2625    if (mBuf == NULLP)
2626    {
2627       SSLOGERROR(ERRCLS_INT_PAR, ESS082, ERRZERO, "SRemPstMsg : Null Buffer");
2628       return RFAILED;
2629    }
2630    if (mBuf->b_datap->db_type != SS_M_PROTO)
2631    {
2632       SSLOGERROR(ERRCLS_INT_PAR, ESS083, ERRZERO, "SRemPstMsg : Incorrect\
2633                                                    buffer type");
2634       return RFAILED;
2635    }
2636 #endif
2637
2638    /* get the SsMsgInfo */
2639    minfo = (SsMsgInfo*) mBuf->b_rptr;
2640
2641    if (!(last = minfo->endptr))
2642       return (ROKDNA);
2643
2644    /* read databyte into dataPtr and decrement write ptr */
2645    *dataPtr = *--last->b_wptr;
2646
2647    /* if all data is exhausted, release the blk */
2648    if (last->b_rptr == last->b_wptr)
2649    {
2650       for (tmp = mBuf; tmp->b_cont != last;)
2651          tmp = tmp->b_cont;
2652       tmp->b_cont = NULLP;
2653       (Void) SPutDBuf(minfo->region, minfo->pool, last);
2654
2655       /* update endptr */
2656       if (mBuf->b_cont)
2657          minfo->endptr = tmp;
2658       else
2659          minfo->endptr = NULLP;
2660    }
2661    /* update SsMsgInfo */
2662    minfo->len--;
2663
2664    return ROK;
2665 }
2666
2667
2668 /*
2669 *
2670 *       Fun:   SRemPreMsgMult
2671 *
2672 *       Desc:  This function copies and then removes consecutive bytes of
2673 *              data from the beginning of a message.
2674 *
2675 *       Ret:   ROK      - ok
2676 *              ROKDNA   - ok, data not available
2677 *              RFAILED  - failed, general (optional)
2678 *
2679 *       Notes: if message is empty: message is unchanged. return is ok,
2680 *              data not available.
2681 *
2682 *              if the destination buffer is NULL, data is not copied.
2683 *
2684 *              if message is not empty: data is removed from front of
2685 *              message, data is returned by destination pointer. message
2686 *              length is decremented. return is ok.
2687 *
2688 *              the first byte of data read from the message will be placed
2689 *              in the destination buffer first (i.e. this was the first byte
2690 *              of the message), the last byte of data read from the message
2691 *              will be placed in the destination buffer last.
2692 *
2693 *       File:  ss_msg.c
2694 *
2695 */
2696
2697 #ifdef ANSI
2698 S16 SRemPreMsgMult
2699 (
2700 Data *dst,                  /* destination */
2701 MsgLen cnt,                 /* count */
2702 Buffer *mBuf
2703 )
2704 #else
2705 S16 SRemPreMsgMult(dst, cnt, mBuf)
2706 Data *dst;                  /* destination */
2707 MsgLen cnt;                 /* count */
2708 Buffer *mBuf;               /* message buffer */
2709 #endif
2710 {
2711    SsMsgInfo *minfo;
2712    Buffer *tmp;
2713    MsgLen numBytes;
2714 #ifdef T2K_MEM_LEAK_DBG
2715    char* file = __FILE__;
2716    uint32_t line = __LINE__;
2717 #endif
2718
2719
2720    /* ss023.103 - Modification of SRemPreMsgMult for bug fix */
2721
2722 #if (ERRCLASS & ERRCLS_INT_PAR)
2723    /* check count */
2724    if (cnt <= 0)
2725    {
2726       SSLOGERROR(ERRCLS_INT_PAR, ESS084, ERRZERO, "SRemPreMsgMult:Invalid\
2727                                                    count");
2728       return RFAILED;
2729    }
2730    /* check message buffer */
2731    if (!mBuf)
2732    {
2733       SSLOGERROR(ERRCLS_INT_PAR, ESS085, ERRZERO, "SRemPreMsgMult:Null Buffer");
2734       return RFAILED;
2735    }
2736    /* ss021.103 - Addition to check for NULL pointer */
2737    /* check data pointer */
2738    /* ss022.103 - Removed check for NULL pointer */
2739    if (mBuf->b_datap->db_type != SS_M_PROTO)
2740    {
2741       SSLOGERROR(ERRCLS_INT_PAR, ESS086, ERRZERO, "SRemPreMsgMult : Incorrect\
2742                                                    buffer type");
2743       return RFAILED;
2744    }
2745 #endif
2746
2747    /* get the SsMsgInfo */
2748    minfo = (SsMsgInfo*) mBuf->b_rptr;
2749
2750    /* check if data present */
2751    if (minfo->len < cnt)
2752       return (ROKDNA);
2753    else
2754       minfo->len -= cnt;
2755
2756    while (cnt)
2757    {
2758       /* get the first SS_M_DATA blk */
2759       tmp = mBuf->b_cont;
2760
2761       /* determine the number of bytes to be copy */
2762       numBytes = MIN(cnt, tmp->b_wptr - tmp->b_rptr);
2763
2764       /* decrement cnt */
2765       cnt -= numBytes;
2766
2767       /* move data into dst */
2768       if (dst != NULLP)
2769       {
2770
2771         SMemCpy( (Void *) dst, (Void *) tmp->b_rptr,  (size_t) numBytes);
2772         dst += numBytes;
2773       }
2774       
2775       tmp->b_rptr += numBytes;
2776
2777       if (tmp->b_rptr == tmp->b_wptr)
2778       {
2779          mBuf->b_cont = tmp->b_cont;
2780         (Void) SPutDBuf(minfo->region, minfo->pool, tmp);
2781       }
2782    }
2783    /* update SsMsgInfo */
2784    if (!minfo->len)
2785       minfo->endptr = NULLP;
2786
2787    return ROK;
2788 }
2789
2790 /*
2791 *
2792 *       Fun:   SRemPstMsgMult
2793 *
2794 *       Desc:  This function copies and then removes consecutive bytes of
2795 *              data from the end of a message.
2796 *
2797 *       Ret:   ROK      - ok
2798 *              ROKDNA   - ok, data not available
2799 *              RFAILED  - failed, general (optional)
2800 *
2801 *       Notes: if message is empty: message is unchanged. return is ok,
2802 *              data not available.
2803 *
2804 *              if the destination buffer is NULL, data is not copied.
2805 *
2806 *              if message is not empty: data is removed from front of
2807 *              message, data is returned by destination pointer. message
2808 *              length is decremented. return is ok.
2809 *
2810 *              the first byte of data read from the message will be placed
2811 *              in the destination buffer first (i.e. this was the last byte
2812 *              of the message), the last byte of data read from the message
2813 *              will be placed in the destination buffer last.
2814 *
2815 *       File:  ss_msg.c
2816 *
2817 */
2818
2819 #ifdef ANSI
2820 S16 SRemPstMsgMult
2821 (
2822 Data *dst,                  /* destination */
2823 MsgLen cnt,                 /* count */
2824 Buffer *mBuf
2825 )
2826 #else
2827 S16 SRemPstMsgMult(dst, cnt, mBuf)
2828 Data *dst;                  /* destination */
2829 MsgLen cnt;                 /* count */
2830 Buffer *mBuf;               /* message buffer */
2831 #endif
2832 {
2833    SsMsgInfo *minfo;
2834    Buffer *tmp;
2835    Buffer *prev;
2836    MsgLen count;
2837    MsgLen numBytes;
2838    Data *cptr;
2839 #ifdef T2K_MEM_LEAK_DBG
2840    char* file = __FILE__;
2841    uint32_t line = __LINE__;
2842 #endif
2843
2844
2845 #if (ERRCLASS & ERRCLS_INT_PAR)
2846    /* check count */
2847    if (cnt <= 0)
2848    {
2849       SSLOGERROR(ERRCLS_INT_PAR, ESS087, ERRZERO, "SRemPstMsgMult:Invalid\
2850                                                    count");
2851       return RFAILED;
2852    }
2853    /* check message buffer */
2854    if (mBuf == NULLP)
2855    {
2856       SSLOGERROR(ERRCLS_INT_PAR, ESS088, ERRZERO, "SRemPstMsgMult:Null Buffer");
2857       return RFAILED;
2858    }
2859    if (mBuf->b_datap->db_type != SS_M_PROTO)
2860    {
2861       SSLOGERROR(ERRCLS_INT_PAR, ESS089, ERRZERO, "SRemPstMsgMult : Incorrect\
2862                                                    buffer type");
2863       return RFAILED;
2864    }
2865    /* ss021.103 - Addition to check dst data pointer */
2866    /* check data pointer */
2867    /* ss022.103 - Removed check for NULL destination pointer */
2868 #endif
2869  
2870    /* get the SsMsgInfo */
2871    minfo = (SsMsgInfo*) mBuf->b_rptr;
2872  
2873    /* check if data present */
2874    if (minfo->len < cnt)
2875       return (ROKDNA);
2876    else
2877    {
2878       minfo->len -= cnt;
2879       count = minfo->len;
2880       prev = mBuf;
2881       tmp = mBuf->b_cont;
2882    }
2883
2884    /* determine blk containing offset, and prev node */
2885    FIND_OFFSET_AND_PREV(prev, tmp, count)
2886
2887    if (dst != NULLP)
2888       dst += cnt;
2889
2890    while (cnt)
2891    {
2892       numBytes = MIN(cnt, (tmp->b_wptr - tmp->b_rptr - count));
2893
2894       tmp->b_wptr -= numBytes;
2895
2896       cnt -= numBytes;
2897
2898       /* copy data */
2899       cptr = tmp->b_wptr;
2900       if (dst != NULLP)
2901       {
2902          while (numBytes--)
2903             *--dst = *cptr++;
2904       }
2905
2906       if (tmp->b_rptr == tmp->b_wptr)
2907       {
2908          prev->b_cont = tmp->b_cont;
2909          (Void) SPutDBuf(minfo->region, minfo->pool, tmp);
2910          tmp = prev;
2911       }
2912       prev = tmp;
2913       tmp = tmp->b_cont;
2914
2915       count = 0;
2916    }
2917    if (mBuf == prev)
2918       minfo->endptr = NULLP;
2919    else
2920       minfo->endptr = prev;
2921
2922    return ROK;
2923 }
2924
2925 /*
2926 *
2927 *       Fun:   SExamMsg
2928 *
2929 *       Desc:  This function copies one byte of data from a message
2930 *              without modifying the message.
2931 *
2932 *       Ret:   ROK      - ok
2933 *              ROKDNA   - ok, data not available
2934 *              RFAILED  - failed, general (optional)
2935 *
2936 *       Notes: index is 0 based and indicates location in message
2937 *
2938 *              if index is less than the length of the message:
2939 *              message is unchanged and data is examined at specified
2940 *              index and returned via pointer to data. message length
2941 *              is unchanged. return is ok.
2942 *
2943 *              if index is greater than or equal to
2944 *              the length of the message: message is unchanged and 0
2945 *              is returned via pointer to data. return is ok, data
2946 *              not available.
2947 *
2948 *       File:  ss_msg.c
2949 *
2950 */
2951
2952
2953 #ifdef ANSI
2954 S16 SExamMsg
2955 (
2956 Data *dataPtr,              /* pointer to data */
2957 Buffer *mBuf,               /* message buffer */
2958 MsgLen idx
2959 )
2960 #else
2961 S16 SExamMsg(dataPtr, mBuf, idx)
2962 Data *dataPtr;              /* pointer to data */
2963 Buffer *mBuf;               /* message buffer */
2964 MsgLen idx;                 /* index */
2965 #endif
2966 {
2967    SsMsgInfo *minfo;
2968    Buffer *tmp;
2969
2970 #if (ERRCLASS & ERRCLS_INT_PAR)
2971    /* check data pointer */
2972    if (!dataPtr)
2973    {
2974       SSLOGERROR(ERRCLS_INT_PAR, ESS090, ERRZERO, "SExamMsg : Null Buffer");
2975       return RFAILED;
2976    }
2977    /* check message buffer */
2978    if (!mBuf)
2979    {
2980       SSLOGERROR(ERRCLS_INT_PAR, ESS091, ERRZERO, "SExamMsg : Null Buffer");
2981       return RFAILED;
2982    }
2983    /* check index */
2984    if (idx < 0)
2985    {
2986       SSLOGERROR(ERRCLS_INT_PAR, ESS092, ERRZERO, "SExamMsg : Invalid Index");
2987       return RFAILED;
2988    }
2989    if (mBuf->b_datap->db_type != SS_M_PROTO)
2990    {
2991       SSLOGERROR(ERRCLS_INT_PAR, ESS093, ERRZERO, "SExamMsg : Incorrect buffer\
2992                                                    type");
2993       return RFAILED;
2994    }
2995 #endif
2996  
2997    /* get the SsMsgInfo */
2998    minfo = (SsMsgInfo*) mBuf->b_rptr;
2999
3000    if (minfo->len <= idx)
3001    {
3002       return (ROKDNA);
3003    }
3004
3005    /* get the first SS_M_DATA blk */
3006    tmp = mBuf->b_cont;
3007
3008    /* determine offset */
3009    FIND_OFFSET(tmp, idx)
3010
3011    *dataPtr = *(tmp->b_rptr + idx);
3012
3013    return ROK;
3014 }
3015
3016
3017 /* s002.301 */
3018 /*
3019 *
3020 *       Fun:   SGetDataFrmMsg
3021 *
3022 *       Desc:  This function copies requested byte of data from a message
3023 *              without modifying the message.
3024 *
3025 *       Ret:   ROK      - ok
3026 *              ROKDNA   - ok, data not available
3027 *              RFAILED  - failed, general (optional)
3028 *
3029 *       Notes: index is 0 based and indicates location in message
3030 *
3031 *              if index is less than the length of the message:
3032 *              message is unchanged and data is examined at specified
3033 *              index and returned via pointer to data. message length
3034 *              is unchanged. return is ok.
3035 *
3036 *              if index is greater than or equal to
3037 *              the length of the message: message is unchanged and 0
3038 *              is returned via pointer to data. return is ok, data
3039 *              not available.
3040 *
3041 *       File:  sm_msg.c
3042 *
3043 */
3044
3045
3046 #ifdef ANSI
3047 S16 SGetDataFrmMsg 
3048 (
3049 Buffer *mBuf,               /* message buffer */
3050 Data *dataPtr,              /* pointer to data */
3051 MsgLen idx,
3052 MsgLen dataLen
3053 )
3054 #else
3055 S16 SGetDataFrmMsg(mBuf, dataPtr, idx, dataLen)
3056 Buffer *mBuf;               /* message buffer */
3057 Data *dataPtr;              /* pointer to data */
3058 MsgLen idx;                 /* index */
3059 MsgLen dataLen;
3060 #endif
3061 {
3062    SsMsgInfo *minfo;
3063    Buffer *tmp;
3064         MsgLen offSetLen;
3065         Data   *tmpDataPtr = dataPtr;
3066
3067 #if (ERRCLASS & ERRCLS_INT_PAR)
3068    /* check data pointer */
3069    if (!dataPtr)
3070    {
3071       SSLOGERROR(ERRCLS_INT_PAR, ESS090, ERRZERO, "SGetDataFrmMsg: Null Buffer");
3072       return RFAILED;
3073    }
3074    /* check message buffer */
3075    if (!mBuf)
3076    {
3077       SSLOGERROR(ERRCLS_INT_PAR, ESS091, ERRZERO, "SGetDataFrmMsg: Null Buffer");
3078       return RFAILED;
3079    }
3080    /* check index */
3081    if (idx < 0)
3082    {
3083       SSLOGERROR(ERRCLS_INT_PAR, ESS092, ERRZERO, "SGetDataFrmMsg: Invalid Index");
3084       return RFAILED;
3085    }
3086    if (mBuf->b_datap->db_type != SS_M_PROTO)
3087    {
3088       SSLOGERROR(ERRCLS_INT_PAR, ESS093, ERRZERO, "SGetDataFrmMsg: Incorrect buffer\
3089                                                    type");
3090       return RFAILED;
3091    }
3092 #endif
3093  
3094    /* get the SsMsgInfo */
3095    minfo = (SsMsgInfo*) mBuf->b_rptr;
3096
3097    if (minfo->len <= (idx + dataLen) )
3098    {
3099       return (ROKDNA);
3100    }
3101
3102    /* get the first SS_M_DATA blk */
3103    tmp = mBuf->b_cont;
3104
3105    /* determine offset */
3106    if(tmp == NULLP)
3107         {
3108                 return (ROKDNA);        
3109         }
3110         else
3111         {
3112           FIND_OFFSET(tmp, idx)
3113                  offSetLen = ((tmp->b_wptr - (tmp->b_rptr + idx)));
3114
3115           for(;(offSetLen < dataLen && tmp != NULLP);)
3116           {
3117                  SMemCpy((Void *)tmpDataPtr, (Void *)(tmp->b_rptr + idx), (size_t)offSetLen);
3118                  dataLen = dataLen - offSetLen;
3119                  tmp = tmp->b_cont;
3120                  idx = 0;
3121                  tmpDataPtr = tmpDataPtr + offSetLen;
3122                  offSetLen = tmp->b_wptr - tmp->b_rptr;
3123           }
3124           if( tmp == NULLP )
3125           {
3126             return (ROKDNA);    
3127           }
3128      SMemCpy((Void *)tmpDataPtr, (Void *)(tmp->b_rptr + idx), (size_t)dataLen);
3129         }
3130
3131    return ROK;
3132 } /* End of SGetDataFrmMsg() */
3133
3134 /*
3135 *
3136 *       Fun:   SFndLenMsg
3137 *
3138 *       Desc:  This function determines the length of data within
3139 *              a message.
3140 *
3141 *       Ret:   ROK      - ok
3142 *              RFAILED  - failed, general (optional)
3143 *
3144 *       Notes: length of message is determined, message is unchanged
3145 *              and length is returned via pointer to length. return is ok.
3146 *
3147 *       File:  ss_msg.c
3148 *
3149 */
3150
3151
3152 #ifdef ANSI
3153 S16 SFndLenMsg
3154 (
3155 REG1 Buffer *mBuf,          /* message buffer */
3156 MsgLen *lngPtr
3157 )
3158 #else
3159 S16 SFndLenMsg(mBuf, lngPtr)
3160 REG1 Buffer *mBuf;          /* message buffer */
3161 MsgLen *lngPtr;             /* pointer to length */
3162 #endif
3163 {
3164    SsMsgInfo *minfo;
3165
3166 #if (ERRCLASS & ERRCLS_INT_PAR)
3167    /* check message buffer */
3168    if (mBuf == NULLP)
3169    {
3170       SSLOGERROR(ERRCLS_INT_PAR, ESS094, ERRZERO, "SFndLenMsg : Null Buffer");
3171       return RFAILED;
3172    }
3173    /* check length pointer */
3174    if (lngPtr == NULLP)
3175    {
3176       SSLOGERROR(ERRCLS_INT_PAR, ESS095, ERRZERO, "SFndLenMsg : Null Buffer");
3177       return RFAILED;
3178    }
3179    if (mBuf->b_datap->db_type != SS_M_PROTO)
3180    {
3181       SSLOGERROR(ERRCLS_INT_PAR, ESS096, ERRZERO, "SFndLenMsg : Incorrect\
3182                                                    buffer type");
3183       return RFAILED;
3184    }
3185 #endif
3186  
3187    /* get the SsMsgInfo */
3188    minfo = (SsMsgInfo*) mBuf->b_rptr;
3189
3190    /* read length */
3191    *lngPtr = minfo->len;
3192
3193    return ROK;
3194 }
3195
3196
3197 /* #ifdef SS_LOCKLESS_MEMORY */
3198
3199 /*
3200 *
3201 *       Fun:   SSegMsg
3202 *
3203 *       Desc:  This function will segment one specified message into two
3204 *              messages.
3205 *
3206 *       Ret:   ROK     - ok
3207 *              ROKDNA  - ok, data not available
3208 *              RFAILED - failed, general (optional)
3209 *              ROUTRES - failed, out of resources (optional)
3210 *
3211 *       Notes: message 1 is the original message.
3212 *
3213 *              message 2 is the new message.
3214 *
3215 *              index is 0 based and indicates location in message 1
3216 *              from which message 2 will be created.
3217 *
3218 *              if index is equal to 0: message 2 is created and all data
3219 *              attached to message 1 is moved to message 2. message 1
3220 *              is not returned to memory. return is ok.
3221 *
3222 *              if index is not equal to 0 and less than the length of
3223 *              the message minus 1: message 2 is created, all data
3224 *              attached to message 1 from index (inclusive) is moved to
3225 *              message 2. message 1 contains data from index 0 to index
3226 *              minus 1. return is ok.
3227 *
3228 *              if index is not equal to 0 and greater than or equal to
3229 *              the length of the message minus 1: message 1 is unchanged.
3230 *              message 2 is set to null. return is ok, data not available.
3231 *
3232 *       File:  ss_msg.c
3233 *
3234 */
3235 #ifdef T2K_MEM_LEAK_DBG
3236 S16 SSegMsgNew
3237 (
3238 Buffer *mBuf1,              /* message 1 */
3239 MsgLen idx,                 /* index */
3240 Buffer **mBuf2,
3241 char* file,
3242 uint32_t line
3243 )
3244 #else
3245 #ifdef ANSI
3246 S16 SSegMsg
3247 (
3248 Buffer *mBuf1,              /* message 1 */
3249 MsgLen idx,                 /* index */
3250 Buffer **mBuf2
3251 )
3252 #else
3253 S16 SSegMsg(mBuf1, idx, mBuf2)
3254 Buffer *mBuf1;              /* message 1 */
3255 MsgLen idx;                 /* index */
3256 Buffer **mBuf2;             /* message 2 */
3257 #endif
3258 #endif
3259 {
3260    SsMsgInfo *minfo1;
3261    SsMsgInfo *minfo2;
3262    Buffer *tmp;
3263    Buffer *prev;
3264    Buffer *next;
3265
3266
3267 #if (ERRCLASS & ERRCLS_INT_PAR)
3268    /* check message buffer 1 */
3269    if (mBuf1 == NULLP)
3270    {
3271       SSLOGERROR(ERRCLS_INT_PAR, ESS097, ERRZERO, "SSegMsg : Null Buffer");
3272       return RFAILED;
3273    }
3274    /* check message buffer 2 */
3275    if (mBuf2 == NULLP)
3276    {
3277       SSLOGERROR(ERRCLS_INT_PAR, ESS098, ERRZERO, "SSegMsg : Null Buffer");
3278       return RFAILED;
3279    }
3280    if (idx < 0)
3281    {
3282       SSLOGERROR(ERRCLS_INT_PAR, ESS099, ERRZERO, "SSegMsg : Invalid index");
3283       return RFAILED;
3284    }
3285    if (mBuf1->b_datap->db_type != SS_M_PROTO)
3286    {
3287       SSLOGERROR(ERRCLS_INT_PAR, ESS100, ERRZERO, "SSegMsg : Incorrect buffer\
3288                                                    type");
3289       return RFAILED;
3290    }
3291 #endif
3292
3293    /* get the SsMsgInfo of mBuf1 */
3294    minfo1 = (SsMsgInfo*) mBuf1->b_rptr;
3295 #ifdef RGL_SPECIFIC_CHANGES
3296    minfo1->region = 0;
3297 #endif   
3298
3299    /* if index > length of mBuf, return */
3300    if (idx >= minfo1->len)
3301    {
3302       *mBuf2 = NULLP;
3303       return (ROKDNA);
3304    }
3305    /* allocate message buffer */
3306    if (SGetMsg(minfo1->region, minfo1->pool, mBuf2) != ROK)
3307    {
3308       SSLOGERROR(ERRCLS_DEBUG, ESS101, ERRZERO, "SSegMsg : SGetMsg failed");
3309       return RFAILED;
3310    }
3311
3312    /* get the SsMsgInfo of mBuf2 */
3313    minfo2 = (SsMsgInfo*) (*mBuf2)->b_rptr;
3314
3315    /* adjust the lengths of mBuf1, mBuf2 */
3316    minfo2->len = minfo1->len - idx;
3317    minfo1->len = idx;
3318
3319    /* set the endptr of mBuf2 to mBuf1 */
3320    minfo2->endptr = minfo1->endptr;
3321
3322    /* if index is zero ... */
3323    if (!idx)
3324    {
3325       (*mBuf2)->b_cont = mBuf1->b_cont;
3326
3327       /* set the endptr and b_cont of mBuf1 to NULLP */
3328       minfo1->endptr = NULLP;
3329       mBuf1->b_cont = NULLP;
3330
3331       return ROK;
3332    }
3333
3334    /* get the first SS_M_DATA blk */
3335    tmp = mBuf1->b_cont;
3336    prev = mBuf1;
3337
3338    FIND_OFFSET_AND_PREV(prev, tmp, idx)
3339
3340    /* segmented at the start of a blk */
3341    if (!idx)
3342    {
3343       (*mBuf2)->b_cont = tmp;
3344       prev->b_cont = NULLP;
3345       minfo1->endptr = prev;
3346    }
3347    else
3348    {
3349 #ifndef SS_MULTICORE_SUPPORT
3350       /* allocate a message blk without a data blk */
3351       /* ssDupB internally increments the reference count */
3352 #ifdef SS_M_PROTO_REGION
3353       if (!(next = DupMsg(minfo1->region, tmp)))
3354 #else
3355       if (!(next = ssDupB(tmp)))
3356 #endif /* SS_M_PROTO_REGION */
3357       {
3358          /* reset length */
3359          minfo1->len += minfo2->len;
3360          (Void) SPutMsg(*mBuf2);
3361          return (ROUTRES);
3362       }
3363
3364       (*mBuf2)->b_cont = next;
3365
3366       tmp->b_cont = NULLP;
3367
3368       tmp->b_wptr = tmp->b_rptr + idx;
3369       next->b_rptr = tmp->b_wptr;
3370
3371       /* If the index was in the last mblk of the message, the
3372        *  end pointer of the new message needs to be set to the
3373        *  dup'ped mblk. Otherwise, the end pointer of the first
3374        *  message will be set to the mblk in which the index
3375        *  was found, and the end pointer of the new message can
3376        *  remain where it is.
3377        */
3378       if (minfo1->endptr == tmp)
3379       {
3380          minfo2->endptr = next;
3381       }
3382       else
3383       {
3384          minfo1->endptr = tmp;
3385       }
3386 #else /*SS_MULTICORE_SUPPORT*/
3387 /* 
3388  * SDeRegTTsk patch
3389  */
3390 #ifdef SS_M_PROTO_REGION
3391                 if (!(next = DupMsg(minfo1->region, tmp)))
3392 #else
3393                   if (!(next = ssDupB(tmp)))
3394 #endif /* SS_M_PROTO_REGION */
3395       {
3396          /* reset length */
3397          minfo1->len += minfo2->len;
3398          (Void) SPutMsg(*mBuf2);
3399          return (ROUTRES);
3400       }
3401       (*mBuf2)->b_cont = next;
3402       tmp->b_wptr = tmp->b_rptr + idx;
3403       next->b_rptr += idx;
3404       prev = tmp;
3405       tmp = tmp->b_cont;
3406       /* copy rest of the blocks */
3407       if(tmp)
3408       {
3409          next->b_cont = tmp;
3410          prev->b_cont = NULLP;
3411          minfo2->endptr = minfo1->endptr; 
3412          minfo1->endptr = prev;
3413       }
3414       else
3415       {
3416          next->b_cont = NULLP;
3417          minfo2->endptr = next;
3418       }
3419
3420 #endif /*SS_MULTICORE_SUPPORT*/
3421    }
3422
3423    return ROK;
3424 }
3425
3426 /*
3427 *
3428 *       Fun:   SCpyFixMsg
3429 *
3430 *       Desc:  This function copies data from a fixed buffer to a
3431 *              message.
3432 *
3433 *       Ret:   ROK      - ok
3434 *              RFAILED  - failed, general (optional)
3435 *
3436 *       Notes: None
3437 *
3438 *       File:  ss_msg.c
3439 *
3440 */
3441
3442 #ifdef ANSI
3443 S16 SCpyFixMsg
3444 (
3445 Data *srcBuf,               /* source buffer */
3446 Buffer *dstMbuf,            /* destination message buffer */
3447 MsgLen dstIdx,              /* destination index */
3448 MsgLen cnt,                 /* count */
3449 MsgLen *cCnt
3450 )
3451 #else
3452 S16 SCpyFixMsg(srcBuf, dstMbuf, dstIdx, cnt, cCnt)
3453 Data *srcBuf;               /* source buffer */
3454 Buffer *dstMbuf;            /* destination message buffer */
3455 MsgLen dstIdx;              /* destination index */
3456 MsgLen cnt;                 /* count */
3457 MsgLen *cCnt;               /* copied count */
3458 #endif
3459 {
3460    S16 ret;
3461    SsMsgInfo *minfo;
3462    Buffer *right;
3463
3464 #if (ERRCLASS & ERRCLS_INT_PAR)
3465    /* check source message buffer */
3466    if (srcBuf == NULLP)
3467    {
3468       SSLOGERROR(ERRCLS_INT_PAR, ESS102, ERRZERO, "SCpyFixMsg : Null Buffer");
3469       return RFAILED;
3470    }
3471    /* check destination message buffer */
3472    if (dstMbuf == NULLP)
3473    {
3474       SSLOGERROR(ERRCLS_INT_PAR, ESS103, ERRZERO, "SCpyFixMsg : Null Buffer");
3475       return RFAILED;
3476    }
3477    /* check copied count buffer */
3478    if (cCnt == NULLP)
3479    {
3480       SSLOGERROR(ERRCLS_INT_PAR, ESS104, ERRZERO, "SCpyFixMsg : Null Buffer");
3481       return RFAILED;
3482    }
3483    /* check copy count */
3484    if (cnt <= 0)
3485    {
3486       SSLOGERROR(ERRCLS_INT_PAR, ESS105, ERRZERO, "SCpyFixMsg : Invalid count");
3487       return RFAILED;
3488    }
3489    if (dstMbuf->b_datap->db_type != SS_M_PROTO)
3490    {
3491       SSLOGERROR(ERRCLS_INT_PAR, ESS106, ERRZERO, "SCpyFixMsg : Incorrect\
3492                                                    buffer type");
3493       return RFAILED;
3494    }
3495 #endif
3496
3497    minfo = (SsMsgInfo*) dstMbuf->b_rptr;
3498
3499    if (minfo->len < dstIdx)
3500    {
3501 #if (ERRCLASS & ERRCLS_DEBUG)
3502       SSLOGERROR(ERRCLS_DEBUG, ESS107, ERRZERO, "SCpyFixMsg : Invalid Index");
3503 #endif
3504       return RFAILED;
3505    }
3506
3507 /* ss021.103 - Addition test if message length will exceed max msg length */
3508 #if (ERRCLASS & ERRCLS_INT_PAR)
3509 #ifdef LONG_MSG
3510    if (minfo->len > 0x7FFFFFFF - cnt)
3511 #else
3512    if (minfo->len > 0x7FFF - cnt)
3513 #endif
3514    {
3515       *cCnt = 0;
3516
3517       SSLOGERROR(ERRCLS_INT_PAR, ESS108, ERRZERO, "SCpyFixMsg : msgLen + cnt > maxS16");
3518       return (ROUTRES);
3519    }
3520 #endif
3521    
3522    /* add data at the start of dst buffer */
3523    if (!dstIdx)
3524    {
3525       if ((ret = SAddPreMsgMult(srcBuf, cnt, dstMbuf)) != ROK)
3526       {
3527 #if (ERRCLASS & ERRCLS_DEBUG)
3528          SSLOGERROR(ERRCLS_DEBUG, ESS109, ERRZERO, "SCpyFixMsg : Failed in\
3529                                                     SAddPreMsgMult");
3530 #endif
3531          return (ret);
3532       }
3533       *cCnt = cnt;
3534
3535       return ROK;
3536    }
3537
3538    /* add data at the end of the dst buffer */
3539    if (minfo->len == dstIdx)
3540    {
3541       if ((ret = SAddPstMsgMult(srcBuf, cnt, dstMbuf)) != ROK)
3542       {
3543          return (ret);
3544       }
3545       *cCnt = cnt;
3546
3547       return ROK;
3548    }
3549
3550    /* segment the message into dstMbuf and right */
3551    if ((ret = SSegMsg(dstMbuf, dstIdx, &right)) != ROK)
3552    {
3553       return (ret);
3554    }
3555
3556    /* append data at the end of dstMbuf */
3557    if ((ret = SAddPstMsgMult(srcBuf, cnt, dstMbuf)) != ROK)
3558    {
3559       /* ss020.103 - Addition for cleanup */
3560       (Void) SPutMsg(right);
3561       return (ret);
3562    }
3563
3564    /* cancatenate dstMbuf and right */
3565    if ((ret = SCatMsg(dstMbuf, right, M1M2)) != ROK)
3566    {
3567       /* ss020.103 - Addition for cleanup */
3568       (Void) SPutMsg(right);
3569       return (ret);
3570    }
3571
3572    *cCnt = cnt;
3573
3574    (Void) SPutMsg(right);
3575
3576    return ROK;
3577 }
3578
3579 /*
3580 *
3581 *       Fun:   SCpyMsgFix
3582 *
3583 *       Desc:  This function copies data from a message
3584 *              into a fixed buffer.
3585 *
3586 *       Ret:   ROK      - ok
3587 *              RFAILED  - failed, general (optional)
3588 *
3589 *       Notes: None
3590 *
3591 *       File:  ss_msg.c
3592 *
3593 */
3594
3595 #ifdef ANSI
3596 S16 SCpyMsgFix
3597 (
3598 Buffer *srcMbuf,            /* source message buffer */
3599 MsgLen srcIdx,              /* source index */
3600 MsgLen cnt,                 /* count */
3601 Data *dstBuf,               /* destination buffer */
3602 MsgLen *cCnt
3603 )
3604 #else
3605 S16 SCpyMsgFix(srcMbuf, srcIdx, cnt, dstBuf, cCnt)
3606 Buffer *srcMbuf;            /* source message buffer */
3607 MsgLen srcIdx;              /* source index */
3608 MsgLen cnt;                 /* count */
3609 Data *dstBuf;               /* destination buffer */
3610 MsgLen *cCnt;               /* copied count */
3611 #endif
3612 {
3613    Data *cptr;
3614    Buffer *tmp;
3615    SsMsgInfo *minfo;
3616    MsgLen numBytes;
3617
3618 #if (ERRCLASS & ERRCLS_INT_PAR)
3619    /* check source message buffer */
3620    if (srcMbuf == NULLP)
3621    {
3622       SSLOGERROR(ERRCLS_INT_PAR, ESS110, ERRZERO, "SCpyMsgFix : Null Buffer");
3623       return RFAILED;
3624    }
3625    /* check destination message buffer */
3626    if (dstBuf == NULLP)
3627    {
3628       SSLOGERROR(ERRCLS_INT_PAR, ESS111, ERRZERO, "SCpyMsgFix : Null Buffer");
3629       return RFAILED;
3630    }
3631    if (cnt <= 0)
3632    {
3633       SSLOGERROR(ERRCLS_INT_PAR, ESS112, ERRZERO, "SCpyMsgFix : Invalid Index");
3634       return RFAILED;
3635    }
3636  
3637    if (srcIdx < 0)
3638    {
3639       SSLOGERROR(ERRCLS_INT_PAR, ESS113, ERRZERO, "SCpyMsgFix : Invalid Index");
3640       return RFAILED;
3641    }
3642    if (!cCnt)
3643    {
3644       SSLOGERROR(ERRCLS_INT_PAR, ESS114, ERRZERO, "SCpyMsgFix : Null Buffer");
3645       return RFAILED;
3646    }
3647    if (srcMbuf->b_datap->db_type != SS_M_PROTO)
3648    {
3649       SSLOGERROR(ERRCLS_INT_PAR, ESS115, ERRZERO, "SCpyMsgFix : Incorrect\
3650                                                    buffer type");
3651       return RFAILED;
3652    }
3653 #endif
3654
3655    /* get SsMsgInfo */
3656    minfo = (SsMsgInfo*) srcMbuf->b_rptr;
3657
3658    if ((srcIdx + cnt) > minfo->len)
3659    {
3660       *cCnt = 0;
3661       return (ROKDNA);
3662    }
3663
3664    /* get the first SS_M_DATA blk */
3665    tmp = srcMbuf->b_cont;
3666
3667    /* get to the srcIdx-th offset */
3668    FIND_OFFSET(tmp, srcIdx)
3669
3670    *cCnt = cnt;
3671
3672    /* set cptr to the read ptr of tmp + offset */
3673    cptr = tmp->b_rptr + srcIdx;
3674
3675    while (cnt)
3676    {
3677       /* determine the number of bytes to be copied */
3678       numBytes = MIN(cnt, (tmp->b_wptr - cptr));
3679
3680       /* decrement cnt */
3681       cnt -= numBytes;
3682
3683       /* copy data */
3684
3685 /* ss002.13 addition */
3686
3687    /* ss003.13 addition */
3688       SMemCpy((Void *) dstBuf, (Void *) cptr, (size_t) numBytes);
3689
3690       cptr += numBytes;
3691       dstBuf += numBytes;
3692
3693
3694       /* get the next blk */
3695       if ((tmp = tmp->b_cont))
3696          /* set cptr to the read ptr of tmp */
3697          cptr = tmp->b_rptr;
3698       else
3699          break;
3700    }
3701
3702    return ROK;
3703 }
3704
3705 /*
3706 *
3707 *       Fun:   SCpyMsgMsg
3708 *
3709 *       Desc:  This function is used to copy a message into
3710 *              a new region and or pool of memory.
3711 *
3712 *       Ret:   ROK      - ok
3713 *              RFAILED  - failed, general (optional)
3714 *              ROUTRES  - failed, out of resources (optional)
3715 *
3716 *       Notes: None
3717 *
3718 *       File:  ss_msg.c
3719 *
3720 */
3721
3722 #ifdef T2K_MEM_LEAK_DBG
3723 S16 SCpyMsgMsgNew
3724 (
3725 Buffer *srcBuf,
3726 Region dstRegion,
3727 Pool dstPool,
3728 Buffer **dstBuf,
3729 char* file,
3730 uint32_t line
3731 )
3732 #else
3733 #ifdef ANSI
3734 S16 SCpyMsgMsg
3735 (
3736 Buffer *srcBuf,
3737 Region dstRegion,
3738 Pool dstPool,
3739 Buffer **dstBuf
3740 )
3741 #else
3742 S16 SCpyMsgMsg(srcBuf, dstRegion, dstPool, dstBuf)
3743 Buffer *srcBuf;
3744 Region dstRegion;
3745 Pool dstPool;
3746 Buffer **dstBuf;
3747 #endif
3748 #endif
3749 {
3750    SsMsgInfo *minfo1;
3751    SsMsgInfo *minfo2;
3752    Buffer *tmp;
3753    Buffer *curblk;
3754    Buffer *newblk;
3755    Buffer *prevblk;
3756  
3757    /* ss021.103 - Addition of return value */
3758 #ifndef SS_PERF
3759 #if (ERRCLASS & ERRCLS_INT_PAR)
3760    S16 ret;
3761 #endif
3762 #endif
3763  
3764 #if (ERRCLASS & ERRCLS_INT_PAR)
3765    if (!srcBuf)
3766    {
3767       SSLOGERROR(ERRCLS_INT_PAR, ESS116, ERRZERO, "SCpyMsgMsg : Null Buffer");
3768       return RFAILED;
3769    }
3770    if (srcBuf->b_datap->db_type != SS_M_PROTO)
3771    {
3772       SSLOGERROR(ERRCLS_INT_PAR, ESS117, ERRZERO, "SCpyMsgMsg : Incorrect\
3773                                                    buffer type");
3774       return RFAILED;
3775    }
3776    /* ss021.103 - Addition to validate region and pool */
3777    if (dstRegion >= SS_MAX_REGS)
3778    {
3779       SSLOGERROR(ERRCLS_INT_PAR, ESS118, ERRZERO, 
3780                  "SCpyMsgMsg : Invalid region id");
3781       return RFAILED;
3782    }
3783  
3784    if (dstPool >= SS_MAX_POOLS_PER_REG)
3785    {
3786       SSLOGERROR(ERRCLS_INT_PAR, ESS119, ERRZERO, 
3787                  "SCpyMsgMsg : Invalid pool id");
3788       return RFAILED;
3789    }
3790 /* ss037.103 Removed the semaphore operation for performance enhancement */
3791
3792 #ifndef SS_PERF
3793    /* ss021.103 - Addition to check if region is valid */
3794    /* acquire one semaphore, to protect against deregistration */
3795    SS_ACQUIRE_SEMA(&osCp.regionTblSem, ret);
3796    if (ret != ROK)
3797    {
3798
3799 #if (ERRCLASS & ERRCLS_DEBUG)
3800       SSLOGERROR(ERRCLS_DEBUG, ESS120, (ErrVal) ret,
3801                   "Could not lock region table");
3802 #endif
3803
3804       return RFAILED;
3805    }
3806 #endif
3807
3808 #if (ERRCLASS & ERRCLS_INT_PAR)
3809    /* verify that this region is present */
3810    if (osCp.regionTbl[dstRegion].used == FALSE)
3811    {
3812 #ifndef SS_PERF
3813       if( SS_RELEASE_SEMA(&osCp.regionTblSem) != ROK)
3814       {
3815 #if (ERRCLASS & ERRCLS_DEBUG)
3816          SSLOGERROR(ERRCLS_DEBUG, ESS121, ERRZERO,
3817                   "Could not release semaphore");
3818          return RFAILED;
3819 #endif
3820       }
3821 #endif
3822       SSLOGERROR(ERRCLS_INT_PAR, ESS122, dstRegion, "Region not registered");
3823       return RFAILED;
3824    }
3825 #endif
3826 /* ss037.103 Removed the semaphore operation for performance enhancement */
3827
3828 #ifndef SS_PERF
3829    if( SS_RELEASE_SEMA(&osCp.regionTblSem) != ROK)
3830    {
3831 #if (ERRCLASS & ERRCLS_DEBUG)
3832       SSLOGERROR(ERRCLS_DEBUG, ESS123, ERRZERO,
3833                   "Could not release semaphore");
3834       return RFAILED;
3835 #endif
3836    }
3837 #endif
3838 #endif
3839
3840 #ifdef XEON_SPECIFIC_CHANGES
3841    dstRegion = 0;
3842    dstPool   = 0;
3843 #endif   
3844    /* allocate a message buffer */
3845    if (SGetMsg(dstRegion, dstPool, dstBuf) != ROK)
3846    {
3847 #if (ERRCLASS & ERRCLS_DEBUG)
3848       SSLOGERROR(ERRCLS_DEBUG, ESS124, ERRZERO, "SCpyMsgMsg : SGetMsg failed");
3849 #endif
3850       return RFAILED;
3851    }
3852    /* get the SsMsgInfo from srcBuf */
3853    minfo1 = (SsMsgInfo*) srcBuf->b_rptr;
3854
3855    /* get the SsMsgInfo from srcBuf */
3856    minfo2 = (SsMsgInfo*) (*dstBuf)->b_rptr;
3857
3858    /* get the first SS_M_DATA blk of srcBuf */
3859    tmp = srcBuf->b_cont;
3860
3861    /* if srcBuf and dstBuf belong to the same region, increment the reference
3862     * count
3863     */
3864       newblk = NULLP;
3865       curblk = NULLP;
3866       prevblk = NULLP;
3867
3868       while (tmp)
3869       {
3870 #ifdef SS_M_PROTO_REGION 
3871          if ((curblk = DupMsg(dstRegion,tmp)) == NULLP)
3872 #else
3873          if ((curblk = ssDupB(tmp)) == NULLP)
3874 #endif /* SS_M_PROTO_REGION */
3875          {
3876             while (newblk)
3877             {
3878                curblk = newblk->b_cont;
3879                (Void) SPutDBuf(minfo1->region, minfo1->pool, newblk);
3880                newblk = curblk;
3881             }
3882             printf("Failed to get the buffer of size %s %d\n", __FILE__, __LINE__);
3883             (Void) SPutMsg(*dstBuf);
3884             return (ROUTRES);
3885          }
3886
3887          if (!prevblk)
3888             newblk = curblk;
3889          else
3890             prevblk->b_cont = curblk;
3891          prevblk = curblk;
3892
3893          tmp = tmp->b_cont;
3894       }
3895       if (curblk)
3896          curblk->b_cont = NULLP;
3897
3898       minfo2->len = minfo1->len;
3899       minfo2->endptr = curblk;
3900       (*dstBuf)->b_cont = newblk;
3901
3902       return ROK;
3903 }
3904
3905
3906
3907 /*
3908 *
3909 *       Fun:   SAddMsgRef
3910 *
3911 *       Desc:  This function is used to copy a message into
3912 *              a new region and or pool of memory.
3913 *
3914 *       Ret:   ROK      - ok
3915 *              RFAILED  - failed, general (optional)
3916 *              ROUTRES  - failed, out of resources (optional)
3917 *
3918 *       Notes: None
3919 *
3920 *       File:  ss_msg.c
3921 *
3922 */
3923 #ifdef T2K_MEM_LEAK_DBG
3924 S16 SAddMsgRefNew
3925 (
3926 Buffer *srcBuf,
3927 Region dstRegion,
3928 Pool dstPool,
3929 Buffer **dstBuf,
3930 char* file,
3931 uint32_t line
3932 )
3933 #else
3934 #ifdef ANSI
3935 S16 SAddMsgRef
3936 (
3937 Buffer *srcBuf,
3938 Region dstRegion,
3939 Pool dstPool,
3940 Buffer **dstBuf
3941 )
3942 #else
3943 S16 SAddMsgRef(srcBuf, dstRegion, dstPool, dstBuf)
3944 Buffer *srcBuf;
3945 Region dstRegion;
3946 Pool dstPool;
3947 Buffer **dstBuf;
3948 #endif
3949 #endif
3950 {
3951    SsMsgInfo *minfo1;
3952    SsMsgInfo *minfo2;
3953    Buffer *tmp;
3954    Buffer *dBuf;
3955    Buffer *curblk;
3956    Buffer *newblk;
3957    Buffer *prevblk;
3958    Data *cptr;
3959    MsgLen numBytes;
3960
3961
3962 #if (ERRCLASS & ERRCLS_INT_PAR)
3963    if (!srcBuf)
3964    {
3965       SSLOGERROR(ERRCLS_INT_PAR, ESS126, ERRZERO, "SAddMsgRef : Null Buffer");
3966       return RFAILED;
3967    }
3968    if (srcBuf->b_datap->db_type != SS_M_PROTO)
3969    {
3970       SSLOGERROR(ERRCLS_INT_PAR, ESS127, ERRZERO, "SAddMsgRef : Incorrect\
3971                                                    buffer type");
3972       return RFAILED;
3973    }
3974 #endif
3975 #ifdef XEON_SPECIFIC_CHANGES
3976    dstRegion = 0;
3977    dstPool   = 0;
3978 #endif   
3979
3980    /* allocate a message buffer */
3981    if (SGetMsg(dstRegion, dstPool, dstBuf) != ROK)
3982    {
3983 #if (ERRCLASS & ERRCLS_DEBUG)
3984       SSLOGERROR(ERRCLS_DEBUG, ESS128, ERRZERO, "SAddMsgRef : SGetMsg failed");
3985 #endif
3986       return RFAILED;
3987    }
3988    /* get the SsMsgInfo from srcBuf */
3989    minfo1 = (SsMsgInfo*) srcBuf->b_rptr;
3990
3991    /* get the SsMsgInfo from srcBuf */
3992    minfo2 = (SsMsgInfo*) (*dstBuf)->b_rptr;
3993
3994    /* get the first SS_M_DATA blk of srcBuf */
3995    tmp = srcBuf->b_cont;
3996
3997    /* if srcBuf and dstBuf belong to the same region, increment the reference
3998     * count
3999     */
4000    if (dstRegion == minfo1->region)
4001    {
4002       newblk = NULLP;
4003       curblk = NULLP;
4004       prevblk = NULLP;
4005
4006       while (tmp)
4007       {
4008 #ifdef SS_M_PROTO_REGION 
4009          if ((curblk = DupMsg(dstRegion, tmp)) == NULLP)
4010 #else
4011          if ((curblk = ssDupB(tmp)) == NULLP)
4012 #endif /* SS_M_PROTO_REGION */
4013          {
4014             while (newblk)
4015             {
4016                curblk = newblk->b_cont;
4017                (Void) SPutDBuf(minfo1->region, minfo1->pool, newblk);
4018                newblk = curblk;
4019             }
4020             (Void) SPutMsg(*dstBuf);
4021             return (ROUTRES);
4022          }
4023
4024          if (!prevblk)
4025             newblk = curblk;
4026          else
4027             prevblk->b_cont = curblk;
4028          prevblk = curblk;
4029
4030          tmp = tmp->b_cont;
4031       }
4032       if (curblk)
4033          curblk->b_cont = NULLP;
4034
4035       minfo2->len = minfo1->len;
4036       minfo2->endptr = curblk;
4037       (*dstBuf)->b_cont = newblk;
4038
4039       return ROK;
4040    }
4041
4042    /* allocate a data buffer */
4043    if (ssGetDBufOfSize(dstRegion, minfo1->len, &dBuf) != ROK)
4044    {
4045 /* ss016.13: addition */
4046       (Void) SPutMsg(*dstBuf);
4047       SSLOGERROR(ERRCLS_DEBUG, ESS129, ERRZERO, "SAddMsgRef : ssGetDBufOfSize\
4048                  failed");
4049       return (ROUTRES);
4050    }
4051    dBuf->b_datap->db_type = SS_M_DATA;
4052
4053    while (tmp)
4054    {
4055       numBytes = tmp->b_wptr - tmp->b_rptr;
4056       cptr = tmp->b_rptr;
4057       while (numBytes--)
4058          *dBuf->b_wptr++ = *cptr++;
4059       tmp = tmp->b_cont;
4060    }
4061    minfo2->len = minfo1->len;
4062    /* set the endptr and b_cont of dstBuf to point to dBuf */
4063    minfo2->endptr = dBuf;
4064    (*dstBuf)->b_cont = dBuf;
4065
4066    return ROK;
4067 }
4068 /* ss012.13: Addition */
4069 #ifdef SS_M_PROTO_REGION 
4070 /*
4071 *
4072 *       Fun:   DupMsg
4073 *
4074 *       Desc:  Duplicates the specified message block, copying it
4075 *              into a newly-allocated message block. Increments
4076 *              the reference count of the data block that is pointed
4077 *              at by the original message block descriptor.
4078 *
4079 *       Ret:   non-NULL - ok
4080 *              NULL     - failure
4081 *
4082 *       Notes:
4083 *
4084 *       File:  ss_msg.c
4085 *
4086 */
4087 #ifdef T2K_MEM_LEAK_DBG
4088 static Buffer *DupMsgNew
4089 (
4090 Region region,              /* region id */
4091 Buffer *mp,                  /* message block */
4092 char* file,
4093 uint32_t line
4094 )
4095 #else
4096 #ifdef ANSI
4097 static Buffer *DupMsg
4098 (
4099 Region region,              /* region id */
4100 Buffer *mp                  /* message block */
4101 )
4102 #else
4103 static Buffer *DupMsg(region, mp)
4104 Region region;              /* region id */
4105 Buffer *mp;                 /* message block */
4106 #endif
4107 #endif
4108 {
4109    Buffer *bp;                  /* mblk for iteration */
4110    S16 r;                       /* return value */
4111    Size m;                      /* temporary */
4112
4113
4114 #if (ERRCLASS & ERRCLS_INT_PAR)
4115    if (mp == NULLP)
4116    {
4117       SSLOGERROR(ERRCLS_INT_PAR, ESS130, ERRZERO, "Null pointer");
4118       return (NULLP);
4119    }
4120
4121   if (region >= SS_MAX_REGS)
4122    {
4123       SSLOGERROR(ERRCLS_INT_PAR, ESS131, ERRZERO, "DupMsg : Invalid region\
4124                                                    id");
4125       return (NULLP);
4126    }
4127 #endif
4128
4129
4130 /* allocate a single block for the mblock and the dblock */
4131 #if 1
4132    m = (sizeof(SsMblk) + sizeof(SsDblk));
4133 #else
4134    numBytes = mp->b_wptr - mp->b_rptr;
4135    m = MDBSIZE + numBytes;
4136 #endif /* SS_MULTICORE_SUPPORT */
4137 /* ss001.301: additions */
4138 #ifdef SS_HISTOGRAM_SUPPORT 
4139    r = SAlloc(region, &m, 0, (Data **)&bp, __LINE__, (uint8_t*) __FILE__, ENTNC);
4140 #else
4141    r = SAlloc(region, &m, 0, (Data **)&bp);
4142 #endif /* SS_HISTOGRAM_SUPPORT */
4143    if (r != ROK)
4144    {
4145 #if (ERRCLASS & ERRCLS_ADD_RES)
4146       SSLOGERROR(ERRCLS_ADD_RES, ESS132, (ErrVal) r, "SAlloc() failed");
4147 #endif
4148
4149      return (NULLP);
4150    }
4151 /* generic set-up-message function */
4152 #if 1
4153 #ifndef SS_DBUF_REFLOCK_DISABLE
4154    SS_STRM_INITB(bp, (SsDblk *)(((uint8_t *)bp) + sizeof(SsMblk)), NULLP, 0, NULLP);
4155 #endif
4156 #else
4157    data = (Data *) (bp) + MDBSIZE;
4158    SS_STRM_INITB(bp,(SsDblk *)(((uint8_t *)bp) + sizeof(SsMblk)), data, numBytes, NULLP);
4159 #endif /* SS_MULTICORE_SUPPORT */
4160
4161
4162    /*  make the new message block identical to the one to be dup'ed.
4163     *  notice that an mblk/dblk pair is allocated but only the mblk
4164     *  is used,, this is for optimum performance in the average case.
4165     */
4166    SMemCpy( (Void *)bp, (Void *)mp, (size_t)sizeof(SsMblk));
4167
4168    /* ss006.301 -  added the Lock */
4169 #ifndef SS_DBUF_REFLOCK_DISABLE
4170    if((SLock(&(mp->b_datap->dBufLock))) != ROK)
4171    {
4172       SSLOGERROR(ERRCLS_DEBUG, ESS335, ERRZERO,
4173                      "Could not lock the mBuf Ref Lock");
4174       return (NULLP);
4175    }
4176 #endif
4177    /* increment the reference count of the dblock */
4178    /* increment the reference count of the dblock */
4179    mp->b_datap->db_ref++;
4180    mp->b_datap->shared = TRUE;
4181 #ifndef SS_DBUF_REFLOCK_DISABLE
4182    if((SUnlock(&(mp->b_datap->dBufLock))) != ROK)
4183    {
4184       SSLOGERROR(ERRCLS_DEBUG, ESS335, ERRZERO,
4185                      "Could not lock the mBuf Ref Lock");
4186       return (NULLP);
4187    }
4188 #endif
4189
4190
4191    return (bp);
4192 } /* DupMsg */
4193 #endif /* SS_M_PROTO_REGION */
4194
4195 /*
4196 *
4197 *       Fun:   SGetDBuf
4198 *
4199 *       Desc:  This function allocates a buffer from the dynamic
4200 *              memory pool indicated by the caller.
4201 *
4202 *       Ret:   ROK      - ok
4203 *              RFAILED  - failed, general (optional)
4204 *              ROUTRES  - failed, out of resources (optional)
4205 *
4206 *       Notes: The dynamic memory pools are used to create and
4207 *              manipulate messages.
4208 *
4209 *              SGetDBuf is never called by a protocol layer.
4210 *
4211 *              SGetDBuf assumes that interrupts are already disabled.
4212 *
4213 *       File:  ss_msg.c
4214 *
4215 */
4216
4217 #ifdef T2K_MEM_LEAK_DBG
4218 S16 SGetDBufNew
4219 (
4220 Region region,              /* region id */
4221 Pool pool,                  /* pool id */
4222 Buffer **bufPtr,
4223 char* file,
4224 uint32_t line
4225 )
4226 #else
4227 #ifdef ANSI
4228 S16 SGetDBuf
4229 (
4230 Region region,              /* region id */
4231 Pool pool,                  /* pool id */
4232 Buffer **bufPtr
4233 )
4234 #else
4235 S16 SGetDBuf(region, pool, bufPtr)
4236 Region region;              /* region id */
4237 Pool pool;                  /* pool id */
4238 Buffer **bufPtr;            /* pointer to buffer */
4239 #endif
4240 #endif
4241 {
4242    Size size;
4243    Size mdsize;
4244    Data *data;
4245    SsDblk *dptr;
4246 #ifdef SS_LOCKLESS_MEMORY
4247 #ifdef SS_USE_ICC_MEMORY
4248    CmMmDynRegCb   *regCb;
4249 #else
4250    CmMmGlobRegCb  *regCb;
4251 #endif
4252 #else
4253    SsRegionEntry *regp;
4254 #endif /* SS_LOCKLESS_MEMORY */
4255       /* ss021.103 - Addition of return value */
4256 #ifndef SS_PERF
4257 #if (ERRCLASS & ERRCLS_INT_PAR)
4258    S16 ret;
4259 #endif
4260 #endif
4261    
4262 #if (ERRCLASS & ERRCLS_INT_PAR)
4263    /* check buffer pointer */
4264    if (!bufPtr)
4265    {
4266       SSLOGERROR(ERRCLS_INT_PAR, ESS133, ERRZERO, "SGetDBuf : Null Buffer");
4267       return RFAILED;
4268    }
4269    if (region >= SS_MAX_REGS)
4270    {
4271       SSLOGERROR(ERRCLS_INT_PAR, ESS134, ERRZERO, "SGetDBuf : Invalid region\
4272                                                    id");
4273       return RFAILED;
4274    }
4275  
4276    if (pool >= SS_MAX_POOLS_PER_REG)
4277    {
4278       SSLOGERROR(ERRCLS_INT_PAR, ESS135, ERRZERO, "SGetDBuf : Invalid pool id");
4279       return RFAILED;
4280    }
4281 /* ss037.103 Removed the semaphore operation for performance enhancement */
4282
4283 #ifndef SS_PERF
4284    /* ss021.103 - Addition to check if region is registered */
4285    /* acquire one semaphore, to protect against deregistration */
4286    SS_ACQUIRE_SEMA(&osCp.regionTblSem, ret);
4287    if (ret != ROK)
4288    {
4289
4290 #if (ERRCLASS & ERRCLS_DEBUG)
4291       SSLOGERROR(ERRCLS_DEBUG, ESS136, (ErrVal) ret,
4292                   "Could not lock region table");
4293 #endif
4294
4295       return RFAILED;
4296    }
4297
4298 #endif
4299
4300 #if (ERRCLASS & ERRCLS_INT_PAR)
4301    /* verify that this region is present */
4302    if (osCp.regionTbl[region].used == FALSE)
4303    {
4304 /* ss037.103 Removed the semaphore operation for performance enhancement */
4305
4306 #ifndef SS_PERF
4307       if( SS_RELEASE_SEMA(&osCp.regionTblSem) != ROK)
4308       {
4309 #if (ERRCLASS & ERRCLS_DEBUG)
4310          SSLOGERROR(ERRCLS_DEBUG, ESS137, ERRZERO,
4311                   "Could not release semaphore");
4312          return RFAILED;
4313 #endif
4314       }
4315 #endif
4316       SSLOGERROR(ERRCLS_INT_PAR, ESS138, region, "Region not registered");
4317       return RFAILED;
4318    }
4319 #endif
4320 /* ss037.103 Removed the semaphore operation for performance enhancement */
4321
4322 #ifndef SS_PERF
4323    if( SS_RELEASE_SEMA(&osCp.regionTblSem) != ROK)
4324    {
4325 #if (ERRCLASS & ERRCLS_DEBUG)
4326       SSLOGERROR(ERRCLS_DEBUG, ESS139, ERRZERO,
4327                   "Could not release semaphore");
4328       return RFAILED;
4329 #endif
4330    }
4331 #endif
4332 #endif
4333
4334 #ifdef SS_LOCKLESS_MEMORY
4335 #ifdef SS_USE_ICC_MEMORY
4336    regCb = (CmMmDynRegCb *)(osCp.dynRegionTbl[region].regCb);
4337    mdsize = regCb->bktSize[pool];
4338 #else  /* SS_USE_ICC_MEMORY */
4339    regCb = osCp.globRegCb;
4340 #ifdef SS_MEM_WL_DEBUG
4341    mdsize = regCb->bktTbl[pool].size - 4;
4342 #else  /* SS_MEM_WL_DEBUG */
4343    mdsize = regCb->bktTbl[pool].size;
4344 #endif /* SS_MEM_WL_DEBUG */
4345 #endif /* SS_USE_ICC_MEMORY */
4346 #else
4347    regp = &osCp.regionTbl[region];
4348    size = regp->poolTbl[pool].u.dpool.size;
4349    /* ss006.301 : optimized this function */
4350    mdsize = MDBSIZE + size;
4351 #endif /* SS_LOCKLESS_MEMORY */
4352
4353    /* ss006.301 : optimized this function */
4354 /* ss001.301: additions */
4355 #ifdef SS_HISTOGRAM_SUPPORT 
4356     if (SAlloc(region, &mdsize, 0, (Data **) bufPtr, __LINE__, (uint8_t*) __FILE__, ENTNC) != ROK)
4357 #else
4358     if (SAlloc(region, &mdsize, 0, (Data **) bufPtr) != ROK)
4359 #endif /* SS_HISTOGRAM_SUPPORT */
4360     {
4361        return (ROUTRES);
4362     }
4363     data = (Data *) (*bufPtr) + MDBSIZE;
4364     size = mdsize - MDBSIZE;
4365
4366    dptr = (SsDblk*) (((Data *) (*bufPtr)) + MBSIZE);
4367
4368    INITB((*bufPtr), dptr, data, size, NULLP)
4369 #ifndef SS_DBUF_REFLOCK_DISABLE
4370    if((SInitLock (&(dptr->dBufLock), SS_LOCK_MUTEX)) != 0)
4371    {
4372       printf("Falied to destroy lock\n");
4373    }
4374 #endif
4375
4376    return ROK;
4377 }
4378
4379 /*
4380 *
4381 *       Fun:   SPutDBuf
4382 *
4383 *       Desc:  This function deallocates a buffer back to the
4384 *              dynamic memory pool indicated by the caller.
4385 *
4386 *       Ret:   ROK      - ok
4387 *              RFAILED  - failed, general (optional)
4388 *
4389 *       Notes: The dynamic memory pools are used to create and
4390 *              manipulate messages.
4391 *
4392 *              SPutDBuf is never called by a protocol layer.
4393 *
4394 *              SPutDBuf assumes that interrupts are already disabled.
4395 *
4396 *       File:  ss_msg.c
4397 *
4398 */
4399
4400 #ifdef T2K_MEM_LEAK_DBG
4401 S16 SPutDBufNew
4402 (
4403 Region region,
4404 Pool pool,
4405 Buffer *buf,
4406 char* file,
4407 uint32_t line
4408 )
4409 #else
4410 #ifdef ANSI
4411 S16 SPutDBuf
4412 (
4413 Region region,
4414 Pool pool,
4415 Buffer *buf
4416 )
4417 #else
4418 S16 SPutDBuf(region, pool, buf)
4419 Region region;
4420 Pool pool;
4421 Buffer *buf;
4422 #endif
4423 #endif
4424 {
4425    register SsDblk *dptr;
4426    /* ss021.103 - Addition to check return value of SFree */
4427    S16 ret;
4428 #if (defined(SS_USE_ZBC_MEMORY) && defined (TENB_DPDK_BUF))
4429    Data  *dpdkBuf;
4430 #endif   
4431  
4432  
4433    /* ss021.103 - Addition of ret initialization */
4434    ret = ROK;
4435  
4436 #if (ERRCLASS & ERRCLS_INT_PAR)
4437    if (region >= SS_MAX_REGS)
4438    {
4439       SSLOGERROR(ERRCLS_INT_PAR, ESS141, ERRZERO, "SPutDBuf:Invalid region");
4440       return RFAILED;
4441    }
4442  
4443    if (pool >= SS_MAX_POOLS_PER_REG)
4444    {
4445       SSLOGERROR(ERRCLS_INT_PAR, ESS142, ERRZERO, "SPutDBuf:Invalid pool");
4446       return RFAILED;
4447    }
4448  
4449    if (buf == NULLP)
4450    {
4451       SSLOGERROR(ERRCLS_INT_PAR, ESS143, ERRZERO, "SPutDBuf:Null pointer");
4452       return RFAILED;
4453    }
4454
4455    if ( (buf->b_datap->db_type != SS_M_DATA) && (buf->b_datap->db_type != SS_M_PROTO))
4456    {
4457       SSLOGERROR(ERRCLS_INT_PAR, ESS144, ERRZERO, "SPutDBuf:Incorrect\
4458                  buffer type");
4459       return RFAILED;
4460    }
4461 #endif
4462 /* ss016.13: Addition */
4463    dptr = buf->b_datap; 
4464
4465 #ifdef SS_USE_ZBC_MEMORY
4466    if(dptr->db_type == SS_MEM_TYPE_SSI_ZBC)
4467    {
4468       ret = SPutZbcDBuf(region, buf);
4469       return (ret);
4470    }
4471
4472 #ifdef TENB_DPDK_BUF
4473    if(dptr->db_type == SS_MEM_TYPE_DPDK_ZBC)
4474    {
4475       /* Not considering referances for DPDK buffer for now */
4476       SDetachDpdkPtrFrmDBuf(buf, &dpdkBuf);
4477       SPutSBufDpdk(dpdkBuf);
4478       ret = SFree(region, (Data *) buf, MDBSIZE);
4479       return (ret);
4480    }
4481 #endif  /* TENB_DPDK_BUF */
4482 #endif /* SS_USE_ZBC_MEMORY */
4483
4484    /* ss028.103 - Addition of lock for mBuf reference count */
4485     /* ss006.301 : optimized this funciton */
4486 #if 1
4487    if(!dptr->shared)
4488    {
4489 #ifndef SS_DBUF_REFLOCK_DISABLE
4490        SDestroyLock(&dptr->dBufLock);
4491 #endif
4492       /* if the data block is not shared, free the buffer, checks not reqd */
4493 #ifdef SS_HISTOGRAM_SUPPORT 
4494       ret = SFree(region, (Data *) buf, MDBSIZE + dptr->db_lim - dptr->db_base
4495          , __LINE__, (uint8_t*) __FILE__, ENTNC);
4496 #else
4497       ret =  SFree(region, (Data *) buf, MDBSIZE + dptr->db_lim - dptr->db_base);
4498 #endif /* SS_HISTOGRAM_SUPPORT */
4499    }
4500    else
4501    {
4502 #ifndef SS_DBUF_REFLOCK_DISABLE
4503         if((ret=SLock(&dptr->dBufLock)))
4504         {
4505            SSLOGERROR(ERRCLS_DEBUG, ESSXXX, ERRZERO,
4506                 "Could not lock the mBuf Ref Lock");
4507            return RFAILED;
4508         }
4509 #endif
4510       --dptr->db_ref;
4511       /* if buffer's message blk is obtained during dupb */
4512       if (buf != (SsMblk *) (((Data*) dptr) - MBSIZE))
4513       {
4514         
4515          SsDblk* dupdptr = (SsDblk *)((uint8_t *)buf + MBSIZE);
4516          dupdptr->db_ref--;
4517          if(dupdptr->db_ref == 0)
4518          {
4519
4520 #ifdef SS_HISTOGRAM_SUPPORT 
4521          ret = SFree(region, (Data *) buf, MDBSIZE, __LINE__, (uint8_t*) __FILE__, ENTNC);
4522 #else
4523          ret = SFree(region, (Data *) buf, MDBSIZE);
4524
4525 #endif /* SS_HISTOGRAM_SUPPORT */
4526          }
4527          buf = (SsMblk *) (((Data*) dptr) - MBSIZE);
4528       }
4529       /* if reference count falls to zero */
4530       if (!dptr->db_ref)
4531       {
4532 #ifndef SS_DBUF_REFLOCK_DISABLE
4533         ret = SUnlock(&dptr->dBufLock) ;
4534         if((SDestroyLock(&dptr->dBufLock)) != 0)
4535         {
4536             printf("Falied to destroy lock\n");
4537         }
4538 #endif
4539       /* free buffer to region */
4540 #ifdef SS_HISTOGRAM_SUPPORT 
4541          ret = SFree(region, (Data *) buf, MDBSIZE + dptr->db_lim - dptr->db_base
4542          , __LINE__, (uint8_t*) __FILE__, ENTNC);
4543 #else
4544          ret =  SFree(region, (Data *) buf, MDBSIZE + dptr->db_lim - dptr->db_base);
4545 #endif /* SS_HISTOGRAM_SUPPORT */
4546          return (ret);
4547       }
4548 #ifndef SS_DBUF_REFLOCK_DISABLE
4549       ret = SUnlock(&(dptr->dBufLock));
4550 #endif
4551    }
4552 #else /* SS_MULTICORE_SUPPORT */
4553    /* If MultiCore Support enabled, Dblk never be shared */
4554 #ifdef SS_HISTOGRAM_SUPPORT 
4555    ret = SFree(region, (Data *) buf, MDBSIZE + dptr->db_lim - dptr->db_base
4556          , __LINE__, (uint8_t*) __FILE__, ENTNC);
4557 #else
4558    ret =  SFree(region, (Data *) buf, MDBSIZE + dptr->db_lim - dptr->db_base);
4559 #endif /* SS_HISTOGRAM_SUPPORT */
4560 #endif /* SS_MULTICORE_SUPPORT */
4561    return (ret);
4562 }
4563
4564
4565 /* #ifdef SS_LOCKLESS_MEMORY */
4566
4567
4568 /*
4569 *
4570 *       Fun:   SCatMsg
4571 *
4572 *       Desc:  This function will concatenate the two specified messages
4573 *              into one message.
4574 *
4575 *       Ret:   ROK      - ok
4576 *              RFAILED  - failed, general (optional)
4577 *
4578 *       Notes: if order equal M1M2: all data attached to message 2 is
4579 *              moved to the end of message 1. message 2 is set to empty.
4580 *              message 1 length is increased by length of message 2.
4581 *              message 2 length is set to zero. message 2 is not returned
4582 *              to memory. return is ok.
4583 *
4584 *              if order equal M2M1: all data attached to message 2 is
4585 *              moved to the front of message 1. message 2 is set to empty.
4586 *              message 1 length is increased by length of message 2.
4587 *              message 2 length is set to zero. message 2 is not returned
4588 *              to memory. return is ok.
4589 *
4590 *       File:  ss_msg.c
4591 *
4592 */
4593
4594
4595 #ifdef ANSI
4596 S16 SCatMsg
4597 (
4598 Buffer *mBuf1,              /* message 1 */
4599 Buffer *mBuf2,              /* message 2 */
4600 Order order
4601 )
4602 #else
4603 S16 SCatMsg(mBuf1, mBuf2, order)
4604 Buffer *mBuf1;              /* message 1 */
4605 Buffer *mBuf2;              /* message 2 */
4606 Order order;                /* order */
4607 #endif
4608 {
4609    SsMsgInfo *minfo1;
4610    SsMsgInfo *minfo2;
4611    Buffer *tmp;
4612    Buffer *newb;
4613
4614 #if (ERRCLASS & ERRCLS_INT_PAR)
4615    /* check message buffer 1 */
4616    if (mBuf1 == NULLP)
4617    {
4618       SSLOGERROR(ERRCLS_INT_PAR, ESS148, ERRZERO, "SCatMsg : Null Buffer");
4619       return RFAILED;
4620    }
4621    /* check message buffer 2 */
4622    if (mBuf2 == NULLP)
4623    {
4624       SSLOGERROR(ERRCLS_INT_PAR, ESS149, ERRZERO, "SCatMsg : Null Buffer");
4625       return RFAILED;
4626    }
4627    /* ss021.103 - Addition to test if same buffer */
4628    /* check message buffer 1 and 2 not same buffer */
4629    if (mBuf1 == mBuf2)
4630    {
4631       SSLOGERROR(ERRCLS_INT_PAR, ESS150, ERRZERO, "SCatMsg : mBuf1 == mBuf2");
4632       return RFAILED;
4633    }
4634    if ((order != M1M2) && (order != M2M1))
4635    {
4636       SSLOGERROR(ERRCLS_INT_PAR, ESS151, ERRZERO, "SCatMsg : Invalid Order");
4637       return RFAILED;
4638    }
4639    if ((mBuf1->b_datap->db_type != SS_M_PROTO) ||
4640        (mBuf2->b_datap->db_type != SS_M_PROTO))
4641    {
4642       SSLOGERROR(ERRCLS_INT_PAR, ESS152, ERRZERO, 
4643                                          "SCatMsg : Incorrect buffer type");
4644       return RFAILED;
4645    }
4646 #endif
4647
4648    /* no data to append or prepend */
4649    if (!mBuf2->b_cont)
4650       return ROK;
4651
4652    minfo1 = (SsMsgInfo*) mBuf1->b_rptr;
4653    minfo2 = (SsMsgInfo*) mBuf2->b_rptr;
4654
4655 /* ss021.103 - Addition to test max length of message is not exceeded */
4656 #if (ERRCLASS & ERRCLS_INT_PAR)
4657 #ifdef LONG_MSG
4658    if (minfo1->len > 0x7FFFFFFF - minfo2->len)
4659 #else
4660    if (minfo1->len > 0x7FFF - minfo2->len)
4661 #endif
4662    {
4663       SSLOGERROR(ERRCLS_INT_PAR, ESS153, ERRZERO, "SCpyFixMsg : messages too big");
4664       return (ROUTRES);
4665    }
4666 #endif
4667    
4668    if (minfo1->region != minfo2->region)
4669    {
4670       /* duplicate mBuf2 from the region/pool of mBuf1, initialise mBuf2 */
4671 /*ss015.13: addition */
4672       if (SCpyMsgMsg(mBuf2, minfo1->region, minfo1->pool, &newb) != ROK)
4673          return RFAILED;
4674
4675       minfo2 = (SsMsgInfo*) newb->b_rptr;
4676    }
4677    else
4678       newb = mBuf2;
4679
4680    if ((tmp = mBuf1->b_cont) == NULLP)
4681    {
4682       mBuf1->b_cont = newb->b_cont;
4683       minfo1->endptr = minfo2->endptr;
4684    }
4685    else
4686    {
4687       if (order == M1M2)
4688       /* attach newb after mBuf1 */
4689       {
4690          minfo1->endptr->b_cont = newb->b_cont;
4691          minfo1->endptr = minfo2->endptr;
4692       }
4693       else
4694       {
4695          if (order == M2M1)
4696          /* attach newb before mBuf1 */
4697          {
4698             minfo2->endptr->b_cont = mBuf1->b_cont;
4699             mBuf1->b_cont = newb->b_cont;
4700          }
4701          else /* invalid order */
4702          {
4703 #if (ERRCLASS & ERRCLS_DEBUG)
4704             SSLOGERROR(ERRCLS_DEBUG, ESS154, ERRZERO, "SCatMsg:Invalid order");
4705 #endif
4706             if (newb && (newb != mBuf2))
4707                (Void) SPutMsg(newb);
4708             return RFAILED;
4709          }
4710       }
4711    }
4712    minfo1->len += minfo2->len;
4713
4714
4715    minfo2->endptr = NULLP;
4716    minfo2->len = 0;
4717  
4718    newb->b_cont = NULLP;
4719
4720    if (newb != mBuf2)
4721    {
4722       (Void) SPutMsg(newb);
4723       (Void) SInitMsg(mBuf2);
4724    }
4725
4726    return ROK;
4727 }
4728
4729 /*
4730 *
4731 *       Fun:   SRepMsg
4732 *
4733 *       Desc:  This function replaces one byte of data in a message.
4734 *
4735 *       Ret:   ROK      - ok
4736 *              ROKDNA   - ok, data not available
4737 *              RFAILED  - failed, general (optional)
4738 *
4739 *       Notes: index is 0 based and indicates location in message
4740 *
4741 *               if index is less than the length of the message:
4742 *              data is replaced at specified index. message length
4743 *              is unchanged. return is ok.
4744 *
4745 *              if index is greater than or equal to
4746 *              the length of the message: message is unchanged.
4747 *              return is ok, data not available.
4748 *
4749 *       File:  ss_msg.c
4750 *
4751 */
4752
4753
4754 #ifdef ANSI
4755 S16 SRepMsg
4756 (
4757 Data data,                  /* data */
4758 Buffer *mBuf,               /* message buffer */
4759 MsgLen idx
4760 )
4761 #else
4762 S16 SRepMsg(data, mBuf, idx)
4763 Data data;                  /* data */
4764 Buffer *mBuf;               /* message buffer */
4765 MsgLen idx;                 /* index */
4766 #endif
4767 {
4768    SsMsgInfo *minfo;
4769    Buffer *tmp;
4770    Buffer *newb;
4771    Buffer *prev;
4772    MsgLen numBytes;
4773
4774
4775 #ifdef T2K_MEM_LEAK_DBG
4776    char* file = __FILE__;
4777    uint32_t line = __LINE__;
4778 #endif
4779 #if ( ERRCLASS & ERRCLS_INT_PAR)
4780    /* check message buffer */
4781    if (mBuf == NULLP)
4782    {
4783       SSLOGERROR(ERRCLS_INT_PAR, ESS155, ERRZERO, "SRepMsg : Null Buffer");
4784       return RFAILED;
4785    }
4786    if (idx < 0)
4787    {
4788       SSLOGERROR(ERRCLS_INT_PAR, ESS156, ERRZERO, "SRepMsg : Invalid index");
4789       return RFAILED;
4790    }
4791    if (mBuf->b_datap->db_type != SS_M_PROTO)
4792    {
4793       SSLOGERROR(ERRCLS_INT_PAR, ESS157, ERRZERO, "SRepMsg : Incorrect buffer\
4794                                                    type");
4795       return RFAILED;
4796    }
4797 #endif
4798
4799    minfo = (SsMsgInfo*) (mBuf)->b_rptr;
4800
4801    /* if index > length of the buffer */
4802    if (minfo->len <= idx)
4803    {
4804       return (ROKDNA);
4805    }
4806
4807    tmp = mBuf->b_cont; /* get the first SS_M_DATA blk */
4808    prev = mBuf;        /* parent */
4809
4810    FIND_OFFSET_AND_PREV(prev, tmp, idx)
4811
4812    /* if ref cnt is greater than 1, duplicate tmp */
4813    if (tmp->b_datap->db_ref > 1)
4814    {
4815       /* allocate a message blk of message size of tmp */
4816       numBytes = tmp->b_wptr - tmp->b_rptr;
4817       if (ssGetDBufOfSize(minfo->region, numBytes, &newb) != ROK)
4818       {
4819          SSLOGERROR(ERRCLS_DEBUG, ESS158, ERRZERO, "SRepMsg : ssGetDBufOfSize\
4820                     failed");
4821          return RFAILED;
4822       }
4823       while (numBytes--)
4824          *newb->b_wptr++ = *tmp->b_rptr++;
4825
4826       newb->b_cont = tmp->b_cont;
4827       prev->b_cont = newb;
4828       if (minfo->endptr == tmp)
4829          minfo->endptr = newb;
4830
4831       /* free tmp */
4832       (Void) SPutDBuf(minfo->region, minfo->pool, tmp);
4833       tmp = newb;
4834    }
4835    *(tmp->b_rptr + idx) = data;
4836
4837    return ROK;
4838 }
4839
4840 /*
4841 *
4842 *       Fun:   SUpdMsg
4843 *
4844 *       Desc:  Update a message with a new dBuf
4845 *
4846 *       Ret:   ROK/RFAILED
4847 *
4848 *       Notes:
4849 *
4850 *       File:  ss_msg.c
4851 *
4852 */
4853
4854
4855 #ifdef ANSI
4856 S16 SUpdMsg
4857 (
4858 Buffer *mBuf,                   /* message buffer */
4859 Buffer *dBuf,                   /* data buffer */
4860 MsgLen dLen
4861 )
4862 #else
4863 S16 SUpdMsg(mBuf, dBuf, dLen)
4864 Buffer *mBuf;                   /* message buffer */
4865 Buffer *dBuf;                   /* data buffer */
4866 MsgLen dLen;                    /* data length */
4867 #endif
4868 {
4869    SsMsgInfo *minfo;
4870
4871 #if (ERRCLASS & ERRCLS_INT_PAR)
4872    if (!mBuf)
4873    {
4874       SSLOGERROR(ERRCLS_INT_PAR, ESS159, ERRZERO, "SUpdMsg : Null Buffer");
4875       return RFAILED;
4876    }
4877    if (!dBuf)
4878    {
4879       SSLOGERROR(ERRCLS_INT_PAR, ESS160, ERRZERO, "SUpdMsg : Null Buffer");
4880       return RFAILED;
4881    }
4882    if (dLen < 0)
4883    {
4884       SSLOGERROR(ERRCLS_INT_PAR, ESS161, ERRZERO, "SUpdMsg : Invalid length");
4885       return RFAILED;
4886    }
4887    if ((mBuf->b_datap->db_type != SS_M_PROTO) ||
4888        (dBuf->b_datap->db_type != SS_M_DATA))
4889    {
4890       SSLOGERROR(ERRCLS_INT_PAR, ESS162, ERRZERO, "SUpdMsg : Incorrect buffer\
4891                                                    type");
4892       return RFAILED;
4893    }
4894 #endif
4895
4896    /* get the message info of mBuf */
4897    minfo = (SsMsgInfo*) (mBuf)->b_rptr;
4898
4899   /* accept zero length data */
4900
4901    /* buffer offset out of bounds */
4902    if ((dBuf->b_rptr + dLen) > dBuf->b_datap->db_lim)
4903    {
4904       SSLOGERROR(ERRCLS_DEBUG, ESS163, ERRZERO, "SUpdMsg:Offset out of bounds");
4905       return RFAILED;
4906    }
4907
4908    /* offset write ptr from read ptr by dLen */
4909    dBuf->b_wptr = dBuf->b_rptr + dLen;
4910
4911    /* attach dBuf at the end of mBuf */
4912    if (minfo->endptr)
4913       minfo->endptr->b_cont = dBuf;
4914    else
4915       mBuf->b_cont = dBuf;
4916
4917    /* update SsMsgInfo */
4918    minfo->endptr = dBuf;
4919
4920    minfo->len += dLen;
4921
4922    return ROK;
4923 }
4924
4925 /*
4926 *
4927 *       Fun:   SAddDBufPst
4928 *
4929 *       Desc:  This function queues a data buffer to the
4930 *              back of the specified message buffer .
4931 *
4932 *       Ret:   ROK     - ok
4933 *              RFAILED - failed, general (optional)
4934 *
4935 *       Notes: if queue is empty: buffer is placed in the queue.
4936 *              queue length is incremented.
4937 *
4938 *              if queue is not empty: buffer is placed behind all
4939 *              other buffers in queue. queue length is incremented.
4940 *
4941 *       File:  ss_msg.c
4942 *
4943 */
4944
4945  
4946 #ifdef ANSI
4947 S16 SAddDBufPst
4948 (
4949 Buffer *mBuf,                   /* message buffer */
4950 Buffer *dBuf
4951 )
4952 #else
4953 S16 SAddDBufPst(mBuf, dBuf)
4954 Buffer *mBuf;                   /* message buffer */
4955 Buffer *dBuf;                   /* data buffer */
4956 #endif
4957 {
4958    SsMsgInfo *minfo;
4959
4960 #if (ERRCLASS & ERRCLS_INT_PAR)
4961    /* check buffer queue */
4962    if (!mBuf || !dBuf)
4963    {
4964       SSLOGERROR(ERRCLS_INT_PAR, ESS164, ERRZERO, "SAddDBufPst : Null Buffer");
4965       return RFAILED;
4966    }
4967    if ((mBuf->b_datap->db_type != SS_M_PROTO) ||
4968        (dBuf->b_datap->db_type != SS_M_DATA))
4969    {
4970       SSLOGERROR(ERRCLS_INT_PAR, ESS165, ERRZERO, "SAddDBufPst : Incorrect\
4971                                                    buffer type");
4972       return RFAILED;
4973    }
4974 #endif
4975
4976    /* no data, return */
4977    if (dBuf->b_wptr == dBuf->b_rptr)
4978       return ROK;
4979
4980    minfo = (SsMsgInfo*) (mBuf)->b_rptr;
4981  
4982    /* attach dBuf at the end of mBuf */
4983    if (minfo->endptr)
4984       minfo->endptr->b_cont = dBuf;
4985    else
4986       mBuf->b_cont = dBuf;
4987
4988    /* update SsMsgInfo */
4989    minfo->endptr = dBuf;
4990
4991    minfo->len += dBuf->b_wptr - dBuf->b_rptr;
4992
4993    return ROK;
4994 }
4995
4996 /*
4997 *
4998 *       Fun:   SAddDBufPre
4999 *
5000 *       Desc:  This function queues a data buffer to the
5001 *              front of the specified message buffer.
5002 *
5003 *       Ret:   ROK     - ok
5004 *              RFAILED - failed, general (optional)
5005 *
5006 *       Notes: if buffer queue is empty: buffer is placed in the queue. queue
5007 *              length is incremented.
5008 *
5009 *              if buffer queue is not empty: buffer is placed in front of all
5010 *              other buffers in queue. queue length is incremented.
5011 *
5012 *       File:  ss_msg.c
5013 *
5014 */
5015
5016  
5017 #ifdef ANSI
5018 S16 SAddDBufPre
5019 (
5020 Buffer *mBuf,                    /* message buffer */
5021 Buffer *dBuf
5022 )
5023 #else
5024 S16 SAddDBufPre(mBuf, dBuf)
5025 Buffer *mBuf;                    /* message buffer */
5026 Buffer *dBuf;                    /* data buffer */
5027 #endif
5028 {
5029    SsMsgInfo *minfo;
5030    Buffer *tmp;
5031
5032 #if (ERRCLASS & ERRCLS_INT_PAR)
5033    /* check buffer queue */
5034    if (!mBuf || !dBuf)
5035    {
5036       SSLOGERROR(ERRCLS_INT_PAR, ESS166, ERRZERO, "SAddDBufPre : Null Buffer");
5037       return RFAILED;
5038    }
5039    if ((mBuf->b_datap->db_type != SS_M_PROTO) ||
5040        (dBuf->b_datap->db_type != SS_M_DATA))
5041    {
5042       SSLOGERROR(ERRCLS_INT_PAR, ESS167, ERRZERO, "SAddDBufPre : Incorrect\
5043                                                    buffer type");
5044       return RFAILED;
5045    }
5046 #endif
5047  
5048    /* no data, return */
5049    if (dBuf->b_wptr == dBuf->b_rptr)
5050       return ROK;
5051  
5052    minfo = (SsMsgInfo*) (mBuf)->b_rptr;
5053
5054    tmp = mBuf->b_cont;
5055
5056    /* attach dBuf at the start of mBuf */
5057    mBuf->b_cont = dBuf;
5058    dBuf->b_cont = tmp;
5059
5060    minfo->len += dBuf->b_wptr - dBuf->b_rptr;
5061
5062    if (!tmp)
5063       minfo->endptr = dBuf;
5064
5065    return ROK;
5066 }
5067  
5068 /*
5069 *
5070 *       Fun:   SRemDBufPre
5071 *
5072 *       Desc:  This function dequeues a data buffer from
5073 *              the front of the specified message buffer.
5074 *
5075 *       Ret:   ROK     - ok
5076 *              ROKDNA  - ok, data not available
5077 *              RFAILED - failed, general (optional)
5078 *
5079 *       Notes: if queue is empty: pointer to buffer is set to null and
5080 *              return is ok, data not available. queue length is unchanged.
5081 *
5082 *              if queue is not empty: pointer to buffer is set to first
5083 *              buffer in queue, first buffer in queue is removed and
5084 *              return is ok. queue length is decremented.
5085 *
5086 *       File:  ss_msg.c
5087 *
5088 */
5089
5090  
5091 #ifdef ANSI
5092 S16 SRemDBufPre
5093 (
5094 Buffer *mBuf,                   /* message buffer */
5095 Buffer **dBufPtr
5096 )
5097 #else
5098 S16 SRemDBufPre(mBuf, dBufPtr)
5099 Buffer *mBuf;                   /* message buffer */
5100 Buffer **dBufPtr;               /* pointer to data buffer */
5101 #endif
5102 {
5103    SsMsgInfo *minfo;
5104
5105 #if (ERRCLASS & ERRCLS_INT_PAR)
5106    /* check buffer pointer */
5107    if (dBufPtr == NULLP)
5108    {
5109       SSLOGERROR(ERRCLS_INT_PAR, ESS168, ERRZERO, "SRemDBufPre : Null Buffer");
5110       return RFAILED;
5111    }
5112    /* check queue */
5113    if (mBuf == NULLP)
5114    {
5115       SSLOGERROR(ERRCLS_INT_PAR, ESS169, ERRZERO, "SRemDBufPre : Null Buffer");
5116       return RFAILED;
5117    }
5118    if (mBuf->b_datap->db_type != SS_M_PROTO)
5119    {
5120       SSLOGERROR(ERRCLS_INT_PAR, ESS170, ERRZERO, "SRemDBufPre : Incorrect\
5121                                                    buffer type");
5122       return RFAILED;
5123    }
5124 #endif
5125
5126    /* no data blk, return */
5127    if ((*dBufPtr = mBuf->b_cont) == NULLP)
5128    {
5129       return ROKDNA;
5130    }
5131    minfo = (SsMsgInfo*) (mBuf)->b_rptr;
5132
5133    mBuf->b_cont = (*dBufPtr)->b_cont;
5134    (*dBufPtr)->b_cont = NULLP;
5135
5136    /* update SsMsgInfo */
5137    if (!(minfo->len -= (*dBufPtr)->b_wptr - (*dBufPtr)->b_rptr))
5138       minfo->endptr = NULLP;
5139
5140    return ROK;
5141 }
5142
5143 /*
5144 *
5145 *       Fun:   SRemDBufPst
5146 *
5147 *       Desc:  This function dequeues a data or message buffer from the
5148 *              back of the specified message buffer.
5149 *
5150 *       Ret:   ROK     - ok
5151 *              ROKDNA  - ok, data not available
5152 *              RFAILED - failed, general (optional)
5153 *
5154 *       Notes: if queue is empty: pointer to buffer is set to null and
5155 *              return is ok, data not available. queue length is unchanged.
5156 *
5157 *              if queue is not empty: pointer to buffer is set to last
5158 *              buffer in queue, last buffer in queue is removed and
5159 *              return is ok. queue length is decremented.
5160 *
5161 *       File:  ss_msg.c
5162 *
5163 */
5164
5165  
5166 #ifdef ANSI
5167 S16 SRemDBufPst
5168 (
5169 Buffer *mBuf,                   /* message buffer */
5170 Buffer **dBufPtr
5171 )
5172 #else
5173 S16 SRemDBufPst(mBuf, dBufPtr)
5174 Buffer *mBuf;                   /* message buffer */
5175 Buffer **dBufPtr;               /* pointer to data buffer */
5176 #endif
5177 {
5178    SsMsgInfo *minfo;
5179    Buffer *tmp;
5180
5181 #if (ERRCLASS & ERRCLS_INT_PAR)
5182    /* check buffer pointer */
5183    if (!dBufPtr)
5184    {
5185       SSLOGERROR(ERRCLS_INT_PAR, ESS171, ERRZERO, "SRemDBufPst : Null Buffer");
5186       return RFAILED;
5187    }
5188    /* check message */
5189    if (!mBuf)
5190    {
5191       SSLOGERROR(ERRCLS_INT_PAR, ESS172, ERRZERO, "SRemDBufPst : Null Buffer");
5192       return RFAILED;
5193    }
5194    if (mBuf->b_datap->db_type != SS_M_PROTO)
5195    {
5196       SSLOGERROR(ERRCLS_INT_PAR, ESS173, ERRZERO, "SRemDBufPst : Incorrect\
5197                                                    buffer type");
5198       return RFAILED;
5199    }
5200 #endif
5201  
5202    minfo = (SsMsgInfo*) (mBuf)->b_rptr;
5203  
5204    /* no data blk, return */
5205    if ((*dBufPtr = minfo->endptr) == NULLP)
5206    {
5207       return ROKDNA;
5208    }
5209    for (tmp = mBuf; tmp->b_cont != (*dBufPtr); )
5210       tmp = tmp->b_cont;
5211
5212    tmp->b_cont = NULLP;
5213
5214    /* update SsMsgInfo */
5215    if (!(minfo->len -= (*dBufPtr)->b_wptr - (*dBufPtr)->b_rptr))
5216       minfo->endptr = NULLP;
5217    else
5218       minfo->endptr = tmp;
5219
5220    return ROK;
5221 }
5222
5223 /*
5224 *
5225 *       Fun:   SInitNxtDBuf
5226 *
5227 *       Desc:  Initialize next Data Buffer Id
5228 *
5229 *       Ret:   ROK/RFAILED
5230 *
5231 *       Notes: Must be called prior to SGetNxtDBuf
5232 *
5233 *       File:  ss_msg.c
5234 *
5235 */
5236
5237  
5238 #ifdef ANSI
5239 S16 SInitNxtDBuf
5240 (
5241 Buffer *mBuf
5242 )
5243 #else
5244 S16 SInitNxtDBuf(mBuf)
5245 Buffer *mBuf;                   /* message buffer */
5246 #endif
5247 {
5248    SsMsgInfo *minfo;
5249
5250 #if (ERRCLASS & ERRCLS_INT_PAR)
5251    if (!mBuf)
5252    {
5253       SSLOGERROR(ERRCLS_INT_PAR, ESS174, ERRZERO, "SInitNxtDBuf : Null Buffer");
5254       return RFAILED;
5255    }
5256    if (mBuf->b_datap->db_type != SS_M_PROTO)
5257    {
5258       SSLOGERROR(ERRCLS_INT_PAR, ESS175, ERRZERO, "SInitNxtDBuf : Incorrect\
5259                                                    buffer type");
5260       return RFAILED;
5261    }
5262 #endif
5263
5264    minfo = (SsMsgInfo*) mBuf->b_rptr;
5265
5266    /* set the next ptr of mBuf to point to the first SS_M_DATA blk */
5267    minfo->next = mBuf->b_cont;
5268
5269    return ROK;
5270 }
5271
5272 /*
5273 *
5274 *       Fun:   SGetNxtDBuf
5275 *
5276 *       Desc:  Get next dBuf in message chain
5277 *
5278 *       Ret:   ROK/RFAILED
5279 *
5280 *       Notes: Must be called after SInitNxtDBuf
5281 *
5282 *       File:  ss_msg.c
5283 *
5284 */
5285
5286  
5287 #ifdef ANSI
5288 S16 SGetNxtDBuf
5289 (
5290 Buffer *mBuf,                   /* message buffer */
5291 Buffer **dBuf
5292 )
5293 #else
5294 S16 SGetNxtDBuf(mBuf, dBuf)
5295 Buffer *mBuf;                   /* message buffer */
5296 Buffer **dBuf;                  /* data buffer return */
5297 #endif
5298 {
5299    SsMsgInfo *minfo;
5300
5301 #if (ERRCLASS & ERRCLS_INT_PAR)
5302    if (!mBuf)
5303    {
5304       SSLOGERROR(ERRCLS_INT_PAR, ESS176, ERRZERO, "SGetNxtDBuf : Null Buffer");
5305       return RFAILED;
5306    }
5307    if (!dBuf)
5308    {
5309       SSLOGERROR(ERRCLS_INT_PAR, ESS177, ERRZERO, "SGetNxtDBuf : Null Buffer");
5310       return RFAILED;
5311    }
5312    if (mBuf->b_datap->db_type != SS_M_PROTO)
5313    {
5314       SSLOGERROR(ERRCLS_INT_PAR, ESS178, ERRZERO, "SGetNxtDBuf : Incorrect\
5315                                                    buffer type");
5316       return RFAILED;
5317    }
5318 #endif
5319
5320    minfo = (SsMsgInfo*) (mBuf)->b_rptr;
5321
5322    /* if next ptr of mBuf is NULLP, return */
5323    if ((*dBuf = minfo->next) == NULLP)
5324       return (ROKDNA);
5325
5326    /* update next */
5327    minfo->next = (*dBuf)->b_cont;
5328
5329    return ROK;
5330 }
5331
5332 /*
5333 *
5334 *       Fun:   SChkNxtDBuf
5335 *
5336 *       Desc:  check if next data buffer exists.
5337 *
5338 *       Ret:   ROK/ROKDNA/RFAILED
5339 *
5340 *       Notes: doesn't modify nxtDBuf
5341 *
5342 *       File:  ss_msg.c
5343 *
5344 */
5345
5346 #ifdef ANSI
5347 S16 SChkNxtDBuf
5348 (
5349 Buffer *mBuf
5350 )
5351 #else
5352 S16 SChkNxtDBuf(mBuf)
5353 Buffer *mBuf;                   /* message buffer */
5354 #endif
5355 {
5356    SsMsgInfo *minfo;
5357
5358 #if (ERRCLASS & ERRCLS_INT_PAR)
5359    if (!mBuf)
5360    {
5361       SSLOGERROR(ERRCLS_INT_PAR, ESS179, ERRZERO, "SChkNxtDBuf : Null Buffer");
5362       return RFAILED;
5363    }
5364    if (mBuf->b_datap->db_type != SS_M_PROTO)
5365    {
5366       SSLOGERROR(ERRCLS_INT_PAR, ESS180, ERRZERO, "SChkNxtDBuf : Incorrect\
5367                                                    buffer type");
5368       return RFAILED;
5369    }
5370 #endif /* ERRCLASS */
5371
5372    minfo = (SsMsgInfo*) (mBuf)->b_rptr;
5373
5374    /* if next is valid, return ROK */
5375    if (minfo->next)
5376       return ROK;
5377    else
5378       return (ROKDNA);
5379 }
5380
5381 /*
5382 *
5383 *       Fun:   SGetDataRx
5384 *
5385 *       Desc:  Given a data buffer, return a pointer to the
5386 *              data payload, and the length of the payload
5387 *
5388 *
5389 *       Ret:   ROK ok
5390 *              RFAILED error
5391 *
5392 *       Notes: This assumes an uninitialized dBuf
5393 *
5394 *       File:  ss_msg.c
5395 *
5396 */
5397
5398  
5399 #ifdef ANSI
5400 S16 SGetDataRx
5401 (
5402 Buffer *dBuf,                   /* data buffer */
5403 MsgLen pad,                     /* pad */
5404 Data **retDatPtr,               /* return data pointer */
5405 MsgLen *retDatLen
5406 )
5407 #else
5408 S16 SGetDataRx(dBuf, pad, retDatPtr, retDatLen)
5409 Buffer *dBuf;                   /* data buffer */
5410 MsgLen pad;                     /* pad */
5411 Data **retDatPtr;               /* return data pointer */
5412 MsgLen *retDatLen;              /* return data length */
5413 #endif
5414 {
5415
5416 #if (ERRCLASS & ERRCLS_INT_PAR)
5417    if (!dBuf)
5418    {
5419       SSLOGERROR(ERRCLS_INT_PAR, ESS181, ERRZERO, "SGetDataRx : Null Buffer");
5420       return RFAILED;
5421    }
5422    if (!retDatLen || (pad < 0))
5423    {
5424       SSLOGERROR(ERRCLS_INT_PAR, ESS182, ERRZERO, "SGetDataRx : Null Buffer");
5425       return RFAILED;
5426    }
5427    if (!retDatPtr)
5428    {
5429       SSLOGERROR(ERRCLS_INT_PAR, ESS183, ERRZERO, "SGetDataRx : Null Buffer");
5430       return RFAILED;
5431    }
5432    if (dBuf->b_datap->db_type != SS_M_DATA)
5433    {
5434       SSLOGERROR(ERRCLS_INT_PAR, ESS184, ERRZERO, "SGetDataRx : Incorrect\
5435                                                    buffer type");
5436       return RFAILED;
5437    }
5438 #endif /* ERRCLASS */
5439
5440    if (dBuf->b_datap->db_ref > 1)   /* cannot write to a shared buffer */
5441    {
5442 #if (ERRCLASS & ERRCLS_DEBUG)
5443       SSLOGERROR(ERRCLS_DEBUG, ESS185, ERRZERO, "SGetDataRx : Reference\
5444                                                  count > 1");
5445 #endif
5446       return RFAILED;
5447    }
5448
5449    /* return the write ptr loc(with padding) if there is data to write to */
5450    if ((*retDatLen = (dBuf->b_datap->db_lim - dBuf->b_wptr - pad)) > 0)
5451       *retDatPtr = dBuf->b_wptr + pad;
5452    else
5453    {
5454       *retDatPtr = NULLP;
5455 #if (ERRCLASS & ERRCLS_DEBUG)
5456       SSLOGERROR(ERRCLS_DEBUG, ESS186, ERRZERO, "SGetDataRx:No data available");
5457 #endif
5458       return RFAILED;
5459    }
5460
5461    return ROK;
5462 }
5463
5464 /*
5465 *
5466 *       Fun:   SGetDataTx
5467 *
5468 *       Desc:  Given a data buffer, return a pointer to the
5469 *              data payload, and the length of the payload
5470 *
5471 *
5472 *       Ret:   ROK ok
5473 *              RFAILED error
5474 *
5475 *       Notes: This assumes an initialized dBuf
5476 *
5477 *       File:  ss_msg.c
5478 *
5479 */
5480
5481  
5482 #ifdef ANSI
5483 S16 SGetDataTx
5484 (
5485 Buffer *dBuf,                   /* data buffer */
5486 Data **retDatPtr,               /* return data pointer */
5487 MsgLen *retDatLen               /* return data length */
5488 )
5489 #else
5490 S16 SGetDataTx(dBuf, retDatPtr, retDatLen)
5491 Buffer *dBuf;                   /* data buffer */
5492 Data **retDatPtr;               /* return data pointer */
5493 MsgLen *retDatLen;              /* return data length */
5494 #endif
5495 {
5496
5497 #if (ERRCLASS & ERRCLS_INT_PAR)
5498    /* ss021.103 - Modification to check parameters */
5499    if (!retDatPtr)
5500    {
5501       SSLOGERROR(ERRCLS_INT_PAR, ESS187, ERRZERO, "SGetDataTx : Null Buffer");
5502       return RFAILED;
5503    }
5504    if (!dBuf)
5505    {
5506       SSLOGERROR(ERRCLS_INT_PAR, ESS188, ERRZERO, "SGetDataTx : Null Buffer");
5507       *retDatPtr = (Data *)NULLP;
5508       return RFAILED;
5509    }
5510    if (!retDatLen)
5511    {
5512       SSLOGERROR(ERRCLS_INT_PAR, ESS189, ERRZERO, "SGetDataTx : Null Buffer");
5513       return RFAILED;
5514    }
5515    if (dBuf->b_datap->db_type != SS_M_DATA)
5516    {
5517       SSLOGERROR(ERRCLS_INT_PAR, ESS190, ERRZERO, "SGetDataTx : Incorrect\
5518                                                    buffer type");
5519       *retDatPtr = (Data *)NULLP;
5520       return RFAILED;
5521    }
5522 #endif /* ERRCLASS */
5523
5524    /* if there is data, return read ptr */
5525    if ((*retDatLen = dBuf->b_wptr - dBuf->b_rptr))
5526       *retDatPtr = dBuf->b_rptr;
5527    else
5528    {
5529       *retDatPtr = NULLP;
5530 #if (ERRCLASS & ERRCLS_DEBUG)
5531       SSLOGERROR(ERRCLS_DEBUG, ESS191, ERRZERO, "SGetDataTx : Buffer empty");
5532 #endif
5533       return RFAILED;
5534    }
5535
5536    return ROK;
5537 }
5538
5539 #ifndef SS_ENABLE_MACROS
5540 \f
5541 /*
5542 *
5543 *       Fun:   SGetBufRegionPool
5544 *
5545 *       Desc:  returns the region and pool of the message buffer
5546 *
5547 *       Ret:   ROK on success
5548 *              RFAILED on error
5549 *
5550 *       Notes: None
5551 *
5552 *       File:  ss_msg.c
5553 *
5554 */
5555 #ifdef ANSI
5556 S16 SGetBufRegionPool
5557 (
5558 Buffer *mBuf,                   /* message buffer */
5559 Region *region,                 /* region */
5560 Pool   *pool                    /* pool */
5561 )
5562 #else
5563 S16 SGetBufRegionPool(mBuf, region, pool)
5564 Buffer *mBuf;                   /* message buffer */
5565 Region *region;                 /* region */
5566 Pool   *pool;                   /* pool */
5567 #endif
5568 {
5569    SsMsgInfo *mInfo;            /* message info pointer */
5570
5571
5572 #if (ERRCLASS & ERRCLS_INT_PAR)
5573
5574    if (mBuf == NULLP)
5575    {
5576       SSLOGERROR(ERRCLS_INT_PAR, ESS192, ERRZERO, 
5577                  "SGetBufRegionPool : Null Buffer");
5578       return RFAILED;
5579    }
5580    if ((region == NULLP) && (pool == NULLP))
5581    {
5582       SSLOGERROR(ERRCLS_INT_PAR, ESS193, ERRZERO, 
5583                  "SGetBufRegionPool : Null region and pool pointers");
5584       return RFAILED;
5585    }
5586    if (mBuf->b_datap->db_type != SS_M_PROTO)
5587    {
5588       SSLOGERROR(ERRCLS_INT_PAR, ESS194, ERRZERO, 
5589                  "SUpdMsg : Incorrect buffer type");
5590       return RFAILED;
5591    }
5592 #endif /* (ERRCLASS & ERRCLS_INT_PAR */
5593
5594    /* get message info of mBuf */
5595    if ((mInfo = (SsMsgInfo *) mBuf->b_rptr) == NULLP)
5596    {
5597 #if (ERRCLASS & ERRCLS_DEBUG)
5598       SSLOGERROR(ERRCLS_DEBUG, ESS195, ERRZERO, 
5599                  "SGetBufRegionPool : mBuf's control data is null");
5600 #endif
5601       return RFAILED;
5602    }
5603
5604    if (region != NULLP)
5605       *region = mInfo->region;
5606    if (pool != NULLP)
5607       *pool   = mInfo->pool;
5608
5609    return ROK;
5610 } /* end of SGetBufRegionPool */
5611
5612 #endif /* SS_ENABLE_MACROS */
5613 \f
5614 /*
5615 *
5616 *       Fun:   SCompressMsg
5617 *
5618 *       Desc:  This function is used to compress a message into
5619 *              the minimum number of data buffers needed.
5620 *
5621 *       Ret:   ROK      - ok
5622 *              RFAILED  - failed, general (optional)
5623 *              ROUTRES  - failed, out of resources (optional)
5624 *
5625 *       Notes: None
5626 *
5627 *       File:  ss_msg.c
5628 *
5629 */
5630
5631  
5632 #ifdef ANSI
5633 S16 SCompressMsg
5634 (
5635 Buffer *mBuf
5636 )
5637 #else
5638 S16 SCompressMsg(mBuf)
5639 Buffer *mBuf;                    /* message buffer */
5640 #endif
5641 {
5642    SsMsgInfo *minfo;
5643    MsgLen numBytes;
5644    Buffer *dBuf;
5645    Buffer *tmp;
5646    Buffer *next;
5647    S16 ret;
5648
5649 #ifdef T2K_MEM_LEAK_DBG
5650    char* file = __FILE__;
5651    uint32_t line = __LINE__;
5652 #endif
5653
5654 #if (ERRCLASS & ERRCLS_INT_PAR)
5655    if (!mBuf)
5656    {
5657       SSLOGERROR(ERRCLS_INT_PAR, ESS196, ERRZERO, "SCompressMsg : Null Buffer");
5658       return RFAILED;
5659    }
5660    if (mBuf->b_datap->db_type != SS_M_PROTO)
5661    {
5662       SSLOGERROR(ERRCLS_INT_PAR, ESS197, ERRZERO, "SCompressMsg : Incorrect\
5663                                                    buffer type");
5664       return RFAILED;
5665    }
5666 #endif
5667
5668    minfo = (SsMsgInfo*) (mBuf)->b_rptr;
5669
5670    if ((tmp = mBuf->b_cont) == minfo->endptr)
5671    {
5672       return ROK;
5673    }
5674
5675    /* allocate a data buffer of size bytes*/
5676    if ((ret = ssGetDBufOfSize(minfo->region, minfo->len, &dBuf)) != ROK)
5677    {
5678       SSLOGERROR(ERRCLS_DEBUG, ESS198, ERRZERO, "SAddMsgRef : ssGetDBufOfSize\
5679                  failed");
5680       return RFAILED;
5681    }
5682    dBuf->b_datap->db_type = SS_M_DATA;
5683
5684    while (tmp)
5685    {
5686       /* determine number of bytes to be copied */
5687       numBytes = tmp->b_wptr - tmp->b_rptr;
5688
5689       /* copy data */
5690
5691 /* ss002.13: addition */
5692       /* ss003.13: addition */
5693       SMemCpy( (Void *)dBuf->b_wptr, (Void *)tmp->b_rptr, (size_t)numBytes);
5694
5695       dBuf->b_wptr += numBytes;
5696       tmp->b_rptr += numBytes;
5697
5698
5699       next = tmp->b_cont;
5700       (Void) SPutDBuf(minfo->region, minfo->pool, tmp);
5701       tmp = next;
5702    }
5703    /* update mBuf and SsMsgInfo */
5704    mBuf->b_cont = dBuf;
5705    minfo->endptr = dBuf;
5706
5707    return ROK;
5708 }
5709
5710 /*
5711 *
5712 *       Fun:   SPrntMsg
5713 *
5714 *       Desc:  This function prints the contents of a message. The
5715 *              following information is printed: queue length,
5716 *              message length, direction, hexadecimal and ASCII
5717 *              (if appropriate) values of all bytes in the message.
5718 *
5719 *              This function should be used for debugging only.
5720 *
5721 *       Ret:   ROK      - ok
5722 *
5723 *       Notes: None
5724 *
5725 *       File:  ss_msg.c
5726 *
5727 */
5728   
5729 #ifdef ANSI
5730 S16 SPrntMsg
5731 (
5732 Buffer *mBuf,               /* message buffer */
5733 S16 src,                    /* source id */
5734 S16 dst                     /* destination id */
5735 )
5736 #else
5737 S16 SPrntMsg(mBuf, src, dst)
5738 Buffer *mBuf;               /* message buffer */
5739 S16 src;                    /* source id */
5740 S16 dst;                    /* destination id */
5741 #endif
5742 {
5743    QLen qlen;               /* queue length */
5744         MsgLen mlen;             /* message length */
5745 /*
5746  * SDeRegTTsk patch
5747  */
5748         /* ss038.103 : 102061 Changed to MsgLen from S16 */
5749         MsgLen i;                /* counter */
5750         S16 j;                   /* counter */
5751    S16 k;                   /* counter */
5752    uint8_t data;                 /* data */
5753    uint8_t tdata[16] = {0};            /* temporary data */
5754    S8 prntBuf[256];         /* print buffer */
5755    Buffer *tmp;             /* buffer ptr */
5756    Data *cptr;
5757 /* ss012.13: Addition */
5758    Data reg;
5759
5760
5761
5762    if (mBuf == NULLP)
5763    {
5764       sprintf(prntBuf,"\nmsg: empty\n");
5765       SPrint(prntBuf);
5766       SPrint( (S8*)"\n\n");
5767       return ROK;
5768    }
5769
5770    for (qlen = 0, tmp = mBuf->b_cont; tmp; qlen++)
5771       tmp = tmp->b_cont;
5772    mlen = ((SsMsgInfo*)(mBuf->b_rptr))->len;
5773    reg = ((SsMsgInfo*)(mBuf->b_rptr))->region;
5774    /*ss013.301: Fixed Warnings for 32/64 bit compilation*/
5775    sprintf(prntBuf,"\nmsg: qlen: %04d mlen: %04d   %02d-->%02d region: %02d\n",
5776            (uint16_t)qlen,(uint16_t)mlen,src,dst,reg);
5777    SPrint( prntBuf);
5778 #ifdef XEON_SPECIFIC_CHANGES
5779    printf("%s\n", prntBuf);
5780 #endif   
5781    SPrint((S8*) "dat: ");
5782 #ifdef XEON_SPECIFIC_CHANGES
5783    printf("\ndat: ");
5784 #endif   
5785
5786    if (mlen == 0)
5787    {
5788       sprintf(prntBuf," empty\n");
5789       SPrint(prntBuf);
5790       SPrint( (S8*)"\n\n");
5791       return ROK;
5792    }
5793    tmp = mBuf->b_cont;
5794    cptr = tmp->b_rptr;
5795    data= *cptr++; 
5796    i = 0;
5797    while( i < mlen  ) 
5798    {
5799       j = 0;
5800       for( j = 0; j < 16; j++)
5801       {
5802          if( i < mlen )
5803          { 
5804             /* print hex */
5805             tdata[j]=data;
5806             sprintf( prntBuf,"%02x ",(uint16_t) data);
5807             SPrint( prntBuf);
5808 #ifdef XEON_SPECIFIC_CHANGES
5809    printf("%s\n", prntBuf);
5810 #endif   
5811             if (cptr == tmp->b_wptr)
5812             {
5813                tmp = tmp->b_cont;
5814                if (tmp)
5815                   cptr = tmp->b_rptr;
5816             }
5817        /* ss024.103 - Modification to fix bug */
5818             i++;
5819        if ( i < mlen )
5820                data = *cptr++;
5821          }
5822          else
5823          {
5824             sprintf( prntBuf,"   ");
5825             SPrint( prntBuf );
5826 #ifdef XEON_SPECIFIC_CHANGES
5827    printf("%s\n", prntBuf);
5828 #endif   
5829          }
5830       } 
5831       for (k = 0; k < 16; k++)
5832       {
5833          if( (i >= mlen) && (k>=(mlen%16)) && mlen%16 != 0)
5834             break;
5835          if (AIsAscii(tdata[k]))
5836          {
5837             /* print character if printable */
5838             sprintf(prntBuf,"%c",tdata[k]);
5839             SPrint( prntBuf);
5840 #ifdef XEON_SPECIFIC_CHANGES
5841    printf("%s\n", prntBuf);
5842 #endif   
5843          }
5844          else
5845          {
5846             /* print . if non printable */
5847             SPrint((S8*) ".");
5848 #ifdef XEON_SPECIFIC_CHANGES
5849    printf(".");
5850 #endif   
5851          }
5852       }
5853       sprintf(prntBuf,"\n     ");
5854       SPrint(prntBuf);
5855 #ifdef XEON_SPECIFIC_CHANGES
5856    printf("%s\n", prntBuf);
5857 #endif   
5858    }
5859    return ROK;
5860
5861 } /* end of SPrntMsg */
5862
5863
5864
5865 /*
5866 *
5867 *       Fun:   SGetPstMsgMult
5868 *
5869 *       Desc:  This function allocates consecutive bytes of data at the
5870 *              end of a message.
5871 *
5872 *       Ret:   ROK      - ok
5873 *              RFAILED  - failed, general (optional)
5874 *              ROUTRES  - failed, out of resources (optional)
5875 *
5876 *       Notes: if message is empty: message length is incremented.
5877 *              return is ok.
5878 *
5879 *              if message is not empty: space is allocated in back of
5880 *              all other data in message.  message length is incremented.
5881 *              return is ok.
5882 *
5883 *       File:  ss_msg.c
5884 *
5885 */
5886 #ifdef ANSI
5887 S16 SGetPstMsgMult
5888 (
5889 MsgLen cnt,                 /* count */
5890 Buffer *mBuf                /* message buffer */
5891 )
5892 #else
5893 S16 SGetPstMsgMult(cnt, mBuf)
5894 MsgLen cnt;                 /* count */
5895 Buffer *mBuf;               /* message buffer */
5896 #endif
5897 {
5898    Buffer *tmp;
5899    Buffer *newb;
5900    SsMsgInfo *minfo;
5901    MsgLen numBytes;
5902    MsgLen avail;
5903
5904
5905 #if ( ERRCLASS & ERRCLS_INT_PAR )
5906    /* check message buffer */
5907    if (mBuf == NULLP)
5908    {
5909       return RFAILED;
5910    }
5911    /* check count */
5912    if (cnt <= 0)
5913    {
5914       return RFAILED;
5915    }
5916    if (mBuf->b_datap->db_type != SS_M_PROTO)
5917    {
5918       return RFAILED;
5919    }
5920 #endif
5921    /* get the message info */
5922    minfo = (SsMsgInfo *) (mBuf->b_rptr);
5923
5924    /* init avail */
5925    avail = 0;
5926
5927    if ((tmp = minfo->endptr) && (tmp->b_datap->db_ref == 1) &&
5928        ((avail = tmp->b_datap->db_lim - tmp->b_wptr)))
5929    {
5930       numBytes = MIN(cnt, avail);
5931
5932       cnt -= numBytes;
5933       minfo->len += numBytes;
5934
5935
5936 /* ss002.13 addition */
5937 /* ss003.13 addition */
5938       SMemSet( (Void *)tmp->b_wptr, (Data)'\0', (size_t)numBytes);
5939
5940       tmp->b_wptr += numBytes;
5941       if (!cnt)
5942          return ROK;
5943    }
5944    if (ssGetDBufOfSize(minfo->region, cnt, &newb) != ROK)
5945    {
5946       /* ss027.103 - Modification to fix bug in SGetPstMsgMult */
5947       if ((avail) && (tmp))
5948          tmp->b_wptr = tmp->b_datap->db_lim - avail;
5949       minfo->len -= avail;
5950       return (ROUTRES);
5951    }
5952
5953    minfo->len += cnt;
5954
5955
5956 /* ss002.13: addition */
5957    /* ss003.13: addition */
5958    SMemSet( (Void *)newb->b_wptr, (Data)'\0', (size_t)cnt);
5959
5960    newb->b_wptr += cnt;
5961
5962
5963    if (tmp)
5964       tmp->b_cont = newb;
5965    else
5966       mBuf->b_cont = newb;
5967
5968    minfo->endptr = newb;
5969
5970    return ROK;
5971 }
5972
5973 /*
5974 *
5975 *       Fun:   SChkMsg
5976 *
5977 *       Desc:  Check Message
5978 *
5979 *       Ret:   ROK on success
5980 *              RFAILED on error
5981 *
5982 *       Notes: Check that the first buffer in a message
5983 *              contains at least two bytes. This check is required
5984 *              by 68302/68360 processors to insure accurate fisu
5985 *              generation.
5986 *
5987 *       File:  ss_msg.c
5988 *
5989 */
5990 #ifdef ANSI
5991 S16 SChkMsg
5992 (
5993 Buffer *mBuf
5994 )
5995 #else
5996 S16 SChkMsg(mBuf)
5997 Buffer *mBuf;
5998 #endif
5999 {
6000    SsMsgInfo *minfo;
6001    Buffer *tmp;
6002
6003  
6004 #if ( ERRCLASS & ERRCLS_INT_PAR )
6005    /* check message buffer */
6006    if (mBuf == NULLP)
6007    {
6008       return RFAILED;
6009    }
6010    if (mBuf->b_datap->db_type != SS_M_PROTO)
6011    {
6012       return RFAILED;
6013    }
6014 #endif
6015    /* get the message info */
6016    minfo = (SsMsgInfo *) (mBuf->b_rptr);
6017
6018    if (minfo->len < 2)
6019       return RFAILED;
6020
6021    /* get the first M_DATA blk*/
6022    tmp = mBuf->b_cont;
6023
6024    if (((tmp->b_rptr + 2) <= tmp->b_wptr) || (SCompressMsg(mBuf) == ROK))
6025       return ROK;
6026
6027    return RFAILED;
6028 }
6029
6030 /*
6031 *
6032 *       Fun:   SAlignDBufEven
6033 *
6034 *       Desc:  align data portion of a data buffer on an even
6035 *              byte boundary.
6036 *
6037 *       Ret:   ROK/RFAILED
6038 *
6039 *       Notes: required for SS7 microcode on the 68302
6040 *
6041 *       File:  ss_msg.c
6042 *
6043 */
6044 #ifdef ANSI
6045 S16 SAlignDBufEven
6046 (
6047 Buffer *dBuf                      /* data buffer */
6048 )
6049 #else
6050 S16 SAlignDBufEven(dBuf)
6051 Buffer *dBuf;                   /* data buffer  */
6052 #endif
6053 {
6054    MsgLen len;
6055    Data *src;
6056
6057 #if (ERRCLASS & ERRCLS_INT_PAR)
6058    if (!dBuf)
6059    {
6060       SSLOGERROR(ERRCLS_INT_PAR, ESS199, ERRZERO, "SAlignDBufEven : Null\
6061                  Buffer");
6062       return RFAILED;
6063    }
6064    if (dBuf->b_datap->db_type != SS_M_DATA)
6065    {
6066       SSLOGERROR(ERRCLS_INT_PAR, ESS200, ERRZERO, "SAlignDBufEven : Incorrect\
6067                                                    buffer type");
6068       return RFAILED;
6069    }
6070 #endif
6071
6072    src = dBuf->b_rptr;
6073 /* ss008.13: addition */
6074    if (!((PTR)src % (PTR)2))
6075       return ROK;
6076
6077    if (dBuf->b_datap->db_ref > 1)
6078       return RFAILED;
6079
6080    len = dBuf->b_wptr - dBuf->b_rptr;
6081
6082    if (dBuf->b_datap->db_base < dBuf->b_rptr)
6083    {
6084       dBuf->b_wptr = --dBuf->b_rptr;
6085       while (len--)
6086          *dBuf->b_wptr++ = *src++;
6087    }
6088    else
6089       if (dBuf->b_datap->db_lim > dBuf->b_wptr)
6090       {
6091          src = dBuf->b_wptr - 1;
6092          dBuf->b_rptr = ++dBuf->b_wptr;
6093          while (len--)
6094             *--dBuf->b_rptr = *src--;
6095       }
6096       else
6097          return RFAILED;
6098
6099    return ROK;
6100 }
6101
6102 /* ss004.13: addition */
6103 /*
6104 *
6105 *       Fun:   SAlignDBuf
6106 *
6107 *       Desc:  Align data portion of a data buffer on the specified 
6108 *              boundary. No restriction is imposed on the alignment.
6109 *
6110 *       Ret:   ROK/RFAILED
6111 *
6112 *       Notes: required by drivers (68360, 860)
6113 *
6114 *       File:  ss_msg.c
6115 *
6116 */
6117 #ifdef ANSI
6118 S16 SAlignDBuf
6119 (
6120 Buffer *dBuf,                      /* data buffer */
6121 uint32_t    align                       /* alignemnt required */
6122 )
6123 #else
6124 S16 SAlignDBuf(dBuf, align)
6125 Buffer *dBuf;                      /* data buffer  */
6126 uint32_t    align;                      /* alignemnt required */
6127 #endif
6128 {
6129    MsgLen len;
6130    Data   *src;
6131    uint32_t    upShift;                 /* no. of bytes to be shifted up */
6132    uint32_t    downShift;               /* no. of bytes to be shifted down */
6133
6134 #if (ERRCLASS & ERRCLS_INT_PAR)
6135    if (!dBuf)
6136    {
6137       SSLOGERROR(ERRCLS_INT_PAR, ESS201, ERRZERO, "SAlignDBuf: Null\
6138                  Buffer");
6139       return RFAILED;
6140    }
6141    if (dBuf->b_datap->db_type != SS_M_DATA)
6142    {
6143       SSLOGERROR(ERRCLS_INT_PAR, ESS202, ERRZERO, "SAlignDBuf: Incorrect\
6144                                                    buffer type");
6145       return RFAILED;
6146    }
6147 #endif
6148
6149    src = dBuf->b_rptr;
6150 /* ss008.13: addition */
6151    upShift = (PTR)src % (PTR)align; /* no. of bytes by which to shift up
6152                                      * the read and write pointers */
6153
6154    if (!upShift)
6155       return ROK;
6156
6157    if (dBuf->b_datap->db_ref > 1)
6158       return RFAILED;
6159
6160    downShift = align - upShift;   /* no of bytes by which to shift down
6161                                    * the read and write pointers */
6162    len = dBuf->b_wptr - dBuf->b_rptr;
6163
6164    if (dBuf->b_datap->db_base <= (dBuf->b_rptr - upShift))
6165    {
6166       /* there is space to shift up the read and write pointers */
6167
6168       dBuf->b_rptr -= upShift;   /* align the read pointer */
6169       dBuf->b_wptr = dBuf->b_rptr;
6170       SMemCpy( (Void *) dBuf->b_wptr, (Void *) src,  (size_t) len);
6171       dBuf->b_wptr = dBuf->b_wptr + len;
6172    }
6173    else
6174    {
6175       if (dBuf->b_datap->db_lim > dBuf->b_wptr + downShift)
6176       {
6177          src = dBuf->b_wptr - 1;
6178          dBuf->b_wptr = dBuf->b_wptr + downShift;
6179          dBuf->b_rptr = dBuf->b_wptr;
6180          while (len--)
6181             *--dBuf->b_rptr = *src--;
6182       }
6183       else
6184          return RFAILED;
6185    }
6186
6187    return ROK;
6188 }
6189
6190 \f
6191 /*
6192 *
6193 *       Fun:   SGetSMem
6194 *
6195 *       Desc:  Allocates a static buffer pool within the specified
6196 *              memory region.
6197 *
6198 *       Ret:   ROK      - ok
6199 *              RFAILED  - failed, general (optional)
6200 *
6201 *       Notes: The new memory management scheme makes this function
6202 *              meaningless. It merely sets the pool ID to zero and
6203 *              returns.
6204 *
6205 *       File:  ss_msg.c
6206 *
6207 */
6208 #ifdef ANSI
6209 S16 SGetSMem
6210 (
6211 Region region,                  /* region ID */
6212 Size size,                      /* size */
6213 Pool *pool                      /* pointer to pool ID */
6214 )
6215 #else
6216 S16 SGetSMem(region, size, pool)
6217 Region region;                  /* region ID */
6218 Size size;                      /* size */
6219 Pool *pool;                     /* pointer to pool ID */
6220 #endif
6221 {
6222
6223    UNUSED(size);
6224
6225
6226 #if (ERRCLASS & ERRCLS_INT_PAR)
6227    /* validate region ID */
6228    if (region >= SS_MAX_REGS)
6229    {
6230       SSLOGERROR(ERRCLS_INT_PAR, ESS203, region, "Invalid region");
6231       return RFAILED;
6232    }
6233
6234    /* validate pointer to pool ID */
6235    if (pool == NULLP)
6236    {
6237       SSLOGERROR(ERRCLS_INT_PAR, ESS204, region, "Null pointer");
6238       return RFAILED;
6239    }
6240 #endif
6241
6242
6243    /* set the pool ID to zero and return success */
6244    *pool = 0;
6245
6246
6247    return ROK;
6248 }
6249
6250 \f
6251 /*
6252 *
6253 *       Fun:   SPutSMem
6254 *
6255 *       Desc:  Deallocates a static buffer pool within the specified
6256 *              memory region.
6257 *
6258 *       Ret:   ROK      - ok
6259 *              RFAILED  - failed, general (optional)
6260 *
6261 *       Notes: The new memory management scheme makes this function
6262 *              meaningless. It does nothing.
6263 *
6264 *       File:  ss_msg.c
6265 *
6266 */
6267 #ifdef ANSI
6268 S16 SPutSMem
6269 (
6270 Region region,                  /* region ID */
6271 Pool pool                       /* pool ID */
6272 )
6273 #else
6274 S16 SPutSMem(region, pool)
6275 Region region;                  /* region ID */
6276 Pool pool;                      /* pool ID */
6277 #endif
6278 {
6279    /* ss021.103 - Addition of return value */
6280 #if (ERRCLASS & ERRCLS_INT_PAR)
6281    S16   ret;
6282 #endif
6283
6284
6285 #if (ERRCLASS & ERRCLS_INT_PAR)
6286    /* validate region ID */
6287    if (region >= SS_MAX_REGS)
6288    {
6289       SSLOGERROR(ERRCLS_INT_PAR, ESS205, region, "Invalid region");
6290       return RFAILED;
6291    }
6292
6293    /* validate pool ID */
6294    if (pool >= SS_MAX_POOLS_PER_REG)
6295    {
6296       SSLOGERROR(ERRCLS_INT_PAR, ESS206, region, "Invalid pool");
6297       return RFAILED;
6298    }
6299
6300    /* ss021.103 - Addition to check if region is registered */
6301    /* acquire one semaphore, to protect against deregistration */
6302    SS_ACQUIRE_SEMA(&osCp.regionTblSem, ret);
6303    if (ret != ROK)
6304    {
6305
6306 #if (ERRCLASS & ERRCLS_DEBUG)
6307       SSLOGERROR(ERRCLS_DEBUG, ESS207, (ErrVal) ret,
6308                   "Could not lock region table");
6309 #endif
6310
6311       return RFAILED;
6312    }
6313
6314
6315 #if (ERRCLASS & ERRCLS_INT_PAR)
6316    /* verify that this region is present */
6317    if (osCp.regionTbl[region].used == FALSE)
6318    {
6319       if( SS_RELEASE_SEMA(&osCp.regionTblSem) != ROK)
6320       {
6321 #if (ERRCLASS & ERRCLS_DEBUG)
6322          SSLOGERROR(ERRCLS_DEBUG, ESS208, ERRZERO,
6323                   "Could not release semaphore");
6324          return RFAILED;
6325 #endif
6326       }
6327
6328       SSLOGERROR(ERRCLS_INT_PAR, ESS209, region, "Region not registered");
6329       return RFAILED;
6330    }
6331 #endif
6332
6333    if( SS_RELEASE_SEMA(&osCp.regionTblSem) != ROK)
6334    {
6335 #if (ERRCLASS & ERRCLS_DEBUG)
6336       SSLOGERROR(ERRCLS_DEBUG, ESS210, ERRZERO,
6337                   "Could not release semaphore");
6338       return RFAILED;
6339 #endif
6340    }
6341 #endif
6342
6343    return ROK;
6344 }
6345
6346 \f
6347 /*
6348 *
6349 *       Fun:   SChkRes
6350 *
6351 *       Desc:  Checks the available system resources (memory).
6352 *
6353 *       Ret:   ROK      - ok
6354 *              RFAILED  - failed, general (optional)
6355 *
6356 *       Notes:
6357 *
6358 *       File:  ss_msg.c
6359 *
6360 */
6361 #ifdef ANSI
6362 S16 SChkRes
6363 (
6364 Region region,                  /* region ID */
6365 Pool pool,                      /* pool ID */
6366 Status *status                  /* pointer to status */
6367 )
6368 #else
6369 S16 SChkRes(region, pool, status)
6370 Region region;                  /* region ID */
6371 Pool pool;                      /* pool ID */
6372 Status *status;                 /* pointer to status */
6373 #endif
6374 {
6375    S16 ret;
6376    SMemCtl mctl;
6377
6378
6379
6380 #if (ERRCLASS & ERRCLS_INT_PAR)
6381    /* validate region ID */
6382    if (region >= SS_MAX_REGS)
6383    {
6384       SSLOGERROR(ERRCLS_INT_PAR, ESS229, region, "Invalid region");
6385       return RFAILED;
6386    }
6387
6388    /* validate pool ID */
6389    if (pool >= SS_MAX_POOLS_PER_REG)
6390    {
6391       SSLOGERROR(ERRCLS_INT_PAR, ESS230, region, "Invalid pool");
6392       return RFAILED;
6393    }
6394
6395    /* validate status pointer */
6396    if (status == NULLP)
6397    {
6398       SSLOGERROR(ERRCLS_INT_PAR, ESS231, ERRZERO, "Null pointer");
6399       return RFAILED;
6400    }
6401 #endif
6402 /* ss037.103 Removed the semaphore operation for performance enhancement */
6403
6404 #ifndef SS_PERF
6405    /* acquire one semaphore, to protect against deregistration */
6406    SS_ACQUIRE_SEMA(&osCp.regionTblSem, ret);
6407    if (ret != ROK)
6408    {
6409
6410 #if (ERRCLASS & ERRCLS_DEBUG)
6411       SSLOGERROR(ERRCLS_DEBUG, ESS232, (ErrVal) ret,
6412                   "Could not lock region table");
6413 #endif
6414
6415       return RFAILED;
6416    }
6417
6418
6419 #if (ERRCLASS & ERRCLS_INT_PAR)
6420    /* verify that this region is around */
6421    if (osCp.regionTbl[region].used == FALSE)
6422    {
6423
6424 /* ss006.13: addition */
6425       if( SS_RELEASE_SEMA(&osCp.regionTblSem) != ROK)
6426       {
6427 #if (ERRCLASS & ERRCLS_DEBUG)
6428          SSLOGERROR(ERRCLS_DEBUG, ESS233, ERRZERO,
6429                   "Could not release semaphore");
6430          return RFAILED;
6431 #endif
6432       }
6433
6434       SSLOGERROR(ERRCLS_INT_PAR, ESS234, region, "Region not registered");
6435       return RFAILED;
6436    }
6437
6438    /* verify that this is a valid pool */
6439    if (osCp.regionTbl[region].poolTbl[pool].type != SS_POOL_DYNAMIC)
6440    {
6441       SSLOGERROR(ERRCLS_DEBUG, ESS235, ERRZERO, "Invalid pool");
6442       return RFAILED;
6443    }
6444 #endif
6445 #endif
6446
6447    /* call the memory manager to check resources */
6448    mctl.op = SS_MEM_CHK_RES;
6449    mctl.u.chkres.size = osCp.regionTbl[region].poolTbl[pool].u.dpool.size;
6450    mctl.u.chkres.status = status;
6451    ret = (osCp.regionTbl[region].ctl)
6452              (osCp.regionTbl[region].regCb, SS_MEM_CHK_RES, &mctl);
6453 #ifndef SS_PERF
6454    /* release the semaphore we took */
6455
6456 /* ss006.13: addition */
6457    if( SS_RELEASE_SEMA(&osCp.regionTblSem) != ROK)
6458    {
6459 #if (ERRCLASS & ERRCLS_DEBUG)
6460       SSLOGERROR(ERRCLS_DEBUG, ESS236, ERRZERO,
6461                   "Could not release semaphore");
6462       return RFAILED;
6463 #endif
6464    }
6465 #endif
6466
6467    return (ret);
6468 }
6469
6470 /*
6471 *
6472 *       Fun:   SSwapMsg
6473 *
6474 *       Desc:  This function will swap two message data contents.
6475 *              The original mBuf pointers are unchanged.
6476 *
6477 *       Ret:   ROK     - ok
6478 *              RFAILED - failed
6479 *
6480 *       Notes: 
6481 *
6482 *       File:  ss_msg.c
6483 *
6484 */
6485
6486
6487 #ifdef ANSI
6488 S16 SSwapMsg
6489 (
6490 Buffer *mBuf1,              /* message 1 */
6491 Buffer *mBuf2               /* message 2 */
6492 )
6493 #else
6494 S16 SSwapMsg(mBuf1, mBuf2)
6495 Buffer *mBuf1;              /* message 1 */
6496 Buffer *mBuf2;              /* message 2 */
6497 #endif
6498 {
6499    SsMsgInfo *minfop;
6500    SsMsgInfo minfo;
6501 #if (ERRCLASS & ERRCLS_INT_PAR)
6502    SsMsgInfo *minfo1;
6503    SsMsgInfo *minfo2;
6504 #endif
6505    Buffer *tmp;
6506    uint8_t tmp2;
6507    
6508
6509 #if (ERRCLASS & ERRCLS_INT_PAR)
6510    /* check message buffer 1 */
6511    if (mBuf1 == NULLP)
6512    {
6513       SSLOGERROR(ERRCLS_INT_PAR, ESS237, ERRZERO, "SSwapMsg : Null Buffer");
6514       return RFAILED;
6515    }
6516    /* check message buffer 2 */
6517    if (mBuf2 == NULLP)
6518    {
6519       SSLOGERROR(ERRCLS_INT_PAR, ESS238, ERRZERO, "SSwapMsg : Null Buffer");
6520       return RFAILED;
6521    }
6522    if (mBuf1->b_datap->db_type != SS_M_PROTO)
6523    {
6524       SSLOGERROR(ERRCLS_INT_PAR, ESS239, ERRZERO, "SSwapMsg: Incorrect buffer\
6525                                                    type");
6526       return RFAILED;
6527    }
6528    if (mBuf2->b_datap->db_type != SS_M_PROTO)
6529    {
6530       SSLOGERROR(ERRCLS_INT_PAR, ESS240, ERRZERO, "SSwapMsg: Incorrect buffer\
6531                                                    type");
6532       return RFAILED;
6533    }
6534    minfo1 = (SsMsgInfo*) mBuf1->b_rptr;
6535    minfo2 = (SsMsgInfo*) mBuf2->b_rptr;
6536    if (minfo1->region != minfo2->region)
6537    {
6538       SSLOGERROR(ERRCLS_INT_PAR, ESS241, ERRZERO, "SSwapMsg: differnt regions\
6539                                                    for messages");
6540       return RFAILED;
6541    }
6542 #endif
6543
6544
6545    tmp = mBuf1->b_next;
6546    mBuf1->b_next = mBuf2->b_next;
6547    mBuf2->b_next = tmp;
6548
6549    tmp = mBuf1->b_prev;
6550    mBuf1->b_prev = mBuf2->b_prev;
6551    mBuf2->b_prev = tmp;
6552
6553    tmp = mBuf1->b_cont;
6554    mBuf1->b_cont = mBuf2->b_cont;
6555    mBuf2->b_cont = tmp;
6556
6557 /* ss008.301 */
6558 #ifdef SS_DBLK_FREE_RTN 
6559    tmp = (Buffer *) mBuf1->b_datap->db_frtnp;
6560    mBuf1->b_datap->db_frtnp = mBuf2->b_datap->db_frtnp;
6561    mBuf2->b_datap->db_frtnp = (struct ssfree_rtn *) tmp;
6562 #endif   
6563
6564    tmp2 = mBuf1->b_datap->db_ref;
6565    mBuf1->b_datap->db_ref = mBuf2->b_datap->db_ref;
6566    mBuf2->b_datap->db_ref = tmp2;
6567
6568
6569    minfop = & minfo;
6570    
6571    SMemCpy((Void *) minfop, (Void *) (mBuf1->b_datap->db_base), (size_t)(sizeof(SsMsgInfo)));
6572    SMemCpy((Void *) (mBuf1->b_datap->db_base), (Void *) (mBuf2->b_datap->db_base), (size_t)(sizeof(SsMsgInfo)));
6573    SMemCpy((Void *) (mBuf2->b_datap->db_base), (Void *) minfop, (size_t)(sizeof(SsMsgInfo)));
6574
6575    return ROK;
6576 }
6577 /* ss004.301 : Cavium changes */
6578 #ifdef SS_SEUM_CAVIUM
6579
6580 /*
6581  *
6582  *       Fun:   SConvPtrPhy
6583  *
6584  *       Desc:  This function will converts the pointer to
6585  *              physical address.
6586  *
6587  *       Ret:   ROK      - ok
6588  *              RFAILED  - failed, general (optional)
6589  *
6590  *       Notes: Function to convert the Pointer (Virtual address) to
6591  *              Physical Address. This will take the Buffer type as
6592  *              input and will convert all pointer associated with
6593  *              that to Physical Address 
6594  *
6595  *       File:  ss_msg.c
6596  *
6597  */
6598 #ifdef ANSI
6599 S16 SConvPtrPhy
6600 (
6601  Buffer  **mBuf
6602 )
6603 #else
6604 S16 SConvPtrPhy (mBuf)
6605   Buffer **mBuf;
6606 #endif
6607 {
6608
6609   Buffer      *curPtr;
6610   Buffer      *nextPtr;
6611   SsMsgInfo   *minfoPtr;
6612   SsDblk      *dblkPtr;
6613   SsFrtn      *frtnPtr;
6614
6615
6616   /* check mBuf for NULLP */
6617   if ( (mBuf == NULLP) || (*mBuf == NULLP ) )
6618   {
6619          return RFAILED;
6620   }
6621
6622   /* first block in Buffer is head */
6623   curPtr = *mBuf;
6624   nextPtr = curPtr->b_cont;
6625
6626   /* Get the physical address of the Pointer */
6627   if(curPtr->b_cont)
6628   {
6629          curPtr->b_cont = (Buffer*)cvmx_ptr_to_phys (curPtr->b_cont);
6630   }
6631
6632   if(curPtr->b_next)
6633   {
6634          curPtr->b_next = (Buffer*)cvmx_ptr_to_phys (curPtr->b_next);
6635   }
6636
6637   if(curPtr->b_prev)
6638   {
6639          curPtr->b_prev = (Buffer*)cvmx_ptr_to_phys (curPtr->b_prev);
6640   }
6641
6642   /* Convert the pointers of Minfo to Physical addr */
6643   minfoPtr = (SsMsgInfo*)curPtr->b_rptr;
6644
6645   if (minfoPtr->endptr)
6646   {
6647          minfoPtr->endptr = (Buffer*)cvmx_ptr_to_phys (minfoPtr->endptr);
6648   }
6649   if (minfoPtr->next)
6650   {
6651          minfoPtr->next = (Buffer*)cvmx_ptr_to_phys (minfoPtr->next);
6652   }
6653
6654   curPtr->b_rptr = (uint8_t*)cvmx_ptr_to_phys (curPtr->b_rptr);
6655
6656   curPtr->b_wptr = (uint8_t*)cvmx_ptr_to_phys (curPtr->b_wptr);
6657
6658   /* Convert the pointers of Dblock to Physical addr */
6659   dblkPtr = (SsDblk*)curPtr->b_datap;
6660
6661 /* ss008.301 */
6662 #ifdef SS_DBLK_FREE_RTN 
6663   frtnPtr = dblkPtr->db_frtnp;
6664   if( frtnPtr != NULLP)
6665   {
6666          if (frtnPtr->free_func)
6667          {
6668                 frtnPtr->free_func = (Void*)cvmx_ptr_to_phys (frtnPtr->free_func);
6669          }
6670          if (frtnPtr->free_arg)
6671          {
6672                 frtnPtr->free_arg = (S8*)cvmx_ptr_to_phys (frtnPtr->free_arg);
6673          }
6674
6675          dblkPtr->db_frtnp = (SsFrtn*)cvmx_ptr_to_phys (dblkPtr->db_frtnp);
6676   }
6677 #endif
6678
6679   dblkPtr->db_base = (uint8_t*)cvmx_ptr_to_phys (dblkPtr->db_base);
6680   dblkPtr->db_lim = (uint8_t*)cvmx_ptr_to_phys (dblkPtr->db_lim);
6681
6682   curPtr->b_datap = (SsDblk*)cvmx_ptr_to_phys (curPtr->b_datap);
6683
6684   /* second block onwards  is dblk */
6685   curPtr = nextPtr;
6686
6687   while(curPtr)
6688   {
6689          nextPtr = curPtr->b_cont;
6690
6691          /* Get the physical address of the Pointer */
6692          if(curPtr->b_cont)
6693          {
6694                 curPtr->b_cont = (Buffer*)cvmx_ptr_to_phys (curPtr->b_cont);
6695          }
6696
6697          curPtr->b_rptr = (uint8_t*)cvmx_ptr_to_phys (curPtr->b_rptr);
6698          curPtr->b_wptr = (uint8_t*)cvmx_ptr_to_phys (curPtr->b_wptr);
6699
6700          /* Convert the pointers of Dblock to Physical addr */
6701          dblkPtr = (SsDblk*)curPtr->b_datap;
6702
6703 /* ss008.301 */
6704 #ifdef SS_DBLK_FREE_RTN 
6705          frtnPtr = dblkPtr->db_frtnp;
6706          if( frtnPtr != NULLP)
6707          {
6708                 if (frtnPtr->free_func)
6709                 {
6710                   frtnPtr->free_func = (Void*)cvmx_ptr_to_phys (frtnPtr->free_func);
6711                 }
6712                 if (frtnPtr->free_arg)
6713                 {
6714                   frtnPtr->free_arg = (S8*)cvmx_ptr_to_phys (frtnPtr->free_arg);
6715                 }
6716
6717                 dblkPtr->db_frtnp = (SsFrtn*)cvmx_ptr_to_phys (dblkPtr->db_frtnp);
6718          }
6719 #endif    
6720
6721          dblkPtr->db_base = (uint8_t*)cvmx_ptr_to_phys (dblkPtr->db_base);
6722          dblkPtr->db_lim = (uint8_t*)cvmx_ptr_to_phys (dblkPtr->db_lim);
6723
6724          curPtr->b_datap = (SsDblk*)cvmx_ptr_to_phys (curPtr->b_datap);
6725
6726          curPtr = nextPtr;
6727
6728   }
6729
6730   *mBuf = (Buffer*)cvmx_ptr_to_phys (*mBuf);
6731
6732   return ROK;
6733
6734 } /* SConvPtrPhy */
6735
6736 /*
6737  *
6738  *       Fun:   SConvPhyPtr
6739  *
6740  *       Desc:  This function will converts the physical address
6741  *              into pointers.
6742  *
6743  *       Ret:   On Success - Pointer to converted buffer
6744  *              On Failuer - Returns NULL
6745  *
6746  *       Notes: Function to Convert the Physical address to Pointer 
6747  *              (Virtual address) This will take work buffer as input
6748  *              and will convert all address associated with that to 
6749  *              Pointers.
6750  *
6751  *       File:  ss_msg.c
6752  *
6753  */
6754 #ifdef ANSI
6755 S16 SConvPhyPtr
6756 (
6757  Buffer  **workPtr
6758  )
6759 #else
6760 S16 SConvPhyPtr (workPtr)
6761   Buffer  **workPtr;
6762 #endif
6763 {
6764
6765   Buffer      *curPtr;
6766   Buffer      *mBuf;
6767   SsMsgInfo   *minfoPtr;
6768   SsDblk      *dblkPtr;
6769   SsFrtn      *frtnPtr;
6770
6771
6772   /* check workPtr for NULLP */
6773   if ( (workPtr == NULLP) || (*workPtr == NULLP) )
6774   {
6775          return RFAILED;
6776   }
6777
6778   /* Convert the buffer address to pointer */
6779   mBuf = (Buffer*)cvmx_phys_to_ptr ((uint64_t)(*workPtr));
6780
6781   curPtr = mBuf;
6782
6783   /* first block is mblk */
6784   if (curPtr->b_next)
6785   {
6786          curPtr->b_next = (Buffer*)cvmx_phys_to_ptr ((uint64_t)curPtr->b_next);
6787   }
6788
6789   if (curPtr->b_prev)
6790   {
6791          curPtr->b_prev = (Buffer*)cvmx_phys_to_ptr ((uint64_t)curPtr->b_prev);
6792   }
6793
6794   if(curPtr->b_cont)
6795   {
6796          curPtr->b_cont = (Buffer*)cvmx_phys_to_ptr ((uint64_t)curPtr->b_cont);
6797   }
6798
6799   curPtr->b_rptr = (uint8_t*)cvmx_phys_to_ptr ((uint64_t)curPtr->b_rptr);
6800
6801   /* Get the pointer for minfo */
6802   minfoPtr = (SsMsgInfo*)curPtr->b_rptr;
6803
6804   if (minfoPtr->endptr)
6805   {
6806          minfoPtr->endptr = (Buffer*)cvmx_phys_to_ptr ((uint64_t)minfoPtr->endptr);
6807   }
6808   if (minfoPtr->next)
6809   {
6810          minfoPtr->next = (Buffer*)cvmx_phys_to_ptr ((uint64_t)minfoPtr->next);
6811   }
6812
6813   curPtr->b_wptr = (uint8_t*)cvmx_phys_to_ptr ((uint64_t)curPtr->b_wptr);
6814
6815   curPtr->b_datap = (SsDblk*)cvmx_phys_to_ptr ((uint64_t)curPtr->b_datap);
6816
6817   /* Get the Dblock pointers */
6818   dblkPtr = (SsDblk*)curPtr->b_datap;
6819
6820 /* ss008.301 */
6821 #ifdef SS_DBLK_FREE_RTN 
6822   if (dblkPtr->db_frtnp)
6823   {
6824          dblkPtr->db_frtnp = (SsFrtn*)cvmx_phys_to_ptr ((uint64_t)dblkPtr->db_frtnp);
6825     frtnPtr = dblkPtr->db_frtnp;
6826
6827          if (frtnPtr->free_func)
6828          {
6829                 frtnPtr->free_func = cvmx_phys_to_ptr ((uint64_t)frtnPtr->free_func);
6830          }
6831          if (frtnPtr->free_arg)
6832          {
6833                 frtnPtr->free_arg = (S8*)cvmx_phys_to_ptr ((uint64_t)frtnPtr->free_arg);
6834          }
6835   }
6836 #endif  
6837
6838   dblkPtr->db_base = (uint8_t*)cvmx_phys_to_ptr ((uint64_t)dblkPtr->db_base);
6839   dblkPtr->db_lim = (uint8_t*)cvmx_phys_to_ptr ((uint64_t)dblkPtr->db_lim);
6840
6841   curPtr = curPtr->b_cont;
6842
6843   /* after the first block is dblk */
6844   while(curPtr)
6845   {
6846          if(curPtr->b_cont)
6847          {
6848                 curPtr->b_cont = (Buffer*)cvmx_phys_to_ptr ((uint64_t)curPtr->b_cont);
6849          }
6850
6851          curPtr->b_rptr = (uint8_t*)cvmx_phys_to_ptr ((uint64_t)curPtr->b_rptr);
6852
6853          curPtr->b_wptr = (uint8_t*)cvmx_phys_to_ptr ((uint64_t)curPtr->b_wptr);
6854
6855          curPtr->b_datap = (SsDblk*)cvmx_phys_to_ptr ((uint64_t)curPtr->b_datap);
6856
6857          /* Get the Dblock pointers */
6858          dblkPtr = (SsDblk*)curPtr->b_datap;
6859
6860 /* ss008.301 */
6861 #ifdef SS_DBLK_FREE_RTN 
6862          if (dblkPtr->db_frtnp)
6863          {
6864                 dblkPtr->db_frtnp = (SsFrtn*)cvmx_phys_to_ptr ((uint64_t)dblkPtr->db_frtnp);
6865
6866       frtnPtr = dblkPtr->db_frtnp;
6867
6868                 if (frtnPtr->free_func)
6869                 {
6870                   frtnPtr->free_func = cvmx_phys_to_ptr ((uint64_t)frtnPtr->free_func);
6871                 }
6872                 if (frtnPtr->free_arg)
6873                 {
6874                   frtnPtr->free_arg = (S8*)cvmx_phys_to_ptr ((uint64_t)frtnPtr->free_arg);
6875                 }
6876          }
6877 #endif    
6878
6879          dblkPtr->db_base = (uint8_t*)cvmx_phys_to_ptr ((uint64_t)dblkPtr->db_base);
6880          dblkPtr->db_lim = (uint8_t*)cvmx_phys_to_ptr ((uint64_t)dblkPtr->db_lim);
6881
6882          curPtr = curPtr->b_cont;
6883   }
6884
6885   /* Place the converted buffer */
6886   *workPtr = mBuf;
6887
6888   return ROK;
6889
6890 } /* SConvPhyPtr */
6891
6892 /*
6893  *
6894  *       Fun:   SCpyFpaMsg
6895  *
6896  *       Desc:  This function will copy the message from FPA region
6897  *              to other region
6898  *
6899  *       Ret:   On Success - ROK
6900  *              On Failuer - RFAILED
6901  *
6902  *       Notes: Function to copy the message from FPA region to
6903  *              other region. This will internally allocates the
6904  *              memory for the destination buffer and copies the
6905  *              message in the same chain list 
6906  *
6907  *       File:
6908  *
6909  */
6910 #ifdef ANSI
6911 S16 SCpyFpaMsg
6912 (
6913  Buffer *srcBuf,
6914  Region dstRegion,
6915  Pool dstPool,
6916  Buffer **dstBuf
6917  )
6918 #else
6919 S16 SCpyFpaMsg (srcBuf, dstRegion, dstPool, dstBuf)
6920   Buffer *srcBuf;
6921   Region dstRegion;
6922   Pool dstPool;
6923   Buffer **dstBuf;
6924 #endif
6925 {
6926
6927   Size        numBytes;
6928   Size        size;
6929   S16         ret;
6930   Buffer     *curPtr = NULLP;
6931   Data       *dat = NULLP;
6932   Buffer     *tmpblk = NULLP;
6933   Buffer     *newblk = NULLP;
6934   Buffer     *prevblk = NULLP;
6935   SsMsgInfo  *minfoSrc = NULLP;
6936   SsMsgInfo  *minfoDst = NULLP;
6937   SsDblk     *dblkPtr = NULLP;
6938   SsDblk     *dptr = NULLP;
6939
6940
6941   if ( srcBuf == (Buffer*)NULLP )
6942   {
6943          return RFAILED;
6944   }
6945
6946   if ((dstRegion >= SS_MAX_REGS) || (dstPool >= SS_MAX_POOLS_PER_REG))
6947   {
6948          /* Free the source buffer and return failure */
6949          SPutFpaMsg(srcBuf);
6950          return RFAILED;
6951   }
6952
6953   /* Allocate memory for destination buffer */
6954   if(SGetMsg(dstRegion, dstPool, dstBuf) != ROK)
6955   {
6956          /* Free the source buffer and return failure */
6957          SPutFpaMsg(srcBuf);
6958          return (ROUTRES);
6959   }
6960
6961   /* get the minfo of dest and src buffers */
6962   minfoSrc = (SsMsgInfo*) srcBuf->b_rptr;
6963   minfoDst = (SsMsgInfo*) (*dstBuf)->b_rptr;
6964
6965   curPtr = srcBuf->b_cont;
6966
6967   /* Copy all the blocks associated with this Buffer */
6968   while(curPtr)
6969   {
6970          /* Allocate the memeory for dblock */
6971
6972          dblkPtr = (SsDblk*)curPtr->b_datap;
6973          numBytes = dblkPtr->db_lim - dblkPtr->db_base;
6974          size = numBytes + MDBSIZE;
6975
6976          ret = SAlloc( dstRegion, &size, 0, (Data**)&tmpblk);
6977
6978          if(ret != ROK)
6979          {
6980                 /* Free all allocated buffers before returning */
6981                 while (newblk)
6982                 {
6983                   tmpblk = newblk->b_cont;
6984                   (Void) SPutDBuf(dstRegion, dstPool, newblk);
6985                   newblk = tmpblk;
6986                 }
6987                 (Void) SPutMsg(*dstBuf);
6988                 /* Free the source buffer and return failure */
6989                 SPutFpaMsg(srcBuf);
6990                 return (ROUTRES);
6991          }
6992
6993          dat = (Data *)tmpblk + MDBSIZE;
6994          dptr = (SsDblk*) (((Data *) tmpblk) + MBSIZE);
6995          size -= MDBSIZE;
6996
6997          /* Initialize the pointer and copy the data */
6998          INITB( tmpblk, dptr, dat, size, NULLP );
6999 #ifndef SS_DBUF_REFLOCK_DISABLE
7000          SInitLock (&(dptr->dBufLock), SS_LOCK_MUTEX);
7001 #endif
7002          numBytes = curPtr->b_wptr - curPtr->b_rptr;
7003          /* Copy the data part if its present */
7004          if (numBytes > 0 )
7005          {
7006                 SMemCpy( (Void *) tmpblk->b_wptr, (Void *) curPtr->b_rptr, numBytes);
7007                 tmpblk->b_wptr += numBytes;
7008          }
7009
7010          if (!prevblk)
7011          {
7012                 newblk = tmpblk;
7013          }
7014          else
7015          {
7016                 prevblk->b_cont = tmpblk;
7017          }
7018
7019          prevblk = tmpblk;
7020          curPtr = curPtr->b_cont;
7021   }
7022
7023   if (tmpblk)
7024   {
7025          tmpblk->b_cont = NULLP;
7026   }
7027
7028   *minfoDst = *minfoSrc;
7029   minfoDst->region = 0;
7030   minfoDst->pool = 0;
7031   minfoDst->len  = minfoSrc->len;
7032   minfoDst->endptr  = tmpblk;
7033   minfoDst->next = NULLP;
7034
7035   (*dstBuf)->b_cont = newblk;
7036
7037   /* Free the source buffer after copying it */
7038   SPutFpaMsg(srcBuf);
7039
7040   return ROK;
7041
7042 } /* SCpyFpaMsg */
7043
7044 /*
7045  *
7046  *       Fun:   SCpyMsgFpa
7047  *
7048  *       Desc:  This function will copy the message from any region
7049  *              to FPA region
7050  *
7051  *       Ret:   On Success - ROK
7052  *              On Failuer - RFAILED
7053  *
7054  *       Notes: Function will copy the mbuf from msg to FPA.
7055  *              This function allocates the memory internally
7056  *              and copies the message to newly allocated mBuf.
7057  *              The size of mBuf should be either of one
7058  *              pre-difined sizes otherwise mBuf is dopped.
7059  *
7060  *       File:
7061  *
7062  */
7063 #ifdef ANSI
7064 S16 SCpyMsgFpa
7065 (
7066  Buffer *srcBuf,
7067  Buffer **dstBuf
7068  )
7069 #else
7070 S16 SCpyMsgFpa (srcBuf, dstBuf)
7071   Buffer *srcBuf;
7072   Buffer **dstBuf;
7073 #endif
7074 {
7075
7076   Buffer     *curPtr = NULLP;
7077   Data       *dat = NULLP;
7078   Buffer     *tmpblk = NULLP;
7079   Buffer     *prevblk = NULLP;
7080   Buffer     *newblk = NULLP;
7081   SsMsgInfo  *minfoSrc = NULLP;
7082   SsMsgInfo  *minfoDst = NULLP;
7083   SsDblk     *dblkPtr = NULLP;
7084   SsDblk     *dptr = NULLP;
7085   uint32_t         numBytes;
7086   Pool        pool;
7087
7088
7089   if (srcBuf == (Buffer*)NULLP)
7090   {
7091          return RFAILED;
7092   }
7093
7094   *dstBuf = (Buffer*)cvmx_fpa_alloc(SS_CVMX_POOL_0);
7095
7096   if ( *dstBuf == NULLP )
7097   {
7098          /* Free the source buffer before returning  */
7099          SPutMsg(srcBuf);
7100          return (ROUTRES);
7101   }
7102
7103   dat = (Data *)(*dstBuf) + MDBSIZE;
7104   dptr = (SsDblk*) ((Data *)(*dstBuf) + MBSIZE);
7105   numBytes = SS_CVMX_POOL_0_SIZE - MDBSIZE;
7106
7107   /* Initialize the pointers of new block */
7108   INITB((*dstBuf), dptr, dat, numBytes, NULLP);
7109
7110   (*dstBuf)->b_datap->db_type = SS_M_PROTO;
7111   (*dstBuf)->b_wptr = (*dstBuf)->b_rptr + sizeof(SsMsgInfo);
7112
7113   minfoSrc = (SsMsgInfo*) srcBuf->b_rptr;
7114   minfoDst = (SsMsgInfo*) (*dstBuf)->b_rptr;
7115   curPtr = srcBuf->b_cont;
7116
7117   while(curPtr)
7118   {
7119          dblkPtr = (SsDblk*)curPtr->b_datap;
7120
7121          /* Get the size required which is to be allocated */
7122          numBytes = dblkPtr->db_lim - dblkPtr->db_base;
7123          numBytes += MDBSIZE;
7124
7125          /* get the pool depending on the size need to be allocated */
7126          switch(numBytes)
7127          {
7128                 case SS_CVMX_POOL_0_SIZE:
7129
7130                   pool = SS_CVMX_POOL_0;
7131                   break;
7132
7133                 case SS_CVMX_POOL_1_SIZE:
7134
7135                   pool = SS_CVMX_POOL_1;
7136                   break;
7137
7138                 case SS_CVMX_POOL_2_SIZE:
7139
7140                   pool = SS_CVMX_POOL_2;
7141                   break;
7142
7143                 case SS_CVMX_POOL_3_SIZE:
7144
7145                   pool = SS_CVMX_POOL_3;
7146                   break;
7147
7148                 default:
7149                   /* size doesn't match, drop the mBuf and returning 
7150                         * RFAILED */
7151                   SSLOGERROR(ERRCLS_INT_PAR, ESS048, ERRZERO, "SCpyMsgFpa: Invalid\
7152                                 buffer size, dropping the message");
7153
7154                   (*dstBuf)->b_cont = newblk;
7155                   SPutFpaMsg(*dstBuf);
7156                   /* Free the source buffer before returning  */
7157                   SPutMsg(srcBuf);
7158                   return RFAILED;
7159          }
7160
7161          /* Allocate for mBuf and copy both the header and contents */
7162          tmpblk = (Buffer*)cvmx_fpa_alloc(pool);
7163
7164          if ( tmpblk == NULLP )
7165          {
7166                 /* Return error if fails to allocate memory */
7167
7168                 (*dstBuf)->b_cont = newblk;
7169                 SPutFpaMsg(*dstBuf);
7170                 /* Free the source buffer before returning  */
7171                 SPutMsg(srcBuf);
7172                 return (ROUTRES);
7173          }
7174
7175          dat = (Data *)tmpblk + MDBSIZE;
7176          dptr = (SsDblk*) (((Data *) tmpblk) + MBSIZE);
7177          numBytes -= MDBSIZE;
7178
7179          /* Initialize the pointers of new block */
7180          INITB( tmpblk, dptr, dat, numBytes, NULLP );
7181
7182          numBytes = curPtr->b_wptr - curPtr->b_rptr;
7183          /* Copy the message contents */
7184          if (numBytes > 0 )
7185          {
7186                 SMemCpy( (Void *) tmpblk->b_wptr, (Void *) curPtr->b_rptr, numBytes);
7187                 tmpblk->b_wptr += numBytes;
7188          }
7189
7190          /* Add the mew mBuf to the Buffer chain */
7191          if (!prevblk)
7192          {
7193                 newblk = tmpblk;
7194          }
7195          else
7196          {
7197                 prevblk->b_cont = tmpblk;
7198          }
7199
7200          prevblk = tmpblk;
7201          /* Get the next block */
7202          curPtr = curPtr->b_cont;
7203   }
7204
7205   /* Initialize the last mBuf */
7206   if (tmpblk)
7207   {
7208          tmpblk->b_cont = NULLP;
7209   }
7210
7211   *minfoDst = *minfoSrc;
7212   minfoDst->region = 0;
7213   minfoDst->pool = 0;
7214   minfoDst->len  = minfoSrc->len;
7215   minfoDst->endptr  = tmpblk;
7216   minfoDst->next = NULLP;
7217
7218   (*dstBuf)->b_cont = newblk;
7219
7220   /* Free the source buffer after copying it */
7221   SPutMsg(srcBuf);
7222
7223   return ROK;
7224
7225 } /* SCpyMsgFpa */
7226
7227 /*
7228  *
7229  *       Fun:   SPutFpaMsg
7230  *
7231  *       Desc:  This function will free the Buffer associated with
7232  *              FPA pool.
7233  *
7234  *       Ret:   On Success - ROK
7235  *              On Failuer - RFAILED
7236  *
7237  *       Notes: Function free the all buffer associated with wqBuf
7238  *              This will get the pool id by the size of the buffer
7239  *              and same will be used to free the buffer.
7240  *
7241  *       File:
7242  *
7243  */
7244 #ifdef ANSI
7245 S16 SPutFpaMsg
7246 (
7247  Buffer *fpaBuf
7248 )
7249 #else
7250 S16 SPutFpaMsg(fpaBuf)
7251 Buffer *fpaBuf;
7252 #endif
7253 {
7254   uint16_t      size;
7255   Buffer   *curBlk;
7256   Buffer   *nextBlk;
7257   SsDblk   *dblkPtr;
7258
7259
7260   if( fpaBuf == NULLP )
7261   {
7262          return ROK;
7263   }
7264
7265   curBlk = fpaBuf->b_cont;
7266
7267   while(curBlk)
7268   {
7269          nextBlk = curBlk->b_cont;
7270
7271          dblkPtr = (SsDblk*)curBlk->b_datap;
7272
7273          size = dblkPtr->db_lim - dblkPtr->db_base + MDBSIZE;
7274
7275          /* Free the dblock according to its size */
7276          switch(size)
7277          {
7278                 case SS_CVMX_POOL_0_SIZE:
7279                   cvmx_fpa_free(curBlk, SS_CVMX_POOL_0, 0);
7280                   break;
7281
7282                 case SS_CVMX_POOL_1_SIZE:
7283                   cvmx_fpa_free(curBlk, SS_CVMX_POOL_1, 0);
7284                   break;
7285
7286                 case SS_CVMX_POOL_2_SIZE:
7287                   cvmx_fpa_free(curBlk, SS_CVMX_POOL_2, 0);
7288                   break;
7289
7290                 case SS_CVMX_POOL_3_SIZE:
7291                   cvmx_fpa_free(curBlk, SS_CVMX_POOL_3, 0);
7292                   break;
7293
7294                 default:
7295                   SSLOGERROR(ERRCLS_INT_PAR, ESS048, ERRZERO, "SPutFpaMsg: Invalid\
7296                                 buffer size, dropping the message");
7297                   break;
7298          }
7299
7300          curBlk = nextBlk;
7301   }
7302
7303   cvmx_fpa_free(fpaBuf, SS_CVMX_POOL_0, 0);
7304
7305   return ROK;
7306
7307 } /* SPutFpaMsg */
7308
7309 #endif /* SS_SEUM_CAVIUM */
7310
7311 /* ss006.301 : new buffer management APIs, start */
7312 /*
7313 *
7314 *       Fun:   SCpyPartMsg
7315 *
7316 *       Desc:  This function is used to copy a portion of message(srcBuf) into
7317 *              another msg(dstBuf)
7318 *
7319 *       Ret:   ROK      - ok
7320 *              RFAILED  - failed, general (optional)
7321 *              ROUTRES  - failed, out of resources (optional) - In this case
7322 *                         caller shall reclaim the resources allocated for dstBuf.
7323 *
7324 *       Notes: None
7325 *
7326 *       File:  ss_msg.c
7327 *
7328 */
7329
7330 #ifdef ANSI
7331 S16 SCpyPartMsg
7332 (
7333 Buffer *srcBuf,
7334 MsgLen idx,
7335 MsgLen cnt,
7336 Buffer *dstBuf
7337 )
7338 #else
7339 S16 SCpyPartMsg(srcBuf, idx, cnt, dstBuf)
7340 Buffer *srcBuf;
7341 MsgLen idx;
7342 MsgLen cnt;
7343 Buffer *dstBuf;
7344 #endif
7345 {
7346    SsMsgInfo *sMinfo;
7347    SsMsgInfo *dMinfo;
7348    Buffer    *dBuf;
7349    Buffer    *sBuf;
7350    Buffer    *dPrev;
7351    MsgLen    sCnt;
7352    MsgLen    dCnt;
7353    MsgLen    numCpd;
7354  
7355
7356 #if (ERRCLASS & ERRCLS_INT_PAR)
7357    if (!srcBuf)
7358    {
7359       SSLOGERROR(ERRCLS_INT_PAR, ESSXXX, ERRZERO, "SCpyPartMsg : Null src Buffer");
7360       return RFAILED;
7361    }
7362    if (srcBuf->b_datap->db_type != SS_M_PROTO)
7363    {
7364       SSLOGERROR(ERRCLS_INT_PAR, ESSXXX, ERRZERO, "SCpyPartMsg : Incorrect\
7365                                                    src buffer type");
7366       return RFAILED;
7367    }
7368    if(!dstBuf)
7369    {
7370       SSLOGERROR(ERRCLS_INT_PAR, ESSXXX, ERRZERO, "SCpyPartMsg : Null dst Buffer");
7371       return RFAILED;
7372    }
7373    if (dstBuf->b_datap->db_type != SS_M_PROTO)
7374    {
7375       SSLOGERROR(ERRCLS_INT_PAR, ESSXXX, ERRZERO, "SCpyPartMsg : Incorrect\
7376                                                    dst buffer type");
7377       return RFAILED;
7378    }
7379 #endif /* (ERRCLASS & ERRCLS_INT_PAR) */
7380
7381    /* if index > length of mBuf, return */
7382    sMinfo = (SsMsgInfo*)srcBuf->b_rptr;
7383    dMinfo = (SsMsgInfo*)dstBuf->b_rptr;
7384
7385    if (idx >= sMinfo->len)
7386    {
7387       SSLOGERROR(ERRCLS_INT_PAR, ESSXXX, ERRZERO, "SCpyPartMsg : Incorrect\
7388                                                    idx value ");
7389       return RFAILED;
7390    }
7391
7392    sBuf = srcBuf->b_cont;
7393    FIND_OFFSET(sBuf, idx)
7394
7395    dPrev = dstBuf;
7396 /*-- ss008.301 */
7397    dBuf = (dMinfo->endptr) ? (dMinfo->endptr) : (dstBuf->b_cont);
7398    if(dMinfo->endptr)
7399    {
7400       Buffer *crnt;
7401       crnt = dPrev->b_cont;
7402       while(crnt)
7403       {
7404          dPrev = crnt;
7405          crnt = crnt->b_cont;
7406       }
7407    }
7408    dCnt = 0;
7409    numCpd = 0;
7410    while(cnt && sBuf)
7411    {
7412       sCnt = sBuf->b_wptr - (sBuf->b_rptr + idx);
7413       if(dBuf)
7414       {
7415          dCnt = dBuf->b_datap->db_lim - dBuf->b_wptr;
7416       }
7417       else
7418       {
7419          /* allocate a data buffer */
7420          if (SGetDBuf(dMinfo->region, dMinfo->pool, &dBuf) != ROK)
7421          {
7422             SSLOGERROR(ERRCLS_DEBUG, ESS125, ERRZERO, "SCpyPartMsg : SGetDBuf\
7423                  failed");
7424             return (ROUTRES);
7425          }
7426          dCnt = MIN(cnt, (dBuf->b_datap->db_lim - dBuf->b_datap->db_base));
7427          dPrev->b_cont = dBuf;
7428          dPrev = dBuf;
7429       }
7430       if(sCnt > cnt) /* src Dblk has enough data to copy */
7431       {
7432          if(dCnt < cnt)
7433          {
7434             SMemCpy((void*)dBuf->b_wptr, (void*)(sBuf->b_rptr + idx), dCnt);
7435             cnt -= dCnt;
7436             dBuf->b_wptr += dCnt;
7437             dBuf = dBuf->b_cont;
7438             idx += dCnt;
7439             numCpd += dCnt;
7440          }
7441          else
7442          {
7443             SMemCpy((void*)dBuf->b_wptr, (void*)(sBuf->b_rptr + idx), cnt);
7444             dBuf->b_wptr += cnt;
7445             dBuf->b_cont = NULLP;
7446             numCpd += cnt;
7447             cnt = 0;
7448             break;
7449          }
7450       }
7451       else /* src dBlk has partial data to be copied */
7452       {
7453          if(dCnt < sCnt)
7454          {
7455             SMemCpy((void*)dBuf->b_wptr, (void*)(sBuf->b_rptr + idx), dCnt);
7456             dBuf->b_wptr += dCnt;
7457             dBuf = dBuf->b_cont;
7458             cnt -= dCnt;
7459             idx += dCnt;
7460             numCpd += dCnt;
7461          }
7462          else
7463          {
7464             SMemCpy((void*)dBuf->b_wptr, (void*)(sBuf->b_rptr + idx), sCnt);
7465             dBuf->b_wptr += sCnt;
7466             cnt -= sCnt;
7467             sBuf = sBuf->b_cont;
7468             numCpd += sCnt;
7469             idx = 0;
7470          }
7471       }
7472    }
7473    /* update the msgLen in dstBuf */
7474    dMinfo->len += numCpd;
7475    dMinfo->endptr = dBuf; 
7476
7477    return ROK;
7478 }
7479
7480 /*
7481 *
7482 *       Fun:   SRepPartMsg
7483 *
7484 *       Desc:  This function is used to replace a portion of message(mBuf) with the
7485 *              given data
7486 *
7487 *       Ret:   ROK      - ok
7488 *              RFAILED  - failed, general (optional)
7489 *
7490 *       Notes: None
7491 *
7492 *       File:  ss_msg.c
7493 *
7494 */
7495
7496 #ifdef ANSI
7497 S16 SRepPartMsg
7498 (
7499 Buffer *srcBuf,
7500 MsgLen idx,
7501 MsgLen cnt,
7502 Buffer *dstBuf
7503 )
7504 #else
7505 S16 SRepPartMsg(srcBuf, idx, cnt, dstBuf)
7506 Buffer *srcBuf;
7507 MsgLen idx;
7508 MsgLen cnt;
7509 Buffer *dstBuf;
7510 #endif
7511 {
7512    SsMsgInfo *sMinfo;
7513    SsMsgInfo *dMinfo;
7514    Buffer    *dBuf;
7515    Buffer    *sBuf;
7516    MsgLen     dCnt;
7517    MsgLen     sCnt;
7518    MsgLen     sIdx;
7519    MsgLen     cpBytes;
7520
7521
7522 #if (ERRCLASS & ERRCLS_INT_PAR)
7523    if (!dstBuf)
7524    {
7525       SSLOGERROR(ERRCLS_INT_PAR, ESSXXX, ERRZERO, "SRepPartMsg : Null dstBuf");
7526       return RFAILED;
7527    }
7528    if (dstBuf->b_datap->db_type != SS_M_PROTO)
7529    {
7530       SSLOGERROR(ERRCLS_INT_PAR, ESSXXX, ERRZERO, "SRepPartMsg : Incorrect\
7531                                                    dstBuf buffer type");
7532       return RFAILED;
7533    }
7534    if(!srcBuf)
7535    {
7536       SSLOGERROR(ERRCLS_INT_PAR, ESSXXX, ERRZERO, "SRepPartMsg : Null Src Buffer ");
7537       return RFAILED;
7538    }
7539    if (srcBuf->b_datap->db_type != SS_M_PROTO)
7540    {
7541       SSLOGERROR(ERRCLS_INT_PAR, ESSXXX, ERRZERO, "SRepPartMsg : Incorrect\
7542                                                    sBuf buffer type");
7543       return RFAILED;
7544    }
7545 #endif  /* (ERRCLASS & ERRCLS_INT_PAR) */
7546
7547    dMinfo = (SsMsgInfo*)dstBuf->b_rptr;
7548    sMinfo = (SsMsgInfo*)srcBuf->b_rptr;
7549
7550    if(((idx + cnt) > dMinfo->len) || (cnt < sMinfo->len))
7551    {
7552       SSLOGERROR(ERRCLS_INT_PAR, ESSXXX, ERRZERO, "SRepPartMsg : Incorrect\
7553                                                    cnt value ");
7554       return RFAILED;
7555    }
7556
7557    dBuf = dstBuf->b_cont;
7558    FIND_OFFSET(dBuf, idx)
7559    sBuf = srcBuf->b_cont;
7560    sIdx = 0;
7561    while(cnt && dBuf && sBuf)
7562    {
7563       dCnt = (dBuf->b_wptr - (dBuf->b_rptr + idx));
7564       sCnt = sBuf->b_wptr - (sBuf->b_rptr + sIdx);
7565       cpBytes = MIN(cnt, sCnt);
7566       cpBytes = MIN(cpBytes, dCnt);
7567       SMemCpy((void*)(dBuf->b_rptr + idx), (void*)(sBuf->b_rptr + sIdx), cpBytes);
7568       
7569       if(cpBytes < cnt)
7570       {
7571          if(cpBytes == sCnt)
7572          {
7573             /* move to next DBlk in srcBuf */
7574             sBuf = sBuf->b_cont;
7575             idx += cpBytes;
7576             sIdx = 0;
7577          }
7578          else /* cpBytes equals dCnt */
7579          {
7580             /* move to the next DBlk in dstBuf */
7581             dBuf = dBuf->b_cont;
7582             idx = 0;
7583             sIdx += cpBytes;
7584          }
7585          cnt -= cpBytes;
7586       }
7587       else
7588       {
7589          cnt = 0;
7590          break;
7591       }
7592    }
7593    if(cnt)
7594    {
7595       SSLOGERROR(ERRCLS_INT_PAR, ESSXXX, ERRZERO, "SRepPartMsg : unable to replace\
7596                                                     some bytes ");
7597       return RFAILED;
7598    }
7599
7600    return ROK;
7601 }
7602
7603 /*
7604 *
7605 *       Fun:   SMovPartMsg
7606 *
7607 *       Desc:  This function will move a portion of the first msg to second msg
7608 *
7609 *       Ret:   ROK     - ok
7610 *              ROKDNA  - ok, data not available
7611 *              RFAILED - failed, general (optional)
7612 *              ROUTRES - failed, out of resources (optional)
7613 *
7614 *       Notes: message 1 is the message from which the segment will be copied 
7615 *
7616 *              message 2 is the updated message.
7617 *
7618 *              index is 0 based and indicates location in message 1
7619 *              up to which the data will be copied to message 2 
7620 *
7621 *              if index is equal to 0, message 1 will not be changed and no data 
7622                shall be copied to message 2.
7623 *              message 1 is not returned to memory. return is ok.
7624 *
7625 *              if index is not equal to 0 and less than the length of
7626 *              the message minus 1: data upto index, shall be copied to message 2 
7627 *              and read/write pointers of message 1 will be updated accordingly              
7628 *
7629 *              if index is not equal to 0 and greater than or equal to
7630 *              the length of the message minus 1: all of the message 1 data.
7631 *              shall be copied to message 2. return is ok.
7632 *
7633 *       File:  ss_msg.c
7634 *
7635 */
7636
7637
7638 #ifdef ANSI
7639 S16 SMovPartMsg
7640 (
7641 Buffer *srcBuf,              /* message 1 */
7642 MsgLen idx,                 /* index */
7643 Buffer *dstBuf              /* message 2 */
7644 )
7645 #else
7646 S16 SMovPartMsg(srcBuf, idx, dstBuf)
7647 Buffer *srcBuf;              /* message 1 */
7648 MsgLen idx;                 /* index */
7649 Buffer *dstBuf;             /* message 2 */
7650 #endif
7651 {
7652    MsgLen     dCnt;
7653    MsgLen     sCnt;
7654    Buffer     *dPrev;
7655    Buffer     *sBuf;
7656    Buffer     *dBuf;
7657    SsMsgInfo  *sMinfo; 
7658    SsMsgInfo  *dMinfo;
7659 #ifdef T2K_MEM_LEAK_DBG
7660    char* file = __FILE__;
7661    uint32_t line = __LINE__;
7662 #endif
7663
7664
7665 #if (ERRCLASS & ERRCLS_INT_PAR)
7666    /* check message buffer 1 */
7667    if ((!srcBuf) || (!dstBuf ))
7668    {
7669       SSLOGERROR(ERRCLS_INT_PAR, ESS097, ERRZERO, "SMovPartMsg : Null Buffer (src or Dst)");
7670       return RFAILED;
7671    }
7672    if (idx < 0)
7673    {
7674       SSLOGERROR(ERRCLS_INT_PAR, ESS099, ERRZERO, "SMovPartMsg : Invalid index");
7675       return RFAILED;
7676    }
7677    if ((srcBuf->b_datap->db_type != SS_M_PROTO) || (dstBuf->b_datap->db_type != SS_M_PROTO)) 
7678    {
7679       SSLOGERROR(ERRCLS_INT_PAR, ESS100, ERRZERO, "SMovPartMsg : Incorrect buffer\
7680                                                    type");
7681       return RFAILED;
7682    }
7683 #endif
7684
7685    /* get the SsMsgInfo of srcBuf */
7686    sMinfo = (SsMsgInfo*) srcBuf->b_rptr;
7687
7688    /* get the SsMsgInfo of dstBuf */
7689    dMinfo = (SsMsgInfo*) dstBuf->b_rptr;
7690
7691    /* if index > length of mBuf, return */
7692    /*-- ss008.301 */
7693    if (idx > sMinfo->len)
7694    {
7695       return (ROKDNA);
7696    }
7697
7698    /* one block might not sufficient - Check for generic implementation */
7699    sBuf = srcBuf->b_cont;
7700    /* dBuf = dMinfo->endptr; */
7701    dPrev =  (dBuf = dMinfo->endptr) ?  dBuf : dstBuf;
7702
7703    /* adjust the lengths of srcBuf, dstBuf */
7704    sMinfo->len -= idx;
7705    dMinfo->len += idx;
7706
7707    while(idx && sBuf) 
7708    {
7709       sCnt = sBuf->b_wptr - sBuf->b_rptr;
7710       if(!dBuf)
7711       {
7712          /* allocate a data buffer */
7713          if (SGetDBuf(dMinfo->region, dMinfo->pool, &dBuf) != ROK)
7714          {
7715             SSLOGERROR(ERRCLS_DEBUG, ESS125, ERRZERO, "SCpyMsgPartMsg : SGetDBuf\
7716                  failed");
7717             return (ROUTRES);
7718          }
7719          dCnt = MIN(idx, (dBuf->b_datap->db_lim - dBuf->b_datap->db_base));
7720          dPrev->b_cont = dBuf;
7721          dPrev = dBuf;
7722       }
7723       else
7724       {
7725          dCnt = dBuf->b_datap->db_lim - dBuf->b_wptr;
7726       }
7727       if(sCnt > idx) /* src Dblk has enough data to copy */
7728       {
7729          if(dCnt < idx)
7730          {
7731             SMemCpy((void*)dBuf->b_wptr, (void*)(sBuf->b_rptr), dCnt);
7732             dBuf->b_wptr += dCnt;
7733             dBuf = dBuf->b_cont;
7734             idx -= dCnt;
7735             sBuf->b_rptr += dCnt;
7736          }
7737          else
7738          {
7739             SMemCpy((void*)dBuf->b_wptr, (void*)(sBuf->b_rptr), idx);
7740             dBuf->b_wptr += idx;
7741             dBuf->b_cont = NULLP;
7742             sBuf->b_rptr += idx;
7743             idx = 0;
7744             break;
7745          }
7746       }
7747       else /* src dBlk has partial data to be copied */
7748       {
7749          if(dCnt < sCnt)
7750          {
7751             SMemCpy((void*)dBuf->b_wptr, (void*)(sBuf->b_rptr), dCnt);
7752             dBuf->b_wptr += dCnt;
7753             dBuf = dBuf->b_cont;
7754             idx -= dCnt;
7755             sBuf->b_rptr += dCnt;
7756          }
7757          else
7758          {
7759             Buffer *tmp;
7760             SMemCpy((void*)dBuf->b_wptr, (void*)(sBuf->b_rptr), sCnt);
7761             dBuf->b_wptr += sCnt;
7762             idx -= sCnt;
7763             /* deallocate the sBuf here */
7764             tmp = sBuf->b_cont;
7765             /* printf("SMovPartMsg() sBuf is completely copied sBuf->b_cont(%p)\n", sBuf->b_cont); */
7766             (Void)SPutDBuf(sMinfo->region, sMinfo->pool, sBuf);
7767             srcBuf->b_cont = sBuf = tmp;
7768          }
7769       }
7770    }
7771    dMinfo->endptr = dBuf;
7772    if(idx)
7773    {
7774       sMinfo->len += idx;
7775       dMinfo->len -= idx;
7776       SSLOGERROR(ERRCLS_INT_PAR, ESSXXX, ERRZERO, "SMovPartMsg : unable to copy\
7777                                                     some bytes ");
7778       return RFAILED;
7779    }
7780
7781    return ROK;
7782 }
7783
7784 /*
7785 *
7786 *       Fun:   SPkMsgMult
7787 *
7788 *       Desc:  This function copies consecutive bytes of data to the
7789 *              beginning of a message.
7790 *
7791 *       Ret:   ROK      - ok
7792 *              RFAILED  - failed, general (optional)
7793 *              ROUTRES  - failed, out of resources (optional)
7794 *
7795 *       Notes: if message is empty: data is placed in the message. message
7796 *              length is incremented. return is ok.
7797 *
7798 *              if message is not empty: data is read by source pointer,
7799 *              data is placed in front of all other data in message.
7800 *              message length is incremented. return is ok.
7801 *
7802 *              the first byte of data pointed to by the source pointer 
7803 *              will be placed at the front of the message first (i.e. it 
7804 *              will become the first byte of the message) and the last 
7805 *              byte will be placed in front of the existing msg contents,
7806 *              i.e. order of the source is preserved.
7807 *
7808 *       File:  ss_msg.c
7809 *
7810 */
7811  
7812 #ifdef ANSI
7813 S16 SPkMsgMult
7814 (
7815 Data *src,
7816 MsgLen cnt,
7817 Buffer *mBuf
7818 )
7819 #else
7820 S16 SPkMsgMult(src, cnt, mBuf)
7821 Data *src;
7822 MsgLen cnt;
7823 Buffer *mBuf;
7824 #endif
7825 {
7826    SsMsgInfo *minfo;   /* Message info */
7827    Buffer *tmp;
7828    Buffer *newblk;
7829    MsgLen numBytes;  /* no. of bytes to be copied */
7830    MsgLen offset;
7831
7832
7833 #if (ERRCLASS & ERRCLS_INT_PAR)
7834    /* check message buffer */
7835    if (mBuf == NULLP)
7836    {
7837       SSLOGERROR(ERRCLS_INT_PAR, ESS070, ERRZERO, "SPkMsgMult:Null Buffer");
7838       return RFAILED;
7839    }
7840    /* check source */
7841    if (src == NULLP)
7842    {
7843       SSLOGERROR(ERRCLS_INT_PAR, ESS071, ERRZERO, "SPkMsgMult:Null Buffer");
7844       return RFAILED;
7845    }
7846    /* check count */
7847    if (cnt <= 0)
7848    {
7849       SSLOGERROR(ERRCLS_INT_PAR, ESS072, ERRZERO, "SPkMsgMult: Invalid\
7850                                                    count");
7851       return RFAILED;
7852    }
7853    if (mBuf->b_datap->db_type != SS_M_PROTO)
7854    {
7855       SSLOGERROR(ERRCLS_INT_PAR, ESS073, ERRZERO, "SPkMsgMult: Incorrect\
7856                                                    buffer type");
7857       return RFAILED;
7858    }
7859 #endif
7860
7861    /* get the SsMsgInfo of mBuf */
7862    minfo = (SsMsgInfo*) mBuf->b_rptr;
7863
7864 /* ss014.13: Addition */
7865    offset = 0;
7866
7867    if ((tmp = mBuf->b_cont) && (tmp->b_datap->db_ref == 1) &&
7868        (tmp->b_datap->db_base < tmp->b_rptr))
7869    {
7870       /* store the offset of the read pointer of tmp */
7871       offset = tmp->b_rptr - tmp->b_datap->db_base;
7872
7873       /* determine the number of bytes to copy */
7874       numBytes = MIN(cnt, offset);
7875
7876       /* decrement cnt */
7877       cnt -= numBytes;
7878
7879       /* update the read ptr */
7880       tmp->b_rptr -= numBytes;
7881       /* copy data */
7882       memcpy(tmp->b_rptr, (src + cnt), numBytes);
7883       minfo->len += numBytes;
7884       if (!cnt)
7885       {
7886          return ROK;
7887       }
7888    }
7889
7890    /* allocate a DBlk minimum of size Cnt to pack the data */
7891    newblk = NULLP;
7892    if (ssGetDBufOfSize(minfo->region, cnt, &newblk) != ROK)
7893    {
7894        return (ROUTRES);
7895    }
7896    newblk->b_datap->db_type = SS_M_DATA;
7897    newblk->b_rptr = newblk->b_datap->db_lim - cnt;
7898    newblk->b_wptr = newblk->b_datap->db_lim;
7899    memcpy(newblk->b_rptr, src, cnt);
7900    /* attach the newblk chain into mBuf */
7901    newblk->b_cont = tmp;
7902    mBuf->b_cont = newblk;
7903    minfo->len += cnt;
7904    minfo->endptr = newblk;
7905
7906    return ROK;
7907 }
7908 /* ss006.301 : new buffer management APIs, end */
7909 /*
7910 *
7911 *       Fun:   SGetReadPtr 
7912 *
7913 *       Desc:  This function retunrs the pointer to the read the message from mBuf
7914 *              
7915 *       Ret:   ROK      - ok
7916 *              RFAILED  - failed, general (optional)
7917 *
7918 *       Notes: 
7919 *
7920 *       File:  ss_msg.c
7921 *
7922 */
7923 #ifdef ANSI
7924 S16 SGetReadPtr
7925 (
7926  Buffer *mBuf, 
7927  uint8_t** data, 
7928  MsgLen *len
7929 )
7930 #else
7931 S16 SGetReadPtr (mBuf, data, len)
7932 Buffer *mBuf; 
7933 uint8_t** data; 
7934 MsgLen *len;
7935 #endif 
7936 {
7937 /*   Buffer       *tmp; */
7938
7939    if (mBuf && mBuf->b_cont)
7940    {
7941       *data = mBuf->b_cont->b_rptr;
7942       *len = ((SsMsgInfo*)(mBuf->b_rptr))->len;
7943    }
7944    else
7945    {
7946       *data = NULLP;
7947       *len = 0;
7948    }
7949
7950    return ROK;
7951 }
7952 #ifdef SS_USE_ZBC_MEMORY
7953 /*
7954 *
7955 *       Fun:   SAttachPtrToBuf 
7956 *
7957 *       Desc:  This function attaches the Pointer provided into a new
7958 *              message buffer after allocating the same. It allocates
7959 *              header (M-Block) and an additional dBuf (D-Block) and attaches
7960 *              the provided pointer to it.
7961 *
7962 *       Ret:   ROK      - ok
7963 *              RFAILED  - failed, general (optional)
7964 *              ROUTRES  - failed, out of resources (optional)
7965 *
7966 *       Notes: 
7967 *
7968 *       File:  ss_msg.c
7969 *
7970 */
7971
7972 #ifdef T2K_MEM_LEAK_DBG
7973 S16 SAttachPtrToBufNew
7974 (
7975 Region   region,
7976 Pool     pool,
7977 Data    *ptr,
7978 MsgLen   totalLen,
7979 Buffer** mBuf,
7980 char* file,
7981 uint32_t line
7982 )
7983 #else
7984 #ifdef ANSI
7985 S16 SAttachPtrToBuf
7986 (
7987 Region   region,
7988 Pool     pool,
7989 Data    *ptr,
7990 MsgLen   totalLen,
7991 Buffer** mBuf
7992 )
7993 #else
7994 S16 SAttachPtrToBuf(region, pool, ptr, totalLen, mBuf)
7995 Region   region;
7996 Pool     pool;
7997 Data    *ptr;
7998 MsgLen   totalLen;
7999 Buffer** mBuf;
8000 #endif
8001 #endif
8002 {
8003    SsMsgInfo *minfo;   /* Message info */
8004    Size       mdsize;
8005    SsDblk    *dptr;
8006    Buffer    *newblk;
8007    /*   Void      *iccHdlr; */
8008
8009
8010 #if (ERRCLASS & ERRCLS_INT_PAR)
8011    if (ptr == NULLP)
8012    {
8013       SSLOGERROR(ERRCLS_INT_PAR, ESSXXX, ERRZERO, "SAttachPtrToBuf: Null PTR");
8014       return RFAILED;
8015    }
8016 #endif
8017
8018 #ifdef XEON_SPECIFIC_CHANGES
8019    region = 0;
8020 #endif   
8021    if(SGetMsg(region, pool, mBuf) != ROK)
8022    {
8023       SSLOGERROR(ERRCLS_INT_PAR, ESSXXX, ERRZERO, "SAttachPtrToBuf: SGetMsg Failed");
8024       return (ROUTRES);
8025    }
8026
8027    mdsize = MDBSIZE;
8028
8029 #ifdef SS_HISTOGRAM_SUPPORT 
8030    if (SAlloc(region, &mdsize, 0, (Data **) &newblk, __LINE__, (uint8_t*) __FILE__, ENTNC) != ROK)
8031 #else
8032    if (SAlloc(region, &mdsize, 0, (Data **) &newblk) != ROK)
8033 #endif /* SS_HISTOGRAM_SUPPORT */
8034    {
8035        SSLOGERROR(ERRCLS_INT_PAR, ESSXXX, ERRZERO, "SAttachPtrToBuf: SAlloc Failed");
8036        SPutMsg(*mBuf);
8037        return (ROUTRES);
8038    }
8039
8040    (*mBuf)->b_cont = newblk;
8041
8042    dptr = (SsDblk*) (((Data *) (newblk)) + MBSIZE);
8043
8044    INITB((newblk), dptr, (Data*) NULL, 0, NULLP)
8045
8046
8047    newblk->b_datap->db_base = ptr;
8048    newblk->b_datap->db_lim = ptr + totalLen;
8049    newblk->b_rptr = newblk->b_datap->db_base;
8050    newblk->b_wptr = newblk->b_rptr + totalLen;
8051
8052 #ifndef SS_DBUF_REFLOCK_DISABLE
8053    if((SInitLock (&(dptr->dBufLock), SS_LOCK_MUTEX)) != 0)
8054    {
8055       printf("Falied to destroy lock\n");
8056    }
8057 #endif
8058
8059    dptr->db_type = SS_MEM_TYPE_SSI_ZBC; 
8060
8061    /* get the SsMsgInfo of mBuf */
8062    minfo = (SsMsgInfo*) (*mBuf)->b_rptr;
8063    minfo->len = totalLen;
8064    minfo->endptr = newblk;
8065
8066    return ROK;
8067 }
8068
8069 /*
8070 *
8071 *       Fun:   SPutZbcDBuf
8072 *
8073 *       Desc:  This function deallocates a buffer back to the
8074 *              dynamic memory pool which is allocated for ZBC
8075 *
8076 *       Ret:   ROK      - ok
8077 *              RFAILED  - failed, general (optional)
8078 *
8079 *       Notes:
8080 *
8081 *       File:  ss_msg.c
8082 *
8083 */
8084
8085 #ifdef T2K_MEM_LEAK_DBG
8086 static S16 SPutZbcDBufNew
8087 (
8088 Region region,
8089 Buffer *buf,
8090 char* file,
8091 uint32_t line
8092 )
8093 #else
8094 #ifdef ANSI
8095 static S16 SPutZbcDBuf
8096 (
8097 Region region,
8098 Buffer *buf
8099 )
8100 #else
8101 static S16 SPutZbcDBuf(region, buf)
8102 Region region;
8103 Buffer *buf;
8104 #endif
8105 #endif
8106 {
8107    register SsDblk *dptr;
8108    MsgLen          bufLen;
8109    S16             ret = ROK;
8110  
8111  
8112    dptr = buf->b_datap; 
8113    /* Get the length of the buffer */
8114    bufLen = buf->b_datap->db_lim - buf->b_datap->db_base;
8115
8116    /* If the buffer is not shared, free the buffer */
8117    if(!dptr->shared)
8118    {
8119 #ifndef SS_DBUF_REFLOCK_DISABLE
8120        SDestroyLock(&dptr->dBufLock);
8121 #endif
8122
8123        /* Free the ZBC buffer first and then free the block allocated for the 
8124         * D-Block */
8125
8126 #ifdef SS_HISTOGRAM_SUPPORT 
8127       ret = SFree(region, (Data *) buf->b_datap->db_base, bufLen, __LINE__, 
8128                   (uint8_t*) __FILE__, ENTNC);
8129 #else
8130       ret = SFree(region, (Data *) buf->b_datap->db_base, bufLen);
8131 #endif /* SS_HISTOGRAM_SUPPORT */
8132
8133       /* if the data block is not shared, free the buffer, checks not reqd */
8134 #ifdef SS_HISTOGRAM_SUPPORT 
8135       ret = SFree(region, (Data *) buf, MDBSIZE, __LINE__, (uint8_t*) __FILE__, 
8136                   ENTNC);
8137 #else
8138       ret =  SFree(region, (Data *) buf, MDBSIZE);
8139 #endif /* SS_HISTOGRAM_SUPPORT */
8140    }
8141    /* If the buffer is shared, reduce the refernce count and free the buffer
8142     * if reference count falls to zero */
8143    else
8144    {
8145 #ifndef SS_DBUF_REFLOCK_DISABLE
8146         if((ret=SLock(&dptr->dBufLock)))
8147         {
8148            SSLOGERROR(ERRCLS_DEBUG, ESSXXX, ERRZERO,
8149                 "Could not lock the mBuf Ref Lock");
8150            return RFAILED;
8151         }
8152 #endif
8153       --dptr->db_ref;
8154       /* if buffer's message blk is obtained during dupb */
8155       if (buf != (SsMblk *) (((Data*) dptr) - MBSIZE))
8156       {
8157 #ifdef SS_HISTOGRAM_SUPPORT 
8158          ret = SFree(region, (Data *) buf, MDBSIZE, __LINE__, (uint8_t*) __FILE__, ENTNC);
8159 #else
8160          ret = SFree(region, (Data *) buf, MDBSIZE);
8161 #endif /* SS_HISTOGRAM_SUPPORT */
8162          buf = (SsMblk *) (((Data*) dptr) - MBSIZE);
8163       }
8164       /* if reference count falls to zero */
8165       if (!dptr->db_ref)
8166       {
8167 #ifndef SS_DBUF_REFLOCK_DISABLE
8168          ret = SUnlock(&dptr->dBufLock) ;
8169          if((SDestroyLock(&dptr->dBufLock)) != 0)
8170          {
8171              printf("Falied to destroy lock\n");
8172          }
8173 #endif
8174 #ifdef SS_HISTOGRAM_SUPPORT 
8175          ret = SFree(region, (Data *) buf->b_datap->db_base, bufLen, __LINE__, 
8176                    (uint8_t*) __FILE__, ENTNC);
8177 #else
8178          ret = SFree(region, (Data *) buf->b_datap->db_base, bufLen);
8179 #endif /* SS_HISTOGRAM_SUPPORT */
8180
8181 #ifdef SS_HISTOGRAM_SUPPORT 
8182          ret = SFree(region, (Data *) buf, MDBSIZE, __LINE__, (uint8_t*) __FILE__, 
8183                   ENTNC);
8184 #else
8185          ret =  SFree(region, (Data *) buf, MDBSIZE);
8186 #endif /* SS_HISTOGRAM_SUPPORT */
8187           return (ret);
8188       }
8189 #ifndef SS_DBUF_REFLOCK_DISABLE
8190       ret = SUnlock(&(dptr->dBufLock));
8191 #endif
8192    }
8193
8194    return (ret);
8195 }
8196 #endif /* SS_USE_ZBC_MEMORY */
8197
8198 #ifdef INTEL_WLS
8199
8200 /*
8201 *
8202 *       Fun:   SAttachPtrToMBuf 
8203 *
8204 *       Desc:  This function attaches the Pointer provided into a new
8205 *              message buffer after allocating the same. It allocates
8206 *              header (M-Block) and an additional dBuf (D-Block) and attaches
8207 *              the provided pointer to it.
8208 *
8209 *       Ret:   ROK      - ok
8210 *              RFAILED  - failed, general (optional)
8211 *              ROUTRES  - failed, out of resources (optional)
8212 *
8213 *       Notes: 
8214 *
8215 *       File:  ss_msg.c
8216 *
8217 */
8218 #ifdef T2K_MEM_LEAK_DBG
8219 S16 SAttachPtrToMBuf1
8220 (
8221 Region   region,
8222 Pool     pool,
8223 Data    *ptr,
8224 MsgLen   totalLen,
8225 MsgLen   ptrLen,
8226 Buffer** mBuf,
8227 char* file,
8228 uint32_t line
8229 )
8230 #else
8231 #ifdef ANSI
8232 S16 SAttachPtrToMBuf
8233 (
8234 Region   region,
8235 Pool     pool,
8236 Data    *ptr,
8237 MsgLen   totalLen,
8238 MsgLen   ptrLen,
8239 Buffer** mBuf
8240 )
8241 #else
8242 S16 SAttachPtrToMBuf(region, pool, ptr, totalLen, ptrLen, mBuf)
8243 Region   region;
8244 Pool     pool;
8245 Data    *ptr;
8246 MsgLen   totalLen;
8247 MsgLen   ptrLen;
8248 Buffer** mBuf;
8249 #endif
8250 #endif
8251 {
8252    SsMsgInfo *minfo;   /* Message info */
8253    Size       mdsize;
8254    SsDblk    *dptr;
8255    Buffer    *newblk;
8256    /*   Void      *iccHdlr; */
8257
8258
8259 #if (ERRCLASS & ERRCLS_INT_PAR)
8260    if (ptr == NULLP)
8261    {
8262       SSLOGERROR(ERRCLS_INT_PAR, ESSXXX, ERRZERO, "SAttachPtrToBuf: Null PTR");
8263       return RFAILED;
8264    }
8265 #endif
8266
8267    if(SGetMsg(region, pool, mBuf) != ROK)
8268    {
8269       SSLOGERROR(ERRCLS_INT_PAR, ESSXXX, ERRZERO, "SAttachPtrToBuf: SGetMsg Failed");
8270       return (ROUTRES);
8271    }
8272
8273    region = 0;
8274    mdsize = MDBSIZE;
8275
8276 #ifdef SS_HISTOGRAM_SUPPORT 
8277    if (SAlloc(region, &mdsize, 0, (Data **) &newblk, __LINE__, (uint8_t*) __FILE__, ENTNC) != ROK)
8278 #else
8279    if (SAlloc(region, &mdsize, 0, (Data **) &newblk) != ROK)
8280 #endif /* SS_HISTOGRAM_SUPPORT */
8281    {
8282        SSLOGERROR(ERRCLS_INT_PAR, ESSXXX, ERRZERO, "SAttachPtrToBuf: SAlloc Failed");
8283        SPutMsg(*mBuf);
8284        return (ROUTRES);
8285    }
8286
8287    (*mBuf)->b_cont = newblk;
8288
8289    dptr = (SsDblk*) (((Data *) (newblk)) + MBSIZE);
8290
8291    INITB((newblk), dptr, (Data*) NULL, 0, NULLP)
8292
8293
8294    newblk->b_datap->db_base = ptr;
8295    newblk->b_datap->db_lim = ptr + ptrLen;
8296    newblk->b_rptr = newblk->b_datap->db_base;
8297    newblk->b_wptr = newblk->b_rptr + totalLen;
8298
8299 #ifndef SS_DBUF_REFLOCK_DISABLE
8300    if((SInitLock (&(dptr->dBufLock), SS_LOCK_MUTEX)) != 0)
8301    {
8302       printf("Falied to destroy lock\n");
8303    }
8304 #endif
8305
8306    dptr->db_type = SS_MEM_TYPE_SSI_ZBC; 
8307
8308    /* get the SsMsgInfo of mBuf */
8309    minfo = (SsMsgInfo*) (*mBuf)->b_rptr;
8310    minfo->len    = totalLen;
8311    minfo->endptr = newblk;
8312
8313    //printf("Mbuf PTR = %x, DBlk PTR = %x, PTR = %x\n", *mBuf, newblk, ptr);
8314    return ROK;
8315 }
8316 #endif /* INTEL_WLS */
8317 S16 SIncMsgRef(Buffer *srcBuf,Region dstRegion, Pool dstPool,Buffer **dstBuf)
8318 {
8319 #ifndef L2_OPTMZ 
8320   return (SAddMsgRef(srcBuf,dstRegion, dstPool,dstBuf));
8321 #else 
8322  *dstBuf = srcBuf;
8323 #endif 
8324   return ROK;
8325 }
8326 #ifdef L2_OPTMZ
8327 Void SResetMBuf(Buffer *mbuf)
8328 {
8329    SsMsgInfo *minfo;
8330    Buffer *tmp;
8331 #if 1
8332
8333    minfo = (SsMsgInfo *) mbuf->b_rptr;
8334    //tmp   = minfo->endptr;
8335    tmp   = mbuf->b_cont;
8336
8337    minfo->len  = 0;
8338    if(tmp)
8339    {
8340
8341     //    printf("SResetMBuf The write & read ptr is %p %p %p %p \n", tmp->b_wptr, tmp->b_rptr, tmp->b_datap->db_base, tmp->b_datap->db_lim );
8342 //      tmp->b_wptr = tmp->b_rptr = tmp->b_datap->db_base;
8343       tmp->b_wptr = tmp->b_rptr = (tmp->b_datap->db_base + 5);
8344       tmp->b_datap->db_ref = 1;
8345       tmp->b_datap->shared = 0;
8346    }
8347 #endif
8348
8349
8350    return;
8351 }
8352 #endif
8353
8354 #ifdef INTEL_WLS
8355 /*
8356 *
8357 *       Fun:   SAttachWlsPtrToMBuf 
8358 *
8359 *       Desc:  This function attaches the Pointer provided into a new
8360 *              message buffer after allocating the same. It allocates
8361 *              header (M-Block) and an additional dBuf (D-Block) and attaches
8362 *              the provided pointer to it.
8363 *
8364 *       Ret:   ROK      - ok
8365 *              RFAILED  - failed, general (optional)
8366 *              ROUTRES  - failed, out of resources (optional)
8367 *
8368 *       Notes: 
8369 *
8370 *       File:  ss_msg.c
8371 *
8372 */
8373 #ifdef T2K_MEM_LEAK_DBG
8374 S16 SAttachWlsPtrToMBuf1
8375 (
8376 Region   region,
8377 Pool     pool,
8378 Data    *ptr,
8379 Data    *readPtr,
8380 MsgLen   totalLen,
8381 MsgLen   ptrLen,
8382 Buffer** mBuf,
8383 char* file,
8384 uint32_t line
8385 )
8386 #else
8387 #ifdef ANSI
8388 S16 SAttachWlsPtrToMBuf
8389 (
8390 Region   region,
8391 Pool     pool,
8392 Data    *ptr,
8393 Data    *readPtr,
8394 MsgLen   totalLen,
8395 MsgLen   ptrLen,
8396 Buffer** mBuf
8397 )
8398 #else
8399 S16 SAttachWlsPtrToMBuf(region, pool, ptr, readPtr, totalLen, ptrLen, mBuf)
8400 Region   region;
8401 Pool     pool;
8402 Data    *ptr;
8403 Data    *readPtr;
8404 MsgLen   totalLen;
8405 MsgLen   ptrLen;
8406 Buffer** mBuf;
8407 #endif
8408 #endif
8409 {
8410    SsMsgInfo *minfo;   /* Message info */
8411    Size       mdsize;
8412    SsDblk    *dptr;
8413    Buffer    *newblk;
8414    /*   Void      *iccHdlr; */
8415
8416
8417 #if (ERRCLASS & ERRCLS_INT_PAR)
8418    if (ptr == NULLP)
8419    {
8420       SSLOGERROR(ERRCLS_INT_PAR, ESSXXX, ERRZERO, "SAttachPtrToBuf: Null PTR");
8421       return RFAILED;
8422    }
8423 #endif
8424
8425    region = 0;
8426    if(SGetMsg(region, pool, mBuf) != ROK)
8427    {
8428       SSLOGERROR(ERRCLS_INT_PAR, ESSXXX, ERRZERO, "SAttachPtrToBuf: SGetMsg Failed");
8429       return (ROUTRES);
8430    }
8431
8432    mdsize = MDBSIZE;
8433
8434 #ifdef SS_HISTOGRAM_SUPPORT 
8435    if (SAlloc(region, &mdsize, 0, (Data **) &newblk, __LINE__, (uint8_t*) __FILE__, ENTNC) != ROK)
8436 #else
8437    if (SAlloc(region, &mdsize, 0, (Data **) &newblk) != ROK)
8438 #endif /* SS_HISTOGRAM_SUPPORT */
8439    {
8440        SSLOGERROR(ERRCLS_INT_PAR, ESSXXX, ERRZERO, "SAttachPtrToBuf: SAlloc Failed");
8441        SPutMsg(*mBuf);
8442        return (ROUTRES);
8443    }
8444
8445    (*mBuf)->b_cont = newblk;
8446
8447    dptr = (SsDblk*) (((Data *) (newblk)) + MBSIZE);
8448
8449    INITB((newblk), dptr, (Data*) NULL, 0, NULLP)
8450
8451
8452    newblk->b_datap->db_base = ptr;
8453    newblk->b_datap->db_lim  = ptr + ptrLen;
8454    newblk->b_rptr           = readPtr;
8455    newblk->b_wptr           = newblk->b_rptr + totalLen;
8456
8457 #ifndef SS_DBUF_REFLOCK_DISABLE
8458    if((SInitLock (&(dptr->dBufLock), SS_LOCK_MUTEX)) != 0)
8459    {
8460       printf("Falied to destroy lock\n");
8461    }
8462 #endif
8463
8464    dptr->db_type = SS_MEM_TYPE_SSI_ZBC; 
8465
8466    /* get the SsMsgInfo of mBuf */
8467    minfo         = (SsMsgInfo*) (*mBuf)->b_rptr;
8468    minfo->len    = totalLen;
8469    minfo->endptr = newblk;
8470
8471    //printf("Mbuf PTR = %x, DBlk PTR = %x, PTR = %x\n", *mBuf, newblk, ptr);
8472    return ROK;
8473 }
8474
8475 #ifdef TENB_DPDK_BUF
8476
8477 uint32_t numeTti;
8478
8479 S16 SGetSBufDpdk
8480 (
8481 Data **ptr,                     /* pointer to buffer */
8482 Size size                       /* size requested */
8483 )
8484 {
8485     S16   ret=ROK;
8486     
8487     *ptr = ntl_alloc(mtGetNtlHdl(), size);
8488
8489     return (ret);
8490 }
8491
8492 S16 SPutSBufDpdk
8493 (
8494 Data *ptr                     /* pointer to buffer */
8495 )
8496 {
8497     S16   ret;
8498     uint32_t   flags = 0;
8499
8500     ntl_free(mtGetNtlHdl(), ptr);
8501
8502     return (ret);
8503 }
8504
8505 S16 SDetachDpdkPtrFrmDBuf
8506 (
8507 Buffer     *mBuf,
8508 Data       **ptr
8509 )
8510 {
8511
8512     Buffer       *msgBlk;
8513     SsMsgInfo    *minfo;
8514
8515     //msgBlk = mBuf->b_cont;
8516     //*ptr   = msgBlk->b_rptr;
8517     *ptr   = mBuf->b_datap->db_base;
8518
8519     mBuf->b_cont = NULL;
8520
8521     //minfo         = (SsMsgInfo*) mBuf->b_rptr;
8522     //minfo->len    = 0;
8523
8524
8525     return ROK;
8526 }
8527
8528
8529 S16 SDetachDpdkPtrFrmMBuf
8530 (
8531  Buffer     *mBuf,
8532  Data       **ptr
8533  )
8534 {
8535
8536    Buffer       *msgBlk;
8537    SsMsgInfo    *minfo;
8538    register     SsDblk *dptr;
8539
8540
8541
8542    msgBlk = mBuf->b_cont;
8543    if(msgBlk == NULLP)
8544    {
8545       *ptr = NULLP;
8546        return RFAILED;
8547    }
8548       
8549    dptr = msgBlk->b_datap;
8550    if(dptr->db_type != SS_MEM_TYPE_DPDK_ZBC)
8551    {
8552       *ptr = NULLP;
8553       return RFAILED;
8554    }   
8555
8556    *ptr   = msgBlk->b_rptr;
8557
8558    mBuf->b_cont = NULL;
8559
8560    minfo         = (SsMsgInfo*) mBuf->b_rptr;
8561    minfo->len    = 0;
8562
8563    return ROK;
8564 }
8565
8566
8567 #ifdef ANSI
8568 S16 SAttachDpdkPtrToMBuf
8569 (
8570 Region   region,
8571 Pool     pool,
8572 Data    *ptr,
8573 Data    *readPtr,
8574 MsgLen   msgLen,
8575 MsgLen   totalLen,
8576 Buffer** mBuf
8577 )
8578 #else
8579 S16 SAttachDpdkPtrToMBuf(region, pool, ptr, readPtr, msgLen, totalLen, mBuf)
8580 Region   region;
8581 Pool     pool;
8582 Data    *ptr;
8583 Data    *readPtr;
8584 MsgLen   msgLen;
8585 MsgLen   totalLen;
8586 Buffer** mBuf;
8587 #endif
8588 {
8589
8590    SsDblk    *dptr;
8591
8592    if(0 == msgLen)
8593       return RFAILED;
8594
8595    SAttachWlsPtrToMBuf(region, pool, ptr, readPtr, msgLen, totalLen, mBuf);
8596    dptr = (SsDblk*) (((Data *) ((*mBuf)->b_cont)) + MBSIZE);
8597    
8598    dptr->db_type = SS_MEM_TYPE_DPDK_ZBC;
8599
8600    return ROK;
8601 }
8602
8603 #endif /* TENB_DPDK_BUF */
8604
8605 #endif /* INTEL_WLS */
8606 \f
8607 /**********************************************************************
8608          End of file
8609 **********************************************************************/
8610