File: CDebuggingMemoryManager.cc

package info (click to toggle)
gnuift 0.1.14%2Bds-1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 5,632 kB
  • ctags: 2,973
  • sloc: cpp: 15,867; sh: 8,281; ansic: 1,812; perl: 1,007; php: 651; makefile: 483; lisp: 344
file content (344 lines) | stat: -rw-r--r-- 7,953 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#include <iostream>
#include "libMRML/include/CDebuggingMemoryManager.h"
#include <cassert>
#include <cstdlib>


CDebuggingMemoryManager::CDebuggingMemoryManager(const CDebuggingMemoryManagerSize inSize):
  mBuffer(new lTChunk[inSize / sizeof(lTChunk) + 2]),
  mFreeList(mBuffer),
  mUsedList(mBuffer+1),
  cMagic(0x1),
  cUnMagic(0x2)
{
  cVM=0x88414004;

  if(mBuffer)
    {
      mFreeList=mBuffer;
      mUsedList=mBuffer + 1;
      mFreeList->mPrev=0;
      mFreeList->mNext=mBuffer+2;
      mFreeList->mPreceding=0;
      mFreeList->mFollowing=mBuffer+1;
      mFreeList->mSize=0;
      mFreeList->mMagic=cUnMagic;

      mUsedList->mPrev=0;
      mUsedList->mNext=0;
      mUsedList->mPreceding=mBuffer;
      mUsedList->mFollowing=mBuffer+2;
      mUsedList->mSize=0;
      mUsedList->mMagic=cMagic;

      lTChunk* lFreeChunk=mBuffer+2;
      lTChunk* lLastNode=mBuffer+(inSize / sizeof(lTChunk));

      lFreeChunk->mPrev=mBuffer;
      lFreeChunk->mNext=0;
      lFreeChunk->mPreceding=mBuffer+1;
      lFreeChunk->mSize=inSize - sizeof(lTChunk)*3;
      lFreeChunk->mFollowing=lLastNode;
      lFreeChunk->mMagic=cUnMagic;

      ///A last node, which marks the end for the Chunklist
      ///The other lists need no end node
      lLastNode->mPrev=0;
      lLastNode->mNext=0;
      lLastNode->mPreceding=mBuffer+2;
      lLastNode->mSize=0;
      lLastNode->mFollowing=lLastNode;
      lLastNode->mMagic=cMagic;

    }
};


void* CDebuggingMemoryManager::getMem( CDebuggingMemoryManagerSize inSize){
  mMutex.lock();
  //inSize+=8;

  //Look for first free element of the right size;
  lTChunk* lCurrent= mFreeList;

  //For sake of cleanliness.
  //The number of Records for the strucure and the memblock
  CDebuggingMemoryManagerSize lNumRecords= (inSize + sizeof(lTChunk))/sizeof(lTChunk)+1;
  //the corresponding size
  CDebuggingMemoryManagerSize lSize= lNumRecords * sizeof(lTChunk) ;

  while(lCurrent && (lCurrent->mSize < lSize))
    lCurrent= lCurrent->mNext;

  if(lCurrent){

    assert(lCurrent->mMagic== cUnMagic);
    assert(lCurrent->mSize >= lSize);

    //Cut the chunk in two pieces

    lTChunk lNewChunk;

    //the preceding chunk of a free chunk is always occupied
    //and  free chunk has always a preceding node:
    lNewChunk.mPrev= lCurrent->mPreceding;
    lNewChunk.mNext= lNewChunk.mPrev->mNext;
    lNewChunk.mPreceding=lCurrent;
    lNewChunk.mFollowing=lCurrent->mFollowing;
    lNewChunk.mSize= lSize - sizeof(lTChunk);
    lNewChunk.mMagic=cMagic;

    CDebuggingMemoryManagerSize lOffset =(lCurrent->mSize/sizeof(lTChunk))- lNumRecords;

    assert(lOffset >= 0);

    ///Position of the new occupied chunk
    lTChunk* lPos= lCurrent
      + 1 //lCurrent will not be overwritten
      + lOffset;
      
    assert(lNewChunk.mPrev);

    //A previous node of lCurrent ALWAYS exists, but...
    lNewChunk.mPrev->mNext= lPos;
    if(lNewChunk.mNext)
      lNewChunk.mNext->mPrev= lPos;

    //A chunk here has always a following and preceding chunk
    assert(lNewChunk.mFollowing);
    lNewChunk.mFollowing->mPreceding= lPos;

    assert(lNewChunk.mPreceding);
    lNewChunk.mPreceding->mFollowing= lPos;

    *lPos= lNewChunk;

    lCurrent->mSize -= lSize;

    mMutex.unlock();   
    return (void*)(lPos+1);
  }else
    assert(0);
};

void CDebuggingMemoryManager::FreeChunk(lTChunk* inChunk){
  mMutex.lock();

  assert(inChunk);
  assert(inChunk->mPrev);
  assert(inChunk->mMagic==cMagic);


  if(inChunk->mNext)
    inChunk->mNext->mPrev=inChunk->mPrev;

  inChunk->mPrev->mNext=inChunk->mNext;

  inChunk->mMagic=cUnMagic;
  mMutex.unlock();
}

bool CDebuggingMemoryManager::freeMem(void* inChunk){
  mMutex.lock();

  lTChunk* lChunk= ((lTChunk*) inChunk);
  lChunk--;

  if(lChunk->mMagic != cMagic){
    //Deallocation of something free
    mMutex.unlock();
    return false;
  }else{
    //The previous node is free
    if(lChunk->mPreceding->mMagic == cUnMagic){
      if(lChunk->mFollowing->mMagic == cUnMagic)
	{
	  // both the preceding and the following node are free
	  // and BOTH EXIST! => Three free nodes -> 1 free node
	  lTChunk* lNew = lChunk->mPreceding;

	  lNew->mSize+= lChunk->mSize 
	    + lChunk->mFollowing->mSize 
	    + 2 *sizeof(lTChunk);

	  //New pointers to the successors
	  lNew->mNext=       lChunk->mFollowing->mNext;
	  lNew->mFollowing = lChunk->mFollowing->mFollowing;

	  //New pointers from successors to predecessor
	  // has to exist because of the termination of the PrecFollow-list
	  lTChunk* lNextOccupied= lChunk->mFollowing->mFollowing;
	  assert(lNextOccupied);
	  {
	    // The next occupied node exists.
	    lNextOccupied->mPreceding= lNew;
	  }
	  if(lNew->mNext)
	    lNew->mNext->mPrev=lNew;

	}else{
	  // only the preceding node is free (and exists)
	  // Changes of the FreeList and of the PF-list

	  lTChunk* lNew = lChunk->mPreceding;

	  
	  lNew->mSize+= lChunk->mSize 
	    + sizeof(lTChunk);

	  lNew->mFollowing = lChunk->mFollowing;
	  //lNew->mNext stays: The FreeList is not changed

	  // has to exist because of the termination of the PrecFollow-list
	  lTChunk* lNextOccupied= lChunk->mFollowing;
	  assert(lNextOccupied);
	  {
	    // The next occupied node exists.
	    lNextOccupied->mPreceding= lNew;
	  }
	}
      FreeChunk(lChunk);
    }else{
      if(lChunk->mFollowing->mMagic == cUnMagic){

	//only the following node is free (and exists)

	lTChunk* lFollowing = lChunk->mFollowing;
	lChunk->mSize+=     
	  lFollowing->mSize
	  +sizeof(lTChunk);
	lChunk->mFollowing= lFollowing->mFollowing;


	//lFollowing out of the PF-List
	lFollowing->mFollowing->mPreceding= lChunk;

	//Chunk out of the OccupiedList
	FreeChunk(lChunk);

	//Move lChunk into the FreeList
	lChunk->mPrev=lFollowing->mPrev;
	lFollowing->mPrev->mNext= lChunk;

	// lFollowing out of FreeList
	lChunk->mNext= lFollowing->mNext;
	if(lChunk->mNext)
	  lChunk->mNext->mPrev=lChunk;
      }else{
	//Looking for the next piece of free mem.
	lTChunk* lCurrent=lChunk;

	while(lCurrent && (lCurrent->mPreceding->mMagic == cMagic))
	  lCurrent = lCurrent -> mPreceding;

	//lCurrent has to exist: Either the FreeList-anchor
	//or some other piece of free mem.
	assert(lCurrent);

	//lChunk out of the Occupied list
	FreeChunk(lChunk);

	// According to initialization,
	// lCurrent->mPreceding must be a free node.
	// and it exsists

	lTChunk* lFree=lCurrent -> mPreceding;
	assert(lFree);

	//lChunk into the FreeList
	lChunk->mNext=lFree->mNext;
	lChunk->mPrev=lFree;
	lFree->mNext= lChunk;
	if(lChunk->mNext)
	  lChunk->mNext->mPrev= lChunk;
      }
    }
  }
  mMutex.unlock();
  return true;
};

bool CDebuggingMemoryManager::isValid()const{
  return cVM==0x88414004;
}



ostream& operator<<(ostream& outStream,const CDebuggingMemoryManager& inMem){
  outStream << endl << "List of free memory chunks" << endl;
  {
    lTChunk* i=inMem.mFreeList;
    while(i && i!=i->mNext){
      outStream << i
		<< ","
		<< i->mPrev
		<< ","
		<< i->mSize
		<< ","
		<< i->mMagic%16
		<< endl;
      i=i->mNext;
    }

    outStream<< endl << "List of occupied memory chunks" << endl;

    i=inMem.mUsedList;
    while(i && i!=i->mNext){
      outStream << i
		<< ","
		<< i->mPrev
		<< ","
		<< i->mSize
		<< ","
		<< i->mMagic%16
		<< endl;
      i=i->mNext;
    }

    outStream << endl << "PrecedingFollowList" << endl;

    i=inMem.mFreeList;
    while(i!=i->mFollowing){
      outStream << i
		<< ","
		<< i->mPreceding
		<< ","
		<< i->mSize
		<< ","
		<< i->mMagic%16
		<< endl;
      i=i->mFollowing;
    }
    {
      outStream << i
		<< ","
		<< i->mPreceding
		<< ","
		<< i->mSize
		<< ","
		<< i->mMagic%16
		<< endl;
      i=i->mFollowing;
    }

    outStream << "END" << endl;
    i=inMem.mBuffer+(50000 / sizeof(lTChunk));


    outStream << "Terminator" << endl;
    outStream << i 
	      << "," 
	      << i->mPreceding 
	      << ","
	      << i->mSize
	      << ","
	      << i->mMagic%16
	      << endl;
  }
  return outStream;
};