-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmmemory.c
More file actions
executable file
·816 lines (655 loc) · 24.5 KB
/
mmemory.c
File metadata and controls
executable file
·816 lines (655 loc) · 24.5 KB
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
///////////////////////////////////////////////////////////////////////////////
// Used abbreviations
///////////////////////////////////////////////////////////////////////////////
// VAS = Virtual Address Space
// va = virtual address
// pa = physical address
// seg = segment
///////////////////////////////////////////////////////////////////////////////
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <signal.h>
#include <assert.h>
#include "mmemory.h"
#include "logger.h"
///////////////////////////////////////////////////////////////////////////////
// Struct definitions
///////////////////////////////////////////////////////////////////////////////
typedef char* PA;
struct Segment
{
long nSize;
bool bIsFree;
struct Segment* pNext;
struct Segment* pPrev;
};
typedef struct Segment Segment;
typedef struct
{
Segment segment;
PA pSegAddress;
bool bIsPresent;
bool bIsAvailable;
} SegmentRecord;
typedef struct
{
SegmentRecord* pFirstRecord;
// TODO: size_t (probably not)
int nSize;
int nReserved;
int nFirstAvailableRecord;
int nForbiddenSegments[3];
Segment* pSegListHead;
} SegmentTable;
///////////////////////////////////////////////////////////////////////////////
#define LONG(x) ((long)(x))
#define VOID(x) ((void*)(x))
#define RECORD(x) ((SegmentRecord*)(x))
// number of bytes in VA reserved for segment index and offset
#define SEG_INDEX_BYTES 2
#define SEG_OFFSET_BYTES (sizeof(VA) - SEG_INDEX_BYTES)
// used to retrieve segment info from VA
#define GET_VA_SEG_INDEX(va) (LONG(va) >> (8 * SEG_OFFSET_BYTES))
#define GET_VA_SEG_OFFSET(va) (LONG(va) & ((1L << (8 * SEG_OFFSET_BYTES)) - 1))
// used to set segment info into VA
#define SET_VA_SEG_INDEX(va, index) (va = (VA)(GET_VA_SEG_OFFSET(va) | (index << (8 * SEG_OFFSET_BYTES))))
#define SET_VA_SEG_OFFSET(va, offset) (va = (VA)(((GET_VA_SEG_INDEX(va) << (8 * SEG_OFFSET_BYTES)) | offset)))
// handles signed negative integers
// #define GET_VA_SEG_INDEX(va) ((va >> (8 * SEG_OFFSET_BYTES)) & ((0xffL << ((SEG_INDEX_BYTES + 1)*8)) - 1))
#define SEG_TABLE_INCREMENT 10
#define SEG_TABLE_SIZE(reserved) (sizeof(SegmentTable) + (sizeof(SegmentRecord) * reserved))
// limits
#define VAS_SIZE (100 * 1024 * 1024) // bytes
#define MAX_SEGMENTS ((2 << (8*SEG_INDEX_BYTES)) - 1)
#define MAX_SEG_SIZE ((2L << (8*SEG_OFFSET_BYTES)) - 1)
#define MIN_SEG_SIZE (SEG_TABLE_INCREMENT * sizeof(SegmentRecord))
///////////////////////////////////////////////////////////////////////////////
// Globals
///////////////////////////////////////////////////////////////////////////////
PA g_pStartAddress;
size_t g_nCurrentVasSize = 0;
int g_nMaxRecords;
size_t g_nMaxSegmentSize;
SegmentTable* g_pSegTable;
#define GET_SEG_RECORD(va) (g_pSegTable->pFirstRecord + GET_VA_SEG_INDEX(va))
#define GET_SEG_RECORD_NO(i) (g_pSegTable->pFirstRecord + i)
#define GET_SEG_RECORD_INDEX(record) ((LONG(record) - LONG(g_pSegTable)) / sizeof(SegmentRecord))
///////////////////////////////////////////////////////////////////////////////
Segment* merge_free_segments(Segment* pFirstSegment, Segment* pSecondSegment)
{
LOG("Merging free segments:");
LOG_ADDR(" first:", LONG(pFirstSegment));
LOG_ADDR(" second:", LONG(pSecondSegment));
pFirstSegment->nSize += pSecondSegment->nSize;
pFirstSegment->pNext = pSecondSegment->pNext;
if (pSecondSegment->pNext)
pSecondSegment->pPrev = pFirstSegment;
return pFirstSegment;
}
Segment* initialize_free_segment(PA pAddress, size_t nSize, Segment* pPrevious, Segment* pNext)
{
// TODO: do some checks (probably not)
Segment* pFreeSegment = (Segment*)pAddress;
pFreeSegment->nSize = nSize;
pFreeSegment->bIsFree = true;
// link/merge segments
pFreeSegment->pPrev = pPrevious;
if (pPrevious)
{
if (pPrevious->bIsFree)
pFreeSegment = merge_free_segments(pPrevious, pFreeSegment);
else
pPrevious->pNext = pFreeSegment;
}
else
g_pSegTable->pSegListHead = pFreeSegment;
pFreeSegment->pNext = pNext;
if (pNext)
{
if (pNext->bIsFree)
pFreeSegment = merge_free_segments(pFreeSegment, pNext);
else
pNext->pPrev = pFreeSegment;
}
LOG("Free memory segment initialized:");
LOG_LONG("\tsize:", pFreeSegment->nSize);
LOG_ADDR("\taddress:", LONG(pFreeSegment));
LOG_ADDR("\tnext:", LONG(pFreeSegment->pNext));
LOG_ADDR("\tprev:", LONG(pFreeSegment->pPrev));
return pFreeSegment;
}
Segment* unload_segment(SegmentRecord* pRecord)
{
LOG_INT("Unloading segment No.", GET_SEG_RECORD_INDEX(pRecord));
LOG_ADDR(" seg record:", LONG(pRecord));
Segment* pSegmentToUnload = &pRecord->segment;
// allocate disk memory for segment
void* pDiskMemory = malloc(pSegmentToUnload->nSize);
if (!pDiskMemory)
{
LOG("Error! Can't allocate disk memory for segment");
return NULL;
}
if (pRecord->pSegAddress)
{
// copy segment content to disk memory
memcpy(pDiskMemory, pRecord->pSegAddress, pSegmentToUnload->nSize);
// replace segment in memory with free segment
pSegmentToUnload = initialize_free_segment(pRecord->pSegAddress,
pSegmentToUnload->nSize,
pSegmentToUnload->pPrev,
pSegmentToUnload->pNext);
}
// update record state
pRecord->pSegAddress = pDiskMemory;
pRecord->bIsPresent = false;
pRecord->segment.pPrev = NULL;
pRecord->segment.pNext = NULL;
LOG("Segment successfully unloaded");
return pSegmentToUnload;
}
bool segment_is_forbidden(const SegmentRecord* pSegRecord)
{
const int nSegIndex = GET_SEG_RECORD_INDEX(pSegRecord);
for (int i = 0; i < 3; ++i)
{
if (g_pSegTable->nForbiddenSegments[i] == nSegIndex)
return true;
}
return false;
}
Segment* find_free_place_for_segment(size_t nSize, bool bForce)
{
LOG_LONG("Searching for free place to load the segment of size:", nSize);
// + sizeof(Segment) - ensure that there will be no free segments with size < sizeof(Segment)
const size_t nSizeWithOffset = nSize + sizeof(Segment);
Segment* pSeg = g_pSegTable->pSegListHead;
while (pSeg)
{
LOG(" found segment:");
LOG_ADDR(" address:", LONG(pSeg));
LOG_LONG(" size:", pSeg->nSize);
LOG_INT(" free:", pSeg->bIsFree);
if (pSeg->bIsFree && (pSeg->nSize >= nSizeWithOffset || pSeg->nSize == nSize))
{
LOG_ADDR("Found suitable free segment with address:", LONG(pSeg));
return pSeg;
}
pSeg = pSeg->pNext;
}
LOG("No suitable free segment found");
if (bForce)
{
LOG("Forcing memory deallocation");
// find first not free and not forbidden segment
pSeg = g_pSegTable->pSegListHead;
while (pSeg)
{
if (!pSeg->bIsFree && !segment_is_forbidden(RECORD(pSeg)))
break;
pSeg = pSeg->pNext;
}
// find largest segment
Segment* pLargestSegment = pSeg;
while (pSeg)
{
if (!pSeg->bIsFree && !segment_is_forbidden(RECORD(pSeg)))
{
// found segment with suitable size
if (pSeg->nSize >= nSizeWithOffset || pSeg->nSize == nSize)
return unload_segment(RECORD(pSeg));
// mark largest found segment
if (pSeg->nSize > pLargestSegment->nSize)
pLargestSegment = pSeg;
}
pSeg = pSeg->pNext;
}
// unload largest segment found
if (!pLargestSegment)
{
LOG("Cannot deallocate enough memory for segment");
return NULL;
}
LOG_ADDR("Unloading largest:", LONG(pLargestSegment));
Segment* pFreeSeg = unload_segment(RECORD(pLargestSegment));
// keep unloading following segments
SegmentRecord* pNextRecord;
while (pFreeSeg->nSize < nSizeWithOffset && pFreeSeg->nSize != nSize && pFreeSeg->pNext)
{
pNextRecord = RECORD(pFreeSeg->pNext);
if (segment_is_forbidden(pNextRecord))
break;
pFreeSeg = unload_segment(pNextRecord);
}
if (pFreeSeg->nSize >= nSizeWithOffset || pFreeSeg->nSize == nSize)
return pFreeSeg;
// unload previous segments, if memory is still not enough
while (pFreeSeg->nSize < nSizeWithOffset && pFreeSeg->nSize != nSize && pFreeSeg->pPrev)
{
pNextRecord = RECORD(pFreeSeg->pPrev);
if (segment_is_forbidden(pNextRecord))
break;
pFreeSeg = unload_segment(pNextRecord);
}
if (pFreeSeg->nSize < nSizeWithOffset && pFreeSeg->nSize != nSize)
{
LOG("Cannot deallocate enough memory for segment");
return NULL;
}
return pFreeSeg;
}
return NULL;
}
bool load_segment_into_memory(SegmentRecord* pRecord, Segment* pFreeSegment)
{
LOG("Loading segment into memory:");
LOG_INT("\tsegment No.", GET_SEG_RECORD_INDEX(pRecord));
LOG_ADDR("\tdestination address:", LONG(pFreeSegment));
Segment* pSegmentToLoad = &pRecord->segment;
const size_t nMemoryLeft = pFreeSegment->nSize - pSegmentToLoad->nSize;
// initialize free segment in the rest of the memory
if (nMemoryLeft > 0)
{
PA pNewFreeSegment = (char*)pFreeSegment + pSegmentToLoad->nSize;
initialize_free_segment(pNewFreeSegment,
nMemoryLeft,
pSegmentToLoad,
pFreeSegment->pNext);
}
// if no memory left, just link new segment to the next one
else if (nMemoryLeft == 0)
{
pSegmentToLoad->pNext = pFreeSegment->pNext;
if (pSegmentToLoad->pNext)
pSegmentToLoad->pNext->pPrev = pSegmentToLoad;
}
else
{
LOG("Error! Free segment is too small");
return false;
}
// link previous segment to the new one
pSegmentToLoad->pPrev = pFreeSegment->pPrev;
if (pFreeSegment->pPrev)
pFreeSegment->pPrev->pNext = pSegmentToLoad;
else
// set segment list head
g_pSegTable->pSegListHead = pSegmentToLoad;
if (pRecord->pSegAddress)
{
// copy segment content into memory
memcpy(VOID(pFreeSegment), VOID(pRecord->pSegAddress), pSegmentToLoad->nSize);
// free disk memory
free(VOID(pRecord->pSegAddress));
}
pRecord->pSegAddress = (PA)pFreeSegment;
pRecord->bIsPresent = true;
//pSegmentToLoad->bIsFree = false;
LOG("Segment has been successfully loaded");
return true;
}
void load_adjacent_segments_into_memory(int nSegmentIndex)
{
const int segmentsToLoad[3] = {nSegmentIndex, nSegmentIndex - 1, nSegmentIndex + 1};
LOG("Loading into memory adjacent segments:");
for (int i = 0; i < 3; ++i)
LOG_INT(" no.", segmentsToLoad[i]);
for (int i = 0; i < 3; ++i)
{
const int nSegment = segmentsToLoad[i];
SegmentRecord* pRecord;
if (nSegment >= 0 && nSegment < g_pSegTable->nSize)
{
pRecord = GET_SEG_RECORD_NO(nSegment);
if (!pRecord->bIsAvailable && !pRecord->bIsPresent)
{
// forbid segment from unloading, while loading adjacent ones
g_pSegTable->nForbiddenSegments[i] = nSegment;
Segment* pFreeSegment = find_free_place_for_segment(pRecord->segment.nSize, true);
if (pFreeSegment)
load_segment_into_memory(pRecord, pFreeSegment);
}
}
}
// from now on segments can be unloaded
for (int i = 0; i < 3; ++i)
g_pSegTable->nForbiddenSegments[i] = -1;
}
bool increase_table_size()
{
LOG("Increasing segment table size");
if (g_pSegTable->nReserved + SEG_TABLE_INCREMENT > g_nMaxRecords)
return false;
g_pSegTable->nReserved += SEG_TABLE_INCREMENT;
Segment* pFirstSegment = g_pSegTable->pSegListHead;
if (!pFirstSegment->bIsFree)
{
for (int i = 0; i < g_pSegTable->nSize; ++i)
{
SegmentRecord* pRecord = GET_SEG_RECORD_NO(i);
// unload first segment in memory if it is not free
if (&(pRecord->segment) == pFirstSegment)
{
pFirstSegment = unload_segment(pRecord);
if (!pFirstSegment)
return false;
break;
}
}
}
// cutting piece of memory from first segment
PA pNewFreeSegment = g_pStartAddress + SEG_TABLE_SIZE(g_pSegTable->nReserved);
const size_t nNewSize = pFirstSegment->nSize - (LONG(pNewFreeSegment) - LONG(pFirstSegment));
g_pSegTable->pSegListHead = initialize_free_segment(pNewFreeSegment,
nNewSize,
NULL,
pFirstSegment->pNext);
return true;
}
// TODO: check max records
bool insert_new_record_into_table(size_t nSegmentSize)
{
LOG("Inserting new record into SegmentTable");
const size_t nTableSize = g_pSegTable->nSize;
// reserve space for table records if it exceeds
if (g_pSegTable->nFirstAvailableRecord >= g_pSegTable->nReserved)
if (!increase_table_size())
return false;
SegmentRecord* pNewRecord = GET_SEG_RECORD_NO(g_pSegTable->nFirstAvailableRecord);
pNewRecord->pSegAddress = NULL;
pNewRecord->bIsPresent = false;
pNewRecord->bIsAvailable = false;
pNewRecord->segment.nSize = nSegmentSize;
pNewRecord->segment.bIsFree = false;
pNewRecord->segment.pNext = NULL;
LOG_INT("Segment record No.", g_pSegTable->nFirstAvailableRecord);
LOG_ADDR(" is loaded into memory address:", LONG(pNewRecord));
if (g_pSegTable->nFirstAvailableRecord == g_pSegTable->nSize)
g_pSegTable->nSize++;
// find place to load next record
bool bFoundAvailable = false;
for (int i = g_pSegTable->nFirstAvailableRecord + 1; i < nTableSize; ++i)
if (GET_SEG_RECORD_NO(i)->bIsAvailable)
{
g_pSegTable->nFirstAvailableRecord = i;
bFoundAvailable = true;
break;
}
if (!bFoundAvailable)
g_pSegTable->nFirstAvailableRecord = g_pSegTable->nSize;
LOG_INT("Table size:", g_pSegTable->nSize);
LOG_INT("Next record will have index", g_pSegTable->nFirstAvailableRecord);
return true;
}
///////////////////////////////////////////////////////////////////////////////
// Log functions
///////////////////////////////////////////////////////////////////////////////
#ifndef NO_LOG
void log_struct_sizes()
{
LOG_STR("######################################################################");
LOG("###### Struct sizes ######");
LOG_INT("SegmentTable:", sizeof(SegmentTable));
LOG_INT("SegmentRecord:", sizeof(SegmentRecord));
LOG_INT("Segment:", sizeof(Segment));
LOG_STR("######################################################################");
}
void log_memory_dump()
{
LOG_STR("**********************************************************************");
LOG("****** Memory dump ******");
LOG("Segment table:");
LOG_ADDR(" address:", LONG(g_pSegTable));
LOG_INT(" records:", g_pSegTable->nSize);
LOG_INT(" reserved:", g_pSegTable->nReserved);
LOG("Segment records:");
SegmentRecord* pRecord;
for (int i = 0; i < g_pSegTable->nSize; ++i)
{
pRecord = GET_SEG_RECORD_NO(i);
LOG_INT(" record No.", i);
LOG_ADDR(" address:", LONG(pRecord));
LOG_ADDR(" segment address:", LONG(pRecord->pSegAddress));
LOG_INT(" present:", pRecord->bIsPresent);
LOG_INT(" available:", pRecord->bIsAvailable);
}
LOG("Segments:");
Segment* pSeg = g_pSegTable->pSegListHead;
while (pSeg)
{
if (pSeg->bIsFree)
LOG(" Segment (free):");
else
LOG_INT(" Segment No.", GET_SEG_RECORD_INDEX(RECORD(pSeg)));
LOG_ADDR(" address:", LONG(pSeg));
LOG_LONG(" size:", pSeg->nSize);
LOG_INT(" free:", pSeg->bIsFree);
pSeg = pSeg->pNext;
}
LOG_STR("**********************************************************************");
}
#endif
///////////////////////////////////////////////////////////////////////////////
// Core functions
///////////////////////////////////////////////////////////////////////////////
int m_malloc(VA* ptr, size_t szBlock)
{
LOG_LONG("m_malloc: Initializing memory segment of size:", szBlock);
if (!ptr || szBlock < MIN_SEG_SIZE || szBlock > g_nMaxSegmentSize)
{
LOG("m_malloc: ERROR! Wrong parameters");
return WRONG_PARAMETERS;
}
if (g_nCurrentVasSize + szBlock > VAS_SIZE || g_pSegTable->nFirstAvailableRecord >= g_nMaxRecords)
{
LOG("m_malloc: ERROR! Not enough memory");
return NOT_ENOUGH_MEMORY;
}
g_nCurrentVasSize += szBlock;
const int nSegmentIndex = g_pSegTable->nFirstAvailableRecord;
if (!insert_new_record_into_table(szBlock))
{
LOG("m_malloc: ERROR! Can not insert new record");
return UNKNOWN_ERROR;
}
SegmentRecord* pNewRecord = GET_SEG_RECORD_NO(nSegmentIndex);
Segment* pFreeSegment = find_free_place_for_segment(szBlock, false);
if (!pFreeSegment)
{
if (!unload_segment(pNewRecord))
{
LOG("m_malloc: ERROR! Can not unload allocated segment");
return UNKNOWN_ERROR;
}
}
else
{
if (!load_segment_into_memory(pNewRecord, pFreeSegment))
{
LOG("m_malloc: ERROR! Can not load allocated segment into memory");
return UNKNOWN_ERROR;
}
}
SET_VA_SEG_INDEX(*ptr, LONG(nSegmentIndex));
SET_VA_SEG_OFFSET(*ptr, 0L);
LOG("m_malloc: Segment successfully initialized");
return SUCCESS;
}
int m_free(VA ptr)
{
const int nSegmentIndex = GET_VA_SEG_INDEX(ptr);
const long nSegmentOffset = GET_VA_SEG_OFFSET(ptr);
LOG("m_free: Deallocating memory from segment");
LOG_INT(" no.", nSegmentIndex);
if (nSegmentIndex >= g_pSegTable->nSize || nSegmentIndex < 0)
{
LOG("m_free: ERROR! Wrong parameters");
return WRONG_PARAMETERS;
}
SegmentRecord* pRecord = GET_SEG_RECORD_NO(nSegmentIndex);
// TODO: ignore non-zero offset?
if (pRecord->bIsAvailable || nSegmentOffset != 0)
{
LOG("m_free: ERROR! Wrong parameters");
return WRONG_PARAMETERS;
}
if (pRecord->bIsPresent)
{
initialize_free_segment(pRecord->pSegAddress,
pRecord->segment.nSize,
pRecord->segment.pPrev,
pRecord->segment.pNext);
}
else
{
LOG_ADDR("Deallocating disk memory at:", LONG(pRecord->pSegAddress));
free(pRecord->pSegAddress);
}
pRecord->bIsAvailable = true;
g_nCurrentVasSize -= pRecord->segment.nSize;
if (g_pSegTable->nFirstAvailableRecord > nSegmentIndex)
g_pSegTable->nFirstAvailableRecord = nSegmentIndex;
LOG("m_free: Segment deallocation successful");
return SUCCESS;
}
int m_read(VA ptr, void* pBuffer, size_t szBuffer)
{
const int nSegmentIndex = GET_VA_SEG_INDEX(ptr);
const long nSegmentOffset = GET_VA_SEG_OFFSET(ptr);
LOG("m_read: Reading from segment");
LOG_INT(" segment no.", nSegmentIndex);
LOG_LONG(" offset:", nSegmentOffset);
if (nSegmentIndex >= g_pSegTable->nSize || nSegmentIndex < 0)
{
LOG("m_read: ERROR! Wrong segment index");
return WRONG_PARAMETERS;
}
SegmentRecord* pRecord = GET_SEG_RECORD_NO(nSegmentIndex);
if (pRecord->bIsAvailable || !pBuffer || szBuffer <= 0)
{
LOG("m_read: ERROR! Wrong parameters");
return WRONG_PARAMETERS;
}
if (pRecord->segment.nSize - nSegmentOffset < szBuffer)
{
LOG("m_read: ERROR! Reading outside the segment");
return SEGMENT_VIOLATION;
}
// TODO: check size
if (!pRecord->bIsPresent)
{
LOG("Required segment is not present in memory");
load_adjacent_segments_into_memory(nSegmentIndex);
}
if (!pRecord->bIsPresent)
{
LOG("m_read: ERROR! Can not load required segment into memory");
return UNKNOWN_ERROR;
}
LOG_LONG("Reading from segment to buffer of size:", szBuffer);
memcpy(pBuffer, VOID(pRecord->pSegAddress + nSegmentOffset), szBuffer);
LOG("m_read: Reading successfully finished");
return SUCCESS;
}
int m_write(VA ptr, void* pBuffer, size_t szBuffer)
{
const int nSegmentIndex = GET_VA_SEG_INDEX(ptr);
const long nSegmentOffset = GET_VA_SEG_OFFSET(ptr);
LOG("m_write: Writing into segment");
LOG_INT(" segment no.", nSegmentIndex);
LOG_LONG(" offset:", nSegmentOffset);
if (nSegmentIndex >= g_pSegTable->nSize || nSegmentIndex < 0)
{
LOG("m_write: ERROR! Wrong segment index");
return WRONG_PARAMETERS;
}
SegmentRecord* pRecord = GET_SEG_RECORD_NO(nSegmentIndex);
if (pRecord->bIsAvailable || !pBuffer || szBuffer <= 0)
{
LOG("m_write: ERROR! Wrong parameters");
return WRONG_PARAMETERS;
}
if ((pRecord->segment.nSize - nSegmentOffset) < szBuffer)
{
LOG("m_write: ERROR! Writing outside the segment");
return SEGMENT_VIOLATION;
}
// TODO: check size
if (!pRecord->bIsPresent)
{
LOG("Required segment is not present in memory");
load_adjacent_segments_into_memory(nSegmentIndex);
}
if (!pRecord->bIsPresent)
{
LOG("m_write: ERROR! Can not load required segment into memory");
return UNKNOWN_ERROR;
}
LOG_INT("Writing into segment from buffer of size:", szBuffer);
memcpy(VOID(pRecord->pSegAddress + nSegmentOffset), pBuffer, szBuffer);
LOG("m_write: Writing successfully finished");
return SUCCESS;
}
int m_init(int n, int szPage)
{
#ifndef NO_LOG
// ensure that log will be written even after program crash
const int signals[6] = {SIGINT, SIGILL, SIGABRT, SIGFPE, SIGSEGV, SIGTERM};
for (size_t i = 0; i < 6; ++i)
assert(signal(signals[i], terminate_logger) != SIG_ERR);
log_struct_sizes();
#endif
LOG("m_init: Initializing memory manager");
LOG(" params:");
LOG_INT(" ", n);
LOG_INT(" ", szPage);
if (n <= 0 || szPage <= 0)
{
LOG("m_init: ERROR! Wrong parameters");
return WRONG_PARAMETERS;
}
const long nTotalMemory = LONG(n) * szPage;
const int nTableInitialSize = SEG_TABLE_SIZE(SEG_TABLE_INCREMENT);
g_nCurrentVasSize = 0;
// limit min size of allocated memory
if (nTotalMemory < nTableInitialSize + MIN_SEG_SIZE)
{
LOG("m_init: ERROR! Wrong parameters");
return WRONG_PARAMETERS;
}
// limit max amount of records, otherwise SegmentTable may fill all memory
g_nMaxRecords = ((nTotalMemory / 3) - sizeof(SegmentTable)) / sizeof(SegmentRecord);
if (g_nMaxRecords < SEG_TABLE_INCREMENT)
g_nMaxRecords = SEG_TABLE_INCREMENT;
LOG_INT("Max records:", g_nMaxRecords);
// limit max segment size
g_nMaxSegmentSize = nTotalMemory - SEG_TABLE_SIZE(g_nMaxRecords);
// if (g_nMaxSegmentSize > MAX_SEG_SIZE)
// g_nMaxSegmentSize = MAX_SEG_SIZE;
LOG_LONG("Max segment size:", g_nMaxSegmentSize);
LOG_LONG("Allocating physical memory (bytes):", nTotalMemory);
g_pStartAddress = malloc(nTotalMemory);
if (!g_pStartAddress)
{
LOG("m_init: ERROR! Can not allocate enough memory");
return UNKNOWN_ERROR;
}
g_pSegTable = (SegmentTable*)g_pStartAddress;
PA pFirstAvailableAddress = g_pStartAddress + nTableInitialSize;
LOG_ADDR("Segment table physical address:", LONG(g_pStartAddress));
LOG_INT("Segment table initial size (bytes):", nTableInitialSize);
LOG_ADDR("First available physical address:", LONG(pFirstAvailableAddress));
// init segment table
g_pSegTable->pFirstRecord = RECORD(g_pSegTable + 1);
g_pSegTable->nSize = 0;
g_pSegTable->nReserved = SEG_TABLE_INCREMENT;
g_pSegTable->nFirstAvailableRecord = 0;
g_pSegTable->nForbiddenSegments[0] = g_pSegTable->nForbiddenSegments[1] = g_pSegTable->nForbiddenSegments[2] = -1;
g_pSegTable->pSegListHead = initialize_free_segment(pFirstAvailableAddress,
nTotalMemory - nTableInitialSize,
NULL,
NULL);
LOG("m_init: Memory manager initialized successfully");
return SUCCESS;
}