From 014e23323d6dc79605da8ebad1c357176ede9122 Mon Sep 17 00:00:00 2001 From: MikalaiR Date: Mon, 1 Feb 2016 12:58:04 +0000 Subject: MirLua: Code cleanup git-svn-id: http://svn.miranda-ng.org/main/trunk@16206 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- plugins/MirLua/src/m_database.cpp | 9 +++------ plugins/MirLua/src/mlua_metatable.h | 4 ++-- plugins/MirLua/src/stdafx.h | 8 ++++++++ 3 files changed, 13 insertions(+), 8 deletions(-) (limited to 'plugins/MirLua') diff --git a/plugins/MirLua/src/m_database.cpp b/plugins/MirLua/src/m_database.cpp index a8cb78f435..82d4fe6aeb 100644 --- a/plugins/MirLua/src/m_database.cpp +++ b/plugins/MirLua/src/m_database.cpp @@ -182,8 +182,7 @@ static int array__call(lua_State *L) BLOB *blob = (BLOB*)lua_newuserdata(L, sizeof(BLOB)); blob->cbSize = size; - blob->pBlobData = (BYTE*)mir_alloc(sizeof(BYTE) * blob->cbSize); - memcpy(blob->pBlobData, udata, sizeof(BYTE)* blob->cbSize); + blob->pBlobData = mir_memdup(udata, size); luaL_setmetatable(L, MT_BLOB); return 1; @@ -291,8 +290,7 @@ static int lua_GetSetting(lua_State *L) { BLOB *blob = (BLOB*)lua_newuserdata(L, sizeof(BLOB)); blob->cbSize = dbv.cpbVal; - blob->pBlobData = (BYTE*)mir_alloc(sizeof(BYTE) * blob->cbSize); - memcpy(blob->pBlobData, dbv.pbVal, sizeof(BYTE) * blob->cbSize); + blob->pBlobData = mir_memdup(dbv.pbVal, dbv.cpbVal); luaL_setmetatable(L, MT_BLOB); } break; @@ -542,8 +540,7 @@ static int dbcw__index(lua_State *L) { BLOB *blob = (BLOB*)lua_newuserdata(L, sizeof(BLOB)); blob->cbSize = dbcw->value.cpbVal; - blob->pBlobData = (BYTE*)mir_alloc(sizeof(BYTE) * blob->cbSize); - memcpy(blob->pBlobData, dbcw->value.pbVal, sizeof(BYTE) * blob->cbSize); + blob->pBlobData = mir_memdup(dbcw->value.pbVal, blob->cbSize); luaL_setmetatable(L, MT_BLOB); } break; diff --git a/plugins/MirLua/src/mlua_metatable.h b/plugins/MirLua/src/mlua_metatable.h index 372f8684a3..ec5ee58451 100644 --- a/plugins/MirLua/src/mlua_metatable.h +++ b/plugins/MirLua/src/mlua_metatable.h @@ -111,7 +111,7 @@ private: lua_pushstring(L, ptrA(mir_utf8encode(field->GetValue(obj)))); break; case LUA_TSTRINGW: - lua_pushstring(L, ptrA(mir_utf8encodeW(field->GetValue(obj)))); + lua_pushstring(L, T2Utf(field->GetValue(obj))); break; case LUA_TLIGHTUSERDATA: lua_pushlightuserdata(L, field->GetValue(obj)); @@ -150,7 +150,7 @@ public: template MT& Field(R T::*M, const char *name, int type, size_t size = sizeof(R)) { - size_t offset = reinterpret_cast(&(((T*)0)->*M)); + size_t offset = offsetof(T, *M); if (type != LUA_TNONE) fields[name] = new MTField(offset, size, type); return *this; diff --git a/plugins/MirLua/src/stdafx.h b/plugins/MirLua/src/stdafx.h index efefd020ad..90e60660d8 100644 --- a/plugins/MirLua/src/stdafx.h +++ b/plugins/MirLua/src/stdafx.h @@ -47,6 +47,14 @@ struct luaM_const intptr_t value; }; +template +T *mir_memdup(T* ptr, size_t size = sizeof(T), bool bCalloc = false) +{ + void *newptr = (bCalloc ? mir_calloc : mir_alloc)(size); + memcpy(newptr, ptr, size); + return (T*)newptr; +} + #include "mlua.h" #include "mlua_script.h" #include "mlua_module_loader.h" -- cgit v1.2.3