void Hash2_init(Hash2 *hash, Mem *mem, int init_size) { hash->count = 0; init_size = align_pow2(init_size, 16); hash->entry_num = init_size; hash->entry = Mem_get(mem, sizeof(HashEntry2) * init_size); memset(hash->entry, 0, sizeof(HashEntry2) * init_size); }
RefMap *refmap_new(int size) { RefMap *rm = buf_new(fs->cls_map, sizeof(RefMap)); int max = align_pow2(size == 0 ? 32 : size, 32); HashValueEntry **entry = malloc(sizeof(HashValueEntry*) * max); memset(entry, 0, sizeof(HashValueEntry*) * max); rm->entry = entry; rm->entry_num = max; rm->count = 0; return rm; }
shm_buf_t *shm_init(void *p, unsigned sz) { if (sz < sizeof(shm_buf_t) + sizeof(unsigned)) return 0; else { shm_buf_t *buf = (shm_buf_t *) p; buf->mutex = 0; buf->sz = align_pow2((sz - sizeof(shm_buf_t)) / sizeof(unsigned)); buf->top = buf->tail = 0; buf->pending = buf->lost = 0; return buf; } }
int main(int argc, char **argv) { io_context_t ctx; struct iocb iocb; struct iocb *iocbs[1]; struct io_event event; struct stat st; char *ptr, *buf, one[PATH_MAX], two[PATH_MAX]; size_t buflen, ptrlen; long rc; int fd1, fd2; if ((argc < 2) || (strcmp(argv[1],"-h") == 0)) { printf("Usage: partial_aio_direct <filename>\n"); exit(1); } snprintf(one, sizeof(one), "%s-1", argv[1]); snprintf(two, sizeof(two), "%s-2", argv[1]); unlink(one); unlink(two); fd1 = open(one, O_CREAT|O_RDWR|O_DIRECT, 0644); if (fd1 < 0) { perror("open"); exit(1); } fd2 = open(two, O_CREAT|O_RDWR|O_DIRECT, 0644); if (fd2 < 0) { perror("open"); exit(1); } if (fstat(fd1, &st)) { perror("open"); exit(1); } if (ftruncate(fd1, st.st_blksize)) { perror("ftruncate()"); exit(1); } if (ftruncate(fd2, st.st_blksize)) { perror("ftruncate()"); exit(1); } if (ftruncate(fd1, st.st_blksize * 2)) { perror("ftruncate()"); exit(1); } if (ftruncate(fd2, st.st_blksize * 2)) { perror("ftruncate()"); exit(1); } /* assumes this is a power of two */ buflen = st.st_blksize * 2; ptrlen = st.st_blksize * 4; /* some slop */ ptr = calloc(1, ptrlen); if (ptr == NULL) { perror("calloc"); exit(1); } /* align buf to the next natural buflen alignment after ptr */ buf = align_pow2(ptr, st.st_blksize); rc = io_queue_init(100, &ctx); if (rc) { printf("queue_init: %ld\n", rc); exit(1); } io_prep_pwrite(&iocb, fd1, buf, buflen, 0); memset(ptr, 0x42, ptrlen); printf("saw block size %lu, writing with buflen %lu\n", st.st_blksize, buflen); iocbs[0] = &iocb; rc = io_submit(ctx, 1, iocbs); if (rc != 1) { printf("submit: %ld\n", rc); exit(1); } rc = io_getevents(ctx, 1, 1, &event, NULL); printf("got %ld: data %p iocb %p res %ld res2 %ld\n", rc, event.data, event.obj, event.res, event.res2); return 0; }
void disas_objdump(FILE *fp_out, const char *objdump, const char *machine, bool big_endian, uint64_t addr, void *buf, size_t len) { char *output; char template_in[] = "/tmp/etrace-disas-in-XXXXXX"; char template_out[] = "/tmp/etrace-disas-out-XXXXXX"; char *str = NULL; long pagesize = sysconf(_SC_PAGE_SIZE); int stdio[3] = {0, 1, 2}; int fd_out, fd_in; size_t fsize, fsize_aligned, pos = 0; pid_t wpid; pid_t kid; fd_in = mkstemp(template_in); fd_out = mkstemp(template_out); if (fd_in < 0 || fd_out < 0) { perror("mkstemp"); exit(1); } unlink(template_out); safe_write(fd_in, buf, len); if (asprintf(&str, "--adjust-vma=0x%" PRIx64, addr) < 0) { fprintf(stderr, "asprintf failed\n"); exit(1); } char *nm_argv[] = { (char *) objdump, "-D", "-b", "binary", "-m", (char *) machine, big_endian ? "-EB" : "-EL", str, template_in, NULL}; stdio[1] = fd_out; kid = run(nm_argv[0], nm_argv, stdio); wpid = waitpid(kid, NULL, 0); if (wpid != kid) { perror("wait"); exit(1); } unlink(template_in); /* Done, now mmap the file. */ fsize = get_filesize(fd_out); fsize_aligned = align_pow2(fsize, pagesize); output = mmap(NULL, fsize_aligned, PROT_READ, MAP_PRIVATE, fd_out, 0); if (output == MAP_FAILED) { perror("mmap"); exit(1); } { char *s = output; int nl; for (nl = 0; nl < 7; nl++) { while (s[pos++] != '\n') ; } fflush(stdout); safe_write(1, s + pos, fsize - pos); } close(fd_in); close(fd_out); free(str); }