/* -*- Mode: C -*- ====================================================================== FILE: icalmemory.c CREATOR: eric 30 June 1999 $Id$ $Locker$ The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.mozilla.org/MPL/ Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. This program is free software; you can redistribute it and/or modify it under the terms of either: The LGPL as published by the Free Software Foundation, version 2.1, available at: http://www.fsf.org/copyleft/lesser.html Or: The Mozilla Public License Version 1.0. You may obtain a copy of the License at http://www.mozilla.org/MPL/ The Original Code is icalmemory.h ======================================================================*/ /* libical often passes strings back to the caller. To make these * interfaces simple, I did not want the caller to have to pass in a * memory buffer, but having libical pass out newly allocated memory * makes it difficult to de-allocate the memory. * * The ring buffer in this scheme makes it possible for libical to pass * out references to memory which the caller does not own, and be able * to de-allocate the memory later. The ring allows libical to have * several buffers active simultaneously, which is handy when creating * string representations of components. */ #define ICALMEMORY_C #ifdef HAVE_CONFIG_H #include "config.h" #endif #ifdef DMALLOC #include "dmalloc.h" #endif #include "icalmemory.h" #include "icalerror.h" #include /* for printf (debugging) */ #include /* for malloc, realloc */ #include /* for memset(), strdup */ #define BUFFER_RING_SIZE 25 #define MIN_BUFFER_SIZE 200 void icalmemory_free_tmp_buffer (void* buf); /* HACK. Not threadsafe */ void* buffer_ring[BUFFER_RING_SIZE]; int buffer_pos = -1; int initialized = 0; /* Add an existing buffer to the buffer ring */ void icalmemory_add_tmp_buffer(void* buf) { /* I don't think I need this -- I think static arrays are initialized to 0 as a standard part of C, but I am not sure. */ if (initialized == 0){ int i; for(i=0; i= (size_t) *buf_size) { *buf_size = (*buf_size) * 2 + final_length; new_buf = realloc(*buf,*buf_size); new_pos = (void*)((size_t)new_buf + data_length); *pos = new_pos; *buf = new_buf; } strcpy(*pos, string); *pos += string_length; } void icalmemory_append_char(char** buf, char** pos, size_t* buf_size, char ch) { char *new_buf; char *new_pos; size_t data_length, final_length; #ifndef ICAL_NO_INTERNAL_DEBUG icalerror_check_arg_rv( (buf!=0),"buf"); icalerror_check_arg_rv( (*buf!=0),"*buf"); icalerror_check_arg_rv( (pos!=0),"pos"); icalerror_check_arg_rv( (*pos!=0),"*pos"); icalerror_check_arg_rv( (buf_size!=0),"buf_size"); icalerror_check_arg_rv( (*buf_size!=0),"*buf_size"); #endif data_length = (size_t)*pos - (size_t)*buf; final_length = data_length + 2; if ( final_length > (size_t) *buf_size ) { *buf_size = (*buf_size) * 2 + final_length +1; new_buf = realloc(*buf,*buf_size); new_pos = (void*)((size_t)new_buf + data_length); *pos = new_pos; *buf = new_buf; } **pos = ch; *pos += 1; **pos = 0; }