prepare for new fmap API

Introduce a vtable for core fmap functions, and provide static inline wrappers
that call these.
Also switch over the POSIX interface to use the vtable.
remotes/push_mirror/fmapify
Török Edvin 14 years ago
parent 778408051c
commit d2bfc86ae1
  1. 75
      libclamav/fmap.c
  2. 96
      libclamav/fmap.h

@ -92,6 +92,12 @@ pthread_mutex_t fmap_mutex = PTHREAD_MUTEX_INITIALIZER;
which may in turn prevent some mmap constants to be defined */
ssize_t pread(int fd, void *buf, size_t count, off_t offset);
static const void *handle_need(fmap_t *m, size_t at, size_t len, int lock);
static void handle_unneed_off(fmap_t *m, size_t at, size_t len);
static const void *handle_need_offstr(fmap_t *m, size_t at, size_t len_hint);
static const void *handle_gets(fmap_t *m, char *dst, size_t *at, size_t max_len);
static void unmap_mmap(fmap_t *m);
static void unmap_malloc(fmap_t *m);
fmap_t *fmap_check_empty(int fd, off_t offset, size_t len, int *empty) {
unsigned int pages, mapsz, hdrsz;
@ -154,6 +160,11 @@ fmap_t *fmap_check_empty(int fd, off_t offset, size_t len, int *empty) {
m->pgsz = pgsz;
m->paged = 0;
m->dont_cache_flag = 0;
m->unmap = dumb ? unmap_malloc : unmap_mmap;
m->need = handle_need;
m->need_offstr = handle_need_offstr;
m->gets = handle_gets;
m->unneed_off = handle_unneed_off;
return m;
}
@ -343,7 +354,7 @@ static int fmap_readpage(fmap_t *m, unsigned int first_page, unsigned int count,
}
static void *fmap_need(fmap_t *m, size_t at, size_t len, int lock) {
static const void *handle_need(fmap_t *m, size_t at, size_t len, int lock) {
unsigned int first_page, last_page, lock_count;
char *ret;
@ -371,23 +382,6 @@ static void *fmap_need(fmap_t *m, size_t at, size_t len, int lock) {
return (void *)ret;
}
void *fmap_need_off(fmap_t *m, size_t at, size_t len) {
return fmap_need(m, at, len, 1);
}
void *fmap_need_off_once(fmap_t *m, size_t at, size_t len) {
return fmap_need(m, at, len, 0);
}
void *fmap_need_ptr(fmap_t *m, void *ptr, size_t len) {
return fmap_need_off(m, (char *)ptr - (char *)m - m->hdrsz, len);
}
void *fmap_need_ptr_once(fmap_t *m, void *ptr, size_t len) {
return fmap_need_off_once(m, (char *)ptr - (char *)m - m->hdrsz, len);
}
void *fmap_need_str(fmap_t *m, void *ptr, size_t len_hint) {
size_t at = (char *)ptr - (char *)m - m->hdrsz;
return fmap_need_offstr(m, at, len_hint);
}
static void fmap_unneed_page(fmap_t *m, unsigned int page) {
uint32_t s = fmap_bitmap[page];
@ -406,7 +400,7 @@ static void fmap_unneed_page(fmap_t *m, unsigned int page) {
return;
}
void fmap_unneed_off(fmap_t *m, size_t at, size_t len) {
static void handle_unneed_off(fmap_t *m, size_t at, size_t len) {
unsigned int i, first_page, last_page;
if(m->dumb) return;
if(!len) {
@ -427,23 +421,21 @@ void fmap_unneed_off(fmap_t *m, size_t at, size_t len) {
}
}
void fmap_unneed_ptr(fmap_t *m, void *ptr, size_t len) {
fmap_unneed_off(m, (char *)ptr - (char *)m - m->hdrsz, len);
}
void funmap(fmap_t *m) {
static void unmap_mmap(fmap_t *m)
{
#ifdef ANONYMOUS_MAP
if(!m->dumb) {
size_t len = m->pages * m->pgsz + m->hdrsz;
fmap_lock;
munmap((void *)m, len);
fmap_unlock;
} else
size_t len = m->pages * m->pgsz + m->hdrsz;
fmap_lock;
munmap((void *)m, len);
fmap_unlock;
#endif
free((void *)m);
}
void *fmap_need_offstr(fmap_t *m, size_t at, size_t len_hint) {
static void unmap_malloc(fmap_t *m) {
free((void *)m);
}
static const void *handle_need_offstr(fmap_t *m, size_t at, size_t len_hint) {
unsigned int i, first_page, last_page;
void *ptr = (void *)((char *)m + m->hdrsz + at);
@ -482,8 +474,7 @@ void *fmap_need_offstr(fmap_t *m, size_t at, size_t len_hint) {
return NULL;
}
void *fmap_gets(fmap_t *m, char *dst, size_t *at, size_t max_len) {
static const void *handle_gets(fmap_t *m, char *dst, size_t *at, size_t max_len) {
unsigned int i, first_page, last_page;
char *src = (void *)((char *)m + m->hdrsz + *at), *endptr = NULL;
size_t len = MIN(max_len-1, m->len - *at), fullen = len;
@ -679,22 +670,6 @@ fmap_t *fmap(int fd, off_t offset, size_t len) {
return fmap_check_empty(fd, offset, len, &unused);
}
int fmap_readn(fmap_t *m, void *dst, size_t at, size_t len) {
char *src;
if(at == m->len)
return 0;
if(at > m->len)
return -1;
if(len > m->len - at)
len = m->len - at;
src = fmap_need_off_once(m, at, len);
if(!src)
return -1;
memcpy(dst, src, len);
return len;
}
static inline unsigned int fmap_align_items(unsigned int sz, unsigned int al) {
return sz / al + (sz % al != 0);
}

@ -26,9 +26,14 @@
#endif
#include <time.h>
#include <string.h>
#include "cltypes.h"
#include "clamav.h"
typedef struct {
struct cl_fmap;
typedef cl_fmap_t fmap_t;
struct cl_fmap {
int _fd;
unsigned short dumb;
unsigned short dont_cache_flag;
@ -39,27 +44,89 @@ typedef struct {
unsigned int hdrsz;
unsigned int pgsz;
unsigned int paged;
/* vtable for implementation */
void (*unmap)(fmap_t*);
const void* (*need)(fmap_t*, size_t at, size_t len, int lock);
const void* (*need_offstr)(fmap_t*, size_t at, size_t len_hint);
const void* (*gets)(fmap_t*, char *dst, size_t *at, size_t max_len);
void (*unneed_off)(fmap_t*, size_t at, size_t len);
#ifdef _WIN32
HANDLE fh;
HANDLE mh;
void *data;
#endif
uint32_t placeholder_for_bitmap;
} fmap_t;
};
fmap_t *fmap(int fd, off_t offset, size_t len);
fmap_t *fmap_check_empty(int fd, off_t offset, size_t len, int *empty);
void funmap(fmap_t *m);
void *fmap_need_off(fmap_t *m, size_t at, size_t len);
void *fmap_need_off_once(fmap_t *m, size_t at, size_t len);
void *fmap_need_ptr(fmap_t *m, void *ptr, size_t len);
void *fmap_need_ptr_once(fmap_t *m, void *ptr, size_t len);
void fmap_unneed_off(fmap_t *m, size_t at, size_t len);
void fmap_unneed_ptr(fmap_t *m, void *ptr, size_t len);
int fmap_readn(fmap_t *m, void *dst, size_t at, size_t len);
void *fmap_need_str(fmap_t *m, void *ptr, size_t len_hint);
void *fmap_need_offstr(fmap_t *m, size_t at, size_t len_hint);
void *fmap_gets(fmap_t *m, char *dst, size_t *at, size_t max_len);
static inline void funmap(fmap_t *m)
{
m->unmap(m);
}
static inline const void *fmap_need_off(fmap_t *m, size_t at, size_t len)
{
return m->need(m, at, len, 1);
}
static inline const void *fmap_need_off_once(fmap_t *m, size_t at, size_t len)
{
return m->need(m, at, len, 0);
}
static inline const void *fmap_need_ptr(fmap_t *m, void *ptr, size_t len)
{
return m->need(m, (char *)ptr - (char *)m - m->hdrsz, len, 1);
}
static inline const void *fmap_need_ptr_once(fmap_t *m, void *ptr, size_t len)
{
return m->need(m, (char *)ptr - (char *)m - m->hdrsz, len, 0);
}
static inline void fmap_unneed_off(fmap_t *m, size_t at, size_t len)
{
m->unneed_off(m, at, len);
}
static inline void fmap_unneed_ptr(fmap_t *m, void *ptr, size_t len)
{
fmap_unneed_off(m, (char *)ptr - (char *)m - m->hdrsz, len);
}
static inline int fmap_readn(fmap_t *m, void *dst, size_t at, size_t len)
{
const void *src;
if(at == m->len)
return 0;
if(at > m->len)
return -1;
if(len > m->len - at)
len = m->len - at;
src = fmap_need_off_once(m, at, len);
if(!src)
return -1;
memcpy(dst, src, len);
return len;
}
static inline const void *fmap_need_str(fmap_t *m, void *ptr, size_t len_hint)
{
return m->need_offstr(m, (char *)ptr - (char *)m - m->hdrsz, len_hint);
}
static inline const void *fmap_need_offstr(fmap_t *m, size_t at, size_t len_hint)
{
return m->need_offstr(m, at, len_hint);
}
static inline const void *fmap_gets(fmap_t *m, char *dst, size_t *at, size_t max_len) {
return m->gets(m, dst, at, max_len);
}
static inline const void *fmap_need_off_once_len(fmap_t *m, size_t at, size_t len, size_t *lenout)
{
@ -71,7 +138,7 @@ static inline const void *fmap_need_off_once_len(fmap_t *m, size_t at, size_t le
if(len > m->len - at)
len = m->len - at;
p = fmap_need_off_once(m, at, len);
*lenout = p ? len : -1;
*lenout = p ? len : 0;
return p;
}
@ -80,6 +147,7 @@ static inline const void *fmap_need_ptr_once_len(fmap_t *m, const void *ptr, siz
return fmap_need_off_once_len(m, (char*)ptr - (char*)m - m->hdrsz, len, lenout);
}
/* deprecated */
int fmap_fd(fmap_t *m);
#endif

Loading…
Cancel
Save