Lightning-fast memcpy, malloc, and free implementations using opcodes
Author |
Message |
Foundry
|
Posted: Mon Apr 18, 2016 7:08 pm Post subject: Lightning-fast memcpy, malloc, and free implementations using opcodes |
|
|
I want to share a small memory manipulation library that I've ended up using in several Turing projects of mine. It replicates the standard memory copying, allocation, and freeing functions found in C by using specially constructed Turing functions to call the equivalent memcpy, malloc, and free C functions through the interpreter. Because of this, the code provided is significantly faster than any other implementation of this functionality done purely in Turing. Ridiculously faster.
The memory library itself relies on another small library to assist in dealing with the Turing interpreter's opcode schema, which in turn was pulled from a larger project of mine designed to bring some reflection capabilities to Turing.
All that being said, enjoy. I'd be willing to explain a bit more about how and why the code works if people are interested.
Description: |
|
Download |
Filename: |
Files.zip |
Filesize: |
3.33 KB |
Downloaded: |
196 Time(s) |
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
Dreadnought
|
Posted: Tue Apr 19, 2016 12:31 am Post subject: Re: Lightning-fast memcpy, malloc, and free implementations using opcodes |
|
|
Neat stuff.
EDIT: Forget my question, I was corrupting memory until I remembered how big a byte is .
|
|
|
|
|
|
Foundry
|
Posted: Tue Apr 19, 2016 10:17 am Post subject: Re: Lightning-fast memcpy, malloc, and free implementations using opcodes |
|
|
Dreadnought @ Tue Apr 19, 2016 12:31 am wrote: Neat stuff. However, I don't can't seem tofigure out how to use my "alloced" memory without crashing. For example
Turing: | import Memory
proc memWrite (ptr : addressint, dat : array 0 .. * of nat4)
for i : 0 .. upper (dat )
nat4@ (ptr + i * sizeof (nat4)) := dat (i )
end for
end memWrite
const SIZE := 2
var dummy : array 0 .. SIZE - 1 of nat4 := init (0, 0)
var data : addressint := Memory.Alloc (SIZE )
memWrite (data, dummy ) % comment this out to prevent crash
Memory.Free (data ) |
I crashes when I try to free the memory (I've probably gone and corrupted stuff). Any ideas?
You're writing past the region in memory that you have allocated.
When you call Memory.Alloc (SIZE), you are giving yourself 2 bytes to worth with. Your dummy array consists of two 4-byte unsigned ints, which means that you end up writing 6 bytes past the end of your region of memory. If you allocate 8 bytes (or sizeof(nat4) * (upper(dummy) + 1)) instead of 2, the problem is solved.
|
|
|
|
|
|
Nathan4102
|
Posted: Tue Apr 19, 2016 10:38 am Post subject: RE:Lightning-fast memcpy, malloc, and free implementations using opcodes |
|
|
I didn't know Turing had such low level functions. Interesting
|
|
|
|
|
|
|
|