Skip to content

Commit

Permalink
Reduce memory allocation by packing strings and auto len calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
GTANAdam committed Apr 14, 2019
1 parent 57de0e5 commit e1a9824
Show file tree
Hide file tree
Showing 13 changed files with 3,099 additions and 373 deletions.
366 changes: 183 additions & 183 deletions RagePawn/RagePawn.vcxproj

Large diffs are not rendered by default.

11 changes: 5 additions & 6 deletions RagePawn/format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@
#include <tchar.h>
#include <cassert>

// native format(output[], size = sizeof output, const format[], ...);
// native format(output[], const format[], ...);
cell AMX_NATIVE_CALL n_format(AMX *amx, const cell *params)
{
strdata info;
info.params = params + 4;
info.numparams = (int)(params[0] / sizeof(cell) - 3);
info.params = params + 3;
info.numparams = (int)(params[0] / sizeof(cell) - 2);
info.skip = 0;
info.length = (int)params[2];

cell* cstra = amx_Address(amx, params[3]);
cell* cstra = amx_Address(amx, params[2]);
const auto result = get_str(amx, cstra, &info) + '\0';

SET_STRING(result.c_str(), params[1], params[2]);
SET_STRING(result.c_str(), params[1]);
return true;
}

Expand Down
6 changes: 3 additions & 3 deletions RagePawn/main.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
#define NATIVE(name) static cell AMX_NATIVE_CALL name(AMX *amx, const cell *params)
// char*: output
#define CHECK_PARAMS(n) { if (params[0] != (n * sizeof(cell))) { printf("Invalid parameter count (count: %d, valid: %d): ", params[0] / sizeof(cell), n); return 0; } }
#define GET_STRING_EX(input, output) cell* address = amx_Address(amx, input); amx_GetString(output, address, 0, sizeof output)
#define SET_STRING(input, destination, length) cell* addr = amx_Address(amx, destination); amx_SetString(addr, input, 0, 0, (int)length)
#define GET_STRING(amx,param,result) do { cell *amx_cstr_; int amx_length_; amx_cstr_ = amx_Address((amx), (param)); amx_StrLen(amx_cstr_, &amx_length_); if (amx_length_ > 0 && ((result) = (char*)alloca((amx_length_ + 1) * sizeof(*(result)))) != NULL) amx_GetString((char*)(result), amx_cstr_, sizeof(*(result))>1, amx_length_ + 1); else (result) = NULL; } while (0)
#define GET_STRING_EX(input, output) cell* addr = amx_Address(amx, input); amx_GetString(output, addr, 0, sizeof output)
#define SET_STRING(input, destination) cell* addr = amx_Address(amx, destination); amx_SetString(addr, input, 1, 0, strlen(input) + 1)
#define GET_STRING(amx, param, result) do { cell *amx_cstr_; int amx_length_; amx_cstr_ = amx_Address((amx), (param)); amx_StrLen(amx_cstr_, &amx_length_); if (amx_length_ > 0 && ((result) = (char*)alloca((amx_length_ + 1) * sizeof(*(result)))) != NULL) amx_GetString((char*)(result), amx_cstr_, sizeof(*(result))>1, amx_length_ + 1); else (result) = NULL; } while (0)
4 changes: 2 additions & 2 deletions RagePawn/player_natives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
#include "api.hpp"
#include "xxHash_xxhash.hpp"

// native GetPlayerName(playerid, name[], len);
// native GetPlayerName(playerid, name[]);
NATIVE (n_GetPlayerName)
{
HAS_PLAYER(player, params[1])
{
SET_STRING(player->GetName().c_str(), params[2], params[3]);
SET_STRING(player->GetName().c_str(), params[2]);
return true;
}
return false;
Expand Down
358 changes: 179 additions & 179 deletions amxlib/amxlib.vcxproj

Large diffs are not rendered by default.

Loading

0 comments on commit e1a9824

Please sign in to comment.