Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

device refactoring (done)

  • Loading branch information...
commit 4862d1ff51cb7e46e66af570045f531fb09a4996 1 parent f3a418c
@cruppstahl authored
Showing with 76 additions and 39 deletions.
  1. +3 −2 TODO
  2. +22 −22 src/device.cc
  3. +51 −12 src/device.h
  4. +0 −3  src/env.cc
View
5 TODO
@@ -65,7 +65,7 @@ o why do tests with 20 threads fail with oom when using mmap? the cache limits
should still work
o look at Peter's mail - sometimes, ham_env_create_db returns
- HAM_INTERNAL_ERROR (only win32, C++ API)
+ HAM_INTERNAL_ERROR (only win32, C++ API, Windows 7)
o more fine-grained locking
x cache
@@ -85,10 +85,11 @@ o more fine-grained locking
x protect with mutex
x device
x make sure it's perfectly abstracted
- o protect with mutex
+ x protect with mutex
o freelist
o remove legacy code (1.x)
o create c++ class
+ o freelist class takes ownership of freelist_cache (from device)
o protect with mutex
o db
o completely hide the local/remote implementation in the database
View
44 src/device.cc
@@ -27,7 +27,7 @@ DeviceImplDisk::alloc_page(Page *page)
{
ham_status_t st;
ham_offset_t pos;
- ham_size_t size=m_device->get_pagesize();
+ ham_size_t size=m_pagesize;
st=os_get_filesize(m_fd, &pos);
if (st)
@@ -55,7 +55,7 @@ DeviceImplDisk::read(ham_offset_t offset, void *buffer, ham_offset_t size)
* we're done unless there are file filters (or if we're reading the
* header page - the header page is not filtered)
*/
- head=m_device->get_env()->get_file_filter();
+ head=m_device->m_env->get_file_filter();
if (!head || offset==0)
return (0);
@@ -64,7 +64,7 @@ DeviceImplDisk::read(ham_offset_t offset, void *buffer, ham_offset_t size)
*/
while (head) {
if (head->after_read_cb) {
- st=head->after_read_cb((ham_env_t *)m_device->get_env(), head,
+ st=head->after_read_cb((ham_env_t *)m_device->m_env, head,
(ham_u8_t *)buffer, (ham_size_t)size);
if (st)
return (st);
@@ -81,9 +81,9 @@ DeviceImplDisk::read_page(Page *page)
ham_u8_t *buffer;
ham_status_t st;
ham_file_filter_t *head=0;
- ham_size_t size=m_device->get_pagesize();
+ ham_size_t size=m_pagesize;
- head=m_device->get_env()->get_file_filter();
+ head=m_device->m_env->get_file_filter();
/*
* first, try to mmap the file (if mmap is available/enabled).
@@ -93,21 +93,21 @@ DeviceImplDisk::read_page(Page *page)
* in such a case, the os_mmap function will return HAM_LIMITS_REACHED
* and we force a fallback to read/write.
*/
- if (!(m_device->get_flags()&HAM_DISABLE_MMAP)) {
+ if (!(m_device->m_flags&HAM_DISABLE_MMAP)) {
st=os_mmap(m_fd, page->get_mmap_handle_ptr(),
page->get_self(), size,
- m_device->get_flags()&HAM_READ_ONLY, &buffer);
+ m_device->m_flags&HAM_READ_ONLY, &buffer);
if (st && st!=HAM_LIMITS_REACHED)
return (st);
if (st==HAM_LIMITS_REACHED) {
- m_device->set_flags(m_device->get_flags()|HAM_DISABLE_MMAP);
+ m_device->m_flags=m_device->m_flags|HAM_DISABLE_MMAP;
goto fallback_rw;
}
}
else {
fallback_rw:
if (page->get_pers()==0) {
- buffer=(ham_u8_t *)m_device->get_env()->get_allocator()->alloc(size);
+ buffer=(ham_u8_t *)m_device->m_env->get_allocator()->alloc(size);
if (!buffer)
return (HAM_OUT_OF_MEMORY);
page->set_pers((page_data_t *)buffer);
@@ -133,7 +133,8 @@ DeviceImplDisk::read_page(Page *page)
/* otherwise run the filters */
while (head) {
if (head->after_read_cb) {
- st=head->after_read_cb((ham_env_t *)m_device->get_env(), head, buffer, size);
+ st=head->after_read_cb((ham_env_t *)m_device->m_env,
+ head, buffer, size);
if (st)
return (st);
}
@@ -147,8 +148,7 @@ DeviceImplDisk::read_page(Page *page)
ham_status_t
DeviceImplDisk::write_page(Page *page)
{
- return (write(page->get_self(), page->get_pers(),
- m_device->get_pagesize()));
+ return (write(page->get_self(), page->get_pers(), m_pagesize));
}
ham_status_t
@@ -162,20 +162,20 @@ DeviceImplDisk::write(ham_offset_t offset, void *buffer, ham_offset_t size)
* run page through page-level filters, but not for the
* root-page!
*/
- head=m_device->get_env()->get_file_filter();
+ head=m_device->m_env->get_file_filter();
if (!head || offset==0)
return (os_pwrite(m_fd, offset, buffer, size));
/* don't modify the data in-place! */
- tempdata=(ham_u8_t *)m_device->get_env()->get_allocator()->alloc((ham_size_t)size);
+ tempdata=(ham_u8_t *)m_device->m_env->get_allocator()->alloc((ham_size_t)size);
if (!tempdata)
return (HAM_OUT_OF_MEMORY);
memcpy(tempdata, buffer, size);
while (head) {
if (head->before_write_cb) {
- st=head->before_write_cb((ham_env_t *)m_device->get_env(), head, tempdata,
- (ham_size_t)size);
+ st=head->before_write_cb((ham_env_t *)m_device->m_env,
+ head, tempdata, (ham_size_t)size);
if (st)
break;
}
@@ -185,7 +185,7 @@ DeviceImplDisk::write(ham_offset_t offset, void *buffer, ham_offset_t size)
if (!st)
st=os_pwrite(m_fd, offset, tempdata, size);
- m_device->get_env()->get_allocator()->free(tempdata);
+ m_device->m_env->get_allocator()->free(tempdata);
return (st);
}
@@ -196,12 +196,12 @@ DeviceImplDisk::free_page(Page *page)
if (page->get_pers()) {
if (page->get_flags()&Page::NPERS_MALLOC) {
- m_device->get_env()->get_allocator()->free(page->get_pers());
+ m_device->m_env->get_allocator()->free(page->get_pers());
page->set_flags(page->get_flags()&~Page::NPERS_MALLOC);
}
else {
st=os_munmap(page->get_mmap_handle_ptr(),
- page->get_pers(), m_device->get_pagesize());
+ page->get_pers(), m_pagesize);
if (st)
return (st);
}
@@ -215,11 +215,11 @@ ham_status_t
DeviceImplInMemory::alloc_page(Page *page)
{
ham_u8_t *buffer;
- ham_size_t size=m_device->get_pagesize();
+ ham_size_t size=m_pagesize;
ham_assert(page->get_pers()==0, (0));
- buffer=(ham_u8_t *)m_device->get_env()->get_allocator()->alloc(size);
+ buffer=(ham_u8_t *)m_device->m_env->get_allocator()->alloc(size);
if (!buffer)
return (HAM_OUT_OF_MEMORY);
page->set_pers((page_data_t *)buffer);
@@ -235,7 +235,7 @@ DeviceImplInMemory::free_page(Page *page)
ham_assert(page->get_pers()!=0, (0));
ham_assert(page->get_flags()|Page::NPERS_MALLOC, (0));
- m_device->get_env()->get_allocator()->free(page->get_pers());
+ m_device->m_env->get_allocator()->free(page->get_pers());
page->set_pers(0);
page->set_flags(page->get_flags()&~Page::NPERS_MALLOC);
View
63 src/device.h
@@ -33,7 +33,7 @@ class DeviceImplementation {
public:
/** constructor */
DeviceImplementation(Device *device)
- : m_device(device) {
+ : m_device(device), m_pagesize(0) {
}
/** Create a new device */
@@ -97,9 +97,22 @@ class DeviceImplementation {
/** frees a page on the device; plays counterpoint to @ref alloc_page */
virtual ham_status_t free_page(Page *page) = 0;
+ /** sets the pagesize */
+ void set_pagesize(ham_size_t ps) {
+ m_pagesize=ps;
+ }
+
+ /** gets the pagesize */
+ ham_size_t get_pagesize() {
+ return (m_pagesize);
+ }
+
protected:
/** the Device object which created this DeviceImplementation */
Device *m_device;
+
+ /** the pagesize */
+ ham_size_t m_pagesize;
};
@@ -342,69 +355,83 @@ class Device {
/** virtual destructor */
~Device() {
+ ScopedLock lock(m_mutex);
delete m_impl;
+ m_impl=0;
}
/** set the flags */
void set_flags(ham_u32_t flags) {
+ ScopedLock lock(m_mutex);
m_flags=flags;
}
/** get the flags */
ham_u32_t get_flags() {
+ ScopedLock lock(m_mutex);
return (m_flags);
}
/** Create a new device */
ham_status_t create(const char *filename, ham_u32_t flags,
ham_u32_t mode) {
+ ScopedLock lock(m_mutex);
m_flags=flags;
return (m_impl->create(filename, flags, mode));
}
/** opens an existing device */
ham_status_t open(const char *filename, ham_u32_t flags) {
+ ScopedLock lock(m_mutex);
m_flags=flags;
return (m_impl->open(filename, flags));
}
/** closes the device */
ham_status_t close() {
+ ScopedLock lock(m_mutex);
return (m_impl->close());
}
/** flushes the device */
ham_status_t flush() {
+ ScopedLock lock(m_mutex);
return (m_impl->flush());
}
/** truncate/resize the device */
ham_status_t truncate(ham_offset_t newsize) {
+ ScopedLock lock(m_mutex);
return (m_impl->truncate(newsize));
}
/** returns true if the device is open */
bool is_open() {
+ ScopedLock lock(m_mutex);
return (m_impl->is_open());
}
/** get the current file/storage size */
ham_status_t get_filesize(ham_offset_t *length) {
+ ScopedLock lock(m_mutex);
return (m_impl->get_filesize(length));
}
/** seek position in a file */
ham_status_t seek(ham_offset_t offset, int whence) {
+ ScopedLock lock(m_mutex);
return (m_impl->seek(offset, whence));
}
/** tell the position in a file */
ham_status_t tell(ham_offset_t *offset) {
+ ScopedLock lock(m_mutex);
return (m_impl->tell(offset));
}
/** reads from the device; this function does not use mmap */
ham_status_t read(ham_offset_t offset, void *buffer, ham_offset_t size) {
+ ScopedLock lock(m_mutex);
return (m_impl->read(offset, buffer, size));
}
@@ -412,22 +439,26 @@ class Device {
* and is responsible for writing the data is run through the file
* filters */
ham_status_t write(ham_offset_t offset, void *buffer, ham_offset_t size) {
+ ScopedLock lock(m_mutex);
return (m_impl->write(offset, buffer, size));
}
/** reads a page from the device; this function CAN use mmap */
ham_status_t read_page(Page *page) {
+ ScopedLock lock(m_mutex);
return (m_impl->read_page(page));
}
/** writes a page to the device */
ham_status_t write_page(Page *page) {
+ ScopedLock lock(m_mutex);
return (m_impl->write_page(page));
}
/** allocate storage from this device; this function
* will *NOT* use mmap. */
ham_status_t alloc(ham_size_t size, ham_offset_t *address) {
+ ScopedLock lock(m_mutex);
return (m_impl->alloc(size, address));
}
@@ -440,44 +471,55 @@ class Device {
* function will assert that the page is not dirty.
*/
ham_status_t alloc_page(Page *page) {
+ ScopedLock lock(m_mutex);
return (m_impl->alloc_page(page));
}
/** frees a page on the device; plays counterpoint to @ref alloc_page */
ham_status_t free_page(Page *page) {
+ ScopedLock lock(m_mutex);
return (m_impl->free_page(page));
}
/** get the Environment */
Environment *get_env() {
+ ScopedLock lock(m_mutex);
return (m_env);
}
- /** get the pagesize for this device */
- /** TODO can this be private? */
- ham_size_t get_pagesize() {
- return (m_pagesize);
- }
-
/** set the pagesize for this device */
- /** TODO can this be private? */
void set_pagesize(ham_size_t pagesize) {
- m_pagesize=pagesize;
+ ScopedLock lock(m_mutex);
+ m_impl->set_pagesize(pagesize);
+ }
+
+ /** get the pagesize for this device */
+ ham_size_t get_pagesize() {
+ ScopedLock lock(m_mutex);
+ return (m_impl->get_pagesize());
}
/** set the freelist cache */
/** TODO should this move to the Env? */
void set_freelist_cache(freelist_cache_t *cache) {
+ ScopedLock lock(m_mutex);
m_freelist_cache=cache;
}
/** get the freelist cache */
/** TODO should this move to the Env? */
freelist_cache_t *get_freelist_cache() {
+ ScopedLock lock(m_mutex);
return (m_freelist_cache);
}
protected:
+ friend class DeviceImplDisk;
+ friend class DeviceImplInMemory;
+
+ /** a mutex to protect the device */
+ Mutex m_mutex;
+
/** The actual implementation */
DeviceImplementation *m_impl;
@@ -487,9 +529,6 @@ class Device {
/** the device flags */
ham_u32_t m_flags;
- /** the pagesize */
- ham_size_t m_pagesize;
-
/** the freelist cache is managed by the device */
freelist_cache_t *m_freelist_cache;
};
View
3  src/env.cc
@@ -182,11 +182,8 @@ _local_fun_create(Environment *env, const char *filename,
}
else {
device=env->get_device();
- ham_assert(device->get_pagesize(), (0));
- ham_assert(env->get_pagesize() == device->get_pagesize(), (0));
}
ham_assert(device == env->get_device(), (0));
- ham_assert(env->get_pagesize() == device->get_pagesize(), (""));
/* create the file */
st=device->create(filename, flags, mode);
Please sign in to comment.
Something went wrong with that request. Please try again.