Back to index

lightning-sunbird  0.9+nobinonly
Classes | Defines | Functions | Variables
icalmemory.c File Reference

Common memory management routines. More...

#include "icalmemory.h"
#include "icalerror.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

Go to the source code of this file.

Classes

struct  buffer_ring

Defines

#define ICALMEMORY_C
#define BUFFER_RING_SIZE   2500
#define MIN_BUFFER_SIZE   200

Functions

void icalmemory_free_tmp_buffer (void *buf)
void icalmemory_free_ring_byval (buffer_ring *br)
 get rid of this buffer ring
static buffer_ringbuffer_ring_new (void)
static buffer_ringget_buffer_ring_global (void)
static buffer_ringget_buffer_ring (void)
void icalmemory_add_tmp_buffer (void *buf)
 Add an existing buffer to the buffer ring.
voidicalmemory_tmp_buffer (size_t size)
 Create a new temporary buffer on the ring.
void icalmemory_free_ring ()
 Free all memory used in the ring.
char * icalmemory_tmp_copy (const char *str)
 Like strdup, but the buffer is on the ring.
char * icalmemory_strdup (const char *s)
 A wrapper around strdup.
voidicalmemory_new_buffer (size_t size)
voidicalmemory_resize_buffer (void *buf, size_t size)
void icalmemory_free_buffer (void *buf)
void icalmemory_append_string (char **buf, char **pos, size_t *buf_size, const char *string)
 icalmemory_append_string will copy the string 'string' to the buffer 'buf' starting at position 'pos', reallocing 'buf' if it is too small.
void icalmemory_append_char (char **buf, char **pos, size_t *buf_size, char ch)
 icalmemory_append_char is similar, but is appends a character instead of a string

Variables

static buffer_ringglobal_buffer_ring = 0

Detailed Description

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.

Definition in file icalmemory.c.


Class Documentation

struct buffer_ring

Definition at line 78 of file icalmemory.c.

Collaboration diagram for buffer_ring:
Class Members
int pos
void * ring

Define Documentation

Definition at line 72 of file icalmemory.c.

Definition at line 51 of file icalmemory.c.

Definition at line 73 of file icalmemory.c.


Function Documentation

static buffer_ring* buffer_ring_new ( void  ) [static]

Definition at line 105 of file icalmemory.c.

                                           {
       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);
}

Here is the caller graph for this function:

static buffer_ring* get_buffer_ring ( void  ) [static]

Definition at line 143 of file icalmemory.c.

                                          {
#ifdef HAVE_PTHREAD
       return(get_buffer_ring_pthread());
#else
       return get_buffer_ring_global();
#endif
}

Here is the call graph for this function:

Here is the caller graph for this function:

static buffer_ring* get_buffer_ring_global ( void  ) [static]

Definition at line 136 of file icalmemory.c.

Here is the call graph for this function:

Here is the caller graph for this function:

Add an existing buffer to the buffer ring.

Add an externally allocated buffer to the ring.

Definition at line 153 of file icalmemory.c.

{
    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 */
    br->ring[br->pos] = buf; 
}

Here is the call graph for this function:

Here is the caller graph for this function:

void icalmemory_append_char ( char **  buf,
char **  pos,
size_t *  buf_size,
char  ch 
)

icalmemory_append_char is similar, but is appends a character instead of a string

Definition at line 330 of file icalmemory.c.

{
    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;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void icalmemory_append_string ( char **  buf,
char **  pos,
size_t *  buf_size,
const char *  string 
)

icalmemory_append_string will copy the string 'string' to the buffer 'buf' starting at position 'pos', reallocing 'buf' if it is too small.

'buf_size' is the size of 'buf' and will be changed if 'buf' is reallocated. 'pos' will point to the last byte of the new string in 'buf', usually a '\0'

Definition at line 288 of file icalmemory.c.

{
    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;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 282 of file icalmemory.c.

{
    free(buf);
}

Here is the caller graph for this function:

Free all memory used in the ring.

Definition at line 212 of file icalmemory.c.

Here is the call graph for this function:

Here is the caller graph for this function:

get rid of this buffer ring

Definition at line 202 of file icalmemory.c.

                                                 {
   int i;
   for(i=0; i<BUFFER_RING_SIZE; i++){
    if ( br->ring[i] != 0){
       free( br->ring[i]);
    }
    }
   free(br);
}

Here is the caller graph for this function:

Definition at line 240 of file icalmemory.c.

{
   if(buf == 0)
   {
       return;
   }

   free(buf);
}
void* icalmemory_new_buffer ( size_t  size)

Definition at line 256 of file icalmemory.c.

{
    void *b = malloc(size);

    if( b == 0){
       icalerror_set_errno(ICAL_NEWFAILED_ERROR);
       return 0;
    }

    memset(b,0,size); 

    return b;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void* icalmemory_resize_buffer ( void buf,
size_t  size 
)

Definition at line 270 of file icalmemory.c.

{
    void *b = realloc(buf, size);

    if( b == 0){
       icalerror_set_errno(ICAL_NEWFAILED_ERROR);
       return 0;
    }

   return b;
}

Here is the call graph for this function:

char* icalmemory_strdup ( const char *  s)

A wrapper around strdup.

Partly to trap calls to strdup, partly because in -ansi, gcc on Red Hat claims that strdup is undeclared

Definition at line 234 of file icalmemory.c.

{
    return strdup(s);
}

Here is the caller graph for this function:

void* icalmemory_tmp_buffer ( size_t  size)

Create a new temporary buffer on the ring.

Libical owns these and will deallocate them.

Definition at line 179 of file icalmemory.c.

{
    char *buf;

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

Here is the call graph for this function:

Here is the caller graph for this function:

char* icalmemory_tmp_copy ( const char *  str)

Like strdup, but the buffer is on the ring.

Definition at line 224 of file icalmemory.c.

{
    char* b = icalmemory_tmp_buffer(strlen(str)+1);

    strcpy(b,str);

    return b;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

Definition at line 86 of file icalmemory.c.