summaryrefslogtreecommitdiffabout
authorzautrix <zautrix>2005-07-04 19:21:19 (UTC)
committer zautrix <zautrix>2005-07-04 19:21:19 (UTC)
commit16898aecbedb978169efef1e1e0233578a42a46e (patch) (side-by-side diff)
treea391b6c44fd2df62daf5f643393af03bf051f4f0
parent2710cddd5b0b69efc1c5a7f8516d5b451ff258f1 (diff)
downloadkdepimpi-16898aecbedb978169efef1e1e0233578a42a46e.zip
kdepimpi-16898aecbedb978169efef1e1e0233578a42a46e.tar.gz
kdepimpi-16898aecbedb978169efef1e1e0233578a42a46e.tar.bz2
free ring buffer
Diffstat (more/less context) (show whitespace changes)
-rw-r--r--libical/src/libical/icalmemory.c2
1 files changed, 1 insertions, 1 deletions
diff --git a/libical/src/libical/icalmemory.c b/libical/src/libical/icalmemory.c
index 3ed38ad..d22561e 100644
--- a/libical/src/libical/icalmemory.c
+++ b/libical/src/libical/icalmemory.c
@@ -1,168 +1,168 @@
/* -*- 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
======================================================================*/
/**
* @file icalmemory.c
* @brief Common memory management routines.
*
* 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 <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 100
+#define BUFFER_RING_SIZE 25
#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) {
buffer_ring *br;
int i;
br = (buffer_ring *)malloc(sizeof(buffer_ring));
for(i=0; i<BUFFER_RING_SIZE; i++){
br->ring[i] = 0;
}
br->pos = 0;
return(br);
}
#ifdef HAVE_PTHREAD
static buffer_ring* get_buffer_ring_pthread(void) {
buffer_ring *br;
pthread_once(&ring_key_once, ring_key_alloc);
br = pthread_getspecific(ring_key);
if (!br) {
br = buffer_ring_new();
pthread_setspecific(ring_key, br);
}
return(br);
}
#endif
/* get buffer ring via a single global for a non-threaded program */
static buffer_ring* get_buffer_ring_global(void) {
if (global_buffer_ring == 0) {
global_buffer_ring = buffer_ring_new();
}
return(global_buffer_ring);
}
static buffer_ring *get_buffer_ring(void) {
#ifdef HAVE_PTHREAD
return(get_buffer_ring_pthread());
#else
return get_buffer_ring_global();
#endif
}
/** Add an existing buffer to the buffer ring */
void icalmemory_add_tmp_buffer(void* buf)
{
buffer_ring *br = get_buffer_ring();
/* Wrap around the ring */
if(++(br->pos) == BUFFER_RING_SIZE){
br->pos = 0;
}
/* Free buffers as their slots are overwritten */
if ( br->ring[br->pos] != 0){
free( br->ring[br->pos]);
}
/* Assign the buffer to a slot */