STXXL  1.4-dev
 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-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 <cstring>
15 #include <limits>
16 #include <cassert>
17 
18 #include <stxxl/bits/io/mem_file.h>
19 #include <stxxl/bits/io/iostats.h>
20 
22 
23 void mem_file::serve(void* buffer, offset_type offset, size_type bytes,
25 {
26  scoped_mutex_lock lock(m_mutex);
27 
28  if (type == request::READ)
29  {
30  stats::scoped_read_timer read_timer(bytes);
31  memcpy(buffer, m_ptr + offset, bytes);
32  }
33  else
34  {
35  stats::scoped_write_timer write_timer(bytes);
36  memcpy(m_ptr + offset, buffer, bytes);
37  }
38 }
39 
40 const char* mem_file::io_type() const
41 {
42  return "memory";
43 }
44 
45 mem_file::~mem_file()
46 {
47  free(m_ptr);
48  m_ptr = NULL;
49 }
50 
51 void mem_file::lock()
52 {
53  // nothing to do
54 }
55 
56 file::offset_type mem_file::size()
57 {
58  return m_size;
59 }
60 
61 void mem_file::set_size(offset_type newsize)
62 {
63  scoped_mutex_lock lock(m_mutex);
64  assert(newsize <= std::numeric_limits<offset_type>::max());
65 
66  m_ptr = (char*)realloc(m_ptr, (size_t)newsize);
67  m_size = newsize;
68 }
69 
70 void mem_file::discard(offset_type offset, offset_type size)
71 {
72  scoped_mutex_lock lock(m_mutex);
73 #ifndef STXXL_MEMFILE_DONT_CLEAR_FREED_MEMORY
74  // overwrite the freed region with uninitialized memory
75  STXXL_VERBOSE("discard at " << offset << " len " << size);
76  void* uninitialized = malloc(STXXL_BLOCK_ALIGN);
77  while (size >= STXXL_BLOCK_ALIGN) {
78  memcpy(m_ptr + offset, uninitialized, STXXL_BLOCK_ALIGN);
79  offset += STXXL_BLOCK_ALIGN;
80  size -= STXXL_BLOCK_ALIGN;
81  }
82  assert(size <= std::numeric_limits<offset_type>::max());
83  if (size > 0)
84  memcpy(m_ptr + offset, uninitialized, (size_t)size);
85  free(uninitialized);
86 #else
87  STXXL_UNUSED(offset);
88  STXXL_UNUSED(size);
89 #endif
90 }
91 
#define STXXL_VERBOSE(x)
Definition: verbose.h:116
void * malloc(size_t size)
void * realloc(void *ptr, size_t size)
#define STXXL_BLOCK_ALIGN
Definition: request.h:33
request::size_type size_type
the size of a request
Definition: file.h:62
Aquire a lock that&#39;s valid until the end of scope.
Definition: mutex.h:123
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
void STXXL_UNUSED(const U &)
Definition: unused.h:22
static uint_pair max()
return an uint_pair instance containing the largest value possible
Definition: uint_types.h:241
void free(void *ptr)
static const size_t bytes
number of bytes in uint_pair
Definition: uint_types.h:96
request::offset_type offset_type
the offset of a request, also the size of the file
Definition: file.h:60
void discard(StreamAlgorithm &in)
Reads stream content and discards it. Useful where you do not need the processed stream anymore...
Definition: stream.h:640
#define STXXL_END_NAMESPACE
Definition: namespace.h:17