int get_fd_from_cache(FDcache fd_cache, const char path[], off_t* pst_size) { struct pathfd_t* p; int fd; struct stat stat_buf; if (fd_cache == NULL) return -1; if (strlen(path) >= FD_CACHE_MAX_PATH_LEN - 1) return -1; for (p = fd_cache->a; p != fd_cache->aend; ++p) { if (!strcmp(p->path, path)) { *pst_size = p->st_size; return p->fd; } } if ((fd = add_fd_to_cache(fd_cache, path, pst_size)) == -1) { if ((fd = open(fd_cache->dir_prefix_buf, O_RDONLY|O_NONBLOCK)) == -1) return -1; if (fstat(fd, &stat_buf) == -1) { logfl(fd_cache->logger, "fstat error: %s", strerror(errno)); return -1; } *pst_size = stat_buf.st_size; return fd; } else { return fd; } }
void e820::print(const char* who) const { unsigned long avl_size = 0, resv_size = 0; logfl("%s provided E820 memory map with %u entries:", who, nr_entries); for (int i = 0; i < nr_entries; ++i) { logfl("memory [%016lx - %016lx) %s", map[i].addr, map[i].addr + map[i].len, type_to_string(map[i].type)); if (map[i].type == E820_AVAILABLE) avl_size += map[i].len; else resv_size += map[i].len; } logfl("available memory: %u MiB reserved memory: %u KiB", avl_size / 1MiB, resv_size / 1KiB); }
FDcache newFDcache(const char dir_prefix[], Logger errl) { FILE* filog; FDcache fd_cache; if (strlen(dir_prefix) > FD_CACHE_DIR_PREFIX_LEN - FD_CACHE_MAX_PATH_LEN) return NULL; if ((fd_cache = (FDcache)malloc(sizeof(struct fd_cache_t))) == NULL) { logfl(errl, "malloc error: %s", strerror(errno)); exit(1); } strcpy(fd_cache->dir_prefix_buf, dir_prefix); fd_cache->dir_prefix_pend = fd_cache->dir_prefix_buf + strlen(dir_prefix); fd_cache->aend = fd_cache->a; fd_cache->logger = errl; return fd_cache; }
static int add_fd_to_cache(FDcache fd_cache, const char path[], off_t* pst_size) { struct stat stat_buf; if (fd_cache == NULL) return -1; strcpy(fd_cache->dir_prefix_pend, path); if (fd_cache->aend - fd_cache->a == FD_CACHE_LEN) return -1; if ((fd_cache->aend->fd = open(fd_cache->dir_prefix_buf, O_RDONLY|O_NONBLOCK)) == -1) return -1; if (fstat(fd_cache->aend->fd, &stat_buf) == -1) { logfl(fd_cache->logger, "fstat error: %s", strerror(errno)); return -1; } *pst_size = fd_cache->aend->st_size = stat_buf.st_size; strcpy(fd_cache->aend->path, path); fd_cache->aend++; return (fd_cache->aend - 1)->fd; }