code reorg and moving files to csql base directory
[csql.git] / src / base / Util.cxx
blob7ad53cd8eaa4d0c645b2bb4ce13a10ae3ae87979
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 unsigned int Util::hashString(char *strVal)
27 unsigned int hval, g;
28 hval = 0;
29 char *str =strVal;
30 while (*str != '\0')
32 hval <<= 4;
33 hval += (unsigned int) *str++;
34 g = hval & ((unsigned int) 0xf << (32 - 4));
35 if (g != 0)
37 hval ^= g >> (32 - 8);
38 hval ^= g;
41 return hval;
44 DbRetVal GlobalUniqueID::create()
46 int key = Conf::config.getShmIDKey();
47 int id = os::shm_create(key, MAX_UNIQUE_ID *sizeof(int), 0666);
48 if (-1 == id) {
49 if (errno != EEXIST)
50 printError(ErrOS, "Unable to create shared memory");
51 return ErrOS;
53 ptr = os::shm_attach(id, NULL, 0);
54 if ((void*)-1 == ptr) {
55 printError(ErrOS, "Unable to attach shared memory");
56 return ErrOS;
58 memset(ptr, 0, MAX_UNIQUE_ID *sizeof(int));
59 return OK;
62 DbRetVal GlobalUniqueID::open()
64 if (ptr != NULL) return OK;
65 int key = Conf::config.getShmIDKey();
66 int id = os::shm_open(key, MAX_UNIQUE_ID *sizeof(int), 0666);
67 if (-1 == id) {
68 printError(ErrOS, "Unable to create shared memory");
70 ptr = os::shm_attach(id, NULL, 0);
71 if ((void*)-1 == ptr) {
72 printError(ErrOS, "Unable to create shared memory");
74 return OK;
77 DbRetVal GlobalUniqueID::destroy()
79 int key = Conf::config.getShmIDKey();
80 int id = os::shm_open(key, MAX_UNIQUE_ID *sizeof(int), 0666);
81 if (-1 == id) {
82 // printError(ErrOS, "Unable to open shared memory");
83 return ErrOS;
85 os::shm_remove(id);
86 return OK;
89 int GlobalUniqueID::getID(UniqueIDType type)
91 InUse *id = (int*)(((char*)ptr) + sizeof(int) * type);
92 InUse oldVal = *id;
93 InUse newVal = oldVal + 1;
94 int ret = Mutex::CASGen(id, oldVal, newVal);
95 if (ret) return -1;
96 return *id;