Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added mmap-based implementation for POSIX-compliant systems. #5

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Added mmap-based implementation for POSIX-compliant systems. #5

wants to merge 4 commits into from

Conversation

ghost
Copy link

@ghost ghost commented Apr 22, 2018

Preamble

Tar is a very simple format, which makes it possible to very easily find a pointer to a packed file.
Using mmap, I added the ability to directly map a tar file into the computer's address space, and
find pointers to large pieces of memory.

This eliminates potentially unnecessary copies, and in my testing (getting the contents of a file many
thousands of times), the mmap-based implementation proved to be 60% faster, which is a great improvement.

What was added

I added mtar_open_mapped,mtar_get_mapped, and mtar_get_pointer
to open, query, and loop through a mapped file respectively.

What was lost

All features were added, no functionality was lost in the base API.

Example

mtar_t tar;
const char *str;
mtar_header_t h;

/* Open the file and map its contents */
mtar_open_mapped(&tar, "test.tar");

/* Print all file names and sizes */
while ( (mtar_read_header(&tar, &h)) != MTAR_ENULLRECORD ) {
  const char *data;
  printf("%s (%d bytes)\n", h.name, h.size);
  /* Get file pointer and bump to next header */
  mtar_get_pointer(&tar, (const void**) &data);
}

/* Get mapped pointer to "test.txt" */
mtar_get_mapped(&tar, "test.txt", &str);
/* Directly print contents */
printf("%s", str);
mtar_close(&tar);

tar->close = file_close;

/* Open file */
info = malloc(sizeof(struct mmap_info));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

info is never freed.

Copy link

@galsjel galsjel May 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should be able to get away with initializing this on the stack, I don't see a purpose for allocating if it never leaves this scope.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants