~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/xid.cc

Cleanup around SAFEMALLOC

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 
 *
4
 
 *  Copyright (C) 2008 Sun Microsystems
5
 
 *
6
 
 *  This program is free software; you can redistribute it and/or modify
7
 
 *  it under the terms of the GNU General Public License as published by
8
 
 *  the Free Software Foundation; version 2 of the License.
9
 
 *
10
 
 *  This program is distributed in the hope that it will be useful,
11
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 *  GNU General Public License for more details.
14
 
 *
15
 
 *  You should have received a copy of the GNU General Public License
16
 
 *  along with this program; if not, write to the Free Software
17
 
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
 
 */
19
 
 
20
 
#include "config.h"
21
 
#include <string.h>
22
 
 
23
 
#include <drizzled/my_hash.h>
24
 
#include <drizzled/xid.h>
25
 
#include "drizzled/internal/my_pthread.h"
26
 
#include "drizzled/charset.h"
27
 
#include "drizzled/global_charset_info.h"
28
 
#include "drizzled/charset_info.h"
29
 
 
30
 
namespace drizzled
31
 
{
32
 
 
33
 
bool XID::eq(XID *xid)
34
 
{
35
 
  return eq(xid->gtrid_length, xid->bqual_length, xid->data);
36
 
}
37
 
 
38
 
bool XID::eq(long g, long b, const char *d)
39
 
{
40
 
  return g == gtrid_length && b == bqual_length && !memcmp(d, data, g+b);
41
 
}
42
 
 
43
 
void XID::set(XID *xid)
44
 
{
45
 
  memcpy(this, xid, xid->length());
46
 
}
47
 
 
48
 
void XID::set(long f, const char *g, long gl, const char *b, long bl)
49
 
{
50
 
  formatID= f;
51
 
  memcpy(data, g, gtrid_length= gl);
52
 
  memcpy(data+gl, b, bqual_length= bl);
53
 
}
54
 
 
55
 
void XID::set(uint64_t xid)
56
 
{
57
 
  my_xid tmp;
58
 
  formatID= 1;
59
 
  set(DRIZZLE_XID_PREFIX_LEN, 0, DRIZZLE_XID_PREFIX);
60
 
  memcpy(data+DRIZZLE_XID_PREFIX_LEN, &server_id, sizeof(server_id));
61
 
  tmp= xid;
62
 
  memcpy(data+DRIZZLE_XID_OFFSET, &tmp, sizeof(tmp));
63
 
  gtrid_length=DRIZZLE_XID_GTRID_LEN;
64
 
}
65
 
 
66
 
void XID::set(long g, long b, const char *d)
67
 
{
68
 
  formatID= 1;
69
 
  gtrid_length= g;
70
 
  bqual_length= b;
71
 
  memcpy(data, d, g+b);
72
 
}
73
 
 
74
 
bool XID::is_null()
75
 
{
76
 
  return formatID == -1;
77
 
}
78
 
 
79
 
void XID::null()
80
 
{
81
 
  formatID= -1;
82
 
}
83
 
 
84
 
my_xid XID::quick_get_my_xid()
85
 
{
86
 
  my_xid tmp;
87
 
  memcpy(&tmp, data+DRIZZLE_XID_OFFSET, sizeof(tmp));
88
 
  return tmp;
89
 
}
90
 
 
91
 
my_xid XID::get_my_xid()
92
 
{
93
 
  return gtrid_length == DRIZZLE_XID_GTRID_LEN && bqual_length == 0 &&
94
 
    !memcmp(data+DRIZZLE_XID_PREFIX_LEN, &server_id, sizeof(server_id)) &&
95
 
    !memcmp(data, DRIZZLE_XID_PREFIX, DRIZZLE_XID_PREFIX_LEN) ?
96
 
    quick_get_my_xid() : 0;
97
 
}
98
 
 
99
 
uint32_t XID::length()
100
 
{
101
 
  return sizeof(formatID)+sizeof(gtrid_length)+sizeof(bqual_length)+
102
 
    gtrid_length+bqual_length;
103
 
}
104
 
 
105
 
unsigned char *XID::key()
106
 
{
107
 
  return (unsigned char *)&gtrid_length;
108
 
}
109
 
 
110
 
uint32_t XID::key_length()
111
 
{
112
 
  return sizeof(gtrid_length)+sizeof(bqual_length)+gtrid_length+bqual_length;
113
 
}
114
 
 
115
 
/***************************************************************************
116
 
  Handling of XA id cacheing
117
 
***************************************************************************/
118
 
pthread_mutex_t LOCK_xid_cache;
119
 
HASH xid_cache;
120
 
 
121
 
unsigned char *xid_get_hash_key(const unsigned char *, size_t *, bool);
122
 
void xid_free_hash(void *);
123
 
 
124
 
unsigned char *xid_get_hash_key(const unsigned char *ptr, size_t *length,
125
 
                        bool )
126
 
{
127
 
  *length=((XID_STATE*)ptr)->xid.key_length();
128
 
  return ((XID_STATE*)ptr)->xid.key();
129
 
}
130
 
 
131
 
void xid_free_hash(void *ptr)
132
 
{
133
 
  XID_STATE *state= (XID_STATE *)ptr;
134
 
  if (state->in_session == false)
135
 
    delete state;
136
 
}
137
 
 
138
 
bool xid_cache_init()
139
 
{
140
 
  pthread_mutex_init(&LOCK_xid_cache, MY_MUTEX_INIT_FAST);
141
 
  return hash_init(&xid_cache, &my_charset_bin, 100, 0, 0,
142
 
                   xid_get_hash_key, xid_free_hash, 0) != 0;
143
 
}
144
 
 
145
 
void xid_cache_free()
146
 
{
147
 
  if (hash_inited(&xid_cache))
148
 
  {
149
 
    hash_free(&xid_cache);
150
 
    pthread_mutex_destroy(&LOCK_xid_cache);
151
 
  }
152
 
}
153
 
 
154
 
XID_STATE *xid_cache_search(XID *xid)
155
 
{
156
 
  pthread_mutex_lock(&LOCK_xid_cache);
157
 
  XID_STATE *res=(XID_STATE *)hash_search(&xid_cache, xid->key(), xid->key_length());
158
 
  pthread_mutex_unlock(&LOCK_xid_cache);
159
 
  return res;
160
 
}
161
 
 
162
 
bool xid_cache_insert(XID *xid, enum xa_states xa_state)
163
 
{
164
 
  XID_STATE *xs;
165
 
  bool res;
166
 
  pthread_mutex_lock(&LOCK_xid_cache);
167
 
  if (hash_search(&xid_cache, xid->key(), xid->key_length()))
168
 
    res= false;
169
 
  else if ((xs = new XID_STATE) == NULL)
170
 
    res= true;
171
 
  else
172
 
  {
173
 
    xs->xa_state=xa_state;
174
 
    xs->xid.set(xid);
175
 
    xs->in_session=0;
176
 
    res= my_hash_insert(&xid_cache, (unsigned char*)xs);
177
 
  }
178
 
  pthread_mutex_unlock(&LOCK_xid_cache);
179
 
  return res;
180
 
}
181
 
 
182
 
bool xid_cache_insert(XID_STATE *xid_state)
183
 
{
184
 
  pthread_mutex_lock(&LOCK_xid_cache);
185
 
  bool res=my_hash_insert(&xid_cache, (unsigned char*)xid_state);
186
 
  pthread_mutex_unlock(&LOCK_xid_cache);
187
 
  return res;
188
 
}
189
 
 
190
 
void xid_cache_delete(XID_STATE *xid_state)
191
 
{
192
 
  pthread_mutex_lock(&LOCK_xid_cache);
193
 
  hash_delete(&xid_cache, (unsigned char *)xid_state);
194
 
  pthread_mutex_unlock(&LOCK_xid_cache);
195
 
}
196
 
 
197
 
} /* namespace drizzled */