int mmap_remove(mmap_handle_t* mmap_handle) { FAIL_ON(unlink(mmap_handle->path) != 0); return 0; fail: return -1; }
int mmap_unload(mmap_handle_t* mmap_handle) { FAIL_ON(munmap(mmap_handle->start_addr, mmap_handle->size) != 0); return 0; fail: return -1; }
void Player::jump() { if(inPortal){ int32_t to = layer; if(portalDirection == Portal::Up){ to++; while(to < map->layers.size()){ if(map->layers[to]->type == Layer::Object) break; to++; } FAIL_ON(to == map->layers.size(), "No layer found going up"); } else { to--; while(to > 0 ){ if(map->layers[to]->type == Layer::Object) break; to--; } FAIL_ON(to < 0, "No layer found going down"); } ObjectLayer* layerO = (ObjectLayer*)map->layers[to]; Map* map = this->map; /* avoid use after free as this class is deleted */ shared_ptr<Player> player = shared_ptr<Player>(new Player(layerO, body->GetPosition(), to)); ((ObjectLayer*)map->layers[layer])->objects.erase(map->player); map->player = player; layerO->objects.insert(map->player); } else { setState(PlayerPreJump); jumpStart = time; } }
static inline int query_engine_tester(mws::MwsIndexNode* data, encoded_formula_t* query, result_callback_t cb, void *cb_handle) { const char* ms_path = TMPFILE_PATH; memsector_writer_t mswr; memsector_handle_t ms; /* ensure the file does not exist */ FAIL_ON(unlink(TMPFILE_PATH) != 0 && errno != ENOENT); FAIL_ON(memsector_create(&mswr, ms_path, TMPFILE_SIZE) != 0); printf("Memsector %s created\n", ms_path); data->exportToMemsector(&mswr); printf("Index exported to memsector\n"); printf("Space used: %d\n", memsector_size_inuse(&mswr.ms_header->alloc_header)); FAIL_ON(memsector_save(&mswr) != 0); printf("Memsector saved\n"); FAIL_ON(memsector_load(&ms, ms_path) != 0); printf("Memsector loaded\n"); if (query_engine_run(&ms.index, query, cb, cb_handle) == QUERY_ERROR) { goto fail; } printf("Query successfull\n"); FAIL_ON(memsector_remove(&ms) != 0); printf("Memsector removed\n"); return EXIT_SUCCESS; fail: if (data) delete data; return EXIT_FAILURE; }
int mmap_create(const char* path, off_t size, int flags, mmap_handle_t* mmap_handle) { int fd = -1; char* mapped_region; int oflags; int prot; /* round up size to page boundary */ size = round_to_page_size(size); /* setting open/mmap flags */ prot = PROT_READ | PROT_WRITE; oflags = O_RDWR | O_CREAT | O_EXCL; /* opening file */ FAIL_ON((fd = open(path, oflags, S_IRUSR | S_IWUSR)) < 0); /* set file size by seek and write */ FAIL_ON(lseek(fd, size - 1, SEEK_SET) == (off_t)-1); FAIL_ON(write(fd, " ", 1) != 1); /* memory-map the file */ mapped_region = mmap(/* addr = */ NULL, size, prot, flags, fd, /* offset = */ 0); FAIL_ON(mapped_region == MAP_FAILED); /* copy to mmap_handle */ mmap_handle->path = path; mmap_handle->start_addr = mapped_region; mmap_handle->size = size; /* close file descriptor */ (void) close(fd); return 0; fail: if (fd >= 0) (void) close(fd); return -1; }
int mmap_load(const char* path, int flags, mmap_handle_t* mmap_handle) { int fd = -1; struct stat s; off_t size; char* mapped_region; int oflags; int prot; /* setting open/mmap flags */ prot = PROT_READ; oflags = O_RDONLY; /* opening file */ FAIL_ON((fd = open(path, oflags)) < 0); /* get file size */ FAIL_ON(fstat(fd, &s) < 0); size = s.st_size; /* memory-map the file */ mapped_region = mmap(/* addr = */ NULL, size, prot, flags, fd, /* offset = */ 0); FAIL_ON(mapped_region == MAP_FAILED); /* copy to mmap_handle */ mmap_handle->path = path; mmap_handle->start_addr = mapped_region; mmap_handle->size = size; /* close file descriptor */ (void) close(fd); return 0; fail: if (fd >= 0) (void) close(fd); return -1; }
BEGIN_DECLS int save_pid_file(const char *path) { FILE* fp = fopen(path, "w"); FAIL_ON(fp == NULL); fprintf(fp, "%"PRIu64, (uint64_t) getpid()); return fclose(fp); fail: return -1; }