OpenMPI  0.1.1
convert.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2004-2007 The Trustees of Indiana University and Indiana
3  * University Research and Technology
4  * Corporation. All rights reserved.
5  * Copyright (c) 2004-2006 The University of Tennessee and The University
6  * of Tennessee Research Foundation. All rights
7  * reserved.
8  * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
9  * University of Stuttgart. All rights reserved.
10  * Copyright (c) 2004-2005 The Regents of the University of California.
11  * All rights reserved.
12  * $COPYRIGHT$
13  *
14  * Additional copyrights may follow
15  *
16  * $HEADER$
17  */
18 /**
19  * @file
20  *
21  * This file will hopefully not last long in the tree, but it's
22  * unfortunately necessary for now.
23  *
24  * There are multiple places in the code base where we need to safely
25  * convert from a size_t to an int. However, on some platforms,
26  * sizeof(size_t) is larger than sizeof(int), so casting from size_t
27  * -> int will result in a compiler warning and potentially data
28  * truncation.
29  *
30  * But, unfortunately, we still need to do it. But we definitely do
31  * not want compiler warnings. So when sizeof(size_t)>sizeof(int),
32  * the solution is the treat the size_t value like an array and
33  * dereference the appropriate nibble and cast that to an int (which
34  * accounts for both big and little endian machines).
35  *
36  * Most places in the code where this casting must occur are because
37  * collision of APIs (e.g., one API requires a size_t and another API
38  * requires an int. And in most places, we're not going to overflow
39  * the int when casting down into it (e.g., it's the result of a
40  * strlen, or the length of the buffer in an ompi_buffer_t -- if that
41  * buffer is larger than MAX_INT, we've got other problems!).
42  *
43  * BUT -- the whole premise of casting down to an int is dangerous.
44  * So we provide extra protection here to detect overflow situations
45  * and print out appropriate warnings. So if this situation ever
46  * occurs, we'll still overflow, but we'll have a good indication that
47  * it's happening, and where.
48  */
49 
50 #ifndef OPAL_CONVERT_H
51 #define OPAL_CONVERT_H
52 
53 #include "opal_config.h"
54 
55 BEGIN_C_DECLS
56 
57 /**
58  * Convert a size_t to an int.
59  *
60  * @param in The size_t value to be converted
61  * @param out The output int value.
62  * @param want_check Whether to check for truncation or not
63  *
64  * @returns OPAL_SUCESS If all went well
65  * @returns OPAL_NOT_SUPPORTED if the size_t value was truncated
66  *
67  * The conversion will always occur. However, if the size_t value was
68  * truncated (i.e., sizeof(size_t) > sizeof(int), and the cast down to
69  * the int actually changed the value), OPAL_NOT_SUPPORTED will be
70  * returned.
71  *
72  * On platforms where sizeof(size_t) <= sizeof(int), this function
73  * will aways return OPAL_SUCCESS.
74  */
75 OPAL_DECLSPEC int opal_size2int(size_t in, int *out, bool want_check) __opal_attribute_nonnull__(2);
76 
77 END_C_DECLS
78 
79 #endif
BEGIN_C_DECLS OPAL_DECLSPEC int opal_size2int(size_t in, int *out, bool want_check) __opal_attribute_nonnull__(2)
Convert a size_t to an int.
Definition: convert.c:31