rambrain
managedDummySwap.cpp
Go to the documentation of this file.
1 /* rambrain - a dynamical physical memory extender
2  * Copyright (C) 2015 M. Imgrund, A. Arth
3  * mimgrund (at) mpifr-bonn.mpg.de
4  * arth (at) usm.uni-muenchen.de
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include <malloc.h>
21 #include <memory.h>
22 #include "managedDummySwap.h"
23 #include "managedMemory.h"
24 #include <mm_malloc.h>
25 
26 namespace rambrain
27 {
28 
30 {
31  swapFree = swapSize = size;
32  swapUsed = 0;
33 }
34 
36 {
37  if ( chunk->size + swapUsed > swapSize ) {
38  return 0;
39  }
40  void *buf = _mm_malloc ( chunk->size, memoryAlignment );
41  if ( buf ) {
42  chunk->swapBuf = buf;
43  memcpy ( chunk->swapBuf, chunk->locPtr, chunk->size );
44  _mm_free ( chunk->locPtr );
45  chunk->locPtr = NULL; // not strictly required.
46  chunk->status = MEM_SWAPPED;
47  claimUsageof ( chunk->size, false, true );
48  claimUsageof ( chunk->size, true, false );
49 #ifdef SWAPSTATS
51 #endif
54 
55  return chunk->size;
56  } else {
58  //managedMemory::signalSwappingCond();
59  return 0;
60  }
61 
62 
63 }
64 
65 global_bytesize managedDummySwap::swapOut ( managedMemoryChunk **chunklist, unsigned int nchunks )
66 {
67  global_bytesize n_swapped = 0;
68  for ( unsigned int n = 0; n < nchunks; ++n ) {
69  n_swapped += swapOut ( chunklist[n] );
70  }
71  return n_swapped;
72 }
73 
74 
76 {
77  void *buf = _mm_malloc ( chunk->size, memoryAlignment );
78  if ( buf ) {
79  chunk->locPtr = buf;
80  memcpy ( chunk->locPtr, chunk->swapBuf, chunk->size );
81  _mm_free ( chunk->swapBuf );
82  chunk->swapBuf = NULL; // Not strictly required
83  chunk->status = MEM_ALLOCATED;
84  claimUsageof ( chunk->size, false, false );
85  claimUsageof ( chunk->size, true, true );
86 #ifdef SWAPSTATS
88 #endif
91  return chunk->size;
92  } else {
95  return 0;
96  }
97 
98 }
99 
100 
101 global_bytesize managedDummySwap::swapIn ( managedMemoryChunk **chunklist, unsigned int nchunks )
102 {
103  global_bytesize n_swapped = 0;
104  for ( unsigned int n = 0; n < nchunks; ++n ) {
105  n_swapped += swapIn ( chunklist[n] );
106  }
107  return n_swapped;
108 }
109 
110 
112 {
113  if ( chunk->status == MEM_SWAPPED ) {
114  claimUsageof ( chunk->size, false, false );
115  _mm_free ( chunk->swapBuf );
116  }
117 }
118 
119 }
120 
static void signalSwappingCond()
signals that a swapping action has completed and memory limits have changed
void * locPtr
pointer to the actual data in RAM
managedDummySwap(rambrain::global_bytesize size)
void * swapBuf
a place to store additional swapping information
Class that serves as a backend to managedMemory to actual write/read managedMemoryChunks to/from hard...
Definition: managedSwap.h:35
virtual global_bytesize swapOut(managedMemoryChunk **chunklist, unsigned int nchunks)
Trigger swap out of the chunks pointed to by chunklist.
virtual void swapDelete(managedMemoryChunk *chunk)
Mark chunk as deleted.
static managedMemory * defaultManager
void claimUsageof(global_bytesize bytes, bool rambytes, bool used)
account for memory usage change
Definition: managedSwap.cpp:35
global_bytesize swapUsed
Definition: managedSwap.h:147
uint64_t global_bytesize
Definition: common.h:65
manages all managed Chunks of raw memory
global_bytesize swapFree
Definition: managedSwap.h:148
global_bytesize swap_in_bytes
global_bytesize size
Size of actual object in bytes.
global_bytesize swap_out_bytes
global_bytesize swapSize
Definition: managedSwap.h:146
virtual global_bytesize swapIn(managedMemoryChunk **chunklist, unsigned int nchunks)
Trigger swap in of the chunks pointed to by chunklist.