1 /******************************************************************************
2  * $Id$
3  *
4  * Project:  MapServer
5  * Purpose:  MapCache tile caching support file: automatic expanding buffer
6  * Author:   Thomas Bonfort and the MapServer team.
7  *
8  ******************************************************************************
9  * Copyright (c) 1996-2011 Regents of the University of Minnesota.
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining a
12  * copy of this software and associated documentation files (the "Software"),
13  * to deal in the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15  * and/or sell copies of the Software, and to permit persons to whom the
16  * Software is furnished to do so, subject to the following conditions:
17  *
18  * The above copyright notice and this permission notice shall be included in
19  * all copies of this Software or works derived from this Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  *****************************************************************************/
29 
30 #include "mapcache.h"
31 #include <stdlib.h>
32 #define INITIAL_BUFFER_SIZE 100
33 
_mapcache_buffer_realloc(mapcache_buffer * buffer,apr_off_t len)34 static void _mapcache_buffer_realloc(mapcache_buffer *buffer, apr_off_t len)
35 {
36   if(buffer->avail) {
37     unsigned char* newbuf ;
38     while ( len > buffer->avail ) {
39       buffer->avail += buffer->avail;
40     }
41     newbuf = realloc(buffer->buf, buffer->avail) ;
42     if ( newbuf != buffer->buf ) {
43       if ( buffer->buf )
44         apr_pool_cleanup_kill(buffer->pool, buffer->buf, (void*)free) ;
45       apr_pool_cleanup_register(buffer->pool, newbuf,(void*)free, apr_pool_cleanup_null);
46       buffer->buf = newbuf ;
47     }
48   } else {
49     buffer->avail = len;
50     buffer->buf = malloc(buffer->avail);
51     apr_pool_cleanup_register(buffer->pool, buffer->buf,(void*)free, apr_pool_cleanup_null);
52   }
53 }
54 
mapcache_buffer_create(size_t initialStorage,apr_pool_t * pool)55 mapcache_buffer *mapcache_buffer_create(size_t initialStorage, apr_pool_t* pool)
56 {
57   mapcache_buffer *buffer = apr_pcalloc(pool, sizeof(mapcache_buffer));
58   if(!buffer) return NULL;
59   buffer->pool = pool;
60   buffer->avail = initialStorage;
61   if(buffer->avail) {
62     buffer->buf = malloc(buffer->avail);
63     apr_pool_cleanup_register(buffer->pool, buffer->buf,(void*)free, apr_pool_cleanup_null);
64   } else {
65     buffer->buf = NULL;
66   }
67   return buffer;
68 }
69 
mapcache_buffer_append(mapcache_buffer * buffer,size_t len,void * data)70 int mapcache_buffer_append(mapcache_buffer *buffer, size_t len, void *data)
71 {
72   size_t total = buffer->size + len;
73   if(total > buffer->avail)
74     _mapcache_buffer_realloc(buffer,total);
75 
76   memcpy(((unsigned char*)buffer->buf) + buffer->size, data, len);
77 
78   buffer->size += len;
79   return len;
80 }
81 /* vim: ts=2 sts=2 et sw=2
82 */
83