OpenMPI  0.1.1
ompi_misc.h
1 /*
2  * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
3  * University Research and Technology
4  * Corporation. All rights reserved.
5  * Copyright (c) 2004-2005 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 #ifndef OMPI_MISC_H
20 #define OMPI_MISC_H
21 
22 #include <stdio.h>
23 #include <stdlib.h>
24 
25 #define _SC_PAGESIZE 0
26 #define _SC_OPEN_MAX 1
27 
28 #if 0
29 /* currently, this is a memory leak */
30 static __inline char* getenv (const char *name)
31 {
32  int ret;
33  char *buffer;
34  DWORD length = GetEnvironmentVariable( (LPCSTR)name, NULL, 0 );
35 
36  if( 0 == length ) return NULL;
37  buffer = (char *)malloc(sizeof(char) * length);
38  ret = GetEnvironmentVariable((LPCSTR)name, (LPSTR)buffer, length);
39  return (ret > 0) ? buffer: NULL;
40 }
41 
42 
43 static __inline int setenv (const char *name, const char *value, int rewrite)
44 {
45  int ret;
46  if( 0 == rewrite ) {
47  DWORD length = 0;
48  if( 0 == (length = GetEnvironmentVariable( (LPCSTR)name, NULL, length )) ) {
49  if( ERROR_ENVVAR_NOT_FOUND == GetLastError() ) { /* do not exist */
50  return 0;
51  }
52  }
53  }
54  /* just push it back to the windows thingy */
55  ret = SetEnvironmentVariable ((LPCSTR)name, (LPCSTR)value);
56  return (0 != ret)? 1: 0;
57 }
58 #endif
59 
60 static __inline unsigned int sleep(unsigned int seconds) {
61 
62  /* Allow interruptions */
63  SleepEx(seconds * 1000, TRUE);
64  return 0;
65 }
66 
67 /* this function can currently ONLY return the page size. for it to
68  do the entire sysconf range it needs to be extended */
69 static __inline size_t sysconf(int option) {
70 
71  SYSTEM_INFO sys_info;
72 
73  if( _SC_OPEN_MAX == option ) {
74  return _getmaxstdio();
75  }
76 
77  GetSystemInfo(&sys_info);
78  if (_SC_PAGESIZE == option){
79  return (size_t)sys_info.dwPageSize;
80  }
81  printf( "This functionality is not supported: line: %d\tfile: %s\n",
82  __LINE__, __FILE__ );
83  abort();
84  return 0;
85 }
86 
87 #define F_GETFL 0
88 #define F_SETFL 1
89 #define O_NONBLOCK 0
90 /*
91  * this function is currently defined only for setting the socket to be
92  * in the non-blocking mode. Else this function returns error not implemented.
93  * This calls ioctlsocket in the winsock library
94  */
95 static __inline int fcntl (int fildes, int cmd, ...) {
96  int ret;
97  int mode;
98 
99  switch (cmd) {
100  case F_SETFL: mode = 1; ret = ioctlsocket ((SOCKET)fildes, FIONBIO, (u_long FAR*) &mode);
101  break;
102  case F_GETFL: ret = 0;
103  break;
104  default: printf("Option not supported: %d %s\n", __LINE__, __FILE__);
105  abort();
106  };
107 
108  return ret;
109 }
110 
111 #endif /* OMPI_MISC_H */