[Plugin] Memory access plugin
#1

memory.dll (Win32) / memory.so (Ubuntu 14.01 LTS, Debian 7 Wheezy) / memory.inc
Memory access plugin
The predecessor of https://sampforum.blast.hk/showthread.php?tid=645166



About
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:

Code:
plugins memory
LINUX:
Code:
plugins memory.so
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
Reply
#2

AH I've been wanting something like this for years!
Reply
#3

Nice release - I think I might even use this soon!
Reply
#4

Great release. I'll be using this soon!
Reply
#5

Great release.
Reply
#6

Looks useful. Good job.
Reply
#7

Nice release. I haven't look into the code well, quickly view. Anyway, i've compiled the Linux version for Ubuntu , lastest version, 32 bits. Also, i didn't made any test(just opened the SAMP-Server to check out), therefore, if any errors pops up, tell me and i'll work for a fix.

http://www.solidfiles.com/d/df11603217/
Reply
#8

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.
Reply
#9

Quote:
Originally Posted by BigETI
View Post
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.
No exactly, i had to fix the includes. But, the main code is the same, since i didn't found any windows specific features.

@Small fix at your context.

Quote:

You have just lost the reference to the dynamicly allocated memory. This memory still exists, but it becomes totally useless, unless you've restarted your machine manually.

It's a little bit wrong, After the SAMP-Server is closed the OS itself will claim all the memory back. All it can lead is a memory leak.
Reply
#10

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.
Reply
#11

Can someone give example on handling multidimensional arrays?
Reply
#12

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
Reply
#13

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.
Reply
#14

Just use MEM::realloc(), as it behaves like this.
Reply
#15

Thx, i just was afraid that memory addresses would mess up due to w*h but just realized it will be fine.
Reply
#16

nice
Reply
#17

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.
Reply
#18

I've tried doing something like this.. didn't really work well:
It does set the value to "Test" but when I try to set it in index 5 specifically it resets all indexes to "Test 5" and when I set it to "None" on player connect it displays as "|ne"
pawn Код:
MEM::struct e_TEXT
{
    Message[20]
};
new Pointer:Texts[MAX_PLAYERS][10];

public OnPlayerConnect(playerid)
{
    MEM_MACR_foreach(10, i)
    {
        Texts[playerid][i] = MEM::calloc(e_TEXT);
        MEM::set_arr(Texts[playerid][i], Message, "None", 5);
        MEM::free(Texts[playerid][i]);
    }
    return 1;
}

CMD:add(playerid, params[])
{
    MEM_MACR_foreach(10, i)
    {
        MEM::set_arr(Texts[playerid][i], Message, "Test", 5);
        MEM::free(Texts[playerid][i]);
    }
    MEM::set_arr(Texts[playerid][5], Message, "Test 5", 8);
    MEM::free(Texts[playerid][5]);
    return 1;
}

CMD:show(playerid, params[])
{
    new text[20];
    MEM_MACR_foreach(10, i)
    {
        MEM::get_arr(Texts[playerid][i], Message, text, 20);
        SCMEx(playerid, -1, "Text in slot %d: %s", i, text);
        MEM::free(Texts[playerid][i]);
    }
    return 1;
}
I got it to work, nevermind. Had to remove MEM free.
Reply
#19

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.
Reply
#20

Quote:
Originally Posted by BigETI
Посмотреть сообщение
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.
Is there a way to set a pointer to NULL?
And I don't quite understand the difference between calloc() and malloc(), I used calloc and it was perfectly fine but when I used malloc() the data was somewhat corrupted.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)