diff options
author | Jakob Schlyter <jakob@cvs.openbsd.org> | 2010-04-15 21:14:57 +0000 |
---|---|---|
committer | Jakob Schlyter <jakob@cvs.openbsd.org> | 2010-04-15 21:14:57 +0000 |
commit | 61384037fa6e42ca393d6c5d5409850fa501e129 (patch) | |
tree | 5318c995cc7b7d66e1b10324345e7023493690fa /usr.sbin/nsd/util.c | |
parent | 12e84b047121c2691fd674a590c1695108dce5ed (diff) |
resolve conflicts from import of v3.2.5
Diffstat (limited to 'usr.sbin/nsd/util.c')
-rw-r--r-- | usr.sbin/nsd/util.c | 73 |
1 files changed, 63 insertions, 10 deletions
diff --git a/usr.sbin/nsd/util.c b/usr.sbin/nsd/util.c index 2a50c15a5c4..0dc81fa9b12 100644 --- a/usr.sbin/nsd/util.c +++ b/usr.sbin/nsd/util.c @@ -27,6 +27,17 @@ #include "namedb.h" #include "rdata.h" +#ifdef USE_MMAP_ALLOC +#include <sys/mman.h> + +#if defined(MAP_ANON) && !defined(MAP_ANONYMOUS) +#define MAP_ANONYMOUS MAP_ANON +#elif defined(MAP_ANONYMOUS) && !defined(MAP_ANON) +#define MAP_ANON MAP_ANONYMOUS +#endif + +#endif /* USE_MMAP_ALLOC */ + #ifndef NDEBUG unsigned nsd_debug_facilities = 0xffff; int nsd_debug_level = 0; @@ -242,6 +253,44 @@ xrealloc(void *ptr, size_t size) return ptr; } +#ifdef USE_MMAP_ALLOC + +void * +mmap_alloc(size_t size) +{ + void *base; + + size += MMAP_ALLOC_HEADER_SIZE; + base = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + if (base == MAP_FAILED) { + log_msg(LOG_ERR, "mmap failed: %s", strerror(errno)); + exit(1); + } + + *((size_t*) base) = size; + return (void*)((uintptr_t)base + MMAP_ALLOC_HEADER_SIZE); +} + + +void +mmap_free(void *ptr) +{ + void *base; + size_t size; + + if (!ptr) return; + + base = (void*)((uintptr_t)ptr - MMAP_ALLOC_HEADER_SIZE); + size = *((size_t*) base); + + if (munmap(base, size) == -1) { + log_msg(LOG_ERR, "munmap failed: %s", strerror(errno)); + exit(1); + } +} + +#endif /* USE_MMAP_ALLOC */ + int write_data(FILE *file, const void *data, size_t size) { @@ -263,7 +312,8 @@ write_data(FILE *file, const void *data, size_t size) } } -int write_socket(int s, const void *buf, size_t size) +int +write_socket(int s, const void *buf, size_t size) { const char* data = (const char*)buf; size_t total_count = 0; @@ -739,7 +789,8 @@ static u_long crctab[] = { #define COMPUTE(var, ch) (var) = (var) << 8 ^ crctab[(var) >> 24 ^ (ch)] -uint32_t compute_crc(uint32_t crc, uint8_t* data, size_t len) +uint32_t +compute_crc(uint32_t crc, uint8_t* data, size_t len) { size_t i; for(i=0; i<len; ++i) @@ -747,7 +798,8 @@ uint32_t compute_crc(uint32_t crc, uint8_t* data, size_t len) return crc; } -int write_data_crc(FILE *file, const void *data, size_t size, uint32_t* crc) +int +write_data_crc(FILE *file, const void *data, size_t size, uint32_t* crc) { int ret = write_data(file, data, size); *crc = compute_crc(*crc, (uint8_t*)data, size); @@ -755,7 +807,8 @@ int write_data_crc(FILE *file, const void *data, size_t size, uint32_t* crc) } #define SERIAL_BITS 32 -int compare_serial(uint32_t a, uint32_t b) +int +compare_serial(uint32_t a, uint32_t b) { const uint32_t cutoff = ((uint32_t) 1 << (SERIAL_BITS - 1)); @@ -768,6 +821,12 @@ int compare_serial(uint32_t a, uint32_t b) } } +uint16_t +qid_generate(void) +{ + return (uint16_t) arc4random(); +} + void cleanup_region(void *data) { @@ -958,9 +1017,3 @@ addr2ip( return (0); } - -uint16_t -qid_generate() -{ - return (uint16_t) arc4random(); -} |