2 The oRTP library is an RTP (Realtime Transport Protocol - rfc1889) stack.
3 Copyright (C) 2001 Simon MORLAT simon.morlat@linphone.org
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include "ortp/ortp.h"
22 #include "ortp/str_utils.h"
25 void qinit(queue_t *q){
26 mblk_init(&q->_q_stopper);
27 q->_q_stopper.b_next=&q->_q_stopper;
28 q->_q_stopper.b_prev=&q->_q_stopper;
32 void mblk_init(mblk_t *mp)
34 mp->b_cont=mp->b_prev=mp->b_next=NULL;
35 mp->b_rptr=mp->b_wptr=NULL;
40 dblk_t *datab_alloc(int size){
42 int total_size=sizeof(dblk_t)+size;
43 db=(dblk_t *) ortp_malloc(total_size);
44 db->db_base=(uint8_t*)db+sizeof(dblk_t);
45 db->db_lim=db->db_base+size;
47 db->db_freefn=NULL; /* the buffer pointed by db_base must never be freed !*/
51 static inline void datab_ref(dblk_t *d){
55 static inline void datab_unref(dblk_t *d){
58 if (d->db_freefn!=NULL)
59 d->db_freefn(d->db_base);
65 mblk_t *allocb(int size, int pri)
70 mp=(mblk_t *) ortp_malloc(sizeof(mblk_t));
72 datab=datab_alloc(size);
75 mp->b_rptr=mp->b_wptr=datab->db_base;
76 mp->b_next=mp->b_prev=mp->b_cont=NULL;
80 mblk_t *esballoc(uint8_t *buf, int size, int pri, void (*freefn)(void*) )
85 mp=(mblk_t *) ortp_malloc(sizeof(mblk_t));
87 datab=(dblk_t *) ortp_malloc(sizeof(dblk_t));
91 datab->db_lim=buf+size;
93 datab->db_freefn=freefn;
96 mp->b_rptr=mp->b_wptr=buf;
97 mp->b_next=mp->b_prev=mp->b_cont=NULL;
102 void freeb(mblk_t *mp)
104 return_if_fail(mp->b_datap!=NULL);
105 return_if_fail(mp->b_datap->db_base!=NULL);
107 datab_unref(mp->b_datap);
111 void freemsg(mblk_t *mp)
123 mblk_t *dupb(mblk_t *mp)
126 return_val_if_fail(mp->b_datap!=NULL,NULL);
127 return_val_if_fail(mp->b_datap->db_base!=NULL,NULL);
129 datab_ref(mp->b_datap);
130 newm=(mblk_t *) ortp_malloc(sizeof(mblk_t));
132 newm->reserved1=mp->reserved1;
133 newm->reserved2=mp->reserved2;
134 newm->b_datap=mp->b_datap;
135 newm->b_rptr=mp->b_rptr;
136 newm->b_wptr=mp->b_wptr;
140 /* duplicates a complex mblk_t */
141 mblk_t *dupmsg(mblk_t* m)
143 mblk_t *newm=NULL,*mp,*prev;
155 void putq(queue_t *q,mblk_t *mp)
157 q->_q_stopper.b_prev->b_next=mp;
158 mp->b_prev=q->_q_stopper.b_prev;
159 mp->b_next=&q->_q_stopper;
160 q->_q_stopper.b_prev=mp;
164 mblk_t *getq(queue_t *q)
167 tmp=q->_q_stopper.b_next;
168 if (tmp==&q->_q_stopper) return NULL;
169 q->_q_stopper.b_next=tmp->b_next;
170 tmp->b_next->b_prev=&q->_q_stopper;
177 mblk_t * peekq(queue_t *q){
179 tmp=q->_q_stopper.b_next;
180 if (tmp==&q->_q_stopper) return NULL;
184 /* insert mp in q just before emp */
185 void insq(queue_t *q,mblk_t *emp, mblk_t *mp)
192 emp->b_prev->b_next=mp;
193 mp->b_prev=emp->b_prev;
198 void remq(queue_t *q, mblk_t *mp){
200 mp->b_prev->b_next=mp->b_next;
201 mp->b_next->b_prev=mp->b_prev;
206 /* remove and free all messages in the q */
207 void flushq(queue_t *q, int how)
211 while ((mp=getq(q))!=NULL)
217 int msgdsize(const mblk_t *mp)
221 msgsize+=(int) (mp->b_wptr-mp->b_rptr);
227 void msgpullup(mblk_t *mp,int len)
233 if (mp->b_cont==NULL && len==-1) return; /*nothing to do, message is not fragmented */
235 if (len==-1) len=msgdsize(mp);
237 while(wlen<len && mp!=NULL){
239 int mlen=mp->b_wptr-mp->b_rptr;
241 memcpy(&db->db_base[wlen],mp->b_rptr,mlen);
245 memcpy(&db->db_base[wlen],mp->b_rptr,remain);
249 /*set firstm to point to the new datab */
250 freemsg(firstm->b_cont);
252 datab_unref(firstm->b_datap);
254 firstm->b_rptr=db->db_base;
255 firstm->b_wptr=firstm->b_rptr+wlen;
259 mblk_t *copyb(mblk_t *mp)
262 int len=(int) (mp->b_wptr-mp->b_rptr);
263 newm=allocb(len,BPRI_MED);
264 memcpy(newm->b_wptr,mp->b_rptr,len);
269 mblk_t *copymsg(mblk_t *mp)
282 mblk_t * appendb(mblk_t *mp, const char *data, int size, bool_t pad){
286 padcnt= (int)(4L-( (long)(((long)mp->b_wptr)+size) % 4L)) % 4L;
288 if ((mp->b_wptr + size +padcnt) > mp->b_datap->db_lim){
289 /* buffer is not large enough: append a new block (with the same size ?)*/
290 int plen=(int)((char*)mp->b_datap->db_lim - (char*) mp->b_datap->db_base);
291 mp->b_cont=allocb(MAX(plen,size),0);
294 if (size) memcpy(mp->b_wptr,data,size);
296 for (i=0;i<padcnt;i++){
303 void msgappend(mblk_t *mp, const char *data, int size, bool_t pad){
304 while(mp->b_cont!=NULL) mp=mp->b_cont;
305 appendb(mp,data,size,pad);
308 mblk_t *concatb(mblk_t *mp, mblk_t *newm){
309 while (mp->b_cont!=NULL) mp=mp->b_cont;
311 while(newm->b_cont!=NULL) newm=newm->b_cont;
315 void msgb_allocator_init(msgb_allocator_t *a){
319 mblk_t *msgb_allocator_alloc(msgb_allocator_t *a, int size){
321 mblk_t *m,*found=NULL;
323 /*lookup for an unused msgb (data block with ref count ==1)*/
324 for(m=qbegin(q);!qend(q,m);m=qnext(q,m)){
325 if (m->b_datap->db_ref==1 && m->b_datap->db_lim-m->b_datap->db_base>=size){
331 found=allocb(size,0);
337 void msgb_allocator_uninit(msgb_allocator_t *a){