author | zautrix <zautrix> | 2005-07-04 19:16:55 (UTC) |
---|---|---|
committer | zautrix <zautrix> | 2005-07-04 19:16:55 (UTC) |
commit | 4f3238355f67a256f338986ca13322ef23960895 (patch) (side-by-side diff) | |
tree | 023eabaf2c42d2c58753e5a3f9c5c680f132fbf8 /libical/src | |
parent | b0ed0793fa5d27475d8954a683ea68ca9ac0017f (diff) | |
download | kdepimpi-4f3238355f67a256f338986ca13322ef23960895.zip kdepimpi-4f3238355f67a256f338986ca13322ef23960895.tar.gz kdepimpi-4f3238355f67a256f338986ca13322ef23960895.tar.bz2 |
free ring buffer
-rw-r--r-- | libical/src/libical/icalmemory.c | 7 |
1 files changed, 4 insertions, 3 deletions
diff --git a/libical/src/libical/icalmemory.c b/libical/src/libical/icalmemory.c index 058ef37..18d7ef9 100644 --- a/libical/src/libical/icalmemory.c +++ b/libical/src/libical/icalmemory.c @@ -40,66 +40,66 @@ * 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 <stdio.h> /* for printf (debugging) */ #include <stdlib.h> /* for malloc, realloc */ #include <string.h> /* for memset(), strdup */ #ifdef WIN32 #include <windows.h> #endif -#define BUFFER_RING_SIZE 2500 -#define MIN_BUFFER_SIZE 200 +#define BUFFER_RING_SIZE 50 +#define MIN_BUFFER_SIZE 64 /* HACK. Not threadsafe */ typedef struct { int pos; void *ring[BUFFER_RING_SIZE]; } buffer_ring; void icalmemory_free_tmp_buffer (void* buf); void icalmemory_free_ring_byval(buffer_ring *br); static buffer_ring* global_buffer_ring = 0; #ifdef HAVE_PTHREAD #include <pthread.h> static pthread_key_t ring_key; static pthread_once_t ring_key_once = PTHREAD_ONCE_INIT; static void ring_destroy(void * buf) { if (buf) icalmemory_free_ring_byval((buffer_ring *) buf); pthread_setspecific(ring_key, NULL); } static void ring_key_alloc(void) { pthread_key_create(&ring_key, ring_destroy); } #endif static buffer_ring * buffer_ring_new(void) { @@ -184,66 +184,67 @@ icalmemory_tmp_buffer (size_t size) size = MIN_BUFFER_SIZE; } buf = (void*)malloc(size); if( buf == 0){ icalerror_set_errno(ICAL_NEWFAILED_ERROR); return 0; } memset(buf,0,size); icalmemory_add_tmp_buffer(buf); return buf; } /** get rid of this buffer ring */ void icalmemory_free_ring_byval(buffer_ring *br) { int i; for(i=0; i<BUFFER_RING_SIZE; i++){ if ( br->ring[i] != 0){ free( br->ring[i]); } } free(br); } void icalmemory_free_ring() { buffer_ring *br; br = get_buffer_ring(); - icalmemory_free_ring_byval(br); + if ( global_buffer_ring == br ) + global_buffer_ring = 0; } /** Like strdup, but the buffer is on the ring. */ char* icalmemory_tmp_copy(const char* str) { char* b = icalmemory_tmp_buffer(strlen(str)+1); strcpy(b,str); return b; } char* icalmemory_strdup(const char *s) { return strdup(s); } void icalmemory_free_tmp_buffer (void* buf) { if(buf == 0) { return; } free(buf); } |