STXXL  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
mem_file.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * lib/io/mem_file.cpp
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 2008 Andreas Beckmann <[email protected]>
7  * Copyright (C) 2013 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 <cstring>
15 #include <limits>
16 #include <cassert>
17 
18 #include <stxxl/bits/io/mem_file.h>
19 #include <stxxl/bits/io/iostats.h>
20 
21 
23 
24 
25 void mem_file::serve(const request* req) throw (io_error)
26 {
27  scoped_mutex_lock lock(m_mutex);
28 
29  assert(req->get_file() == this);
30  offset_type offset = req->get_offset();
31  void* buffer = req->get_buffer();
32  size_type bytes = req->get_size();
33  request::request_type type = req->get_type();
34 
35  if (type == request::READ)
36  {
37  stats::scoped_read_timer read_timer(bytes);
38  memcpy(buffer, ptr + offset, bytes);
39  }
40  else
41  {
42  stats::scoped_write_timer write_timer(bytes);
43  memcpy(ptr + offset, buffer, bytes);
44  }
45 }
46 
47 const char* mem_file::io_type() const
48 {
49  return "memory";
50 }
51 
52 mem_file::~mem_file()
53 {
54  free(ptr);
55  ptr = NULL;
56 }
57 
58 void mem_file::lock()
59 {
60  // nothing to do
61 }
62 
63 file::offset_type mem_file::size()
64 {
65  return sz;
66 }
67 
68 void mem_file::set_size(offset_type newsize)
69 {
70  scoped_mutex_lock lock(m_mutex);
71  assert(newsize <= std::numeric_limits<offset_type>::max());
72 
73  ptr = (char*)realloc(ptr, (size_t)newsize);
74  sz = newsize;
75 }
76 
77 void mem_file::discard(offset_type offset, offset_type size)
78 {
79  scoped_mutex_lock lock(m_mutex);
80 #ifndef STXXL_MEMFILE_DONT_CLEAR_FREED_MEMORY
81  // overwrite the freed region with uninitialized memory
82  STXXL_VERBOSE("discard at " << offset << " len " << size);
83  void* uninitialized = malloc(BLOCK_ALIGN);
84  while (size >= BLOCK_ALIGN) {
85  memcpy(ptr + offset, uninitialized, BLOCK_ALIGN);
86  offset += BLOCK_ALIGN;
87  size -= BLOCK_ALIGN;
88  }
89  assert(size <= std::numeric_limits<offset_type>::max());
90  if (size > 0)
91  memcpy(ptr + offset, uninitialized, (size_t)size);
92  free(uninitialized);
93 #else
94  STXXL_UNUSED(offset);
95  STXXL_UNUSED(size);
96 #endif
97 }
98 
#define STXXL_VERBOSE(x)
Definition: verbose.h:102
static const int bytes
number of bytes in uint_pair
Definition: uint_types.h:99
request::size_type size_type
the size of a request
Definition: file.h:66
#define BLOCK_ALIGN
Definition: request.h:34
Aquire a lock that&#39;s valid until the end of scope.
Definition: mutex.h:106
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
void STXXL_UNUSED(const U &)
Definition: unused.h:23
static uint_pair max()
return an uint_pair instance containing the largest value possible
Definition: uint_types.h:244
void discard(StreamAlgorithm_ &in)
Reads stream content and discards it. Useful where you do not need the processed stream anymore...
Definition: stream.h:606
request::offset_type offset_type
the offset of a request, also the size of the file
Definition: file.h:64
Request with basic properties like file and offset.
Definition: request.h:39
#define STXXL_END_NAMESPACE
Definition: namespace.h:17