From 9bcfe627321ddc81691b835dffaa6324ac4684a4 Mon Sep 17 00:00:00 2001 From: lloyd Date: Sun, 28 Sep 2008 19:29:24 +0000 Subject: Move all modules into src/ directory --- src/allocation/alloc_mmap/mmap_mem.cpp | 128 +++++++++++++++++++++++++++++++++ src/allocation/alloc_mmap/mmap_mem.h | 27 +++++++ src/allocation/alloc_mmap/modinfo.txt | 25 +++++++ src/allocation/ml_unix/mlock.cpp | 33 +++++++++ src/allocation/ml_unix/modinfo.txt | 25 +++++++ src/allocation/ml_win32/mlock.cpp | 27 +++++++ src/allocation/ml_win32/modinfo.txt | 12 ++++ 7 files changed, 277 insertions(+) create mode 100644 src/allocation/alloc_mmap/mmap_mem.cpp create mode 100644 src/allocation/alloc_mmap/mmap_mem.h create mode 100644 src/allocation/alloc_mmap/modinfo.txt create mode 100644 src/allocation/ml_unix/mlock.cpp create mode 100644 src/allocation/ml_unix/modinfo.txt create mode 100644 src/allocation/ml_win32/mlock.cpp create mode 100644 src/allocation/ml_win32/modinfo.txt (limited to 'src/allocation') diff --git a/src/allocation/alloc_mmap/mmap_mem.cpp b/src/allocation/alloc_mmap/mmap_mem.cpp new file mode 100644 index 000000000..e9ea17a53 --- /dev/null +++ b/src/allocation/alloc_mmap/mmap_mem.cpp @@ -0,0 +1,128 @@ +/************************************************* +* Memory Mapping Allocator Source File * +* (C) 1999-2007 Jack Lloyd * +*************************************************/ + +#include +#include + +#ifndef _XOPEN_SOURCE + #define _XOPEN_SOURCE 500 +#endif + +#ifndef _XOPEN_SOURCE_EXTENDED + #define _XOPEN_SOURCE_EXTENDED 1 +#endif + +#include +#include +#include +#include +#include +#include + +#ifndef MAP_FAILED + #define MAP_FAILED -1 +#endif + +namespace Botan { + +namespace { + +/************************************************* +* MemoryMapping_Allocator Exception * +*************************************************/ +class MemoryMapping_Failed : public Exception + { + public: + MemoryMapping_Failed(const std::string& msg) : + Exception("MemoryMapping_Allocator: " + msg) {} + }; + +} + +/************************************************* +* Memory Map a File into Memory * +*************************************************/ +void* MemoryMapping_Allocator::alloc_block(u32bit n) + { + class TemporaryFile + { + public: + int get_fd() const { return fd; } + const std::string path() const { return filepath; } + + TemporaryFile(const std::string& base) + { + const std::string path = base + "XXXXXX"; + + filepath = new char[path.length() + 1]; + std::strcpy(filepath, path.c_str()); + + mode_t old_umask = ::umask(077); + fd = ::mkstemp(filepath); + ::umask(old_umask); + } + + ~TemporaryFile() + { + delete[] filepath; + if(fd != -1 && ::close(fd) == -1) + throw MemoryMapping_Failed("Could not close file"); + } + private: + int fd; + char* filepath; + }; + + TemporaryFile file("/tmp/botan_"); + + if(file.get_fd() == -1) + throw MemoryMapping_Failed("Could not create file"); + + if(::unlink(file.path().c_str())) + throw MemoryMapping_Failed("Could not unlink file '" + file.path() + "'"); + + ::lseek(file.get_fd(), n-1, SEEK_SET); + if(::write(file.get_fd(), "\0", 1) != 1) + throw MemoryMapping_Failed("Could not write to file"); + +#ifndef MAP_NOSYNC + #define MAP_NOSYNC 0 +#endif + + void* ptr = ::mmap(0, n, + PROT_READ | PROT_WRITE, + MAP_SHARED | MAP_NOSYNC, + file.get_fd(), 0); + + if(ptr == static_cast(MAP_FAILED)) + throw MemoryMapping_Failed("Could not map file"); + + return ptr; + } + +/************************************************* +* Remove a Memory Mapping * +*************************************************/ +void MemoryMapping_Allocator::dealloc_block(void* ptr, u32bit n) + { + if(ptr == 0) + return; + + const byte PATTERNS[] = { 0x00, 0xFF, 0xAA, 0x55, 0x73, 0x8C, 0x5F, 0xA0, + 0x6E, 0x91, 0x30, 0xCF, 0xD3, 0x2C, 0xAC, 0x00 }; + + for(u32bit j = 0; j != sizeof(PATTERNS); j++) + { + std::memset(ptr, PATTERNS[j], n); + + if(::msync(ptr, n, MS_SYNC)) + throw MemoryMapping_Failed("Sync operation failed"); + } + + if(::munmap(ptr, n)) + throw MemoryMapping_Failed("Could not unmap file"); + } + +} diff --git a/src/allocation/alloc_mmap/mmap_mem.h b/src/allocation/alloc_mmap/mmap_mem.h new file mode 100644 index 000000000..a6f68d039 --- /dev/null +++ b/src/allocation/alloc_mmap/mmap_mem.h @@ -0,0 +1,27 @@ +/************************************************* +* Memory Mapping Allocator Header File * +* (C) 1999-2007 Jack Lloyd * +*************************************************/ + +#ifndef BOTAN_MMAP_ALLOCATOR_H__ +#define BOTAN_MMAP_ALLOCATOR_H__ + +#include + +namespace Botan { + +/************************************************* +* Memory Mapping Allocator * +*************************************************/ +class MemoryMapping_Allocator : public Pooling_Allocator + { + public: + std::string type() const { return "mmap"; } + private: + void* alloc_block(u32bit); + void dealloc_block(void*, u32bit); + }; + +} + +#endif diff --git a/src/allocation/alloc_mmap/modinfo.txt b/src/allocation/alloc_mmap/modinfo.txt new file mode 100644 index 000000000..8cc2b206e --- /dev/null +++ b/src/allocation/alloc_mmap/modinfo.txt @@ -0,0 +1,25 @@ +realname "Disk Based Allocation System" + +define ALLOC_MMAP +modset unix + +load_on auto + + +mmap_mem.cpp +mmap_mem.h + + + +linux +freebsd +openbsd +netbsd +solaris +qnx +darwin +tru64 + +# Only without -ansi, otherwise can't get mkstemp +#cygwin + diff --git a/src/allocation/ml_unix/mlock.cpp b/src/allocation/ml_unix/mlock.cpp new file mode 100644 index 000000000..98214a215 --- /dev/null +++ b/src/allocation/ml_unix/mlock.cpp @@ -0,0 +1,33 @@ +/************************************************* +* Memory Locking Functions Source File * +* (C) 1999-2007 Jack Lloyd * +*************************************************/ + +#include + +#ifndef _POSIX_C_SOURCE + #define _POSIX_C_SOURCE 199309 +#endif + +#include +#include + +namespace Botan { + +/************************************************* +* Lock an area of memory into RAM * +*************************************************/ +void lock_mem(void* ptr, u32bit bytes) + { + mlock(ptr, bytes); + } + +/************************************************* +* Unlock a previously locked region of memory * +*************************************************/ +void unlock_mem(void* ptr, u32bit bytes) + { + munlock(ptr, bytes); + } + +} diff --git a/src/allocation/ml_unix/modinfo.txt b/src/allocation/ml_unix/modinfo.txt new file mode 100644 index 000000000..201a30ead --- /dev/null +++ b/src/allocation/ml_unix/modinfo.txt @@ -0,0 +1,25 @@ +realname "Memory Locking for Unix" + +load_on auto + + +mlock.cpp + + + +aix +darwin +freebsd +hpux +irix +linux +netbsd +openbsd +qnx +solaris +tru64 + + + +tru64 -> rt + diff --git a/src/allocation/ml_win32/mlock.cpp b/src/allocation/ml_win32/mlock.cpp new file mode 100644 index 000000000..8c5919934 --- /dev/null +++ b/src/allocation/ml_win32/mlock.cpp @@ -0,0 +1,27 @@ +/************************************************* +* Memory Locking Functions Source File * +* (C) 1999-2007 Jack Lloyd * +*************************************************/ + +#include +#include + +namespace Botan { + +/************************************************* +* Lock an area of memory into RAM * +*************************************************/ +void lock_mem(void* ptr, u32bit bytes) + { + VirtualLock(ptr, bytes); + } + +/************************************************* +* Unlock a previously locked region of memory * +*************************************************/ +void unlock_mem(void* ptr, u32bit bytes) + { + VirtualUnlock(ptr, bytes); + } + +} diff --git a/src/allocation/ml_win32/modinfo.txt b/src/allocation/ml_win32/modinfo.txt new file mode 100644 index 000000000..92936e1de --- /dev/null +++ b/src/allocation/ml_win32/modinfo.txt @@ -0,0 +1,12 @@ +realname "Memory Locking for Win32" + +load_on auto + + +mlock.cpp + + + +windows +cygwin + -- cgit v1.2.3