add mem:realloc to malloc device (thanks again to Rick C!)

FossilOrigin-Name: 493cde1a91b60883baed6a0aa5cac65c2a0d4130aaeae363763f697228d59335
This commit is contained in:
crc 2022-07-27 09:57:31 +00:00
parent 3417d48ffb
commit 0317b9074c
4 changed files with 167 additions and 81 deletions

View file

@ -4,18 +4,20 @@
{{
:mem:invoke #15 io:scan-for io:invoke ;
#0 'ALLOC const
#1 'FREE const
#2 'STORE const
#3 'FETCH const
#0 'ALLOC const
#1 'FREE const
#2 'STORE const
#3 'FETCH const
#4 'RESIZE const
---reveal---
:mem:alloc (n--aa) ALLOC mem:invoke ;
:mem:set (an--) STORE mem:invoke ;
:mem:get (a--n) FETCH mem:invoke ;
:mem:free (aa--) FREE mem:invoke ;
:mem:alloc (n--aa) ALLOC mem:invoke ;
:mem:set (an--) STORE mem:invoke ;
:mem:get (a--n) FETCH mem:invoke ;
:mem:free (aa--) FREE mem:invoke ;
:mem:resize (aan--) RESIZE mem:invoke ;
}}
~~~

73
tests/malloc.retro Normal file
View file

@ -0,0 +1,73 @@
# Allocator Tests
## Test Setup / Helpers
```
hex
:print s:put nl ;
:n:print n:put nl ;
:double:print n:put n:put nl ;
```
## Test 1: `mem:alloc`
Attempt to allocate 32 bytes of memory and assign it to the const `region1`:
```
'===TEST1:_mem:alloc print
'Allocating... print
#32 mem:alloc
'Storing_to_`region1`... print
'region1 double:const
'New_memory_region_allocated_at print
region1 double:print
'===OK print nl nl
```
## Test 2: `mem:set`
```
'===TEST2:_mem:set print
'Creating_a_random_value... print
n:random
'Storing_to_`random-value`... print
'random-value const
'New_random_value_is: print
random-value n:print
'Storing_value_to_`region1`... print
region1 random-value mem:set
'===OK print nl nl
```
## Test 3: `mem:get`
```
'===TEST3:_mem:get print
'Expect_ s:put 'to_contain_ s:put random-value n:put nl
'Actual_value:_____ s:put region1 mem:get n:put nl
'===OK print nl nl
```
## Test 4: `mem:resize`
```
'===TEST4:_mem:resize print
'Resizing_`region1`_to_64_bytes... print
region1 #64 mem:resize
'Storing_to_`region2`... print
'region2 double:const
'New_region_address:_ s:put region2 double:print
'Old_region_address:_ s:put region1 double:print
'===OK print nl nl
```
## Test 5: Read/Write to resized region
```
'Region2_contains: print
region2 mem:get n:put nl
'Should_equal: print
random-value n:print nl
```
## Test 5: `mem:free`

View file

@ -10,8 +10,8 @@
#define CELL_MAX LLONG_MAX - 1
#endif
#endif
CELL ngaImageCells = 18920;
CELL ngaImage[] = { 1793,18427,18875,18919,202207,410,382,1242,1535,0,10,1,10,2,10,3,10,4,10,
CELL ngaImageCells = 18953;
CELL ngaImage[] = { 1793,18460,18908,18952,202207,410,382,1242,1535,0,10,1,10,2,10,3,10,4,10,
5,10,6,10,7,10,8,10,11,10,12,10,13,10,14,10,15,10,16,10,
17,10,18,10,19,10,20,10,21,10,22,10,23,10,24,10,25,68223234,1,2575,
85000450,1,656912,161,178,268505089,63,62,285281281,0,63,2063,10,101384453,0,9,10,68485378,255,18350338,
@ -20,8 +20,8 @@ CELL ngaImage[] = { 1793,18427,18875,18919,202207,410,382,1242,1535,0,10,1,10,2,
101777669,1,17565186,107,524545,111,64,167838467,-1,134287105,3,59,659457,3,459023,128,2049,56,25,2049,
128,1793,135,2049,135,117506307,0,128,0,524545,26,133,168820993,0,147,1642241,147,134283523,11,133,
1793,128,524545,2049,128,1793,128,16846593,147,161,178,1793,64,16846593,147,133,178,1793,64,7,
10,659713,1,659713,2,659713,3,659713,4,659713,5,1793,18682,17108737,3,2,524559,128,2049,128,
2049,128,524545,0,128,524545,0,128,2049,142,168820998,2,17663,1242,167841793,211,9,17826049,0,211,
10,659713,1,659713,2,659713,3,659713,4,659713,5,1793,18715,17108737,3,2,524559,128,2049,128,
2049,128,524545,0,128,524545,0,128,2049,142,168820998,2,17696,1242,167841793,211,9,17826049,0,211,
2,15,25,524546,188,134287105,212,116,2305,213,459023,221,134287361,212,216,659201,211,10,659969,7,
2049,56,25,17694978,58,237,9,84152833,48,319750404,236,117507601,239,184618754,45,25,16974851,-1,168886532,1,
134284289,1,252,134284289,0,239,660227,32,0,0,115,105,103,105,108,58,105,0,285278479,269,
@ -170,7 +170,7 @@ CELL ngaImage[] = { 1793,18427,18875,18919,202207,410,382,1242,1535,0,10,1,10,2,
110,58,105,110,99,0,659713,1,10,3114,3139,166,16136,266800217,110,58,100,101,99,0,
659969,1,10,3128,3158,166,16136,1032861494,110,58,98,101,116,119,101,101,110,63,0,67503109,
1793,3166,67503109,67503109,2049,3106,10,1,3161,2049,2241,11,10,3142,3187,166,16136,-2130983749,83,99,
111,112,101,76,105,115,116,0,18777,18831,10,3172,3198,166,16136,5864091,123,123,0,2049,
111,112,101,76,105,115,116,0,18810,18864,10,3172,3198,166,16136,5864091,123,123,0,2049,
1576,2,1,3187,2049,59,16,10,3190,3225,166,16136,-1644352334,45,45,45,114,101,118,101,
97,108,45,45,45,0,2049,1576,1,3187,2049,3125,16,10,3207,3241,166,16136,5864159,125,
125,0,1,3187,2049,56,4,15,11,1793,3255,3841,3187,4097,2,10,1,3250,1793,3281,
@ -228,7 +228,7 @@ CELL ngaImage[] = { 1793,18427,18875,18919,202207,410,382,1242,1535,0,10,1,10,2,
10,4239,4297,154,16136,1204567429,84,101,109,112,83,116,114,105,110,103,115,0,32,4280,
4317,154,16136,1808866840,84,101,109,112,83,116,114,105,110,103,77,97,120,0,512,4298,
4331,166,16136,-1027089649,83,84,82,73,78,71,83,0,2049,1545,3841,4297,3841,4317,19,18,
10,4318,4353,154,0,0,67,117,114,114,101,110,116,0,14,10,4340,4370,166,0,
10,4318,4353,154,0,0,67,117,114,114,101,110,116,0,16,10,4340,4370,166,0,
0,115,58,112,111,105,110,116,101,114,0,3841,4353,3841,4317,19,2049,4331,17,10,
4355,4391,166,0,0,115,58,110,101,120,116,0,1,4353,2049,3864,3841,4353,3841,4297,
11,1793,4407,1,0,4097,4353,10,1,4402,9,10,4318,4423,166,16136,410724968,115,58,116,
@ -889,72 +889,73 @@ CELL ngaImage[] = { 1793,18427,18875,18919,202207,410,382,1242,1535,0,10,1,10,2,
115,116,0,2049,17427,1,17453,2049,8460,10,17484,17525,166,0,0,109,101,109,58,105,
110,118,111,107,101,0,1,15,2049,10655,2049,10636,10,17509,0,154,0,0,65,76,
76,79,67,0,17532,1,154,0,0,70,82,69,69,0,17543,2,154,0,0,83,
84,79,82,69,0,17553,3,154,0,0,70,69,84,67,72,0,17484,17590,166,0,
369169257,109,101,109,58,97,108,108,111,99,0,1,0,2049,17525,10,17575,17608,166,0,
-1111837014,109,101,109,58,115,101,116,0,1,2,2049,17525,10,17595,17626,166,0,-1111850082,109,
101,109,58,103,101,116,0,1,3,2049,17525,10,17613,17645,166,0,1963630784,109,101,109,
58,102,114,101,101,0,1,1,2049,17525,10,17631,17659,166,18446,193470948,84,73,66,0,
1,7,15,10,17650,17679,166,18446,-460132687,105,109,97,103,101,58,115,97,118,101,0,
1,1000,2049,10655,2049,10636,10,17663,17697,166,0,0,101,100,105,116,63,0,2,1793,
17704,1,8,11,10,1,17700,1793,17712,1,127,11,10,1,17708,2049,2255,22,10,17686,
17730,166,0,0,103,97,116,104,101,114,0,2049,17697,1,15,1,4134,2049,64,10,
17718,17750,166,0,0,99,121,99,108,101,0,2049,10985,2049,2217,4,8,2049,2644,25,
3,2049,17730,1,17750,7,10,17663,17783,166,18446,-1658019367,112,97,114,115,101,45,117,110,
116,105,108,0,1793,17795,2049,4451,2049,4234,2049,17750,771,2049,4096,10,1,17785,2049,4260,
10,17766,17811,166,18446,272733106,115,58,103,101,116,0,1793,17833,1793,17819,1,13,11,10,
1,17815,1793,17827,1,10,11,10,1,17823,2049,2255,22,10,1,17813,2049,17783,10,17800,
17849,166,18446,255552908,99,108,101,97,114,0,2049,4472,92,94,91,50,74,92,94,91,
48,59,48,72,0,1,17851,2049,8246,2049,10761,10,17838,17883,154,18446,-976122143,78,111,69,
99,104,111,0,0,17871,17893,154,0,0,69,79,84,0,0,17884,17908,154,0,0,
73,103,110,111,114,105,110,103,0,0,17894,17924,166,0,0,105,103,110,111,114,
105,110,103,63,0,3841,17908,10,17909,17940,166,0,0,118,101,114,115,105,111,110,
0,3841,4,1,100,20,10,17927,17957,166,0,0,100,111,110,101,63,0,2,4097,
17893,1793,17966,1,13,11,10,1,17962,1793,17974,1,10,11,10,1,17970,1793,17982,1,
32,11,10,1,17978,2049,2298,22,22,10,17946,17999,166,0,0,101,111,108,63,0,
3841,17893,1793,18007,1,13,11,10,1,18003,1793,18015,1,10,11,10,1,18011,2049,2255,
22,10,17989,18033,166,0,0,118,97,108,105,100,63,0,2,2049,102,2049,2812,10,
18021,18054,166,0,0,99,104,101,99,107,45,101,111,102,0,2,1793,18061,1,-1,
11,10,1,18057,1793,18069,1,4,11,10,1,18065,2049,2255,22,1793,18079,2049,11052,10,
1,18076,9,10,18039,18091,166,0,0,98,115,0,2049,4212,1,2,2049,2675,1793,18103,
2049,4160,3,10,1,18099,9,2049,4160,3,10,18083,18124,166,0,0,99,104,101,99,
107,45,98,115,0,2,1793,18131,1,8,11,10,1,18127,1793,18139,1,127,11,10,
1,18135,2049,2255,22,1793,18149,2049,18091,10,1,18146,9,10,18110,18164,166,0,0,99,
104,101,99,107,0,2049,18054,2049,18124,10,18153,18184,166,0,0,99,104,97,114,97,
99,116,101,114,0,2049,10985,2,2049,4134,10,18169,18202,166,0,0,98,117,102,102,
101,114,0,1793,18212,2049,17659,2049,4234,8,2049,4096,10,1,18204,2049,4260,10,18190,18233,
166,0,0,114,101,97,100,45,116,111,107,101,110,0,1793,18249,1793,18244,2049,18184,
2049,18164,2049,17957,10,1,18237,2049,2397,10,1,18235,2049,18202,2049,4587,10,18217,18267,166,
0,0,105,110,112,117,116,0,2049,18233,2049,18033,10,18256,18285,166,0,0,112,114,
111,99,101,115,115,0,2049,17924,1793,18303,771,2049,17999,1793,18299,1,17908,2049,3931,10,
1,18294,9,10,1,18289,2049,2862,1,410,1,15,2049,64,10,17871,18322,178,18446,5861507,
47,47,0,2049,15624,1,17908,2049,3916,10,18314,18341,166,18446,-208531877,98,97,110,110,101,
114,0,2049,17940,2049,4472,82,69,84,82,79,32,49,50,32,40,37,110,46,37,
110,41,92,110,0,1,18345,2049,8246,2049,10761,2049,10837,2049,1545,2049,10837,18,2049,1545,
2049,4472,37,110,32,77,97,120,44,32,37,110,32,85,115,101,100,44,32,37,
110,32,70,114,101,101,92,110,0,1,18381,2049,8246,2049,10761,10,18329,18427,166,18446,
192495636,108,105,115,116,101,110,0,3841,17883,1793,18434,2049,18341,10,1,18431,2049,72,2049,
18267,2049,18285,1,18438,7,10,105,110,116,101,114,102,97,99,101,47,114,101,116,
114,111,45,117,110,105,120,46,114,101,116,114,111,0,18415,18486,154,0,0,83,
111,117,114,99,101,115,0,1,18725,0,0,0,0,0,0,0,0,0,0,0,
84,79,82,69,0,17553,3,154,0,0,70,69,84,67,72,0,17564,4,154,0,
0,82,69,83,73,90,69,0,17484,17602,166,0,369169257,109,101,109,58,97,108,108,
111,99,0,1,0,2049,17525,10,17587,17620,166,0,-1111837014,109,101,109,58,115,101,116,
0,1,2,2049,17525,10,17607,17638,166,0,-1111850082,109,101,109,58,103,101,116,0,1,
3,2049,17525,10,17625,17657,166,0,1963630784,109,101,109,58,102,114,101,101,0,1,1,
2049,17525,10,17643,17678,166,0,-45070288,109,101,109,58,114,101,115,105,122,101,0,1,
4,2049,17525,10,17662,17692,166,18479,193470948,84,73,66,0,1,7,15,10,17683,17712,166,
18479,-460132687,105,109,97,103,101,58,115,97,118,101,0,1,1000,2049,10655,2049,10636,10,
17696,17730,166,0,0,101,100,105,116,63,0,2,1793,17737,1,8,11,10,1,17733,
1793,17745,1,127,11,10,1,17741,2049,2255,22,10,17719,17763,166,0,0,103,97,116,
104,101,114,0,2049,17730,1,15,1,4134,2049,64,10,17751,17783,166,0,0,99,121,
99,108,101,0,2049,10985,2049,2217,4,8,2049,2644,25,3,2049,17763,1,17783,7,10,
17696,17816,166,18479,-1658019367,112,97,114,115,101,45,117,110,116,105,108,0,1793,17828,2049,
4451,2049,4234,2049,17783,771,2049,4096,10,1,17818,2049,4260,10,17799,17844,166,18479,272733106,115,
58,103,101,116,0,1793,17866,1793,17852,1,13,11,10,1,17848,1793,17860,1,10,11,
10,1,17856,2049,2255,22,10,1,17846,2049,17816,10,17833,17882,166,18479,255552908,99,108,101,
97,114,0,2049,4472,92,94,91,50,74,92,94,91,48,59,48,72,0,1,17884,
2049,8246,2049,10761,10,17871,17916,154,18479,-976122143,78,111,69,99,104,111,0,0,17904,17926,
154,0,0,69,79,84,0,0,17917,17941,154,0,0,73,103,110,111,114,105,110,
103,0,0,17927,17957,166,0,0,105,103,110,111,114,105,110,103,63,0,3841,17941,
10,17942,17973,166,0,0,118,101,114,115,105,111,110,0,3841,4,1,100,20,10,
17960,17990,166,0,0,100,111,110,101,63,0,2,4097,17926,1793,17999,1,13,11,10,
1,17995,1793,18007,1,10,11,10,1,18003,1793,18015,1,32,11,10,1,18011,2049,2298,
22,22,10,17979,18032,166,0,0,101,111,108,63,0,3841,17926,1793,18040,1,13,11,
10,1,18036,1793,18048,1,10,11,10,1,18044,2049,2255,22,10,18022,18066,166,0,0,
118,97,108,105,100,63,0,2,2049,102,2049,2812,10,18054,18087,166,0,0,99,104,
101,99,107,45,101,111,102,0,2,1793,18094,1,-1,11,10,1,18090,1793,18102,1,
4,11,10,1,18098,2049,2255,22,1793,18112,2049,11052,10,1,18109,9,10,18072,18124,166,
0,0,98,115,0,2049,4212,1,2,2049,2675,1793,18136,2049,4160,3,10,1,18132,9,
2049,4160,3,10,18116,18157,166,0,0,99,104,101,99,107,45,98,115,0,2,1793,
18164,1,8,11,10,1,18160,1793,18172,1,127,11,10,1,18168,2049,2255,22,1793,18182,
2049,18124,10,1,18179,9,10,18143,18197,166,0,0,99,104,101,99,107,0,2049,18087,
2049,18157,10,18186,18217,166,0,0,99,104,97,114,97,99,116,101,114,0,2049,10985,
2,2049,4134,10,18202,18235,166,0,0,98,117,102,102,101,114,0,1793,18245,2049,17692,
2049,4234,8,2049,4096,10,1,18237,2049,4260,10,18223,18266,166,0,0,114,101,97,100,
45,116,111,107,101,110,0,1793,18282,1793,18277,2049,18217,2049,18197,2049,17990,10,1,18270,
2049,2397,10,1,18268,2049,18235,2049,4587,10,18250,18300,166,0,0,105,110,112,117,116,
0,2049,18266,2049,18066,10,18289,18318,166,0,0,112,114,111,99,101,115,115,0,2049,
17957,1793,18336,771,2049,18032,1793,18332,1,17941,2049,3931,10,1,18327,9,10,1,18322,2049,
2862,1,410,1,15,2049,64,10,17904,18355,178,18479,5861507,47,47,0,2049,15624,1,17941,
2049,3916,10,18347,18374,166,18479,-208531877,98,97,110,110,101,114,0,2049,17973,2049,4472,82,
69,84,82,79,32,49,50,32,40,37,110,46,37,110,41,92,110,0,1,18378,
2049,8246,2049,10761,2049,10837,2049,1545,2049,10837,18,2049,1545,2049,4472,37,110,32,77,97,
120,44,32,37,110,32,85,115,101,100,44,32,37,110,32,70,114,101,101,92,
110,0,1,18414,2049,8246,2049,10761,10,18362,18460,166,18479,192495636,108,105,115,116,101,110,
0,3841,17916,1793,18467,2049,18374,10,1,18464,2049,72,2049,18300,2049,18318,1,18471,7,10,
105,110,116,101,114,102,97,99,101,47,114,101,116,114,111,45,117,110,105,120,
46,114,101,116,114,111,0,18448,18519,154,0,0,83,111,117,114,99,101,115,0,
1,18758,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,18473,18626,166,0,0,
107,110,111,119,110,63,0,2,1,18486,2049,9003,10,18614,18643,166,0,0,105,110,
100,101,120,0,1,18486,4,2049,9320,1,18486,4,2049,9140,10,18632,18666,166,0,0,
114,101,99,111,114,100,0,2049,4500,2,1,18486,2049,3864,3841,18486,1,18486,17,16,
10,1793,18723,2049,15559,2049,18626,1793,18691,2049,18643,10,1,18688,1793,18698,2049,18666,10,1,
18695,2049,64,1793,18711,1,190,1,2,17,8,10,1,18704,2049,2229,2049,1576,2049,184,
16,2049,11116,10,1,18682,100,105,99,116,45,119,111,114,100,115,45,108,105,115,
116,105,110,103,46,102,111,114,116,104,0,18415,18763,166,18725,-1519205998,100,58,119,111,
114,100,115,0,1793,18772,2049,188,2049,10761,2049,10731,10,1,18765,2049,8491,10,18750,18795,
166,18725,1750461083,100,58,119,111,114,100,115,45,119,105,116,104,0,2049,1977,2049,5573,
1793,18826,2049,188,2,2049,1977,2049,5286,1793,18815,2049,10761,2049,10731,10,1,18810,1793,18821,
3,10,1,18819,2049,64,10,1,18801,2049,8491,10,18777,18852,166,18725,-1509196721,100,105,115,
112,108,97,121,45,105,102,45,108,101,102,116,0,2,2049,1977,2049,5519,1793,18864,
2049,10761,2049,10731,10,1,18859,1793,18870,3,10,1,18868,2049,64,10,18777,18903,166,18725,
-1159574567,100,58,119,111,114,100,115,45,98,101,103,105,110,110,105,110,103,45,119,
105,116,104,0,2049,1977,2049,5573,1793,18914,2049,188,2049,18852,10,1,18909,2049,8491,10,
0 };
0,0,0,0,0,0,0,0,18506,18659,166,0,0,107,110,111,119,110,63,0,
2,1,18519,2049,9003,10,18647,18676,166,0,0,105,110,100,101,120,0,1,18519,4,
2049,9320,1,18519,4,2049,9140,10,18665,18699,166,0,0,114,101,99,111,114,100,0,
2049,4500,2,1,18519,2049,3864,3841,18519,1,18519,17,16,10,1793,18756,2049,15559,2049,18659,
1793,18724,2049,18676,10,1,18721,1793,18731,2049,18699,10,1,18728,2049,64,1793,18744,1,190,
1,2,17,8,10,1,18737,2049,2229,2049,1576,2049,184,16,2049,11116,10,1,18715,100,
105,99,116,45,119,111,114,100,115,45,108,105,115,116,105,110,103,46,102,111,
114,116,104,0,18448,18796,166,18758,-1519205998,100,58,119,111,114,100,115,0,1793,18805,2049,
188,2049,10761,2049,10731,10,1,18798,2049,8491,10,18783,18828,166,18758,1750461083,100,58,119,111,
114,100,115,45,119,105,116,104,0,2049,1977,2049,5573,1793,18859,2049,188,2,2049,1977,
2049,5286,1793,18848,2049,10761,2049,10731,10,1,18843,1793,18854,3,10,1,18852,2049,64,10,
1,18834,2049,8491,10,18810,18885,166,18758,-1509196721,100,105,115,112,108,97,121,45,105,102,
45,108,101,102,116,0,2,2049,1977,2049,5519,1793,18897,2049,10761,2049,10731,10,1,18892,
1793,18903,3,10,1,18901,2049,64,10,18810,18936,166,18758,-1159574567,100,58,119,111,114,100,
115,45,98,101,103,105,110,110,105,110,103,45,119,105,116,104,0,2049,1977,2049,
5573,1793,18947,2049,188,2049,18885,10,1,18942,2049,8491,10,0 };

View file

@ -268,8 +268,17 @@ void malloc_fetch(NgaState *vm) {
stack_push(vm, value);
}
// TODO: realloc() support
// void malloc_realloc(NgaState *vm) { }
void malloc_realloc(NgaState *vm) {
CELL bytes = stack_pop(vm);
double_cell addr1;
addr1.lsw = stack_pop(vm);
addr1.msw = stack_pop(vm);
double_cell addr2;
addr2.val = realloc(addr1.val, bytes);
stack_push(vm, addr2.msw);
stack_push(vm, addr2.lsw);
}
void query_malloc(NgaState *vm) {
stack_push(vm, 0);
@ -283,6 +292,7 @@ void io_malloc(NgaState *vm) {
case 1: malloc_free(vm); return;
case 2: malloc_store(vm); return;
case 3: malloc_fetch(vm); return;
case 4: malloc_realloc(vm); return;
}
stack_push(vm, -1);
}