code reorganisation phase-I
[csql.git] / src / storage / Util.cxx
blobd3541d98fc90419bc4cfc0768377e78fb1fd25d2
1 #include<Util.h>
2 #include<Config.h>
4 unsigned int Util::hashBinary(char *strVal, int length)
6 unsigned int hval, g;
7 hval = 0;
8 char *str =strVal;
9 int iter = 0;
10 while (iter != length)
12 hval <<= 4;
13 hval += (unsigned int) *str++;
14 g = hval & ((unsigned int) 0xf << (32 - 4));
15 if (g != 0)
17 hval ^= g >> (32 - 8);
18 hval ^= g;
20 iter++;
22 return hval;
25 DbRetVal GlobalUniqueID::create()
27 int key = Conf::config.getShmIDKey();
28 int id = os::shm_create(key, MAX_UNIQUE_ID *sizeof(int), 0666);
29 if (-1 == id) {
30 printError(ErrOS, "Unable to create shared memory");
31 return ErrOS;
33 ptr = os::shm_attach(id, NULL, 0);
34 if ((void*)-1 == ptr) {
35 printError(ErrOS, "Unable to attach shared memory");
36 return ErrOS;
38 memset(ptr, 0, MAX_UNIQUE_ID *sizeof(int));
41 DbRetVal GlobalUniqueID::open()
43 if (ptr != NULL) return OK;
44 int key = Conf::config.getShmIDKey();
45 int id = os::shm_open(key, MAX_UNIQUE_ID *sizeof(int), 0666);
46 if (-1 == id) {
47 printError(ErrOS, "Unable to create shared memory");
49 ptr = os::shm_attach(id, NULL, 0);
50 if ((void*)-1 == ptr) {
51 printError(ErrOS, "Unable to create shared memory");
53 return OK;
56 DbRetVal GlobalUniqueID::destroy()
58 int key = Conf::config.getShmIDKey();
59 int id = os::shm_open(key, MAX_UNIQUE_ID *sizeof(int), 0666);
60 if (-1 == id) {
61 printError(ErrOS, "Unable to open shared memory");
62 return ErrOS;
64 os::shmctl(id, IPC_RMID);
67 int GlobalUniqueID::getID(UniqueIDType type)
69 int *id = (int*)(((char*)ptr) + sizeof(int) * type);
70 int oldVal = *id;
71 int newVal = oldVal + 1;
72 int ret = Mutex::CAS(id, oldVal, newVal);
73 if (ret) return -1;
74 return *id;