Mercury
mercury_proc.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2013 Argonne National Laboratory, Department of Energy,
3  *                    UChicago Argonne, LLC and The HDF Group.
4  * All rights reserved.
5  *
6  * The full copyright notice, including terms governing use, modification,
7  * and redistribution, is contained in the COPYING file that can be
8  * found at the root of the source code distribution tree.
9  */
10 
11 #ifndef MERCURY_PROC_H
12 #define MERCURY_PROC_H
13 
14 #include "mercury_types.h"
15 #include "mercury_error.h"
16 #include "mercury_bulk.h"
17 
18 #include <stdlib.h>
19 #include <string.h>
20 #ifdef HG_HAS_XDR
21 #include <rpc/types.h>
22 #include <rpc/xdr.h>
23 # ifdef __APPLE__
24 # define xdr_int8_t xdr_char
25 # define xdr_uint8_t xdr_u_char
26 # define xdr_uint16_t xdr_u_int16_t
27 # define xdr_uint32_t xdr_u_int32_t
28 # define xdr_uint64_t xdr_u_int64_t
29 # endif
30 #endif
31 
32 #ifndef HG_PROC_INLINE
33  #if defined(__GNUC__) && !defined(__GNUC_STDC_INLINE__)
34  #define HG_PROC_INLINE extern HG_INLINE
35  #else
36  #define HG_PROC_INLINE HG_INLINE
37  #endif
38 #endif
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 
52 HG_EXPORT void *
53 hg_proc_buf_alloc(size_t size);
54 
62 HG_EXPORT hg_return_t
63 hg_proc_buf_free(void *mem_ptr);
64 
79 HG_EXPORT hg_return_t
80 hg_proc_create(void *buf, size_t buf_size, hg_proc_op_t op, hg_proc_hash_t hash,
81  hg_proc_t *proc);
82 
90 HG_EXPORT hg_return_t
92 
100 HG_EXPORT hg_proc_op_t
102 
110 HG_EXPORT size_t
112 
122 HG_EXPORT hg_return_t
123 hg_proc_set_size(hg_proc_t proc, size_t buf_size);
124 
132 HG_EXPORT size_t
134 
142 HG_EXPORT void *
144 
145 #ifdef HG_HAS_XDR
146 
153 HG_EXPORT XDR *
154 hg_proc_get_xdr_ptr(hg_proc_t proc);
155 #endif
156 
165 HG_EXPORT hg_return_t
166 hg_proc_set_buf_ptr(hg_proc_t proc, void *buf_ptr);
167 
175 HG_EXPORT void *
177 
185 HG_EXPORT size_t
187 
196 HG_EXPORT hg_return_t
197 hg_proc_set_extra_buf_is_mine(hg_proc_t proc, hg_bool_t mine);
198 
207 HG_EXPORT hg_return_t
209 
220 HG_EXPORT hg_return_t
221 hg_proc_memcpy(hg_proc_t proc, void *data, size_t data_size);
222 
234 static HG_INLINE void *
235 hg_proc_buf_memcpy(void *buf, void *data, size_t data_size, hg_proc_op_t op)
236 {
237  const void *src = NULL;
238  void *dest = NULL;
239 
240  if ((op != HG_ENCODE) && (op != HG_DECODE)) return NULL;
241  src = (op == HG_ENCODE) ? (const void *) data : (const void *) buf;
242  dest = (op == HG_ENCODE) ? buf : data;
243  memcpy(dest, src, data_size);
244 
245  return ((char *) buf + data_size);
246 }
247 
252  hg_int8_t *data);
254  hg_uint8_t *data);
256  hg_int16_t *data);
258  hg_uint16_t *data);
260  hg_int32_t *data);
262  hg_uint32_t *data);
264  hg_int64_t *data);
266  hg_uint64_t *data);
267 HG_EXPORT HG_PROC_INLINE hg_return_t hg_proc_raw(hg_proc_t proc, void *buf,
268  size_t buf_size);
270  hg_bulk_t *handle);
271 
272 
273 /* Note: float types are not supported but can be built on top of the existing
274  * proc routines; encoding floats using XDR could modify checksum */
275 
279 #define hg_proc_int8_t hg_proc_hg_int8_t
280 #define hg_proc_uint8_t hg_proc_hg_uint8_t
281 #define hg_proc_int16_t hg_proc_hg_int16_t
282 #define hg_proc_uint16_t hg_proc_hg_uint16_t
283 #define hg_proc_int32_t hg_proc_hg_int32_t
284 #define hg_proc_uint32_t hg_proc_hg_uint32_t
285 #define hg_proc_int64_t hg_proc_hg_int64_t
286 #define hg_proc_uint64_t hg_proc_hg_uint64_t
287 
288 /* Map mercury common types */
289 #define hg_proc_hg_bool_t hg_proc_hg_uint8_t
290 #define hg_proc_hg_ptr_t hg_proc_hg_uint64_t
291 #define hg_proc_hg_id_t hg_proc_hg_uint32_t
292 
302 hg_proc_hg_int8_t(hg_proc_t proc, hg_int8_t *data)
303 {
304  hg_return_t ret = HG_FAIL;
305 #ifdef HG_HAS_XDR
306  ret = xdr_int8_t(hg_proc_get_xdr_ptr(proc), data) ? HG_SUCCESS : HG_FAIL;
307 #else
308  ret = hg_proc_memcpy(proc, data, sizeof(hg_int8_t));
309 #endif
310  return ret;
311 }
312 
322 hg_proc_hg_uint8_t(hg_proc_t proc, hg_uint8_t *data)
323 {
324  hg_return_t ret = HG_FAIL;
325 #ifdef HG_HAS_XDR
326  ret = xdr_uint8_t(hg_proc_get_xdr_ptr(proc), data) ? HG_SUCCESS : HG_FAIL;
327 #else
328  ret = hg_proc_memcpy(proc, data, sizeof(hg_uint8_t));
329 #endif
330  return ret;
331 }
332 
342 hg_proc_hg_int16_t(hg_proc_t proc, hg_int16_t *data)
343 {
344  hg_return_t ret = HG_FAIL;
345 #ifdef HG_HAS_XDR
346  ret = xdr_int16_t(hg_proc_get_xdr_ptr(proc), data) ? HG_SUCCESS : HG_FAIL;
347 #else
348  ret = hg_proc_memcpy(proc, data, sizeof(hg_int16_t));
349 #endif
350  return ret;
351 }
352 
362 hg_proc_hg_uint16_t(hg_proc_t proc, hg_uint16_t *data)
363 {
364  hg_return_t ret = HG_FAIL;
365 #ifdef HG_HAS_XDR
366  ret = xdr_uint16_t(hg_proc_get_xdr_ptr(proc), data) ? HG_SUCCESS : HG_FAIL;
367 #else
368  ret = hg_proc_memcpy(proc, data, sizeof(hg_uint16_t));
369 #endif
370  return ret;
371 }
372 
382 hg_proc_hg_int32_t(hg_proc_t proc, hg_int32_t *data)
383 {
384  hg_return_t ret = HG_FAIL;
385 #ifdef HG_HAS_XDR
386  ret = xdr_int32_t(hg_proc_get_xdr_ptr(proc), data) ? HG_SUCCESS : HG_FAIL;
387 #else
388  ret = hg_proc_memcpy(proc, data, sizeof(hg_int32_t));
389 #endif
390  return ret;
391 }
392 
402 hg_proc_hg_uint32_t(hg_proc_t proc, hg_uint32_t *data)
403 {
404  hg_return_t ret = HG_FAIL;
405 #ifdef HG_HAS_XDR
406  ret = xdr_uint32_t(hg_proc_get_xdr_ptr(proc), data) ? HG_SUCCESS : HG_FAIL;
407 #else
408  ret = hg_proc_memcpy(proc, data, sizeof(hg_uint32_t));
409 #endif
410  return ret;
411 }
412 
422 hg_proc_hg_int64_t(hg_proc_t proc, hg_int64_t *data)
423 {
424  hg_return_t ret = HG_FAIL;
425 #ifdef HG_HAS_XDR
426  ret = xdr_int64_t(hg_proc_get_xdr_ptr(proc), data) ? HG_SUCCESS : HG_FAIL;
427 #else
428  ret = hg_proc_memcpy(proc, data, sizeof(hg_int64_t));
429 #endif
430  return ret;
431 }
432 
442 hg_proc_hg_uint64_t(hg_proc_t proc, hg_uint64_t *data)
443 {
444  hg_return_t ret = HG_FAIL;
445 #ifdef HG_HAS_XDR
446  ret = xdr_uint64_t(hg_proc_get_xdr_ptr(proc), data) ? HG_SUCCESS : HG_FAIL;
447 #else
448  ret = hg_proc_memcpy(proc, data, sizeof(hg_uint64_t));
449 #endif
450  return ret;
451 }
452 
463 hg_proc_raw(hg_proc_t proc, void *buf, size_t buf_size)
464 {
465  hg_uint8_t *buf_ptr;
466  hg_uint8_t *buf_ptr_lim = (hg_uint8_t*) buf + buf_size;
467  hg_return_t ret = HG_FAIL;
468 
469  for (buf_ptr = (hg_uint8_t*) buf; buf_ptr < buf_ptr_lim; buf_ptr++) {
470  ret = hg_proc_uint8_t(proc, buf_ptr);
471  if (ret != HG_SUCCESS) {
472  HG_ERROR_DEFAULT("Proc error");
473  break;
474  }
475  }
476 
477  return ret;
478 }
479 
490 {
491  hg_return_t ret = HG_FAIL;
492  void *buf = NULL;
493  hg_uint64_t buf_size = 0;
494 
495  switch (hg_proc_get_op(proc)) {
496  case HG_ENCODE:
497  if (*handle != HG_BULK_NULL) {
498  buf_size = HG_Bulk_handle_get_serialize_size(*handle);
499  buf = malloc(buf_size);
500  ret = HG_Bulk_handle_serialize(buf, buf_size, *handle);
501  if (ret != HG_SUCCESS) {
502  HG_ERROR_DEFAULT("Could not serialize bulk handle");
503  ret = HG_FAIL;
504  return ret;
505  }
506  } else {
507  /* If HG_BULK_NULL set 0 to buf_size */
508  buf_size = 0;
509  }
510  /* Encode size */
511  ret = hg_proc_uint64_t(proc, &buf_size);
512  if (ret != HG_SUCCESS) {
513  HG_ERROR_DEFAULT("Proc error");
514  ret = HG_FAIL;
515  return ret;
516  }
517  if (buf_size) {
518  /* Encode serialized buffer */
519  ret = hg_proc_raw(proc, buf, buf_size);
520  if (ret != HG_SUCCESS) {
521  HG_ERROR_DEFAULT("Proc error");
522  ret = HG_FAIL;
523  return ret;
524  }
525  free(buf);
526  buf = NULL;
527  }
528  break;
529  case HG_DECODE:
530  /* Decode size */
531  ret = hg_proc_uint64_t(proc, &buf_size);
532  if (ret != HG_SUCCESS) {
533  HG_ERROR_DEFAULT("Proc error");
534  ret = HG_FAIL;
535  return ret;
536  }
537  if (buf_size) {
538  buf = malloc(buf_size);
539  /* Decode serialized buffer */
540  ret = hg_proc_raw(proc, buf, buf_size);
541  if (ret != HG_SUCCESS) {
542  HG_ERROR_DEFAULT("Proc error");
543  ret = HG_FAIL;
544  return ret;
545  }
546  ret = HG_Bulk_handle_deserialize(handle, buf, buf_size);
547  if (ret != HG_SUCCESS) {
548  HG_ERROR_DEFAULT("Could not deserialize bulk handle");
549  ret = HG_FAIL;
550  return ret;
551  }
552  free(buf);
553  buf = NULL;
554  } else {
555  /* If buf_size is 0, define handle to HG_BULK_NULL */
556  *handle = HG_BULK_NULL;
557  }
558  break;
559  case HG_FREE:
560  if (*handle != HG_BULK_NULL) {
561  ret = HG_Bulk_handle_free(*handle);
562  if (ret != HG_SUCCESS) {
563  HG_ERROR_DEFAULT("Could not free bulk handle");
564  ret = HG_FAIL;
565  return ret;
566  }
567  *handle = HG_BULK_NULL;
568  } else {
569  /* If *handle is HG_BULK_NULL, just return success */
570  ret = HG_SUCCESS;
571  }
572  break;
573  default:
574  break;
575  }
576  return ret;
577 }
578 
579 #ifdef __cplusplus
580 }
581 #endif
582 
583 #endif /* MERCURY_PROC_H */
HG_EXPORT HG_PROC_INLINE hg_return_t hg_proc_hg_uint32_t(hg_proc_t proc, hg_uint32_t *data)
Generic processing routine.
Definition: mercury_proc.h:402
HG_EXPORT HG_PROC_INLINE hg_return_t hg_proc_hg_uint64_t(hg_proc_t proc, hg_uint64_t *data)
Generic processing routine.
Definition: mercury_proc.h:442
HG_EXPORT hg_proc_op_t hg_proc_get_op(hg_proc_t proc)
Get the operation type associated to the processor.
HG_EXPORT hg_return_t HG_Bulk_handle_free(hg_bulk_t handle)
Free bulk handle.
HG_EXPORT HG_PROC_INLINE hg_return_t hg_proc_hg_int16_t(hg_proc_t proc, hg_int16_t *data)
Generic processing routine.
Definition: mercury_proc.h:342
HG_EXPORT HG_PROC_INLINE hg_return_t hg_proc_hg_bulk_t(hg_proc_t proc, hg_bulk_t *handle)
Generic processing routine.
Definition: mercury_proc.h:489
void * hg_bulk_t
Definition: mercury_types.h:23
HG_EXPORT hg_return_t hg_proc_free(hg_proc_t proc)
Free the processor.
HG_EXPORT hg_return_t hg_proc_set_size(hg_proc_t proc, size_t buf_size)
Request a new buffer size.
HG_EXPORT hg_return_t HG_Bulk_handle_deserialize(hg_bulk_t *handle, const void *buf, size_t buf_size)
Deserialize bulk handle from a buffer.
HG_EXPORT HG_PROC_INLINE hg_return_t hg_proc_raw(hg_proc_t proc, void *buf, size_t buf_size)
Generic processing routine.
Definition: mercury_proc.h:463
HG_EXPORT hg_return_t hg_proc_memcpy(hg_proc_t proc, void *data, size_t data_size)
Base proc routine using memcpy.
HG_EXPORT HG_PROC_INLINE hg_return_t hg_proc_hg_uint16_t(hg_proc_t proc, hg_uint16_t *data)
Generic processing routine.
Definition: mercury_proc.h:362
#define HG_BULK_NULL
Definition: mercury_types.h:42
HG_EXPORT size_t hg_proc_get_extra_size(hg_proc_t proc)
Get eventual size of the extra buffer used by processor.
HG_EXPORT hg_return_t hg_proc_buf_free(void *mem_ptr)
Free memory which has been previously allocated using hg_proc_buf_alloc.
#define HG_ERROR_DEFAULT
Definition: mercury_error.h:55
HG_EXPORT hg_return_t hg_proc_flush(hg_proc_t proc)
Flush the proc after data has been encoded or decoded and verify data using base checksum if availabl...
HG_EXPORT size_t HG_Bulk_handle_get_serialize_size(hg_bulk_t handle)
Get size required to serialize bulk handle.
#define HG_PROC_INLINE
Definition: mercury_proc.h:36
HG_EXPORT void * hg_proc_get_buf_ptr(hg_proc_t proc)
Get pointer to current buffer (for manual encoding).
HG_EXPORT HG_PROC_INLINE hg_return_t hg_proc_hg_int64_t(hg_proc_t proc, hg_int64_t *data)
Generic processing routine.
Definition: mercury_proc.h:422
#define hg_proc_uint64_t
Definition: mercury_proc.h:286
HG_EXPORT void * hg_proc_get_extra_buf(hg_proc_t proc)
Get eventual extra buffer used by processor.
HG_EXPORT size_t hg_proc_get_size_left(hg_proc_t proc)
Get size left for processing.
void * hg_proc_t
Definition: mercury_types.h:21
#define hg_proc_uint8_t
Definition: mercury_proc.h:280
HG_EXPORT hg_return_t hg_proc_set_extra_buf_is_mine(hg_proc_t proc, hg_bool_t mine)
Set extra buffer to mine (if other calls mine, buffer is no longer freed after hg_proc_free) ...
hg_proc_hash_t
Hash methods available for proc.
Definition: mercury_types.h:59
HG_EXPORT HG_PROC_INLINE hg_return_t hg_proc_hg_int32_t(hg_proc_t proc, hg_int32_t *data)
Generic processing routine.
Definition: mercury_proc.h:382
HG_EXPORT HG_PROC_INLINE hg_return_t hg_proc_hg_uint8_t(hg_proc_t proc, hg_uint8_t *data)
Generic processing routine.
Definition: mercury_proc.h:322
hg_proc_op_t
Proc operations.
Definition: mercury_types.h:50
enum hg_return hg_return_t
HG_EXPORT size_t hg_proc_get_size(hg_proc_t proc)
Get buffer size available for processing.
HG_EXPORT hg_return_t HG_Bulk_handle_serialize(void *buf, size_t buf_size, hg_bulk_t handle)
Serialize bulk handle into a buffer.
static HG_INLINE void * hg_proc_buf_memcpy(void *buf, void *data, size_t data_size, hg_proc_op_t op)
Copy data to buf if HG_ENCODE or buf to data if HG_DECODE and return incremented pointer to buf...
Definition: mercury_proc.h:235
HG_EXPORT void * hg_proc_buf_alloc(size_t size)
Can be used to allocate a buffer that will be used by the generic processor.
HG_EXPORT HG_PROC_INLINE hg_return_t hg_proc_hg_int8_t(hg_proc_t proc, hg_int8_t *data)
Inline prototypes (do not remove)
Definition: mercury_proc.h:302
HG_EXPORT hg_return_t hg_proc_set_buf_ptr(hg_proc_t proc, void *buf_ptr)
Set new buffer pointer (for manual encoding).
HG_EXPORT hg_return_t hg_proc_create(void *buf, size_t buf_size, hg_proc_op_t op, hg_proc_hash_t hash, hg_proc_t *proc)
Create a new encoding/decoding processor.