~ubuntu-branches/ubuntu/raring/ibutils/raring-proposed

« back to all changes in this revision

Viewing changes to ibmgtsim/src/swig_alternate_mangling.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Benoit Mortier
  • Date: 2010-01-11 22:22:00 UTC
  • Revision ID: james.westby@ubuntu.com-20100111222200-53kum2et5nh13rv3
Tags: upstream-1.2-OFED-1.4.2
ImportĀ upstreamĀ versionĀ 1.2-OFED-1.4.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2004 Mellanox Technologies LTD. All rights reserved.
 
3
 *
 
4
 * This software is available to you under a choice of one of two
 
5
 * licenses.  You may choose to be licensed under the terms of the GNU
 
6
 * General Public License (GPL) Version 2, available from the file
 
7
 * COPYING in the main directory of this source tree, or the
 
8
 * OpenIB.org BSD license below:
 
9
 *
 
10
 *     Redistribution and use in source and binary forms, with or
 
11
 *     without modification, are permitted provided that the following
 
12
 *     conditions are met:
 
13
 *
 
14
 *      - Redistributions of source code must retain the above
 
15
 *        copyright notice, this list of conditions and the following
 
16
 *        disclaimer.
 
17
 *
 
18
 *      - Redistributions in binary form must reproduce the above
 
19
 *        copyright notice, this list of conditions and the following
 
20
 *        disclaimer in the documentation and/or other materials
 
21
 *        provided with the distribution.
 
22
 *
 
23
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
24
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
25
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
26
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 
27
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 
28
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 
29
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 
30
 * SOFTWARE.
 
31
 *
 
32
 * $Id$
 
33
 */
 
34
 
 
35
 
 
36
// the type of map holding alternate object mangling function and reverse one
 
37
// note we hold it by the original mangled type
 
38
// the type of map holding alternate object mangling function and reverse one
 
39
// note we hold it by the original mangled type
 
40
struct less_char_array : public binary_function<const char *, const char *, bool> {
 
41
        bool operator() (const char * x, const char * y) const { return (strcmp(x,y) < 0); }
 
42
};
 
43
 
 
44
#define charp_getname_map map<const char *, int (*)(Tcl_Obj *, void *,char *type),  less_char_array >
 
45
#define charp_getobjp_map map<const char *, int (*)(Tcl_Obj *, void **), less_char_array >
 
46
 
 
47
// two maps - for in and out of C++ space
 
48
charp_getname_map SWIG_AlternateObjMangling;
 
49
charp_getobjp_map SWIG_AlternateNameToObj;
 
50
 
 
51
/*---------------------------------------------------------------------
 
52
 * void SWIG_SetPointerObj(Tcl_Obj *objPtr, void *ptr, char *type)
 
53
 *
 
54
 * Sets a Tcl object to a pointer value.
 
55
 *           ptr = void pointer value
 
56
 *           type = string representing type
 
57
 *
 
58
 *---------------------------------------------------------------------*/
 
59
SWIGSTATIC
 
60
void SWIG_SetPointerObj(Tcl_Obj *objPtr, void *_ptr, char *type) {
 
61
 
 
62
  // if we have an alternate mangling use it:
 
63
  charp_getname_map::const_iterator I = SWIG_AlternateObjMangling.find(type);
 
64
  if (I != SWIG_AlternateObjMangling.end()) {
 
65
         int (*getName)(Tcl_Obj *, void *, char *type) = (*I).second;
 
66
         if (getName(objPtr, _ptr, type)) {
 
67
                cerr << "-E- Fail to convert object to string\n";
 
68
         }
 
69
         return;
 
70
  }
 
71
 
 
72
  static char _hex[16] =
 
73
  {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
 
74
          'a', 'b', 'c', 'd', 'e', 'f'};
 
75
  unsigned long _p, _s;
 
76
  char _result[20], *_r;    /* Note : a 64-bit hex number = 16 digits */
 
77
  char _temp[20], *_c;
 
78
  _r = _result;
 
79
  _p = (unsigned long) _ptr;
 
80
  if (_p > 0) {
 
81
    while (_p > 0) {
 
82
      _s = _p & 0xf;
 
83
      *(_r++) = _hex[_s];
 
84
      _p = _p >> 4;
 
85
    }
 
86
    *_r = '_';
 
87
    _c = &_temp[0];
 
88
    while (_r >= _result)
 
89
      *(_c++) = *(_r--);
 
90
    *_c = 0;
 
91
    Tcl_SetStringObj(objPtr,_temp,-1);
 
92
  } else {
 
93
    Tcl_SetStringObj(objPtr,"NULL",-1);
 
94
  }
 
95
  if (_ptr)
 
96
    Tcl_AppendToObj(objPtr,type,-1);
 
97
}
 
98
 
 
99
/*---------------------------------------------------------------------
 
100
 * char *SWIG_GetPointerObj(Tcl_Interp *interp, Tcl_Obj *objPtr, void **ptr, char *type)
 
101
 *
 
102
 * Attempts to extract a pointer value from our pointer type.
 
103
 * Upon failure, returns a string corresponding to the actual datatype.
 
104
 * Upon success, returns NULL and sets the pointer value in ptr.
 
105
 *---------------------------------------------------------------------*/
 
106
 
 
107
SWIGSTATIC
 
108
char *SWIG_GetPointerObj(Tcl_Interp *interp, Tcl_Obj *objPtr, void **ptr, char *_t) {
 
109
  unsigned long _p;
 
110
  char temp_type[256];
 
111
  char *name;
 
112
  int  i, len;
 
113
  SwigPtrType *sp,*tp;
 
114
  SwigCacheType *cache;
 
115
  int  start, end;
 
116
  char *_c;
 
117
  _p = 0;
 
118
 
 
119
  // if alternate mangling is defined use it:
 
120
  charp_getobjp_map::const_iterator I = SWIG_AlternateNameToObj.find(_t);
 
121
  if (I != SWIG_AlternateNameToObj.end()) {
 
122
         int (* getObjByName)(Tcl_Obj *, void **) = (*I).second;
 
123
         if (getObjByName(objPtr, ptr)) {
 
124
                cerr << "-E- fail to get object by name\n";
 
125
                return Tcl_GetStringFromObj(objPtr, &i);
 
126
         }
 
127
         return 0;
 
128
  }
 
129
 
 
130
  /* Extract the pointer value as a string */
 
131
  _c = Tcl_GetStringFromObj(objPtr, &i);
 
132
 
 
133
  /* Pointer values must start with leading underscore */
 
134
  if (*_c == '_') {
 
135
         _c++;
 
136
         /* Extract hex value from pointer */
 
137
         while (*_c) {
 
138
                if ((*_c >= '0') && (*_c <= '9'))
 
139
                  _p = (_p << 4) + (*_c - '0');
 
140
                else if ((*_c >= 'a') && (*_c <= 'f'))
 
141
                  _p = (_p << 4) + ((*_c - 'a') + 10);
 
142
                else
 
143
                  break;
 
144
                _c++;
 
145
         }
 
146
 
 
147
         if (_t) {
 
148
                if (strcmp(_t,_c)) {
 
149
                  if (!SwigPtrSort) {
 
150
                         qsort((void *) SwigPtrTable, SwigPtrN, sizeof(SwigPtrType), swigsort);
 
151
                         for (i = 0; i < 256; i++) {
 
152
                                SwigStart[i] = SwigPtrN;
 
153
                         }
 
154
                         for (i = SwigPtrN-1; i >= 0; i--) {
 
155
                                SwigStart[(int) (SwigPtrTable[i].name[1])] = i;
 
156
                         }
 
157
                         for (i = 255; i >= 1; i--) {
 
158
                                if (SwigStart[i-1] > SwigStart[i])
 
159
                                  SwigStart[i-1] = SwigStart[i];
 
160
                         }
 
161
                         SwigPtrSort = 1;
 
162
                         for (i = 0; i < SWIG_CACHESIZE; i++)
 
163
                                SwigCache[i].stat = 0;
 
164
                  }
 
165
 
 
166
                  /* First check cache for matches.  Uses last cache value as starting point */
 
167
                  cache = &SwigCache[SwigLastCache];
 
168
                  for (i = 0; i < SWIG_CACHESIZE; i++) {
 
169
                         if (cache->stat) {
 
170
                                if (strcmp(_t,cache->name) == 0) {
 
171
                                  if (strcmp(_c,cache->mapped) == 0) {
 
172
                                         cache->stat++;
 
173
                                         *ptr = (void *) _p;
 
174
                                         if (cache->tp->cast) *ptr = (*(cache->tp->cast))(*ptr);
 
175
                                         return (char *) 0;
 
176
                                  }
 
177
                                }
 
178
                         }
 
179
                         SwigLastCache = (SwigLastCache+1) & SWIG_CACHEMASK;
 
180
                         if (!SwigLastCache) cache = SwigCache;
 
181
                         else cache++;
 
182
                  }
 
183
                  /* We have a type mismatch.  Will have to look through our type
 
184
                          mapping table to figure out whether or not we can accept this datatype */
 
185
 
 
186
                  start = SwigStart[(int) _t[1]];
 
187
                  end = SwigStart[(int) _t[1]+1];
 
188
                  sp = &SwigPtrTable[start];
 
189
                  while (start < end) {
 
190
                         if (swigcmp(_t,sp) == 0) break;
 
191
                         sp++;
 
192
                         start++;
 
193
                  }
 
194
                  if (start >= end) sp = 0;
 
195
                  /* Try to find a match for this */
 
196
                  if (sp) {
 
197
                         while (swigcmp(_t,sp) == 0) {
 
198
                                name = sp->name;
 
199
                                len = sp->len;
 
200
                                tp = sp->next;
 
201
                                /* Try to find entry for our given datatype */
 
202
                                while(tp) {
 
203
                                  if (tp->len >= 255) {
 
204
                                         return _c;
 
205
                                  }
 
206
                                  strcpy(temp_type,tp->name);
 
207
                                  strncat(temp_type,_t+len,255-tp->len);
 
208
                                  if (strcmp(_c,temp_type) == 0) {
 
209
 
 
210
                                         strcpy(SwigCache[SwigCacheIndex].mapped,_c);
 
211
                                         strcpy(SwigCache[SwigCacheIndex].name,_t);
 
212
                                         SwigCache[SwigCacheIndex].stat = 1;
 
213
                                         SwigCache[SwigCacheIndex].tp = tp;
 
214
                                         SwigCacheIndex = SwigCacheIndex & SWIG_CACHEMASK;
 
215
 
 
216
                                         /* Get pointer value */
 
217
                                         *ptr = (void *) _p;
 
218
                                         if (tp->cast) *ptr = (*(tp->cast))(*ptr);
 
219
                                         return (char *) 0;
 
220
                                  }
 
221
                                  tp = tp->next;
 
222
                                }
 
223
                                sp++;
 
224
                                /* Hmmm. Didn't find it this time */
 
225
                         }
 
226
                  }
 
227
                  /* Didn't find any sort of match for this data.
 
228
                          Get the pointer value and return the received type */
 
229
                  *ptr = (void *) _p;
 
230
                  return _c;
 
231
                } else {
 
232
                  /* Found a match on the first try.  Return pointer value */
 
233
                  *ptr = (void *) _p;
 
234
                  return (char *) 0;
 
235
                }
 
236
         } else {
 
237
                /* No type specified.  Good luck */
 
238
                *ptr = (void *) _p;
 
239
                return (char *) 0;
 
240
         }
 
241
  } else {
 
242
    if (strcmp (_c, "NULL") == 0) {
 
243
                *ptr = (void *) 0;
 
244
                return (char *) 0;
 
245
    }
 
246
    *ptr = (void *) 0;
 
247
    return _c;
 
248
  }
 
249
}