Name | Executed | Routines | % | Executed | Lines | % | Unexecuted |
/home/matt/eu/rds/include/std/eumem.e | 3 | 3 | 100.00% | 31 | 35 | 88.57% | 4 |
Routine | Executed | Lines | Unexecuted | |
malloc() | 12 | 14 | 85.71% | 2 |
valid() | 10 | 12 | 83.33% | 2 |
free() | 6 | 6 | 100.00% | 0 |
# | Executed | |
1 | -- (c) Copyright - See License.txt | |
2 | -- | |
3 | --**** | |
4 | -- == Pseudo Memory | |
5 | -- | |
6 | -- | |
7 | -- One use is to emulate PBR, such as Euphoria's map and stack types. | |
8 | -- | |
9 | -- < | |
10 | ||
11 | namespace eumem | |
12 | ||
13 | --** | |
14 | -- The (pseudo) RAM heap space. Use [[:malloc]] to gain ownership to a heap location | |
15 | -- and [[:free]] to release it back to the system. | |
16 | 22 | export sequence ram_space = {} |
17 | ||
18 | 22 | integer ram_free_list = 0 |
19 | integer free_rid | |
20 | ||
21 | --** | |
22 | -- Allocate a block of (pseudo) memory | |
23 | -- | |
24 | -- Parameters: | |
25 | -- # ##mem_struct_p## : The initial structure (sequence) to occupy the allocated | |
26 | -- block. If this is an integer, a sequence of zero this long is used. The default | |
27 | -- is the number 1, meaning that the default initial structure is {0} | |
28 | -- # ##cleanup_p## : Identifies whether the memory should be released automatically | |
29 | -- when the reference count for the handle for the allocated block drops to | |
30 | -- zero, or when passed to ##delete()##. If 0, then the block must be freed | |
31 | -- using the [[:free]] procedure. | |
32 | -- | |
33 | -- Returns: | |
34 | -- A **handle**, to the acquired block. Once you acquire this, you can use it as you | |
35 | -- need to. Note that if ##cleanup_p## is 1, then the variable holding the | |
36 | -- handle must be capable of storing an atom as a double floating point value | |
37 | -- (i.e., not an integer). | |
38 | -- | |
39 | -- Example 1: | |
40 | -- | |
41 | -- my_spot = malloc() | |
42 | -- ram_space[my_spot] = my_data | |
43 | -- | |
44 | ||
45 | 84 | |
46 | integer temp_ | |
47 | ||
48 | 84 | if atom(mem_struct_p) then |
49 | 84 | mem_struct_p = repeat(0, mem_struct_p) |
50 | end if | |
51 | 84 | if ram_free_list = 0 then |
52 | 41 | ram_space = append(ram_space, mem_struct_p) |
53 | 41 | if cleanup_p then |
54 | 41 | return delete_routine( length(ram_space), free_rid ) |
55 | else | |
56 | 0 | return length(ram_space) |
57 | end if | |
58 | end if | |
59 | ||
60 | 43 | temp_ = ram_free_list |
61 | 43 | ram_free_list = ram_space[temp_] |
62 | 43 | ram_space[temp_] = mem_struct_p |
63 | ||
64 | 43 | if cleanup_p then |
65 | 43 | return delete_routine( temp_, free_rid ) |
66 | else | |
67 | 0 | return temp_ |
68 | end if | |
69 | end function | |
70 | ||
71 | --** | |
72 | -- Deallocate a block of (pseudo) memory | |
73 | -- | |
74 | -- Parameters: | |
75 | -- # ##mem_p## : The handle to a previously acquired [[:ram_space]] location. | |
76 | -- | |
77 | -- Comments: | |
78 | -- This allows the location to be used by other parts of your application. You | |
79 | -- should no longer access this location again because it could be acquired by | |
80 | -- some other process in your application. This routine should only be called | |
81 | -- if you passed 0 as ##cleanup_p## to [[:malloc]]. | |
82 | -- | |
83 | -- Example 1: | |
84 | -- | |
85 | -- my_spot = malloc(1,0) | |
86 | -- ram_space[my_spot] = my_data | |
87 | -- -- . . . do some processing . . . | |
88 | -- free(my_spot) | |
89 | -- | |
90 | ||
91 | 49 | |
92 | 49 | if mem_p < 1 then return end if |
93 | 49 | if mem_p > length(ram_space) then return end if |
94 | ||
95 | 49 | ram_space[mem_p] = ram_free_list |
96 | 49 | ram_free_list = floor(mem_p) |
97 | 49 | end procedure |
98 | 22 | free_rid = routine_id("free") |
99 | ||
100 | --** | |
101 | -- Validates a block of (pseudo) memory | |
102 | -- | |
103 | -- Parameters: | |
104 | -- # ##mem_p## : The handle to a previously acquired [[:ram_space]] location. | |
105 | -- # ##mem_struct_p## : If an integer, this is the length of the sequence that | |
106 | -- should be occupying the ram_space location pointed to by ##mem_p##. | |
107 | -- | |
108 | -- Returns: | |
109 | -- An **integer**,\\ | |
110 | -- 0 if either the ##mem_p## is invalid or if the sequence at that location is | |
111 | -- the wrong length.\\ | |
112 | -- 1 if the handle and contents is okay. | |
113 | -- | |
114 | -- Comments: | |
115 | -- This can only check the length of the contents at the location. Nothing else | |
116 | -- is checked at that location. | |
117 | -- | |
118 | -- Example 1: | |
119 | -- | |
120 | -- my_spot = malloc() | |
121 | -- ram_space[my_spot] = my_data | |
122 | -- . . . do some processing . . | |
123 | -- if valid(my_spot, length(my_data)) then | |
124 | -- free(my_spot) | |
125 | -- end if | |
126 | -- | |
127 | ||
128 | 5422 | |
129 | 5422 | if not integer(mem_p) then return 0 end if |
130 | 5421 | if mem_p < 1 then return 0 end if |
131 | 5414 | if mem_p > length(ram_space) then return 0 end if |
132 | ||
133 | 5414 | if sequence(mem_struct_p) then return 1 end if |
134 | ||
135 | 2 | if atom(ram_space[mem_p]) then |
136 | 1 | if mem_struct_p >= 0 then |
137 | 1 | return 0 |
138 | end if | |
139 | ||
140 | 0 | return 1 |
141 | end if | |
142 | ||
143 | 1 | if length(ram_space[mem_p]) != mem_struct_p then |
144 | 0 | return 0 |
145 | end if | |
146 | ||
147 | 1 | return 1 |
148 | end function |