allocator fixes
[csql.git] / src / storage / Util.cxx
blob27a6b75e711ae8220a60bcfc44f117d390b28532
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 if (errno != EEXIST)
31 printError(ErrOS, "Unable to create shared memory");
32 return ErrOS;
34 ptr = os::shm_attach(id, NULL, 0);
35 if ((void*)-1 == ptr) {
36 printError(ErrOS, "Unable to attach shared memory");
37 return ErrOS;
39 memset(ptr, 0, MAX_UNIQUE_ID *sizeof(int));
42 DbRetVal GlobalUniqueID::open()
44 if (ptr != NULL) return OK;
45 int key = Conf::config.getShmIDKey();
46 int id = os::shm_open(key, MAX_UNIQUE_ID *sizeof(int), 0666);
47 if (-1 == id) {
48 printError(ErrOS, "Unable to create shared memory");
50 ptr = os::shm_attach(id, NULL, 0);
51 if ((void*)-1 == ptr) {
52 printError(ErrOS, "Unable to create shared memory");
54 return OK;
57 DbRetVal GlobalUniqueID::destroy()
59 int key = Conf::config.getShmIDKey();
60 int id = os::shm_open(key, MAX_UNIQUE_ID *sizeof(int), 0666);
61 if (-1 == id) {
62 // printError(ErrOS, "Unable to open shared memory");
63 return ErrOS;
65 os::shmctl(id, IPC_RMID);
68 int GlobalUniqueID::getID(UniqueIDType type)
70 int *id = (int*)(((char*)ptr) + sizeof(int) * type);
71 int oldVal = *id;
72 int newVal = oldVal + 1;
73 int ret = Mutex::CAS(id, oldVal, newVal);
74 if (ret) return -1;
75 return *id;