Debian Codegraph @

terps/alan3/memory.c

raw symbols SCIP
1 /*----------------------------------------------------------------------*\
2
3 The Amachine memory
4
5 \*----------------------------------------------------------------------*/
6 #include "memory.h"
7
8 /* Imports */
9 #include "types.h"
10 #include "syserr.h"
11
12
13 /* PUBLIC DATA */
14
15 Aword *memory = NULL;
16 static ACodeHeader dummyHeader; /* Dummy to use until memory allocated */
17 ACodeHeader *header = &dummyHeader;
18 int memTop = 0; /* Top of load memory */
19
20 /* Private data */
21 typedef struct {
22 Aptr aptr;
23 void *voidp;
24 } PointerMapEntry;
25
26 /* (64-bit) pointers mapped to 32-bit Awords through mapping table:
27 Aptrs are numbered incrementally for each address that needs to be mapped.
28 A slot in the table is empty if its voidp field == NULL.
29 That slot can then be reused, so index in the table bears no meaning.
30 */
31
32 static PointerMapEntry *pointerMap = NULL;
33 static int pointerMapSize = 0;
34 static int nextAptr = 1;
35
36
37 /*======================================================================*/
38 void resetPointerMap(void) {
39 if (pointerMap != NULL) free(pointerMap);
40 pointerMap = NULL;
41 pointerMapSize = 0;
42 }
43
44 /*======================================================================*/
45 void *fromAptr(Aptr aptr) {
46 int index;
47
48 for (index=0; index < pointerMapSize && pointerMap[index].aptr != aptr; index++)
49 ;
50
51 if (index == pointerMapSize)
52 syserr("No pointerMap entry for Aptr");
53
54 return pointerMap[index].voidp;
55 }
56
57
58 /*======================================================================*/
59 Aptr toAptr(void *ptr) {
60 int index;
61
62 if (pointerMap == NULL) {
63 pointerMap = (PointerMapEntry *)allocate(sizeof(PointerMapEntry));
64 pointerMapSize = 1;
65 }
66
67 /* Search for ptr */
68 for (index=0; index < pointerMapSize; index++)
69 if (pointerMap[index].voidp == ptr)
70 return pointerMap[index].aptr;
71
72 /* So find a free slot */
73 for (index=0; index < pointerMapSize && pointerMap[index].voidp != NULL; index++)
74 ;
75
76 /* Need to extend pointerMap */
77 if (index == pointerMapSize) {
78 pointerMap = realloc(pointerMap, (index+1)*sizeof(PointerMapEntry));
79 pointerMapSize++;
80 }
81
82 pointerMap[index].voidp = ptr;
83 pointerMap[index].aptr = nextAptr++;
84 return pointerMap[index].aptr;
85 }
86
87 /*-------------------------------------------------------------------------------*/
88 static void forgetAptr(void *ptr) {
89 for (int index=0; index < pointerMapSize; index++)
90 if (pointerMap[index].voidp == ptr) {
91 pointerMap[index].aptr = 0;
92 pointerMap[index].voidp = NULL;
93 return;
94 }
95 }
96
97 /* Allocation/Deallocation: */
98 /*======================================================================*/
99 void *allocate(unsigned long lengthInBytes)
100 {
101 void *p = (void *)calloc((size_t)lengthInBytes, 1);
102
103 if (p == NULL)
104 syserr("Out of memory.");
105
106 return p;
107 }
108
109
110 /*======================================================================*/
111 void deallocate(void *memory)
112 {
113 forgetAptr(memory);
114 free(memory);
115 }
116
117
118 /*======================================================================*/
119 void *duplicate(void *original, unsigned long len)
120 {
121 void *p = allocate(len+1);
122
123 memcpy(p, original, len);
124 return p;
125 }