0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034 #include <string.h>
0035 #include <stdio.h>
0036 #include <stdlib.h>
0037 #include <memory.h>
0038 #include <errno.h>
0039 #include <utlist.h>
0040
0041 #include <tperror.h>
0042 #include <ubf.h>
0043 #include <atmi.h>
0044 #include <typed_buf.h>
0045 #include <ndebug.h>
0046 #include <fdatatype.h>
0047 #include <userlog.h>
0048 #include <typed_carray.h>
0049
0050
0051 #define CARRAY_DEFAULT_SIZE 512
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065 expublic int CARRAY_prepare_outgoing (typed_buffer_descr_t *descr, char *idata, long ilen,
0066 char *obuf, long *olen, long flags)
0067 {
0068 int ret=EXSUCCEED;
0069 char fn[]="CARRAY_prepare_outgoing";
0070
0071
0072 if (NULL!=olen && *olen < ilen)
0073 {
0074 ndrx_TPset_error_fmt(TPEINVAL, "%s: Internal buffer space: %d, "
0075 "but requested: %d", fn, *olen, ilen);
0076 ret=EXFAIL;
0077 goto out;
0078 }
0079
0080 memcpy(obuf, idata, ilen);
0081
0082
0083 if (NULL!=olen)
0084 *olen = ilen;
0085
0086 out:
0087 return ret;
0088 }
0089
0090
0091
0092
0093
0094
0095
0096
0097 expublic int CARRAY_prepare_incoming (typed_buffer_descr_t *descr, char *rcv_data,
0098 long rcv_len, char **odata, long *olen, long flags)
0099 {
0100 int ret=EXSUCCEED;
0101 int rcv_buf_size;
0102 int existing_size;
0103
0104 char fn[]="CARRAY_prepare_incoming";
0105 buffer_obj_t *outbufobj=NULL;
0106
0107 NDRX_LOG(log_debug, "Entering %s", fn);
0108
0109 rcv_buf_size = rcv_len;
0110
0111
0112
0113 if (NULL==(outbufobj=ndrx_find_buffer(*odata)))
0114 {
0115 ndrx_TPset_error_fmt(TPEINVAL, "Output buffer %p is not allocated "
0116 "with tpalloc()!", *odata);
0117 ret=EXFAIL;
0118 goto out;
0119 }
0120
0121
0122 if (NULL!=outbufobj)
0123 {
0124
0125 if (flags & TPNOCHANGE && outbufobj->type_id!=BUF_TYPE_CARRAY)
0126 {
0127
0128 ndrx_TPset_error_fmt(TPEOTYPE, "Receiver expects %s but got %s buffer",
0129 G_buf_descr[BUF_TYPE_NULL].type,
0130 G_buf_descr[outbufobj->type_id].type
0131 );
0132 ret=EXFAIL;
0133 goto out;
0134 }
0135
0136
0137
0138
0139 if (outbufobj->type_id!=BUF_TYPE_CARRAY)
0140 {
0141 NDRX_LOG(log_warn, "User buffer %s is different, "
0142 "free it up and re-allocate as CARRAY", G_buf_descr[outbufobj->type_id].type);
0143 ndrx_tpfree(*odata, outbufobj);
0144 *odata=NULL;
0145 }
0146 }
0147
0148
0149 if (NULL!=*odata)
0150 {
0151 NDRX_LOG(log_debug, "%s: Output buffer exists", fn);
0152
0153 existing_size = outbufobj->size;
0154
0155
0156 NDRX_LOG(log_debug, "%s: Output buffer size: %d, received %d", fn,
0157 existing_size, rcv_buf_size);
0158
0159 if (existing_size>=rcv_buf_size)
0160 {
0161
0162 NDRX_LOG(log_debug, "%s: Using existing buffer", fn);
0163 }
0164 else
0165 {
0166
0167 char *new_addr;
0168 NDRX_LOG(log_debug, "%s: Reallocating", fn);
0169
0170 if (NULL==(new_addr=ndrx_tprealloc(*odata, rcv_buf_size)))
0171 {
0172 NDRX_LOG(log_error, "%s: _tprealloc failed!", fn);
0173 ret=EXFAIL;
0174 goto out;
0175 }
0176
0177
0178 *odata = new_addr;
0179 }
0180 }
0181 else
0182 {
0183
0184 NDRX_LOG(log_debug, "%s: Incoming buffer where missing - "
0185 "allocating new!", fn);
0186
0187 *odata = ndrx_tpalloc(&G_buf_descr[BUF_TYPE_CARRAY], NULL, NULL, rcv_len);
0188
0189 if (NULL==*odata)
0190 {
0191
0192 NDRX_LOG(log_error, "Failed to allocat new buffer!");
0193 goto out;
0194 }
0195 }
0196
0197
0198 memcpy(*odata, rcv_data, rcv_len);
0199
0200 if (NULL!=olen)
0201 {
0202 *olen = rcv_len;
0203 }
0204
0205 out:
0206 return ret;
0207 }
0208
0209
0210
0211
0212
0213
0214
0215
0216 expublic char * CARRAY_tpalloc (typed_buffer_descr_t *descr, char *subtype, long *len)
0217 {
0218 char *ret;
0219
0220 if (CARRAY_DEFAULT_SIZE > *len )
0221 {
0222 *len = CARRAY_DEFAULT_SIZE;
0223 }
0224
0225
0226 ret=(char *)NDRX_MALLOC(*len);
0227
0228 if (NULL!=ret)
0229 {
0230 ret[0] = EXEOS;
0231 }
0232 else
0233 {
0234 ndrx_TPset_error_fmt(TPEOS, "%s: Failed to allocate CARRAY buffer (len=%ld): %s",
0235 __func__, len, strerror(errno));
0236 }
0237
0238 out:
0239 return ret;
0240 }
0241
0242
0243
0244
0245
0246
0247
0248 expublic char * CARRAY_tprealloc(typed_buffer_descr_t *descr, char *cur_ptr, long len)
0249 {
0250 char *ret=NULL;
0251
0252 if (0==len)
0253 {
0254 len = CARRAY_DEFAULT_SIZE;
0255 }
0256
0257
0258 ret=(char *)NDRX_REALLOC(cur_ptr, len);
0259
0260 if (NULL==ret)
0261 {
0262 ndrx_TPset_error_fmt(TPEOS, "%s: Failed to reallocate CARRAY buffer (len=%ld): %s",
0263 __func__, len, strerror(errno));
0264 }
0265
0266 return ret;
0267 }
0268
0269
0270
0271
0272
0273
0274 expublic void CARRAY_tpfree(typed_buffer_descr_t *descr, char *buf)
0275 {
0276 NDRX_FREE(buf);
0277 }
0278
0279
0280
0281
0282
0283
0284
0285
0286
0287 expublic int CARRAY_test(typed_buffer_descr_t *descr, char *buf, BFLDLEN len, char *expr)
0288 {
0289 int ret=EXFALSE;
0290
0291 NDRX_LOG(log_error, "Carray buffers do not support event filters! Expr: [%s]", expr);
0292 userlog("Carray buffers do not support event filters! Expr: [%s]", expr);
0293
0294 return ret;
0295 }
0296
0297
0298