~brianaker/libmemcached/1164440

« back to all changes in this revision

Viewing changes to lib/memcached_string.c

  • Committer: patg@patg.net
  • Date: 2008-05-05 03:32:10 UTC
  • mfrom: (317.1.71)
  • Revision ID: patg@patg.net-20080505033210-yxv0us02t8lb44dy
Merge 477:75823cad36b7 with -r 476

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include "common.h"
2
 
 
3
 
memcached_return memcached_string_check(memcached_string_st *string, size_t need)
4
 
{
5
 
  if (need && need > (size_t)(string->current_size - (size_t)(string->end - string->string)))
6
 
  {
7
 
    size_t current_offset= string->end - string->string;
8
 
    char *new_value;
9
 
    size_t adjust;
10
 
    size_t new_size;
11
 
 
12
 
    /* This is the block multiplier. To keep it larger and surive division errors we must round it up */
13
 
    adjust= (need - (size_t)(string->current_size - (size_t)(string->end - string->string))) / string->block_size;
14
 
    adjust++;
15
 
 
16
 
    new_size= sizeof(char) * (size_t)((adjust * string->block_size) + string->current_size);
17
 
    /* Test for overflow */
18
 
    if (new_size < need)
19
 
      return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
20
 
 
21
 
    if (string->root->call_realloc)
22
 
      new_value= (char *)string->root->call_realloc(string->root, string->string, new_size);
23
 
    else
24
 
      new_value= (char *)realloc(string->string, new_size);
25
 
 
26
 
    if (new_value == NULL)
27
 
      return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
28
 
 
29
 
    string->string= new_value;
30
 
    string->end= string->string + current_offset;
31
 
 
32
 
    string->current_size+= (string->block_size * adjust);
33
 
  }
34
 
 
35
 
  return MEMCACHED_SUCCESS;
36
 
}
37
 
 
38
 
memcached_string_st *memcached_string_create(memcached_st *ptr, memcached_string_st *string, size_t initial_size)
39
 
{
40
 
  memcached_return rc;
41
 
 
42
 
  /* Saving malloc calls :) */
43
 
  if (string)
44
 
  {
45
 
    memset(string, 0, sizeof(memcached_string_st));
46
 
    string->is_allocated= MEMCACHED_NOT_ALLOCATED;
47
 
  }
48
 
  else
49
 
  {
50
 
    if (ptr->call_malloc)
51
 
      string= (memcached_string_st *)ptr->call_malloc(ptr, sizeof(memcached_string_st));
52
 
    else
53
 
      string= (memcached_string_st *)malloc(sizeof(memcached_string_st));
54
 
 
55
 
    if (string == NULL)
56
 
      return NULL;
57
 
    memset(string, 0, sizeof(memcached_string_st));
58
 
    string->is_allocated= MEMCACHED_ALLOCATED;
59
 
  }
60
 
  string->block_size= MEMCACHED_BLOCK_SIZE;
61
 
  string->root= ptr;
62
 
 
63
 
  rc=  memcached_string_check(string, initial_size);
64
 
  if (rc != MEMCACHED_SUCCESS)
65
 
  {
66
 
    if (ptr->call_free)
67
 
      ptr->call_free(ptr, string);
68
 
    else
69
 
      free(string);
70
 
 
71
 
    return NULL;
72
 
  }
73
 
 
74
 
  WATCHPOINT_ASSERT(string->string == string->end);
75
 
 
76
 
  return string;
77
 
}
78
 
 
79
 
memcached_return memcached_string_append_character(memcached_string_st *string, 
80
 
                                                   char character)
81
 
{
82
 
  memcached_return rc;
83
 
 
84
 
  WATCHPOINT_ASSERT(string->is_allocated != MEMCACHED_USED);
85
 
 
86
 
  rc=  memcached_string_check(string, 1);
87
 
 
88
 
  if (rc != MEMCACHED_SUCCESS)
89
 
    return rc;
90
 
 
91
 
  *string->end= ' ';
92
 
  string->end++;
93
 
 
94
 
  return MEMCACHED_SUCCESS;
95
 
}
96
 
 
97
 
memcached_return memcached_string_append(memcached_string_st *string,
98
 
                                         char *value, size_t length)
99
 
{
100
 
  memcached_return rc;
101
 
 
102
 
  WATCHPOINT_ASSERT(string->is_allocated != MEMCACHED_USED);
103
 
 
104
 
  rc= memcached_string_check(string, length);
105
 
 
106
 
  if (rc != MEMCACHED_SUCCESS)
107
 
    return rc;
108
 
 
109
 
  WATCHPOINT_ASSERT(length <= string->current_size);
110
 
  WATCHPOINT_ASSERT(string->string);
111
 
  WATCHPOINT_ASSERT(string->end >= string->string);
112
 
 
113
 
  memcpy(string->end, value, length);
114
 
  string->end+= length;
115
 
 
116
 
  return MEMCACHED_SUCCESS;
117
 
}
118
 
 
119
 
size_t memcached_string_backspace(memcached_string_st *string, size_t remove)
120
 
{
121
 
  WATCHPOINT_ASSERT(string->is_allocated != MEMCACHED_USED);
122
 
 
123
 
  if (string->end - string->string  > remove)
124
 
  {
125
 
    size_t difference;
126
 
 
127
 
    difference= string->end - string->string;
128
 
    string->end= string->string;
129
 
 
130
 
    return difference;
131
 
  }
132
 
  string->end-= remove;
133
 
 
134
 
  return remove;
135
 
}
136
 
 
137
 
char *memcached_string_c_copy(memcached_string_st *string)
138
 
{
139
 
  char *c_ptr;
140
 
 
141
 
  WATCHPOINT_ASSERT(string->is_allocated != MEMCACHED_USED);
142
 
 
143
 
  if (string->root->call_malloc)
144
 
    c_ptr= (char *)string->root->call_malloc(string->root, (memcached_string_length(string)+1) * sizeof(char));
145
 
  else
146
 
    c_ptr= (char *)malloc((memcached_string_length(string)+1) * sizeof(char));
147
 
 
148
 
  if (c_ptr == NULL)
149
 
    return NULL;
150
 
 
151
 
  memcpy(c_ptr, memcached_string_value(string), memcached_string_length(string));
152
 
  c_ptr[memcached_string_length(string)]= 0;
153
 
 
154
 
  return c_ptr;
155
 
}
156
 
 
157
 
memcached_return memcached_string_reset(memcached_string_st *string)
158
 
{
159
 
  WATCHPOINT_ASSERT(string->is_allocated != MEMCACHED_USED);
160
 
  string->end= string->string;
161
 
  
162
 
  return MEMCACHED_SUCCESS;
163
 
}
164
 
 
165
 
void memcached_string_free(memcached_string_st *ptr)
166
 
{
167
 
  if (ptr == NULL)
168
 
    return;
169
 
 
170
 
  if (ptr->string)
171
 
  {
172
 
    if (ptr->root->call_free)
173
 
      ptr->root->call_free(ptr->root, ptr->string);
174
 
    else
175
 
      free(ptr->string);
176
 
  }
177
 
 
178
 
  if (ptr->is_allocated == MEMCACHED_ALLOCATED)
179
 
  {
180
 
    if (ptr->root->call_free)
181
 
      ptr->root->call_free(ptr->root, ptr);
182
 
    else
183
 
      free(ptr);
184
 
  }
185
 
  else
186
 
    ptr->is_allocated= MEMCACHED_USED;
187
 
}