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