Skip to content

Commit

Permalink
Merge branch 'mmcco/assume-mmap-patch' blueness/sthttpd#5
Browse files Browse the repository at this point in the history
Signed-off-by: Joachim Nilsson <[email protected]>
  • Loading branch information
troglobit committed Feb 11, 2018
2 parents 2dc57dd + 1480171 commit eb44199
Showing 1 changed file with 3 additions and 43 deletions.
46 changes: 3 additions & 43 deletions src/mmc.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,7 @@
# include <time.h>
# endif
#endif

#ifdef HAVE_MMAP
#include <sys/mman.h>
#endif /* HAVE_MMAP */

#include "file.h"
#include "libhttpd.h"
Expand Down Expand Up @@ -286,8 +283,8 @@ void *mmc_map(char *filename, struct stat *sbP, struct timeval *nowP)
m->reftime = now;

/* Avoid doing anything for zero-length files; some systems don't like
** to mmap them, other systems dislike mallocing zero bytes.
*/
** to mmap them, other systems dislike mallocing zero bytes.
*/
if (m->size == 0) {
/* arbitrary non-NULL address */
m->addr = (void *)1;
Expand All @@ -305,7 +302,7 @@ void *mmc_map(char *filename, struct stat *sbP, struct timeval *nowP)
memcpy(m->addr, buf, size_size);
goto cont;
}
#ifdef HAVE_MMAP

/* Map the file into memory. */
m->addr = mmap(0, size_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (m->addr == (void *)-1 && errno == ENOMEM) {
Expand All @@ -324,45 +321,12 @@ void *mmc_map(char *filename, struct stat *sbP, struct timeval *nowP)

return NULL;
}
#else /* HAVE_MMAP */
/* Read the file into memory. */
m->addr = (void *)malloc(size_size);
if (!m->addr) {
/* Ooo, out of memory. Free all unreferenced maps
** and try again.
*/
panic();
m->addr = (void *)malloc(size_size);
}

if (!m->addr) {
syslog(LOG_ERR, "out of memory storing a file");
close(fd);
free(m);
--alloc_count;

return NULL;
}

if (file_read(fd, m->addr, size_size) != m->size) {
syslog(LOG_ERR, "read: %s", strerror(errno));
close(fd);
free(m->addr);
free(m);
--alloc_count;

return NULL;
}
#endif /* HAVE_MMAP */
}
close(fd);
cont:
/* Put the Map into the hash table. */
if (add_hash(m) < 0) {
syslog(LOG_ERR, "add_hash() failure");
#ifndef HAVE_MMAP
free(m->addr);
#endif
free(m);
--alloc_count;

Expand Down Expand Up @@ -484,12 +448,8 @@ static void really_unmap(Map **mm)

m = *mm;
if (m->size) {
#ifdef HAVE_MMAP
if (munmap(m->addr, m->size) < 0)
syslog(LOG_ERR, "munmap: %s", strerror(errno));
#else
free(m->addr);
#endif
}

/* Update the total byte count. */
Expand Down

0 comments on commit eb44199

Please sign in to comment.