| 1 /* |
|
| 2 The mediastreamer library aims at providing modular media processing and I/O |
|
| 3 for linphone, but also for any telephony application. |
|
| 4 Copyright (C) 2001 Simon MORLAT simon.morlat@linphone.org |
|
| 5 |
|
| 6 This library is free software; you can redistribute it and/or |
|
| 7 modify it under the terms of the GNU Lesser General Public |
|
| 8 License as published by the Free Software Foundation; either |
|
| 9 version 2.1 of the License, or (at your option) any later version. |
|
| 10 |
|
| 11 This library is distributed in the hope that it will be useful, |
|
| 12 but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
| 14 Lesser General Public License for more details. |
|
| 15 |
|
| 16 You should have received a copy of the GNU Lesser General Public |
|
| 17 License along with this library; if not, write to the Free Software |
|
| 18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
| 19 */ |
|
| 20 |
|
| 21 #include "msbuffer.h" |
|
| 22 #include "msutils.h" |
|
| 23 #include <string.h> |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 MSBuffer * ms_buffer_new(guint32 size) |
|
| 28 { |
|
| 29 MSBuffer *buf; |
|
| 30 buf=(MSBuffer*)g_malloc(sizeof(MSBuffer)+size); |
|
| 31 buf->ref_count=0; |
|
| 32 buf->size=size; |
|
| 33 ms_trace("ms_buffer_new: Allocating buffer of %i bytes.",size); |
|
| 34 /* allocate the data buffer: there is a lot of optmisation that can be done by using a pool of cached buffers*/ |
|
| 35 buf->buffer=((char*)(buf))+sizeof(MSBuffer); /* to avoid to do two allocations, |
|
| 36 buffer info and buffer are contigous.*/ |
|
| 37 buf->flags=MS_BUFFER_CONTIGUOUS; |
|
| 38 return(buf); |
|
| 39 } |
|
| 40 |
|
| 41 MSBuffer *ms_buffer_alloc(gint flags) |
|
| 42 { |
|
| 43 MSBuffer *buf; |
|
| 44 buf=(MSBuffer*)g_malloc(sizeof(MSBuffer)); |
|
| 45 buf->ref_count=0; |
|
| 46 buf->size=0; |
|
| 47 buf->buffer=NULL; |
|
| 48 buf->flags=0; |
|
| 49 return(buf); |
|
| 50 } |
|
| 51 |
|
| 52 |
|
| 53 void ms_buffer_destroy(MSBuffer *buf) |
|
| 54 { |
|
| 55 if (buf->flags & MS_BUFFER_CONTIGUOUS){ |
|
| 56 g_free(buf); |
|
| 57 } |
|
| 58 else { |
|
| 59 g_free(buf->buffer); |
|
| 60 g_free(buf); |
|
| 61 } |
|
| 62 } |
|
| 63 |
|
| 64 MSMessage *ms_message_alloc() |
|
| 65 { |
|
| 66 MSMessage *m=g_malloc(sizeof(MSMessage)); |
|
| 67 memset(m,0,sizeof(MSMessage)); |
|
| 68 return m; |
|
| 69 } |
|
| 70 |
|
| 71 MSMessage *ms_message_new(gint size) |
|
| 72 { |
|
| 73 MSMessage *m=ms_message_alloc(); |
|
| 74 MSBuffer *buf=ms_buffer_new(size); |
|
| 75 ms_message_set_buf(m,buf); |
|
| 76 return m; |
|
| 77 } |
|
| 78 |
|
| 79 void ms_message_destroy(MSMessage *m) |
|
| 80 { |
|
| 81 /* the buffer is freed if its ref_count goes to zero */ |
|
| 82 if (m->buffer!=NULL){ |
|
| 83 m->buffer->ref_count--; |
|
| 84 if (m->buffer->ref_count==0) ms_buffer_destroy(m->buffer); |
|
| 85 } |
|
| 86 g_free(m); |
|
| 87 } |
|
| 88 |
|
| 89 MSMessage * ms_message_dup(MSMessage *m) |
|
| 90 { |
|
| 91 MSMessage *msg=ms_message_alloc(); |
|
| 92 ms_message_set_buf(msg,m->buffer); |
|
| 93 return msg; |
|
| 94 } |
|