aboutsummaryrefslogtreecommitdiffstats
path: root/libical/src/libical/icalmemory.c
blob: 7d99eb62b3fb920a715fc841a86df344953b7eb9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/* -*- 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.
 
 The Original Code is icalmemory.h
 The Initial Developer of the Original Code is Eric Busboom

 (C) COPYRIGHT 1999 The Software Studio. 
 http://www.softwarestudio.org

 ======================================================================*/

/* 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

#include "icalmemory.h"
#include "icalerror.h"

#include <stdio.h> /* for printf (debugging) */
#include <stdlib.h> /* for malloc, realloc */

#define BUFFER_RING_SIZE 25
#define MIN_BUFFER_SIZE 200

void* buffer_ring[BUFFER_RING_SIZE+1];
int buffer_pos = 0;
int initialized = 0;

/* Create a new temporary buffer on the ring. Libical owns these and wil deallocate them. */
void*
icalmemory_tmp_buffer (size_t size)
{
    void *rtrn; 
    /* 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<BUFFER_RING_SIZE; i++){
        buffer_ring[i]  = 0;
    }
    initialized = 1;
    }
    
    /* Ideally, this routine would re-use an existing buffer if it is
       larger than the requested buffer. Maybe later.... */

    if (size < MIN_BUFFER_SIZE){
    size = MIN_BUFFER_SIZE;
    }
    
    if ( buffer_ring[buffer_pos] != 0){
      /*sprintf(buffer_ring[buffer_pos], "***DEALLOCATED MEMORY***: %d",buffer_pos);*/
    free( buffer_ring[buffer_pos]);
    buffer_ring[buffer_pos] = 0;
    }


    rtrn = buffer_ring[buffer_pos] = (void*)malloc(size);

    memset(rtrn,0,size); 

    if(++buffer_pos > BUFFER_RING_SIZE){
    buffer_pos = 0;
    }

    return rtrn;
}

void icalmemory_free_ring()
{
    
   int i;
   for(i=0; i<BUFFER_RING_SIZE; i++){
    if ( buffer_ring[i] != 0){
       free( buffer_ring[i]);
    }
    buffer_ring[i]  = 0;
   }

   initialized = 1;

}

/* Like strdup, but the buffer is on the ring. */
char*
icalmemory_tmp_copy(char* str)
{
    char* b = icalmemory_tmp_buffer(strlen(str)+1);

    strcpy(b,str);

    return b;
}
    


void
icalmemory_free_tmp_buffer (void* buf)
{
   if(buf == 0)
   {
       return;
   }

   free(buf);
}


/* These buffer routines create memory the old fashioned way -- so the caller will have to delocate the new memory */

void* icalmemory_new_buffer(size_t size)
{
    /* HACK. need to handle out of memory case */
    void *b = malloc(size);

    memset(b,0,size); 

    return b;
}

void* icalmemory_resize_buffer(void* buf, size_t size)
{
    /* HACK. need to handle out of memory case */

    return realloc(buf, size);
}

void icalmemory_free_buffer(void* buf)
{
    free(buf);
}

void 
icalmemory_append_string(char** buf, char** pos, size_t* buf_size, 
                  char* string)
{
    char *new_buf;
    char *new_pos;

    size_t data_length, final_length, string_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");
    icalerror_check_arg_rv( (string!=0),"string");
#endif 

    string_length = strlen(string);
    data_length = (size_t)*pos - (size_t)*buf;    
    final_length = data_length + string_length; 

    if ( final_length > (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;
    
}