bool f_shm_detach(int64 shm_identifier) { Lock lock(g_shm_mutex); set<sysvshm_shm*>::iterator iter = g_shms.find((sysvshm_shm*)shm_identifier); if (iter == g_shms.end()) { raise_warning("%lld is not a SysV shared memory index", shm_identifier); return false; } g_shms.erase(iter); delete *iter; return true; }
bool f_shm_has_var(int64 shm_identifier, int64 variable_key) { Lock lock(g_shm_mutex); std::set<sysvshm_shm*>::iterator iter = g_shms.find((sysvshm_shm*)shm_identifier); if (iter == g_shms.end()) { raise_warning("%" PRId64 " is not a SysV shared memory index", shm_identifier); return false; } sysvshm_shm *shm_list_ptr = *iter; long shm_varpos = check_shm_data(shm_list_ptr->ptr, variable_key); return shm_varpos >= 0; }
bool f_shm_remove(int64 shm_identifier) { Lock lock(g_shm_mutex); set<sysvshm_shm*>::iterator iter = g_shms.find((sysvshm_shm*)shm_identifier); if (iter == g_shms.end()) { raise_warning("%lld is not a SysV shared memory index", shm_identifier); return false; } sysvshm_shm *shm_list_ptr = *iter; if (shmctl(shm_list_ptr->id, IPC_RMID,NULL) < 0) { raise_warning("failed for key 0x%x, id %lld: %s", shm_list_ptr->key, shm_identifier, Util::safe_strerror(errno).c_str()); return false; } return true; }
bool f_shm_remove_var(int64 shm_identifier, int64 variable_key) { Lock lock(g_shm_mutex); set<sysvshm_shm*>::iterator iter = g_shms.find((sysvshm_shm*)shm_identifier); if (iter == g_shms.end()) { raise_warning("%lld is not a SysV shared memory index", shm_identifier); return false; } sysvshm_shm *shm_list_ptr = *iter; long shm_varpos = check_shm_data(shm_list_ptr->ptr, variable_key); if (shm_varpos < 0) { raise_warning("variable key %lld doesn't exist", variable_key); return false; } remove_shm_data(shm_list_ptr->ptr, shm_varpos); return true; }
Variant f_shm_get_var(int64 shm_identifier, int64 variable_key) { Lock lock(g_shm_mutex); set<sysvshm_shm*>::iterator iter = g_shms.find((sysvshm_shm*)shm_identifier); if (iter == g_shms.end()) { raise_warning("%lld is not a SysV shared memory index", shm_identifier); return false; } sysvshm_shm *shm_list_ptr = *iter; long shm_varpos = check_shm_data(shm_list_ptr->ptr, variable_key); if (shm_varpos < 0) { return false; } sysvshm_chunk *shm_var = (sysvshm_chunk*)((char *)shm_list_ptr->ptr + shm_varpos); return f_unserialize(String(&shm_var->mem, shm_var->length, AttachLiteral)); }
Variant f_shm_attach(int64 shm_key, int64 shm_size /* = 10000 */, int64 shm_flag /* = 0666 */) { char *shm_ptr; long shm_id; if (shm_size < 1) { raise_warning("Segment size must be greater then zero."); return false; } sysvshm_shm *shm_list_ptr = new sysvshm_shm(); /* get the id from a specified key or create new shared memory */ if ((shm_id = shmget(shm_key, 0, 0)) < 0) { if (shm_size < (int)sizeof(sysvshm_chunk_head)) { raise_warning("failed for key 0x%x: memorysize too small", shm_key); free(shm_list_ptr); return false; } if ((shm_id = shmget(shm_key, shm_size, shm_flag | IPC_CREAT | IPC_EXCL)) < 0) { raise_warning("failed for key 0x%x: %s", shm_key, Util::safe_strerror(errno).c_str()); free(shm_list_ptr); return false; } } if ((shm_ptr = (char*)shmat(shm_id, NULL, 0)) == (char *)-1) { raise_warning("failed for key 0x%x: %s", shm_key, Util::safe_strerror(errno).c_str()); free(shm_list_ptr); return false; } /* check if shm is already initialized */ sysvshm_chunk_head *chunk_ptr = (sysvshm_chunk_head *)shm_ptr; if (strcmp((char*) &(chunk_ptr->magic), "PHP_SM") != 0) { strcpy((char*) &(chunk_ptr->magic), "PHP_SM"); chunk_ptr->start = sizeof(sysvshm_chunk_head); chunk_ptr->end = chunk_ptr->start; chunk_ptr->total = shm_size; chunk_ptr->free = shm_size-chunk_ptr->end; } shm_list_ptr->key = shm_key; shm_list_ptr->id = shm_id; shm_list_ptr->ptr = chunk_ptr; Lock lock(g_shm_mutex); g_shms.insert(shm_list_ptr); return (int64)shm_list_ptr; }
bool f_shm_put_var(int64 shm_identifier, int64 variable_key, CVarRef variable) { Lock lock(g_shm_mutex); set<sysvshm_shm*>::iterator iter = g_shms.find((sysvshm_shm*)shm_identifier); if (iter == g_shms.end()) { raise_warning("%lld is not a SysV shared memory index", shm_identifier); return false; } sysvshm_shm *shm_list_ptr = *iter; /* setup string-variable and serialize */ String serialized = f_serialize(variable); /* insert serialized variable into shared memory */ int ret = put_shm_data(shm_list_ptr->ptr, variable_key, (char*)serialized.data(), serialized.size()); if (ret == -1) { raise_warning("not enough shared memory left"); return false; } return true; }