Posts: 1,046
Threads: 29
Joined: Mar 2010
16.07.2013, 22:48
(
Last edited by BigETI; 23/11/2017 at 03:21 PM.
Reason: Obsolete release
)
This plugin allows you to allocate dynamic memory, use it, and free it after you don't want to use it anymore.
Why?
PAWN literally doesn't allow you to manage dynamic memory at all. But still there has been releases such as for example y_malloc, which allows you to use dynamic memory in your PAWN scripts.
Speed comparison between y_malloc and this plugin can be found here:
Allow your scripts to shrink their AMX sizes, by using dynamic memory.
Documentation
Setup
Put the plugin binary into your server's "plugins" folder and set in server.cfg
Windows:
LINUX:
After that you can use the memory include in your scripts:
pawn Code:
// On top of your script
#include <memory>
// your code
Compile and run.
Alternatively you can use the secure version (which is slower by a fraction):
Windows:
Code:
plugins memory_secure
LINUX:
Code:
plugins memory_secure.so
Of course you can use the memory include in your scripts:
pawn Code:
// On top of your script
#define SECURE_MEMORY_PLUGIN
#include <memory>
// your code
Compile and run.
Downloads
Changelog
Quote:
- v1.1.1 -> Code optimization
- v1.1 -> Added MEM_copy(), MEM_zero(), MEM_E_res (secure only), MEM_is() (secure only), MEM_len() (secure only), and MEM_result() (secure only) ( 17.10.2013 )
- v1.0.01 -> Updated SDK and changed the includes ( 17.07.2013 )
- v1.0 -> Initial release ( 17.07.2013 )
|
Credits
If you are interested to compile this for Linux distributions, just let me know by personal messaging me and leave me links to download the compiled binar(y|ies).
You'll be added into the credits, of course.
Quote:
- BigETI for the source code and compiled binaries for Windows distributions and Ubuntu 14.01 LTS
- Bluescreen for compiling the plugins for Debian 7 Wheezy
- SA:MP development team
- Y_Less for the speed comparison idea
|
Thanks for their previous support:
Quote:
- leonardo1434
- Josstaa
- Mellnik
- Tenshi
- Mark™
|
Best Regards
~ BigETI
Posts: 2,169
Threads: 206
Joined: Jul 2010
Reputation:
0
AH I've been wanting something like this for years!
Posts: 2,929
Threads: 160
Joined: Feb 2009
Reputation:
0
Nice release - I think I might even use this soon!
Posts: 1,391
Threads: 85
Joined: Sep 2012
Reputation:
0
Great release. I'll be using this soon!
Posts: 1,046
Threads: 29
Joined: Mar 2010
I appreciate all of your comments related to this release.
As a mod actually deleted this whole thread earlier, I have to say that THIS plugin is not going to hack memory of your servers. By deleting this post means to handle releases such as for example y_malloc as an evil hacking tool. y_malloc gives you the same functionalities, but this plugin might be slightly faster ( see speed comparison )
@leonardo1434 Did it compile without editing you anything inside the source code itself?
I'll add your binary once I am online at my computer again, of course.
Posts: 2,187
Threads: 81
Joined: Aug 2011
Reputation:
0
Nice! I would love to try and create a linked list using this! I can think of so many applications of that in the SA-MP world.
Posts: 2,203
Threads: 154
Joined: Oct 2009
Reputation:
0
Can someone give example on handling multidimensional arrays?
Posts: 1,046
Threads: 29
Joined: Mar 2010
Example (can be done better):
pawn Код:
// Get value from "m_arr"
#define g_mArr(%0,%1) (MEM_get_val(m_arr,(m_arr_w*(%0))+(%1)))
// Set value at "m_arr"
#define s_mArr(%0,%1,%2) (MEM_set_val(m_arr,(m_arr_w*(%0))+(%1)),(%2))
// Declarations
new Pointer:m_arr = Pointer:NULL;
new m_arr_h, m_arr_w;
// set "m_arr_h" and "m_arr_w" any number you like (except negative values!)
// Initialize somewhere (example OnGameModeInit())
m_arr = MEM::calloc(m_arr_w*m_arr_h);
if(m_arr)
{
// Check, if valid
}
// ...
// Free after not used anymore (example OnGameModeExit())
MEM::free(m_arr);
pawn Код:
// Init
m_arr_h = 20;
m_arr_w = 50;
m_arr = MEM::calloc(m_arr_h*m_arr_w);
if(m_arr)
{
// Check, if valid
}
// ...
// Set at index 19*50+47, also called [19][47] "100"
s_mArr(19, 47, 100);
// Set at index 19*50+48, also called [19][48] "200"
s_mArr(19, 48, 200);
// Set at index 19*50+49, also called [19][49] "300"
s_mArr(19, 49, 300);
// Print
new i = 19, j = 47;
printf("[%d][%d]: %d", i, j, g_mArr(i, j));
j++;
printf("[%d][%d]: %d", i, j, g_mArr(i, j));
j++;
printf("[%d][%d]: %d", i, j, g_mArr(i, j));
//...
// somewhere else...
// End
MEM::free(m_arr);
prints
Код:
[19][47]: 100
[19][48]: 200
[19][49]: 300
Posts: 2,203
Threads: 154
Joined: Oct 2009
Reputation:
0
Thx, example is perfect.
EDIT: By the way, what about expanding arrays? Lets say for example i have allocated [20][20] and i need to expand my array to [30][20]... (If im not wrong than everything will get messed up...so i must allocate new memory and copy everything there?)
Sorry but im just more used to calloc in c/++ as thats what i learned first.
Posts: 1,046
Threads: 29
Joined: Mar 2010
Just use MEM::realloc(), as it behaves like
this.
Posts: 2,203
Threads: 154
Joined: Oct 2009
Reputation:
0
Thx, i just was afraid that memory addresses would mess up due to w*h but just realized it will be fine.
Posts: 1,046
Threads: 29
Joined: Mar 2010
I wrote an update (mainly to the secure version) and some new speed comparisons with y_malloc. Accesses to single values with this plugin might take a bit longer (just a fraction), but it's much better with copying large arrays. I'll surely post the results and the announced update, if I am back at home again.
Posts: 1,046
Threads: 29
Joined: Mar 2010
Just don't use MEM::free(), if you still need and use that memory.
Also check for NULL pointers after MEM::malloc() or MEM::calloc().
If you don't want to corrupt your memory, you can use the secure version.
Anyway I've successfully posted an update of these plugins.