示例#1
0
文件: vmspace.c 项目: roscopeco/mink
int vmspace_init(vmspace_t *vms, uintptr_t addr, uintptr_t sz) {
  /* FIXME: Assert starts and finishes on a page boundary! */
  range_t r;
  r.start = addr;
  r.extent = sz;

  vms->start = addr;
  vms->size = sz;
  spinlock_init(&vms->lock);

  size_t overhead = round_to_page_size(buddy_calc_overhead(r));
  size_t npages = overhead >> get_page_shift();
  uintptr_t start = r.start + r.extent - overhead;

  int ok = map(start,
               alloc_pages(PAGE_REQ_NONE, npages), npages,
               PAGE_WRITE);
  assert(ok == 0 && "map() failed in vmspace_init!");

  r.extent -= overhead;

  buddy_init(&vms->allocator, (uint8_t*)start, r, /*start_freed=*/0);

  buddy_free_range(&vms->allocator, r);

  return 0;
}
示例#2
0
文件: mmap.c 项目: KWARC/mws
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;
}