changing lock bucket size
[csql.git] / include / os.h
blob6ccb42cdea1d247d2899372d69896e32fb1ffcf9
1 /*************************************************************************
2 * *
3 * Copyright (C) Lakshya Solutions Ltd. All rights reserved. *
4 * *
5 ***************************************************************************/
7 #ifndef OS_H
8 #define OS_H
9 #include<build.h>
11 //Added for gettimeofday impl for windows
12 #if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
13 #define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64
14 #else
15 #define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
16 #endif
19 #define SQL_STMT_LEN 8192
20 #define MAX_FILE_LEN 1024
21 #define IDENTIFIER_LENGTH 64
22 #define ERROR_STRING_LENGTH 128
23 #define DEFAULT_VALUE_BUF_LENGTH 32
24 #define STATE_LENGTH 8
25 #define SYSTEMDB "SYSTEMDB"
26 #define DBAUSER "root"
27 #define DBAPASS "manager"
28 #define I_USER "i@1r4D_f$_a"
29 #define I_PASS "a_$f_D4r1@i"
30 #define DEFAULT_CONFIG_FILE "/etc/csql/csql.conf"
31 #define LOCK_BUCKET_SIZE 2048
32 #define STMT_BUCKET_SIZE 1023
33 #define MAX_CHUNKS 20
34 #define PAGE_SIZE Conf::config.getPageSize()
35 #define MAX_MUTEX_PER_THREAD 5
36 #define MAX_THREADS_PER_PROCESS 30
37 #define MAX_FILE_PATH_LEN 1024
38 #define CHUNK_NAME_LEN 64
39 #define LOG_ROLLOVER_SIZE 20*1024*1024
40 #define SIGCSQL1 SIGUSR1
41 #define MIN_VARCHAR_ALLOC_SIZE 30
43 #define BIT(x) (1 << (x))
44 #define SETBITS(x,y) ((x) |= (y))
45 #define CLEARBITS(x,y) ((x) &= (~(y)))
46 #define SETBIT(x,y) SETBITS((x), (BIT((y))))
47 #define CLEARBIT(x,y) CLEARBITS((x), (BIT((y))))
48 #define BITSET(x,y) ((x) & (BIT(y)))
49 #define DllExport
50 typedef void (*sighandler_t)(int);
51 #define LHANDLE void*
52 #define LENGTH socklen_t
54 #include <stdio.h>
55 #include <fcntl.h>
56 #include <sys/types.h>
57 #include <sys/stat.h>
58 #include <math.h>
59 #include <string.h>
60 #include <stdlib.h>
61 #include <limits.h>
62 #include <signal.h>
65 #if defined(SOLARIS) || defined(LINUX) || defined(FreeBSD)
67 #include <sys/mman.h>
68 #include <sys/msg.h>
69 #include <sys/shm.h>
70 #include <unistd.h>
71 #include <pthread.h>
72 #include <ctype.h>
73 #include <sys/socket.h>
74 #include <fnmatch.h>
75 #include <sys/time.h>
76 #include <stdarg.h>
77 #include <net/if.h>
78 #include <arpa/inet.h>
79 #include <netinet/in.h>
80 #include <netinet/tcp.h>
81 #include <sys/file.h>
82 #include <sys/wait.h>
83 #include <netdb.h>
84 #if defined(SOLARIS)
85 #include <sys/varargs.h>
86 #include <crypt.h>
87 #include <errno.h>
88 #include <sys/atomic.h>
89 #include <timecsql.h>
90 #include <ucontext.h>
91 //extern int errno;
92 #endif
93 #if defined(LINUX)
94 #include <execinfo.h>
95 #include <ifaddrs.h>
96 #include <sys/errno.h>
97 #include <crypt.h>
98 #endif
99 #if defined(FreeBSD)
100 #include <sys/errno.h>
101 #endif
102 #include <dlfcn.h>
105 enum FileOpenMode
107 fileOpenReadOnly = O_RDONLY,
108 fileOpenWriteOnly = O_WRONLY,
109 fileOpenReadWrite = O_RDWR,
110 fileOpenAppend = O_CREAT| O_APPEND |O_RDWR,
111 fileOpenCreat = O_CREAT |O_RDWR,
112 // If set and fileOpenExcl is set, the
113 // open will fail if the file already exists.
114 fileOpenExcl = O_EXCL,
115 fileOpenTrunc = O_TRUNC
118 enum MapProtMode
120 mapProtRead = PROT_READ,
121 mapProtWrite = PROT_WRITE,
122 mapProcExec = PROT_EXEC,
123 mapProcNone = PROT_NONE
126 enum MapMode
128 mapShared = MAP_SHARED,
129 mapPrivate = MAP_PRIVATE,
130 mapFixed = MAP_FIXED,
131 // Interpret address exactly.
132 mapNoReserve = MAP_NORESERVE,
133 // Don't reserver swap space.
135 //mapAlign = MAP_ALIGN,
136 //for msync system call
137 mapSync = MS_SYNC ,
138 mapASync = MS_ASYNC
141 typedef int file_desc;
142 typedef key_t shared_memory_key;
143 typedef int shared_memory_id;
144 #if defined(__sparcv9)
145 typedef long InUse;
146 #else
147 typedef int InUse;
148 #endif
150 #elif defined(WINNT)
151 #include <io.h>
152 #include <time.h>
153 #include <winsock2.h>
154 #include <errno.h>
155 #include <tchar.h>
157 #define snprintf sprintf_s
158 #define strncasecmp _strnicmp
159 #define strcasecmp strcmpi
160 #define vsnprintf vsnprintf_s
161 #define strtok_r strtok_s
162 typedef HANDLE file_desc;
163 typedef long off_t;
164 typedef void* caddr_t;
165 typedef int InUse;
166 typedef int pid_t;
167 typedef int pthread_t;
168 typedef int mode_t;
169 typedef int key_t;
170 typedef int shared_memory_key;
171 typedef int shared_memory_id;
172 typedef size_t socklen_t;
173 #define MAP_FAILED -1
174 #define MSG_NOSIGNAL 0
175 #define SHM_RND 0
176 #define IPC_RMID 0
177 #define SQL_API __declspec(dllexport)
178 #define ALLREADY_HAVE_WINDOWS_TYPE
179 #define SIGCSQL1 0 //drop table signal wont work for windoes
180 #define SIGCHLD SIGTERM
181 #define MS_SYNC 0
182 #define O_SYNC 0
183 #define O_DIRECT 0
184 #define LENGTH int
185 #define LHANDLE HMODULE
186 #ifndef APP_BUILD
187 #define DllExport __declspec( dllexport )
188 #else
189 #define DllExport
190 #endif
191 typedef signed short int SWORD;
192 #define SQL_NOUNICODEMAP //Note: to suppress mapping to unicode APIs
193 enum FileOpenMode
195 fileOpenReadOnly = _O_RDONLY,
196 fileOpenWriteOnly = _O_WRONLY,
197 fileOpenReadWrite = _O_RDWR,
198 fileOpenAppend = _O_CREAT| _O_APPEND |_O_RDWR,
199 fileOpenCreat = _O_CREAT |_O_RDWR,
200 // If set and fileOpenExcl is set, the
201 // open will fail if the file already exists.
202 fileOpenExcl = _O_EXCL,
203 fileOpenTrunc = _O_TRUNC
205 enum MapProtMode
207 mapProtRead = PAGE_READONLY,
208 mapProtWrite = PAGE_READWRITE,
209 mapProcExec = PAGE_READWRITE, //execute is not supported in win2k
210 mapProcNone = PAGE_READWRITE // exec and none mode is not used in code
213 //Mapping all modes to dummy for now
214 enum MapMode
216 mapShared = PAGE_READWRITE ,
217 mapPrivate = PAGE_READWRITE ,
218 mapFixed = PAGE_READWRITE ,
219 mapNoReserve = PAGE_READWRITE ,
220 mapSync = PAGE_READWRITE ,
221 mapASync = PAGE_READWRITE
224 extern DllExport int optind, opterr;
225 extern DllExport TCHAR *optarg;
227 #endif
229 class DllExport os
231 public:
232 static void* mmap(void* addr, size_t len, int prot, int flags, file_desc fildes, off_t off);
233 static int munmap(caddr_t addr, size_t len);
234 static int open(const char *name, FileOpenMode flags, size_t size);
235 static int close(int fd);
236 static file_desc openFile(const char *name, FileOpenMode flags, size_t size=0);
237 static int closeFile(file_desc fd);
238 static int closeSocket(int fd);
239 static int lockFile(int fd);
240 static int unlockFile(int fd);
241 static int truncate(const char* fname);
242 static off_t lseek(int fildes, off_t offset, int whence);
243 static int openFileForAppend(const char* fname, int flags);
244 static int getFileSize(const char* fname);
245 static size_t write(int fildes, char *buf, size_t size);
246 static int msync(caddr_t addr, size_t len, int flags);
247 static int fsync(int fildes);
248 inline static size_t alignLong(size_t size)
249 { return ((size - 1) | (sizeof(long) - 1)) + 1;}
250 inline static size_t align(size_t size)
251 { return ((size - 1) | (sizeof(InUse) - 1)) + 1;}
252 static char* encrypt(const char * key, const char *salt);
253 static void* memset(void *src, int c, size_t size);
254 static void* memcpy(void *src, const void *dest, size_t size);
255 static int memcmp(const void *s1, const void *s2, size_t size);
256 static int select(int nfds, fd_set *readfds, fd_set *writefds,
257 fd_set *exceptfds, struct timeval * timeout);
259 static int usleep(int microsecs);
260 static int sleep(int secs);
261 static shared_memory_id shm_create(shared_memory_key key, size_t size, int flag);
262 static shared_memory_id shm_open(shared_memory_key key, size_t size, int flag);
263 static void* shm_attach(shared_memory_id id, const void *ptr, int flag);
264 static int shm_detach (void*);
265 static int shmctl(int shmid, int cmd);
266 static int shm_remove(int shmid);
267 inline static double floor(double val)
268 { return ::floor(val); }
269 static sighandler_t signal(int signum, sighandler_t handler);
271 static int gettimeofday(struct timeval *tp);
272 static struct tm* localtime(long *secs);
273 static pid_t getpid();
274 static pthread_t getthrid();
275 static char* getenv(const char *envVarName);
276 static int setenv(const char *envVarName, const char *value);
278 static int kill(pid_t pid, int sig);
279 static bool atobool(char *value);
280 static pid_t createProcess(const char* cmdName, const char *arg0);
281 static pid_t fork();
282 static size_t send(int fd, const void *buf, size_t len, int flags);
283 static size_t recv(int fd, void *buf, size_t len, int flags);
284 static int gethostname(char *hostname, size_t len);
285 static int strmatch(char *pattern, char *str);
286 static int msgget(key_t key, int oflag);
287 static int msgsnd(int msqid, const void *ptr, size_t len, int flag);
288 static int msgrcv(int msqid, void *ptr, size_t len, long type, int flag);
289 static int msgctl(int msqid, int cmd, struct msqid_ds *buff);
290 static int isValidIP(char ipstr[] );
291 static bool fileExists(char *fileName);
292 static char* strcasestr(char *s1, const char *s2);
293 static int getNoOfProcessors();
294 static mode_t umask(mode_t mask);
295 static int fdatasync(file_desc fd);
296 static int atexit(void (*exitHndlr)(void));
297 static void* dlsym(LHANDLE hdl, char* funcName);
298 static char getopt(int argc, char *argv[], char *opt);
301 #endif