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