media/mtransport/third_party/nrappkit/src/util/p_buf.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/mtransport/third_party/nrappkit/src/util/p_buf.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,217 @@
     1.4 +/**
     1.5 +   p_buf.c
     1.6 +
     1.7 +
     1.8 +   Copyright (C) 2003, Network Resonance, Inc.
     1.9 +   Copyright (C) 2006, Network Resonance, Inc.
    1.10 +   All Rights Reserved
    1.11 +
    1.12 +   Redistribution and use in source and binary forms, with or without
    1.13 +   modification, are permitted provided that the following conditions
    1.14 +   are met:
    1.15 +
    1.16 +   1. Redistributions of source code must retain the above copyright
    1.17 +      notice, this list of conditions and the following disclaimer.
    1.18 +   2. Redistributions in binary form must reproduce the above copyright
    1.19 +      notice, this list of conditions and the following disclaimer in the
    1.20 +      documentation and/or other materials provided with the distribution.
    1.21 +   3. Neither the name of Network Resonance, Inc. nor the name of any
    1.22 +      contributors to this software may be used to endorse or promote
    1.23 +      products derived from this software without specific prior written
    1.24 +      permission.
    1.25 +
    1.26 +   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
    1.27 +   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.28 +   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.29 +   ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
    1.30 +   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    1.31 +   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    1.32 +   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    1.33 +   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    1.34 +   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    1.35 +   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    1.36 +   POSSIBILITY OF SUCH DAMAGE.
    1.37 +
    1.38 +
    1.39 +   All Rights Reserved.
    1.40 +
    1.41 +   ekr@rtfm.com  Tue Nov 25 16:33:08 2003
    1.42 + */
    1.43 +
    1.44 +static char *RCSSTRING __UNUSED__ ="Id: p_buf.c,v 1.3 2004/01/03 22:13:53 ekr Exp $";
    1.45 +
    1.46 +#include <string.h>
    1.47 +#include <stddef.h>
    1.48 +#include "nr_common.h"
    1.49 +#include "p_buf.h"
    1.50 +
    1.51 +
    1.52 +static int nr_p_buf_destroy_chain(nr_p_buf_head *head);
    1.53 +static int nr_p_buf_destroy(nr_p_buf *buf);
    1.54 +
    1.55 +int nr_p_buf_ctx_create(size,ctxp)
    1.56 +  int size;
    1.57 +  nr_p_buf_ctx **ctxp;
    1.58 +  {
    1.59 +    int _status;
    1.60 +    nr_p_buf_ctx *ctx=0;
    1.61 +
    1.62 +    if(!(ctx=(nr_p_buf_ctx *)RCALLOC(sizeof(nr_p_buf_ctx))))
    1.63 +      ABORT(R_NO_MEMORY);
    1.64 +
    1.65 +    ctx->buf_size=size;
    1.66 +    STAILQ_INIT(&ctx->free_list);
    1.67 +
    1.68 +    *ctxp=ctx;
    1.69 +    _status=0;
    1.70 +  abort:
    1.71 +    if(_status){
    1.72 +      nr_p_buf_ctx_destroy(&ctx);
    1.73 +    }
    1.74 +    return(_status);
    1.75 +  }
    1.76 +
    1.77 +int nr_p_buf_ctx_destroy(ctxp)
    1.78 +  nr_p_buf_ctx **ctxp;
    1.79 +  {
    1.80 +    nr_p_buf_ctx *ctx;
    1.81 +
    1.82 +    if(!ctxp || !*ctxp)
    1.83 +      return(0);
    1.84 +
    1.85 +    ctx=*ctxp;
    1.86 +
    1.87 +    nr_p_buf_destroy_chain(&ctx->free_list);
    1.88 +
    1.89 +    RFREE(ctx);
    1.90 +    *ctxp=0;
    1.91 +
    1.92 +    return(0);
    1.93 +  }
    1.94 +
    1.95 +int nr_p_buf_alloc(ctx,bufp)
    1.96 +  nr_p_buf_ctx *ctx;
    1.97 +  nr_p_buf **bufp;
    1.98 +  {
    1.99 +    int _status;
   1.100 +    nr_p_buf *buf=0;
   1.101 +
   1.102 +    if(!STAILQ_EMPTY(&ctx->free_list)){
   1.103 +      buf=STAILQ_FIRST(&ctx->free_list);
   1.104 +      STAILQ_REMOVE_HEAD(&ctx->free_list,entry);
   1.105 +      goto ok;
   1.106 +    }
   1.107 +    else {
   1.108 +      if(!(buf=(nr_p_buf *)RCALLOC(sizeof(nr_p_buf))))
   1.109 +        ABORT(R_NO_MEMORY);
   1.110 +      if(!(buf->data=(UCHAR *)RMALLOC(ctx->buf_size)))
   1.111 +        ABORT(R_NO_MEMORY);
   1.112 +      buf->size=ctx->buf_size;
   1.113 +    }
   1.114 +
   1.115 +  ok:
   1.116 +     buf->r_offset=0;
   1.117 +     buf->length=0;
   1.118 +
   1.119 +     *bufp=buf;
   1.120 +    _status=0;
   1.121 +  abort:
   1.122 +     if(_status){
   1.123 +       nr_p_buf_destroy(buf);
   1.124 +     }
   1.125 +    return(_status);
   1.126 +  }
   1.127 +
   1.128 +int nr_p_buf_free(ctx,buf)
   1.129 +  nr_p_buf_ctx *ctx;
   1.130 +  nr_p_buf *buf;
   1.131 +  {
   1.132 +    STAILQ_INSERT_TAIL(&ctx->free_list,buf,entry);
   1.133 +
   1.134 +    return(0);
   1.135 +  }
   1.136 +
   1.137 +int nr_p_buf_free_chain(ctx,head)
   1.138 +  nr_p_buf_ctx *ctx;
   1.139 +  nr_p_buf_head *head;
   1.140 +  {
   1.141 +    nr_p_buf *n1,*n2;
   1.142 +
   1.143 +    n1=STAILQ_FIRST(head);
   1.144 +    while(n1){
   1.145 +      n2=STAILQ_NEXT(n1,entry);
   1.146 +
   1.147 +      nr_p_buf_free(ctx,n1);
   1.148 +
   1.149 +      n1=n2;
   1.150 +    }
   1.151 +
   1.152 +    return(0);
   1.153 +  }
   1.154 +
   1.155 +
   1.156 +int nr_p_buf_write_to_chain(ctx,chain,data,len)
   1.157 +  nr_p_buf_ctx *ctx;
   1.158 +  nr_p_buf_head *chain;
   1.159 +  UCHAR *data;
   1.160 +  UINT4 len;
   1.161 +  {
   1.162 +    int r,_status;
   1.163 +    nr_p_buf *buf;
   1.164 +
   1.165 +    buf=STAILQ_LAST(chain,nr_p_buf_,entry);
   1.166 +    while(len){
   1.167 +      int towrite;
   1.168 +
   1.169 +      if(!buf){
   1.170 +        if(r=nr_p_buf_alloc(ctx,&buf))
   1.171 +          ABORT(r);
   1.172 +        STAILQ_INSERT_TAIL(chain,buf,entry);
   1.173 +      }
   1.174 +
   1.175 +      towrite=MIN(len,(buf->size-(buf->length+buf->r_offset)));
   1.176 +
   1.177 +      memcpy(buf->data+buf->length+buf->r_offset,data,towrite);
   1.178 +      len-=towrite;
   1.179 +      data+=towrite;
   1.180 +      buf->length+=towrite;
   1.181 +
   1.182 +      r_log(LOG_COMMON,LOG_DEBUG,"Wrote %d bytes to buffer %p",towrite,buf);
   1.183 +      buf=0;
   1.184 +    }
   1.185 +
   1.186 +    _status=0;
   1.187 +  abort:
   1.188 +    return(_status);
   1.189 +  }
   1.190 +
   1.191 +static int nr_p_buf_destroy_chain(head)
   1.192 +  nr_p_buf_head *head;
   1.193 +  {
   1.194 +    nr_p_buf *n1,*n2;
   1.195 +
   1.196 +    n1=STAILQ_FIRST(head);
   1.197 +    while(n1){
   1.198 +      n2=STAILQ_NEXT(n1,entry);
   1.199 +
   1.200 +      nr_p_buf_destroy(n1);
   1.201 +
   1.202 +      n1=n2;
   1.203 +    }
   1.204 +
   1.205 +    return(0);
   1.206 +  }
   1.207 +
   1.208 +static int nr_p_buf_destroy(buf)
   1.209 +  nr_p_buf *buf;
   1.210 +  {
   1.211 +    if(!buf)
   1.212 +      return(0);
   1.213 +
   1.214 +    RFREE(buf->data);
   1.215 +    RFREE(buf);
   1.216 +
   1.217 +    return(0);
   1.218 +  }
   1.219 +
   1.220 +

mercurial