author | zautrix <zautrix> | 2005-07-04 19:16:55 (UTC) |
---|---|---|
committer | zautrix <zautrix> | 2005-07-04 19:16:55 (UTC) |
commit | 4f3238355f67a256f338986ca13322ef23960895 (patch) (unidiff) | |
tree | 023eabaf2c42d2c58753e5a3f9c5c680f132fbf8 /libical | |
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 @@ | |||
40 | * interfaces simple, I did not want the caller to have to pass in a | 40 | * interfaces simple, I did not want the caller to have to pass in a |
41 | * memory buffer, but having libical pass out newly allocated memory | 41 | * memory buffer, but having libical pass out newly allocated memory |
42 | * makes it difficult to de-allocate the memory. | 42 | * makes it difficult to de-allocate the memory. |
43 | * | 43 | * |
44 | * The ring buffer in this scheme makes it possible for libical to pass | 44 | * The ring buffer in this scheme makes it possible for libical to pass |
45 | * out references to memory which the caller does not own, and be able | 45 | * out references to memory which the caller does not own, and be able |
46 | * to de-allocate the memory later. The ring allows libical to have | 46 | * to de-allocate the memory later. The ring allows libical to have |
47 | * several buffers active simultaneously, which is handy when creating | 47 | * several buffers active simultaneously, which is handy when creating |
48 | * string representations of components. | 48 | * string representations of components. |
49 | */ | 49 | */ |
50 | 50 | ||
51 | #define ICALMEMORY_C | 51 | #define ICALMEMORY_C |
52 | 52 | ||
53 | #ifdef HAVE_CONFIG_H | 53 | #ifdef HAVE_CONFIG_H |
54 | #include "config.h" | 54 | #include "config.h" |
55 | #endif | 55 | #endif |
56 | 56 | ||
57 | #ifdef DMALLOC | 57 | #ifdef DMALLOC |
58 | #include "dmalloc.h" | 58 | #include "dmalloc.h" |
59 | #endif | 59 | #endif |
60 | 60 | ||
61 | #include "icalmemory.h" | 61 | #include "icalmemory.h" |
62 | #include "icalerror.h" | 62 | #include "icalerror.h" |
63 | 63 | ||
64 | #include <stdio.h> /* for printf (debugging) */ | 64 | #include <stdio.h> /* for printf (debugging) */ |
65 | #include <stdlib.h> /* for malloc, realloc */ | 65 | #include <stdlib.h> /* for malloc, realloc */ |
66 | #include <string.h> /* for memset(), strdup */ | 66 | #include <string.h> /* for memset(), strdup */ |
67 | 67 | ||
68 | #ifdef WIN32 | 68 | #ifdef WIN32 |
69 | #include <windows.h> | 69 | #include <windows.h> |
70 | #endif | 70 | #endif |
71 | 71 | ||
72 | #define BUFFER_RING_SIZE 2500 | 72 | #define BUFFER_RING_SIZE 50 |
73 | #define MIN_BUFFER_SIZE 200 | 73 | #define MIN_BUFFER_SIZE 64 |
74 | 74 | ||
75 | 75 | ||
76 | /* HACK. Not threadsafe */ | 76 | /* HACK. Not threadsafe */ |
77 | 77 | ||
78 | typedef struct { | 78 | typedef struct { |
79 | int pos; | 79 | int pos; |
80 | void *ring[BUFFER_RING_SIZE]; | 80 | void *ring[BUFFER_RING_SIZE]; |
81 | } buffer_ring; | 81 | } buffer_ring; |
82 | 82 | ||
83 | void icalmemory_free_tmp_buffer (void* buf); | 83 | void icalmemory_free_tmp_buffer (void* buf); |
84 | void icalmemory_free_ring_byval(buffer_ring *br); | 84 | void icalmemory_free_ring_byval(buffer_ring *br); |
85 | 85 | ||
86 | static buffer_ring* global_buffer_ring = 0; | 86 | static buffer_ring* global_buffer_ring = 0; |
87 | 87 | ||
88 | #ifdef HAVE_PTHREAD | 88 | #ifdef HAVE_PTHREAD |
89 | #include <pthread.h> | 89 | #include <pthread.h> |
90 | 90 | ||
91 | static pthread_key_t ring_key; | 91 | static pthread_key_t ring_key; |
92 | static pthread_once_t ring_key_once = PTHREAD_ONCE_INIT; | 92 | static pthread_once_t ring_key_once = PTHREAD_ONCE_INIT; |
93 | 93 | ||
94 | static void ring_destroy(void * buf) { | 94 | static void ring_destroy(void * buf) { |
95 | if (buf) icalmemory_free_ring_byval((buffer_ring *) buf); | 95 | if (buf) icalmemory_free_ring_byval((buffer_ring *) buf); |
96 | pthread_setspecific(ring_key, NULL); | 96 | pthread_setspecific(ring_key, NULL); |
97 | } | 97 | } |
98 | 98 | ||
99 | static void ring_key_alloc(void) { | 99 | static void ring_key_alloc(void) { |
100 | pthread_key_create(&ring_key, ring_destroy); | 100 | pthread_key_create(&ring_key, ring_destroy); |
101 | } | 101 | } |
102 | #endif | 102 | #endif |
103 | 103 | ||
104 | 104 | ||
105 | static buffer_ring * buffer_ring_new(void) { | 105 | static buffer_ring * buffer_ring_new(void) { |
@@ -184,66 +184,67 @@ icalmemory_tmp_buffer (size_t size) | |||
184 | size = MIN_BUFFER_SIZE; | 184 | size = MIN_BUFFER_SIZE; |
185 | } | 185 | } |
186 | 186 | ||
187 | buf = (void*)malloc(size); | 187 | buf = (void*)malloc(size); |
188 | 188 | ||
189 | if( buf == 0){ | 189 | if( buf == 0){ |
190 | icalerror_set_errno(ICAL_NEWFAILED_ERROR); | 190 | icalerror_set_errno(ICAL_NEWFAILED_ERROR); |
191 | return 0; | 191 | return 0; |
192 | } | 192 | } |
193 | 193 | ||
194 | memset(buf,0,size); | 194 | memset(buf,0,size); |
195 | 195 | ||
196 | icalmemory_add_tmp_buffer(buf); | 196 | icalmemory_add_tmp_buffer(buf); |
197 | 197 | ||
198 | return buf; | 198 | return buf; |
199 | } | 199 | } |
200 | 200 | ||
201 | /** get rid of this buffer ring */ | 201 | /** get rid of this buffer ring */ |
202 | void icalmemory_free_ring_byval(buffer_ring *br) { | 202 | void icalmemory_free_ring_byval(buffer_ring *br) { |
203 | int i; | 203 | int i; |
204 | for(i=0; i<BUFFER_RING_SIZE; i++){ | 204 | for(i=0; i<BUFFER_RING_SIZE; i++){ |
205 | if ( br->ring[i] != 0){ | 205 | if ( br->ring[i] != 0){ |
206 | free( br->ring[i]); | 206 | free( br->ring[i]); |
207 | } | 207 | } |
208 | } | 208 | } |
209 | free(br); | 209 | free(br); |
210 | } | 210 | } |
211 | 211 | ||
212 | void icalmemory_free_ring() | 212 | void icalmemory_free_ring() |
213 | { | 213 | { |
214 | buffer_ring *br; | 214 | buffer_ring *br; |
215 | br = get_buffer_ring(); | 215 | br = get_buffer_ring(); |
216 | |||
217 | icalmemory_free_ring_byval(br); | 216 | icalmemory_free_ring_byval(br); |
217 | if ( global_buffer_ring == br ) | ||
218 | global_buffer_ring = 0; | ||
218 | } | 219 | } |
219 | 220 | ||
220 | 221 | ||
221 | 222 | ||
222 | /** Like strdup, but the buffer is on the ring. */ | 223 | /** Like strdup, but the buffer is on the ring. */ |
223 | char* | 224 | char* |
224 | icalmemory_tmp_copy(const char* str) | 225 | icalmemory_tmp_copy(const char* str) |
225 | { | 226 | { |
226 | char* b = icalmemory_tmp_buffer(strlen(str)+1); | 227 | char* b = icalmemory_tmp_buffer(strlen(str)+1); |
227 | 228 | ||
228 | strcpy(b,str); | 229 | strcpy(b,str); |
229 | 230 | ||
230 | return b; | 231 | return b; |
231 | } | 232 | } |
232 | 233 | ||
233 | 234 | ||
234 | char* icalmemory_strdup(const char *s) | 235 | char* icalmemory_strdup(const char *s) |
235 | { | 236 | { |
236 | return strdup(s); | 237 | return strdup(s); |
237 | } | 238 | } |
238 | 239 | ||
239 | void | 240 | void |
240 | icalmemory_free_tmp_buffer (void* buf) | 241 | icalmemory_free_tmp_buffer (void* buf) |
241 | { | 242 | { |
242 | if(buf == 0) | 243 | if(buf == 0) |
243 | { | 244 | { |
244 | return; | 245 | return; |
245 | } | 246 | } |
246 | 247 | ||
247 | free(buf); | 248 | free(buf); |
248 | } | 249 | } |
249 | 250 | ||