Thrill  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
memory_file.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * foxxll/io/memory_file.cpp
3  *
4  * Part of FOXXLL. See http://foxxll.org
5  *
6  * Copyright (C) 2008 Andreas Beckmann <[email protected]>
7  * Copyright (C) 2013-2014 Timo Bingmann <[email protected]>
8  *
9  * Distributed under the Boost Software License, Version 1.0.
10  * (See accompanying file LICENSE_1_0.txt or copy at
11  * http://www.boost.org/LICENSE_1_0.txt)
12  **************************************************************************/
13 
14 #include <cassert>
15 #include <cstring>
16 #include <limits>
17 
18 #include <tlx/logger.hpp>
19 #include <tlx/unused.hpp>
20 
21 #include <foxxll/io/iostats.hpp>
23 
24 namespace foxxll {
25 
26 void memory_file::serve(void* buffer, offset_type offset, size_type bytes,
28 {
29  std::unique_lock<std::mutex> lock(mutex_);
30 
31  if (op == request::READ)
32  {
33  file_stats::scoped_read_timer read_timer(file_stats_, bytes);
34  memcpy(buffer, ptr_ + offset, bytes);
35  }
36  else
37  {
38  file_stats::scoped_write_timer write_timer(file_stats_, bytes);
39  memcpy(ptr_ + offset, buffer, bytes);
40  }
41 }
42 
43 const char* memory_file::io_type() const
44 {
45  return "memory";
46 }
47 
49 {
50  free(ptr_);
51  ptr_ = nullptr;
52 }
53 
55 {
56  // nothing to do
57 }
58 
60 {
61  return size_;
62 }
63 
65 {
66  std::unique_lock<std::mutex> lock(mutex_);
67  assert(newsize <= std::numeric_limits<size_t>::max());
68 
69  ptr_ = static_cast<char*>(realloc(ptr_, static_cast<size_t>(newsize)));
70  size_ = newsize;
71 }
72 
74 {
75  std::unique_lock<std::mutex> lock(mutex_);
76 #ifndef FOXXLL_MEMFILE_DONT_CLEAR_FREED_MEMORY
77  // overwrite the freed region with uninitialized memory
78  LOG1 << "discard at " << offset << " len " << size;
79  void* uninitialized = malloc(BlockAlignment);
80  while (size >= BlockAlignment) {
81  memcpy(ptr_ + offset, uninitialized, BlockAlignment);
82  offset += BlockAlignment;
83  size -= BlockAlignment;
84  }
85  assert(size <= std::numeric_limits<offset_type>::max());
86  if (size > 0)
87  memcpy(ptr_ + offset, uninitialized, static_cast<size_t>(size));
88  free(uninitialized);
89 #else
90  tlx::unused(offset);
91  tlx::unused(size);
92 #endif
93 }
94 
95 } // namespace foxxll
96 
97 /******************************************************************************/
98 
99 /**************************************************************************/
void set_size(offset_type newsize) final
Definition: memory_file.cpp:64
static uint_pair max()
return an uint_pair instance containing the largest value possible
Definition: uint_types.hpp:226
const char * io_type() const final
Definition: memory_file.cpp:43
void serve(void *buffer, offset_type offset, size_type bytes, request::read_or_write op) final
Definition: memory_file.cpp:26
#define LOG1
Definition: logger.hpp:145
request::offset_type offset_type
the offset of a request, also the size of the file
Definition: file.hpp:58
offset_type size() final
Definition: memory_file.cpp:59
file_stats * file_stats_
Definition: file.hpp:200
constexpr size_t BlockAlignment
Definition: request.hpp:34
void unused(Types &&...)
Definition: unused.hpp:20
request::size_type size_type
the size of a request
Definition: file.hpp:60
offset_type size_
size of memory area
Definition: memory_file.hpp:35
void * malloc(size_t size) NOEXCEPT
exported malloc symbol that overrides loading from libc
static const size_t bytes
number of bytes in uint_pair
Definition: uint_types.hpp:75
void lock() final
Locks file for reading and writing (acquires a lock in the file system).
Definition: memory_file.cpp:54
std::mutex mutex_
sequentialize function calls
Definition: memory_file.hpp:38
void free(void *ptr) NOEXCEPT
exported free symbol that overrides loading from libc
void discard(offset_type offset, offset_type size) final
Definition: memory_file.cpp:73
char * ptr_
pointer to memory area of "file"
Definition: memory_file.hpp:32
void * realloc(void *ptr, size_t size) NOEXCEPT
exported realloc() symbol that overrides loading from libc