xdas_buf.c

Go to the documentation of this file.
00001 /*----------------------------------------------------------------------------
00002  * Copyright (c) 2006, Novell, Inc.
00003  * All rights reserved.
00004  * 
00005  * Redistribution and use in source and binary forms, with or without 
00006  * modification, are permitted provided that the following conditions are 
00007  * met:
00008  * 
00009  *     * Redistributions of source code must retain the above copyright 
00010  *       notice, this list of conditions and the following disclaimer.
00011  *     * Redistributions in binary form must reproduce the above copyright 
00012  *       notice, this list of conditions and the following disclaimer in the 
00013  *       documentation and/or other materials provided with the distribution.
00014  *     * Neither the name of the Novell nor the names of its contributors 
00015  *       may be used to endorse or promote products derived from this 
00016  *       software without specific prior written permission.
00017  * 
00018  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
00019  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
00020  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
00021  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
00022  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00023  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
00024  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
00025  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
00026  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
00027  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  *--------------------------------------------------------------------------*/
00030 
00044 #include "xdas_buf.h"
00045 #include "xdas_debug.h"
00046 
00047 #include <malloc.h>
00048 #include <string.h>
00049 
00050 /* set a reasonable maximum buffer size */
00051 #define MAX_BUFFER_SIZE (8 * 64 * 1024)
00052 
00063 xdas_buffer xdas_buffer_alloc(size_t size)
00064 {
00065    xdas_buffer result;
00066 
00067    xdas_assert(size <= MAX_BUFFER_SIZE);
00068    if (size > MAX_BUFFER_SIZE)
00069       return 0;
00070 
00071    if ((result = (xdas_buffer)malloc(sizeof(*result) + size + 1)) != 0)
00072    {
00073       result->allocated = size;
00074       result->start = (char *)(result + 1);
00075       result->curpos = result->start;
00076       result->end = result->start + size;
00077    } 
00078    return result;
00079 }
00080 
00094 xdas_buffer xdas_buffer_realloc(xdas_buffer buf, size_t size)
00095 {
00096    xdas_buffer result;
00097 
00098    xdas_assert(size <= MAX_BUFFER_SIZE);
00099    if (size > MAX_BUFFER_SIZE)
00100       return 0;
00101 
00102    if (buf)
00103    {
00104       size_t curoffs = buf->curpos - buf->start;
00105 
00106       xdas_assert(buf->curpos <= buf->end);
00107 
00108       if (buf->allocated >= size)
00109          result = buf;
00110       else
00111       {
00112          if ((result = (xdas_buffer)realloc(buf, sizeof(*result) + size)) != 0)
00113             result->allocated = size;
00114       }
00115       if (result)
00116       {
00117          result->start = (char *)(result + 1);
00118          result->curpos = result->start + curoffs;
00119          result->end = result->start + size;
00120       }
00121    }
00122    else
00123       result = xdas_buffer_alloc(size);
00124    return result;
00125 }
00126 
00136 xdas_buffer xdas_buffer_dup(xdas_buffer buf)
00137 {
00138    xdas_buffer dup = 0;
00139    xdas_assert(!buf || buf->curpos <= buf->end);
00140    if (buf && (dup = xdas_buffer_alloc(buf->end - buf->start)) != 0)
00141       memcpy(dup->start, buf->start, buf->end - buf->start);
00142    return dup;
00143 }
00144 
00154 void xdas_buffer_free(xdas_buffer buf)
00155 {
00156    xdas_assert(!buf || buf->curpos <= buf->end);
00157    free(buf);
00158 }
00159 

Generated on Thu Aug 20 22:33:05 2009 for OpenXDAS by  doxygen 1.5.6